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_APPCACHE_APPCACHE_SERVICE_H_
6 #define WEBKIT_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/appcache/appcache_interfaces.h"
17 #include "webkit/appcache/appcache_storage.h"
18 #include "webkit/storage/webkit_storage_export.h"
21 class URLRequestContext
;
26 class MessageLoopProxy
;
30 class QuotaManagerProxy
;
31 class SpecialStoragePolicy
;
36 class AppCacheBackendImpl
;
37 class AppCacheQuotaClient
;
40 // Refcounted container to avoid copying the collection in callbacks.
41 struct WEBKIT_STORAGE_EXPORT AppCacheInfoCollection
42 : public base::RefCountedThreadSafe
<AppCacheInfoCollection
> {
43 AppCacheInfoCollection();
45 std::map
<GURL
, AppCacheInfoVector
> infos_by_origin
;
48 friend class base::RefCountedThreadSafe
<AppCacheInfoCollection
>;
49 virtual ~AppCacheInfoCollection();
52 // Class that manages the application cache service. Sends notifications
53 // to many frontends. One instance per user-profile. Each instance has
54 // exclusive access to its cache_directory on disk.
55 class WEBKIT_STORAGE_EXPORT AppCacheService
{
57 // If not using quota management, the proxy may be NULL.
58 explicit AppCacheService(quota::QuotaManagerProxy
* quota_manager_proxy
);
59 virtual ~AppCacheService();
61 void Initialize(const base::FilePath
& cache_directory
,
62 base::MessageLoopProxy
* db_thread
,
63 base::MessageLoopProxy
* cache_thread
);
65 // Purges any memory not needed.
68 storage_
->PurgeMemory();
71 // Determines if a request for 'url' can be satisfied while offline.
72 // This method always completes asynchronously.
73 void CanHandleMainResourceOffline(const GURL
& url
,
74 const GURL
& first_party
,
75 const net::CompletionCallback
& callback
);
77 // Populates 'collection' with info about all of the appcaches stored
78 // within the service, 'callback' is invoked upon completion. The service
79 // acquires a reference to the 'collection' until until completion.
80 // This method always completes asynchronously.
81 void GetAllAppCacheInfo(AppCacheInfoCollection
* collection
,
82 const net::CompletionCallback
& callback
);
84 // Deletes the group identified by 'manifest_url', 'callback' is
85 // invoked upon completion. Upon completion, the cache group and
86 // any resources within the group are no longer loadable and all
87 // subresource loads for pages associated with a deleted group
88 // will fail. This method always completes asynchronously.
89 void DeleteAppCacheGroup(const GURL
& manifest_url
,
90 const net::CompletionCallback
& callback
);
92 // Deletes all appcaches for the origin, 'callback' is invoked upon
93 // completion. This method always completes asynchronously.
94 // (virtual for unit testing)
95 virtual void DeleteAppCachesForOrigin(
96 const GURL
& origin
, const net::CompletionCallback
& callback
);
98 // Checks the integrity of 'response_id' by reading the headers and data.
99 // If it cannot be read, the cache group for 'manifest_url' is deleted.
100 void CheckAppCacheResponse(const GURL
& manifest_url
, int64 cache_id
,
103 // Context for use during cache updates, should only be accessed
104 // on the IO thread. We do NOT add a reference to the request context,
105 // it is the callers responsibility to ensure that the pointer
106 // remains valid while set.
107 net::URLRequestContext
* request_context() const { return request_context_
; }
108 void set_request_context(net::URLRequestContext
* context
) {
109 request_context_
= context
;
112 // The appcache policy, may be null, in which case access is always allowed.
113 // The service does NOT assume ownership of the policy, it is the callers
114 // responsibility to ensure that the pointer remains valid while set.
115 AppCachePolicy
* appcache_policy() const { return appcache_policy_
; }
116 void set_appcache_policy(AppCachePolicy
* policy
) {
117 appcache_policy_
= policy
;
120 quota::SpecialStoragePolicy
* special_storage_policy() const {
121 return special_storage_policy_
.get();
123 void set_special_storage_policy(quota::SpecialStoragePolicy
* policy
);
125 quota::QuotaManagerProxy
* quota_manager_proxy() const {
126 return quota_manager_proxy_
.get();
129 AppCacheQuotaClient
* quota_client() const {
130 return quota_client_
;
133 // Each child process in chrome uses a distinct backend instance.
134 // See chrome/browser/AppCacheDispatcherHost.
135 void RegisterBackend(AppCacheBackendImpl
* backend_impl
);
136 void UnregisterBackend(AppCacheBackendImpl
* backend_impl
);
137 AppCacheBackendImpl
* GetBackend(int id
) const {
138 BackendMap::const_iterator it
= backends_
.find(id
);
139 return (it
!= backends_
.end()) ? it
->second
: NULL
;
142 AppCacheStorage
* storage() const { return storage_
.get(); }
144 // Disables the exit-time deletion of session-only data.
145 void set_force_keep_session_state() { force_keep_session_state_
= true; }
146 bool force_keep_session_state() const { return force_keep_session_state_
; }
149 friend class AppCacheStorageImplTest
;
150 friend class AppCacheServiceTest
;
153 class CanHandleOfflineHelper
;
155 class DeleteOriginHelper
;
157 class CheckResponseHelper
;
159 typedef std::set
<AsyncHelper
*> PendingAsyncHelpers
;
160 typedef std::map
<int, AppCacheBackendImpl
*> BackendMap
;
162 AppCachePolicy
* appcache_policy_
;
163 AppCacheQuotaClient
* quota_client_
;
164 scoped_ptr
<AppCacheStorage
> storage_
;
165 scoped_refptr
<quota::SpecialStoragePolicy
> special_storage_policy_
;
166 scoped_refptr
<quota::QuotaManagerProxy
> quota_manager_proxy_
;
167 PendingAsyncHelpers pending_helpers_
;
168 BackendMap backends_
; // One 'backend' per child process.
169 // Context for use during cache updates.
170 net::URLRequestContext
* request_context_
;
171 // If true, nothing (not even session-only data) should be deleted on exit.
172 bool force_keep_session_state_
;
174 DISALLOW_COPY_AND_ASSIGN(AppCacheService
);
177 } // namespace appcache
179 #endif // WEBKIT_APPCACHE_APPCACHE_SERVICE_H_