Browse Source

Just use plain pointer for weak reference.

Cheng Zhao 10 years ago
parent
commit
4006b6407c

+ 4 - 2
atom/browser/net/asar/asar_protocol_handler.cc

@@ -45,7 +45,9 @@ AsarProtocolHandler::AsarProtocolHandler(
     const scoped_refptr<base::TaskRunner>& file_task_runner)
     : file_task_runner_(file_task_runner) {}
 
-AsarProtocolHandler::~AsarProtocolHandler() {}
+AsarProtocolHandler::~AsarProtocolHandler() {
+  LOG(ERROR) << "~AsarProtocolHandler";
+}
 
 net::URLRequestJob* AsarProtocolHandler::MaybeCreateJob(
     net::URLRequest* request,
@@ -60,7 +62,7 @@ net::URLRequestJob* AsarProtocolHandler::MaybeCreateJob(
     return new net::URLRequestFileJob(request, network_delegate, full_path,
                                       file_task_runner_);
 
-  scoped_refptr<Archive> archive = archive_factory_.GetOrCreate(asar_path);
+  Archive* archive = archive_factory_.GetOrCreate(asar_path);
   if (!archive)
     return new net::URLRequestErrorJob(request, network_delegate,
                                        net::ERR_FILE_NOT_FOUND);

+ 1 - 0
atom/browser/net/asar/asar_protocol_handler.h

@@ -6,6 +6,7 @@
 #define ATOM_BROWSER_NET_ASAR_ASAR_PROTOCOL_HANDLER_H_
 
 #include "atom/common/asar/archive_factory.h"
+#include "base/memory/ref_counted.h"
 #include "net/url_request/url_request_job_factory.h"
 
 namespace base {

+ 2 - 2
atom/browser/net/asar/url_request_asar_job.cc

@@ -17,7 +17,7 @@ namespace asar {
 URLRequestAsarJob::URLRequestAsarJob(
     net::URLRequest* request,
     net::NetworkDelegate* network_delegate,
-    const scoped_refptr<Archive>& archive,
+    Archive* archive,
     const base::FilePath& file_path,
     const scoped_refptr<base::TaskRunner>& file_task_runner)
     : net::URLRequestJob(request, network_delegate),
@@ -31,7 +31,7 @@ URLRequestAsarJob::URLRequestAsarJob(
 URLRequestAsarJob::~URLRequestAsarJob() {}
 
 void URLRequestAsarJob::Start() {
-  if (!archive_->GetFileInfo(file_path_, &file_info_)) {
+  if (!archive_ || !archive_->GetFileInfo(file_path_, &file_info_)) {
     NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED,
                                      net::ERR_FILE_NOT_FOUND));
     return;

+ 2 - 2
atom/browser/net/asar/url_request_asar_job.h

@@ -27,7 +27,7 @@ class URLRequestAsarJob : public net::URLRequestJob {
  public:
   URLRequestAsarJob(net::URLRequest* request,
                     net::NetworkDelegate* network_delegate,
-                    const scoped_refptr<Archive>& archive,
+                    Archive* archive,
                     const base::FilePath& file_path,
                     const scoped_refptr<base::TaskRunner>& file_task_runner);
 
@@ -53,7 +53,7 @@ class URLRequestAsarJob : public net::URLRequestJob {
   // Callback after data is asynchronously read from the file into |buf|.
   void DidRead(scoped_refptr<net::IOBuffer> buf, int result);
 
-  const scoped_refptr<Archive> archive_;
+  Archive* archive_;
   Archive::FileInfo file_info_;
   base::FilePath file_path_;
 

+ 7 - 7
atom/common/api/atom_api_asar.cc

@@ -21,21 +21,21 @@ class Archive : public mate::Wrappable {
   static v8::Handle<v8::Value> Create(v8::Isolate* isolate,
                                       const base::FilePath& path) {
     static asar::ArchiveFactory archive_factory;
-    scoped_refptr<asar::Archive> archive = archive_factory.GetOrCreate(path);
+    asar::Archive* archive = archive_factory.GetOrCreate(path);
     if (!archive)
       return v8::False(isolate);
     return (new Archive(archive))->GetWrapper(isolate);
   }
 
  protected:
-  explicit Archive(scoped_refptr<asar::Archive> archive) : archive_(archive) {}
+  explicit Archive(asar::Archive* archive) : archive_(archive) {}
   virtual ~Archive() {}
 
   // Reads the offset and size of file.
   v8::Handle<v8::Value> GetFileInfo(v8::Isolate* isolate,
                                     const base::FilePath& path) {
     asar::Archive::FileInfo info;
-    if (!archive_->GetFileInfo(path, &info))
+    if (!archive_ || !archive_->GetFileInfo(path, &info))
       return v8::False(isolate);
     mate::Dictionary dict(isolate, v8::Object::New(isolate));
     dict.Set("size", info.size);
@@ -47,7 +47,7 @@ class Archive : public mate::Wrappable {
   v8::Handle<v8::Value> Stat(v8::Isolate* isolate,
                              const base::FilePath& path) {
     asar::Archive::Stats stats;
-    if (!archive_->Stat(path, &stats))
+    if (!archive_ || !archive_->Stat(path, &stats))
       return v8::False(isolate);
     mate::Dictionary dict(isolate, v8::Object::New(isolate));
     dict.Set("size", stats.size);
@@ -62,7 +62,7 @@ class Archive : public mate::Wrappable {
   v8::Handle<v8::Value> Readdir(v8::Isolate* isolate,
                                 const base::FilePath& path) {
     std::vector<base::FilePath> files;
-    if (!archive_->Readdir(path, &files))
+    if (!archive_ || !archive_->Readdir(path, &files))
       return v8::False(isolate);
     return mate::ConvertToV8(isolate, files);
   }
@@ -71,7 +71,7 @@ class Archive : public mate::Wrappable {
   v8::Handle<v8::Value> CopyFileOut(v8::Isolate* isolate,
                                     const base::FilePath& path) {
     base::FilePath new_path;
-    if (!archive_->CopyFileOut(path, &new_path))
+    if (!archive_ || !archive_->CopyFileOut(path, &new_path))
       return v8::False(isolate);
     return mate::ConvertToV8(isolate, new_path);
   }
@@ -87,7 +87,7 @@ class Archive : public mate::Wrappable {
   }
 
  private:
-  scoped_refptr<asar::Archive> archive_;
+  asar::Archive* archive_;
 
   DISALLOW_COPY_AND_ASSIGN(Archive);
 };

+ 4 - 5
atom/common/asar/archive.cc

@@ -12,7 +12,6 @@
 #include "base/logging.h"
 #include "base/pickle.h"
 #include "base/json/json_string_value_serializer.h"
-#include "base/stl_util.h"
 #include "base/strings/string_number_conversions.h"
 
 namespace asar {
@@ -184,8 +183,8 @@ bool Archive::Readdir(const base::FilePath& path,
 }
 
 bool Archive::CopyFileOut(const base::FilePath& path, base::FilePath* out) {
-  if (ContainsKey(external_files_, path)) {
-    *out = external_files_[path]->path();
+  if (external_files_.contains(path)) {
+    *out = external_files_.get(path)->path();
     return true;
   }
 
@@ -193,12 +192,12 @@ bool Archive::CopyFileOut(const base::FilePath& path, base::FilePath* out) {
   if (!GetFileInfo(path, &info))
     return false;
 
-  scoped_refptr<ScopedTemporaryFile> temp_file(new ScopedTemporaryFile);
+  scoped_ptr<ScopedTemporaryFile> temp_file(new ScopedTemporaryFile);
   if (!temp_file->InitFromFile(path_, info.offset, info.size))
     return false;
 
-  external_files_[path] = temp_file;
   *out = temp_file->path();
+  external_files_.set(path, temp_file.Pass());
   return true;
 }
 

+ 4 - 8
atom/common/asar/archive.h

@@ -7,9 +7,8 @@
 
 #include <vector>
 
-#include "base/containers/hash_tables.h"
+#include "base/containers/scoped_ptr_hash_map.h"
 #include "base/files/file_path.h"
-#include "base/memory/ref_counted.h"
 #include "base/memory/scoped_ptr.h"
 
 namespace base {
@@ -22,7 +21,7 @@ class ScopedTemporaryFile;
 
 // This class represents an asar package, and provides methods to read
 // information from it.
-class Archive : public base::RefCounted<Archive> {
+class Archive {
  public:
   struct FileInfo {
     FileInfo() : size(0), offset(0) {}
@@ -38,6 +37,7 @@ class Archive : public base::RefCounted<Archive> {
   };
 
   explicit Archive(const base::FilePath& path);
+  virtual ~Archive();
 
   // Read and parse the header.
   bool Init();
@@ -58,16 +58,12 @@ class Archive : public base::RefCounted<Archive> {
   base::DictionaryValue* header() const { return header_.get(); }
 
  private:
-  friend class base::RefCounted<Archive>;
-  virtual ~Archive();
-
   base::FilePath path_;
   uint32 header_size_;
   scoped_ptr<base::DictionaryValue> header_;
 
   // Cached external temporary files.
-  base::hash_map<base::FilePath,  // NOLINT
-                 scoped_refptr<ScopedTemporaryFile> > external_files_;
+  base::ScopedPtrHashMap<base::FilePath, ScopedTemporaryFile> external_files_;
 
   DISALLOW_COPY_AND_ASSIGN(Archive);
 };

+ 9 - 10
atom/common/asar/archive_factory.cc

@@ -5,25 +5,24 @@
 #include "atom/common/asar/archive_factory.h"
 
 #include "atom/common/asar/archive.h"
-#include "base/stl_util.h"
 
 namespace asar {
 
 ArchiveFactory::ArchiveFactory() {}
 
-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));
+Archive* ArchiveFactory::GetOrCreate(const base::FilePath& path) {
+  if (!archives_.contains(path)) {
+    scoped_ptr<Archive> archive(new Archive(path));
     if (!archive->Init())
-      return NULL;
-    archives_[path] = archive;
-    return archive;
+      return nullptr;
+
+    archives_.set(path, archive.Pass());
   }
 
-  return archives_[path];
+  return archives_.get(path);
 }
 
 }  // namespace asar

+ 3 - 4
atom/common/asar/archive_factory.h

@@ -5,9 +5,8 @@
 #ifndef ATOM_COMMON_ASAR_ARCHIVE_FACTORY_H_
 #define ATOM_COMMON_ASAR_ARCHIVE_FACTORY_H_
 
-#include "base/containers/hash_tables.h"
+#include "base/containers/scoped_ptr_hash_map.h"
 #include "base/files/file_path.h"
-#include "base/memory/ref_counted.h"
 
 namespace asar {
 
@@ -18,10 +17,10 @@ class ArchiveFactory {
   ArchiveFactory();
   virtual ~ArchiveFactory();
 
-  scoped_refptr<Archive> GetOrCreate(const base::FilePath& path);
+  Archive* GetOrCreate(const base::FilePath& path);
 
  private:
-  base::hash_map<base::FilePath, scoped_refptr<Archive> > archives_;  // NOLINT
+  base::ScopedPtrHashMap<base::FilePath, Archive> archives_;
 
   DISALLOW_COPY_AND_ASSIGN(ArchiveFactory);
 };

+ 2 - 5
atom/common/asar/scoped_temporary_file.h

@@ -6,7 +6,6 @@
 #define ATOM_COMMON_ASAR_SCOPED_TEMPORARY_FILE_H_
 
 #include "base/files/file_path.h"
-#include "base/memory/ref_counted.h"
 
 namespace asar {
 
@@ -14,9 +13,10 @@ namespace asar {
 // object goes out of scope.  Note that since deletion occurs during the
 // destructor, no further error handling is possible if the directory fails to
 // be deleted.  As a result, deletion is not guaranteed by this class.
-class ScopedTemporaryFile : public base::RefCounted<ScopedTemporaryFile> {
+class ScopedTemporaryFile {
  public:
   ScopedTemporaryFile();
+  virtual ~ScopedTemporaryFile();
 
   // Init an empty temporary file.
   bool Init();
@@ -27,9 +27,6 @@ class ScopedTemporaryFile : public base::RefCounted<ScopedTemporaryFile> {
   base::FilePath path() const { return path_; }
 
  private:
-  friend class base::RefCounted<ScopedTemporaryFile>;
-  virtual ~ScopedTemporaryFile();
-
   base::FilePath path_;
 
   DISALLOW_COPY_AND_ASSIGN(ScopedTemporaryFile);