Chromium Code Reviews| 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. | 65 // |request| must remain valid until the callback is called. |
| 68 void Match(scoped_ptr<ServiceWorkerFetchRequest> request, | 66 void Match(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 69 const ResponseCallback& callback); | 67 const ResponseCallback& callback); |
| 70 | 68 |
| 71 // Puts the request and response object in the cache. The response body (if | 69 // 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 | 70 // present) is stored in the cache, but not the request body. Returns |
| 73 // ErrorTypeOK on success. The callback will always be called. |request| and | 71 // ErrorTypeOK on success. The callback will always be called. |request| and |
| 74 // |response| must remain valid until the callback is called. | 72 // |response| must remain valid until the callback is called. |
|
michaeln
2014/09/09 20:04:58
not due to changes in this cl, but the comment is
jkarlin
2014/09/10 15:06:13
Done.
| |
| 75 void Put(scoped_ptr<ServiceWorkerFetchRequest> request, | 73 void Put(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 76 scoped_ptr<ServiceWorkerResponse> response, | 74 scoped_ptr<ServiceWorkerResponse> response, |
| 77 const ErrorCallback& callback); | 75 const ErrorCallback& callback); |
| 78 | 76 |
| 79 // Returns ErrorNotFound if not found. Otherwise deletes and returns | 77 // Returns ErrorNotFound if not found. Otherwise deletes and returns |
| 80 // ErrorTypeOK. The callback will always be called. |request| must remain | 78 // ErrorTypeOK. The callback will always be called. |request| must remain |
| 81 // valid until the callback is called. | 79 // valid until the callback is called. |
| 82 void Delete(scoped_ptr<ServiceWorkerFetchRequest> request, | 80 void Delete(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 83 const ErrorCallback& callback); | 81 const ErrorCallback& callback); |
| 84 | 82 |
| 85 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest. | 83 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest. |
| 86 // Returns ErrorTypeOK and a vector of requests if there are no errors. The | 84 // Returns ErrorTypeOK and a vector of requests if there are no errors. The |
| 87 // callback will always be called. | 85 // callback will always be called. |
| 88 void Keys(const RequestsCallback& callback); | 86 void Keys(const RequestsCallback& callback); |
| 89 | 87 |
| 88 // Prevent further operations on this object and delete the backend. | |
| 89 void Close(); | |
| 90 | |
| 90 void set_backend(scoped_ptr<disk_cache::Backend> backend) { | 91 void set_backend(scoped_ptr<disk_cache::Backend> backend) { |
| 91 backend_ = backend.Pass(); | 92 backend_ = backend.Pass(); |
| 92 } | 93 } |
| 93 | 94 |
| 94 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); | 95 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); |
| 95 | 96 |
| 96 private: | 97 private: |
| 98 friend class base::RefCounted<ServiceWorkerCache>; | |
| 99 | |
| 97 struct KeysContext; | 100 struct KeysContext; |
| 98 typedef std::vector<disk_cache::Entry*> Entries; | 101 typedef std::vector<disk_cache::Entry*> Entries; |
| 99 | 102 |
| 100 ServiceWorkerCache(const base::FilePath& path, | 103 ServiceWorkerCache(const base::FilePath& path, |
| 101 net::URLRequestContext* request_context, | 104 net::URLRequestContext* request_context, |
| 102 base::WeakPtr<storage::BlobStorageContext> blob_context); | 105 base::WeakPtr<storage::BlobStorageContext> blob_context); |
| 103 | 106 |
| 107 // Operations in progress will complete after the cache is deleted but pending | |
| 108 // operations (those operations waiting for init to finish) won't. | |
| 109 virtual ~ServiceWorkerCache(); | |
| 110 | |
| 104 // Static callbacks for the Keys function. | 111 // Static callbacks for the Keys function. |
| 105 static void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context, | 112 static void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context, |
| 106 int rv); | 113 int rv); |
| 107 static void KeysProcessNextEntry(scoped_ptr<KeysContext> keys_context, | 114 static void KeysProcessNextEntry(scoped_ptr<KeysContext> keys_context, |
| 108 const Entries::iterator& iter); | 115 const Entries::iterator& iter); |
| 109 static void KeysDidReadHeaders( | 116 static void KeysDidReadHeaders( |
| 110 scoped_ptr<KeysContext> keys_context, | 117 scoped_ptr<KeysContext> keys_context, |
| 111 const Entries::iterator& iter, | 118 const Entries::iterator& iter, |
| 112 scoped_ptr<ServiceWorkerRequestResponseHeaders> headers); | 119 scoped_ptr<ServiceWorkerRequestResponseHeaders> headers); |
| 113 | 120 |
| 114 // Loads the backend and calls the callback with the result (true for | 121 // Loads the backend and calls the callback with the result (true for |
| 115 // success). The callback will always be called. | 122 // success). The callback will always be called. |
| 116 void CreateBackend(const ErrorCallback& callback); | 123 void CreateBackend(const ErrorCallback& callback); |
| 117 | 124 |
| 118 void Init(const base::Closure& callback); | 125 void Init(const base::Closure& callback); |
| 119 void InitDone(ErrorType error); | 126 void InitDone(ErrorType error); |
| 120 | 127 |
| 128 // The backend can be deleted via the Close function at any time so always | |
| 129 // check for its existence before use. | |
| 121 scoped_ptr<disk_cache::Backend> backend_; | 130 scoped_ptr<disk_cache::Backend> backend_; |
| 122 base::FilePath path_; | 131 base::FilePath path_; |
| 123 net::URLRequestContext* request_context_; | 132 net::URLRequestContext* request_context_; |
| 124 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; | 133 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; |
| 125 bool initialized_; | 134 bool initialized_; |
| 126 std::vector<base::Closure> init_callbacks_; | 135 std::vector<base::Closure> init_callbacks_; |
| 127 | 136 |
| 128 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; | 137 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; |
| 129 | 138 |
| 130 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); | 139 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); |
| 131 }; | 140 }; |
| 132 | 141 |
| 133 } // namespace content | 142 } // namespace content |
| 134 | 143 |
| 135 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | 144 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ |
| OLD | NEW |