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 "net/base/completion_callback.h"
15 #include "net/base/net_errors.h"
16 #include "webkit/browser/appcache/appcache_storage.h"
17 #include "webkit/browser/webkit_storage_browser_export.h"
18 #include "webkit/common/appcache/appcache_interfaces.h"
21 class URLRequestContext
;
26 class MessageLoopProxy
;
30 class QuotaManagerProxy
;
31 class SpecialStoragePolicy
;
36 class AppCacheBackendImpl
;
37 class AppCacheExecutableHandlerFactory
;
38 class AppCacheQuotaClient
;
41 // Refcounted container to avoid copying the collection in callbacks.
42 struct WEBKIT_STORAGE_BROWSER_EXPORT AppCacheInfoCollection
43 : public base::RefCountedThreadSafe
<AppCacheInfoCollection
> {
44 AppCacheInfoCollection();
46 std::map
<GURL
, AppCacheInfoVector
> infos_by_origin
;
49 friend class base::RefCountedThreadSafe
<AppCacheInfoCollection
>;
50 virtual ~AppCacheInfoCollection();
53 // Class that manages the application cache service. Sends notifications
54 // to many frontends. One instance per user-profile. Each instance has
55 // exclusive access to its cache_directory on disk.
56 class WEBKIT_STORAGE_BROWSER_EXPORT AppCacheService
{
58 // If not using quota management, the proxy may be NULL.
59 explicit AppCacheService(quota::QuotaManagerProxy
* quota_manager_proxy
);
60 virtual ~AppCacheService();
62 void Initialize(const base::FilePath
& cache_directory
,
63 base::MessageLoopProxy
* db_thread
,
64 base::MessageLoopProxy
* cache_thread
);
66 // Purges any memory not needed.
69 storage_
->PurgeMemory();
72 // Determines if a request for 'url' can be satisfied while offline.
73 // This method always completes asynchronously.
74 void CanHandleMainResourceOffline(const GURL
& url
,
75 const GURL
& first_party
,
76 const net::CompletionCallback
& callback
);
78 // Populates 'collection' with info about all of the appcaches stored
79 // within the service, 'callback' is invoked upon completion. The service
80 // acquires a reference to the 'collection' until until completion.
81 // This method always completes asynchronously.
82 void GetAllAppCacheInfo(AppCacheInfoCollection
* collection
,
83 const net::CompletionCallback
& callback
);
85 // Deletes the group identified by 'manifest_url', 'callback' is
86 // invoked upon completion. Upon completion, the cache group and
87 // any resources within the group are no longer loadable and all
88 // subresource loads for pages associated with a deleted group
89 // will fail. This method always completes asynchronously.
90 void DeleteAppCacheGroup(const GURL
& manifest_url
,
91 const net::CompletionCallback
& callback
);
93 // Deletes all appcaches for the origin, 'callback' is invoked upon
94 // completion. This method always completes asynchronously.
95 // (virtual for unit testing)
96 virtual void DeleteAppCachesForOrigin(
97 const GURL
& origin
, const net::CompletionCallback
& callback
);
99 // Checks the integrity of 'response_id' by reading the headers and data.
100 // If it cannot be read, the cache group for 'manifest_url' is deleted.
101 void CheckAppCacheResponse(const GURL
& manifest_url
, int64 cache_id
,
104 // Context for use during cache updates, should only be accessed
105 // on the IO thread. We do NOT add a reference to the request context,
106 // it is the callers responsibility to ensure that the pointer
107 // remains valid while set.
108 net::URLRequestContext
* request_context() const { return request_context_
; }
109 void set_request_context(net::URLRequestContext
* context
) {
110 request_context_
= context
;
113 // The appcache policy, may be null, in which case access is always allowed.
114 // The service does NOT assume ownership of the policy, it is the callers
115 // responsibility to ensure that the pointer remains valid while set.
116 AppCachePolicy
* appcache_policy() const { return appcache_policy_
; }
117 void set_appcache_policy(AppCachePolicy
* policy
) {
118 appcache_policy_
= policy
;
121 // The factory may be null, in which case invocations of exe handlers
122 // will result in an error response.
123 // The service does NOT assume ownership of the factory, it is the callers
124 // responsibility to ensure that the pointer remains valid while set.
125 AppCacheExecutableHandlerFactory
* handler_factory() const {
126 return handler_factory_
;
128 void set_handler_factory(
129 AppCacheExecutableHandlerFactory
* factory
) {
130 handler_factory_
= factory
;
133 quota::SpecialStoragePolicy
* special_storage_policy() const {
134 return special_storage_policy_
.get();
136 void set_special_storage_policy(quota::SpecialStoragePolicy
* policy
);
138 quota::QuotaManagerProxy
* quota_manager_proxy() const {
139 return quota_manager_proxy_
.get();
142 AppCacheQuotaClient
* quota_client() const {
143 return quota_client_
;
146 // Each child process in chrome uses a distinct backend instance.
147 // See chrome/browser/AppCacheDispatcherHost.
148 void RegisterBackend(AppCacheBackendImpl
* backend_impl
);
149 void UnregisterBackend(AppCacheBackendImpl
* backend_impl
);
150 AppCacheBackendImpl
* GetBackend(int id
) const {
151 BackendMap::const_iterator it
= backends_
.find(id
);
152 return (it
!= backends_
.end()) ? it
->second
: NULL
;
155 AppCacheStorage
* storage() const { return storage_
.get(); }
157 // Disables the exit-time deletion of session-only data.
158 void set_force_keep_session_state() { force_keep_session_state_
= true; }
159 bool force_keep_session_state() const { return force_keep_session_state_
; }
162 friend class AppCacheStorageImplTest
;
163 friend class AppCacheServiceTest
;
166 class CanHandleOfflineHelper
;
168 class DeleteOriginHelper
;
170 class CheckResponseHelper
;
172 typedef std::set
<AsyncHelper
*> PendingAsyncHelpers
;
173 typedef std::map
<int, AppCacheBackendImpl
*> BackendMap
;
175 AppCachePolicy
* appcache_policy_
;
176 AppCacheQuotaClient
* quota_client_
;
177 AppCacheExecutableHandlerFactory
* handler_factory_
;
178 scoped_ptr
<AppCacheStorage
> storage_
;
179 scoped_refptr
<quota::SpecialStoragePolicy
> special_storage_policy_
;
180 scoped_refptr
<quota::QuotaManagerProxy
> quota_manager_proxy_
;
181 PendingAsyncHelpers pending_helpers_
;
182 BackendMap backends_
; // One 'backend' per child process.
183 // Context for use during cache updates.
184 net::URLRequestContext
* request_context_
;
185 // If true, nothing (not even session-only data) should be deleted on exit.
186 bool force_keep_session_state_
;
188 DISALLOW_COPY_AND_ASSIGN(AppCacheService
);
191 } // namespace appcache
193 #endif // WEBKIT_BROWSER_APPCACHE_APPCACHE_SERVICE_H_