Separate the archive cache out to ArchiveFactory.

This commit is contained in:
Cheng Zhao 2014-09-23 22:31:45 +08:00
parent b6583635d4
commit 7081f7799b
5 changed files with 69 additions and 18 deletions

View file

@ -192,6 +192,8 @@
'atom/common/api/object_life_monitor.h', 'atom/common/api/object_life_monitor.h',
'atom/common/asar/archive.cc', 'atom/common/asar/archive.cc',
'atom/common/asar/archive.h', '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.cc',
'atom/common/common_message_generator.h', 'atom/common/common_message_generator.h',
'atom/common/crash_reporter/crash_reporter.cc', 'atom/common/crash_reporter/crash_reporter.cc',

View file

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

View file

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

View file

@ -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

View file

@ -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_