Update V8 to version 4.6.55.
[chromium-blink-merge.git] / content / browser / appcache / appcache_storage.h
blob41668cbca4a9a4194fc76a6bb21b8fa8f8557ec5
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 CONTENT_BROWSER_APPCACHE_APPCACHE_STORAGE_H_
6 #define CONTENT_BROWSER_APPCACHE_APPCACHE_STORAGE_H_
8 #include <map>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "content/browser/appcache/appcache_working_set.h"
17 #include "content/common/content_export.h"
18 #include "net/base/completion_callback.h"
20 class GURL;
22 namespace content {
23 FORWARD_DECLARE_TEST(AppCacheStorageTest, DelegateReferences);
24 FORWARD_DECLARE_TEST(AppCacheStorageTest, UsageMap);
25 class AppCache;
26 class AppCacheEntry;
27 class AppCacheGroup;
28 class AppCacheQuotaClientTest;
29 class AppCacheResponseMetadataWriter;
30 class AppCacheResponseReader;
31 class AppCacheResponseTest;
32 class AppCacheResponseWriter;
33 class AppCacheServiceImpl;
34 class AppCacheStorageTest;
35 struct AppCacheInfoCollection;
36 struct HttpResponseInfoIOBuffer;
38 class CONTENT_EXPORT AppCacheStorage {
39 public:
40 typedef std::map<GURL, int64> UsageMap;
42 class CONTENT_EXPORT Delegate {
43 public:
44 // If retrieval fails, 'collection' will be NULL.
45 virtual void OnAllInfo(AppCacheInfoCollection* collection) {}
47 // If a load fails the 'cache' will be NULL.
48 virtual void OnCacheLoaded(AppCache* cache, int64 cache_id) {}
50 // If a load fails the 'group' will be NULL.
51 virtual void OnGroupLoaded(
52 AppCacheGroup* group, const GURL& manifest_url) {}
54 // If successfully stored 'success' will be true.
55 virtual void OnGroupAndNewestCacheStored(
56 AppCacheGroup* group, AppCache* newest_cache, bool success,
57 bool would_exceed_quota) {}
59 // If the operation fails, success will be false.
60 virtual void OnGroupMadeObsolete(AppCacheGroup* group,
61 bool success,
62 int response_code) {}
64 // If a load fails the 'response_info' will be NULL.
65 virtual void OnResponseInfoLoaded(
66 AppCacheResponseInfo* response_info, int64 response_id) {}
68 // If no response is found, entry.response_id() and
69 // fallback_entry.response_id() will be kAppCacheNoResponseId.
70 // If the response is the entry for an intercept or fallback
71 // namespace, the url of the namespece entry is returned.
72 // If a response is found, the cache id and manifest url of the
73 // containing cache and group are also returned.
74 virtual void OnMainResponseFound(
75 const GURL& url, const AppCacheEntry& entry,
76 const GURL& namespace_entry_url, const AppCacheEntry& fallback_entry,
77 int64 cache_id, int64 group_id, const GURL& mainfest_url) {}
79 protected:
80 virtual ~Delegate() {}
83 explicit AppCacheStorage(AppCacheServiceImpl* service);
84 virtual ~AppCacheStorage();
86 // Schedules a task to retrieve basic info about all groups and caches
87 // stored in the system. Upon completion the delegate will be called
88 // with the results.
89 virtual void GetAllInfo(Delegate* delegate) = 0;
91 // Schedules a cache to be loaded from storage. Upon load completion
92 // the delegate will be called back. If the cache already resides in
93 // memory, the delegate will be called back immediately without returning
94 // to the message loop. If the load fails, the delegate will be called
95 // back with a NULL cache pointer.
96 virtual void LoadCache(int64 id, Delegate* delegate) = 0;
98 // Schedules a group and its newest cache, if any, to be loaded from storage.
99 // Upon load completion the delegate will be called back. If the group
100 // and newest cache already reside in memory, the delegate will be called
101 // back immediately without returning to the message loop. If the load fails,
102 // the delegate will be called back with a NULL group pointer.
103 virtual void LoadOrCreateGroup(
104 const GURL& manifest_url, Delegate* delegate) = 0;
106 // Schedules response info to be loaded from storage.
107 // Upon load completion the delegate will be called back. If the data
108 // already resides in memory, the delegate will be called back
109 // immediately without returning to the message loop. If the load fails,
110 // the delegate will be called back with a NULL pointer.
111 virtual void LoadResponseInfo(
112 const GURL& manifest_url, int64 group_id, int64 response_id,
113 Delegate* delegate);
115 // Schedules a group and its newest complete cache to be initially stored or
116 // incrementally updated with new changes. Upon completion the delegate
117 // will be called back. A group without a newest cache cannot be stored.
118 // It's a programming error to call this method without a newest cache. A
119 // side effect of storing a new newest cache is the removal of the group's
120 // old caches and responses from persistent storage (although they may still
121 // linger in the in-memory working set until no longer needed). The new
122 // cache will be added as the group's newest complete cache only if storage
123 // succeeds.
124 virtual void StoreGroupAndNewestCache(
125 AppCacheGroup* group, AppCache* newest_cache, Delegate* delegate) = 0;
127 // Schedules a query to identify a response for a main request. Upon
128 // completion the delegate will be called back.
129 virtual void FindResponseForMainRequest(
130 const GURL& url,
131 const GURL& preferred_manifest_url,
132 Delegate* delegate) = 0;
134 // Performs an immediate lookup of the in-memory cache to
135 // identify a response for a sub resource request.
136 virtual void FindResponseForSubRequest(
137 AppCache* cache, const GURL& url,
138 AppCacheEntry* found_entry, AppCacheEntry* found_fallback_entry,
139 bool* found_network_namespace) = 0;
141 // Immediately updates in-memory storage, if the cache is in memory,
142 // and schedules a task to update persistent storage. If the cache is
143 // already scheduled to be loaded, upon loading completion the entry
144 // will be marked. There is no delegate completion callback.
145 virtual void MarkEntryAsForeign(const GURL& entry_url, int64 cache_id) = 0;
147 // Schedules a task to update persistent storage and doom the group and all
148 // related caches and responses for deletion. Upon completion the in-memory
149 // instance is marked as obsolete and the delegate callback is called.
150 virtual void MakeGroupObsolete(AppCacheGroup* group,
151 Delegate* delegate,
152 int response_code) = 0;
154 // Schedules a task to update persistent storage with the times of the first
155 // evictable error and last successful full update check.
156 virtual void StoreEvictionTimes(AppCacheGroup* group) = 0;
158 // Cancels all pending callbacks for the delegate. The delegate callbacks
159 // will not be invoked after, however any scheduled operations will still
160 // take place. The callbacks for subsequently scheduled operations are
161 // unaffected.
162 void CancelDelegateCallbacks(Delegate* delegate) {
163 DelegateReference* delegate_reference = GetDelegateReference(delegate);
164 if (delegate_reference)
165 delegate_reference->CancelReference();
168 // Creates a reader to read a response from storage.
169 virtual AppCacheResponseReader* CreateResponseReader(
170 const GURL& manifest_url, int64 group_id, int64 response_id) = 0;
172 // Creates a writer to write a new response to storage. This call
173 // establishes a new response id.
174 virtual AppCacheResponseWriter* CreateResponseWriter(
175 const GURL& manifest_url, int64 group_id) = 0;
177 // Creates a metadata writer to write metadata of response to storage.
178 virtual AppCacheResponseMetadataWriter* CreateResponseMetadataWriter(
179 int64 group_id,
180 int64 response_id) = 0;
182 // Schedules the lazy deletion of responses and saves the ids
183 // persistently such that the responses will be deleted upon restart
184 // if they aren't deleted prior to shutdown.
185 virtual void DoomResponses(
186 const GURL& manifest_url, const std::vector<int64>& response_ids) = 0;
188 // Schedules the lazy deletion of responses without persistently saving
189 // the response ids.
190 virtual void DeleteResponses(
191 const GURL& manifest_url, const std::vector<int64>& response_ids) = 0;
193 // Generates unique storage ids for different object types.
194 int64 NewCacheId() {
195 return ++last_cache_id_;
197 int64 NewGroupId() {
198 return ++last_group_id_;
201 // The working set of object instances currently in memory.
202 AppCacheWorkingSet* working_set() { return &working_set_; }
204 // A map of origins to usage.
205 const UsageMap* usage_map() { return &usage_map_; }
207 // Simple ptr back to the service object that owns us.
208 AppCacheServiceImpl* service() { return service_; }
210 protected:
211 friend class content::AppCacheQuotaClientTest;
212 friend class content::AppCacheResponseTest;
213 friend class content::AppCacheStorageTest;
215 // Helper to call a collection of delegates.
216 #define FOR_EACH_DELEGATE(delegates, func_and_args) \
217 do { \
218 for (DelegateReferenceVector::iterator it = delegates.begin(); \
219 it != delegates.end(); ++it) { \
220 if (it->get()->delegate) \
221 it->get()->delegate->func_and_args; \
223 } while (0)
225 // Helper used to manage multiple references to a 'delegate' and to
226 // allow all pending callbacks to that delegate to be easily cancelled.
227 struct CONTENT_EXPORT DelegateReference :
228 public base::RefCounted<DelegateReference> {
229 Delegate* delegate;
230 AppCacheStorage* storage;
232 DelegateReference(Delegate* delegate, AppCacheStorage* storage);
234 void CancelReference() {
235 storage->delegate_references_.erase(delegate);
236 storage = NULL;
237 delegate = NULL;
240 private:
241 friend class base::RefCounted<DelegateReference>;
243 virtual ~DelegateReference();
245 typedef std::map<Delegate*, DelegateReference*> DelegateReferenceMap;
246 typedef std::vector<scoped_refptr<DelegateReference> >
247 DelegateReferenceVector;
249 // Helper used to manage an async LoadResponseInfo calls on behalf of
250 // multiple callers.
251 class ResponseInfoLoadTask {
252 public:
253 ResponseInfoLoadTask(const GURL& manifest_url, int64 group_id,
254 int64 response_id, AppCacheStorage* storage);
255 ~ResponseInfoLoadTask();
257 int64 response_id() const { return response_id_; }
258 const GURL& manifest_url() const { return manifest_url_; }
259 int64 group_id() const { return group_id_; }
261 void AddDelegate(DelegateReference* delegate_reference) {
262 delegates_.push_back(delegate_reference);
265 void StartIfNeeded();
267 private:
268 void OnReadComplete(int result);
270 AppCacheStorage* storage_;
271 GURL manifest_url_;
272 int64 group_id_;
273 int64 response_id_;
274 scoped_ptr<AppCacheResponseReader> reader_;
275 DelegateReferenceVector delegates_;
276 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer_;
279 typedef std::map<int64, ResponseInfoLoadTask*> PendingResponseInfoLoads;
281 DelegateReference* GetDelegateReference(Delegate* delegate) {
282 DelegateReferenceMap::iterator iter =
283 delegate_references_.find(delegate);
284 if (iter != delegate_references_.end())
285 return iter->second;
286 return NULL;
289 DelegateReference* GetOrCreateDelegateReference(Delegate* delegate) {
290 DelegateReference* reference = GetDelegateReference(delegate);
291 if (reference)
292 return reference;
293 return new DelegateReference(delegate, this);
296 ResponseInfoLoadTask* GetOrCreateResponseInfoLoadTask(
297 const GURL& manifest_url, int64 group_id, int64 response_id) {
298 PendingResponseInfoLoads::iterator iter =
299 pending_info_loads_.find(response_id);
300 if (iter != pending_info_loads_.end())
301 return iter->second;
302 return new ResponseInfoLoadTask(manifest_url, group_id, response_id, this);
305 // Should only be called when creating a new response writer.
306 int64 NewResponseId() {
307 return ++last_response_id_;
310 // Helpers to query and notify the QuotaManager.
311 void UpdateUsageMapAndNotify(const GURL& origin, int64 new_usage);
312 void ClearUsageMapAndNotify();
313 void NotifyStorageAccessed(const GURL& origin);
315 // The last storage id used for different object types.
316 int64 last_cache_id_;
317 int64 last_group_id_;
318 int64 last_response_id_;
320 UsageMap usage_map_; // maps origin to usage
321 AppCacheWorkingSet working_set_;
322 AppCacheServiceImpl* service_;
323 DelegateReferenceMap delegate_references_;
324 PendingResponseInfoLoads pending_info_loads_;
326 // The set of last ids must be retrieved from storage prior to being used.
327 static const int64 kUnitializedId;
329 FRIEND_TEST_ALL_PREFIXES(content::AppCacheStorageTest, DelegateReferences);
330 FRIEND_TEST_ALL_PREFIXES(content::AppCacheStorageTest, UsageMap);
332 DISALLOW_COPY_AND_ASSIGN(AppCacheStorage);
335 } // namespace content
337 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_STORAGE_H_