ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_cache.h
bloba12a324c9c09ad6a0193742ce517dd4fd4cfd3ff
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 <list>
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"
18 namespace net {
19 class URLRequestContext;
20 class IOBufferWithSize;
23 namespace storage {
24 class BlobDataBuilder;
25 class BlobDataHandle;
26 class BlobStorageContext;
27 class QuotaManagerProxy;
30 namespace content {
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> {
42 public:
43 enum ErrorType {
44 ErrorTypeOK = 0,
45 ErrorTypeExists,
46 ErrorTypeStorage,
47 ErrorTypeNotFound
50 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY };
51 typedef base::Callback<void(ErrorType)> ErrorCallback;
52 typedef base::Callback<void(ErrorType,
53 scoped_ptr<ServiceWorkerResponse>,
54 scoped_ptr<storage::BlobDataHandle>)>
55 ResponseCallback;
56 typedef std::vector<ServiceWorkerFetchRequest> Requests;
57 typedef base::Callback<void(ErrorType, scoped_ptr<Requests>)>
58 RequestsCallback;
60 static scoped_refptr<ServiceWorkerCache> CreateMemoryCache(
61 const GURL& origin,
62 net::URLRequestContext* request_context,
63 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
64 base::WeakPtr<storage::BlobStorageContext> blob_context);
65 static scoped_refptr<ServiceWorkerCache> CreatePersistentCache(
66 const GURL& origin,
67 const base::FilePath& path,
68 net::URLRequestContext* request_context,
69 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
70 base::WeakPtr<storage::BlobStorageContext> blob_context);
72 // Returns ErrorTypeNotFound if not found.
73 void Match(scoped_ptr<ServiceWorkerFetchRequest> request,
74 const ResponseCallback& callback);
76 // Puts the request and response object in the cache. The response body (if
77 // present) is stored in the cache, but not the request body. Returns
78 // ErrorTypeOK on success.
79 void Put(scoped_ptr<ServiceWorkerFetchRequest> request,
80 scoped_ptr<ServiceWorkerResponse> response,
81 const ResponseCallback& callback);
83 // Returns ErrorNotFound if not found. Otherwise deletes and returns
84 // ErrorTypeOK.
85 void Delete(scoped_ptr<ServiceWorkerFetchRequest> request,
86 const ErrorCallback& callback);
88 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest.
89 // Returns ErrorTypeOK and a vector of requests if there are no errors.
90 void Keys(const RequestsCallback& callback);
92 // Closes the backend. Future operations that require the backend
93 // will exit early. Close should only be called once per ServiceWorkerCache.
94 void Close(const base::Closure& callback);
96 // The size of the cache contents in memory. Returns 0 if the cache backend is
97 // not a memory cache backend.
98 int64 MemoryBackedSize() const;
100 base::WeakPtr<ServiceWorkerCache> AsWeakPtr();
102 private:
103 friend class base::RefCounted<ServiceWorkerCache>;
104 friend class TestServiceWorkerCache;
106 class BlobReader;
107 struct KeysContext;
108 struct MatchContext;
109 struct PutContext;
111 // The backend progresses from uninitialized, to open, to closed, and cannot
112 // reverse direction. The open step may be skipped.
113 enum BackendState {
114 BACKEND_UNINITIALIZED, // No backend, create backend on first operation.
115 BACKEND_OPEN, // Backend can be used.
116 BACKEND_CLOSED // Backend cannot be used. All ops should fail.
119 typedef std::vector<disk_cache::Entry*> Entries;
120 typedef scoped_ptr<disk_cache::Backend> ScopedBackendPtr;
122 ServiceWorkerCache(
123 const GURL& origin,
124 const base::FilePath& path,
125 net::URLRequestContext* request_context,
126 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
127 base::WeakPtr<storage::BlobStorageContext> blob_context);
129 // Async operations in progress will cancel and not run their callbacks.
130 virtual ~ServiceWorkerCache();
132 // Match callbacks
133 void MatchImpl(scoped_ptr<ServiceWorkerFetchRequest> request,
134 const ResponseCallback& callback);
135 void MatchDidOpenEntry(scoped_ptr<MatchContext> match_context, int rv);
136 void MatchDidReadMetadata(scoped_ptr<MatchContext> match_context,
137 scoped_ptr<ServiceWorkerCacheMetadata> headers);
138 void MatchDidReadResponseBodyData(scoped_ptr<MatchContext> match_context,
139 int rv);
140 void MatchDoneWithBody(scoped_ptr<MatchContext> match_context);
142 // Put callbacks.
143 void PutImpl(scoped_ptr<PutContext> put_context);
144 void PutDidDelete(scoped_ptr<PutContext> put_context, ErrorType delete_error);
145 void PutDidCreateEntry(scoped_ptr<PutContext> put_context, int rv);
146 void PutDidWriteHeaders(scoped_ptr<PutContext> put_context,
147 int expected_bytes,
148 int rv);
149 void PutDidWriteBlobToCache(scoped_ptr<PutContext> put_context,
150 scoped_ptr<BlobReader> blob_reader,
151 disk_cache::ScopedEntryPtr entry,
152 bool success);
154 // Delete callbacks
155 void DeleteImpl(scoped_ptr<ServiceWorkerFetchRequest> request,
156 const ErrorCallback& callback);
157 void DeleteDidOpenEntry(
158 const GURL& origin,
159 scoped_ptr<ServiceWorkerFetchRequest> request,
160 const ServiceWorkerCache::ErrorCallback& callback,
161 scoped_ptr<disk_cache::Entry*> entryptr,
162 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
163 int rv);
165 // Keys callbacks.
166 void KeysImpl(const RequestsCallback& callback);
167 void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context, int rv);
168 void KeysProcessNextEntry(scoped_ptr<KeysContext> keys_context,
169 const Entries::iterator& iter);
170 void KeysDidReadMetadata(scoped_ptr<KeysContext> keys_context,
171 const Entries::iterator& iter,
172 scoped_ptr<ServiceWorkerCacheMetadata> metadata);
174 void CloseImpl(const base::Closure& callback);
176 // Loads the backend and calls the callback with the result (true for
177 // success). The callback will always be called. Virtual for tests.
178 virtual void CreateBackend(const ErrorCallback& callback);
179 void CreateBackendDidCreate(const ServiceWorkerCache::ErrorCallback& callback,
180 scoped_ptr<ScopedBackendPtr> backend_ptr,
181 int rv);
183 void InitBackend();
184 void InitDone(ErrorType error);
186 void PendingClosure(const base::Closure& callback);
187 void PendingErrorCallback(const ErrorCallback& callback, ErrorType error);
188 void PendingResponseCallback(
189 const ResponseCallback& callback,
190 ErrorType error,
191 scoped_ptr<ServiceWorkerResponse> response,
192 scoped_ptr<storage::BlobDataHandle> blob_data_handle);
193 void PendingRequestsCallback(const RequestsCallback& callback,
194 ErrorType error,
195 scoped_ptr<Requests> requests);
197 // Be sure to check |backend_state_| before use.
198 scoped_ptr<disk_cache::Backend> backend_;
200 GURL origin_;
201 base::FilePath path_;
202 net::URLRequestContext* request_context_;
203 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_;
204 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_;
205 BackendState backend_state_;
206 scoped_ptr<ServiceWorkerCacheScheduler> scheduler_;
207 bool initializing_;
209 // Whether or not to store data in disk or memory.
210 bool memory_only_;
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_