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 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 | 101 |
102 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); | 102 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); |
103 | 103 |
104 private: | 104 private: |
105 friend class base::RefCounted<ServiceWorkerCache>; | 105 friend class base::RefCounted<ServiceWorkerCache>; |
106 friend class TestServiceWorkerCache; | 106 friend class TestServiceWorkerCache; |
107 | 107 |
108 class BlobReader; | 108 class BlobReader; |
109 struct KeysContext; | 109 struct KeysContext; |
110 struct PutContext; | 110 struct PutContext; |
111 | |
112 // The backend progresses from uninitialized, to open, to closed, and cannot | |
113 // reverse direction. The open step may be skipped. | |
114 enum BackendState { | |
115 BackendUninitialized, // No backend, create backend on first operation. | |
116 BackendOpen, // Backend can be used. | |
117 BackendClosed // Backend cannot be used. All ops should fail. | |
118 }; | |
119 | |
120 typedef std::vector<disk_cache::Entry*> Entries; | 111 typedef std::vector<disk_cache::Entry*> Entries; |
121 | 112 |
122 ServiceWorkerCache( | 113 ServiceWorkerCache( |
123 const GURL& origin, | 114 const GURL& origin, |
124 const base::FilePath& path, | 115 const base::FilePath& path, |
125 net::URLRequestContext* request_context, | 116 net::URLRequestContext* request_context, |
126 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 117 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
127 base::WeakPtr<storage::BlobStorageContext> blob_context); | 118 base::WeakPtr<storage::BlobStorageContext> blob_context); |
128 | 119 |
129 // Operations in progress will complete after the cache is deleted but pending | 120 // Operations in progress will complete after the cache is deleted but pending |
(...skipping 20 matching lines...) Expand all Loading... |
150 const Entries::iterator& iter); | 141 const Entries::iterator& iter); |
151 static void KeysDidReadMetadata( | 142 static void KeysDidReadMetadata( |
152 scoped_ptr<KeysContext> keys_context, | 143 scoped_ptr<KeysContext> keys_context, |
153 const Entries::iterator& iter, | 144 const Entries::iterator& iter, |
154 scoped_ptr<ServiceWorkerCacheMetadata> metadata); | 145 scoped_ptr<ServiceWorkerCacheMetadata> metadata); |
155 | 146 |
156 // Loads the backend and calls the callback with the result (true for | 147 // Loads the backend and calls the callback with the result (true for |
157 // success). The callback will always be called. Virtual for tests. | 148 // success). The callback will always be called. Virtual for tests. |
158 virtual void CreateBackend(const ErrorCallback& callback); | 149 virtual void CreateBackend(const ErrorCallback& callback); |
159 | 150 |
160 void InitBackend(const base::Closure& callback); | 151 void Init(const base::Closure& callback); |
161 void InitDone(ErrorType error); | 152 void InitDone(ErrorType error); |
162 | 153 |
163 void IncPendingOps() { pending_ops_++; } | 154 void IncPendingOps() { pending_ops_++; } |
164 void DecPendingOps(); | 155 void DecPendingOps(); |
165 void PendingErrorCallback(const ErrorCallback& callback, ErrorType error); | 156 void PendingErrorCallback(const ErrorCallback& callback, ErrorType error); |
166 void PendingResponseCallback( | 157 void PendingResponseCallback( |
167 const ResponseCallback& callback, | 158 const ResponseCallback& callback, |
168 ErrorType error, | 159 ErrorType error, |
169 scoped_ptr<ServiceWorkerResponse> response, | 160 scoped_ptr<ServiceWorkerResponse> response, |
170 scoped_ptr<storage::BlobDataHandle> blob_data_handle); | 161 scoped_ptr<storage::BlobDataHandle> blob_data_handle); |
171 void PendingRequestsCallback(const RequestsCallback& callback, | 162 void PendingRequestsCallback(const RequestsCallback& callback, |
172 ErrorType error, | 163 ErrorType error, |
173 scoped_ptr<Requests> requests); | 164 scoped_ptr<Requests> requests); |
174 | 165 |
175 // The backend can be deleted via the Close function at any time so always | 166 // The backend can be deleted via the Close function at any time so always |
176 // check for its existence before use. | 167 // check for its existence before use. |
177 scoped_ptr<disk_cache::Backend> backend_; | 168 scoped_ptr<disk_cache::Backend> backend_; |
178 GURL origin_; | 169 GURL origin_; |
179 base::FilePath path_; | 170 base::FilePath path_; |
180 net::URLRequestContext* request_context_; | 171 net::URLRequestContext* request_context_; |
181 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; | 172 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; |
182 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; | 173 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; |
183 BackendState backend_state_; | 174 bool initialized_; |
184 std::vector<base::Closure> init_callbacks_; | 175 std::vector<base::Closure> init_callbacks_; |
185 | 176 |
186 // Whether or not to store data in disk or memory. | 177 // Whether or not to store data in disk or memory. |
187 bool memory_only_; | 178 bool memory_only_; |
188 | 179 |
189 // The number of started operations that have yet to complete. | 180 // The number of started operations that have yet to complete. |
190 // TODO(jkarlin): pending_ops_ gets double counted on lazy initialization (say | 181 // TODO(jkarlin): pending_ops_ gets double counted on lazy initialization (say |
191 // in ::Put). The counting still works but pending_ops_ doesn't accurately | 182 // in ::Put). The counting still works but pending_ops_ doesn't accurately |
192 // represent the number of operations in flight. Fix this by having the lazy | 183 // represent the number of operations in flight. Fix this by having the lazy |
193 // init callback call a different function than the original caller (::Put). | 184 // init callback call a different function than the original caller (::Put). |
194 size_t pending_ops_; | 185 size_t pending_ops_; |
195 base::Closure ops_complete_callback_; | 186 base::Closure ops_complete_callback_; |
196 | 187 |
197 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; | 188 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; |
198 | 189 |
199 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); | 190 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); |
200 }; | 191 }; |
201 | 192 |
202 } // namespace content | 193 } // namespace content |
203 | 194 |
204 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | 195 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ |
OLD | NEW |