From 7081f7799b5de8bfe68ebadf52a37cf4b022ee98 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 23 Sep 2014 22:31:45 +0800 Subject: [PATCH] Separate the archive cache out to ArchiveFactory. --- atom.gyp | 2 ++ .../browser/net/asar/asar_protocol_handler.cc | 16 +++------- atom/browser/net/asar/asar_protocol_handler.h | 9 ++---- atom/common/asar/archive_factory.cc | 29 +++++++++++++++++ atom/common/asar/archive_factory.h | 31 +++++++++++++++++++ 5 files changed, 69 insertions(+), 18 deletions(-) create mode 100644 atom/common/asar/archive_factory.cc create mode 100644 atom/common/asar/archive_factory.h diff --git a/atom.gyp b/atom.gyp index 8a47a02a5885..46960695c324 100644 --- a/atom.gyp +++ b/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', diff --git a/atom/browser/net/asar/asar_protocol_handler.cc b/atom/browser/net/asar/asar_protocol_handler.cc index 69df5367b9dc..2688d51ada00 100644 --- a/atom/browser/net/asar/asar_protocol_handler.cc +++ b/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(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_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_); } diff --git a/atom/browser/net/asar/asar_protocol_handler.h b/atom/browser/net/asar/asar_protocol_handler.h index 24d8cc991380..4a728b62262e 100644 --- a/atom/browser/net/asar/asar_protocol_handler.h +++ b/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 file_task_runner_; - mutable base::hash_map > archives_; + mutable ArchiveFactory archive_factory_; DISALLOW_COPY_AND_ASSIGN(AsarProtocolHandler); }; diff --git a/atom/common/asar/archive_factory.cc b/atom/common/asar/archive_factory.cc new file mode 100644 index 000000000000..cf8bd2db28f0 --- /dev/null +++ b/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 ArchiveFactory::GetOrCreate(const base::FilePath& path) { + // Create a cache of Archive. + if (!ContainsKey(archives_, path)) { + scoped_refptr archive(new Archive(path)); + if (!archive->Init()) + return NULL; + archives_[path] = archive; + return archive; + } + + return archives_[path]; +} + +} // namespace asar diff --git a/atom/common/asar/archive_factory.h b/atom/common/asar/archive_factory.h new file mode 100644 index 000000000000..2f6700c6beed --- /dev/null +++ b/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 GetOrCreate(const base::FilePath& path); + + private: + base::hash_map > archives_; // NOLINT + + DISALLOW_COPY_AND_ASSIGN(ArchiveFactory); +}; + +} // namespace asar + +#endif // ATOM_COMMON_ASAR_ARCHIVE_FACTORY_H_