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_service.h" | 5 #include "webkit/appcache/appcache_service.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
10 #include "webkit/appcache/appcache_backend_impl.h" | 10 #include "webkit/appcache/appcache_backend_impl.h" |
11 #include "webkit/appcache/appcache_entry.h" | 11 #include "webkit/appcache/appcache_entry.h" |
12 #include "webkit/appcache/appcache_quota_client.h" | |
12 #include "webkit/appcache/appcache_storage_impl.h" | 13 #include "webkit/appcache/appcache_storage_impl.h" |
13 #include "webkit/quota/quota_manager.h" | 14 #include "webkit/quota/quota_manager.h" |
14 #include "webkit/quota/special_storage_policy.h" | 15 #include "webkit/quota/special_storage_policy.h" |
15 | 16 |
16 namespace appcache { | 17 namespace appcache { |
17 | 18 |
18 AppCacheInfoCollection::AppCacheInfoCollection() {} | 19 AppCacheInfoCollection::AppCacheInfoCollection() {} |
19 | 20 |
20 AppCacheInfoCollection::~AppCacheInfoCollection() {} | 21 AppCacheInfoCollection::~AppCacheInfoCollection() {} |
21 | 22 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 delete this; | 137 delete this; |
137 } | 138 } |
138 } | 139 } |
139 | 140 |
140 void AppCacheService::DeleteHelper::OnGroupMadeObsolete( | 141 void AppCacheService::DeleteHelper::OnGroupMadeObsolete( |
141 appcache::AppCacheGroup* group, bool success) { | 142 appcache::AppCacheGroup* group, bool success) { |
142 CallCallback(success ? net::OK : net::ERR_FAILED); | 143 CallCallback(success ? net::OK : net::ERR_FAILED); |
143 delete this; | 144 delete this; |
144 } | 145 } |
145 | 146 |
147 // DeleteOriginHelper ------- | |
148 | |
149 class AppCacheService::DeleteOriginHelper : public AsyncHelper { | |
150 public: | |
151 DeleteOriginHelper( | |
152 AppCacheService* service, const GURL& origin, | |
153 net::CompletionCallback* callback) | |
154 : AsyncHelper(service, callback), origin_(origin), | |
155 successes_(0), failures_(0) { | |
156 } | |
157 | |
158 virtual void Start() { | |
159 // We start by listing all caches, continues in OnAllInfo(). | |
160 service_->storage()->GetAllInfo(this); | |
161 } | |
162 | |
163 private: | |
164 // AppCacheStorage::Delegate methods | |
165 virtual void OnAllInfo(AppCacheInfoCollection* collection); | |
166 virtual void OnGroupLoaded( | |
167 appcache::AppCacheGroup* group, const GURL& manifest_url); | |
168 virtual void OnGroupMadeObsolete( | |
169 appcache::AppCacheGroup* group, bool success); | |
170 | |
171 void CacheCompleted(bool success); | |
172 | |
173 GURL origin_; | |
174 AppCacheInfoVector caches_to_delete_; | |
175 int successes_; | |
176 int failures_; | |
177 DISALLOW_COPY_AND_ASSIGN(DeleteOriginHelper); | |
178 }; | |
179 | |
180 void AppCacheService::DeleteOriginHelper::OnAllInfo( | |
181 AppCacheInfoCollection* collection) { | |
182 if (!collection) { | |
183 // Failed to get a listing. | |
184 CallCallback(net::ERR_FAILED); | |
185 delete this; | |
186 return; | |
187 } | |
188 std::map<GURL, AppCacheInfoVector>::iterator found = | |
189 collection->infos_by_origin.find(origin_); | |
190 if (found == collection->infos_by_origin.end()) { | |
191 // No caches for this origin. | |
192 CallCallback(net::OK); | |
193 delete this; | |
194 return; | |
195 } | |
196 | |
197 // We have some caches to delete. | |
198 caches_to_delete_.swap(found->second); | |
199 successes_ = 0; | |
200 failures_ = 0; | |
201 for (AppCacheInfoVector::iterator iter = caches_to_delete_.begin(); | |
202 iter != caches_to_delete_.end(); ++iter) { | |
203 service_->storage()->LoadOrCreateGroup(iter->manifest_url, this); | |
204 } | |
205 } | |
206 | |
207 void AppCacheService::DeleteOriginHelper::OnGroupLoaded( | |
208 appcache::AppCacheGroup* group, const GURL& manifest_url) { | |
209 if (group) { | |
210 group->set_being_deleted(true); | |
211 group->CancelUpdate(); | |
212 service_->storage()->MakeGroupObsolete(group, this); | |
213 } else { | |
214 CacheCompleted(false); | |
215 } | |
216 } | |
217 | |
218 void AppCacheService::DeleteOriginHelper::OnGroupMadeObsolete( | |
219 appcache::AppCacheGroup* group, bool success) { | |
220 CacheCompleted(success); | |
221 } | |
222 | |
223 void AppCacheService::DeleteOriginHelper::CacheCompleted(bool success) { | |
224 if (success) | |
225 ++successes_; | |
226 else | |
227 ++failures_; | |
228 if ((successes_ + failures_) < static_cast<int>(caches_to_delete_.size())) | |
229 return; | |
230 | |
231 CallCallback(!failures_ ? net::OK : net::ERR_FAILED); | |
kinuko
2011/06/02 08:38:06
for readability can we write like (failures_ == 0
michaeln
2011/06/02 22:46:55
i thought it was a style thing, but not entirely s
| |
232 delete this; | |
233 } | |
234 | |
235 | |
146 // GetInfoHelper ------- | 236 // GetInfoHelper ------- |
147 | 237 |
148 class AppCacheService::GetInfoHelper : AsyncHelper { | 238 class AppCacheService::GetInfoHelper : AsyncHelper { |
149 public: | 239 public: |
150 GetInfoHelper( | 240 GetInfoHelper( |
151 AppCacheService* service, AppCacheInfoCollection* collection, | 241 AppCacheService* service, AppCacheInfoCollection* collection, |
152 net::CompletionCallback* callback) | 242 net::CompletionCallback* callback) |
153 : AsyncHelper(service, callback), collection_(collection) { | 243 : AsyncHelper(service, callback), collection_(collection) { |
154 } | 244 } |
155 | 245 |
(...skipping 15 matching lines...) Expand all Loading... | |
171 collection->infos_by_origin.swap(collection_->infos_by_origin); | 261 collection->infos_by_origin.swap(collection_->infos_by_origin); |
172 CallCallback(collection ? net::OK : net::ERR_FAILED); | 262 CallCallback(collection ? net::OK : net::ERR_FAILED); |
173 delete this; | 263 delete this; |
174 } | 264 } |
175 | 265 |
176 | 266 |
177 // AppCacheService ------- | 267 // AppCacheService ------- |
178 | 268 |
179 AppCacheService::AppCacheService(quota::QuotaManagerProxy* quota_manager_proxy) | 269 AppCacheService::AppCacheService(quota::QuotaManagerProxy* quota_manager_proxy) |
180 : appcache_policy_(NULL), quota_manager_proxy_(quota_manager_proxy), | 270 : appcache_policy_(NULL), quota_manager_proxy_(quota_manager_proxy), |
271 quota_client_(NULL), | |
181 request_context_(NULL) { | 272 request_context_(NULL) { |
182 // TODO(michaeln): Create and register our QuotaClient. | 273 if (quota_manager_proxy_) { |
274 quota_client_ = new AppCacheQuotaClient(this); | |
275 quota_manager_proxy_->RegisterClient(quota_client_); | |
276 } | |
183 } | 277 } |
184 | 278 |
185 AppCacheService::~AppCacheService() { | 279 AppCacheService::~AppCacheService() { |
186 DCHECK(backends_.empty()); | 280 DCHECK(backends_.empty()); |
187 std::for_each(pending_helpers_.begin(), | 281 std::for_each(pending_helpers_.begin(), |
188 pending_helpers_.end(), | 282 pending_helpers_.end(), |
189 std::mem_fun(&AsyncHelper::Cancel)); | 283 std::mem_fun(&AsyncHelper::Cancel)); |
190 STLDeleteElements(&pending_helpers_); | 284 STLDeleteElements(&pending_helpers_); |
285 if (quota_client_) | |
286 quota_client_->NotifyAppCacheDestroyed(); | |
191 } | 287 } |
192 | 288 |
193 void AppCacheService::Initialize(const FilePath& cache_directory, | 289 void AppCacheService::Initialize(const FilePath& cache_directory, |
194 base::MessageLoopProxy* cache_thread) { | 290 base::MessageLoopProxy* cache_thread) { |
195 DCHECK(!storage_.get()); | 291 DCHECK(!storage_.get()); |
196 AppCacheStorageImpl* storage = new AppCacheStorageImpl(this); | 292 AppCacheStorageImpl* storage = new AppCacheStorageImpl(this); |
197 storage->Initialize(cache_directory, cache_thread); | 293 storage->Initialize(cache_directory, cache_thread); |
198 storage_.reset(storage); | 294 storage_.reset(storage); |
199 } | 295 } |
200 | 296 |
(...skipping 11 matching lines...) Expand all Loading... | |
212 GetInfoHelper* helper = new GetInfoHelper(this, collection, callback); | 308 GetInfoHelper* helper = new GetInfoHelper(this, collection, callback); |
213 helper->Start(); | 309 helper->Start(); |
214 } | 310 } |
215 | 311 |
216 void AppCacheService::DeleteAppCacheGroup(const GURL& manifest_url, | 312 void AppCacheService::DeleteAppCacheGroup(const GURL& manifest_url, |
217 net::CompletionCallback* callback) { | 313 net::CompletionCallback* callback) { |
218 DeleteHelper* helper = new DeleteHelper(this, manifest_url, callback); | 314 DeleteHelper* helper = new DeleteHelper(this, manifest_url, callback); |
219 helper->Start(); | 315 helper->Start(); |
220 } | 316 } |
221 | 317 |
318 void AppCacheService::DeleteAppCachesForOrigin( | |
319 const GURL& origin, net::CompletionCallback* callback) { | |
320 DeleteOriginHelper* helper = new DeleteOriginHelper(this, origin, callback); | |
321 helper->Start(); | |
322 } | |
323 | |
222 void AppCacheService::set_special_storage_policy( | 324 void AppCacheService::set_special_storage_policy( |
223 quota::SpecialStoragePolicy* policy) { | 325 quota::SpecialStoragePolicy* policy) { |
224 special_storage_policy_ = policy; | 326 special_storage_policy_ = policy; |
225 } | 327 } |
226 | 328 |
227 void AppCacheService::RegisterBackend( | 329 void AppCacheService::RegisterBackend( |
228 AppCacheBackendImpl* backend_impl) { | 330 AppCacheBackendImpl* backend_impl) { |
229 DCHECK(backends_.find(backend_impl->process_id()) == backends_.end()); | 331 DCHECK(backends_.find(backend_impl->process_id()) == backends_.end()); |
230 backends_.insert( | 332 backends_.insert( |
231 BackendMap::value_type(backend_impl->process_id(), backend_impl)); | 333 BackendMap::value_type(backend_impl->process_id(), backend_impl)); |
232 } | 334 } |
233 | 335 |
234 void AppCacheService::UnregisterBackend( | 336 void AppCacheService::UnregisterBackend( |
235 AppCacheBackendImpl* backend_impl) { | 337 AppCacheBackendImpl* backend_impl) { |
236 backends_.erase(backend_impl->process_id()); | 338 backends_.erase(backend_impl->process_id()); |
237 } | 339 } |
238 | 340 |
239 } // namespace appcache | 341 } // namespace appcache |
OLD | NEW |