OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "webkit/appcache/appcache_disk_cache.h" | 5 #include "webkit/appcache/appcache_disk_cache.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
14 | 14 |
15 namespace appcache { | 15 namespace appcache { |
16 | 16 |
| 17 // TODO(ajwong): Change disk_cache's API to return results via Callback. |
| 18 struct AppCacheDiskCache::CreateBackendDataShim { |
| 19 CreateBackendDataShim() : backend(NULL) {} |
| 20 disk_cache::Backend* backend; |
| 21 }; |
| 22 |
17 // An implementation of AppCacheDiskCacheInterface::Entry that's a thin | 23 // An implementation of AppCacheDiskCacheInterface::Entry that's a thin |
18 // wrapper around disk_cache::Entry. | 24 // wrapper around disk_cache::Entry. |
19 class AppCacheDiskCache::EntryImpl : public Entry { | 25 class AppCacheDiskCache::EntryImpl : public Entry { |
20 public: | 26 public: |
21 explicit EntryImpl(disk_cache::Entry* disk_cache_entry) | 27 explicit EntryImpl(disk_cache::Entry* disk_cache_entry) |
22 : disk_cache_entry_(disk_cache_entry) { | 28 : disk_cache_entry_(disk_cache_entry) { |
23 DCHECK(disk_cache_entry); | 29 DCHECK(disk_cache_entry); |
24 } | 30 } |
25 | 31 |
26 // Entry implementation. | 32 // Entry implementation. |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 net::CompletionCallback callback_; | 122 net::CompletionCallback callback_; |
117 AppCacheDiskCache* owner_; | 123 AppCacheDiskCache* owner_; |
118 disk_cache::Entry* entry_ptr_; | 124 disk_cache::Entry* entry_ptr_; |
119 }; | 125 }; |
120 | 126 |
121 AppCacheDiskCache::AppCacheDiskCache() | 127 AppCacheDiskCache::AppCacheDiskCache() |
122 : is_disabled_(false) { | 128 : is_disabled_(false) { |
123 } | 129 } |
124 | 130 |
125 AppCacheDiskCache::~AppCacheDiskCache() { | 131 AppCacheDiskCache::~AppCacheDiskCache() { |
126 if (create_backend_callback_) { | 132 if (!create_backend_callback_.IsCancelled()) { |
127 create_backend_callback_->Cancel(); | 133 create_backend_callback_.Cancel(); |
128 create_backend_callback_.release(); | 134 OnCreateBackendComplete(NULL, net::ERR_ABORTED); |
129 OnCreateBackendComplete(net::ERR_ABORTED); | |
130 } | 135 } |
131 disk_cache_.reset(); | 136 disk_cache_.reset(); |
132 STLDeleteElements(&active_calls_); | 137 STLDeleteElements(&active_calls_); |
133 } | 138 } |
134 | 139 |
135 int AppCacheDiskCache::InitWithDiskBackend( | 140 int AppCacheDiskCache::InitWithDiskBackend( |
136 const FilePath& disk_cache_directory, int disk_cache_size, bool force, | 141 const FilePath& disk_cache_directory, int disk_cache_size, bool force, |
137 base::MessageLoopProxy* cache_thread, | 142 base::MessageLoopProxy* cache_thread, |
138 const net::CompletionCallback& callback) { | 143 const net::CompletionCallback& callback) { |
139 return Init(net::APP_CACHE, disk_cache_directory, | 144 return Init(net::APP_CACHE, disk_cache_directory, |
140 disk_cache_size, force, cache_thread, callback); | 145 disk_cache_size, force, cache_thread, callback); |
141 } | 146 } |
142 | 147 |
143 int AppCacheDiskCache::InitWithMemBackend( | 148 int AppCacheDiskCache::InitWithMemBackend( |
144 int mem_cache_size, const net::CompletionCallback& callback) { | 149 int mem_cache_size, const net::CompletionCallback& callback) { |
145 return Init(net::MEMORY_CACHE, FilePath(), mem_cache_size, false, NULL, | 150 return Init(net::MEMORY_CACHE, FilePath(), mem_cache_size, false, NULL, |
146 callback); | 151 callback); |
147 } | 152 } |
148 | 153 |
149 void AppCacheDiskCache::Disable() { | 154 void AppCacheDiskCache::Disable() { |
150 if (is_disabled_) | 155 if (is_disabled_) |
151 return; | 156 return; |
152 | 157 |
153 is_disabled_ = true; | 158 is_disabled_ = true; |
154 | 159 |
155 if (create_backend_callback_) { | 160 if (!create_backend_callback_.IsCancelled()) { |
156 create_backend_callback_->Cancel(); | 161 create_backend_callback_.Cancel(); |
157 create_backend_callback_.release(); | 162 OnCreateBackendComplete(NULL, net::ERR_ABORTED); |
158 OnCreateBackendComplete(net::ERR_ABORTED); | |
159 } | 163 } |
160 } | 164 } |
161 | 165 |
162 int AppCacheDiskCache::CreateEntry(int64 key, Entry** entry, | 166 int AppCacheDiskCache::CreateEntry(int64 key, Entry** entry, |
163 const net::CompletionCallback& callback) { | 167 const net::CompletionCallback& callback) { |
164 DCHECK(entry); | 168 DCHECK(entry); |
165 DCHECK(!callback.is_null()); | 169 DCHECK(!callback.is_null()); |
166 if (is_disabled_) | 170 if (is_disabled_) |
167 return net::ERR_ABORTED; | 171 return net::ERR_ABORTED; |
168 | 172 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 | 218 |
215 AppCacheDiskCache::PendingCall::~PendingCall() {} | 219 AppCacheDiskCache::PendingCall::~PendingCall() {} |
216 | 220 |
217 int AppCacheDiskCache::Init(net::CacheType cache_type, | 221 int AppCacheDiskCache::Init(net::CacheType cache_type, |
218 const FilePath& cache_directory, | 222 const FilePath& cache_directory, |
219 int cache_size, bool force, | 223 int cache_size, bool force, |
220 base::MessageLoopProxy* cache_thread, | 224 base::MessageLoopProxy* cache_thread, |
221 const net::CompletionCallback& callback) { | 225 const net::CompletionCallback& callback) { |
222 DCHECK(!is_initializing() && !disk_cache_.get()); | 226 DCHECK(!is_initializing() && !disk_cache_.get()); |
223 is_disabled_ = false; | 227 is_disabled_ = false; |
224 create_backend_callback_ = new CreateBackendCallback( | |
225 this, &AppCacheDiskCache::OnCreateBackendComplete); | |
226 | 228 |
| 229 // TODO(ajwong): Change disk_cache's API to return results via Callback. |
| 230 disk_cache::Backend** backend_ptr = new(disk_cache::Backend*); |
| 231 create_backend_callback_.Reset( |
| 232 base::Bind(&AppCacheDiskCache::OnCreateBackendComplete, |
| 233 base::Unretained(this), base::Owned(backend_ptr))); |
227 int rv = disk_cache::CreateCacheBackend( | 234 int rv = disk_cache::CreateCacheBackend( |
228 cache_type, cache_directory, cache_size, force, cache_thread, NULL, | 235 cache_type, cache_directory, cache_size, force, cache_thread, NULL, |
229 &(create_backend_callback_->backend_ptr_), | 236 backend_ptr, create_backend_callback_.callback()); |
230 base::Bind(&net::OldCompletionCallbackAdapter, create_backend_callback_)); | |
231 if (rv == net::ERR_IO_PENDING) | 237 if (rv == net::ERR_IO_PENDING) |
232 init_callback_ = callback; | 238 init_callback_ = callback; |
233 else | 239 else |
234 OnCreateBackendComplete(rv); | 240 OnCreateBackendComplete(backend_ptr, rv); |
| 241 |
235 return rv; | 242 return rv; |
236 } | 243 } |
237 | 244 |
238 void AppCacheDiskCache::OnCreateBackendComplete(int rv) { | 245 void AppCacheDiskCache::OnCreateBackendComplete( |
| 246 disk_cache::Backend** backend, int rv) { |
239 if (rv == net::OK) { | 247 if (rv == net::OK) { |
240 disk_cache_.reset(create_backend_callback_->backend_ptr_); | 248 DCHECK(backend); |
241 create_backend_callback_->backend_ptr_ = NULL; | 249 disk_cache_.reset(*backend); |
242 } | 250 } |
243 create_backend_callback_ = NULL; | 251 |
| 252 create_backend_callback_.Cancel(); |
244 | 253 |
245 // Invoke our clients callback function. | 254 // Invoke our clients callback function. |
246 if (!init_callback_.is_null()) { | 255 if (!init_callback_.is_null()) { |
247 init_callback_.Run(rv); | 256 init_callback_.Run(rv); |
248 init_callback_.Reset(); | 257 init_callback_.Reset(); |
249 } | 258 } |
250 | 259 |
251 // Service pending calls that were queued up while we were initializing. | 260 // Service pending calls that were queued up while we were initializing. |
252 for (PendingCalls::const_iterator iter = pending_calls_.begin(); | 261 for (PendingCalls::const_iterator iter = pending_calls_.begin(); |
253 iter < pending_calls_.end(); ++iter) { | 262 iter < pending_calls_.end(); ++iter) { |
(...skipping 13 matching lines...) Expand all Loading... |
267 break; | 276 break; |
268 } | 277 } |
269 if (rv != net::ERR_IO_PENDING) | 278 if (rv != net::ERR_IO_PENDING) |
270 iter->callback.Run(rv); | 279 iter->callback.Run(rv); |
271 } | 280 } |
272 pending_calls_.clear(); | 281 pending_calls_.clear(); |
273 } | 282 } |
274 | 283 |
275 } // namespace appcache | 284 } // namespace appcache |
276 | 285 |
OLD | NEW |