1 // Copyright 2014 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 CONTENT_BROWSER_APPCACHE_APPCACHE_SERVICE_IMPL_H_
6 #define CONTENT_BROWSER_APPCACHE_APPCACHE_SERVICE_IMPL_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 "content/common/appcache_interfaces.h"
18 #include "content/common/content_export.h"
19 #include "content/public/browser/appcache_service.h"
20 #include "net/base/completion_callback.h"
21 #include "net/base/net_errors.h"
22 #include "storage/browser/quota/quota_manager_proxy.h"
26 class SingleThreadTaskRunner
;
30 class URLRequestContext
;
34 class SpecialStoragePolicy
;
35 } // namespace storage
38 FORWARD_DECLARE_TEST(AppCacheServiceImplTest
, ScheduleReinitialize
);
39 class AppCacheBackendImpl
;
40 class AppCacheExecutableHandlerFactory
;
41 class AppCacheQuotaClient
;
43 class AppCacheServiceImplTest
;
44 class AppCacheStorageImplTest
;
45 class AppCacheStorage
;
47 // Refcounted container to manage the lifetime of the old storage instance
48 // during Reinitialization.
49 class CONTENT_EXPORT AppCacheStorageReference
50 : public base::RefCounted
<AppCacheStorageReference
> {
52 AppCacheStorage
* storage() const { return storage_
.get(); }
54 friend class AppCacheServiceImpl
;
55 friend class base::RefCounted
<AppCacheStorageReference
>;
56 AppCacheStorageReference(scoped_ptr
<AppCacheStorage
> storage
);
57 ~AppCacheStorageReference();
59 scoped_ptr
<AppCacheStorage
> storage_
;
62 // Class that manages the application cache service. Sends notifications
63 // to many frontends. One instance per user-profile. Each instance has
64 // exclusive access to its cache_directory on disk.
65 class CONTENT_EXPORT AppCacheServiceImpl
66 : public AppCacheService
{
69 class CONTENT_EXPORT Observer
{
71 // An observer method to inform consumers of reinitialzation. Managing
72 // the lifetime of the old storage instance is a delicate process.
73 // Consumers can keep the old disabled instance alive by hanging on to the
75 virtual void OnServiceReinitialized(
76 AppCacheStorageReference
* old_storage_ref
) = 0;
77 virtual ~Observer() {}
80 // If not using quota management, the proxy may be NULL.
81 explicit AppCacheServiceImpl(storage::QuotaManagerProxy
* quota_manager_proxy
);
82 ~AppCacheServiceImpl() override
;
85 const base::FilePath
& cache_directory
,
86 const scoped_refptr
<base::SingleThreadTaskRunner
>& db_thread
,
87 const scoped_refptr
<base::SingleThreadTaskRunner
>& cache_thread
);
89 void AddObserver(Observer
* observer
) {
90 observers_
.AddObserver(observer
);
93 void RemoveObserver(Observer
* observer
) {
94 observers_
.RemoveObserver(observer
);
97 // For use in catastrophic failure modes to reboot the appcache system
98 // without relaunching the browser.
99 void ScheduleReinitialize();
101 // AppCacheService implementation:
102 void CanHandleMainResourceOffline(
104 const GURL
& first_party
,
105 const net::CompletionCallback
& callback
) override
;
106 void GetAllAppCacheInfo(AppCacheInfoCollection
* collection
,
107 const net::CompletionCallback
& callback
) override
;
108 void DeleteAppCacheGroup(const GURL
& manifest_url
,
109 const net::CompletionCallback
& callback
) override
;
111 // Deletes all appcaches for the origin, 'callback' is invoked upon
112 // completion. This method always completes asynchronously.
113 // (virtual for unit testing)
114 virtual void DeleteAppCachesForOrigin(
115 const GURL
& origin
, const net::CompletionCallback
& callback
);
117 // Checks the integrity of 'response_id' by reading the headers and data.
118 // If it cannot be read, the cache group for 'manifest_url' is deleted.
119 void CheckAppCacheResponse(const GURL
& manifest_url
, int64 cache_id
,
122 // Context for use during cache updates, should only be accessed
123 // on the IO thread. We do NOT add a reference to the request context,
124 // it is the callers responsibility to ensure that the pointer
125 // remains valid while set.
126 net::URLRequestContext
* request_context() const { return request_context_
; }
127 void set_request_context(net::URLRequestContext
* context
) {
128 request_context_
= context
;
131 // The appcache policy, may be null, in which case access is always allowed.
132 // The service does NOT assume ownership of the policy, it is the callers
133 // responsibility to ensure that the pointer remains valid while set.
134 AppCachePolicy
* appcache_policy() const { return appcache_policy_
; }
135 void set_appcache_policy(AppCachePolicy
* policy
) {
136 appcache_policy_
= policy
;
139 // The factory may be null, in which case invocations of exe handlers
140 // will result in an error response.
141 // The service does NOT assume ownership of the factory, it is the callers
142 // responsibility to ensure that the pointer remains valid while set.
143 AppCacheExecutableHandlerFactory
* handler_factory() const {
144 return handler_factory_
;
146 void set_handler_factory(
147 AppCacheExecutableHandlerFactory
* factory
) {
148 handler_factory_
= factory
;
151 storage::SpecialStoragePolicy
* special_storage_policy() const {
152 return special_storage_policy_
.get();
154 void set_special_storage_policy(storage::SpecialStoragePolicy
* policy
);
156 storage::QuotaManagerProxy
* quota_manager_proxy() const {
157 return quota_manager_proxy_
.get();
160 AppCacheQuotaClient
* quota_client() const {
161 return quota_client_
;
164 // Each child process in chrome uses a distinct backend instance.
165 // See chrome/browser/AppCacheDispatcherHost.
166 void RegisterBackend(AppCacheBackendImpl
* backend_impl
);
167 void UnregisterBackend(AppCacheBackendImpl
* backend_impl
);
168 AppCacheBackendImpl
* GetBackend(int id
) const {
169 BackendMap::const_iterator it
= backends_
.find(id
);
170 return (it
!= backends_
.end()) ? it
->second
: NULL
;
173 AppCacheStorage
* storage() const { return storage_
.get(); }
175 base::WeakPtr
<AppCacheServiceImpl
> AsWeakPtr() {
176 return weak_factory_
.GetWeakPtr();
179 // Disables the exit-time deletion of session-only data.
180 void set_force_keep_session_state() { force_keep_session_state_
= true; }
181 bool force_keep_session_state() const { return force_keep_session_state_
; }
184 friend class content::AppCacheServiceImplTest
;
185 friend class content::AppCacheStorageImplTest
;
186 FRIEND_TEST_ALL_PREFIXES(content::AppCacheServiceImplTest
,
187 ScheduleReinitialize
);
190 class CanHandleOfflineHelper
;
192 class DeleteOriginHelper
;
194 class CheckResponseHelper
;
196 typedef std::set
<AsyncHelper
*> PendingAsyncHelpers
;
197 typedef std::map
<int, AppCacheBackendImpl
*> BackendMap
;
201 base::FilePath cache_directory_
;
202 scoped_refptr
<base::SingleThreadTaskRunner
> db_thread_
;
203 scoped_refptr
<base::SingleThreadTaskRunner
> cache_thread_
;
204 AppCachePolicy
* appcache_policy_
;
205 AppCacheQuotaClient
* quota_client_
;
206 AppCacheExecutableHandlerFactory
* handler_factory_
;
207 scoped_ptr
<AppCacheStorage
> storage_
;
208 scoped_refptr
<storage::SpecialStoragePolicy
> special_storage_policy_
;
209 scoped_refptr
<storage::QuotaManagerProxy
> quota_manager_proxy_
;
210 PendingAsyncHelpers pending_helpers_
;
211 BackendMap backends_
; // One 'backend' per child process.
212 // Context for use during cache updates.
213 net::URLRequestContext
* request_context_
;
214 // If true, nothing (not even session-only data) should be deleted on exit.
215 bool force_keep_session_state_
;
216 base::Time last_reinit_time_
;
217 base::TimeDelta next_reinit_delay_
;
218 base::OneShotTimer
<AppCacheServiceImpl
> reinit_timer_
;
219 base::ObserverList
<Observer
> observers_
;
222 base::WeakPtrFactory
<AppCacheServiceImpl
> weak_factory_
;
224 DISALLOW_COPY_AND_ASSIGN(AppCacheServiceImpl
);
227 } // namespace content
229 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_SERVICE_IMPL_H_