[email protected] | 27a112c | 2011-01-06 04:19:30 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 49672df | 2009-08-26 23:10:09 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef WEBKIT_APPCACHE_APPCACHE_SERVICE_H_ |
| 6 | #define WEBKIT_APPCACHE_APPCACHE_SERVICE_H_ |
| 7 | |
[email protected] | 145ee1d | 2010-08-19 08:22:59 | [diff] [blame] | 8 | #include <map> |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 9 | #include <set> |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 10 | |
[email protected] | cd85159 | 2011-12-15 19:54:38 | [diff] [blame] | 11 | #include "base/gtest_prod_util.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
| 13 | #include "base/memory/scoped_ptr.h" |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 14 | #include "base/time.h" |
| 15 | #include "net/base/completion_callback.h" |
| 16 | #include "net/base/net_errors.h" |
[email protected] | b2ad61c | 2011-09-14 02:47:54 | [diff] [blame] | 17 | #include "webkit/appcache/appcache_export.h" |
[email protected] | ec5c192 | 2010-07-28 03:14:37 | [diff] [blame] | 18 | #include "webkit/appcache/appcache_interfaces.h" |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 19 | #include "webkit/appcache/appcache_storage.h" |
[email protected] | 49672df | 2009-08-26 23:10:09 | [diff] [blame] | 20 | |
[email protected] | 864b136 | 2010-08-19 03:49:38 | [diff] [blame] | 21 | class FilePath; |
[email protected] | 27a112c | 2011-01-06 04:19:30 | [diff] [blame] | 22 | |
| 23 | namespace net { |
[email protected] | e2fc3d2 | 2009-09-22 19:50:31 | [diff] [blame] | 24 | class URLRequestContext; |
[email protected] | 27a112c | 2011-01-06 04:19:30 | [diff] [blame] | 25 | } // namespace net |
[email protected] | e2fc3d2 | 2009-09-22 19:50:31 | [diff] [blame] | 26 | |
[email protected] | f5ad47a1 | 2010-06-07 22:42:01 | [diff] [blame] | 27 | namespace base { |
| 28 | class MessageLoopProxy; |
| 29 | } |
| 30 | |
[email protected] | 19eb8015 | 2011-02-26 00:28:43 | [diff] [blame] | 31 | namespace quota { |
[email protected] | 9830d77 | 2011-05-16 18:39:39 | [diff] [blame] | 32 | class QuotaManagerProxy; |
[email protected] | 19eb8015 | 2011-02-26 00:28:43 | [diff] [blame] | 33 | class SpecialStoragePolicy; |
| 34 | } |
| 35 | |
[email protected] | 49672df | 2009-08-26 23:10:09 | [diff] [blame] | 36 | namespace appcache { |
| 37 | |
[email protected] | 49672df | 2009-08-26 23:10:09 | [diff] [blame] | 38 | class AppCacheBackendImpl; |
[email protected] | e0184cb | 2011-06-10 23:29:09 | [diff] [blame] | 39 | class AppCacheQuotaClient; |
[email protected] | ea776d02 | 2010-02-17 22:20:36 | [diff] [blame] | 40 | class AppCachePolicy; |
[email protected] | 49672df | 2009-08-26 23:10:09 | [diff] [blame] | 41 | |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 42 | // Refcounted container to avoid copying the collection in callbacks. |
[email protected] | b2ad61c | 2011-09-14 02:47:54 | [diff] [blame] | 43 | struct APPCACHE_EXPORT AppCacheInfoCollection |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 44 | : public base::RefCountedThreadSafe<AppCacheInfoCollection> { |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 45 | AppCacheInfoCollection(); |
| 46 | virtual ~AppCacheInfoCollection(); |
| 47 | |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 48 | std::map<GURL, AppCacheInfoVector> infos_by_origin; |
| 49 | }; |
| 50 | |
[email protected] | 49672df | 2009-08-26 23:10:09 | [diff] [blame] | 51 | // Class that manages the application cache service. Sends notifications |
[email protected] | 6c270d4 | 2009-09-15 20:00:11 | [diff] [blame] | 52 | // to many frontends. One instance per user-profile. Each instance has |
[email protected] | a2c92a1c | 2012-04-03 12:32:14 | [diff] [blame] | 53 | // exclusive access to its cache_directory on disk. |
[email protected] | b2ad61c | 2011-09-14 02:47:54 | [diff] [blame] | 54 | class APPCACHE_EXPORT AppCacheService { |
[email protected] | 49672df | 2009-08-26 23:10:09 | [diff] [blame] | 55 | public: |
[email protected] | 9830d77 | 2011-05-16 18:39:39 | [diff] [blame] | 56 | // If not using quota management, the proxy may be NULL. |
| 57 | explicit AppCacheService(quota::QuotaManagerProxy* quota_manager_proxy); |
[email protected] | 49672df | 2009-08-26 23:10:09 | [diff] [blame] | 58 | virtual ~AppCacheService(); |
| 59 | |
[email protected] | f5ad47a1 | 2010-06-07 22:42:01 | [diff] [blame] | 60 | void Initialize(const FilePath& cache_directory, |
[email protected] | 78f7350 | 2011-09-12 18:26:31 | [diff] [blame] | 61 | base::MessageLoopProxy* db_thread, |
[email protected] | f5ad47a1 | 2010-06-07 22:42:01 | [diff] [blame] | 62 | base::MessageLoopProxy* cache_thread); |
[email protected] | 23f1ef1 | 2009-09-01 22:30:30 | [diff] [blame] | 63 | |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 64 | // Purges any memory not needed. |
[email protected] | 520cdd7 | 2010-01-13 22:14:46 | [diff] [blame] | 65 | void PurgeMemory() { |
| 66 | if (storage_.get()) |
| 67 | storage_->PurgeMemory(); |
| 68 | } |
| 69 | |
[email protected] | beb1340 | 2011-01-25 00:58:38 | [diff] [blame] | 70 | // Determines if a request for 'url' can be satisfied while offline. |
| 71 | // This method always completes asynchronously. |
| 72 | void CanHandleMainResourceOffline(const GURL& url, |
[email protected] | 0a60884 | 2011-09-08 10:55:19 | [diff] [blame] | 73 | const GURL& first_party, |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 74 | const net::CompletionCallback& callback); |
[email protected] | beb1340 | 2011-01-25 00:58:38 | [diff] [blame] | 75 | |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 76 | // Populates 'collection' with info about all of the appcaches stored |
| 77 | // within the service, 'callback' is invoked upon completion. The service |
| 78 | // acquires a reference to the 'collection' until until completion. |
| 79 | // This method always completes asynchronously. |
| 80 | void GetAllAppCacheInfo(AppCacheInfoCollection* collection, |
[email protected] | 18d4f55 | 2011-12-02 02:48:31 | [diff] [blame] | 81 | const net::CompletionCallback& callback); |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 82 | |
| 83 | // Deletes the group identified by 'manifest_url', 'callback' is |
| 84 | // invoked upon completion. Upon completion, the cache group and |
| 85 | // any resources within the group are no longer loadable and all |
| 86 | // subresource loads for pages associated with a deleted group |
| 87 | // will fail. This method always completes asynchronously. |
| 88 | void DeleteAppCacheGroup(const GURL& manifest_url, |
[email protected] | 8b21196 | 2011-12-02 02:57:25 | [diff] [blame] | 89 | const net::CompletionCallback& callback); |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 90 | |
[email protected] | e0184cb | 2011-06-10 23:29:09 | [diff] [blame] | 91 | // Deletes all appcaches for the origin, 'callback' is invoked upon |
| 92 | // completion. This method always completes asynchronously. |
[email protected] | 123f274 | 2011-12-02 04:26:52 | [diff] [blame] | 93 | // (virtual for unit testing) |
| 94 | virtual void DeleteAppCachesForOrigin( |
| 95 | const GURL& origin, const net::CompletionCallback& callback); |
[email protected] | e0184cb | 2011-06-10 23:29:09 | [diff] [blame] | 96 | |
[email protected] | 20d06f6 | 2011-07-22 23:57:35 | [diff] [blame] | 97 | // Checks the integrity of 'response_id' by reading the headers and data. |
| 98 | // If it cannot be read, the cache group for 'manifest_url' is deleted. |
| 99 | void CheckAppCacheResponse(const GURL& manifest_url_, int64 cache_id, |
| 100 | int64 response_id); |
| 101 | |
[email protected] | 6c270d4 | 2009-09-15 20:00:11 | [diff] [blame] | 102 | // Context for use during cache updates, should only be accessed |
[email protected] | e2fc3d2 | 2009-09-22 19:50:31 | [diff] [blame] | 103 | // on the IO thread. We do NOT add a reference to the request context, |
| 104 | // it is the callers responsibility to ensure that the pointer |
| 105 | // remains valid while set. |
[email protected] | 27a112c | 2011-01-06 04:19:30 | [diff] [blame] | 106 | net::URLRequestContext* request_context() const { return request_context_; } |
| 107 | void set_request_context(net::URLRequestContext* context) { |
[email protected] | e2fc3d2 | 2009-09-22 19:50:31 | [diff] [blame] | 108 | request_context_ = context; |
[email protected] | 6c270d4 | 2009-09-15 20:00:11 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | ea776d02 | 2010-02-17 22:20:36 | [diff] [blame] | 111 | // The appcache policy, may be null, in which case access is always allowed. |
| 112 | // The service does NOT assume ownership of the policy, it is the callers |
| 113 | // responsibility to ensure that the pointer remains valid while set. |
| 114 | AppCachePolicy* appcache_policy() const { return appcache_policy_; } |
| 115 | void set_appcache_policy(AppCachePolicy* policy) { |
| 116 | appcache_policy_ = policy; |
| 117 | } |
| 118 | |
[email protected] | 19eb8015 | 2011-02-26 00:28:43 | [diff] [blame] | 119 | quota::SpecialStoragePolicy* special_storage_policy() const { |
| 120 | return special_storage_policy_.get(); |
| 121 | } |
| 122 | void set_special_storage_policy(quota::SpecialStoragePolicy* policy); |
| 123 | |
[email protected] | 9830d77 | 2011-05-16 18:39:39 | [diff] [blame] | 124 | quota::QuotaManagerProxy* quota_manager_proxy() const { |
| 125 | return quota_manager_proxy_.get(); |
| 126 | } |
| 127 | |
[email protected] | e0184cb | 2011-06-10 23:29:09 | [diff] [blame] | 128 | AppCacheQuotaClient* quota_client() const { |
| 129 | return quota_client_; |
| 130 | } |
| 131 | |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 132 | // Each child process in chrome uses a distinct backend instance. |
| 133 | // See chrome/browser/AppCacheDispatcherHost. |
[email protected] | 23f1ef1 | 2009-09-01 22:30:30 | [diff] [blame] | 134 | void RegisterBackend(AppCacheBackendImpl* backend_impl); |
| 135 | void UnregisterBackend(AppCacheBackendImpl* backend_impl); |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 136 | AppCacheBackendImpl* GetBackend(int id) const { |
| 137 | BackendMap::const_iterator it = backends_.find(id); |
[email protected] | 23f1ef1 | 2009-09-01 22:30:30 | [diff] [blame] | 138 | return (it != backends_.end()) ? it->second : NULL; |
| 139 | } |
| 140 | |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 141 | AppCacheStorage* storage() const { return storage_.get(); } |
[email protected] | 23f1ef1 | 2009-09-01 22:30:30 | [diff] [blame] | 142 | |
[email protected] | 0061771 | 2011-07-22 10:21:06 | [diff] [blame] | 143 | bool clear_local_state_on_exit() const { return clear_local_state_on_exit_; } |
| 144 | void set_clear_local_state_on_exit(bool clear_local_state_on_exit) { |
| 145 | clear_local_state_on_exit_ = clear_local_state_on_exit; } |
| 146 | |
[email protected] | e900577 | 2011-12-08 10:44:03 | [diff] [blame] | 147 | bool save_session_state() const { return save_session_state_; } |
| 148 | // If |save_session_state| is true, disables the exit-time deletion for all |
| 149 | // data (also session-only data). |
| 150 | void set_save_session_state(bool save_session_state) { |
| 151 | save_session_state_ = save_session_state; |
| 152 | } |
| 153 | |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 154 | protected: |
[email protected] | e0184cb | 2011-06-10 23:29:09 | [diff] [blame] | 155 | friend class AppCacheStorageImplTest; |
| 156 | friend class AppCacheServiceTest; |
| 157 | |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 158 | class AsyncHelper; |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 159 | class NewAsyncHelper; |
[email protected] | beb1340 | 2011-01-25 00:58:38 | [diff] [blame] | 160 | class CanHandleOfflineHelper; |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 161 | class DeleteHelper; |
[email protected] | e0184cb | 2011-06-10 23:29:09 | [diff] [blame] | 162 | class DeleteOriginHelper; |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 163 | class GetInfoHelper; |
[email protected] | 20d06f6 | 2011-07-22 23:57:35 | [diff] [blame] | 164 | class CheckResponseHelper; |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 165 | |
| 166 | typedef std::set<AsyncHelper*> PendingAsyncHelpers; |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 167 | typedef std::set<NewAsyncHelper*> PendingNewAsyncHelpers; |
[email protected] | 6ed2a5a5 | 2010-11-03 02:29:14 | [diff] [blame] | 168 | typedef std::map<int, AppCacheBackendImpl*> BackendMap; |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 169 | |
[email protected] | ea776d02 | 2010-02-17 22:20:36 | [diff] [blame] | 170 | AppCachePolicy* appcache_policy_; |
[email protected] | e0184cb | 2011-06-10 23:29:09 | [diff] [blame] | 171 | AppCacheQuotaClient* quota_client_; |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 172 | scoped_ptr<AppCacheStorage> storage_; |
[email protected] | 19eb8015 | 2011-02-26 00:28:43 | [diff] [blame] | 173 | scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_; |
[email protected] | 9830d77 | 2011-05-16 18:39:39 | [diff] [blame] | 174 | scoped_refptr<quota::QuotaManagerProxy> quota_manager_proxy_; |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 175 | PendingAsyncHelpers pending_helpers_; |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 176 | PendingNewAsyncHelpers pending_new_helpers_; |
[email protected] | 6ed2a5a5 | 2010-11-03 02:29:14 | [diff] [blame] | 177 | BackendMap backends_; // One 'backend' per child process. |
[email protected] | 27a112c | 2011-01-06 04:19:30 | [diff] [blame] | 178 | // Context for use during cache updates. |
| 179 | net::URLRequestContext* request_context_; |
[email protected] | 0061771 | 2011-07-22 10:21:06 | [diff] [blame] | 180 | bool clear_local_state_on_exit_; |
[email protected] | e900577 | 2011-12-08 10:44:03 | [diff] [blame] | 181 | // If true, nothing (not even session-only data) should be deleted on exit. |
| 182 | bool save_session_state_; |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 183 | |
| 184 | DISALLOW_COPY_AND_ASSIGN(AppCacheService); |
[email protected] | 49672df | 2009-08-26 23:10:09 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | } // namespace appcache |
| 188 | |
| 189 | #endif // WEBKIT_APPCACHE_APPCACHE_SERVICE_H_ |