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 #include "config.h" | 5 #include "config.h" |
6 #include "modules/cachestorage/InspectorCacheStorageAgent.h" | 6 #include "modules/cachestorage/InspectorCacheStorageAgent.h" |
7 | 7 |
8 #include "core/InspectorBackendDispatcher.h" | 8 #include "core/InspectorBackendDispatcher.h" |
9 #include "core/InspectorTypeBuilder.h" | 9 #include "core/InspectorTypeBuilder.h" |
10 #include "modules/serviceworkers/ServiceWorkerGlobalScope.h" | |
11 #include "platform/JSONValues.h" | 10 #include "platform/JSONValues.h" |
12 #include "platform/heap/Handle.h" | 11 #include "platform/heap/Handle.h" |
13 #include "platform/weborigin/DatabaseIdentifier.h" | 12 #include "platform/weborigin/DatabaseIdentifier.h" |
13 #include "platform/weborigin/SecurityOrigin.h" | |
14 #include "public/platform/Platform.h" | 14 #include "public/platform/Platform.h" |
15 #include "public/platform/WebServiceWorkerCache.h" | 15 #include "public/platform/WebServiceWorkerCache.h" |
16 #include "public/platform/WebServiceWorkerCacheError.h" | 16 #include "public/platform/WebServiceWorkerCacheError.h" |
17 #include "public/platform/WebServiceWorkerCacheStorage.h" | 17 #include "public/platform/WebServiceWorkerCacheStorage.h" |
18 #include "public/platform/WebServiceWorkerRequest.h" | 18 #include "public/platform/WebServiceWorkerRequest.h" |
19 #include "public/platform/WebServiceWorkerResponse.h" | 19 #include "public/platform/WebServiceWorkerResponse.h" |
20 #include "public/platform/WebString.h" | 20 #include "public/platform/WebString.h" |
21 #include "public/platform/WebURL.h" | 21 #include "public/platform/WebURL.h" |
22 #include "public/platform/WebVector.h" | 22 #include "public/platform/WebVector.h" |
23 #include "wtf/Noncopyable.h" | 23 #include "wtf/Noncopyable.h" |
24 #include "wtf/OwnPtr.h" | 24 #include "wtf/OwnPtr.h" |
25 #include "wtf/PassRefPtr.h" | 25 #include "wtf/PassRefPtr.h" |
26 #include "wtf/RefCounted.h" | 26 #include "wtf/RefCounted.h" |
27 #include "wtf/RefPtr.h" | 27 #include "wtf/RefPtr.h" |
28 #include "wtf/Vector.h" | 28 #include "wtf/Vector.h" |
29 #include "wtf/text/StringBuilder.h" | 29 #include "wtf/text/StringBuilder.h" |
30 | 30 |
31 #include <algorithm> | 31 #include <algorithm> |
32 | 32 |
33 using blink::TypeBuilder::Array; | 33 using blink::TypeBuilder::Array; |
34 using blink::TypeBuilder::ServiceWorkerCache::DataEntry; | 34 using blink::TypeBuilder::CacheStorage::Cache; |
35 using blink::TypeBuilder::CacheStorage::DataEntry; | |
35 | 36 |
36 typedef blink::InspectorBackendDispatcher::ServiceWorkerCacheCommandHandler::Del eteCacheCallback DeleteCacheCallback; | 37 typedef blink::InspectorBackendDispatcher::CacheStorageCommandHandler::DeleteCac heCallback DeleteCacheCallback; |
37 typedef blink::InspectorBackendDispatcher::ServiceWorkerCacheCommandHandler::Req uestCacheNamesCallback RequestCacheNamesCallback; | 38 typedef blink::InspectorBackendDispatcher::CacheStorageCommandHandler::RequestCa cheNamesCallback RequestCacheNamesCallback; |
38 typedef blink::InspectorBackendDispatcher::ServiceWorkerCacheCommandHandler::Req uestEntriesCallback RequestEntriesCallback; | 39 typedef blink::InspectorBackendDispatcher::CacheStorageCommandHandler::RequestEn triesCallback RequestEntriesCallback; |
39 typedef blink::InspectorBackendDispatcher::CallbackBase RequestCallback; | 40 typedef blink::InspectorBackendDispatcher::CallbackBase RequestCallback; |
40 | 41 |
41 namespace blink { | 42 namespace blink { |
42 | 43 |
43 namespace { | 44 namespace { |
44 | 45 |
45 PassOwnPtr<WebServiceWorkerCacheStorage> assertCacheStorage(ErrorString* errorSt ring, ServiceWorkerGlobalScope* globalScope) | 46 String composeCacheId(const String& securityOrigin, const String& cacheName) |
pfeldman
2015/04/20 18:14:31
super nit: we typically use build* and parse*, but
dmurph
2015/04/20 20:58:17
Done.
| |
46 { | 47 { |
47 String identifier = createDatabaseIdentifierFromSecurityOrigin(globalScope-> securityOrigin()); | 48 String id(securityOrigin); |
48 OwnPtr<WebServiceWorkerCacheStorage> caches = adoptPtr(Platform::current()-> cacheStorage(identifier)); | 49 id.append("|"); |
49 if (!caches) { | 50 id.append(cacheName); |
50 *errorString = "Cache Storage not available in global scope."; | 51 return id; |
51 return nullptr; | 52 } |
53 | |
54 bool decomposeCacheId(ErrorString* errorString, const String& id, String* securi tyOrigin, String* cacheName) | |
55 { | |
56 size_t pipe = id.find('|'); | |
57 if (pipe == WTF::kNotFound) { | |
58 *errorString = "Cannot find the | character to decompose the cache id"; | |
pfeldman
2015/04/20 18:14:31
nit: you can just say that this was a poor id, no
dmurph
2015/04/20 20:58:17
Done.
| |
59 return false; | |
52 } | 60 } |
53 return caches.release(); | 61 *securityOrigin = id.substring(0, pipe); |
62 *cacheName = id.substring(pipe + 1); | |
63 return true; | |
64 } | |
65 | |
66 PassOwnPtr<WebServiceWorkerCacheStorage> assertCacheStorage(ErrorString* errorSt ring, const String& securityOrigin) | |
67 { | |
68 RefPtr<SecurityOrigin> secOrigin = SecurityOrigin::createFromString(security Origin); | |
69 String identifier = createDatabaseIdentifierFromSecurityOrigin(secOrigin.get ()); | |
70 WebServiceWorkerCacheStorage* cache = Platform::current()->cacheStorage(iden tifier); | |
pfeldman
2015/04/20 18:14:31
Adopt right away, you'll still be able to check fo
dmurph
2015/04/20 20:58:17
Done.
| |
71 if (!cache) | |
72 *errorString = "Could not find cache storage."; | |
73 return adoptPtr(cache); | |
54 } | 74 } |
55 | 75 |
56 CString serviceWorkerCacheErrorString(WebServiceWorkerCacheError* error) | 76 CString serviceWorkerCacheErrorString(WebServiceWorkerCacheError* error) |
57 { | 77 { |
58 switch (*error) { | 78 switch (*error) { |
59 case WebServiceWorkerCacheErrorNotImplemented: | 79 case WebServiceWorkerCacheErrorNotImplemented: |
60 return CString("not implemented."); | 80 return CString("not implemented."); |
61 break; | 81 break; |
62 case WebServiceWorkerCacheErrorNotFound: | 82 case WebServiceWorkerCacheErrorNotFound: |
63 return CString("not found."); | 83 return CString("not found."); |
64 break; | 84 break; |
65 case WebServiceWorkerCacheErrorExists: | 85 case WebServiceWorkerCacheErrorExists: |
66 return CString("cache already exists."); | 86 return CString("cache already exists."); |
67 break; | 87 break; |
68 default: | 88 default: |
69 return CString("unknown error."); | 89 return CString("unknown error."); |
70 break; | 90 break; |
71 } | 91 } |
72 } | 92 } |
73 | 93 |
74 class RequestCacheNames | 94 class RequestCacheNames |
75 : public WebServiceWorkerCacheStorage::CacheStorageKeysCallbacks { | 95 : public WebServiceWorkerCacheStorage::CacheStorageKeysCallbacks { |
76 WTF_MAKE_NONCOPYABLE(RequestCacheNames); | 96 WTF_MAKE_NONCOPYABLE(RequestCacheNames); |
77 | 97 |
78 public: | 98 public: |
79 RequestCacheNames(PassRefPtrWillBeRawPtr<RequestCacheNamesCallback> callback ) | 99 RequestCacheNames(const String& securityOrigin, PassRefPtrWillBeRawPtr<Reque stCacheNamesCallback> callback) |
80 : m_callback(callback) | 100 : m_securityOrigin(securityOrigin) |
101 , m_callback(callback) | |
81 { | 102 { |
82 } | 103 } |
83 | 104 |
84 virtual ~RequestCacheNames() { } | 105 virtual ~RequestCacheNames() { } |
85 | 106 |
86 void onSuccess(WebVector<WebString>* caches) | 107 void onSuccess(WebVector<WebString>* caches) |
87 { | 108 { |
88 RefPtr<TypeBuilder::Array<String>> array = TypeBuilder::Array<String>::c reate(); | 109 RefPtr<Array<Cache>> array = Array<Cache>::create(); |
89 for (size_t i = 0; i < caches->size(); i++) { | 110 for (size_t i = 0; i < caches->size(); i++) { |
90 array->addItem(String((*caches)[i])); | 111 String name = String((*caches)[i]); |
112 RefPtr<Cache> entry = Cache::create() | |
113 .setSecurityOrigin(m_securityOrigin) | |
114 .setCacheName(name) | |
115 .setCacheId(composeCacheId(m_securityOrigin, name)); | |
116 array->addItem(entry); | |
91 } | 117 } |
92 m_callback->sendSuccess(array); | 118 m_callback->sendSuccess(array); |
93 } | 119 } |
94 | 120 |
95 void onError(WebServiceWorkerCacheError* error) | 121 void onError(WebServiceWorkerCacheError* error) |
96 { | 122 { |
97 m_callback->sendFailure(String::format("Error requesting cache names: %s ", serviceWorkerCacheErrorString(error).data())); | 123 m_callback->sendFailure(String::format("Error requesting cache names: %s ", serviceWorkerCacheErrorString(error).data())); |
98 } | 124 } |
99 | 125 |
100 private: | 126 private: |
127 String m_securityOrigin; | |
101 RefPtrWillBePersistent<RequestCacheNamesCallback> m_callback; | 128 RefPtrWillBePersistent<RequestCacheNamesCallback> m_callback; |
102 }; | 129 }; |
103 | 130 |
104 struct DataRequestParams { | 131 struct DataRequestParams { |
105 String cacheName; | 132 String cacheName; |
106 int skipCount; | 133 int skipCount; |
107 int pageSize; | 134 int pageSize; |
108 }; | 135 }; |
109 | 136 |
110 struct RequestResponse { | 137 struct RequestResponse { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 { | 172 { |
146 return WTF::codePointCompareLessThan(a.request, b.request); | 173 return WTF::codePointCompareLessThan(a.request, b.request); |
147 }); | 174 }); |
148 if (m_params.skipCount > 0) | 175 if (m_params.skipCount > 0) |
149 m_responses.remove(0, m_params.skipCount); | 176 m_responses.remove(0, m_params.skipCount); |
150 bool hasMore = false; | 177 bool hasMore = false; |
151 if (static_cast<size_t>(m_params.pageSize) < m_responses.size()) { | 178 if (static_cast<size_t>(m_params.pageSize) < m_responses.size()) { |
152 m_responses.remove(m_params.pageSize, m_responses.size() - m_params. pageSize); | 179 m_responses.remove(m_params.pageSize, m_responses.size() - m_params. pageSize); |
153 hasMore = true; | 180 hasMore = true; |
154 } | 181 } |
155 RefPtr<TypeBuilder::Array<DataEntry>> array = TypeBuilder::Array<DataEnt ry>::create(); | 182 RefPtr<Array<DataEntry>> array = Array<DataEntry>::create(); |
156 for (const auto& requestResponse : m_responses) { | 183 for (const auto& requestResponse : m_responses) { |
157 RefPtr<DataEntry> entry = DataEntry::create() | 184 RefPtr<DataEntry> entry = DataEntry::create() |
158 .setRequest(JSONString::create(requestResponse.request)->toJSONS tring()) | 185 .setRequest(JSONString::create(requestResponse.request)->toJSONS tring()) |
159 .setResponse(JSONString::create(requestResponse.response)->toJSO NString()); | 186 .setResponse(JSONString::create(requestResponse.response)->toJSO NString()); |
160 array->addItem(entry); | 187 array->addItem(entry); |
161 } | 188 } |
162 m_callback->sendSuccess(array, hasMore); | 189 m_callback->sendSuccess(array, hasMore); |
163 } | 190 } |
164 | 191 |
165 private: | 192 private: |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 GetCacheKeysForRequestData(const DataRequestParams& params, PassOwnPtr<WebSe rviceWorkerCache> cache, PassRefPtrWillBeRawPtr<RequestEntriesCallback> callback ) | 235 GetCacheKeysForRequestData(const DataRequestParams& params, PassOwnPtr<WebSe rviceWorkerCache> cache, PassRefPtrWillBeRawPtr<RequestEntriesCallback> callback ) |
209 : m_params(params) | 236 : m_params(params) |
210 , m_cache(cache) | 237 , m_cache(cache) |
211 , m_callback(callback) | 238 , m_callback(callback) |
212 { | 239 { |
213 } | 240 } |
214 virtual ~GetCacheKeysForRequestData() { } | 241 virtual ~GetCacheKeysForRequestData() { } |
215 | 242 |
216 void onSuccess(WebVector<WebServiceWorkerRequest>* requests) | 243 void onSuccess(WebVector<WebServiceWorkerRequest>* requests) |
217 { | 244 { |
245 if (requests->isEmpty()) { | |
246 RefPtr<Array<DataEntry>> array = Array<DataEntry>::create(); | |
247 m_callback->sendSuccess(array, false); | |
248 return; | |
249 } | |
218 RefPtr<ResponsesAccumulator> accumulator = adoptRef(new ResponsesAccumul ator(requests->size(), m_params, m_callback)); | 250 RefPtr<ResponsesAccumulator> accumulator = adoptRef(new ResponsesAccumul ator(requests->size(), m_params, m_callback)); |
219 | 251 |
220 for (size_t i = 0; i < requests->size(); i++) { | 252 for (size_t i = 0; i < requests->size(); i++) { |
221 const auto& request = (*requests)[i]; | 253 const auto& request = (*requests)[i]; |
222 auto* cacheRequest = new GetCacheResponsesForRequestData(m_params, r equest, accumulator, m_callback); | 254 auto* cacheRequest = new GetCacheResponsesForRequestData(m_params, r equest, accumulator, m_callback); |
223 m_cache->dispatchMatch(cacheRequest, request, WebServiceWorkerCache: :QueryParams()); | 255 m_cache->dispatchMatch(cacheRequest, request, WebServiceWorkerCache: :QueryParams()); |
224 } | 256 } |
225 } | 257 } |
226 | 258 |
227 void onError(WebServiceWorkerCacheError* error) | 259 void onError(WebServiceWorkerCacheError* error) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
282 { | 314 { |
283 m_callback->sendFailure(String::format("Error requesting cache names: %s ", serviceWorkerCacheErrorString(error).data())); | 315 m_callback->sendFailure(String::format("Error requesting cache names: %s ", serviceWorkerCacheErrorString(error).data())); |
284 } | 316 } |
285 | 317 |
286 private: | 318 private: |
287 RefPtrWillBePersistent<DeleteCacheCallback> m_callback; | 319 RefPtrWillBePersistent<DeleteCacheCallback> m_callback; |
288 }; | 320 }; |
289 | 321 |
290 } // namespace | 322 } // namespace |
291 | 323 |
292 InspectorCacheStorageAgent::InspectorCacheStorageAgent(ServiceWorkerGlobalScope* scope) | 324 InspectorCacheStorageAgent::InspectorCacheStorageAgent() |
293 : InspectorBaseAgent<blink::InspectorCacheStorageAgent, InspectorFrontend::S erviceWorkerCache>("ServiceWorkerCache") | 325 : InspectorBaseAgent<InspectorCacheStorageAgent, InspectorFrontend::CacheSto rage>("CacheStorage") |
294 , m_globalScope(scope) | |
295 { | 326 { |
296 } | 327 } |
297 | 328 |
298 InspectorCacheStorageAgent::~InspectorCacheStorageAgent() { } | 329 InspectorCacheStorageAgent::~InspectorCacheStorageAgent() { } |
299 | 330 |
300 DEFINE_TRACE(InspectorCacheStorageAgent) | 331 DEFINE_TRACE(InspectorCacheStorageAgent) |
301 { | 332 { |
302 InspectorBaseAgent::trace(visitor); | 333 InspectorBaseAgent::trace(visitor); |
303 } | 334 } |
304 | 335 |
305 void InspectorCacheStorageAgent::requestCacheNames(ErrorString* errorString, Pas sRefPtrWillBeRawPtr<RequestCacheNamesCallback> callback) | 336 void InspectorCacheStorageAgent::requestCacheNames(ErrorString* errorString, con st String& securityOrigin, PassRefPtrWillBeRawPtr<RequestCacheNamesCallback> cal lback) |
306 { | 337 { |
307 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, m_globalScope); | 338 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, securityOrigin); |
308 if (!cache) { | 339 if (!cache) { |
309 callback->sendFailure(*errorString); | 340 callback->sendFailure(*errorString); |
310 return; | 341 return; |
311 } | 342 } |
312 cache->dispatchKeys(new RequestCacheNames(callback)); | 343 cache->dispatchKeys(new RequestCacheNames(securityOrigin, callback)); |
313 } | 344 } |
314 | 345 |
315 | 346 void InspectorCacheStorageAgent::requestEntries(ErrorString* errorString, const String& cacheId, int skipCount, int pageSize, PassRefPtrWillBeRawPtr<RequestEntr iesCallback> callback) |
316 void InspectorCacheStorageAgent::requestEntries(ErrorString* errorString, const String& cacheName, int skipCount, int pageSize, PassRefPtrWillBeRawPtr<RequestEn triesCallback> callback) | |
317 { | 347 { |
318 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, m_globalScope); | 348 String securityOrigin, cacheName; |
pfeldman
2015/04/20 18:14:31
Declaration per line, please!
dmurph
2015/04/20 20:58:17
Done.
| |
349 if (!decomposeCacheId(errorString, cacheId, &securityOrigin, &cacheName)) { | |
350 callback->sendFailure(*errorString); | |
351 return; | |
352 } | |
353 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, securityOrigin); | |
pfeldman
2015/04/20 18:14:31
Extract assertCacheStorageForId(errorString, cache
dmurph
2015/04/20 20:58:17
Done.
| |
319 if (!cache) { | 354 if (!cache) { |
320 callback->sendFailure(*errorString); | 355 callback->sendFailure(*errorString); |
321 return; | 356 return; |
322 } | 357 } |
323 DataRequestParams params; | 358 DataRequestParams params; |
324 params.cacheName = cacheName; | 359 params.cacheName = cacheName; |
325 params.pageSize = pageSize; | 360 params.pageSize = pageSize; |
326 params.skipCount = skipCount; | 361 params.skipCount = skipCount; |
327 cache->dispatchOpen(new GetCacheForRequestData(params, callback), WebString( cacheName)); | 362 cache->dispatchOpen(new GetCacheForRequestData(params, callback), WebString( cacheName)); |
328 } | 363 } |
329 | 364 |
330 void InspectorCacheStorageAgent::deleteCache(ErrorString* errorString, const Str ing& cacheName, PassRefPtrWillBeRawPtr<DeleteCacheCallback> callback) | 365 void InspectorCacheStorageAgent::deleteCache(ErrorString* errorString, const Str ing& cacheId, PassRefPtrWillBeRawPtr<DeleteCacheCallback> callback) |
331 { | 366 { |
332 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, m_globalScope); | 367 String securityOrigin, cacheName; |
368 if (!decomposeCacheId(errorString, cacheId, &securityOrigin, &cacheName)) { | |
369 callback->sendFailure(*errorString); | |
370 return; | |
371 } | |
372 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, securityOrigin); | |
333 if (!cache) { | 373 if (!cache) { |
334 callback->sendFailure(*errorString); | 374 callback->sendFailure(*errorString); |
335 return; | 375 return; |
336 } | 376 } |
337 cache->dispatchDelete(new DeleteCache(callback), WebString(cacheName)); | 377 cache->dispatchDelete(new DeleteCache(callback), WebString(cacheName)); |
338 } | 378 } |
339 | 379 |
340 } // namespace blink | 380 } // namespace blink |
OLD | NEW |