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
3 // found in the LICENSE file.
5 #ifndef WEBKIT_BROWSER_APPCACHE_APPCACHE_SERVICE_H_
6 #define WEBKIT_BROWSER_APPCACHE_APPCACHE_SERVICE_H_
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/time/time.h"
16 #include "base/timer/timer.h"
17 #include "net/base/completion_callback.h"
18 #include "net/base/net_errors.h"
19 #include "webkit/browser/quota/quota_manager_proxy.h"
20 #include "webkit/browser/webkit_storage_browser_export.h"
21 #include "webkit/common/appcache/appcache_interfaces.h"
24 class URLRequestContext;
29 class MessageLoopProxy;
33 FORWARD_DECLARE_TEST(AppCacheServiceTest, ScheduleReinitialize);
34 class AppCacheServiceTest;
35 class AppCacheStorageImplTest;
39 class SpecialStoragePolicy;
44 class AppCacheBackendImpl;
45 class AppCacheExecutableHandlerFactory;
46 class AppCacheQuotaClient;
48 class AppCacheStorage;
50 // Refcounted container to avoid copying the collection in callbacks.
51 struct WEBKIT_STORAGE_BROWSER_EXPORT AppCacheInfoCollection
52 : public base::RefCountedThreadSafe<AppCacheInfoCollection> {
53 AppCacheInfoCollection();
55 std::map<GURL, AppCacheInfoVector> infos_by_origin;
58 friend class base::RefCountedThreadSafe<AppCacheInfoCollection>;
59 virtual ~AppCacheInfoCollection();
62 // Refcounted container to manage the lifetime of the old storage instance
63 // during Reinitialization.
64 class WEBKIT_STORAGE_BROWSER_EXPORT AppCacheStorageReference :
65 public base::RefCounted<AppCacheStorageReference> {
67 AppCacheStorage* storage() const { return storage_.get(); }
69 friend class AppCacheService;
70 friend class base::RefCounted<AppCacheStorageReference>;
71 AppCacheStorageReference(scoped_ptr<AppCacheStorage> storage);
72 ~AppCacheStorageReference();
74 scoped_ptr<AppCacheStorage> storage_;
77 // Class that manages the application cache service. Sends notifications
78 // to many frontends. One instance per user-profile. Each instance has
79 // exclusive access to its cache_directory on disk.
80 class WEBKIT_STORAGE_BROWSER_EXPORT AppCacheService {
83 class WEBKIT_STORAGE_BROWSER_EXPORT Observer {
85 // An observer method to inform consumers of reinitialzation. Managing
86 // the lifetime of the old storage instance is a delicate process.
87 // Consumers can keep the old disabled instance alive by hanging on to the
89 virtual void OnServiceReinitialized(
90 AppCacheStorageReference* old_storage_ref) = 0;
91 virtual ~Observer() {}
94 // If not using quota management, the proxy may be NULL.
95 explicit AppCacheService(quota::QuotaManagerProxy* quota_manager_proxy);
96 virtual ~AppCacheService();
98 void Initialize(const base::FilePath& cache_directory,
99 base::MessageLoopProxy* db_thread,
100 base::MessageLoopProxy* cache_thread);
102 void AddObserver(Observer* observer) {
103 observers_.AddObserver(observer);
106 void RemoveObserver(Observer* observer) {
107 observers_.RemoveObserver(observer);
110 // For use in catastrophic failure modes to reboot the appcache system
111 // without relaunching the browser.
112 void ScheduleReinitialize();
114 // Determines if a request for 'url' can be satisfied while offline.
115 // This method always completes asynchronously.
116 void CanHandleMainResourceOffline(const GURL& url,
117 const GURL& first_party,
118 const net::CompletionCallback& callback);
120 // Populates 'collection' with info about all of the appcaches stored
121 // within the service, 'callback' is invoked upon completion. The service
122 // acquires a reference to the 'collection' until until completion.
123 // This method always completes asynchronously.
124 void GetAllAppCacheInfo(AppCacheInfoCollection* collection,
125 const net::CompletionCallback& callback);
127 // Deletes the group identified by 'manifest_url', 'callback' is
128 // invoked upon completion. Upon completion, the cache group and
129 // any resources within the group are no longer loadable and all
130 // subresource loads for pages associated with a deleted group
131 // will fail. This method always completes asynchronously.
132 void DeleteAppCacheGroup(const GURL& manifest_url,
133 const net::CompletionCallback& callback);
135 // Deletes all appcaches for the origin, 'callback' is invoked upon
136 // completion. This method always completes asynchronously.
137 // (virtual for unit testing)
138 virtual void DeleteAppCachesForOrigin(
139 const GURL& origin, const net::CompletionCallback& callback);
141 // Checks the integrity of 'response_id' by reading the headers and data.
142 // If it cannot be read, the cache group for 'manifest_url' is deleted.
143 void CheckAppCacheResponse(const GURL& manifest_url, int64 cache_id,
146 // Context for use during cache updates, should only be accessed
147 // on the IO thread. We do NOT add a reference to the request context,
148 // it is the callers responsibility to ensure that the pointer
149 // remains valid while set.
150 net::URLRequestContext* request_context() const { return request_context_; }
151 void set_request_context(net::URLRequestContext* context) {
152 request_context_ = context;
155 // The appcache policy, may be null, in which case access is always allowed.
156 // The service does NOT assume ownership of the policy, it is the callers
157 // responsibility to ensure that the pointer remains valid while set.
158 AppCachePolicy* appcache_policy() const { return appcache_policy_; }
159 void set_appcache_policy(AppCachePolicy* policy) {
160 appcache_policy_ = policy;
163 // The factory may be null, in which case invocations of exe handlers
164 // will result in an error response.
165 // The service does NOT assume ownership of the factory, it is the callers
166 // responsibility to ensure that the pointer remains valid while set.
167 AppCacheExecutableHandlerFactory* handler_factory() const {
168 return handler_factory_;
170 void set_handler_factory(
171 AppCacheExecutableHandlerFactory* factory) {
172 handler_factory_ = factory;
175 quota::SpecialStoragePolicy* special_storage_policy() const {
176 return special_storage_policy_.get();
178 void set_special_storage_policy(quota::SpecialStoragePolicy* policy);
180 quota::QuotaManagerProxy* quota_manager_proxy() const {
181 return quota_manager_proxy_.get();
184 AppCacheQuotaClient* quota_client() const {
185 return quota_client_;
188 // Each child process in chrome uses a distinct backend instance.
189 // See chrome/browser/AppCacheDispatcherHost.
190 void RegisterBackend(AppCacheBackendImpl* backend_impl);
191 void UnregisterBackend(AppCacheBackendImpl* backend_impl);
192 AppCacheBackendImpl* GetBackend(int id) const {
193 BackendMap::const_iterator it = backends_.find(id);
194 return (it != backends_.end()) ? it->second : NULL;
197 AppCacheStorage* storage() const { return storage_.get(); }
199 // Disables the exit-time deletion of session-only data.
200 void set_force_keep_session_state() { force_keep_session_state_ = true; }
201 bool force_keep_session_state() const { return force_keep_session_state_; }
204 friend class content::AppCacheServiceTest;
205 friend class content::AppCacheStorageImplTest;
206 FRIEND_TEST_ALL_PREFIXES(content::AppCacheServiceTest, ScheduleReinitialize);
209 class CanHandleOfflineHelper;
211 class DeleteOriginHelper;
213 class CheckResponseHelper;
215 typedef std::set<AsyncHelper*> PendingAsyncHelpers;
216 typedef std::map<int, AppCacheBackendImpl*> BackendMap;
220 base::FilePath cache_directory_;
221 scoped_refptr<base::MessageLoopProxy> db_thread_;
222 scoped_refptr<base::MessageLoopProxy> cache_thread_;
223 AppCachePolicy* appcache_policy_;
224 AppCacheQuotaClient* quota_client_;
225 AppCacheExecutableHandlerFactory* handler_factory_;
226 scoped_ptr<AppCacheStorage> storage_;
227 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_;
228 scoped_refptr<quota::QuotaManagerProxy> quota_manager_proxy_;
229 PendingAsyncHelpers pending_helpers_;
230 BackendMap backends_; // One 'backend' per child process.
231 // Context for use during cache updates.
232 net::URLRequestContext* request_context_;
233 // If true, nothing (not even session-only data) should be deleted on exit.
234 bool force_keep_session_state_;
235 base::Time last_reinit_time_;
236 base::TimeDelta next_reinit_delay_;
237 base::OneShotTimer<AppCacheService> reinit_timer_;
238 ObserverList<Observer> observers_;
240 DISALLOW_COPY_AND_ASSIGN(AppCacheService);
243 } // namespace appcache
245 #endif // WEBKIT_BROWSER_APPCACHE_APPCACHE_SERVICE_H_