cc: Make picture pile base thread safe.
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_cache.h
blob867fb3f54543559db78ee748b4ac42c19d302bea
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;
25 class QuotaManagerProxy;
28 namespace content {
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> {
37 public:
38 enum ErrorType {
39 ErrorTypeOK = 0,
40 ErrorTypeExists,
41 ErrorTypeStorage,
42 ErrorTypeNotFound
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>)>
49 ResponseCallback;
50 typedef std::vector<ServiceWorkerFetchRequest> Requests;
51 typedef base::Callback<void(ErrorType, scoped_ptr<Requests>)>
52 RequestsCallback;
54 static scoped_refptr<ServiceWorkerCache> CreateMemoryCache(
55 const GURL& origin,
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(
60 const GURL& origin,
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.
88 void Close();
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();
100 private:
101 friend class base::RefCounted<ServiceWorkerCache>;
103 class BlobReader;
104 struct KeysContext;
105 struct PutContext;
106 typedef std::vector<disk_cache::Entry*> Entries;
108 ServiceWorkerCache(
109 const GURL& origin,
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();
119 // Put callbacks.
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,
123 int expected_bytes,
124 int rv);
125 static void PutDidWriteBlobToCache(scoped_ptr<PutContext> put_context,
126 scoped_ptr<BlobReader> blob_reader,
127 disk_cache::ScopedEntryPtr entry,
128 bool success);
130 // Static callbacks for the Keys function.
131 static void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context,
132 int rv);
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_;
150 GURL origin_;
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_;
155 bool initialized_;
156 std::vector<base::Closure> init_callbacks_;
158 // Whether or not to store data in disk or memory.
159 bool memory_only_;
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_