Chromium Code Reviews| 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 "content/browser/appcache/appcache_disk_cache.h" | 5 #include "content/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" |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 112 AppCacheDiskCache* owner_; | 112 AppCacheDiskCache* owner_; |
| 113 }; | 113 }; |
| 114 | 114 |
| 115 // Separate object to hold state for each Create, Delete, or Doom call | 115 // Separate object to hold state for each Create, Delete, or Doom call |
| 116 // while the call is in-flight and to produce an EntryImpl upon completion. | 116 // while the call is in-flight and to produce an EntryImpl upon completion. |
| 117 class AppCacheDiskCache::ActiveCall { | 117 class AppCacheDiskCache::ActiveCall { |
| 118 public: | 118 public: |
| 119 explicit ActiveCall(AppCacheDiskCache* owner) | 119 explicit ActiveCall(AppCacheDiskCache* owner) |
| 120 : entry_(NULL), | 120 : entry_(NULL), |
| 121 owner_(owner), | 121 owner_(owner), |
| 122 entry_ptr_(NULL) { | 122 entry_ptr_(NULL), |
| 123 weak_factory_(this) { | |
| 123 } | 124 } |
| 124 | 125 |
| 125 int CreateEntry(int64 key, Entry** entry, | 126 int CreateEntry(int64 key, Entry** entry, |
| 126 const net::CompletionCallback& callback) { | 127 const net::CompletionCallback& callback) { |
| 127 int rv = owner_->disk_cache()->CreateEntry( | 128 int rv = owner_->disk_cache()->CreateEntry( |
| 128 base::Int64ToString(key), &entry_ptr_, | 129 base::Int64ToString(key), &entry_ptr_, |
| 129 base::Bind(&ActiveCall::OnAsyncCompletion, base::Unretained(this))); | 130 base::Bind(&ActiveCall::OnAsyncCompletion, weak_factory_.GetWeakPtr())); |
|
michaeln
2015/05/14 02:25:01
I remember there being a difference in behavior be
nhiroki
2015/05/14 04:27:05
I haven't had an absolute proof yet, but it seems
| |
| 130 return HandleImmediateReturnValue(rv, entry, callback); | 131 return HandleImmediateReturnValue(rv, entry, callback); |
| 131 } | 132 } |
| 132 | 133 |
| 133 int OpenEntry(int64 key, Entry** entry, | 134 int OpenEntry(int64 key, Entry** entry, |
| 134 const net::CompletionCallback& callback) { | 135 const net::CompletionCallback& callback) { |
| 135 int rv = owner_->disk_cache()->OpenEntry( | 136 int rv = owner_->disk_cache()->OpenEntry( |
| 136 base::Int64ToString(key), &entry_ptr_, | 137 base::Int64ToString(key), &entry_ptr_, |
| 137 base::Bind(&ActiveCall::OnAsyncCompletion, base::Unretained(this))); | 138 base::Bind(&ActiveCall::OnAsyncCompletion, weak_factory_.GetWeakPtr())); |
| 138 return HandleImmediateReturnValue(rv, entry, callback); | 139 return HandleImmediateReturnValue(rv, entry, callback); |
| 139 } | 140 } |
| 140 | 141 |
| 141 int DoomEntry(int64 key, const net::CompletionCallback& callback) { | 142 int DoomEntry(int64 key, const net::CompletionCallback& callback) { |
| 142 int rv = owner_->disk_cache()->DoomEntry( | 143 int rv = owner_->disk_cache()->DoomEntry( |
| 143 base::Int64ToString(key), | 144 base::Int64ToString(key), |
| 144 base::Bind(&ActiveCall::OnAsyncCompletion, base::Unretained(this))); | 145 base::Bind(&ActiveCall::OnAsyncCompletion, weak_factory_.GetWeakPtr())); |
| 145 return HandleImmediateReturnValue(rv, NULL, callback); | 146 return HandleImmediateReturnValue(rv, NULL, callback); |
| 146 } | 147 } |
| 147 | 148 |
| 149 void Abort() { | |
| 150 callback_.Run(net::ERR_ABORTED); | |
| 151 delete this; | |
| 152 } | |
| 153 | |
| 148 private: | 154 private: |
| 149 int HandleImmediateReturnValue(int rv, Entry** entry, | 155 int HandleImmediateReturnValue(int rv, Entry** entry, |
| 150 const net::CompletionCallback& callback) { | 156 const net::CompletionCallback& callback) { |
| 151 if (rv == net::ERR_IO_PENDING) { | 157 if (rv == net::ERR_IO_PENDING) { |
| 152 // OnAsyncCompletion will be called later. | 158 // OnAsyncCompletion will be called later. |
| 153 callback_ = callback; | 159 callback_ = callback; |
| 154 entry_ = entry; | 160 entry_ = entry; |
| 155 owner_->AddActiveCall(this); | 161 owner_->AddActiveCall(this); |
| 156 return net::ERR_IO_PENDING; | 162 return net::ERR_IO_PENDING; |
| 157 } | 163 } |
| 158 if (rv == net::OK && entry) | 164 if (rv == net::OK && entry) |
| 159 *entry = new EntryImpl(entry_ptr_, owner_); | 165 *entry = new EntryImpl(entry_ptr_, owner_); |
| 160 delete this; | 166 delete this; |
| 161 return rv; | 167 return rv; |
| 162 } | 168 } |
| 163 | 169 |
| 164 void OnAsyncCompletion(int rv) { | 170 void OnAsyncCompletion(int rv) { |
| 165 owner_->RemoveActiveCall(this); | 171 owner_->RemoveActiveCall(this); |
| 166 if (rv == net::OK && entry_) | 172 if (rv == net::OK && entry_) |
| 167 *entry_ = new EntryImpl(entry_ptr_, owner_); | 173 *entry_ = new EntryImpl(entry_ptr_, owner_); |
| 168 callback_.Run(rv); | 174 callback_.Run(rv); |
| 169 callback_.Reset(); | |
| 170 delete this; | 175 delete this; |
| 171 } | 176 } |
| 172 | 177 |
| 173 Entry** entry_; | 178 Entry** entry_; |
| 174 net::CompletionCallback callback_; | 179 net::CompletionCallback callback_; |
| 175 AppCacheDiskCache* owner_; | 180 AppCacheDiskCache* owner_; |
| 176 disk_cache::Entry* entry_ptr_; | 181 disk_cache::Entry* entry_ptr_; |
| 182 | |
| 183 base::WeakPtrFactory<AppCacheDiskCache::ActiveCall> weak_factory_; | |
| 177 }; | 184 }; |
| 178 | 185 |
| 179 AppCacheDiskCache::AppCacheDiskCache() | 186 AppCacheDiskCache::AppCacheDiskCache() |
| 180 : is_disabled_(false) { | 187 : is_disabled_(false) { |
| 181 } | 188 } |
| 182 | 189 |
| 183 AppCacheDiskCache::~AppCacheDiskCache() { | 190 AppCacheDiskCache::~AppCacheDiskCache() { |
| 184 Disable(); | 191 Disable(); |
| 185 } | 192 } |
| 186 | 193 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 | 225 |
| 219 // We need to close open file handles in order to reinitalize the | 226 // We need to close open file handles in order to reinitalize the |
| 220 // appcache system on the fly. File handles held in both entries and in | 227 // appcache system on the fly. File handles held in both entries and in |
| 221 // the main disk_cache::Backend class need to be released. | 228 // the main disk_cache::Backend class need to be released. |
| 222 for (OpenEntries::const_iterator iter = open_entries_.begin(); | 229 for (OpenEntries::const_iterator iter = open_entries_.begin(); |
| 223 iter != open_entries_.end(); ++iter) { | 230 iter != open_entries_.end(); ++iter) { |
| 224 (*iter)->Abandon(); | 231 (*iter)->Abandon(); |
| 225 } | 232 } |
| 226 open_entries_.clear(); | 233 open_entries_.clear(); |
| 227 disk_cache_.reset(); | 234 disk_cache_.reset(); |
| 228 STLDeleteElements(&active_calls_); | 235 |
| 236 for (AppCacheDiskCache::ActiveCall* active_call : active_calls_) | |
| 237 active_call->Abort(); | |
| 238 active_calls_.clear(); | |
| 229 } | 239 } |
| 230 | 240 |
| 231 int AppCacheDiskCache::CreateEntry(int64 key, Entry** entry, | 241 int AppCacheDiskCache::CreateEntry(int64 key, Entry** entry, |
| 232 const net::CompletionCallback& callback) { | 242 const net::CompletionCallback& callback) { |
| 233 DCHECK(entry); | 243 DCHECK(entry); |
| 234 DCHECK(!callback.is_null()); | 244 DCHECK(!callback.is_null()); |
| 235 if (is_disabled_) | 245 if (is_disabled_) |
| 236 return net::ERR_ABORTED; | 246 return net::ERR_ABORTED; |
| 237 | 247 |
| 238 if (is_initializing()) { | 248 if (is_initializing()) { |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 363 NOTREACHED(); | 373 NOTREACHED(); |
| 364 break; | 374 break; |
| 365 } | 375 } |
| 366 if (rv != net::ERR_IO_PENDING) | 376 if (rv != net::ERR_IO_PENDING) |
| 367 iter->callback.Run(rv); | 377 iter->callback.Run(rv); |
| 368 } | 378 } |
| 369 pending_calls_.clear(); | 379 pending_calls_.clear(); |
| 370 } | 380 } |
| 371 | 381 |
| 372 } // namespace content | 382 } // namespace content |
| OLD | NEW |