Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / content / browser / appcache / appcache_service_impl.h
blobcb61f3a47d6c21d5cac2c1cbb2e79ad9a3585bc3
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_
8 #include <map>
9 #include <set>
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"
24 namespace base {
25 class FilePath;
26 class SingleThreadTaskRunner;
27 } // namespace base
29 namespace net {
30 class URLRequestContext;
31 } // namespace net
33 namespace storage {
34 class SpecialStoragePolicy;
35 } // namespace storage
37 namespace content {
38 FORWARD_DECLARE_TEST(AppCacheServiceImplTest, ScheduleReinitialize);
39 class AppCacheBackendImpl;
40 class AppCacheExecutableHandlerFactory;
41 class AppCacheQuotaClient;
42 class AppCachePolicy;
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> {
51 public:
52 AppCacheStorage* storage() const { return storage_.get(); }
53 private:
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 {
67 public:
69 class CONTENT_EXPORT Observer {
70 public:
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
74 // ref provided.
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;
84 void Initialize(
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(
103 const GURL& url,
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,
120 int64 response_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 // Disables the exit-time deletion of session-only data.
176 void set_force_keep_session_state() { force_keep_session_state_ = true; }
177 bool force_keep_session_state() const { return force_keep_session_state_; }
179 protected:
180 friend class content::AppCacheServiceImplTest;
181 friend class content::AppCacheStorageImplTest;
182 FRIEND_TEST_ALL_PREFIXES(content::AppCacheServiceImplTest,
183 ScheduleReinitialize);
185 class AsyncHelper;
186 class CanHandleOfflineHelper;
187 class DeleteHelper;
188 class DeleteOriginHelper;
189 class GetInfoHelper;
190 class CheckResponseHelper;
192 typedef std::set<AsyncHelper*> PendingAsyncHelpers;
193 typedef std::map<int, AppCacheBackendImpl*> BackendMap;
195 void Reinitialize();
197 base::FilePath cache_directory_;
198 scoped_refptr<base::SingleThreadTaskRunner> db_thread_;
199 scoped_refptr<base::SingleThreadTaskRunner> cache_thread_;
200 AppCachePolicy* appcache_policy_;
201 AppCacheQuotaClient* quota_client_;
202 AppCacheExecutableHandlerFactory* handler_factory_;
203 scoped_ptr<AppCacheStorage> storage_;
204 scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy_;
205 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_;
206 PendingAsyncHelpers pending_helpers_;
207 BackendMap backends_; // One 'backend' per child process.
208 // Context for use during cache updates.
209 net::URLRequestContext* request_context_;
210 // If true, nothing (not even session-only data) should be deleted on exit.
211 bool force_keep_session_state_;
212 base::Time last_reinit_time_;
213 base::TimeDelta next_reinit_delay_;
214 base::OneShotTimer<AppCacheServiceImpl> reinit_timer_;
215 ObserverList<Observer> observers_;
217 DISALLOW_COPY_AND_ASSIGN(AppCacheServiceImpl);
220 } // namespace content
222 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_SERVICE_IMPL_H_