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 "webkit/browser/appcache/appcache_disk_cache.h" | 5 #include "webkit/browser/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/files/file_path.h" | 9 #include "base/files/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/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "net/base/cache_type.h" | 13 #include "net/base/cache_type.h" |
14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
15 | 15 |
16 namespace appcache { | 16 namespace appcache { |
17 | 17 |
18 // A callback shim that provides storage for the 'backend_ptr' value | 18 // A callback shim that provides storage for the 'backend_ptr' value |
19 // and will delete a resulting ptr if completion occurs after its | 19 // and will delete a resulting ptr if completion occurs after its |
20 // been canceled. | 20 // been canceled. |
21 class AppCacheDiskCache::CreateBackendCallbackShim | 21 class AppCacheDiskCache::CreateBackendCallbackShim |
22 : public base::RefCounted<CreateBackendCallbackShim> { | 22 : public base::RefCounted<CreateBackendCallbackShim> { |
23 public: | 23 public: |
24 explicit CreateBackendCallbackShim(AppCacheDiskCache* object) | 24 explicit CreateBackendCallbackShim(AppCacheDiskCache* object) |
25 : backend_ptr_(NULL), appcache_diskcache_(object) { | 25 : appcache_diskcache_(object) { |
26 } | 26 } |
27 | 27 |
28 void Cancel() { | 28 void Cancel() { |
29 appcache_diskcache_ = NULL; | 29 appcache_diskcache_ = NULL; |
30 } | 30 } |
31 | 31 |
32 void Callback(int rv) { | 32 void Callback(int rv) { |
33 if (appcache_diskcache_) | 33 if (appcache_diskcache_) |
34 appcache_diskcache_->OnCreateBackendComplete(rv); | 34 appcache_diskcache_->OnCreateBackendComplete(rv); |
35 } | 35 } |
36 | 36 |
37 disk_cache::Backend* backend_ptr_; // Accessed directly. | 37 scoped_ptr<disk_cache::Backend> backend_ptr_; // Accessed directly. |
38 | 38 |
39 private: | 39 private: |
40 friend class base::RefCounted<CreateBackendCallbackShim>; | 40 friend class base::RefCounted<CreateBackendCallbackShim>; |
41 | 41 |
42 ~CreateBackendCallbackShim() { | 42 ~CreateBackendCallbackShim() { |
43 delete backend_ptr_; | |
44 } | 43 } |
45 | 44 |
46 AppCacheDiskCache* appcache_diskcache_; // Unowned pointer. | 45 AppCacheDiskCache* appcache_diskcache_; // Unowned pointer. |
47 }; | 46 }; |
48 | 47 |
49 // An implementation of AppCacheDiskCacheInterface::Entry that's a thin | 48 // An implementation of AppCacheDiskCacheInterface::Entry that's a thin |
50 // wrapper around disk_cache::Entry. | 49 // wrapper around disk_cache::Entry. |
51 class AppCacheDiskCache::EntryImpl : public Entry { | 50 class AppCacheDiskCache::EntryImpl : public Entry { |
52 public: | 51 public: |
53 explicit EntryImpl(disk_cache::Entry* disk_cache_entry) | 52 explicit EntryImpl(disk_cache::Entry* disk_cache_entry) |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 create_backend_callback_)); | 277 create_backend_callback_)); |
279 if (rv == net::ERR_IO_PENDING) | 278 if (rv == net::ERR_IO_PENDING) |
280 init_callback_ = callback; | 279 init_callback_ = callback; |
281 else | 280 else |
282 OnCreateBackendComplete(rv); | 281 OnCreateBackendComplete(rv); |
283 return rv; | 282 return rv; |
284 } | 283 } |
285 | 284 |
286 void AppCacheDiskCache::OnCreateBackendComplete(int rv) { | 285 void AppCacheDiskCache::OnCreateBackendComplete(int rv) { |
287 if (rv == net::OK) { | 286 if (rv == net::OK) { |
288 disk_cache_.reset(create_backend_callback_->backend_ptr_); | 287 disk_cache_ = create_backend_callback_->backend_ptr_.Pass(); |
289 create_backend_callback_->backend_ptr_ = NULL; | |
290 } | 288 } |
291 create_backend_callback_ = NULL; | 289 create_backend_callback_ = NULL; |
292 | 290 |
293 // Invoke our clients callback function. | 291 // Invoke our clients callback function. |
294 if (!init_callback_.is_null()) { | 292 if (!init_callback_.is_null()) { |
295 init_callback_.Run(rv); | 293 init_callback_.Run(rv); |
296 init_callback_.Reset(); | 294 init_callback_.Reset(); |
297 } | 295 } |
298 | 296 |
299 // Service pending calls that were queued up while we were initializing. | 297 // Service pending calls that were queued up while we were initializing. |
(...skipping 14 matching lines...) Expand all Loading... |
314 NOTREACHED(); | 312 NOTREACHED(); |
315 break; | 313 break; |
316 } | 314 } |
317 if (rv != net::ERR_IO_PENDING) | 315 if (rv != net::ERR_IO_PENDING) |
318 iter->callback.Run(rv); | 316 iter->callback.Run(rv); |
319 } | 317 } |
320 pending_calls_.clear(); | 318 pending_calls_.clear(); |
321 } | 319 } |
322 | 320 |
323 } // namespace appcache | 321 } // namespace appcache |
OLD | NEW |