blob: f1f8c0ba74c8af680c884d4c6cbd4a588c9daf55 [file] [log] [blame]
[email protected]27a112c2011-01-06 04:19:301// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]49672df2009-08-26 23:10:092// 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]145ee1d2010-08-19 08:22:598#include <map>
[email protected]d68a4fc62010-03-05 23:40:029#include <set>
[email protected]d68a4fc62010-03-05 23:40:0210
[email protected]cd851592011-12-15 19:54:3811#include "base/gtest_prod_util.h"
[email protected]3b63f8f42011-03-28 01:54:1512#include "base/memory/ref_counted.h"
13#include "base/memory/scoped_ptr.h"
[email protected]d68a4fc62010-03-05 23:40:0214#include "base/time.h"
15#include "net/base/completion_callback.h"
16#include "net/base/net_errors.h"
[email protected]b2ad61c2011-09-14 02:47:5417#include "webkit/appcache/appcache_export.h"
[email protected]ec5c1922010-07-28 03:14:3718#include "webkit/appcache/appcache_interfaces.h"
[email protected]e7dff7ba2009-10-09 03:25:5119#include "webkit/appcache/appcache_storage.h"
[email protected]49672df2009-08-26 23:10:0920
[email protected]864b1362010-08-19 03:49:3821class FilePath;
[email protected]27a112c2011-01-06 04:19:3022
23namespace net {
[email protected]e2fc3d22009-09-22 19:50:3124class URLRequestContext;
[email protected]27a112c2011-01-06 04:19:3025} // namespace net
[email protected]e2fc3d22009-09-22 19:50:3126
[email protected]f5ad47a12010-06-07 22:42:0127namespace base {
28class MessageLoopProxy;
29}
30
[email protected]19eb80152011-02-26 00:28:4331namespace quota {
[email protected]9830d772011-05-16 18:39:3932class QuotaManagerProxy;
[email protected]19eb80152011-02-26 00:28:4333class SpecialStoragePolicy;
34}
35
[email protected]49672df2009-08-26 23:10:0936namespace appcache {
37
[email protected]49672df2009-08-26 23:10:0938class AppCacheBackendImpl;
[email protected]e0184cb2011-06-10 23:29:0939class AppCacheQuotaClient;
[email protected]ea776d022010-02-17 22:20:3640class AppCachePolicy;
[email protected]49672df2009-08-26 23:10:0941
[email protected]d68a4fc62010-03-05 23:40:0242// Refcounted container to avoid copying the collection in callbacks.
[email protected]b2ad61c2011-09-14 02:47:5443struct APPCACHE_EXPORT AppCacheInfoCollection
[email protected]d68a4fc62010-03-05 23:40:0244 : public base::RefCountedThreadSafe<AppCacheInfoCollection> {
[email protected]20f0487a2010-09-30 20:06:3045 AppCacheInfoCollection();
46 virtual ~AppCacheInfoCollection();
47
[email protected]d68a4fc62010-03-05 23:40:0248 std::map<GURL, AppCacheInfoVector> infos_by_origin;
49};
50
[email protected]49672df2009-08-26 23:10:0951// Class that manages the application cache service. Sends notifications
[email protected]6c270d42009-09-15 20:00:1152// to many frontends. One instance per user-profile. Each instance has
[email protected]a2c92a1c2012-04-03 12:32:1453// exclusive access to its cache_directory on disk.
[email protected]b2ad61c2011-09-14 02:47:5454class APPCACHE_EXPORT AppCacheService {
[email protected]49672df2009-08-26 23:10:0955 public:
[email protected]9830d772011-05-16 18:39:3956 // If not using quota management, the proxy may be NULL.
57 explicit AppCacheService(quota::QuotaManagerProxy* quota_manager_proxy);
[email protected]49672df2009-08-26 23:10:0958 virtual ~AppCacheService();
59
[email protected]f5ad47a12010-06-07 22:42:0160 void Initialize(const FilePath& cache_directory,
[email protected]78f73502011-09-12 18:26:3161 base::MessageLoopProxy* db_thread,
[email protected]f5ad47a12010-06-07 22:42:0162 base::MessageLoopProxy* cache_thread);
[email protected]23f1ef12009-09-01 22:30:3063
[email protected]d68a4fc62010-03-05 23:40:0264 // Purges any memory not needed.
[email protected]520cdd72010-01-13 22:14:4665 void PurgeMemory() {
66 if (storage_.get())
67 storage_->PurgeMemory();
68 }
69
[email protected]beb13402011-01-25 00:58:3870 // 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]0a608842011-09-08 10:55:1973 const GURL& first_party,
[email protected]d4d57df2011-11-30 20:33:5274 const net::CompletionCallback& callback);
[email protected]beb13402011-01-25 00:58:3875
[email protected]d68a4fc62010-03-05 23:40:0276 // 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]18d4f552011-12-02 02:48:3181 const net::CompletionCallback& callback);
[email protected]d68a4fc62010-03-05 23:40:0282
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]8b211962011-12-02 02:57:2589 const net::CompletionCallback& callback);
[email protected]d68a4fc62010-03-05 23:40:0290
[email protected]e0184cb2011-06-10 23:29:0991 // Deletes all appcaches for the origin, 'callback' is invoked upon
92 // completion. This method always completes asynchronously.
[email protected]123f2742011-12-02 04:26:5293 // (virtual for unit testing)
94 virtual void DeleteAppCachesForOrigin(
95 const GURL& origin, const net::CompletionCallback& callback);
[email protected]e0184cb2011-06-10 23:29:0996
[email protected]20d06f62011-07-22 23:57:3597 // 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]6c270d42009-09-15 20:00:11102 // Context for use during cache updates, should only be accessed
[email protected]e2fc3d22009-09-22 19:50:31103 // 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]27a112c2011-01-06 04:19:30106 net::URLRequestContext* request_context() const { return request_context_; }
107 void set_request_context(net::URLRequestContext* context) {
[email protected]e2fc3d22009-09-22 19:50:31108 request_context_ = context;
[email protected]6c270d42009-09-15 20:00:11109 }
110
[email protected]ea776d022010-02-17 22:20:36111 // 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]19eb80152011-02-26 00:28:43119 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]9830d772011-05-16 18:39:39124 quota::QuotaManagerProxy* quota_manager_proxy() const {
125 return quota_manager_proxy_.get();
126 }
127
[email protected]e0184cb2011-06-10 23:29:09128 AppCacheQuotaClient* quota_client() const {
129 return quota_client_;
130 }
131
[email protected]d68a4fc62010-03-05 23:40:02132 // Each child process in chrome uses a distinct backend instance.
133 // See chrome/browser/AppCacheDispatcherHost.
[email protected]23f1ef12009-09-01 22:30:30134 void RegisterBackend(AppCacheBackendImpl* backend_impl);
135 void UnregisterBackend(AppCacheBackendImpl* backend_impl);
[email protected]e7dff7ba2009-10-09 03:25:51136 AppCacheBackendImpl* GetBackend(int id) const {
137 BackendMap::const_iterator it = backends_.find(id);
[email protected]23f1ef12009-09-01 22:30:30138 return (it != backends_.end()) ? it->second : NULL;
139 }
140
[email protected]e7dff7ba2009-10-09 03:25:51141 AppCacheStorage* storage() const { return storage_.get(); }
[email protected]23f1ef12009-09-01 22:30:30142
[email protected]00617712011-07-22 10:21:06143 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]e9005772011-12-08 10:44:03147 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]e7dff7ba2009-10-09 03:25:51154 protected:
[email protected]e0184cb2011-06-10 23:29:09155 friend class AppCacheStorageImplTest;
156 friend class AppCacheServiceTest;
157
[email protected]d68a4fc62010-03-05 23:40:02158 class AsyncHelper;
[email protected]d4d57df2011-11-30 20:33:52159 class NewAsyncHelper;
[email protected]beb13402011-01-25 00:58:38160 class CanHandleOfflineHelper;
[email protected]d68a4fc62010-03-05 23:40:02161 class DeleteHelper;
[email protected]e0184cb2011-06-10 23:29:09162 class DeleteOriginHelper;
[email protected]d68a4fc62010-03-05 23:40:02163 class GetInfoHelper;
[email protected]20d06f62011-07-22 23:57:35164 class CheckResponseHelper;
[email protected]d68a4fc62010-03-05 23:40:02165
166 typedef std::set<AsyncHelper*> PendingAsyncHelpers;
[email protected]d4d57df2011-11-30 20:33:52167 typedef std::set<NewAsyncHelper*> PendingNewAsyncHelpers;
[email protected]6ed2a5a52010-11-03 02:29:14168 typedef std::map<int, AppCacheBackendImpl*> BackendMap;
[email protected]d68a4fc62010-03-05 23:40:02169
[email protected]ea776d022010-02-17 22:20:36170 AppCachePolicy* appcache_policy_;
[email protected]e0184cb2011-06-10 23:29:09171 AppCacheQuotaClient* quota_client_;
[email protected]e7dff7ba2009-10-09 03:25:51172 scoped_ptr<AppCacheStorage> storage_;
[email protected]19eb80152011-02-26 00:28:43173 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_;
[email protected]9830d772011-05-16 18:39:39174 scoped_refptr<quota::QuotaManagerProxy> quota_manager_proxy_;
[email protected]d68a4fc62010-03-05 23:40:02175 PendingAsyncHelpers pending_helpers_;
[email protected]d4d57df2011-11-30 20:33:52176 PendingNewAsyncHelpers pending_new_helpers_;
[email protected]6ed2a5a52010-11-03 02:29:14177 BackendMap backends_; // One 'backend' per child process.
[email protected]27a112c2011-01-06 04:19:30178 // Context for use during cache updates.
179 net::URLRequestContext* request_context_;
[email protected]00617712011-07-22 10:21:06180 bool clear_local_state_on_exit_;
[email protected]e9005772011-12-08 10:44:03181 // If true, nothing (not even session-only data) should be deleted on exit.
182 bool save_session_state_;
[email protected]e7dff7ba2009-10-09 03:25:51183
184 DISALLOW_COPY_AND_ASSIGN(AppCacheService);
[email protected]49672df2009-08-26 23:10:09185};
186
187} // namespace appcache
188
189#endif // WEBKIT_APPCACHE_APPCACHE_SERVICE_H_