Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(318)

Side by Side Diff: Source/modules/cachestorage/InspectorCacheStorageAgent.cpp

Issue 1044203004: [Storage] Cache storage inspection on all the frames! (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Comments and fix Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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::CacheId;
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 PassOwnPtr<WebServiceWorkerCacheStorage> assertCacheStorage(ErrorString* errorSt ring, const String& securityOrigin)
46 { 47 {
47 String identifier = createDatabaseIdentifierFromSecurityOrigin(globalScope-> securityOrigin()); 48 RefPtr<SecurityOrigin> secOrigin = SecurityOrigin::createFromString(security Origin);
48 OwnPtr<WebServiceWorkerCacheStorage> caches = adoptPtr(Platform::current()-> cacheStorage(identifier)); 49 String identifier = createDatabaseIdentifierFromSecurityOrigin(secOrigin.get ());
49 if (!caches) { 50 WebServiceWorkerCacheStorage* cache = Platform::current()->cacheStorage(iden tifier);
50 *errorString = "Cache Storage not available in global scope."; 51 if (!cache)
51 return nullptr; 52 *errorString = "Could not find cache storage.";
52 } 53 return adoptPtr(cache);
53 return caches.release();
54 } 54 }
55 55
56 CString serviceWorkerCacheErrorString(WebServiceWorkerCacheError* error) 56 CString serviceWorkerCacheErrorString(WebServiceWorkerCacheError* error)
57 { 57 {
58 switch (*error) { 58 switch (*error) {
59 case WebServiceWorkerCacheErrorNotImplemented: 59 case WebServiceWorkerCacheErrorNotImplemented:
60 return CString("not implemented."); 60 return CString("not implemented.");
61 break; 61 break;
62 case WebServiceWorkerCacheErrorNotFound: 62 case WebServiceWorkerCacheErrorNotFound:
63 return CString("not found."); 63 return CString("not found.");
64 break; 64 break;
65 case WebServiceWorkerCacheErrorExists: 65 case WebServiceWorkerCacheErrorExists:
66 return CString("cache already exists."); 66 return CString("cache already exists.");
67 break; 67 break;
68 default: 68 default:
69 return CString("unknown error."); 69 return CString("unknown error.");
70 break; 70 break;
71 } 71 }
72 } 72 }
73 73
74 class RequestCacheNames 74 class RequestCacheNames
75 : public WebServiceWorkerCacheStorage::CacheStorageKeysCallbacks { 75 : public WebServiceWorkerCacheStorage::CacheStorageKeysCallbacks {
76 WTF_MAKE_NONCOPYABLE(RequestCacheNames); 76 WTF_MAKE_NONCOPYABLE(RequestCacheNames);
77 77
78 public: 78 public:
79 RequestCacheNames(PassRefPtrWillBeRawPtr<RequestCacheNamesCallback> callback ) 79 RequestCacheNames(const String& securityOrigin, PassRefPtrWillBeRawPtr<Reque stCacheNamesCallback> callback)
80 : m_callback(callback) 80 : m_securityOrigin(securityOrigin)
81 , m_callback(callback)
81 { 82 {
82 } 83 }
83 84
84 virtual ~RequestCacheNames() { } 85 virtual ~RequestCacheNames() { }
85 86
86 void onSuccess(WebVector<WebString>* caches) 87 void onSuccess(WebVector<WebString>* caches)
87 { 88 {
88 RefPtr<TypeBuilder::Array<String>> array = TypeBuilder::Array<String>::c reate(); 89 RefPtr<Array<CacheId>> array = Array<CacheId>::create();
89 for (size_t i = 0; i < caches->size(); i++) { 90 for (size_t i = 0; i < caches->size(); i++) {
90 array->addItem(String((*caches)[i])); 91 RefPtr<CacheId> entry = CacheId::create()
92 .setSecurityOrigin(m_securityOrigin)
93 .setCacheName(String((*caches)[i]));
94 array->addItem(entry);
91 } 95 }
92 m_callback->sendSuccess(array); 96 m_callback->sendSuccess(array);
93 } 97 }
94 98
95 void onError(WebServiceWorkerCacheError* error) 99 void onError(WebServiceWorkerCacheError* error)
96 { 100 {
97 m_callback->sendFailure(String::format("Error requesting cache names: %s ", serviceWorkerCacheErrorString(error).data())); 101 m_callback->sendFailure(String::format("Error requesting cache names: %s ", serviceWorkerCacheErrorString(error).data()));
98 } 102 }
99 103
100 private: 104 private:
105 String m_securityOrigin;
101 RefPtrWillBePersistent<RequestCacheNamesCallback> m_callback; 106 RefPtrWillBePersistent<RequestCacheNamesCallback> m_callback;
102 }; 107 };
103 108
104 struct DataRequestParams { 109 struct DataRequestParams {
105 String cacheName; 110 String cacheName;
106 int skipCount; 111 int skipCount;
107 int pageSize; 112 int pageSize;
108 }; 113 };
109 114
110 struct RequestResponse { 115 struct RequestResponse {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 { 150 {
146 return WTF::codePointCompareLessThan(a.request, b.request); 151 return WTF::codePointCompareLessThan(a.request, b.request);
147 }); 152 });
148 if (m_params.skipCount > 0) 153 if (m_params.skipCount > 0)
149 m_responses.remove(0, m_params.skipCount); 154 m_responses.remove(0, m_params.skipCount);
150 bool hasMore = false; 155 bool hasMore = false;
151 if (static_cast<size_t>(m_params.pageSize) < m_responses.size()) { 156 if (static_cast<size_t>(m_params.pageSize) < m_responses.size()) {
152 m_responses.remove(m_params.pageSize, m_responses.size() - m_params. pageSize); 157 m_responses.remove(m_params.pageSize, m_responses.size() - m_params. pageSize);
153 hasMore = true; 158 hasMore = true;
154 } 159 }
155 RefPtr<TypeBuilder::Array<DataEntry>> array = TypeBuilder::Array<DataEnt ry>::create(); 160 RefPtr<Array<DataEntry>> array = Array<DataEntry>::create();
156 for (const auto& requestResponse : m_responses) { 161 for (const auto& requestResponse : m_responses) {
157 RefPtr<DataEntry> entry = DataEntry::create() 162 RefPtr<DataEntry> entry = DataEntry::create()
158 .setRequest(JSONString::create(requestResponse.request)->toJSONS tring()) 163 .setRequest(JSONString::create(requestResponse.request)->toJSONS tring())
159 .setResponse(JSONString::create(requestResponse.response)->toJSO NString()); 164 .setResponse(JSONString::create(requestResponse.response)->toJSO NString());
160 array->addItem(entry); 165 array->addItem(entry);
161 } 166 }
162 m_callback->sendSuccess(array, hasMore); 167 m_callback->sendSuccess(array, hasMore);
163 } 168 }
164 169
165 private: 170 private:
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 GetCacheKeysForRequestData(const DataRequestParams& params, PassOwnPtr<WebSe rviceWorkerCache> cache, PassRefPtrWillBeRawPtr<RequestEntriesCallback> callback ) 213 GetCacheKeysForRequestData(const DataRequestParams& params, PassOwnPtr<WebSe rviceWorkerCache> cache, PassRefPtrWillBeRawPtr<RequestEntriesCallback> callback )
209 : m_params(params) 214 : m_params(params)
210 , m_cache(cache) 215 , m_cache(cache)
211 , m_callback(callback) 216 , m_callback(callback)
212 { 217 {
213 } 218 }
214 virtual ~GetCacheKeysForRequestData() { } 219 virtual ~GetCacheKeysForRequestData() { }
215 220
216 void onSuccess(WebVector<WebServiceWorkerRequest>* requests) 221 void onSuccess(WebVector<WebServiceWorkerRequest>* requests)
217 { 222 {
223 if (requests->isEmpty()) {
224 RefPtr<Array<DataEntry>> array = Array<DataEntry>::create();
225 m_callback->sendSuccess(array, false);
226 return;
227 }
218 RefPtr<ResponsesAccumulator> accumulator = adoptRef(new ResponsesAccumul ator(requests->size(), m_params, m_callback)); 228 RefPtr<ResponsesAccumulator> accumulator = adoptRef(new ResponsesAccumul ator(requests->size(), m_params, m_callback));
219 229
220 for (size_t i = 0; i < requests->size(); i++) { 230 for (size_t i = 0; i < requests->size(); i++) {
221 const auto& request = (*requests)[i]; 231 const auto& request = (*requests)[i];
222 auto* cacheRequest = new GetCacheResponsesForRequestData(m_params, r equest, accumulator, m_callback); 232 auto* cacheRequest = new GetCacheResponsesForRequestData(m_params, r equest, accumulator, m_callback);
223 m_cache->dispatchMatch(cacheRequest, request, WebServiceWorkerCache: :QueryParams()); 233 m_cache->dispatchMatch(cacheRequest, request, WebServiceWorkerCache: :QueryParams());
224 } 234 }
225 } 235 }
226 236
227 void onError(WebServiceWorkerCacheError* error) 237 void onError(WebServiceWorkerCacheError* error)
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 { 292 {
283 m_callback->sendFailure(String::format("Error requesting cache names: %s ", serviceWorkerCacheErrorString(error).data())); 293 m_callback->sendFailure(String::format("Error requesting cache names: %s ", serviceWorkerCacheErrorString(error).data()));
284 } 294 }
285 295
286 private: 296 private:
287 RefPtrWillBePersistent<DeleteCacheCallback> m_callback; 297 RefPtrWillBePersistent<DeleteCacheCallback> m_callback;
288 }; 298 };
289 299
290 } // namespace 300 } // namespace
291 301
292 InspectorCacheStorageAgent::InspectorCacheStorageAgent(ServiceWorkerGlobalScope* scope) 302 InspectorCacheStorageAgent::InspectorCacheStorageAgent()
293 : InspectorBaseAgent<blink::InspectorCacheStorageAgent, InspectorFrontend::S erviceWorkerCache>("ServiceWorkerCache") 303 : InspectorBaseAgent<InspectorCacheStorageAgent, InspectorFrontend::CacheSto rage>("CacheStorage")
294 , m_globalScope(scope)
295 { 304 {
296 } 305 }
297 306
298 InspectorCacheStorageAgent::~InspectorCacheStorageAgent() { } 307 InspectorCacheStorageAgent::~InspectorCacheStorageAgent() { }
299 308
300 DEFINE_TRACE(InspectorCacheStorageAgent) 309 DEFINE_TRACE(InspectorCacheStorageAgent)
301 { 310 {
302 InspectorBaseAgent::trace(visitor); 311 InspectorBaseAgent::trace(visitor);
303 } 312 }
304 313
305 void InspectorCacheStorageAgent::requestCacheNames(ErrorString* errorString, Pas sRefPtrWillBeRawPtr<RequestCacheNamesCallback> callback) 314 void InspectorCacheStorageAgent::requestCacheNames(ErrorString* errorString, con st String& securityOrigin, PassRefPtrWillBeRawPtr<RequestCacheNamesCallback> cal lback)
306 { 315 {
307 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, m_globalScope); 316 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, securityOrigin);
308 if (!cache) { 317 if (!cache) {
309 callback->sendFailure(*errorString); 318 callback->sendFailure(*errorString);
310 return; 319 return;
311 } 320 }
312 cache->dispatchKeys(new RequestCacheNames(callback)); 321 cache->dispatchKeys(new RequestCacheNames(securityOrigin, callback));
313 } 322 }
314 323
315 324 void InspectorCacheStorageAgent::requestEntries(ErrorString* errorString, const RefPtr<JSONObject>& cacheId, int skipCount, int pageSize, PassRefPtrWillBeRawPtr <RequestEntriesCallback> callback)
316 void InspectorCacheStorageAgent::requestEntries(ErrorString* errorString, const String& cacheName, int skipCount, int pageSize, PassRefPtrWillBeRawPtr<RequestEn triesCallback> callback)
317 { 325 {
318 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, m_globalScope); 326 String securityOrigin, cacheName;
327 if (!cacheId->getString("securityOrigin", &securityOrigin) || !cacheId->getS tring("cacheName", &cacheName)) {
328 *errorString = "Could not read cacheId";
329 callback->sendFailure(*errorString);
330 return;
331 }
332 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, securityOrigin);
319 if (!cache) { 333 if (!cache) {
320 callback->sendFailure(*errorString); 334 callback->sendFailure(*errorString);
321 return; 335 return;
322 } 336 }
323 DataRequestParams params; 337 DataRequestParams params;
324 params.cacheName = cacheName; 338 params.cacheName = cacheName;
325 params.pageSize = pageSize; 339 params.pageSize = pageSize;
326 params.skipCount = skipCount; 340 params.skipCount = skipCount;
327 cache->dispatchOpen(new GetCacheForRequestData(params, callback), WebString( cacheName)); 341 cache->dispatchOpen(new GetCacheForRequestData(params, callback), WebString( cacheName));
328 } 342 }
329 343
330 void InspectorCacheStorageAgent::deleteCache(ErrorString* errorString, const Str ing& cacheName, PassRefPtrWillBeRawPtr<DeleteCacheCallback> callback) 344 void InspectorCacheStorageAgent::deleteCache(ErrorString* errorString, const Ref Ptr<JSONObject>& cacheId, PassRefPtrWillBeRawPtr<DeleteCacheCallback> callback)
331 { 345 {
332 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, m_globalScope); 346 String securityOrigin, cacheName;
347 if (!cacheId->getString("securityOrigin", &securityOrigin) || !cacheId->getS tring("cacheName", &cacheName)) {
348 *errorString = "Could not read cacheId";
349 callback->sendFailure(*errorString);
350 return;
351 }
352 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, securityOrigin);
333 if (!cache) { 353 if (!cache) {
334 callback->sendFailure(*errorString); 354 callback->sendFailure(*errorString);
335 return; 355 return;
336 } 356 }
337 cache->dispatchDelete(new DeleteCache(callback), WebString(cacheName)); 357 cache->dispatchDelete(new DeleteCache(callback), WebString(cacheName));
338 } 358 }
339 359
340 } // namespace blink 360 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698