OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ |
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
11 #include "content/common/service_worker/service_worker_types.h" | |
12 #include "net/base/completion_callback.h" | |
13 #include "net/disk_cache/disk_cache.h" | |
11 | 14 |
12 namespace net { | 15 namespace net { |
13 class URLRequestContext; | 16 class URLRequestContext; |
17 class IOBufferWithSize; | |
14 } | 18 } |
15 | 19 |
16 namespace webkit_blob { | 20 namespace webkit_blob { |
21 class BlobData; | |
22 class BlobDataHandle; | |
17 class BlobStorageContext; | 23 class BlobStorageContext; |
18 } | 24 } |
19 | 25 |
20 namespace content { | 26 namespace content { |
27 class ChromeBlobStorageContext; | |
21 | 28 |
22 // TODO(jkarlin): Fill this in with a real Cache implementation as | |
23 // specified in | |
24 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html. | |
25 // TODO(jkarlin): Unload cache backend from memory once the cache object is no | 29 // TODO(jkarlin): Unload cache backend from memory once the cache object is no |
26 // longer referenced in javascript. | 30 // longer referenced in javascript. |
27 | 31 |
28 // Represents a ServiceWorker Cache as seen in | 32 // Represents a ServiceWorker Cache as seen in |
29 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html. | 33 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html. |
30 // InitializeIfNeeded must be called before calling the other public members. | 34 // InitializeIfNeeded must be called before calling the other public members. |
31 class ServiceWorkerCache { | 35 class CONTENT_EXPORT ServiceWorkerCache { |
32 public: | 36 public: |
37 enum ErrorType { | |
38 ErrorTypeOK = 0, | |
39 ErrorTypeExists, | |
40 ErrorTypeStorage, | |
41 ErrorTypeNotFound | |
42 }; | |
43 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; | |
44 typedef base::Callback<void(ErrorType)> ErrorCallback; | |
45 typedef base::Callback<void(bool)> BoolCallback; | |
46 typedef base::Callback<void(disk_cache::ScopedEntryPtr, bool)> | |
47 EntryBoolCallback; | |
48 typedef base::Callback<void(ErrorType, | |
49 scoped_ptr<ServiceWorkerResponse>, | |
50 scoped_ptr<webkit_blob::BlobDataHandle>)> | |
51 ResponseCallback; | |
52 class BlobReader; | |
53 | |
33 static scoped_ptr<ServiceWorkerCache> CreateMemoryCache( | 54 static scoped_ptr<ServiceWorkerCache> CreateMemoryCache( |
34 const std::string& name, | 55 const std::string& name, |
35 net::URLRequestContext* request_context, | 56 net::URLRequestContext* request_context, |
36 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); | 57 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); |
37 static scoped_ptr<ServiceWorkerCache> CreatePersistentCache( | 58 static scoped_ptr<ServiceWorkerCache> CreatePersistentCache( |
38 const base::FilePath& path, | 59 const base::FilePath& path, |
39 const std::string& name, | 60 const std::string& name, |
40 net::URLRequestContext* request_context, | 61 net::URLRequestContext* request_context, |
41 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); | 62 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); |
42 | 63 |
43 virtual ~ServiceWorkerCache(); | 64 virtual ~ServiceWorkerCache(); |
44 | 65 |
45 // Loads the backend and calls the callback with the result (true for | 66 // Loads the backend and calls the callback with the result (true for |
46 // success). This must be called before member functions that require a | 67 // success). This must be called before member functions that require a |
47 // backend are called. | 68 // backend are called. |
48 void CreateBackend(const base::Callback<void(bool)>& callback); | 69 void CreateBackend(const ErrorCallback& callback); |
70 | |
71 // Returns ErrorTypeNotFound if not found. | |
72 void Match(ServiceWorkerFetchRequest* request, | |
73 const ResponseCallback& callback); | |
74 | |
75 // Puts the request and response object in the cache. The response body (if | |
76 // present) is stored in the cache, but not the request body. Returns | |
77 // ErrorTypeOK on success. | |
78 void Put(ServiceWorkerFetchRequest* request, | |
79 ServiceWorkerResponse* response, | |
80 const ErrorCallback& callback); | |
81 | |
82 // Returns ErrorNotFound if not found. Otherwise deletes and returns | |
83 // ErrorTypeOK. | |
84 void Delete(ServiceWorkerFetchRequest* request, | |
85 const ErrorCallback& callback); | |
86 | |
87 // Call to determine if CreateBackend must be called. | |
88 bool HasCreatedBackend() const; | |
49 | 89 |
50 void set_name(const std::string& name) { name_ = name; } | 90 void set_name(const std::string& name) { name_ = name; } |
51 const std::string& name() const { return name_; } | 91 const std::string& name() const { return name_; } |
52 int32 id() const { return id_; } | 92 int32 id() const { return id_; } |
53 void set_id(int32 id) { id_ = id; } | 93 void set_id(int32 id) { id_ = id; } |
54 | 94 |
55 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); | 95 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); |
56 | 96 |
57 private: | 97 private: |
98 typedef scoped_ptr<disk_cache::Backend> ScopedBackendPtr; | |
99 | |
100 struct ResponseReadContext; | |
101 | |
58 ServiceWorkerCache( | 102 ServiceWorkerCache( |
59 const base::FilePath& path, | 103 const base::FilePath& path, |
60 const std::string& name, | 104 const std::string& name, |
61 net::URLRequestContext* request_context, | 105 net::URLRequestContext* request_context, |
62 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); | 106 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); |
63 | 107 |
108 // CreateBackend callbacks | |
109 void CreateBackendDidCreate(const ErrorCallback& callback, | |
110 scoped_ptr<ScopedBackendPtr> backend_ptr, | |
111 int rv); | |
112 | |
113 // Put callbacks | |
114 void PutDidCreateEntry( | |
115 ServiceWorkerFetchRequest* request, | |
116 ServiceWorkerResponse* response, | |
117 const ErrorCallback& callback, | |
118 scoped_ptr<disk_cache::Entry*> entry, | |
119 scoped_ptr<webkit_blob::BlobDataHandle> blob_data_handle, | |
120 int error); | |
121 void PutDidWriteHeaders( | |
122 ServiceWorkerResponse* response, | |
123 const ErrorCallback& callback, | |
124 disk_cache::ScopedEntryPtr entry, | |
125 scoped_ptr<webkit_blob::BlobDataHandle> blob_data_handle, | |
126 int expected_bytes, | |
127 int rv); | |
128 void PutDidWriteBlobToCache(const ErrorCallback& callback, | |
129 scoped_ptr<BlobReader> blob_reader, | |
130 disk_cache::ScopedEntryPtr entry, | |
131 bool success); | |
132 | |
133 // Match callbacks | |
134 void MatchDidOpenEntry(ServiceWorkerFetchRequest* request, | |
135 const ResponseCallback& callback, | |
136 scoped_ptr<disk_cache::Entry*> entryptr, | |
137 int rv); | |
138 void MatchDidReadHeaderData( | |
139 ServiceWorkerFetchRequest* request, | |
140 const ResponseCallback& callback, | |
141 disk_cache::ScopedEntryPtr entry, | |
142 const scoped_refptr<net::IOBufferWithSize>& buffer, | |
143 int rv); | |
144 void MatchDidReadResponseBodyData( | |
145 ServiceWorkerFetchRequest* request, | |
146 const ResponseCallback& callback, | |
147 disk_cache::ScopedEntryPtr entry, | |
148 scoped_ptr<ServiceWorkerResponse> response, | |
149 scoped_ptr<ResponseReadContext> response_context, | |
150 int rv); | |
151 void MatchDoneWithBody(ServiceWorkerFetchRequest* request, | |
152 const ResponseCallback& callback, | |
153 scoped_ptr<ServiceWorkerResponse> response, | |
154 scoped_ptr<ResponseReadContext> response_context); | |
155 | |
156 // Delete callbacks | |
157 void DeleteDidOpenEntry(ServiceWorkerFetchRequest* request, | |
michaeln
2014/08/21 01:46:15
All of these DidSomething() async completion callb
jkarlin
2014/08/21 18:31:21
I've reworked ServiceWorkerCache so that its publi
| |
158 const ErrorCallback& callback, | |
159 scoped_ptr<disk_cache::Entry*> entryptr, | |
160 int rv); | |
161 | |
162 scoped_ptr<disk_cache::Backend> backend_; | |
64 base::FilePath path_; | 163 base::FilePath path_; |
65 std::string name_; | 164 std::string name_; |
66 net::URLRequestContext* request_context_; | 165 net::URLRequestContext* request_context_; |
67 base::WeakPtr<webkit_blob::BlobStorageContext> blob_storage_context_; | 166 base::WeakPtr<webkit_blob::BlobStorageContext> blob_storage_context_; |
68 int32 id_; | 167 int32 id_; |
69 | 168 |
70 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; | 169 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; |
71 | 170 |
72 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); | 171 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); |
73 }; | 172 }; |
74 | 173 |
75 } // namespace content | 174 } // namespace content |
76 | 175 |
77 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | 176 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ |
OLD | NEW |