Have ServiceWorkerCache::Put return a Response when completed.
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_cache.h
bloba88845e7a3216f431730b594b6598341b38f1a81
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"
16 namespace net {
17 class URLRequestContext;
18 class IOBufferWithSize;
21 namespace storage {
22 class BlobData;
23 class BlobDataHandle;
24 class BlobStorageContext;
27 namespace content {
28 class ChromeBlobStorageContext;
29 class ServiceWorkerRequestResponseHeaders;
31 // TODO(jkarlin): Unload cache backend from memory once the cache object is no
32 // longer referenced in javascript.
34 // Represents a ServiceWorker Cache as seen in
35 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html.
36 // InitializeIfNeeded must be called before calling the other public members.
37 class CONTENT_EXPORT ServiceWorkerCache
38 : public base::RefCounted<ServiceWorkerCache> {
39 public:
40 enum ErrorType {
41 ErrorTypeOK = 0,
42 ErrorTypeExists,
43 ErrorTypeStorage,
44 ErrorTypeNotFound
46 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY };
47 typedef base::Callback<void(ErrorType)> ErrorCallback;
48 typedef base::Callback<void(ErrorType,
49 scoped_ptr<ServiceWorkerResponse>,
50 scoped_ptr<storage::BlobDataHandle>)>
51 ResponseCallback;
52 typedef std::vector<ServiceWorkerFetchRequest> Requests;
53 typedef base::Callback<void(ErrorType, scoped_ptr<Requests>)>
54 RequestsCallback;
56 static scoped_refptr<ServiceWorkerCache> CreateMemoryCache(
57 net::URLRequestContext* request_context,
58 base::WeakPtr<storage::BlobStorageContext> blob_context);
59 static scoped_refptr<ServiceWorkerCache> CreatePersistentCache(
60 const base::FilePath& path,
61 net::URLRequestContext* request_context,
62 base::WeakPtr<storage::BlobStorageContext> blob_context);
64 // Returns ErrorTypeNotFound if not found. The callback will always be called.
65 void Match(scoped_ptr<ServiceWorkerFetchRequest> request,
66 const ResponseCallback& callback);
68 // Puts the request and response object in the cache. The response body (if
69 // present) is stored in the cache, but not the request body. Returns
70 // ErrorTypeOK on success. The callback will always be called.
71 void Put(scoped_ptr<ServiceWorkerFetchRequest> request,
72 scoped_ptr<ServiceWorkerResponse> response,
73 const ResponseCallback& callback);
75 // Returns ErrorNotFound if not found. Otherwise deletes and returns
76 // ErrorTypeOK. The callback will always be called.
77 void Delete(scoped_ptr<ServiceWorkerFetchRequest> request,
78 const ErrorCallback& callback);
80 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest.
81 // Returns ErrorTypeOK and a vector of requests if there are no errors. The
82 // callback will always be called.
83 void Keys(const RequestsCallback& callback);
85 // Prevent further operations on this object and delete the backend.
86 void Close();
88 void set_backend(scoped_ptr<disk_cache::Backend> backend) {
89 backend_ = backend.Pass();
92 base::WeakPtr<ServiceWorkerCache> AsWeakPtr();
94 private:
95 friend class base::RefCounted<ServiceWorkerCache>;
97 struct KeysContext;
98 typedef std::vector<disk_cache::Entry*> Entries;
100 ServiceWorkerCache(const base::FilePath& path,
101 net::URLRequestContext* request_context,
102 base::WeakPtr<storage::BlobStorageContext> blob_context);
104 // Operations in progress will complete after the cache is deleted but pending
105 // operations (those operations waiting for init to finish) won't.
106 virtual ~ServiceWorkerCache();
108 void PutImpl(scoped_ptr<ServiceWorkerFetchRequest> request,
109 scoped_ptr<ServiceWorkerResponse> response,
110 scoped_ptr<storage::BlobDataHandle> blob_data_handle,
111 const ResponseCallback& callback);
113 // Static callbacks for the Keys function.
114 static void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context,
115 int rv);
116 static void KeysProcessNextEntry(scoped_ptr<KeysContext> keys_context,
117 const Entries::iterator& iter);
118 static void KeysDidReadHeaders(
119 scoped_ptr<KeysContext> keys_context,
120 const Entries::iterator& iter,
121 scoped_ptr<ServiceWorkerRequestResponseHeaders> headers);
123 // Loads the backend and calls the callback with the result (true for
124 // success). The callback will always be called.
125 void CreateBackend(const ErrorCallback& callback);
127 void Init(const base::Closure& callback);
128 void InitDone(ErrorType error);
130 // The backend can be deleted via the Close function at any time so always
131 // check for its existence before use.
132 scoped_ptr<disk_cache::Backend> backend_;
133 base::FilePath path_;
134 net::URLRequestContext* request_context_;
135 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_;
136 bool initialized_;
137 std::vector<base::Closure> init_callbacks_;
139 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_;
141 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache);
144 } // namespace content
146 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_