Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / content / browser / cache_storage / cache_storage_cache.h
blobe63cac40f0baa148b77b389fee1e8280bbee5735
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_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_
6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_
8 #include <vector>
10 #include "base/callback.h"
11 #include "base/files/file_path.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "content/common/cache_storage/cache_storage_types.h"
15 #include "content/common/service_worker/service_worker_types.h"
16 #include "net/disk_cache/disk_cache.h"
18 namespace net {
19 class URLRequestContext;
20 class IOBufferWithSize;
23 namespace storage {
24 class BlobDataHandle;
25 class BlobStorageContext;
26 class QuotaManagerProxy;
29 namespace content {
31 class CacheMetadata;
32 class CacheStorageScheduler;
33 class TestCacheStorageCache;
35 // Represents a ServiceWorker Cache as seen in
36 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/
37 // The asynchronous methods are executed serially. Callbacks to the
38 // public functions will be called so long as the cache object lives.
39 class CONTENT_EXPORT CacheStorageCache
40 : public base::RefCounted<CacheStorageCache> {
41 public:
42 // This enum is used in histograms, so do not change the ordering and always
43 // append new types to the end.
44 enum ErrorType {
45 ERROR_TYPE_OK = 0,
46 ERROR_TYPE_EXISTS,
47 ERROR_TYPE_STORAGE,
48 ERROR_TYPE_NOT_FOUND,
49 ERROR_TYPE_LAST = ERROR_TYPE_NOT_FOUND
52 typedef base::Callback<void(ErrorType)> ErrorCallback;
53 typedef base::Callback<void(ErrorType,
54 scoped_ptr<ServiceWorkerResponse>,
55 scoped_ptr<storage::BlobDataHandle>)>
56 ResponseCallback;
57 typedef std::vector<ServiceWorkerFetchRequest> Requests;
58 typedef base::Callback<void(ErrorType, scoped_ptr<Requests>)>
59 RequestsCallback;
61 static scoped_refptr<CacheStorageCache> CreateMemoryCache(
62 const GURL& origin,
63 net::URLRequestContext* request_context,
64 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
65 base::WeakPtr<storage::BlobStorageContext> blob_context);
66 static scoped_refptr<CacheStorageCache> CreatePersistentCache(
67 const GURL& origin,
68 const base::FilePath& path,
69 net::URLRequestContext* request_context,
70 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
71 base::WeakPtr<storage::BlobStorageContext> blob_context);
73 // Returns ERROR_TYPE_NOT_FOUND if not found.
74 void Match(scoped_ptr<ServiceWorkerFetchRequest> request,
75 const ResponseCallback& callback);
77 // Puts the request and response object in the cache. The response body (if
78 // present) is stored in the cache, but not the request body. Returns
79 // ERROR_TYPE_OK on success.
80 void Put(scoped_ptr<ServiceWorkerFetchRequest> request,
81 scoped_ptr<ServiceWorkerResponse> response,
82 const ResponseCallback& callback);
84 // Returns ErrorNotFound if not found. Otherwise deletes and returns
85 // ERROR_TYPE_OK.
86 void Delete(scoped_ptr<ServiceWorkerFetchRequest> request,
87 const ErrorCallback& callback);
89 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest.
90 // Returns ErrorTypeOK and a vector of requests if there are no errors.
91 void Keys(const RequestsCallback& callback);
93 // Closes the backend. Future operations that require the backend
94 // will exit early. Close should only be called once per CacheStorageCache.
95 void Close(const base::Closure& callback);
97 // The size of the cache contents in memory. Returns 0 if the cache backend is
98 // not a memory cache backend.
99 int64 MemoryBackedSize() const;
101 base::WeakPtr<CacheStorageCache> AsWeakPtr();
103 private:
104 friend class base::RefCounted<CacheStorageCache>;
105 friend class TestCacheStorageCache;
107 class BlobReader;
108 struct KeysContext;
109 struct MatchContext;
110 struct PutContext;
112 // The backend progresses from uninitialized, to open, to closed, and cannot
113 // reverse direction. The open step may be skipped.
114 enum BackendState {
115 BACKEND_UNINITIALIZED, // No backend, create backend on first operation.
116 BACKEND_OPEN, // Backend can be used.
117 BACKEND_CLOSED // Backend cannot be used. All ops should fail.
120 typedef std::vector<disk_cache::Entry*> Entries;
121 typedef scoped_ptr<disk_cache::Backend> ScopedBackendPtr;
123 CacheStorageCache(
124 const GURL& origin,
125 const base::FilePath& path,
126 net::URLRequestContext* request_context,
127 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
128 base::WeakPtr<storage::BlobStorageContext> blob_context);
130 // Async operations in progress will cancel and not run their callbacks.
131 virtual ~CacheStorageCache();
133 // Match callbacks
134 void MatchImpl(scoped_ptr<ServiceWorkerFetchRequest> request,
135 const ResponseCallback& callback);
136 void MatchDidOpenEntry(scoped_ptr<MatchContext> match_context, int rv);
137 void MatchDidReadMetadata(scoped_ptr<MatchContext> match_context,
138 scoped_ptr<CacheMetadata> headers);
139 void MatchDidReadResponseBodyData(scoped_ptr<MatchContext> match_context,
140 int rv);
141 void MatchDoneWithBody(scoped_ptr<MatchContext> match_context);
143 // Put callbacks.
144 void PutImpl(scoped_ptr<PutContext> put_context);
145 void PutDidDelete(scoped_ptr<PutContext> put_context, ErrorType delete_error);
146 void PutDidCreateEntry(scoped_ptr<PutContext> put_context, int rv);
147 void PutDidWriteHeaders(scoped_ptr<PutContext> put_context,
148 int expected_bytes,
149 int rv);
150 void PutDidWriteBlobToCache(scoped_ptr<PutContext> put_context,
151 scoped_ptr<BlobReader> blob_reader,
152 disk_cache::ScopedEntryPtr entry,
153 bool success);
155 // Delete callbacks
156 void DeleteImpl(scoped_ptr<ServiceWorkerFetchRequest> request,
157 const ErrorCallback& callback);
158 void DeleteDidOpenEntry(
159 const GURL& origin,
160 scoped_ptr<ServiceWorkerFetchRequest> request,
161 const CacheStorageCache::ErrorCallback& callback,
162 scoped_ptr<disk_cache::Entry*> entryptr,
163 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
164 int rv);
166 // Keys callbacks.
167 void KeysImpl(const RequestsCallback& callback);
168 void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context, int rv);
169 void KeysProcessNextEntry(scoped_ptr<KeysContext> keys_context,
170 const Entries::iterator& iter);
171 void KeysDidReadMetadata(scoped_ptr<KeysContext> keys_context,
172 const Entries::iterator& iter,
173 scoped_ptr<CacheMetadata> metadata);
175 void CloseImpl(const base::Closure& callback);
177 // Loads the backend and calls the callback with the result (true for
178 // success). The callback will always be called. Virtual for tests.
179 virtual void CreateBackend(const ErrorCallback& callback);
180 void CreateBackendDidCreate(const CacheStorageCache::ErrorCallback& callback,
181 scoped_ptr<ScopedBackendPtr> backend_ptr,
182 int rv);
184 void InitBackend();
185 void InitDone(ErrorType error);
187 void PendingClosure(const base::Closure& callback);
188 void PendingErrorCallback(const ErrorCallback& callback, ErrorType error);
189 void PendingResponseCallback(
190 const ResponseCallback& callback,
191 ErrorType error,
192 scoped_ptr<ServiceWorkerResponse> response,
193 scoped_ptr<storage::BlobDataHandle> blob_data_handle);
194 void PendingRequestsCallback(const RequestsCallback& callback,
195 ErrorType error,
196 scoped_ptr<Requests> requests);
198 // Be sure to check |backend_state_| before use.
199 scoped_ptr<disk_cache::Backend> backend_;
201 GURL origin_;
202 base::FilePath path_;
203 net::URLRequestContext* request_context_;
204 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_;
205 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_;
206 BackendState backend_state_;
207 scoped_ptr<CacheStorageScheduler> scheduler_;
208 bool initializing_;
210 // Whether or not to store data in disk or memory.
211 bool memory_only_;
213 base::WeakPtrFactory<CacheStorageCache> weak_ptr_factory_;
215 DISALLOW_COPY_AND_ASSIGN(CacheStorageCache);
218 } // namespace content
220 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_