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/ref_counted.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 | 79 |
80 // Returns ErrorNotFound if not found. Otherwise deletes and returns | 80 // Returns ErrorNotFound if not found. Otherwise deletes and returns |
81 // ErrorTypeOK. | 81 // ErrorTypeOK. |
82 void Delete(scoped_ptr<ServiceWorkerFetchRequest> request, | 82 void Delete(scoped_ptr<ServiceWorkerFetchRequest> request, |
83 const ErrorCallback& callback); | 83 const ErrorCallback& callback); |
84 | 84 |
85 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest. | 85 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest. |
86 // Returns ErrorTypeOK and a vector of requests if there are no errors. | 86 // Returns ErrorTypeOK and a vector of requests if there are no errors. |
87 void Keys(const RequestsCallback& callback); | 87 void Keys(const RequestsCallback& callback); |
88 | 88 |
89 // Prevents further operations from starting on this object, waits for | 89 // Closes the backend. Pending and future operations that require the backend |
90 // existing operations to finish, and then deletes the backend. Close should | 90 // will exit early. Close should only be called once per ServiceWorkerCache. |
91 // only be called once per ServiceWorkerCache. | |
92 void Close(const base::Closure& callback); | 91 void Close(const base::Closure& callback); |
93 | 92 |
94 // The size of the cache contents in memory. Returns 0 if the cache backend is | 93 // The size of the cache contents in memory. Returns 0 if the cache backend is |
95 // not a memory cache backend. | 94 // not a memory cache backend. |
96 int64 MemoryBackedSize() const; | 95 int64 MemoryBackedSize() const; |
97 | 96 |
98 void set_backend(scoped_ptr<disk_cache::Backend> backend) { | 97 void set_backend(scoped_ptr<disk_cache::Backend> backend) { |
99 backend_ = backend.Pass(); | 98 backend_ = backend.Pass(); |
100 } | 99 } |
101 | 100 |
102 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); | 101 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); |
103 | 102 |
104 private: | 103 private: |
105 friend class base::RefCounted<ServiceWorkerCache>; | 104 friend class base::RefCounted<ServiceWorkerCache>; |
106 friend class TestServiceWorkerCache; | 105 friend class TestServiceWorkerCache; |
107 | 106 |
108 class BlobReader; | 107 class BlobReader; |
109 struct KeysContext; | 108 struct KeysContext; |
110 struct PutContext; | 109 struct PutContext; |
| 110 |
| 111 // The backend progresses from uninitialized, to open, to closed, and cannot |
| 112 // reverse direction. The open step may be skipped. |
| 113 enum BackendState { |
| 114 BACKEND_UNINITIALIZED, // No backend, create backend on first operation. |
| 115 BACKEND_OPEN, // Backend can be used. |
| 116 BACKEND_CLOSED // Backend cannot be used. All ops should fail. |
| 117 }; |
| 118 |
111 typedef std::vector<disk_cache::Entry*> Entries; | 119 typedef std::vector<disk_cache::Entry*> Entries; |
112 | 120 |
113 ServiceWorkerCache( | 121 ServiceWorkerCache( |
114 const GURL& origin, | 122 const GURL& origin, |
115 const base::FilePath& path, | 123 const base::FilePath& path, |
116 net::URLRequestContext* request_context, | 124 net::URLRequestContext* request_context, |
117 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 125 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
118 base::WeakPtr<storage::BlobStorageContext> blob_context); | 126 base::WeakPtr<storage::BlobStorageContext> blob_context); |
119 | 127 |
120 // Operations in progress will complete after the cache is deleted but pending | 128 // Operations in progress will complete after the cache is deleted but pending |
(...skipping 16 matching lines...) Expand all Loading... |
137 // Static callbacks for the Keys function. | 145 // Static callbacks for the Keys function. |
138 static void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context, | 146 static void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context, |
139 int rv); | 147 int rv); |
140 static void KeysProcessNextEntry(scoped_ptr<KeysContext> keys_context, | 148 static void KeysProcessNextEntry(scoped_ptr<KeysContext> keys_context, |
141 const Entries::iterator& iter); | 149 const Entries::iterator& iter); |
142 static void KeysDidReadMetadata( | 150 static void KeysDidReadMetadata( |
143 scoped_ptr<KeysContext> keys_context, | 151 scoped_ptr<KeysContext> keys_context, |
144 const Entries::iterator& iter, | 152 const Entries::iterator& iter, |
145 scoped_ptr<ServiceWorkerCacheMetadata> metadata); | 153 scoped_ptr<ServiceWorkerCacheMetadata> metadata); |
146 | 154 |
| 155 void CloseImpl(const base::Closure& callback); |
| 156 |
147 // Loads the backend and calls the callback with the result (true for | 157 // Loads the backend and calls the callback with the result (true for |
148 // success). The callback will always be called. Virtual for tests. | 158 // success). The callback will always be called. Virtual for tests. |
149 virtual void CreateBackend(const ErrorCallback& callback); | 159 virtual void CreateBackend(const ErrorCallback& callback); |
150 | 160 |
151 void Init(const base::Closure& callback); | 161 void InitBackend(const base::Closure& callback); |
152 void InitDone(ErrorType error); | 162 void InitDone(ErrorType error); |
153 | 163 |
154 void IncPendingOps() { pending_ops_++; } | 164 void IncPendingOps() { pending_ops_++; } |
155 void DecPendingOps(); | 165 void DecPendingOps(); |
156 void PendingErrorCallback(const ErrorCallback& callback, ErrorType error); | 166 void PendingErrorCallback(const ErrorCallback& callback, ErrorType error); |
157 void PendingResponseCallback( | 167 void PendingResponseCallback( |
158 const ResponseCallback& callback, | 168 const ResponseCallback& callback, |
159 ErrorType error, | 169 ErrorType error, |
160 scoped_ptr<ServiceWorkerResponse> response, | 170 scoped_ptr<ServiceWorkerResponse> response, |
161 scoped_ptr<storage::BlobDataHandle> blob_data_handle); | 171 scoped_ptr<storage::BlobDataHandle> blob_data_handle); |
162 void PendingRequestsCallback(const RequestsCallback& callback, | 172 void PendingRequestsCallback(const RequestsCallback& callback, |
163 ErrorType error, | 173 ErrorType error, |
164 scoped_ptr<Requests> requests); | 174 scoped_ptr<Requests> requests); |
165 | 175 |
166 // The backend can be deleted via the Close function at any time so always | 176 // The backend can be deleted via the Close function at any time so always |
167 // check for its existence before use. | 177 // check for its existence before use. |
168 scoped_ptr<disk_cache::Backend> backend_; | 178 scoped_ptr<disk_cache::Backend> backend_; |
169 GURL origin_; | 179 GURL origin_; |
170 base::FilePath path_; | 180 base::FilePath path_; |
171 net::URLRequestContext* request_context_; | 181 net::URLRequestContext* request_context_; |
172 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; | 182 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; |
173 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; | 183 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; |
174 bool initialized_; | 184 BackendState backend_state_; |
175 std::vector<base::Closure> init_callbacks_; | 185 std::vector<base::Closure> init_callbacks_; |
176 | 186 |
177 // Whether or not to store data in disk or memory. | 187 // Whether or not to store data in disk or memory. |
178 bool memory_only_; | 188 bool memory_only_; |
179 | 189 |
180 // The number of started operations that have yet to complete. | 190 // The number of started operations that have yet to complete. |
181 // TODO(jkarlin): pending_ops_ gets double counted on lazy initialization (say | 191 // TODO(jkarlin): pending_ops_ gets double counted on lazy initialization (say |
182 // in ::Put). The counting still works but pending_ops_ doesn't accurately | 192 // in ::Put). The counting still works but pending_ops_ doesn't accurately |
183 // represent the number of operations in flight. Fix this by having the lazy | 193 // represent the number of operations in flight. Fix this by having the lazy |
184 // init callback call a different function than the original caller (::Put). | 194 // init callback call a different function than the original caller (::Put). |
185 size_t pending_ops_; | 195 size_t pending_ops_; |
186 base::Closure ops_complete_callback_; | 196 base::Closure ops_complete_callback_; |
187 | 197 |
188 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; | 198 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; |
189 | 199 |
190 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); | 200 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); |
191 }; | 201 }; |
192 | 202 |
193 } // namespace content | 203 } // namespace content |
194 | 204 |
195 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | 205 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ |
OLD | NEW |