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

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 rebase 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::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 buildCacheId(const String& securityOrigin, const String& cacheName)
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;
52 }
53
54 bool parseCacheId(ErrorString* errorString, const String& id, String* securityOr igin, String* cacheName)
55 {
56 size_t pipe = id.find('|');
57 if (pipe == WTF::kNotFound) {
58 *errorString = "Invalid cache id.";
59 return false;
60 }
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 OwnPtr<WebServiceWorkerCacheStorage> cache = adoptPtr(Platform::current()->c acheStorage(identifier));
71 if (!cache)
72 *errorString = "Could not find cache storage.";
73 return cache.release();
74 }
75
76 PassOwnPtr<WebServiceWorkerCacheStorage> assertCacheStorageAndNameForId(ErrorStr ing* errorString, const String& cacheId, String* cacheName)
77 {
78 String securityOrigin;
79 if (!parseCacheId(errorString, cacheId, &securityOrigin, cacheName)) {
51 return nullptr; 80 return nullptr;
52 } 81 }
53 return caches.release(); 82 return assertCacheStorage(errorString, securityOrigin);
54 } 83 }
55 84
56 CString serviceWorkerCacheErrorString(WebServiceWorkerCacheError* error) 85 CString serviceWorkerCacheErrorString(WebServiceWorkerCacheError* error)
57 { 86 {
58 switch (*error) { 87 switch (*error) {
59 case WebServiceWorkerCacheErrorNotImplemented: 88 case WebServiceWorkerCacheErrorNotImplemented:
60 return CString("not implemented."); 89 return CString("not implemented.");
61 break; 90 break;
62 case WebServiceWorkerCacheErrorNotFound: 91 case WebServiceWorkerCacheErrorNotFound:
63 return CString("not found."); 92 return CString("not found.");
64 break; 93 break;
65 case WebServiceWorkerCacheErrorExists: 94 case WebServiceWorkerCacheErrorExists:
66 return CString("cache already exists."); 95 return CString("cache already exists.");
67 break; 96 break;
68 default: 97 default:
69 return CString("unknown error."); 98 return CString("unknown error.");
70 break; 99 break;
71 } 100 }
72 } 101 }
73 102
74 class RequestCacheNames 103 class RequestCacheNames
75 : public WebServiceWorkerCacheStorage::CacheStorageKeysCallbacks { 104 : public WebServiceWorkerCacheStorage::CacheStorageKeysCallbacks {
76 WTF_MAKE_NONCOPYABLE(RequestCacheNames); 105 WTF_MAKE_NONCOPYABLE(RequestCacheNames);
77 106
78 public: 107 public:
79 RequestCacheNames(PassRefPtrWillBeRawPtr<RequestCacheNamesCallback> callback ) 108 RequestCacheNames(const String& securityOrigin, PassRefPtrWillBeRawPtr<Reque stCacheNamesCallback> callback)
80 : m_callback(callback) 109 : m_securityOrigin(securityOrigin)
110 , m_callback(callback)
81 { 111 {
82 } 112 }
83 113
84 virtual ~RequestCacheNames() { } 114 virtual ~RequestCacheNames() { }
85 115
86 void onSuccess(WebVector<WebString>* caches) 116 void onSuccess(WebVector<WebString>* caches)
87 { 117 {
88 RefPtr<TypeBuilder::Array<String>> array = TypeBuilder::Array<String>::c reate(); 118 RefPtr<Array<Cache>> array = Array<Cache>::create();
89 for (size_t i = 0; i < caches->size(); i++) { 119 for (size_t i = 0; i < caches->size(); i++) {
90 array->addItem(String((*caches)[i])); 120 String name = String((*caches)[i]);
121 RefPtr<Cache> entry = Cache::create()
122 .setSecurityOrigin(m_securityOrigin)
123 .setCacheName(name)
124 .setCacheId(buildCacheId(m_securityOrigin, name));
125 array->addItem(entry);
91 } 126 }
92 m_callback->sendSuccess(array); 127 m_callback->sendSuccess(array);
93 } 128 }
94 129
95 void onError(WebServiceWorkerCacheError* error) 130 void onError(WebServiceWorkerCacheError* error)
96 { 131 {
97 m_callback->sendFailure(String::format("Error requesting cache names: %s ", serviceWorkerCacheErrorString(error).data())); 132 m_callback->sendFailure(String::format("Error requesting cache names: %s ", serviceWorkerCacheErrorString(error).data()));
98 } 133 }
99 134
100 private: 135 private:
136 String m_securityOrigin;
101 RefPtrWillBePersistent<RequestCacheNamesCallback> m_callback; 137 RefPtrWillBePersistent<RequestCacheNamesCallback> m_callback;
102 }; 138 };
103 139
104 struct DataRequestParams { 140 struct DataRequestParams {
105 String cacheName; 141 String cacheName;
106 int skipCount; 142 int skipCount;
107 int pageSize; 143 int pageSize;
108 }; 144 };
109 145
110 struct RequestResponse { 146 struct RequestResponse {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 { 181 {
146 return WTF::codePointCompareLessThan(a.request, b.request); 182 return WTF::codePointCompareLessThan(a.request, b.request);
147 }); 183 });
148 if (m_params.skipCount > 0) 184 if (m_params.skipCount > 0)
149 m_responses.remove(0, m_params.skipCount); 185 m_responses.remove(0, m_params.skipCount);
150 bool hasMore = false; 186 bool hasMore = false;
151 if (static_cast<size_t>(m_params.pageSize) < m_responses.size()) { 187 if (static_cast<size_t>(m_params.pageSize) < m_responses.size()) {
152 m_responses.remove(m_params.pageSize, m_responses.size() - m_params. pageSize); 188 m_responses.remove(m_params.pageSize, m_responses.size() - m_params. pageSize);
153 hasMore = true; 189 hasMore = true;
154 } 190 }
155 RefPtr<TypeBuilder::Array<DataEntry>> array = TypeBuilder::Array<DataEnt ry>::create(); 191 RefPtr<Array<DataEntry>> array = Array<DataEntry>::create();
156 for (const auto& requestResponse : m_responses) { 192 for (const auto& requestResponse : m_responses) {
157 RefPtr<DataEntry> entry = DataEntry::create() 193 RefPtr<DataEntry> entry = DataEntry::create()
158 .setRequest(JSONString::create(requestResponse.request)->toJSONS tring()) 194 .setRequest(JSONString::create(requestResponse.request)->toJSONS tring())
159 .setResponse(JSONString::create(requestResponse.response)->toJSO NString()); 195 .setResponse(JSONString::create(requestResponse.response)->toJSO NString());
160 array->addItem(entry); 196 array->addItem(entry);
161 } 197 }
162 m_callback->sendSuccess(array, hasMore); 198 m_callback->sendSuccess(array, hasMore);
163 } 199 }
164 200
165 private: 201 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 ) 244 GetCacheKeysForRequestData(const DataRequestParams& params, PassOwnPtr<WebSe rviceWorkerCache> cache, PassRefPtrWillBeRawPtr<RequestEntriesCallback> callback )
209 : m_params(params) 245 : m_params(params)
210 , m_cache(cache) 246 , m_cache(cache)
211 , m_callback(callback) 247 , m_callback(callback)
212 { 248 {
213 } 249 }
214 virtual ~GetCacheKeysForRequestData() { } 250 virtual ~GetCacheKeysForRequestData() { }
215 251
216 void onSuccess(WebVector<WebServiceWorkerRequest>* requests) 252 void onSuccess(WebVector<WebServiceWorkerRequest>* requests)
217 { 253 {
254 if (requests->isEmpty()) {
255 RefPtr<Array<DataEntry>> array = Array<DataEntry>::create();
256 m_callback->sendSuccess(array, false);
257 return;
258 }
218 RefPtr<ResponsesAccumulator> accumulator = adoptRef(new ResponsesAccumul ator(requests->size(), m_params, m_callback)); 259 RefPtr<ResponsesAccumulator> accumulator = adoptRef(new ResponsesAccumul ator(requests->size(), m_params, m_callback));
219 260
220 for (size_t i = 0; i < requests->size(); i++) { 261 for (size_t i = 0; i < requests->size(); i++) {
221 const auto& request = (*requests)[i]; 262 const auto& request = (*requests)[i];
222 auto* cacheRequest = new GetCacheResponsesForRequestData(m_params, r equest, accumulator, m_callback); 263 auto* cacheRequest = new GetCacheResponsesForRequestData(m_params, r equest, accumulator, m_callback);
223 m_cache->dispatchMatch(cacheRequest, request, WebServiceWorkerCache: :QueryParams()); 264 m_cache->dispatchMatch(cacheRequest, request, WebServiceWorkerCache: :QueryParams());
224 } 265 }
225 } 266 }
226 267
227 void onError(WebServiceWorkerCacheError* error) 268 void onError(WebServiceWorkerCacheError* error)
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 { 323 {
283 m_callback->sendFailure(String::format("Error requesting cache names: %s ", serviceWorkerCacheErrorString(error).data())); 324 m_callback->sendFailure(String::format("Error requesting cache names: %s ", serviceWorkerCacheErrorString(error).data()));
284 } 325 }
285 326
286 private: 327 private:
287 RefPtrWillBePersistent<DeleteCacheCallback> m_callback; 328 RefPtrWillBePersistent<DeleteCacheCallback> m_callback;
288 }; 329 };
289 330
290 } // namespace 331 } // namespace
291 332
292 InspectorCacheStorageAgent::InspectorCacheStorageAgent(ServiceWorkerGlobalScope* scope) 333 InspectorCacheStorageAgent::InspectorCacheStorageAgent()
293 : InspectorBaseAgent<blink::InspectorCacheStorageAgent, InspectorFrontend::S erviceWorkerCache>("ServiceWorkerCache") 334 : InspectorBaseAgent<InspectorCacheStorageAgent, InspectorFrontend::CacheSto rage>("CacheStorage")
294 , m_globalScope(scope)
295 { 335 {
296 } 336 }
297 337
298 InspectorCacheStorageAgent::~InspectorCacheStorageAgent() { } 338 InspectorCacheStorageAgent::~InspectorCacheStorageAgent() { }
299 339
300 DEFINE_TRACE(InspectorCacheStorageAgent) 340 DEFINE_TRACE(InspectorCacheStorageAgent)
301 { 341 {
302 InspectorBaseAgent::trace(visitor); 342 InspectorBaseAgent::trace(visitor);
303 } 343 }
304 344
305 void InspectorCacheStorageAgent::requestCacheNames(ErrorString* errorString, Pas sRefPtrWillBeRawPtr<RequestCacheNamesCallback> callback) 345 void InspectorCacheStorageAgent::requestCacheNames(ErrorString* errorString, con st String& securityOrigin, PassRefPtrWillBeRawPtr<RequestCacheNamesCallback> cal lback)
306 { 346 {
307 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, m_globalScope); 347 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, securityOrigin);
308 if (!cache) { 348 if (!cache) {
309 callback->sendFailure(*errorString); 349 callback->sendFailure(*errorString);
310 return; 350 return;
311 } 351 }
312 cache->dispatchKeys(new RequestCacheNames(callback)); 352 cache->dispatchKeys(new RequestCacheNames(securityOrigin, callback));
313 } 353 }
314 354
315 355 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 { 356 {
318 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, m_globalScope); 357 String cacheName;
358 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorageAndNameForId( errorString, cacheId, &cacheName);
319 if (!cache) { 359 if (!cache) {
320 callback->sendFailure(*errorString); 360 callback->sendFailure(*errorString);
321 return; 361 return;
322 } 362 }
323 DataRequestParams params; 363 DataRequestParams params;
324 params.cacheName = cacheName; 364 params.cacheName = cacheName;
325 params.pageSize = pageSize; 365 params.pageSize = pageSize;
326 params.skipCount = skipCount; 366 params.skipCount = skipCount;
327 cache->dispatchOpen(new GetCacheForRequestData(params, callback), WebString( cacheName)); 367 cache->dispatchOpen(new GetCacheForRequestData(params, callback), WebString( cacheName));
328 } 368 }
329 369
330 void InspectorCacheStorageAgent::deleteCache(ErrorString* errorString, const Str ing& cacheName, PassRefPtrWillBeRawPtr<DeleteCacheCallback> callback) 370 void InspectorCacheStorageAgent::deleteCache(ErrorString* errorString, const Str ing& cacheId, PassRefPtrWillBeRawPtr<DeleteCacheCallback> callback)
331 { 371 {
332 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, m_globalScope); 372 String cacheName;
373 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorageAndNameForId( errorString, cacheId, &cacheName);
333 if (!cache) { 374 if (!cache) {
334 callback->sendFailure(*errorString); 375 callback->sendFailure(*errorString);
335 return; 376 return;
336 } 377 }
337 cache->dispatchDelete(new DeleteCache(callback), WebString(cacheName)); 378 cache->dispatchDelete(new DeleteCache(callback), WebString(cacheName));
338 } 379 }
339 380
340 } // namespace blink 381 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698