Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / cache_storage / cache_storage_cache.h
blobbeb7fdd0cc1847c64fbbd56addb7f0bd2053d3ab
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_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_
6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_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/cache_storage/cache_storage_types.h"
15 #include "content/common/service_worker/service_worker_types.h"
16 #include "net/base/completion_callback.h"
17 #include "net/disk_cache/disk_cache.h"
19 namespace net {
20 class URLRequestContext;
21 class IOBufferWithSize;
24 namespace storage {
25 class BlobDataBuilder;
26 class BlobDataHandle;
27 class BlobStorageContext;
28 class QuotaManagerProxy;
31 namespace content {
32 class ChromeBlobStorageContext;
33 class CacheMetadata;
34 class CacheStorageScheduler;
35 class TestCacheStorageCache;
37 // Represents a ServiceWorker Cache as seen in
38 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/
39 // The asynchronous methods are executed serially. Callbacks to the
40 // public functions will be called so long as the cache object lives.
41 class CONTENT_EXPORT CacheStorageCache
42 : public base::RefCounted<CacheStorageCache> {
43 public:
44 // This enum is used in histograms, so do not change the ordering and always
45 // append new types to the end.
46 enum ErrorType {
47 ERROR_TYPE_OK = 0,
48 ERROR_TYPE_EXISTS,
49 ERROR_TYPE_STORAGE,
50 ERROR_TYPE_NOT_FOUND,
51 ERROR_TYPE_LAST = ERROR_TYPE_NOT_FOUND
54 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY };
55 typedef base::Callback<void(ErrorType)> ErrorCallback;
56 typedef base::Callback<void(ErrorType,
57 scoped_ptr<ServiceWorkerResponse>,
58 scoped_ptr<storage::BlobDataHandle>)>
59 ResponseCallback;
60 typedef std::vector<ServiceWorkerFetchRequest> Requests;
61 typedef base::Callback<void(ErrorType, scoped_ptr<Requests>)>
62 RequestsCallback;
64 static scoped_refptr<CacheStorageCache> CreateMemoryCache(
65 const GURL& origin,
66 net::URLRequestContext* request_context,
67 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
68 base::WeakPtr<storage::BlobStorageContext> blob_context);
69 static scoped_refptr<CacheStorageCache> CreatePersistentCache(
70 const GURL& origin,
71 const base::FilePath& path,
72 net::URLRequestContext* request_context,
73 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
74 base::WeakPtr<storage::BlobStorageContext> blob_context);
76 // Returns ERROR_TYPE_NOT_FOUND if not found.
77 void Match(scoped_ptr<ServiceWorkerFetchRequest> request,
78 const ResponseCallback& callback);
80 // Puts the request and response object in the cache. The response body (if
81 // present) is stored in the cache, but not the request body. Returns
82 // ERROR_TYPE_OK on success.
83 void Put(scoped_ptr<ServiceWorkerFetchRequest> request,
84 scoped_ptr<ServiceWorkerResponse> response,
85 const ResponseCallback& callback);
87 // Returns ErrorNotFound if not found. Otherwise deletes and returns
88 // ERROR_TYPE_OK.
89 void Delete(scoped_ptr<ServiceWorkerFetchRequest> request,
90 const ErrorCallback& callback);
92 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest.
93 // Returns ErrorTypeOK and a vector of requests if there are no errors.
94 void Keys(const RequestsCallback& callback);
96 // Closes the backend. Future operations that require the backend
97 // will exit early. Close should only be called once per CacheStorageCache.
98 void Close(const base::Closure& callback);
100 // The size of the cache contents in memory. Returns 0 if the cache backend is
101 // not a memory cache backend.
102 int64 MemoryBackedSize() const;
104 base::WeakPtr<CacheStorageCache> AsWeakPtr();
106 private:
107 friend class base::RefCounted<CacheStorageCache>;
108 friend class TestCacheStorageCache;
110 class BlobReader;
111 struct KeysContext;
112 struct MatchContext;
113 struct PutContext;
115 // The backend progresses from uninitialized, to open, to closed, and cannot
116 // reverse direction. The open step may be skipped.
117 enum BackendState {
118 BACKEND_UNINITIALIZED, // No backend, create backend on first operation.
119 BACKEND_OPEN, // Backend can be used.
120 BACKEND_CLOSED // Backend cannot be used. All ops should fail.
123 typedef std::vector<disk_cache::Entry*> Entries;
124 typedef scoped_ptr<disk_cache::Backend> ScopedBackendPtr;
126 CacheStorageCache(
127 const GURL& origin,
128 const base::FilePath& path,
129 net::URLRequestContext* request_context,
130 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
131 base::WeakPtr<storage::BlobStorageContext> blob_context);
133 // Async operations in progress will cancel and not run their callbacks.
134 virtual ~CacheStorageCache();
136 // Match callbacks
137 void MatchImpl(scoped_ptr<ServiceWorkerFetchRequest> request,
138 const ResponseCallback& callback);
139 void MatchDidOpenEntry(scoped_ptr<MatchContext> match_context, int rv);
140 void MatchDidReadMetadata(scoped_ptr<MatchContext> match_context,
141 scoped_ptr<CacheMetadata> headers);
142 void MatchDidReadResponseBodyData(scoped_ptr<MatchContext> match_context,
143 int rv);
144 void MatchDoneWithBody(scoped_ptr<MatchContext> match_context);
146 // Put callbacks.
147 void PutImpl(scoped_ptr<PutContext> put_context);
148 void PutDidDelete(scoped_ptr<PutContext> put_context, ErrorType delete_error);
149 void PutDidCreateEntry(scoped_ptr<PutContext> put_context, int rv);
150 void PutDidWriteHeaders(scoped_ptr<PutContext> put_context,
151 int expected_bytes,
152 int rv);
153 void PutDidWriteBlobToCache(scoped_ptr<PutContext> put_context,
154 scoped_ptr<BlobReader> blob_reader,
155 disk_cache::ScopedEntryPtr entry,
156 bool success);
158 // Delete callbacks
159 void DeleteImpl(scoped_ptr<ServiceWorkerFetchRequest> request,
160 const ErrorCallback& callback);
161 void DeleteDidOpenEntry(
162 const GURL& origin,
163 scoped_ptr<ServiceWorkerFetchRequest> request,
164 const CacheStorageCache::ErrorCallback& callback,
165 scoped_ptr<disk_cache::Entry*> entryptr,
166 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
167 int rv);
169 // Keys callbacks.
170 void KeysImpl(const RequestsCallback& callback);
171 void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context, int rv);
172 void KeysProcessNextEntry(scoped_ptr<KeysContext> keys_context,
173 const Entries::iterator& iter);
174 void KeysDidReadMetadata(scoped_ptr<KeysContext> keys_context,
175 const Entries::iterator& iter,
176 scoped_ptr<CacheMetadata> metadata);
178 void CloseImpl(const base::Closure& callback);
180 // Loads the backend and calls the callback with the result (true for
181 // success). The callback will always be called. Virtual for tests.
182 virtual void CreateBackend(const ErrorCallback& callback);
183 void CreateBackendDidCreate(const CacheStorageCache::ErrorCallback& callback,
184 scoped_ptr<ScopedBackendPtr> backend_ptr,
185 int rv);
187 void InitBackend();
188 void InitDone(ErrorType error);
190 void PendingClosure(const base::Closure& callback);
191 void PendingErrorCallback(const ErrorCallback& callback, ErrorType error);
192 void PendingResponseCallback(
193 const ResponseCallback& callback,
194 ErrorType error,
195 scoped_ptr<ServiceWorkerResponse> response,
196 scoped_ptr<storage::BlobDataHandle> blob_data_handle);
197 void PendingRequestsCallback(const RequestsCallback& callback,
198 ErrorType error,
199 scoped_ptr<Requests> requests);
201 // Be sure to check |backend_state_| before use.
202 scoped_ptr<disk_cache::Backend> backend_;
204 GURL origin_;
205 base::FilePath path_;
206 net::URLRequestContext* request_context_;
207 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_;
208 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_;
209 BackendState backend_state_;
210 scoped_ptr<CacheStorageScheduler> scheduler_;
211 bool initializing_;
213 // Whether or not to store data in disk or memory.
214 bool memory_only_;
216 base::WeakPtrFactory<CacheStorageCache> weak_ptr_factory_;
218 DISALLOW_COPY_AND_ASSIGN(CacheStorageCache);
221 } // namespace content
223 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_