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_
10 #include "base/callback.h"
11 #include "base/files/file_path.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "content/common/service_worker/service_worker_types.h"
15 #include "net/base/completion_callback.h"
16 #include "net/disk_cache/disk_cache.h"
19 class URLRequestContext
;
20 class IOBufferWithSize
;
24 class BlobDataBuilder
;
26 class BlobStorageContext
;
27 class QuotaManagerProxy
;
31 class ChromeBlobStorageContext
;
32 class ServiceWorkerCacheMetadata
;
33 class ServiceWorkerCacheScheduler
;
34 class TestServiceWorkerCache
;
36 // Represents a ServiceWorker Cache as seen in
37 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html.
38 // The asynchronous methods are executed serially. Callbacks to the
39 // public functions will be called so long as the cache object lives.
40 class CONTENT_EXPORT ServiceWorkerCache
41 : public base::RefCounted
<ServiceWorkerCache
> {
43 // This enum is used in histograms, so do not change the ordering and always
44 // append new types to the end.
50 ERROR_TYPE_LAST
= ERROR_TYPE_NOT_FOUND
53 enum EntryIndex
{ INDEX_HEADERS
= 0, INDEX_RESPONSE_BODY
};
54 typedef base::Callback
<void(ErrorType
)> ErrorCallback
;
55 typedef base::Callback
<void(ErrorType
,
56 scoped_ptr
<ServiceWorkerResponse
>,
57 scoped_ptr
<storage::BlobDataHandle
>)>
59 typedef std::vector
<ServiceWorkerFetchRequest
> Requests
;
60 typedef base::Callback
<void(ErrorType
, scoped_ptr
<Requests
>)>
63 static scoped_refptr
<ServiceWorkerCache
> CreateMemoryCache(
65 net::URLRequestContext
* request_context
,
66 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
,
67 base::WeakPtr
<storage::BlobStorageContext
> blob_context
);
68 static scoped_refptr
<ServiceWorkerCache
> CreatePersistentCache(
70 const base::FilePath
& path
,
71 net::URLRequestContext
* request_context
,
72 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
,
73 base::WeakPtr
<storage::BlobStorageContext
> blob_context
);
75 // Returns ERROR_TYPE_NOT_FOUND if not found.
76 void Match(scoped_ptr
<ServiceWorkerFetchRequest
> request
,
77 const ResponseCallback
& callback
);
79 // Puts the request and response object in the cache. The response body (if
80 // present) is stored in the cache, but not the request body. Returns
81 // ERROR_TYPE_OK on success.
82 void Put(scoped_ptr
<ServiceWorkerFetchRequest
> request
,
83 scoped_ptr
<ServiceWorkerResponse
> response
,
84 const ResponseCallback
& callback
);
86 // Returns ErrorNotFound if not found. Otherwise deletes and returns
88 void Delete(scoped_ptr
<ServiceWorkerFetchRequest
> request
,
89 const ErrorCallback
& callback
);
91 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest.
92 // Returns ErrorTypeOK and a vector of requests if there are no errors.
93 void Keys(const RequestsCallback
& callback
);
95 // Closes the backend. Future operations that require the backend
96 // will exit early. Close should only be called once per ServiceWorkerCache.
97 void Close(const base::Closure
& callback
);
99 // The size of the cache contents in memory. Returns 0 if the cache backend is
100 // not a memory cache backend.
101 int64
MemoryBackedSize() const;
103 base::WeakPtr
<ServiceWorkerCache
> AsWeakPtr();
106 friend class base::RefCounted
<ServiceWorkerCache
>;
107 friend class TestServiceWorkerCache
;
114 // The backend progresses from uninitialized, to open, to closed, and cannot
115 // reverse direction. The open step may be skipped.
117 BACKEND_UNINITIALIZED
, // No backend, create backend on first operation.
118 BACKEND_OPEN
, // Backend can be used.
119 BACKEND_CLOSED
// Backend cannot be used. All ops should fail.
122 typedef std::vector
<disk_cache::Entry
*> Entries
;
123 typedef scoped_ptr
<disk_cache::Backend
> ScopedBackendPtr
;
127 const base::FilePath
& path
,
128 net::URLRequestContext
* request_context
,
129 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
,
130 base::WeakPtr
<storage::BlobStorageContext
> blob_context
);
132 // Async operations in progress will cancel and not run their callbacks.
133 virtual ~ServiceWorkerCache();
136 void MatchImpl(scoped_ptr
<ServiceWorkerFetchRequest
> request
,
137 const ResponseCallback
& callback
);
138 void MatchDidOpenEntry(scoped_ptr
<MatchContext
> match_context
, int rv
);
139 void MatchDidReadMetadata(scoped_ptr
<MatchContext
> match_context
,
140 scoped_ptr
<ServiceWorkerCacheMetadata
> headers
);
141 void MatchDidReadResponseBodyData(scoped_ptr
<MatchContext
> match_context
,
143 void MatchDoneWithBody(scoped_ptr
<MatchContext
> match_context
);
146 void PutImpl(scoped_ptr
<PutContext
> put_context
);
147 void PutDidDelete(scoped_ptr
<PutContext
> put_context
, ErrorType delete_error
);
148 void PutDidCreateEntry(scoped_ptr
<PutContext
> put_context
, int rv
);
149 void PutDidWriteHeaders(scoped_ptr
<PutContext
> put_context
,
152 void PutDidWriteBlobToCache(scoped_ptr
<PutContext
> put_context
,
153 scoped_ptr
<BlobReader
> blob_reader
,
154 disk_cache::ScopedEntryPtr entry
,
158 void DeleteImpl(scoped_ptr
<ServiceWorkerFetchRequest
> request
,
159 const ErrorCallback
& callback
);
160 void DeleteDidOpenEntry(
162 scoped_ptr
<ServiceWorkerFetchRequest
> request
,
163 const ServiceWorkerCache::ErrorCallback
& callback
,
164 scoped_ptr
<disk_cache::Entry
*> entryptr
,
165 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
,
169 void KeysImpl(const RequestsCallback
& callback
);
170 void KeysDidOpenNextEntry(scoped_ptr
<KeysContext
> keys_context
, int rv
);
171 void KeysProcessNextEntry(scoped_ptr
<KeysContext
> keys_context
,
172 const Entries::iterator
& iter
);
173 void KeysDidReadMetadata(scoped_ptr
<KeysContext
> keys_context
,
174 const Entries::iterator
& iter
,
175 scoped_ptr
<ServiceWorkerCacheMetadata
> metadata
);
177 void CloseImpl(const base::Closure
& callback
);
179 // Loads the backend and calls the callback with the result (true for
180 // success). The callback will always be called. Virtual for tests.
181 virtual void CreateBackend(const ErrorCallback
& callback
);
182 void CreateBackendDidCreate(const ServiceWorkerCache::ErrorCallback
& callback
,
183 scoped_ptr
<ScopedBackendPtr
> backend_ptr
,
187 void InitDone(ErrorType error
);
189 void PendingClosure(const base::Closure
& callback
);
190 void PendingErrorCallback(const ErrorCallback
& callback
, ErrorType error
);
191 void PendingResponseCallback(
192 const ResponseCallback
& callback
,
194 scoped_ptr
<ServiceWorkerResponse
> response
,
195 scoped_ptr
<storage::BlobDataHandle
> blob_data_handle
);
196 void PendingRequestsCallback(const RequestsCallback
& callback
,
198 scoped_ptr
<Requests
> requests
);
200 // Be sure to check |backend_state_| before use.
201 scoped_ptr
<disk_cache::Backend
> backend_
;
204 base::FilePath path_
;
205 net::URLRequestContext
* request_context_
;
206 scoped_refptr
<storage::QuotaManagerProxy
> quota_manager_proxy_
;
207 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context_
;
208 BackendState backend_state_
;
209 scoped_ptr
<ServiceWorkerCacheScheduler
> scheduler_
;
212 // Whether or not to store data in disk or memory.
215 base::WeakPtrFactory
<ServiceWorkerCache
> weak_ptr_factory_
;
217 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache
);
220 } // namespace content
222 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_