OLD | NEW |
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/blockfile/backend_impl.h" | 10 #include "net/disk_cache/blockfile/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/disk_cache.h" |
13 #include "net/disk_cache/memory/mem_backend_impl.h" | 13 #include "net/disk_cache/memory/mem_backend_impl.h" |
14 #include "net/disk_cache/simple/simple_backend_impl.h" | 14 #include "net/disk_cache/simple/simple_backend_impl.h" |
15 | 15 |
16 #ifdef USE_TRACING_CACHE_BACKEND | 16 #ifdef USE_TRACING_CACHE_BACKEND |
17 #include "net/disk_cache/tracing_cache_backend.h" | 17 #include "net/disk_cache/tracing_cache_backend.h" |
18 #endif | 18 #endif |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 // Builds an instance of the backend depending on platform, type, experiments | 22 // Builds an instance of the backend depending on platform, type, experiments |
23 // 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 |
24 // finished. | 24 // finished. |
25 class CacheCreator { | 25 class CacheCreator { |
26 public: | 26 public: |
27 CacheCreator(const base::FilePath& path, bool force, int max_bytes, | 27 CacheCreator(const base::FilePath& path, |
28 net::CacheType type, net::BackendType backend_type, uint32 flags, | 28 bool force, |
29 base::MessageLoopProxy* thread, net::NetLog* net_log, | 29 int max_bytes, |
| 30 net::CacheType type, |
| 31 net::BackendType backend_type, |
| 32 uint32 flags, |
| 33 base::MessageLoopProxy* thread, |
| 34 net::NetLog* net_log, |
30 scoped_ptr<disk_cache::Backend>* backend, | 35 scoped_ptr<disk_cache::Backend>* backend, |
31 const net::CompletionCallback& callback); | 36 const net::CompletionCallback& callback); |
32 | 37 |
33 // Creates the backend. | 38 // Creates the backend. |
34 int Run(); | 39 int Run(); |
35 | 40 |
36 private: | 41 private: |
37 ~CacheCreator(); | 42 ~CacheCreator(); |
38 | 43 |
39 void DoCallback(int result); | 44 void DoCallback(int result); |
40 | 45 |
41 void OnIOComplete(int result); | 46 void OnIOComplete(int result); |
42 | 47 |
43 const base::FilePath path_; | 48 const base::FilePath path_; |
44 bool force_; | 49 bool force_; |
45 bool retry_; | 50 bool retry_; |
46 int max_bytes_; | 51 int max_bytes_; |
47 net::CacheType type_; | 52 net::CacheType type_; |
48 net::BackendType backend_type_; | 53 net::BackendType backend_type_; |
49 uint32 flags_; | 54 uint32 flags_; |
50 scoped_refptr<base::MessageLoopProxy> thread_; | 55 scoped_refptr<base::MessageLoopProxy> thread_; |
51 scoped_ptr<disk_cache::Backend>* backend_; | 56 scoped_ptr<disk_cache::Backend>* backend_; |
52 net::CompletionCallback callback_; | 57 net::CompletionCallback callback_; |
53 scoped_ptr<disk_cache::Backend> created_cache_; | 58 scoped_ptr<disk_cache::Backend> created_cache_; |
54 net::NetLog* net_log_; | 59 net::NetLog* net_log_; |
55 | 60 |
56 DISALLOW_COPY_AND_ASSIGN(CacheCreator); | 61 DISALLOW_COPY_AND_ASSIGN(CacheCreator); |
57 }; | 62 }; |
58 | 63 |
59 CacheCreator::CacheCreator( | 64 CacheCreator::CacheCreator(const base::FilePath& path, |
60 const base::FilePath& path, bool force, int max_bytes, | 65 bool force, |
61 net::CacheType type, net::BackendType backend_type, uint32 flags, | 66 int max_bytes, |
62 base::MessageLoopProxy* thread, net::NetLog* net_log, | 67 net::CacheType type, |
63 scoped_ptr<disk_cache::Backend>* backend, | 68 net::BackendType backend_type, |
64 const net::CompletionCallback& callback) | 69 uint32 flags, |
| 70 base::MessageLoopProxy* thread, |
| 71 net::NetLog* net_log, |
| 72 scoped_ptr<disk_cache::Backend>* backend, |
| 73 const net::CompletionCallback& callback) |
65 : path_(path), | 74 : path_(path), |
66 force_(force), | 75 force_(force), |
67 retry_(false), | 76 retry_(false), |
68 max_bytes_(max_bytes), | 77 max_bytes_(max_bytes), |
69 type_(type), | 78 type_(type), |
70 backend_type_(backend_type), | 79 backend_type_(backend_type), |
71 flags_(flags), | 80 flags_(flags), |
72 thread_(thread), | 81 thread_(thread), |
73 backend_(backend), | 82 backend_(backend), |
74 callback_(callback), | 83 callback_(callback), |
75 net_log_(net_log) { | 84 net_log_(net_log) { |
76 } | 85 } |
77 | 86 |
78 CacheCreator::~CacheCreator() { | 87 CacheCreator::~CacheCreator() { |
79 } | 88 } |
80 | 89 |
81 int CacheCreator::Run() { | 90 int CacheCreator::Run() { |
82 #if defined(OS_ANDROID) | 91 #if defined(OS_ANDROID) |
83 static const bool kSimpleBackendIsDefault = true; | 92 static const bool kSimpleBackendIsDefault = true; |
84 #else | 93 #else |
85 static const bool kSimpleBackendIsDefault = false; | 94 static const bool kSimpleBackendIsDefault = false; |
86 #endif | 95 #endif |
87 if (backend_type_ == net::CACHE_BACKEND_SIMPLE || | 96 if (backend_type_ == net::CACHE_BACKEND_SIMPLE || |
88 (backend_type_ == net::CACHE_BACKEND_DEFAULT && | 97 (backend_type_ == net::CACHE_BACKEND_DEFAULT && |
89 kSimpleBackendIsDefault)) { | 98 kSimpleBackendIsDefault)) { |
90 disk_cache::SimpleBackendImpl* simple_cache = | 99 disk_cache::SimpleBackendImpl* simple_cache = |
91 new disk_cache::SimpleBackendImpl(path_, max_bytes_, type_, | 100 new disk_cache::SimpleBackendImpl( |
92 thread_.get(), net_log_); | 101 path_, max_bytes_, type_, thread_.get(), net_log_); |
93 created_cache_.reset(simple_cache); | 102 created_cache_.reset(simple_cache); |
94 return simple_cache->Init( | 103 return simple_cache->Init( |
95 base::Bind(&CacheCreator::OnIOComplete, base::Unretained(this))); | 104 base::Bind(&CacheCreator::OnIOComplete, base::Unretained(this))); |
96 } | 105 } |
97 | 106 |
98 // Avoid references to blockfile functions on Android to reduce binary size. | 107 // Avoid references to blockfile functions on Android to reduce binary size. |
99 #if defined(OS_ANDROID) | 108 #if defined(OS_ANDROID) |
100 return net::ERR_FAILED; | 109 return net::ERR_FAILED; |
101 #else | 110 #else |
102 disk_cache::BackendImpl* new_cache = | 111 disk_cache::BackendImpl* new_cache = |
103 new disk_cache::BackendImpl(path_, thread_.get(), net_log_); | 112 new disk_cache::BackendImpl(path_, thread_.get(), net_log_); |
104 created_cache_.reset(new_cache); | 113 created_cache_.reset(new_cache); |
105 new_cache->SetMaxSize(max_bytes_); | 114 new_cache->SetMaxSize(max_bytes_); |
106 new_cache->SetType(type_); | 115 new_cache->SetType(type_); |
107 new_cache->SetFlags(flags_); | 116 new_cache->SetFlags(flags_); |
108 int rv = new_cache->Init( | 117 int rv = new_cache->Init( |
109 base::Bind(&CacheCreator::OnIOComplete, base::Unretained(this))); | 118 base::Bind(&CacheCreator::OnIOComplete, base::Unretained(this))); |
110 DCHECK_EQ(net::ERR_IO_PENDING, rv); | 119 DCHECK_EQ(net::ERR_IO_PENDING, rv); |
111 return rv; | 120 return rv; |
112 #endif | 121 #endif |
113 } | 122 } |
114 | 123 |
115 void CacheCreator::DoCallback(int result) { | 124 void CacheCreator::DoCallback(int result) { |
116 DCHECK_NE(net::ERR_IO_PENDING, result); | 125 DCHECK_NE(net::ERR_IO_PENDING, result); |
117 if (result == net::OK) { | 126 if (result == net::OK) { |
118 #ifndef USE_TRACING_CACHE_BACKEND | 127 #ifndef USE_TRACING_CACHE_BACKEND |
119 *backend_ = created_cache_.Pass(); | 128 *backend_ = created_cache_.Pass(); |
120 #else | 129 #else |
121 *backend_.reset( | 130 *backend_.reset(new disk_cache::TracingCacheBackend(created_cache_.Pass())); |
122 new disk_cache::TracingCacheBackend(created_cache_.Pass())); | |
123 #endif | 131 #endif |
124 } else { | 132 } else { |
125 LOG(ERROR) << "Unable to create cache"; | 133 LOG(ERROR) << "Unable to create cache"; |
126 created_cache_.reset(); | 134 created_cache_.reset(); |
127 } | 135 } |
128 callback_.Run(result); | 136 callback_.Run(result); |
129 delete this; | 137 delete this; |
130 } | 138 } |
131 | 139 |
132 // If the initialization of the cache fails, and |force| is true, we will | 140 // If the initialization of the cache fails, and |force| is true, we will |
(...skipping 16 matching lines...) Expand all Loading... |
149 } | 157 } |
150 | 158 |
151 } // namespace | 159 } // namespace |
152 | 160 |
153 namespace disk_cache { | 161 namespace disk_cache { |
154 | 162 |
155 int CreateCacheBackend(net::CacheType type, | 163 int CreateCacheBackend(net::CacheType type, |
156 net::BackendType backend_type, | 164 net::BackendType backend_type, |
157 const base::FilePath& path, | 165 const base::FilePath& path, |
158 int max_bytes, | 166 int max_bytes, |
159 bool force, base::MessageLoopProxy* thread, | 167 bool force, |
160 net::NetLog* net_log, scoped_ptr<Backend>* backend, | 168 base::MessageLoopProxy* thread, |
| 169 net::NetLog* net_log, |
| 170 scoped_ptr<Backend>* backend, |
161 const net::CompletionCallback& callback) { | 171 const net::CompletionCallback& callback) { |
162 DCHECK(!callback.is_null()); | 172 DCHECK(!callback.is_null()); |
163 if (type == net::MEMORY_CACHE) { | 173 if (type == net::MEMORY_CACHE) { |
164 *backend = disk_cache::MemBackendImpl::CreateBackend(max_bytes, net_log); | 174 *backend = disk_cache::MemBackendImpl::CreateBackend(max_bytes, net_log); |
165 return *backend ? net::OK : net::ERR_FAILED; | 175 return *backend ? net::OK : net::ERR_FAILED; |
166 } | 176 } |
167 DCHECK(thread); | 177 DCHECK(thread); |
168 CacheCreator* creator = new CacheCreator(path, force, max_bytes, type, | 178 CacheCreator* creator = new CacheCreator(path, |
169 backend_type, kNone, | 179 force, |
170 thread, net_log, backend, callback); | 180 max_bytes, |
| 181 type, |
| 182 backend_type, |
| 183 kNone, |
| 184 thread, |
| 185 net_log, |
| 186 backend, |
| 187 callback); |
171 return creator->Run(); | 188 return creator->Run(); |
172 } | 189 } |
173 | 190 |
174 } // namespace disk_cache | 191 } // namespace disk_cache |
OLD | NEW |