Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / webkit / browser / appcache / appcache_service.h
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.
4
5 #ifndef WEBKIT_BROWSER_APPCACHE_APPCACHE_SERVICE_H_
6 #define WEBKIT_BROWSER_APPCACHE_APPCACHE_SERVICE_H_
7
8 #include <map>
9 #include <set>
10
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"
22
23 namespace net {
24 class URLRequestContext;
25 }  // namespace net
26
27 namespace base {
28 class FilePath;
29 class MessageLoopProxy;
30 }
31
32 namespace content {
33 FORWARD_DECLARE_TEST(AppCacheServiceTest, ScheduleReinitialize);
34 class AppCacheServiceTest;
35 class AppCacheStorageImplTest;
36 }
37
38 namespace quota {
39 class SpecialStoragePolicy;
40 }
41
42 namespace appcache {
43
44 class AppCacheBackendImpl;
45 class AppCacheExecutableHandlerFactory;
46 class AppCacheQuotaClient;
47 class AppCachePolicy;
48 class AppCacheStorage;
49
50 // Refcounted container to avoid copying the collection in callbacks.
51 struct WEBKIT_STORAGE_BROWSER_EXPORT AppCacheInfoCollection
52     : public base::RefCountedThreadSafe<AppCacheInfoCollection> {
53   AppCacheInfoCollection();
54
55   std::map<GURL, AppCacheInfoVector> infos_by_origin;
56
57  private:
58   friend class base::RefCountedThreadSafe<AppCacheInfoCollection>;
59   virtual ~AppCacheInfoCollection();
60 };
61
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> {
66  public:
67   AppCacheStorage* storage() const { return storage_.get(); }
68  private:
69   friend class AppCacheService;
70   friend class base::RefCounted<AppCacheStorageReference>;
71   AppCacheStorageReference(scoped_ptr<AppCacheStorage> storage);
72   ~AppCacheStorageReference();
73
74   scoped_ptr<AppCacheStorage> storage_;
75 };
76
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 {
81  public:
82
83   class WEBKIT_STORAGE_BROWSER_EXPORT Observer {
84    public:
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
88     // ref provided.
89     virtual void OnServiceReinitialized(
90         AppCacheStorageReference* old_storage_ref) = 0;
91     virtual ~Observer() {}
92   };
93
94   // If not using quota management, the proxy may be NULL.
95   explicit AppCacheService(quota::QuotaManagerProxy* quota_manager_proxy);
96   virtual ~AppCacheService();
97
98   void Initialize(const base::FilePath& cache_directory,
99                   base::MessageLoopProxy* db_thread,
100                   base::MessageLoopProxy* cache_thread);
101
102   void AddObserver(Observer* observer) {
103     observers_.AddObserver(observer);
104   }
105
106   void RemoveObserver(Observer* observer) {
107     observers_.RemoveObserver(observer);
108   }
109
110   // For use in catastrophic failure modes to reboot the appcache system
111   // without relaunching the browser.
112   void ScheduleReinitialize();
113
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);
119
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);
126
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);
134
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);
140
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,
144                              int64 response_id);
145
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;
153   }
154
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;
161   }
162
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_;
169   }
170   void set_handler_factory(
171       AppCacheExecutableHandlerFactory* factory) {
172     handler_factory_ = factory;
173   }
174
175   quota::SpecialStoragePolicy* special_storage_policy() const {
176     return special_storage_policy_.get();
177   }
178   void set_special_storage_policy(quota::SpecialStoragePolicy* policy);
179
180   quota::QuotaManagerProxy* quota_manager_proxy() const {
181     return quota_manager_proxy_.get();
182   }
183
184   AppCacheQuotaClient* quota_client() const {
185     return quota_client_;
186   }
187
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;
195   }
196
197   AppCacheStorage* storage() const { return storage_.get(); }
198
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_; }
202
203  protected:
204   friend class content::AppCacheServiceTest;
205   friend class content::AppCacheStorageImplTest;
206   FRIEND_TEST_ALL_PREFIXES(content::AppCacheServiceTest, ScheduleReinitialize);
207
208   class AsyncHelper;
209   class CanHandleOfflineHelper;
210   class DeleteHelper;
211   class DeleteOriginHelper;
212   class GetInfoHelper;
213   class CheckResponseHelper;
214
215   typedef std::set<AsyncHelper*> PendingAsyncHelpers;
216   typedef std::map<int, AppCacheBackendImpl*> BackendMap;
217
218   void Reinitialize();
219
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_;
239
240   DISALLOW_COPY_AND_ASSIGN(AppCacheService);
241 };
242
243 }  // namespace appcache
244
245 #endif  // WEBKIT_BROWSER_APPCACHE_APPCACHE_SERVICE_H_