| 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 "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 11 #include "content/common/service_worker/service_worker_types.h" | 12 #include "content/common/service_worker/service_worker_types.h" |
| 12 #include "net/base/completion_callback.h" | 13 #include "net/base/completion_callback.h" |
| 13 #include "net/disk_cache/disk_cache.h" | 14 #include "net/disk_cache/disk_cache.h" |
| 14 | 15 |
| 15 namespace net { | 16 namespace net { |
| 16 class URLRequestContext; | 17 class URLRequestContext; |
| 17 class IOBufferWithSize; | 18 class IOBufferWithSize; |
| 18 } | 19 } |
| 19 | 20 |
| 20 namespace storage { | 21 namespace storage { |
| 21 class BlobData; | 22 class BlobData; |
| 22 class BlobDataHandle; | 23 class BlobDataHandle; |
| 23 class BlobStorageContext; | 24 class BlobStorageContext; |
| 24 } | 25 } |
| 25 | 26 |
| 26 namespace content { | 27 namespace content { |
| 27 class ChromeBlobStorageContext; | 28 class ChromeBlobStorageContext; |
| 28 class ServiceWorkerRequestResponseHeaders; | 29 class ServiceWorkerRequestResponseHeaders; |
| 29 | 30 |
| 30 // TODO(jkarlin): Unload cache backend from memory once the cache object is no | 31 // TODO(jkarlin): Unload cache backend from memory once the cache object is no |
| 31 // longer referenced in javascript. | 32 // longer referenced in javascript. |
| 32 | 33 |
| 33 // Represents a ServiceWorker Cache as seen in | 34 // Represents a ServiceWorker Cache as seen in |
| 34 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html. | 35 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html. |
| 35 // InitializeIfNeeded must be called before calling the other public members. | 36 // InitializeIfNeeded must be called before calling the other public members. |
| 36 class CONTENT_EXPORT ServiceWorkerCache { | 37 class CONTENT_EXPORT ServiceWorkerCache |
| 38 : public base::RefCounted<ServiceWorkerCache> { |
| 37 public: | 39 public: |
| 38 enum ErrorType { | 40 enum ErrorType { |
| 39 ErrorTypeOK = 0, | 41 ErrorTypeOK = 0, |
| 40 ErrorTypeExists, | 42 ErrorTypeExists, |
| 41 ErrorTypeStorage, | 43 ErrorTypeStorage, |
| 42 ErrorTypeNotFound | 44 ErrorTypeNotFound |
| 43 }; | 45 }; |
| 44 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; | 46 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; |
| 45 typedef base::Callback<void(ErrorType)> ErrorCallback; | 47 typedef base::Callback<void(ErrorType)> ErrorCallback; |
| 46 typedef base::Callback<void(ErrorType, | 48 typedef base::Callback<void(ErrorType, |
| 47 scoped_ptr<ServiceWorkerResponse>, | 49 scoped_ptr<ServiceWorkerResponse>, |
| 48 scoped_ptr<storage::BlobDataHandle>)> | 50 scoped_ptr<storage::BlobDataHandle>)> |
| 49 ResponseCallback; | 51 ResponseCallback; |
| 50 typedef std::vector<ServiceWorkerFetchRequest> Requests; | 52 typedef std::vector<ServiceWorkerFetchRequest> Requests; |
| 51 typedef base::Callback<void(ErrorType, scoped_ptr<Requests>)> | 53 typedef base::Callback<void(ErrorType, scoped_ptr<Requests>)> |
| 52 RequestsCallback; | 54 RequestsCallback; |
| 53 | 55 |
| 54 static scoped_ptr<ServiceWorkerCache> CreateMemoryCache( | 56 static scoped_refptr<ServiceWorkerCache> CreateMemoryCache( |
| 55 net::URLRequestContext* request_context, | 57 net::URLRequestContext* request_context, |
| 56 base::WeakPtr<storage::BlobStorageContext> blob_context); | 58 base::WeakPtr<storage::BlobStorageContext> blob_context); |
| 57 static scoped_ptr<ServiceWorkerCache> CreatePersistentCache( | 59 static scoped_refptr<ServiceWorkerCache> CreatePersistentCache( |
| 58 const base::FilePath& path, | 60 const base::FilePath& path, |
| 59 net::URLRequestContext* request_context, | 61 net::URLRequestContext* request_context, |
| 60 base::WeakPtr<storage::BlobStorageContext> blob_context); | 62 base::WeakPtr<storage::BlobStorageContext> blob_context); |
| 61 | 63 |
| 62 // Operations in progress will complete after the cache is deleted but pending | |
| 63 // operations (those operations waiting for init to finish) won't. | |
| 64 virtual ~ServiceWorkerCache(); | |
| 65 | |
| 66 // Returns ErrorTypeNotFound if not found. The callback will always be called. | 64 // Returns ErrorTypeNotFound if not found. The callback will always be called. |
| 67 // |request| must remain valid until the callback is called. | |
| 68 void Match(scoped_ptr<ServiceWorkerFetchRequest> request, | 65 void Match(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 69 const ResponseCallback& callback); | 66 const ResponseCallback& callback); |
| 70 | 67 |
| 71 // Puts the request and response object in the cache. The response body (if | 68 // Puts the request and response object in the cache. The response body (if |
| 72 // present) is stored in the cache, but not the request body. Returns | 69 // present) is stored in the cache, but not the request body. Returns |
| 73 // ErrorTypeOK on success. The callback will always be called. |request| and | 70 // ErrorTypeOK on success. The callback will always be called. |
| 74 // |response| must remain valid until the callback is called. | |
| 75 void Put(scoped_ptr<ServiceWorkerFetchRequest> request, | 71 void Put(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 76 scoped_ptr<ServiceWorkerResponse> response, | 72 scoped_ptr<ServiceWorkerResponse> response, |
| 77 const ErrorCallback& callback); | 73 const ErrorCallback& callback); |
| 78 | 74 |
| 79 // Returns ErrorNotFound if not found. Otherwise deletes and returns | 75 // Returns ErrorNotFound if not found. Otherwise deletes and returns |
| 80 // ErrorTypeOK. The callback will always be called. |request| must remain | 76 // ErrorTypeOK. The callback will always be called. |
| 81 // valid until the callback is called. | |
| 82 void Delete(scoped_ptr<ServiceWorkerFetchRequest> request, | 77 void Delete(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 83 const ErrorCallback& callback); | 78 const ErrorCallback& callback); |
| 84 | 79 |
| 85 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest. | 80 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest. |
| 86 // Returns ErrorTypeOK and a vector of requests if there are no errors. The | 81 // Returns ErrorTypeOK and a vector of requests if there are no errors. The |
| 87 // callback will always be called. | 82 // callback will always be called. |
| 88 void Keys(const RequestsCallback& callback); | 83 void Keys(const RequestsCallback& callback); |
| 89 | 84 |
| 85 // Prevent further operations on this object and delete the backend. |
| 86 void Close(); |
| 87 |
| 90 void set_backend(scoped_ptr<disk_cache::Backend> backend) { | 88 void set_backend(scoped_ptr<disk_cache::Backend> backend) { |
| 91 backend_ = backend.Pass(); | 89 backend_ = backend.Pass(); |
| 92 } | 90 } |
| 93 | 91 |
| 94 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); | 92 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); |
| 95 | 93 |
| 96 private: | 94 private: |
| 95 friend class base::RefCounted<ServiceWorkerCache>; |
| 96 |
| 97 struct KeysContext; | 97 struct KeysContext; |
| 98 typedef std::vector<disk_cache::Entry*> Entries; | 98 typedef std::vector<disk_cache::Entry*> Entries; |
| 99 | 99 |
| 100 ServiceWorkerCache(const base::FilePath& path, | 100 ServiceWorkerCache(const base::FilePath& path, |
| 101 net::URLRequestContext* request_context, | 101 net::URLRequestContext* request_context, |
| 102 base::WeakPtr<storage::BlobStorageContext> blob_context); | 102 base::WeakPtr<storage::BlobStorageContext> blob_context); |
| 103 | 103 |
| 104 // Operations in progress will complete after the cache is deleted but pending |
| 105 // operations (those operations waiting for init to finish) won't. |
| 106 virtual ~ServiceWorkerCache(); |
| 107 |
| 104 void PutImpl(scoped_ptr<ServiceWorkerFetchRequest> request, | 108 void PutImpl(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 105 scoped_ptr<ServiceWorkerResponse> response, | 109 scoped_ptr<ServiceWorkerResponse> response, |
| 106 scoped_ptr<storage::BlobDataHandle> blob_data_handle, | 110 scoped_ptr<storage::BlobDataHandle> blob_data_handle, |
| 107 const ErrorCallback& callback); | 111 const ErrorCallback& callback); |
| 108 | 112 |
| 109 // Static callbacks for the Keys function. | 113 // Static callbacks for the Keys function. |
| 110 static void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context, | 114 static void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context, |
| 111 int rv); | 115 int rv); |
| 112 static void KeysProcessNextEntry(scoped_ptr<KeysContext> keys_context, | 116 static void KeysProcessNextEntry(scoped_ptr<KeysContext> keys_context, |
| 113 const Entries::iterator& iter); | 117 const Entries::iterator& iter); |
| 114 static void KeysDidReadHeaders( | 118 static void KeysDidReadHeaders( |
| 115 scoped_ptr<KeysContext> keys_context, | 119 scoped_ptr<KeysContext> keys_context, |
| 116 const Entries::iterator& iter, | 120 const Entries::iterator& iter, |
| 117 scoped_ptr<ServiceWorkerRequestResponseHeaders> headers); | 121 scoped_ptr<ServiceWorkerRequestResponseHeaders> headers); |
| 118 | 122 |
| 119 // Loads the backend and calls the callback with the result (true for | 123 // Loads the backend and calls the callback with the result (true for |
| 120 // success). The callback will always be called. | 124 // success). The callback will always be called. |
| 121 void CreateBackend(const ErrorCallback& callback); | 125 void CreateBackend(const ErrorCallback& callback); |
| 122 | 126 |
| 123 void Init(const base::Closure& callback); | 127 void Init(const base::Closure& callback); |
| 124 void InitDone(ErrorType error); | 128 void InitDone(ErrorType error); |
| 125 | 129 |
| 130 // The backend can be deleted via the Close function at any time so always |
| 131 // check for its existence before use. |
| 126 scoped_ptr<disk_cache::Backend> backend_; | 132 scoped_ptr<disk_cache::Backend> backend_; |
| 127 base::FilePath path_; | 133 base::FilePath path_; |
| 128 net::URLRequestContext* request_context_; | 134 net::URLRequestContext* request_context_; |
| 129 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; | 135 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; |
| 130 bool initialized_; | 136 bool initialized_; |
| 131 std::vector<base::Closure> init_callbacks_; | 137 std::vector<base::Closure> init_callbacks_; |
| 132 | 138 |
| 133 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; | 139 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; |
| 134 | 140 |
| 135 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); | 141 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); |
| 136 }; | 142 }; |
| 137 | 143 |
| 138 } // namespace content | 144 } // namespace content |
| 139 | 145 |
| 140 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | 146 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ |
| OLD | NEW |