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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_
8 #include "base/callback.h"
9 #include "base/files/file_path.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "content/common/service_worker/service_worker_types.h"
13 #include "net/base/completion_callback.h"
14 #include "net/disk_cache/disk_cache.h"
17 class URLRequestContext
;
18 class IOBufferWithSize
;
24 class BlobStorageContext
;
25 class QuotaManagerProxy
;
29 class ChromeBlobStorageContext
;
30 class ServiceWorkerCacheMetadata
;
31 class TestServiceWorkerCache
;
33 // Represents a ServiceWorker Cache as seen in
34 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html.
35 // Callbacks to the public functions will be called so long as the cache object
37 class CONTENT_EXPORT ServiceWorkerCache
38 : public base::RefCounted
<ServiceWorkerCache
> {
47 enum EntryIndex
{ INDEX_HEADERS
= 0, INDEX_RESPONSE_BODY
};
48 typedef base::Callback
<void(ErrorType
)> ErrorCallback
;
49 typedef base::Callback
<void(ErrorType
,
50 scoped_ptr
<ServiceWorkerResponse
>,
51 scoped_ptr
<storage::BlobDataHandle
>)>
53 typedef std::vector
<ServiceWorkerFetchRequest
> Requests
;
54 typedef base::Callback
<void(ErrorType
, scoped_ptr
<Requests
>)>
57 static scoped_refptr
<ServiceWorkerCache
> CreateMemoryCache(
59 net::URLRequestContext
* request_context
,
60 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
,
61 base::WeakPtr
<storage::BlobStorageContext
> blob_context
);
62 static scoped_refptr
<ServiceWorkerCache
> CreatePersistentCache(
64 const base::FilePath
& path
,
65 net::URLRequestContext
* request_context
,
66 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
,
67 base::WeakPtr
<storage::BlobStorageContext
> blob_context
);
69 // Returns ErrorTypeNotFound if not found.
70 void Match(scoped_ptr
<ServiceWorkerFetchRequest
> request
,
71 const ResponseCallback
& callback
);
73 // Puts the request and response object in the cache. The response body (if
74 // present) is stored in the cache, but not the request body. Returns
75 // ErrorTypeOK on success.
76 void Put(scoped_ptr
<ServiceWorkerFetchRequest
> request
,
77 scoped_ptr
<ServiceWorkerResponse
> response
,
78 const ResponseCallback
& callback
);
80 // Returns ErrorNotFound if not found. Otherwise deletes and returns
82 void Delete(scoped_ptr
<ServiceWorkerFetchRequest
> request
,
83 const ErrorCallback
& callback
);
85 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest.
86 // Returns ErrorTypeOK and a vector of requests if there are no errors.
87 void Keys(const RequestsCallback
& callback
);
89 // Closes the backend. Pending and future operations that require the backend
90 // will exit early. Close should only be called once per ServiceWorkerCache.
91 void Close(const base::Closure
& callback
);
93 // The size of the cache contents in memory. Returns 0 if the cache backend is
94 // not a memory cache backend.
95 int64
MemoryBackedSize() const;
97 base::WeakPtr
<ServiceWorkerCache
> AsWeakPtr();
100 friend class base::RefCounted
<ServiceWorkerCache
>;
101 friend class TestServiceWorkerCache
;
108 // The backend progresses from uninitialized, to open, to closed, and cannot
109 // reverse direction. The open step may be skipped.
111 BACKEND_UNINITIALIZED
, // No backend, create backend on first operation.
112 BACKEND_OPEN
, // Backend can be used.
113 BACKEND_CLOSED
// Backend cannot be used. All ops should fail.
116 typedef std::vector
<disk_cache::Entry
*> Entries
;
117 typedef scoped_ptr
<disk_cache::Backend
> ScopedBackendPtr
;
121 const base::FilePath
& path
,
122 net::URLRequestContext
* request_context
,
123 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
,
124 base::WeakPtr
<storage::BlobStorageContext
> blob_context
);
126 // Async operations in progress will cancel and not run their callbacks.
127 virtual ~ServiceWorkerCache();
130 void MatchDidOpenEntry(scoped_ptr
<MatchContext
> match_context
, int rv
);
131 void MatchDidReadMetadata(scoped_ptr
<MatchContext
> match_context
,
132 scoped_ptr
<ServiceWorkerCacheMetadata
> headers
);
133 void MatchDidReadResponseBodyData(scoped_ptr
<MatchContext
> match_context
,
135 void MatchDoneWithBody(scoped_ptr
<MatchContext
> match_context
);
138 void PutImpl(scoped_ptr
<PutContext
> put_context
);
139 void PutDidDelete(scoped_ptr
<PutContext
> put_context
, ErrorType delete_error
);
140 void PutDidCreateEntry(scoped_ptr
<PutContext
> put_context
, int rv
);
141 void PutDidWriteHeaders(scoped_ptr
<PutContext
> put_context
,
144 void PutDidWriteBlobToCache(scoped_ptr
<PutContext
> put_context
,
145 scoped_ptr
<BlobReader
> blob_reader
,
146 disk_cache::ScopedEntryPtr entry
,
150 void DeleteDidOpenEntry(
152 scoped_ptr
<ServiceWorkerFetchRequest
> request
,
153 const ServiceWorkerCache::ErrorCallback
& callback
,
154 scoped_ptr
<disk_cache::Entry
*> entryptr
,
155 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
,
159 void KeysDidOpenNextEntry(scoped_ptr
<KeysContext
> keys_context
, int rv
);
160 void KeysProcessNextEntry(scoped_ptr
<KeysContext
> keys_context
,
161 const Entries::iterator
& iter
);
162 void KeysDidReadMetadata(scoped_ptr
<KeysContext
> keys_context
,
163 const Entries::iterator
& iter
,
164 scoped_ptr
<ServiceWorkerCacheMetadata
> metadata
);
166 void CloseImpl(const base::Closure
& callback
);
168 // Loads the backend and calls the callback with the result (true for
169 // success). The callback will always be called. Virtual for tests.
170 virtual void CreateBackend(const ErrorCallback
& callback
);
171 void CreateBackendDidCreate(const ServiceWorkerCache::ErrorCallback
& callback
,
172 scoped_ptr
<ScopedBackendPtr
> backend_ptr
,
175 void InitBackend(const base::Closure
& callback
);
176 void InitDone(ErrorType error
);
178 void IncPendingOps() { pending_ops_
++; }
179 void DecPendingOps();
180 void PendingErrorCallback(const ErrorCallback
& callback
, ErrorType error
);
181 void PendingResponseCallback(
182 const ResponseCallback
& callback
,
184 scoped_ptr
<ServiceWorkerResponse
> response
,
185 scoped_ptr
<storage::BlobDataHandle
> blob_data_handle
);
186 void PendingRequestsCallback(const RequestsCallback
& callback
,
188 scoped_ptr
<Requests
> requests
);
190 // The backend can be deleted via the Close function at any time so always
191 // check for its existence before use.
192 scoped_ptr
<disk_cache::Backend
> backend_
;
194 base::FilePath path_
;
195 net::URLRequestContext
* request_context_
;
196 scoped_refptr
<storage::QuotaManagerProxy
> quota_manager_proxy_
;
197 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context_
;
198 BackendState backend_state_
;
199 std::vector
<base::Closure
> init_callbacks_
;
201 // Whether or not to store data in disk or memory.
204 // The number of started operations that have yet to complete.
205 // TODO(jkarlin): pending_ops_ gets double counted on lazy initialization (say
206 // in ::Put). The counting still works but pending_ops_ doesn't accurately
207 // represent the number of operations in flight. Fix this by having the lazy
208 // init callback call a different function than the original caller (::Put).
210 base::Closure ops_complete_callback_
;
212 base::WeakPtrFactory
<ServiceWorkerCache
> weak_ptr_factory_
;
214 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache
);
217 } // namespace content
219 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_