OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #include "content/browser/cache_storage/cache_storage_dispatcher_host.h" | 5 #include "content/browser/cache_storage/cache_storage_dispatcher_host.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/string16.h" | |
9 #include "base/strings/utf_string_conversions.h" | |
10 #include "content/browser/cache_storage/cache_storage_context_impl.h" | 8 #include "content/browser/cache_storage/cache_storage_context_impl.h" |
11 #include "content/browser/cache_storage/cache_storage_listener.h" | 9 #include "content/browser/cache_storage/cache_storage_listener.h" |
12 #include "content/browser/cache_storage/cache_storage_manager.h" | |
13 #include "content/common/cache_storage/cache_storage_messages.h" | 10 #include "content/common/cache_storage/cache_storage_messages.h" |
14 #include "content/public/browser/content_browser_client.h" | 11 #include "content/public/browser/content_browser_client.h" |
15 #include "storage/browser/blob/blob_data_handle.h" | |
16 #include "third_party/WebKit/public/platform/WebServiceWorkerCacheError.h" | |
17 | 12 |
18 namespace content { | 13 namespace content { |
19 | 14 |
20 namespace { | 15 namespace { |
21 | 16 |
22 const uint32 kFilteredMessageClasses[] = {CacheStorageMsgStart}; | 17 const uint32 kFilteredMessageClasses[] = {CacheStorageMsgStart}; |
23 | 18 |
24 blink::WebServiceWorkerCacheError ToWebServiceWorkerCacheError( | |
25 CacheStorage::CacheStorageError err) { | |
26 switch (err) { | |
27 case CacheStorage::CACHE_STORAGE_ERROR_NO_ERROR: | |
28 NOTREACHED(); | |
29 return blink::WebServiceWorkerCacheErrorNotImplemented; | |
30 case CacheStorage::CACHE_STORAGE_ERROR_NOT_IMPLEMENTED: | |
31 return blink::WebServiceWorkerCacheErrorNotImplemented; | |
32 case CacheStorage::CACHE_STORAGE_ERROR_NOT_FOUND: | |
33 return blink::WebServiceWorkerCacheErrorNotFound; | |
34 case CacheStorage::CACHE_STORAGE_ERROR_EXISTS: | |
35 return blink::WebServiceWorkerCacheErrorExists; | |
36 case CacheStorage::CACHE_STORAGE_ERROR_STORAGE: | |
37 // TODO(jkarlin): Change this to CACHE_STORAGE_ERROR_STORAGE once that's | |
38 // added. | |
39 return blink::WebServiceWorkerCacheErrorNotFound; | |
40 case CacheStorage::CACHE_STORAGE_ERROR_CLOSING: | |
41 // TODO(jkarlin): Update this to CACHE_STORAGE_ERROR_CLOSING once that's | |
42 // added. | |
43 return blink::WebServiceWorkerCacheErrorNotFound; | |
44 } | |
45 NOTREACHED(); | |
46 return blink::WebServiceWorkerCacheErrorNotImplemented; | |
47 } | |
48 | |
49 // TODO(jkarlin): CacheStorageCache and CacheStorage should share | |
50 // an error enum type. | |
51 blink::WebServiceWorkerCacheError CacheErrorToWebServiceWorkerCacheError( | |
52 CacheStorageCache::ErrorType err) { | |
53 switch (err) { | |
54 case CacheStorageCache::ERROR_TYPE_OK: | |
55 NOTREACHED(); | |
56 return blink::WebServiceWorkerCacheErrorNotImplemented; | |
57 case CacheStorageCache::ERROR_TYPE_EXISTS: | |
58 return blink::WebServiceWorkerCacheErrorExists; | |
59 case CacheStorageCache::ERROR_TYPE_STORAGE: | |
60 // TODO(jkarlin): Change this to CACHE_STORAGE_ERROR_STORAGE once that's | |
61 // added. | |
62 return blink::WebServiceWorkerCacheErrorNotFound; | |
63 case CacheStorageCache::ERROR_TYPE_NOT_FOUND: | |
64 return blink::WebServiceWorkerCacheErrorNotFound; | |
65 } | |
66 NOTREACHED(); | |
67 return blink::WebServiceWorkerCacheErrorNotImplemented; | |
68 } | |
69 | |
70 } // namespace | 19 } // namespace |
71 | 20 |
72 CacheStorageDispatcherHost::CacheStorageDispatcherHost() | 21 CacheStorageDispatcherHost::CacheStorageDispatcherHost() |
73 : BrowserMessageFilter(kFilteredMessageClasses, | 22 : BrowserMessageFilter(kFilteredMessageClasses, |
74 arraysize(kFilteredMessageClasses)) { | 23 arraysize(kFilteredMessageClasses)) { |
75 } | 24 } |
76 | 25 |
77 CacheStorageDispatcherHost::~CacheStorageDispatcherHost() { | 26 CacheStorageDispatcherHost::~CacheStorageDispatcherHost() { |
78 } | 27 } |
79 | 28 |
80 void CacheStorageDispatcherHost::Init(CacheStorageContextImpl* context) { | 29 void CacheStorageDispatcherHost::Init(CacheStorageContextImpl* context) { |
81 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 30 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
82 BrowserThread::PostTask( | 31 BrowserThread::PostTask( |
83 BrowserThread::IO, FROM_HERE, | 32 BrowserThread::IO, FROM_HERE, |
84 base::Bind(&CacheStorageDispatcherHost::CreateCacheListener, this, | 33 base::Bind(&CacheStorageDispatcherHost::CreateCacheListener, this, |
85 make_scoped_refptr(context))); | 34 make_scoped_refptr(context))); |
86 } | 35 } |
87 | 36 |
88 void CacheStorageDispatcherHost::OnDestruct() const { | 37 void CacheStorageDispatcherHost::OnDestruct() const { |
89 BrowserThread::DeleteOnIOThread::Destruct(this); | 38 BrowserThread::DeleteOnIOThread::Destruct(this); |
90 } | 39 } |
91 | 40 |
92 bool CacheStorageDispatcherHost::OnMessageReceived( | 41 bool CacheStorageDispatcherHost::OnMessageReceived( |
93 const IPC::Message& message) { | 42 const IPC::Message& message) { |
94 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 43 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
95 | |
96 bool handled = true; | |
97 IPC_BEGIN_MESSAGE_MAP(CacheStorageDispatcherHost, message) | |
98 IPC_MESSAGE_HANDLER(CacheStorageHostMsg_CacheStorageHas, OnCacheStorageHas) | |
99 IPC_MESSAGE_HANDLER(CacheStorageHostMsg_CacheStorageOpen, OnCacheStorageOpen) | |
100 IPC_MESSAGE_HANDLER(CacheStorageHostMsg_CacheStorageDelete, | |
101 OnCacheStorageDelete) | |
102 IPC_MESSAGE_HANDLER(CacheStorageHostMsg_CacheStorageKeys, OnCacheStorageKeys) | |
103 IPC_MESSAGE_HANDLER(CacheStorageHostMsg_CacheStorageMatch, | |
104 OnCacheStorageMatch) | |
105 IPC_MESSAGE_UNHANDLED(handled = false) | |
106 IPC_END_MESSAGE_MAP() | |
107 if (handled) | |
108 return handled; | |
109 | |
110 DCHECK(cache_listener_); | 44 DCHECK(cache_listener_); |
111 cache_listener_->OnMessageReceived(message); | 45 bool handled = cache_listener_->OnMessageReceived(message); |
112 if (!handled) | 46 if (!handled) |
113 BadMessageReceived(); | 47 BadMessageReceived(); |
114 return handled; | 48 return handled; |
115 } | 49 } |
116 | 50 |
117 void CacheStorageDispatcherHost::CreateCacheListener( | 51 void CacheStorageDispatcherHost::CreateCacheListener( |
118 CacheStorageContextImpl* context) { | 52 CacheStorageContextImpl* context) { |
119 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 53 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
120 cache_listener_.reset(new CacheStorageListener(this, context)); | 54 cache_listener_.reset(new CacheStorageListener(this, context)); |
121 context_ = context; | |
122 } | |
123 | |
124 void CacheStorageDispatcherHost::OnCacheStorageHas( | |
125 int thread_id, | |
126 int request_id, | |
127 const GURL& origin, | |
128 const base::string16& cache_name) { | |
129 TRACE_EVENT0("CacheStorage", "CacheStorageDispatcherHost::OnCacheStorageHas"); | |
130 context_->cache_manager()->HasCache( | |
131 origin, base::UTF16ToUTF8(cache_name), | |
132 base::Bind(&CacheStorageDispatcherHost::OnCacheStorageHasCallback, this, | |
133 thread_id, request_id)); | |
134 } | |
135 | |
136 void CacheStorageDispatcherHost::OnCacheStorageOpen( | |
137 int thread_id, | |
138 int request_id, | |
139 const GURL& origin, | |
140 const base::string16& cache_name) { | |
141 TRACE_EVENT0("CacheStorage", | |
142 "CacheStorageDispatcherHost::OnCacheStorageOpen"); | |
143 context_->cache_manager()->OpenCache( | |
144 origin, base::UTF16ToUTF8(cache_name), | |
145 base::Bind(&CacheStorageDispatcherHost::OnCacheStorageOpenCallback, this, | |
146 thread_id, request_id)); | |
147 } | |
148 | |
149 void CacheStorageDispatcherHost::OnCacheStorageDelete( | |
150 int thread_id, | |
151 int request_id, | |
152 const GURL& origin, | |
153 const base::string16& cache_name) { | |
154 TRACE_EVENT0("CacheStorage", | |
155 "CacheStorageDispatcherHost::OnCacheStorageDelete"); | |
156 context_->cache_manager()->DeleteCache( | |
157 origin, base::UTF16ToUTF8(cache_name), | |
158 base::Bind(&CacheStorageDispatcherHost::OnCacheStorageDeleteCallback, | |
159 this, thread_id, request_id)); | |
160 } | |
161 | |
162 void CacheStorageDispatcherHost::OnCacheStorageKeys(int thread_id, | |
163 int request_id, | |
164 const GURL& origin) { | |
165 TRACE_EVENT0("CacheStorage", | |
166 "CacheStorageDispatcherHost::OnCacheStorageKeys"); | |
167 context_->cache_manager()->EnumerateCaches( | |
168 origin, | |
169 base::Bind(&CacheStorageDispatcherHost::OnCacheStorageKeysCallback, this, | |
170 thread_id, request_id)); | |
171 } | |
172 | |
173 void CacheStorageDispatcherHost::OnCacheStorageMatch( | |
174 int thread_id, | |
175 int request_id, | |
176 const GURL& origin, | |
177 const ServiceWorkerFetchRequest& request, | |
178 const CacheStorageCacheQueryParams& match_params) { | |
179 TRACE_EVENT0("CacheStorage", | |
180 "CacheStorageDispatcherHost::OnCacheStorageMatch"); | |
181 | |
182 scoped_ptr<ServiceWorkerFetchRequest> scoped_request( | |
183 new ServiceWorkerFetchRequest(request.url, request.method, | |
184 request.headers, request.referrer, | |
185 request.is_reload)); | |
186 | |
187 if (match_params.cache_name.empty()) { | |
188 context_->cache_manager()->MatchAllCaches( | |
189 origin, scoped_request.Pass(), | |
190 base::Bind(&CacheStorageDispatcherHost::OnCacheStorageMatchCallback, | |
191 this, thread_id, request_id)); | |
192 return; | |
193 } | |
194 context_->cache_manager()->MatchCache( | |
195 origin, base::UTF16ToUTF8(match_params.cache_name), scoped_request.Pass(), | |
196 base::Bind(&CacheStorageDispatcherHost::OnCacheStorageMatchCallback, this, | |
197 thread_id, request_id)); | |
198 } | |
199 | |
200 void CacheStorageDispatcherHost::OnCacheStorageHasCallback( | |
201 int thread_id, | |
202 int request_id, | |
203 bool has_cache, | |
204 CacheStorage::CacheStorageError error) { | |
205 if (error != CacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) { | |
206 Send(new CacheStorageMsg_CacheStorageHasError( | |
207 thread_id, request_id, ToWebServiceWorkerCacheError(error))); | |
208 return; | |
209 } | |
210 if (!has_cache) { | |
211 Send(new CacheStorageMsg_CacheStorageHasError( | |
212 thread_id, request_id, blink::WebServiceWorkerCacheErrorNotFound)); | |
213 return; | |
214 } | |
215 Send(new CacheStorageMsg_CacheStorageHasSuccess(thread_id, request_id)); | |
216 } | |
217 | |
218 void CacheStorageDispatcherHost::OnCacheStorageOpenCallback( | |
219 int thread_id, | |
220 int request_id, | |
221 const scoped_refptr<CacheStorageCache>& cache, | |
222 CacheStorage::CacheStorageError error) { | |
223 if (error != CacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) { | |
224 Send(new CacheStorageMsg_CacheStorageOpenError( | |
225 thread_id, request_id, ToWebServiceWorkerCacheError(error))); | |
226 return; | |
227 } | |
228 CacheStorageListener::CacheID cache_id = | |
229 cache_listener_->StoreCacheReference(cache); | |
230 Send(new CacheStorageMsg_CacheStorageOpenSuccess(thread_id, request_id, | |
231 cache_id)); | |
232 } | |
233 | |
234 void CacheStorageDispatcherHost::OnCacheStorageDeleteCallback( | |
235 int thread_id, | |
236 int request_id, | |
237 bool deleted, | |
238 CacheStorage::CacheStorageError error) { | |
239 if (!deleted || error != CacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) { | |
240 Send(new CacheStorageMsg_CacheStorageDeleteError( | |
241 thread_id, request_id, ToWebServiceWorkerCacheError(error))); | |
242 return; | |
243 } | |
244 Send(new CacheStorageMsg_CacheStorageDeleteSuccess(thread_id, request_id)); | |
245 } | |
246 | |
247 void CacheStorageDispatcherHost::OnCacheStorageKeysCallback( | |
248 int thread_id, | |
249 int request_id, | |
250 const std::vector<std::string>& strings, | |
251 CacheStorage::CacheStorageError error) { | |
252 if (error != CacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) { | |
253 Send(new CacheStorageMsg_CacheStorageKeysError( | |
254 thread_id, request_id, ToWebServiceWorkerCacheError(error))); | |
255 return; | |
256 } | |
257 | |
258 std::vector<base::string16> string16s; | |
259 for (size_t i = 0, max = strings.size(); i < max; ++i) { | |
260 string16s.push_back(base::UTF8ToUTF16(strings[i])); | |
261 } | |
262 Send(new CacheStorageMsg_CacheStorageKeysSuccess(thread_id, request_id, | |
263 string16s)); | |
264 } | |
265 | |
266 void CacheStorageDispatcherHost::OnCacheStorageMatchCallback( | |
267 int thread_id, | |
268 int request_id, | |
269 CacheStorageCache::ErrorType error, | |
270 scoped_ptr<ServiceWorkerResponse> response, | |
271 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { | |
272 if (error != CacheStorageCache::ERROR_TYPE_OK) { | |
273 Send(new CacheStorageMsg_CacheStorageMatchError( | |
274 thread_id, request_id, CacheErrorToWebServiceWorkerCacheError(error))); | |
275 return; | |
276 } | |
277 | |
278 if (blob_data_handle) | |
279 cache_listener_->StoreBlobDataHandle(blob_data_handle.Pass()); | |
280 | |
281 Send(new CacheStorageMsg_CacheStorageMatchSuccess(thread_id, request_id, | |
282 *response)); | |
283 } | 55 } |
284 | 56 |
285 } // namespace content | 57 } // namespace content |
OLD | NEW |