Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Side by Side Diff: net/disk_cache/cache_creator.cc

Issue 20737002: Change the API of disk_cache::CreateCacheBackend to use scoped_ptr (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix new test Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/disk_cache/backend_unittest.cc ('k') | net/disk_cache/disk_cache.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/files/file_path.h" 5 #include "base/files/file_path.h"
6 #include "base/metrics/field_trial.h" 6 #include "base/metrics/field_trial.h"
7 #include "base/strings/stringprintf.h" 7 #include "base/strings/stringprintf.h"
8 #include "net/base/cache_type.h" 8 #include "net/base/cache_type.h"
9 #include "net/base/net_errors.h" 9 #include "net/base/net_errors.h"
10 #include "net/disk_cache/backend_impl.h" 10 #include "net/disk_cache/backend_impl.h"
11 #include "net/disk_cache/cache_util.h" 11 #include "net/disk_cache/cache_util.h"
12 #include "net/disk_cache/disk_cache.h"
12 #include "net/disk_cache/mem_backend_impl.h" 13 #include "net/disk_cache/mem_backend_impl.h"
13 #include "net/disk_cache/simple/simple_backend_impl.h" 14 #include "net/disk_cache/simple/simple_backend_impl.h"
14 15
15 #ifdef USE_TRACING_CACHE_BACKEND 16 #ifdef USE_TRACING_CACHE_BACKEND
16 #include "net/disk_cache/tracing_cache_backend.h" 17 #include "net/disk_cache/tracing_cache_backend.h"
17 #endif 18 #endif
18 19
19 namespace { 20 namespace {
20 21
21 // Builds an instance of the backend depending on platform, type, experiments 22 // Builds an instance of the backend depending on platform, type, experiments
22 // etc. Takes care of the retry state. This object will self-destroy when 23 // etc. Takes care of the retry state. This object will self-destroy when
23 // finished. 24 // finished.
24 class CacheCreator { 25 class CacheCreator {
25 public: 26 public:
26 CacheCreator(const base::FilePath& path, bool force, int max_bytes, 27 CacheCreator(const base::FilePath& path, bool force, int max_bytes,
27 net::CacheType type, net::BackendType backend_type, uint32 flags, 28 net::CacheType type, net::BackendType backend_type, uint32 flags,
28 base::MessageLoopProxy* thread, net::NetLog* net_log, 29 base::MessageLoopProxy* thread, net::NetLog* net_log,
29 disk_cache::Backend** backend, 30 scoped_ptr<disk_cache::Backend>* backend,
30 const net::CompletionCallback& callback); 31 const net::CompletionCallback& callback);
31 32
32 // Creates the backend. 33 // Creates the backend.
33 int Run(); 34 int Run();
34 35
35 private: 36 private:
36 ~CacheCreator(); 37 ~CacheCreator();
37 38
38 void DoCallback(int result); 39 void DoCallback(int result);
39 40
40 void OnIOComplete(int result); 41 void OnIOComplete(int result);
41 42
42 const base::FilePath path_; 43 const base::FilePath path_;
43 bool force_; 44 bool force_;
44 bool retry_; 45 bool retry_;
45 int max_bytes_; 46 int max_bytes_;
46 net::CacheType type_; 47 net::CacheType type_;
47 net::BackendType backend_type_; 48 net::BackendType backend_type_;
48 uint32 flags_; 49 uint32 flags_;
49 scoped_refptr<base::MessageLoopProxy> thread_; 50 scoped_refptr<base::MessageLoopProxy> thread_;
50 disk_cache::Backend** backend_; 51 scoped_ptr<disk_cache::Backend>* backend_;
51 net::CompletionCallback callback_; 52 net::CompletionCallback callback_;
52 disk_cache::Backend* created_cache_; 53 scoped_ptr<disk_cache::Backend> created_cache_;
53 net::NetLog* net_log_; 54 net::NetLog* net_log_;
54 55
55 DISALLOW_COPY_AND_ASSIGN(CacheCreator); 56 DISALLOW_COPY_AND_ASSIGN(CacheCreator);
56 }; 57 };
57 58
58 CacheCreator::CacheCreator( 59 CacheCreator::CacheCreator(
59 const base::FilePath& path, bool force, int max_bytes, 60 const base::FilePath& path, bool force, int max_bytes,
60 net::CacheType type, net::BackendType backend_type, uint32 flags, 61 net::CacheType type, net::BackendType backend_type, uint32 flags,
61 base::MessageLoopProxy* thread, net::NetLog* net_log, 62 base::MessageLoopProxy* thread, net::NetLog* net_log,
62 disk_cache::Backend** backend, 63 scoped_ptr<disk_cache::Backend>* backend,
63 const net::CompletionCallback& callback) 64 const net::CompletionCallback& callback)
64 : path_(path), 65 : path_(path),
65 force_(force), 66 force_(force),
66 retry_(false), 67 retry_(false),
67 max_bytes_(max_bytes), 68 max_bytes_(max_bytes),
68 type_(type), 69 type_(type),
69 backend_type_(backend_type), 70 backend_type_(backend_type),
70 flags_(flags), 71 flags_(flags),
71 thread_(thread), 72 thread_(thread),
72 backend_(backend), 73 backend_(backend),
73 callback_(callback), 74 callback_(callback),
74 created_cache_(NULL),
75 net_log_(net_log) { 75 net_log_(net_log) {
76 } 76 }
77 77
78 CacheCreator::~CacheCreator() { 78 CacheCreator::~CacheCreator() {
79 } 79 }
80 80
81 int CacheCreator::Run() { 81 int CacheCreator::Run() {
82 // TODO(gavinp,pasko): While simple backend development proceeds, we're only 82 // TODO(gavinp,pasko): While simple backend development proceeds, we're only
83 // testing it against net::DISK_CACHE. Turn it on for more cache types as 83 // testing it against net::DISK_CACHE. Turn it on for more cache types as
84 // appropriate. 84 // appropriate.
85 if (backend_type_ == net::CACHE_BACKEND_SIMPLE && type_ == net::DISK_CACHE) { 85 if (backend_type_ == net::CACHE_BACKEND_SIMPLE && type_ == net::DISK_CACHE) {
86 disk_cache::SimpleBackendImpl* simple_cache = 86 disk_cache::SimpleBackendImpl* simple_cache =
87 new disk_cache::SimpleBackendImpl(path_, max_bytes_, type_, 87 new disk_cache::SimpleBackendImpl(path_, max_bytes_, type_,
88 thread_.get(), net_log_); 88 thread_.get(), net_log_);
89 created_cache_ = simple_cache; 89 created_cache_.reset(simple_cache);
90 return simple_cache->Init( 90 return simple_cache->Init(
91 base::Bind(&CacheCreator::OnIOComplete, base::Unretained(this))); 91 base::Bind(&CacheCreator::OnIOComplete, base::Unretained(this)));
92 } 92 }
93 disk_cache::BackendImpl* new_cache = 93 disk_cache::BackendImpl* new_cache =
94 new disk_cache::BackendImpl(path_, thread_.get(), net_log_); 94 new disk_cache::BackendImpl(path_, thread_.get(), net_log_);
95 created_cache_ = new_cache; 95 created_cache_.reset(new_cache);
96 new_cache->SetMaxSize(max_bytes_); 96 new_cache->SetMaxSize(max_bytes_);
97 new_cache->SetType(type_); 97 new_cache->SetType(type_);
98 new_cache->SetFlags(flags_); 98 new_cache->SetFlags(flags_);
99 int rv = new_cache->Init( 99 int rv = new_cache->Init(
100 base::Bind(&CacheCreator::OnIOComplete, base::Unretained(this))); 100 base::Bind(&CacheCreator::OnIOComplete, base::Unretained(this)));
101 DCHECK_EQ(net::ERR_IO_PENDING, rv); 101 DCHECK_EQ(net::ERR_IO_PENDING, rv);
102 return rv; 102 return rv;
103 } 103 }
104 104
105 void CacheCreator::DoCallback(int result) { 105 void CacheCreator::DoCallback(int result) {
106 DCHECK_NE(net::ERR_IO_PENDING, result); 106 DCHECK_NE(net::ERR_IO_PENDING, result);
107 if (result == net::OK) { 107 if (result == net::OK) {
108 #ifndef USE_TRACING_CACHE_BACKEND 108 #ifndef USE_TRACING_CACHE_BACKEND
109 *backend_ = created_cache_; 109 *backend_ = created_cache_.Pass();
110 #else 110 #else
111 *backend_ = new disk_cache::TracingCacheBackend(created_cache_); 111 *backend_.reset(
112 new disk_cache::TracingCacheBackend(created_cache_.Pass()));
112 #endif 113 #endif
113 } else { 114 } else {
114 LOG(ERROR) << "Unable to create cache"; 115 LOG(ERROR) << "Unable to create cache";
115 *backend_ = NULL;
116 delete created_cache_;
117 } 116 }
118 callback_.Run(result); 117 callback_.Run(result);
119 delete this; 118 delete this;
120 } 119 }
121 120
122 // If the initialization of the cache fails, and |force| is true, we will 121 // If the initialization of the cache fails, and |force| is true, we will
123 // discard the whole cache and create a new one. 122 // discard the whole cache and create a new one.
124 void CacheCreator::OnIOComplete(int result) { 123 void CacheCreator::OnIOComplete(int result) {
125 if (result == net::OK || !force_ || retry_) 124 if (result == net::OK || !force_ || retry_)
126 return DoCallback(result); 125 return DoCallback(result);
127 126
128 // This is a failure and we are supposed to try again, so delete the object, 127 // This is a failure and we are supposed to try again, so delete the object,
129 // delete all the files, and try again. 128 // delete all the files, and try again.
130 retry_ = true; 129 retry_ = true;
131 delete created_cache_; 130 created_cache_.reset();
132 created_cache_ = NULL;
133 if (!disk_cache::DelayedCacheCleanup(path_)) 131 if (!disk_cache::DelayedCacheCleanup(path_))
134 return DoCallback(result); 132 return DoCallback(result);
135 133
136 // The worker thread will start deleting files soon, but the original folder 134 // The worker thread will start deleting files soon, but the original folder
137 // is not there anymore... let's create a new set of files. 135 // is not there anymore... let's create a new set of files.
138 int rv = Run(); 136 int rv = Run();
139 DCHECK_EQ(net::ERR_IO_PENDING, rv); 137 DCHECK_EQ(net::ERR_IO_PENDING, rv);
140 } 138 }
141 139
142 } // namespace 140 } // namespace
143 141
144 namespace disk_cache { 142 namespace disk_cache {
145 143
146 int CreateCacheBackend(net::CacheType type, 144 int CreateCacheBackend(net::CacheType type,
147 net::BackendType backend_type, 145 net::BackendType backend_type,
148 const base::FilePath& path, 146 const base::FilePath& path,
149 int max_bytes, 147 int max_bytes,
150 bool force, base::MessageLoopProxy* thread, 148 bool force, base::MessageLoopProxy* thread,
151 net::NetLog* net_log, Backend** backend, 149 net::NetLog* net_log, scoped_ptr<Backend>* backend,
152 const net::CompletionCallback& callback) { 150 const net::CompletionCallback& callback) {
153 DCHECK(!callback.is_null()); 151 DCHECK(!callback.is_null());
154 if (type == net::MEMORY_CACHE) { 152 if (type == net::MEMORY_CACHE) {
155 *backend = disk_cache::MemBackendImpl::CreateBackend(max_bytes, net_log); 153 *backend = disk_cache::MemBackendImpl::CreateBackend(max_bytes, net_log);
156 return *backend ? net::OK : net::ERR_FAILED; 154 return *backend ? net::OK : net::ERR_FAILED;
157 } 155 }
158 DCHECK(thread); 156 DCHECK(thread);
159 CacheCreator* creator = new CacheCreator(path, force, max_bytes, type, 157 CacheCreator* creator = new CacheCreator(path, force, max_bytes, type,
160 backend_type, kNone, 158 backend_type, kNone,
161 thread, net_log, backend, callback); 159 thread, net_log, backend, callback);
162 return creator->Run(); 160 return creator->Run();
163 } 161 }
164 162
165 } // namespace disk_cache 163 } // namespace disk_cache
OLDNEW
« no previous file with comments | « net/disk_cache/backend_unittest.cc ('k') | net/disk_cache/disk_cache.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698