| 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_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ |
| 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ |
| 7 | 7 |
| 8 #include <list> |
| 9 |
| 8 #include "base/callback.h" | 10 #include "base/callback.h" |
| 9 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 10 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 12 #include "content/common/service_worker/service_worker_types.h" | 14 #include "content/common/service_worker/service_worker_types.h" |
| 13 #include "net/base/completion_callback.h" | 15 #include "net/base/completion_callback.h" |
| 14 #include "net/disk_cache/disk_cache.h" | 16 #include "net/disk_cache/disk_cache.h" |
| 15 | 17 |
| 16 namespace net { | 18 namespace net { |
| 17 class URLRequestContext; | 19 class URLRequestContext; |
| 18 class IOBufferWithSize; | 20 class IOBufferWithSize; |
| 19 } | 21 } |
| 20 | 22 |
| 21 namespace storage { | 23 namespace storage { |
| 22 class BlobData; | 24 class BlobData; |
| 23 class BlobDataHandle; | 25 class BlobDataHandle; |
| 24 class BlobStorageContext; | 26 class BlobStorageContext; |
| 25 class QuotaManagerProxy; | 27 class QuotaManagerProxy; |
| 26 } | 28 } |
| 27 | 29 |
| 28 namespace content { | 30 namespace content { |
| 29 class ChromeBlobStorageContext; | 31 class ChromeBlobStorageContext; |
| 30 class ServiceWorkerCacheMetadata; | 32 class ServiceWorkerCacheMetadata; |
| 31 class TestServiceWorkerCache; | 33 class TestServiceWorkerCache; |
| 32 | 34 |
| 33 // Represents a ServiceWorker Cache as seen in | 35 // Represents a ServiceWorker Cache as seen in |
| 34 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html. | 36 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html. |
| 35 // Callbacks to the public functions will be called so long as the cache object | 37 // The asynchronous methods are executed serially. Callbacks to the |
| 36 // lives. | 38 // public functions will be called so long as the cache object lives. |
| 37 class CONTENT_EXPORT ServiceWorkerCache | 39 class CONTENT_EXPORT ServiceWorkerCache |
| 38 : public base::RefCounted<ServiceWorkerCache> { | 40 : public base::RefCounted<ServiceWorkerCache> { |
| 39 public: | 41 public: |
| 40 enum ErrorType { | 42 enum ErrorType { |
| 41 ErrorTypeOK = 0, | 43 ErrorTypeOK = 0, |
| 42 ErrorTypeExists, | 44 ErrorTypeExists, |
| 43 ErrorTypeStorage, | 45 ErrorTypeStorage, |
| 44 ErrorTypeNotFound | 46 ErrorTypeNotFound |
| 45 }; | 47 }; |
| 46 | 48 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 const GURL& origin, | 122 const GURL& origin, |
| 121 const base::FilePath& path, | 123 const base::FilePath& path, |
| 122 net::URLRequestContext* request_context, | 124 net::URLRequestContext* request_context, |
| 123 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 125 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
| 124 base::WeakPtr<storage::BlobStorageContext> blob_context); | 126 base::WeakPtr<storage::BlobStorageContext> blob_context); |
| 125 | 127 |
| 126 // Async operations in progress will cancel and not run their callbacks. | 128 // Async operations in progress will cancel and not run their callbacks. |
| 127 virtual ~ServiceWorkerCache(); | 129 virtual ~ServiceWorkerCache(); |
| 128 | 130 |
| 129 // Match callbacks | 131 // Match callbacks |
| 132 void MatchImpl(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 133 const ResponseCallback& callback); |
| 130 void MatchDidOpenEntry(scoped_ptr<MatchContext> match_context, int rv); | 134 void MatchDidOpenEntry(scoped_ptr<MatchContext> match_context, int rv); |
| 131 void MatchDidReadMetadata(scoped_ptr<MatchContext> match_context, | 135 void MatchDidReadMetadata(scoped_ptr<MatchContext> match_context, |
| 132 scoped_ptr<ServiceWorkerCacheMetadata> headers); | 136 scoped_ptr<ServiceWorkerCacheMetadata> headers); |
| 133 void MatchDidReadResponseBodyData(scoped_ptr<MatchContext> match_context, | 137 void MatchDidReadResponseBodyData(scoped_ptr<MatchContext> match_context, |
| 134 int rv); | 138 int rv); |
| 135 void MatchDoneWithBody(scoped_ptr<MatchContext> match_context); | 139 void MatchDoneWithBody(scoped_ptr<MatchContext> match_context); |
| 136 | 140 |
| 137 // Put callbacks. | 141 // Put callbacks. |
| 138 void PutImpl(scoped_ptr<PutContext> put_context); | 142 void PutImpl(scoped_ptr<PutContext> put_context); |
| 139 void PutDidDelete(scoped_ptr<PutContext> put_context, ErrorType delete_error); | 143 void PutDidDelete(scoped_ptr<PutContext> put_context, ErrorType delete_error); |
| 140 void PutDidCreateEntry(scoped_ptr<PutContext> put_context, int rv); | 144 void PutDidCreateEntry(scoped_ptr<PutContext> put_context, int rv); |
| 141 void PutDidWriteHeaders(scoped_ptr<PutContext> put_context, | 145 void PutDidWriteHeaders(scoped_ptr<PutContext> put_context, |
| 142 int expected_bytes, | 146 int expected_bytes, |
| 143 int rv); | 147 int rv); |
| 144 void PutDidWriteBlobToCache(scoped_ptr<PutContext> put_context, | 148 void PutDidWriteBlobToCache(scoped_ptr<PutContext> put_context, |
| 145 scoped_ptr<BlobReader> blob_reader, | 149 scoped_ptr<BlobReader> blob_reader, |
| 146 disk_cache::ScopedEntryPtr entry, | 150 disk_cache::ScopedEntryPtr entry, |
| 147 bool success); | 151 bool success); |
| 148 | 152 |
| 149 // Delete callbacks | 153 // Delete callbacks |
| 154 void DeleteImpl(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 155 const ErrorCallback& callback); |
| 150 void DeleteDidOpenEntry( | 156 void DeleteDidOpenEntry( |
| 151 const GURL& origin, | 157 const GURL& origin, |
| 152 scoped_ptr<ServiceWorkerFetchRequest> request, | 158 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 153 const ServiceWorkerCache::ErrorCallback& callback, | 159 const ServiceWorkerCache::ErrorCallback& callback, |
| 154 scoped_ptr<disk_cache::Entry*> entryptr, | 160 scoped_ptr<disk_cache::Entry*> entryptr, |
| 155 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 161 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
| 156 int rv); | 162 int rv); |
| 157 | 163 |
| 158 // Keys callbacks. | 164 // Keys callbacks. |
| 165 void KeysImpl(const RequestsCallback& callback); |
| 159 void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context, int rv); | 166 void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context, int rv); |
| 160 void KeysProcessNextEntry(scoped_ptr<KeysContext> keys_context, | 167 void KeysProcessNextEntry(scoped_ptr<KeysContext> keys_context, |
| 161 const Entries::iterator& iter); | 168 const Entries::iterator& iter); |
| 162 void KeysDidReadMetadata(scoped_ptr<KeysContext> keys_context, | 169 void KeysDidReadMetadata(scoped_ptr<KeysContext> keys_context, |
| 163 const Entries::iterator& iter, | 170 const Entries::iterator& iter, |
| 164 scoped_ptr<ServiceWorkerCacheMetadata> metadata); | 171 scoped_ptr<ServiceWorkerCacheMetadata> metadata); |
| 165 | 172 |
| 166 void CloseImpl(const base::Closure& callback); | 173 void CloseImpl(const base::Closure& callback); |
| 167 | 174 |
| 168 // Loads the backend and calls the callback with the result (true for | 175 // Loads the backend and calls the callback with the result (true for |
| 169 // success). The callback will always be called. Virtual for tests. | 176 // success). The callback will always be called. Virtual for tests. |
| 170 virtual void CreateBackend(const ErrorCallback& callback); | 177 virtual void CreateBackend(const ErrorCallback& callback); |
| 171 void CreateBackendDidCreate(const ServiceWorkerCache::ErrorCallback& callback, | 178 void CreateBackendDidCreate(const ServiceWorkerCache::ErrorCallback& callback, |
| 172 scoped_ptr<ScopedBackendPtr> backend_ptr, | 179 scoped_ptr<ScopedBackendPtr> backend_ptr, |
| 173 int rv); | 180 int rv); |
| 174 | 181 |
| 175 void InitBackend(const base::Closure& callback); | 182 void InitBackend(const base::Closure& callback); |
| 176 void InitDone(ErrorType error); | 183 void InitDone(const base::Closure& callback, ErrorType error); |
| 177 | 184 |
| 178 void IncPendingOps() { pending_ops_++; } | 185 void CompleteOperationAndRunNext(); |
| 179 void DecPendingOps(); | 186 void RunOperationIfIdle(); |
| 187 void PendingClosure(const base::Closure& callback); |
| 180 void PendingErrorCallback(const ErrorCallback& callback, ErrorType error); | 188 void PendingErrorCallback(const ErrorCallback& callback, ErrorType error); |
| 181 void PendingResponseCallback( | 189 void PendingResponseCallback( |
| 182 const ResponseCallback& callback, | 190 const ResponseCallback& callback, |
| 183 ErrorType error, | 191 ErrorType error, |
| 184 scoped_ptr<ServiceWorkerResponse> response, | 192 scoped_ptr<ServiceWorkerResponse> response, |
| 185 scoped_ptr<storage::BlobDataHandle> blob_data_handle); | 193 scoped_ptr<storage::BlobDataHandle> blob_data_handle); |
| 186 void PendingRequestsCallback(const RequestsCallback& callback, | 194 void PendingRequestsCallback(const RequestsCallback& callback, |
| 187 ErrorType error, | 195 ErrorType error, |
| 188 scoped_ptr<Requests> requests); | 196 scoped_ptr<Requests> requests); |
| 189 | 197 |
| 190 // The backend can be deleted via the Close function at any time so always | 198 // The backend can be deleted via the Close function at any time so always |
| 191 // check for its existence before use. | 199 // check for its existence before use. |
| 192 scoped_ptr<disk_cache::Backend> backend_; | 200 scoped_ptr<disk_cache::Backend> backend_; |
| 193 GURL origin_; | 201 GURL origin_; |
| 194 base::FilePath path_; | 202 base::FilePath path_; |
| 195 net::URLRequestContext* request_context_; | 203 net::URLRequestContext* request_context_; |
| 196 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; | 204 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; |
| 197 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; | 205 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; |
| 198 BackendState backend_state_; | 206 BackendState backend_state_; |
| 199 std::vector<base::Closure> init_callbacks_; | 207 std::list<base::Closure> pending_operations_; |
| 208 bool initializing_; |
| 200 | 209 |
| 201 // Whether or not to store data in disk or memory. | 210 // Whether or not to store data in disk or memory. |
| 202 bool memory_only_; | 211 bool memory_only_; |
| 203 | 212 |
| 204 // The number of started operations that have yet to complete. | |
| 205 // TODO(jkarlin): pending_ops_ gets double counted on lazy initialization (say | |
| 206 // in ::Put). The counting still works but pending_ops_ doesn't accurately | |
| 207 // represent the number of operations in flight. Fix this by having the lazy | |
| 208 // init callback call a different function than the original caller (::Put). | |
| 209 size_t pending_ops_; | |
| 210 base::Closure ops_complete_callback_; | |
| 211 | |
| 212 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; | 213 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; |
| 213 | 214 |
| 214 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); | 215 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); |
| 215 }; | 216 }; |
| 216 | 217 |
| 217 } // namespace content | 218 } // namespace content |
| 218 | 219 |
| 219 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | 220 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ |
| OLD | NEW |