| 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 29 matching lines...) Expand all  Loading... | 
|   51  |   52  | 
|   52   static void DeferredCallCallback(net::CompletionCallback* callback, int rv) { |   53   static void DeferredCallCallback(net::CompletionCallback* callback, int rv) { | 
|   53     callback->Run(rv); |   54     callback->Run(rv); | 
|   54   } |   55   } | 
|   55  |   56  | 
|   56   AppCacheService* service_; |   57   AppCacheService* service_; | 
|   57   net::CompletionCallback* callback_; |   58   net::CompletionCallback* callback_; | 
|   58 }; |   59 }; | 
|   59  |   60  | 
|   60 void AppCacheService::AsyncHelper::Cancel() { |   61 void AppCacheService::AsyncHelper::Cancel() { | 
|   61   CallCallback(net::ERR_ABORTED); |   62   if (callback_) { | 
 |   63     callback_->Run(net::ERR_ABORTED); | 
 |   64     callback_ = NULL; | 
 |   65   } | 
|   62   service_->storage()->CancelDelegateCallbacks(this); |   66   service_->storage()->CancelDelegateCallbacks(this); | 
|   63   service_ = NULL; |   67   service_ = NULL; | 
|   64 } |   68 } | 
|   65  |   69  | 
|   66 // CanHandleOfflineHelper ------- |   70 // CanHandleOfflineHelper ------- | 
|   67  |   71  | 
|   68 class AppCacheService::CanHandleOfflineHelper : AsyncHelper { |   72 class AppCacheService::CanHandleOfflineHelper : AsyncHelper { | 
|   69  public: |   73  public: | 
|   70   CanHandleOfflineHelper( |   74   CanHandleOfflineHelper( | 
|   71       AppCacheService* service, const GURL& url, |   75       AppCacheService* service, const GURL& url, | 
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  136     delete this; |  140     delete this; | 
|  137   } |  141   } | 
|  138 } |  142 } | 
|  139  |  143  | 
|  140 void AppCacheService::DeleteHelper::OnGroupMadeObsolete( |  144 void AppCacheService::DeleteHelper::OnGroupMadeObsolete( | 
|  141       appcache::AppCacheGroup* group, bool success) { |  145       appcache::AppCacheGroup* group, bool success) { | 
|  142   CallCallback(success ? net::OK : net::ERR_FAILED); |  146   CallCallback(success ? net::OK : net::ERR_FAILED); | 
|  143   delete this; |  147   delete this; | 
|  144 } |  148 } | 
|  145  |  149  | 
 |  150 // DeleteOriginHelper ------- | 
 |  151  | 
 |  152 class AppCacheService::DeleteOriginHelper : public AsyncHelper { | 
 |  153  public: | 
 |  154   DeleteOriginHelper( | 
 |  155       AppCacheService* service, const GURL& origin, | 
 |  156       net::CompletionCallback* callback) | 
 |  157       : AsyncHelper(service, callback), origin_(origin), | 
 |  158         successes_(0), failures_(0) { | 
 |  159   } | 
 |  160  | 
 |  161   virtual void Start() { | 
 |  162     // We start by listing all caches, continues in OnAllInfo(). | 
 |  163     service_->storage()->GetAllInfo(this); | 
 |  164   } | 
 |  165  | 
 |  166  private: | 
 |  167   // AppCacheStorage::Delegate methods | 
 |  168   virtual void OnAllInfo(AppCacheInfoCollection* collection); | 
 |  169   virtual void OnGroupLoaded( | 
 |  170       appcache::AppCacheGroup* group, const GURL& manifest_url); | 
 |  171   virtual void OnGroupMadeObsolete( | 
 |  172       appcache::AppCacheGroup* group, bool success); | 
 |  173  | 
 |  174   void CacheCompleted(bool success); | 
 |  175  | 
 |  176   GURL origin_; | 
 |  177   AppCacheInfoVector caches_to_delete_; | 
 |  178   int successes_; | 
 |  179   int failures_; | 
 |  180   DISALLOW_COPY_AND_ASSIGN(DeleteOriginHelper); | 
 |  181 }; | 
 |  182  | 
 |  183 void AppCacheService::DeleteOriginHelper::OnAllInfo( | 
 |  184       AppCacheInfoCollection* collection) { | 
 |  185   if (!collection) { | 
 |  186     // Failed to get a listing. | 
 |  187     CallCallback(net::ERR_FAILED); | 
 |  188     delete this; | 
 |  189     return; | 
 |  190   } | 
 |  191   std::map<GURL, AppCacheInfoVector>::iterator found = | 
 |  192       collection->infos_by_origin.find(origin_); | 
 |  193   if (found == collection->infos_by_origin.end() || found->second.empty()) { | 
 |  194     // No caches for this origin. | 
 |  195     CallCallback(net::OK); | 
 |  196     delete this; | 
 |  197     return; | 
 |  198   } | 
 |  199  | 
 |  200   // We have some caches to delete. | 
 |  201   caches_to_delete_.swap(found->second); | 
 |  202   successes_ = 0; | 
 |  203   failures_ = 0; | 
 |  204   for (AppCacheInfoVector::iterator iter = caches_to_delete_.begin(); | 
 |  205        iter != caches_to_delete_.end(); ++iter) { | 
 |  206     service_->storage()->LoadOrCreateGroup(iter->manifest_url, this); | 
 |  207   } | 
 |  208 } | 
 |  209  | 
 |  210 void AppCacheService::DeleteOriginHelper::OnGroupLoaded( | 
 |  211       appcache::AppCacheGroup* group, const GURL& manifest_url) { | 
 |  212   if (group) { | 
 |  213     group->set_being_deleted(true); | 
 |  214     group->CancelUpdate(); | 
 |  215     service_->storage()->MakeGroupObsolete(group, this); | 
 |  216   } else { | 
 |  217     CacheCompleted(false); | 
 |  218   } | 
 |  219 } | 
 |  220  | 
 |  221 void AppCacheService::DeleteOriginHelper::OnGroupMadeObsolete( | 
 |  222       appcache::AppCacheGroup* group, bool success) { | 
 |  223   CacheCompleted(success); | 
 |  224 } | 
 |  225  | 
 |  226 void AppCacheService::DeleteOriginHelper::CacheCompleted(bool success) { | 
 |  227   if (success) | 
 |  228     ++successes_; | 
 |  229   else | 
 |  230     ++failures_; | 
 |  231   if ((successes_ + failures_) < static_cast<int>(caches_to_delete_.size())) | 
 |  232     return; | 
 |  233  | 
 |  234   CallCallback(!failures_ ? net::OK : net::ERR_FAILED); | 
 |  235   delete this; | 
 |  236 } | 
 |  237  | 
 |  238  | 
|  146 // GetInfoHelper ------- |  239 // GetInfoHelper ------- | 
|  147  |  240  | 
|  148 class AppCacheService::GetInfoHelper : AsyncHelper { |  241 class AppCacheService::GetInfoHelper : AsyncHelper { | 
|  149  public: |  242  public: | 
|  150   GetInfoHelper( |  243   GetInfoHelper( | 
|  151       AppCacheService* service, AppCacheInfoCollection* collection, |  244       AppCacheService* service, AppCacheInfoCollection* collection, | 
|  152       net::CompletionCallback* callback) |  245       net::CompletionCallback* callback) | 
|  153       : AsyncHelper(service, callback), collection_(collection) { |  246       : AsyncHelper(service, callback), collection_(collection) { | 
|  154   } |  247   } | 
|  155  |  248  | 
| (...skipping 14 matching lines...) Expand all  Loading... | 
|  170   if (collection) |  263   if (collection) | 
|  171     collection->infos_by_origin.swap(collection_->infos_by_origin); |  264     collection->infos_by_origin.swap(collection_->infos_by_origin); | 
|  172   CallCallback(collection ? net::OK : net::ERR_FAILED); |  265   CallCallback(collection ? net::OK : net::ERR_FAILED); | 
|  173   delete this; |  266   delete this; | 
|  174 } |  267 } | 
|  175  |  268  | 
|  176  |  269  | 
|  177 // AppCacheService ------- |  270 // AppCacheService ------- | 
|  178  |  271  | 
|  179 AppCacheService::AppCacheService(quota::QuotaManagerProxy* quota_manager_proxy) |  272 AppCacheService::AppCacheService(quota::QuotaManagerProxy* quota_manager_proxy) | 
|  180     : appcache_policy_(NULL), quota_manager_proxy_(quota_manager_proxy), |  273     : appcache_policy_(NULL), quota_client_(NULL), | 
 |  274       quota_manager_proxy_(quota_manager_proxy), | 
|  181       request_context_(NULL)  { |  275       request_context_(NULL)  { | 
|  182   // TODO(michaeln): Create and register our QuotaClient. |  276   if (quota_manager_proxy_) { | 
 |  277     quota_client_ = new AppCacheQuotaClient(this); | 
 |  278     quota_manager_proxy_->RegisterClient(quota_client_); | 
 |  279   } | 
|  183 } |  280 } | 
|  184  |  281  | 
|  185 AppCacheService::~AppCacheService() { |  282 AppCacheService::~AppCacheService() { | 
|  186   DCHECK(backends_.empty()); |  283   DCHECK(backends_.empty()); | 
|  187   std::for_each(pending_helpers_.begin(), |  284   std::for_each(pending_helpers_.begin(), | 
|  188                 pending_helpers_.end(), |  285                 pending_helpers_.end(), | 
|  189                 std::mem_fun(&AsyncHelper::Cancel)); |  286                 std::mem_fun(&AsyncHelper::Cancel)); | 
|  190   STLDeleteElements(&pending_helpers_); |  287   STLDeleteElements(&pending_helpers_); | 
 |  288   if (quota_client_) | 
 |  289     quota_client_->NotifyAppCacheDestroyed(); | 
|  191 } |  290 } | 
|  192  |  291  | 
|  193 void AppCacheService::Initialize(const FilePath& cache_directory, |  292 void AppCacheService::Initialize(const FilePath& cache_directory, | 
|  194                                  base::MessageLoopProxy* cache_thread) { |  293                                  base::MessageLoopProxy* cache_thread) { | 
|  195   DCHECK(!storage_.get()); |  294   DCHECK(!storage_.get()); | 
|  196   AppCacheStorageImpl* storage = new AppCacheStorageImpl(this); |  295   AppCacheStorageImpl* storage = new AppCacheStorageImpl(this); | 
|  197   storage->Initialize(cache_directory, cache_thread); |  296   storage->Initialize(cache_directory, cache_thread); | 
|  198   storage_.reset(storage); |  297   storage_.reset(storage); | 
|  199 } |  298 } | 
|  200  |  299  | 
| (...skipping 11 matching lines...) Expand all  Loading... | 
|  212   GetInfoHelper* helper = new GetInfoHelper(this, collection, callback); |  311   GetInfoHelper* helper = new GetInfoHelper(this, collection, callback); | 
|  213   helper->Start(); |  312   helper->Start(); | 
|  214 } |  313 } | 
|  215  |  314  | 
|  216 void AppCacheService::DeleteAppCacheGroup(const GURL& manifest_url, |  315 void AppCacheService::DeleteAppCacheGroup(const GURL& manifest_url, | 
|  217                                           net::CompletionCallback* callback) { |  316                                           net::CompletionCallback* callback) { | 
|  218   DeleteHelper* helper = new DeleteHelper(this, manifest_url, callback); |  317   DeleteHelper* helper = new DeleteHelper(this, manifest_url, callback); | 
|  219   helper->Start(); |  318   helper->Start(); | 
|  220 } |  319 } | 
|  221  |  320  | 
 |  321 void AppCacheService::DeleteAppCachesForOrigin( | 
 |  322     const GURL& origin,  net::CompletionCallback* callback) { | 
 |  323   DeleteOriginHelper* helper = new DeleteOriginHelper(this, origin, callback); | 
 |  324   helper->Start(); | 
 |  325 } | 
 |  326  | 
|  222 void AppCacheService::set_special_storage_policy( |  327 void AppCacheService::set_special_storage_policy( | 
|  223     quota::SpecialStoragePolicy* policy) { |  328     quota::SpecialStoragePolicy* policy) { | 
|  224   special_storage_policy_ = policy; |  329   special_storage_policy_ = policy; | 
|  225 } |  330 } | 
|  226  |  331  | 
|  227 void AppCacheService::RegisterBackend( |  332 void AppCacheService::RegisterBackend( | 
|  228     AppCacheBackendImpl* backend_impl) { |  333     AppCacheBackendImpl* backend_impl) { | 
|  229   DCHECK(backends_.find(backend_impl->process_id()) == backends_.end()); |  334   DCHECK(backends_.find(backend_impl->process_id()) == backends_.end()); | 
|  230   backends_.insert( |  335   backends_.insert( | 
|  231       BackendMap::value_type(backend_impl->process_id(), backend_impl)); |  336       BackendMap::value_type(backend_impl->process_id(), backend_impl)); | 
|  232 } |  337 } | 
|  233  |  338  | 
|  234 void AppCacheService::UnregisterBackend( |  339 void AppCacheService::UnregisterBackend( | 
|  235     AppCacheBackendImpl* backend_impl) { |  340     AppCacheBackendImpl* backend_impl) { | 
|  236   backends_.erase(backend_impl->process_id()); |  341   backends_.erase(backend_impl->process_id()); | 
|  237 } |  342 } | 
|  238  |  343  | 
|  239 }  // namespace appcache |  344 }  // namespace appcache | 
| OLD | NEW |