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
;
32 // Represents a ServiceWorker Cache as seen in
33 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html.
34 // InitializeIfNeeded must be called before calling the other public members.
35 class CONTENT_EXPORT ServiceWorkerCache
36 : public base::RefCounted
<ServiceWorkerCache
> {
44 enum EntryIndex
{ INDEX_HEADERS
= 0, INDEX_RESPONSE_BODY
};
45 typedef base::Callback
<void(ErrorType
)> ErrorCallback
;
46 typedef base::Callback
<void(ErrorType
,
47 scoped_ptr
<ServiceWorkerResponse
>,
48 scoped_ptr
<storage::BlobDataHandle
>)>
50 typedef std::vector
<ServiceWorkerFetchRequest
> Requests
;
51 typedef base::Callback
<void(ErrorType
, scoped_ptr
<Requests
>)>
54 static scoped_refptr
<ServiceWorkerCache
> CreateMemoryCache(
56 net::URLRequestContext
* request_context
,
57 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
,
58 base::WeakPtr
<storage::BlobStorageContext
> blob_context
);
59 static scoped_refptr
<ServiceWorkerCache
> CreatePersistentCache(
61 const base::FilePath
& path
,
62 net::URLRequestContext
* request_context
,
63 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
,
64 base::WeakPtr
<storage::BlobStorageContext
> blob_context
);
66 // Returns ErrorTypeNotFound if not found. The callback will always be called.
67 void Match(scoped_ptr
<ServiceWorkerFetchRequest
> request
,
68 const ResponseCallback
& callback
);
70 // Puts the request and response object in the cache. The response body (if
71 // present) is stored in the cache, but not the request body. Returns
72 // ErrorTypeOK on success. The callback will always be called.
73 void Put(scoped_ptr
<ServiceWorkerFetchRequest
> request
,
74 scoped_ptr
<ServiceWorkerResponse
> response
,
75 const ResponseCallback
& callback
);
77 // Returns ErrorNotFound if not found. Otherwise deletes and returns
78 // ErrorTypeOK. The callback will always be called.
79 void Delete(scoped_ptr
<ServiceWorkerFetchRequest
> request
,
80 const ErrorCallback
& callback
);
82 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest.
83 // Returns ErrorTypeOK and a vector of requests if there are no errors. The
84 // callback will always be called.
85 void Keys(const RequestsCallback
& callback
);
87 // Prevent further operations on this object and delete the backend.
90 // The size of the cache contents in memory. Returns 0 if the cache backend is
91 // not a memory cache backend.
92 int64
MemoryBackedSize() const;
94 void set_backend(scoped_ptr
<disk_cache::Backend
> backend
) {
95 backend_
= backend
.Pass();
98 base::WeakPtr
<ServiceWorkerCache
> AsWeakPtr();
101 friend class base::RefCounted
<ServiceWorkerCache
>;
106 typedef std::vector
<disk_cache::Entry
*> Entries
;
110 const base::FilePath
& path
,
111 net::URLRequestContext
* request_context
,
112 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
,
113 base::WeakPtr
<storage::BlobStorageContext
> blob_context
);
115 // Operations in progress will complete after the cache is deleted but pending
116 // operations (those operations waiting for init to finish) won't.
117 virtual ~ServiceWorkerCache();
120 static void PutImpl(scoped_ptr
<PutContext
> put_context
);
121 static void PutDidCreateEntry(scoped_ptr
<PutContext
> put_context
, int rv
);
122 static void PutDidWriteHeaders(scoped_ptr
<PutContext
> put_context
,
125 static void PutDidWriteBlobToCache(scoped_ptr
<PutContext
> put_context
,
126 scoped_ptr
<BlobReader
> blob_reader
,
127 disk_cache::ScopedEntryPtr entry
,
130 // Static callbacks for the Keys function.
131 static void KeysDidOpenNextEntry(scoped_ptr
<KeysContext
> keys_context
,
133 static void KeysProcessNextEntry(scoped_ptr
<KeysContext
> keys_context
,
134 const Entries::iterator
& iter
);
135 static void KeysDidReadMetadata(
136 scoped_ptr
<KeysContext
> keys_context
,
137 const Entries::iterator
& iter
,
138 scoped_ptr
<ServiceWorkerCacheMetadata
> metadata
);
140 // Loads the backend and calls the callback with the result (true for
141 // success). The callback will always be called.
142 void CreateBackend(const ErrorCallback
& callback
);
144 void Init(const base::Closure
& callback
);
145 void InitDone(ErrorType error
);
147 // The backend can be deleted via the Close function at any time so always
148 // check for its existence before use.
149 scoped_ptr
<disk_cache::Backend
> backend_
;
151 base::FilePath path_
;
152 net::URLRequestContext
* request_context_
;
153 scoped_refptr
<storage::QuotaManagerProxy
> quota_manager_proxy_
;
154 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context_
;
156 std::vector
<base::Closure
> init_callbacks_
;
158 // Whether or not to store data in disk or memory.
161 base::WeakPtrFactory
<ServiceWorkerCache
> weak_ptr_factory_
;
163 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache
);
166 } // namespace content
168 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_