OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ | 5 #ifndef CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ |
6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ | 6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 class CacheStorageScheduler; | 32 class CacheStorageScheduler; |
33 class TestCacheStorageCache; | 33 class TestCacheStorageCache; |
34 | 34 |
35 // Represents a ServiceWorker Cache as seen in | 35 // Represents a ServiceWorker Cache as seen in |
36 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/ | 36 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/ |
37 // The asynchronous methods are executed serially. Callbacks to the | 37 // The asynchronous methods are executed serially. Callbacks to the |
38 // public functions will be called so long as the cache object lives. | 38 // public functions will be called so long as the cache object lives. |
39 class CONTENT_EXPORT CacheStorageCache | 39 class CONTENT_EXPORT CacheStorageCache |
40 : public base::RefCounted<CacheStorageCache> { | 40 : public base::RefCounted<CacheStorageCache> { |
41 public: | 41 public: |
42 // This enum is used in histograms, so do not change the ordering and always | 42 using ErrorCallback = base::Callback<void(CacheStorageError)>; |
43 // append new types to the end. | 43 using ResponseCallback = |
44 enum ErrorType { | 44 base::Callback<void(CacheStorageError, |
45 ERROR_TYPE_OK = 0, | 45 scoped_ptr<ServiceWorkerResponse>, |
46 ERROR_TYPE_EXISTS, | 46 scoped_ptr<storage::BlobDataHandle>)>; |
47 ERROR_TYPE_STORAGE, | 47 using Requests = std::vector<ServiceWorkerFetchRequest>; |
48 ERROR_TYPE_NOT_FOUND, | 48 using RequestsCallback = |
49 ERROR_TYPE_LAST = ERROR_TYPE_NOT_FOUND | 49 base::Callback<void(CacheStorageError, scoped_ptr<Requests>)>; |
50 }; | |
51 | |
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; | |
60 | 50 |
61 static scoped_refptr<CacheStorageCache> CreateMemoryCache( | 51 static scoped_refptr<CacheStorageCache> CreateMemoryCache( |
62 const GURL& origin, | 52 const GURL& origin, |
63 net::URLRequestContext* request_context, | 53 net::URLRequestContext* request_context, |
64 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 54 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
65 base::WeakPtr<storage::BlobStorageContext> blob_context); | 55 base::WeakPtr<storage::BlobStorageContext> blob_context); |
66 static scoped_refptr<CacheStorageCache> CreatePersistentCache( | 56 static scoped_refptr<CacheStorageCache> CreatePersistentCache( |
67 const GURL& origin, | 57 const GURL& origin, |
68 const base::FilePath& path, | 58 const base::FilePath& path, |
69 net::URLRequestContext* request_context, | 59 net::URLRequestContext* request_context, |
(...skipping 10 matching lines...) Expand all Loading... |
80 void Put(scoped_ptr<ServiceWorkerFetchRequest> request, | 70 void Put(scoped_ptr<ServiceWorkerFetchRequest> request, |
81 scoped_ptr<ServiceWorkerResponse> response, | 71 scoped_ptr<ServiceWorkerResponse> response, |
82 const ErrorCallback& callback); | 72 const ErrorCallback& callback); |
83 | 73 |
84 // Returns ErrorNotFound if not found. Otherwise deletes and returns | 74 // Returns ErrorNotFound if not found. Otherwise deletes and returns |
85 // ERROR_TYPE_OK. | 75 // ERROR_TYPE_OK. |
86 void Delete(scoped_ptr<ServiceWorkerFetchRequest> request, | 76 void Delete(scoped_ptr<ServiceWorkerFetchRequest> request, |
87 const ErrorCallback& callback); | 77 const ErrorCallback& callback); |
88 | 78 |
89 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest. | 79 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest. |
90 // Returns ErrorTypeOK and a vector of requests if there are no errors. | 80 // Returns CACHE_STORAGE_OK and a vector of requests if there are no errors. |
91 void Keys(const RequestsCallback& callback); | 81 void Keys(const RequestsCallback& callback); |
92 | 82 |
93 // Closes the backend. Future operations that require the backend | 83 // Closes the backend. Future operations that require the backend |
94 // will exit early. Close should only be called once per CacheStorageCache. | 84 // will exit early. Close should only be called once per CacheStorageCache. |
95 void Close(const base::Closure& callback); | 85 void Close(const base::Closure& callback); |
96 | 86 |
97 // The size of the cache contents in memory. Returns 0 if the cache backend is | 87 // The size of the cache contents in memory. Returns 0 if the cache backend is |
98 // not a memory cache backend. | 88 // not a memory cache backend. |
99 int64 MemoryBackedSize() const; | 89 int64 MemoryBackedSize() const; |
100 | 90 |
101 base::WeakPtr<CacheStorageCache> AsWeakPtr(); | 91 base::WeakPtr<CacheStorageCache> AsWeakPtr(); |
102 | 92 |
103 private: | 93 private: |
104 friend class base::RefCounted<CacheStorageCache>; | 94 friend class base::RefCounted<CacheStorageCache>; |
105 friend class TestCacheStorageCache; | 95 friend class TestCacheStorageCache; |
106 | 96 |
107 class BlobReader; | 97 class BlobReader; |
108 struct KeysContext; | 98 struct KeysContext; |
109 struct MatchContext; | 99 struct MatchContext; |
110 struct PutContext; | 100 struct PutContext; |
111 | 101 |
112 // The backend progresses from uninitialized, to open, to closed, and cannot | 102 // The backend progresses from uninitialized, to open, to closed, and cannot |
113 // reverse direction. The open step may be skipped. | 103 // reverse direction. The open step may be skipped. |
114 enum BackendState { | 104 enum BackendState { |
115 BACKEND_UNINITIALIZED, // No backend, create backend on first operation. | 105 BACKEND_UNINITIALIZED, // No backend, create backend on first operation. |
116 BACKEND_OPEN, // Backend can be used. | 106 BACKEND_OPEN, // Backend can be used. |
117 BACKEND_CLOSED // Backend cannot be used. All ops should fail. | 107 BACKEND_CLOSED // Backend cannot be used. All ops should fail. |
118 }; | 108 }; |
119 | 109 |
120 typedef std::vector<disk_cache::Entry*> Entries; | 110 using Entries = std::vector<disk_cache::Entry*>; |
121 typedef scoped_ptr<disk_cache::Backend> ScopedBackendPtr; | 111 using ScopedBackendPtr = scoped_ptr<disk_cache::Backend>; |
122 | 112 |
123 CacheStorageCache( | 113 CacheStorageCache( |
124 const GURL& origin, | 114 const GURL& origin, |
125 const base::FilePath& path, | 115 const base::FilePath& path, |
126 net::URLRequestContext* request_context, | 116 net::URLRequestContext* request_context, |
127 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 117 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
128 base::WeakPtr<storage::BlobStorageContext> blob_context); | 118 base::WeakPtr<storage::BlobStorageContext> blob_context); |
129 | 119 |
130 // Async operations in progress will cancel and not run their callbacks. | 120 // Async operations in progress will cancel and not run their callbacks. |
131 virtual ~CacheStorageCache(); | 121 virtual ~CacheStorageCache(); |
132 | 122 |
133 // Match callbacks | 123 // Match callbacks |
134 void MatchImpl(scoped_ptr<ServiceWorkerFetchRequest> request, | 124 void MatchImpl(scoped_ptr<ServiceWorkerFetchRequest> request, |
135 const ResponseCallback& callback); | 125 const ResponseCallback& callback); |
136 void MatchDidOpenEntry(scoped_ptr<MatchContext> match_context, int rv); | 126 void MatchDidOpenEntry(scoped_ptr<MatchContext> match_context, int rv); |
137 void MatchDidReadMetadata(scoped_ptr<MatchContext> match_context, | 127 void MatchDidReadMetadata(scoped_ptr<MatchContext> match_context, |
138 scoped_ptr<CacheMetadata> headers); | 128 scoped_ptr<CacheMetadata> headers); |
139 void MatchDidReadResponseBodyData(scoped_ptr<MatchContext> match_context, | 129 void MatchDidReadResponseBodyData(scoped_ptr<MatchContext> match_context, |
140 int rv); | 130 int rv); |
141 void MatchDoneWithBody(scoped_ptr<MatchContext> match_context); | 131 void MatchDoneWithBody(scoped_ptr<MatchContext> match_context); |
142 | 132 |
143 // Put callbacks. | 133 // Put callbacks. |
144 void PutImpl(scoped_ptr<PutContext> put_context); | 134 void PutImpl(scoped_ptr<PutContext> put_context); |
145 void PutDidDelete(scoped_ptr<PutContext> put_context, ErrorType delete_error); | 135 void PutDidDelete(scoped_ptr<PutContext> put_context, |
| 136 CacheStorageError delete_error); |
146 void PutDidCreateEntry(scoped_ptr<PutContext> put_context, int rv); | 137 void PutDidCreateEntry(scoped_ptr<PutContext> put_context, int rv); |
147 void PutDidWriteHeaders(scoped_ptr<PutContext> put_context, | 138 void PutDidWriteHeaders(scoped_ptr<PutContext> put_context, |
148 int expected_bytes, | 139 int expected_bytes, |
149 int rv); | 140 int rv); |
150 void PutDidWriteBlobToCache(scoped_ptr<PutContext> put_context, | 141 void PutDidWriteBlobToCache(scoped_ptr<PutContext> put_context, |
151 scoped_ptr<BlobReader> blob_reader, | 142 scoped_ptr<BlobReader> blob_reader, |
152 disk_cache::ScopedEntryPtr entry, | 143 disk_cache::ScopedEntryPtr entry, |
153 bool success); | 144 bool success); |
154 | 145 |
155 // Delete callbacks | 146 // Delete callbacks |
(...skipping 19 matching lines...) Expand all Loading... |
175 void CloseImpl(const base::Closure& callback); | 166 void CloseImpl(const base::Closure& callback); |
176 | 167 |
177 // Loads the backend and calls the callback with the result (true for | 168 // Loads the backend and calls the callback with the result (true for |
178 // success). The callback will always be called. Virtual for tests. | 169 // success). The callback will always be called. Virtual for tests. |
179 virtual void CreateBackend(const ErrorCallback& callback); | 170 virtual void CreateBackend(const ErrorCallback& callback); |
180 void CreateBackendDidCreate(const CacheStorageCache::ErrorCallback& callback, | 171 void CreateBackendDidCreate(const CacheStorageCache::ErrorCallback& callback, |
181 scoped_ptr<ScopedBackendPtr> backend_ptr, | 172 scoped_ptr<ScopedBackendPtr> backend_ptr, |
182 int rv); | 173 int rv); |
183 | 174 |
184 void InitBackend(); | 175 void InitBackend(); |
185 void InitDone(ErrorType error); | 176 void InitDone(CacheStorageError error); |
186 | 177 |
187 void PendingClosure(const base::Closure& callback); | 178 void PendingClosure(const base::Closure& callback); |
188 void PendingErrorCallback(const ErrorCallback& callback, ErrorType error); | 179 void PendingErrorCallback(const ErrorCallback& callback, |
| 180 CacheStorageError error); |
189 void PendingResponseCallback( | 181 void PendingResponseCallback( |
190 const ResponseCallback& callback, | 182 const ResponseCallback& callback, |
191 ErrorType error, | 183 CacheStorageError error, |
192 scoped_ptr<ServiceWorkerResponse> response, | 184 scoped_ptr<ServiceWorkerResponse> response, |
193 scoped_ptr<storage::BlobDataHandle> blob_data_handle); | 185 scoped_ptr<storage::BlobDataHandle> blob_data_handle); |
194 void PendingRequestsCallback(const RequestsCallback& callback, | 186 void PendingRequestsCallback(const RequestsCallback& callback, |
195 ErrorType error, | 187 CacheStorageError error, |
196 scoped_ptr<Requests> requests); | 188 scoped_ptr<Requests> requests); |
197 | 189 |
198 // Be sure to check |backend_state_| before use. | 190 // Be sure to check |backend_state_| before use. |
199 scoped_ptr<disk_cache::Backend> backend_; | 191 scoped_ptr<disk_cache::Backend> backend_; |
200 | 192 |
201 GURL origin_; | 193 GURL origin_; |
202 base::FilePath path_; | 194 base::FilePath path_; |
203 net::URLRequestContext* request_context_; | 195 net::URLRequestContext* request_context_; |
204 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; | 196 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; |
205 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; | 197 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; |
206 BackendState backend_state_; | 198 BackendState backend_state_; |
207 scoped_ptr<CacheStorageScheduler> scheduler_; | 199 scoped_ptr<CacheStorageScheduler> scheduler_; |
208 bool initializing_; | 200 bool initializing_; |
209 | 201 |
210 // Whether or not to store data in disk or memory. | 202 // Whether or not to store data in disk or memory. |
211 bool memory_only_; | 203 bool memory_only_; |
212 | 204 |
213 base::WeakPtrFactory<CacheStorageCache> weak_ptr_factory_; | 205 base::WeakPtrFactory<CacheStorageCache> weak_ptr_factory_; |
214 | 206 |
215 DISALLOW_COPY_AND_ASSIGN(CacheStorageCache); | 207 DISALLOW_COPY_AND_ASSIGN(CacheStorageCache); |
216 }; | 208 }; |
217 | 209 |
218 } // namespace content | 210 } // namespace content |
219 | 211 |
220 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ | 212 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ |
OLD | NEW |