Just use plain pointer for weak reference.

This commit is contained in:
Cheng Zhao 2014-09-25 20:38:12 +08:00
parent c95a93ef1c
commit 4006b6407c
10 changed files with 38 additions and 45 deletions

View file

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

View file

@ -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);
};

View file

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

View file

@ -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);
};

View file

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