#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"
#include "net/url_request/url_request_file_job.h"
namespace asar {
base::FilePath full_path;
net::FileURLToFilePath(request->url(), &full_path);
- // Create asar:// job when the path contains "xxx.asar/".
+ // Create asar:// job when the path contains "xxx.asar/", otherwise treat the
+ // URL request as file://.
base::FilePath asar_path, relative_path;
- if (GetAsarPath(full_path, &asar_path, &relative_path))
- return new URLRequestAsarJob(request, network_delegate, asar_path,
- relative_path, file_task_runner_);
- else
+ if (!GetAsarPath(full_path, &asar_path, &relative_path))
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;
+ }
+
+ return new URLRequestAsarJob(request, network_delegate, archives_[asar_path],
+ relative_path, file_task_runner_);
}
bool AsarProtocolHandler::IsSafeRedirectTarget(const GURL& location) const {
#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 "net/url_request/url_request_job_factory.h"
namespace asar {
+class Archive;
+
class AsarProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler {
public:
explicit AsarProtocolHandler(
private:
const scoped_refptr<base::TaskRunner> file_task_runner_;
+ mutable base::hash_map<base::FilePath, // NOLINT
+ scoped_refptr<Archive> > archives_;
+
DISALLOW_COPY_AND_ASSIGN(AsarProtocolHandler);
};
URLRequestAsarJob::URLRequestAsarJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate,
- const base::FilePath& asar_path,
+ const scoped_refptr<Archive>& archive,
const base::FilePath& file_path,
const scoped_refptr<base::TaskRunner>& file_task_runner)
: net::URLRequestJob(request, network_delegate),
- archive_(asar_path),
+ archive_(archive),
file_path_(file_path),
stream_(new net::FileStream(file_task_runner)),
remaining_bytes_(0),
URLRequestAsarJob::~URLRequestAsarJob() {}
void URLRequestAsarJob::Start() {
- if (!archive_.Init() || !archive_.GetFileInfo(file_path_, &file_info_)) {
+ if (!archive_->GetFileInfo(file_path_, &file_info_)) {
NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED,
net::ERR_FILE_NOT_FOUND));
return;
int flags = base::File::FLAG_OPEN |
base::File::FLAG_READ |
base::File::FLAG_ASYNC;
- int rv = stream_->Open(archive_.path(), flags,
+ int rv = stream_->Open(archive_->path(), flags,
base::Bind(&URLRequestAsarJob::DidOpen,
weak_ptr_factory_.GetWeakPtr()));
if (rv != net::ERR_IO_PENDING)
public:
URLRequestAsarJob(net::URLRequest* request,
net::NetworkDelegate* network_delegate,
- const base::FilePath& asar_path,
+ const scoped_refptr<Archive>& archive,
const base::FilePath& file_path,
const scoped_refptr<base::TaskRunner>& file_task_runner);
// Callback after data is asynchronously read from the file into |buf|.
void DidRead(scoped_refptr<net::IOBuffer> buf, int result);
- Archive archive_;
+ const scoped_refptr<Archive> archive_;
Archive::FileInfo file_info_;
base::FilePath file_path_;
scoped_ptr<net::FileStream> stream_;
int64 remaining_bytes_;
+
const scoped_refptr<base::TaskRunner> file_task_runner_;
base::WeakPtrFactory<URLRequestAsarJob> weak_ptr_factory_;
#define ATOM_COMMON_ASAR_ARCHIVE_H_
#include "base/files/file_path.h"
+#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
namespace base {
namespace asar {
-class Archive {
+class Archive : public base::RefCounted<Archive> {
public:
struct FileInfo {
uint32 size;
};
explicit Archive(const base::FilePath& path);
- virtual ~Archive();
// Read and parse the header.
bool Init();
base::FilePath path() const { return path_; }
private:
+ friend class base::RefCounted<Archive>;
+ virtual ~Archive();
+
base::FilePath path_;
uint32 header_size_;
scoped_ptr<base::DictionaryValue> header_;