| 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/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 RequestsCallback; | 52 RequestsCallback; |
| 53 | 53 |
| 54 static scoped_ptr<ServiceWorkerCache> CreateMemoryCache( | 54 static scoped_ptr<ServiceWorkerCache> CreateMemoryCache( |
| 55 net::URLRequestContext* request_context, | 55 net::URLRequestContext* request_context, |
| 56 base::WeakPtr<storage::BlobStorageContext> blob_context); | 56 base::WeakPtr<storage::BlobStorageContext> blob_context); |
| 57 static scoped_ptr<ServiceWorkerCache> CreatePersistentCache( | 57 static scoped_ptr<ServiceWorkerCache> CreatePersistentCache( |
| 58 const base::FilePath& path, | 58 const base::FilePath& path, |
| 59 net::URLRequestContext* request_context, | 59 net::URLRequestContext* request_context, |
| 60 base::WeakPtr<storage::BlobStorageContext> blob_context); | 60 base::WeakPtr<storage::BlobStorageContext> blob_context); |
| 61 | 61 |
| 62 // Operations in progress will complete after the cache is deleted but pending |
| 63 // operations (those operations waiting for init to finish) won't. |
| 62 virtual ~ServiceWorkerCache(); | 64 virtual ~ServiceWorkerCache(); |
| 63 | 65 |
| 64 // Loads the backend and calls the callback with the result (true for | |
| 65 // success). This must be called before member functions that require a | |
| 66 // backend are called. The callback will always be called. | |
| 67 void CreateBackend(const ErrorCallback& callback); | |
| 68 | |
| 69 // Returns ErrorTypeNotFound if not found. The callback will always be called. | 66 // Returns ErrorTypeNotFound if not found. The callback will always be called. |
| 70 // |request| must remain valid until the callback is called. | 67 // |request| must remain valid until the callback is called. |
| 71 void Match(scoped_ptr<ServiceWorkerFetchRequest> request, | 68 void Match(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 72 const ResponseCallback& callback); | 69 const ResponseCallback& callback); |
| 73 | 70 |
| 74 // Puts the request and response object in the cache. The response body (if | 71 // Puts the request and response object in the cache. The response body (if |
| 75 // present) is stored in the cache, but not the request body. Returns | 72 // present) is stored in the cache, but not the request body. Returns |
| 76 // ErrorTypeOK on success. The callback will always be called. |request| and | 73 // ErrorTypeOK on success. The callback will always be called. |request| and |
| 77 // |response| must remain valid until the callback is called. | 74 // |response| must remain valid until the callback is called. |
| 78 void Put(scoped_ptr<ServiceWorkerFetchRequest> request, | 75 void Put(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 79 scoped_ptr<ServiceWorkerResponse> response, | 76 scoped_ptr<ServiceWorkerResponse> response, |
| 80 const ErrorCallback& callback); | 77 const ErrorCallback& callback); |
| 81 | 78 |
| 82 // Returns ErrorNotFound if not found. Otherwise deletes and returns | 79 // Returns ErrorNotFound if not found. Otherwise deletes and returns |
| 83 // ErrorTypeOK. The callback will always be called. |request| must remain | 80 // ErrorTypeOK. The callback will always be called. |request| must remain |
| 84 // valid until the callback is called. | 81 // valid until the callback is called. |
| 85 void Delete(scoped_ptr<ServiceWorkerFetchRequest> request, | 82 void Delete(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 86 const ErrorCallback& callback); | 83 const ErrorCallback& callback); |
| 87 | 84 |
| 88 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest. | 85 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest. |
| 89 // Returns ErrorTypeOK and a vector of requests if there are no errors. The | 86 // Returns ErrorTypeOK and a vector of requests if there are no errors. The |
| 90 // callback will always be called. | 87 // callback will always be called. |
| 91 void Keys(const RequestsCallback& callback); | 88 void Keys(const RequestsCallback& callback); |
| 92 | 89 |
| 93 // Call to determine if CreateBackend must be called. | |
| 94 bool HasCreatedBackend() const; | |
| 95 | |
| 96 void set_backend(scoped_ptr<disk_cache::Backend> backend) { | 90 void set_backend(scoped_ptr<disk_cache::Backend> backend) { |
| 97 backend_ = backend.Pass(); | 91 backend_ = backend.Pass(); |
| 98 } | 92 } |
| 99 | 93 |
| 100 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); | 94 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); |
| 101 | 95 |
| 102 private: | 96 private: |
| 103 struct KeysContext; | 97 struct KeysContext; |
| 104 typedef std::vector<disk_cache::Entry*> Entries; | 98 typedef std::vector<disk_cache::Entry*> Entries; |
| 105 | 99 |
| 106 ServiceWorkerCache(const base::FilePath& path, | 100 ServiceWorkerCache(const base::FilePath& path, |
| 107 net::URLRequestContext* request_context, | 101 net::URLRequestContext* request_context, |
| 108 base::WeakPtr<storage::BlobStorageContext> blob_context); | 102 base::WeakPtr<storage::BlobStorageContext> blob_context); |
| 109 | 103 |
| 110 // Static callbacks for the Keys function. | 104 // Static callbacks for the Keys function. |
| 111 static void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context, | 105 static void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context, |
| 112 int rv); | 106 int rv); |
| 113 static void KeysProcessNextEntry(scoped_ptr<KeysContext> keys_context, | 107 static void KeysProcessNextEntry(scoped_ptr<KeysContext> keys_context, |
| 114 const Entries::iterator& iter); | 108 const Entries::iterator& iter); |
| 115 static void KeysDidReadHeaders( | 109 static void KeysDidReadHeaders( |
| 116 scoped_ptr<KeysContext> keys_context, | 110 scoped_ptr<KeysContext> keys_context, |
| 117 const Entries::iterator& iter, | 111 const Entries::iterator& iter, |
| 118 scoped_ptr<ServiceWorkerRequestResponseHeaders> headers); | 112 scoped_ptr<ServiceWorkerRequestResponseHeaders> headers); |
| 119 | 113 |
| 114 // Loads the backend and calls the callback with the result (true for |
| 115 // success). The callback will always be called. |
| 116 void CreateBackend(const ErrorCallback& callback); |
| 117 |
| 118 void Init(const base::Closure& callback); |
| 119 void InitDone(ErrorType error); |
| 120 |
| 120 scoped_ptr<disk_cache::Backend> backend_; | 121 scoped_ptr<disk_cache::Backend> backend_; |
| 121 base::FilePath path_; | 122 base::FilePath path_; |
| 122 net::URLRequestContext* request_context_; | 123 net::URLRequestContext* request_context_; |
| 123 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; | 124 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; |
| 125 bool initialized_; |
| 126 std::vector<base::Closure> init_callbacks_; |
| 124 | 127 |
| 125 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; | 128 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; |
| 126 | 129 |
| 127 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); | 130 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); |
| 128 }; | 131 }; |
| 129 | 132 |
| 130 } // namespace content | 133 } // namespace content |
| 131 | 134 |
| 132 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | 135 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ |
| OLD | NEW |