Browse Source

Separate the archive cache out to ArchiveFactory.

Cheng Zhao 10 years ago
parent
commit
7081f7799b

+ 2 - 0
atom.gyp

@@ -192,6 +192,8 @@
       'atom/common/api/object_life_monitor.h',
       'atom/common/asar/archive.cc',
       'atom/common/asar/archive.h',
+      'atom/common/asar/archive_factory.cc',
+      'atom/common/asar/archive_factory.h',
       'atom/common/common_message_generator.cc',
       'atom/common/common_message_generator.h',
       'atom/common/crash_reporter/crash_reporter.cc',

+ 5 - 11
atom/browser/net/asar/asar_protocol_handler.cc

@@ -5,8 +5,6 @@
 #include "atom/browser/net/asar/asar_protocol_handler.h"
 
 #include "atom/browser/net/asar/url_request_asar_job.h"
-#include "atom/common/asar/archive.h"
-#include "base/stl_util.h"
 #include "net/base/filename_util.h"
 #include "net/base/net_errors.h"
 #include "net/url_request/url_request_error_job.h"
@@ -62,16 +60,12 @@ net::URLRequestJob* AsarProtocolHandler::MaybeCreateJob(
     return new net::URLRequestFileJob(request, network_delegate, full_path,
                                       file_task_runner_);
 
-  // Create a cache of Archive.
-  if (!ContainsKey(archives_, asar_path)) {
-    scoped_refptr<Archive> archive(new Archive(asar_path));
-    if (!archive->Init())
-      return new net::URLRequestErrorJob(request, network_delegate,
-                                         net::ERR_FILE_NOT_FOUND);
-    archives_[asar_path] = archive;
-  }
+  scoped_refptr<Archive> archive = archive_factory_.GetOrCreate(asar_path);
+  if (!archive)
+    return new net::URLRequestErrorJob(request, network_delegate,
+                                       net::ERR_FILE_NOT_FOUND);
 
-  return new URLRequestAsarJob(request, network_delegate, archives_[asar_path],
+  return new URLRequestAsarJob(request, network_delegate, archive,
                                relative_path, file_task_runner_);
 }
 

+ 2 - 7
atom/browser/net/asar/asar_protocol_handler.h

@@ -5,9 +5,7 @@
 #ifndef ATOM_BROWSER_NET_ASAR_ASAR_PROTOCOL_HANDLER_H_
 #define ATOM_BROWSER_NET_ASAR_ASAR_PROTOCOL_HANDLER_H_
 
-#include "base/containers/hash_tables.h"
-#include "base/files/file_path.h"
-#include "base/memory/ref_counted.h"
+#include "atom/common/asar/archive_factory.h"
 #include "net/url_request/url_request_job_factory.h"
 
 namespace base {
@@ -16,8 +14,6 @@ class TaskRunner;
 
 namespace asar {
 
-class Archive;
-
 class AsarProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler {
  public:
   explicit AsarProtocolHandler(
@@ -33,8 +29,7 @@ class AsarProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler {
  private:
   const scoped_refptr<base::TaskRunner> file_task_runner_;
 
-  mutable base::hash_map<base::FilePath,  // NOLINT
-                         scoped_refptr<Archive> > archives_;
+  mutable ArchiveFactory archive_factory_;
 
   DISALLOW_COPY_AND_ASSIGN(AsarProtocolHandler);
 };

+ 29 - 0
atom/common/asar/archive_factory.cc

@@ -0,0 +1,29 @@
+// Copyright (c) 2014 GitHub, Inc. All rights reserved.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#include "atom/common/asar/archive_factory.h"
+
+#include "atom/common/asar/archive.h"
+#include "base/stl_util.h"
+
+namespace asar {
+
+ArchiveFactory::ArchiveFactory() {}
+
+ArchiveFactory::~ArchiveFactory() {}
+
+scoped_refptr<Archive> ArchiveFactory::GetOrCreate(const base::FilePath& path) {
+  // Create a cache of Archive.
+  if (!ContainsKey(archives_, path)) {
+    scoped_refptr<Archive> archive(new Archive(path));
+    if (!archive->Init())
+      return NULL;
+    archives_[path] = archive;
+    return archive;
+  }
+
+  return archives_[path];
+}
+
+}  // namespace asar

+ 31 - 0
atom/common/asar/archive_factory.h

@@ -0,0 +1,31 @@
+// Copyright (c) 2014 GitHub, Inc. All rights reserved.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#ifndef ATOM_COMMON_ASAR_ARCHIVE_FACTORY_H_
+#define ATOM_COMMON_ASAR_ARCHIVE_FACTORY_H_
+
+#include "base/containers/hash_tables.h"
+#include "base/files/file_path.h"
+#include "base/memory/ref_counted.h"
+
+namespace asar {
+
+class Archive;
+
+class ArchiveFactory {
+ public:
+  ArchiveFactory();
+  virtual ~ArchiveFactory();
+
+  scoped_refptr<Archive> GetOrCreate(const base::FilePath& path);
+
+ private:
+  base::hash_map<base::FilePath, scoped_refptr<Archive> > archives_;  // NOLINT
+
+  DISALLOW_COPY_AND_ASSIGN(ArchiveFactory);
+};
+
+}  // namespace asar
+
+#endif  // ATOM_COMMON_ASAR_ARCHIVE_FACTORY_H_