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/serviceworkers/InspectorServiceWorkerCacheAgent.h" | 6 #include "modules/serviceworkers/InspectorServiceWorkerCacheAgent.h" |
7 | 7 |
8 #include "bindings/core/v8/ScriptState.h" | |
8 #include "core/InspectorBackendDispatcher.h" | 9 #include "core/InspectorBackendDispatcher.h" |
9 #include "core/InspectorTypeBuilder.h" | 10 #include "core/InspectorTypeBuilder.h" |
11 #include "core/dom/Document.h" | |
12 #include "core/frame/DOMWindow.h" | |
13 #include "core/frame/LocalFrame.h" | |
14 #include "core/page/Page.h" | |
15 #include "modules/serviceworkers/CacheStorage.h" | |
16 #include "modules/serviceworkers/GlobalCacheStorage.h" | |
10 #include "modules/serviceworkers/ServiceWorkerGlobalScope.h" | 17 #include "modules/serviceworkers/ServiceWorkerGlobalScope.h" |
11 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" | 18 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" |
12 #include "platform/JSONValues.h" | 19 #include "platform/JSONValues.h" |
13 #include "platform/heap/Handle.h" | 20 #include "platform/heap/Handle.h" |
14 #include "platform/weborigin/DatabaseIdentifier.h" | 21 #include "platform/weborigin/DatabaseIdentifier.h" |
15 #include "public/platform/Platform.h" | 22 #include "public/platform/Platform.h" |
16 #include "public/platform/WebServiceWorkerCache.h" | 23 #include "public/platform/WebServiceWorkerCache.h" |
17 #include "public/platform/WebServiceWorkerCacheError.h" | 24 #include "public/platform/WebServiceWorkerCacheError.h" |
18 #include "public/platform/WebServiceWorkerCacheStorage.h" | 25 #include "public/platform/WebServiceWorkerCacheStorage.h" |
19 #include "public/platform/WebServiceWorkerRequest.h" | 26 #include "public/platform/WebServiceWorkerRequest.h" |
20 #include "public/platform/WebServiceWorkerResponse.h" | 27 #include "public/platform/WebServiceWorkerResponse.h" |
21 #include "public/platform/WebString.h" | 28 #include "public/platform/WebString.h" |
22 #include "public/platform/WebURL.h" | 29 #include "public/platform/WebURL.h" |
23 #include "public/platform/WebVector.h" | 30 #include "public/platform/WebVector.h" |
24 #include "wtf/Noncopyable.h" | 31 #include "wtf/Noncopyable.h" |
25 #include "wtf/OwnPtr.h" | 32 #include "wtf/OwnPtr.h" |
26 #include "wtf/PassRefPtr.h" | 33 #include "wtf/PassRefPtr.h" |
27 #include "wtf/RefCounted.h" | 34 #include "wtf/RefCounted.h" |
28 #include "wtf/RefPtr.h" | 35 #include "wtf/RefPtr.h" |
29 #include "wtf/Vector.h" | 36 #include "wtf/Vector.h" |
30 #include "wtf/text/StringBuilder.h" | 37 #include "wtf/text/StringBuilder.h" |
31 | 38 |
32 #include <algorithm> | 39 #include <algorithm> |
33 | 40 |
34 using blink::TypeBuilder::Array; | 41 using blink::TypeBuilder::Array; |
35 using blink::TypeBuilder::ServiceWorkerCache::DataEntry; | 42 using blink::TypeBuilder::CacheStorage::DataEntry; |
36 | 43 |
37 typedef blink::InspectorBackendDispatcher::ServiceWorkerCacheCommandHandler::Del eteCacheCallback DeleteCacheCallback; | 44 typedef blink::InspectorBackendDispatcher::CacheStorageCommandHandler::DeleteCac heCallback DeleteCacheCallback; |
38 typedef blink::InspectorBackendDispatcher::ServiceWorkerCacheCommandHandler::Req uestCacheNamesCallback RequestCacheNamesCallback; | 45 typedef blink::InspectorBackendDispatcher::CacheStorageCommandHandler::RequestCa cheNamesCallback RequestCacheNamesCallback; |
39 typedef blink::InspectorBackendDispatcher::ServiceWorkerCacheCommandHandler::Req uestEntriesCallback RequestEntriesCallback; | 46 typedef blink::InspectorBackendDispatcher::CacheStorageCommandHandler::RequestEn triesCallback RequestEntriesCallback; |
40 typedef blink::InspectorBackendDispatcher::CallbackBase RequestCallback; | 47 typedef blink::InspectorBackendDispatcher::CallbackBase RequestCallback; |
41 | 48 |
42 namespace blink { | 49 namespace blink { |
43 | 50 |
44 namespace { | 51 namespace { |
45 | 52 |
46 PassOwnPtr<WebServiceWorkerCacheStorage> assertCacheStorage(ErrorString* errorSt ring, ServiceWorkerGlobalScope* globalScope) | 53 LocalFrame* findFrameWithSecurityOrigin(Page* page, const String& securityOrigin ) |
47 { | 54 { |
48 String identifier = createDatabaseIdentifierFromSecurityOrigin(globalScope-> securityOrigin()); | 55 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree().traverse Next()) { |
49 OwnPtr<WebServiceWorkerCacheStorage> caches = adoptPtr(Platform::current()-> cacheStorage(identifier)); | 56 if (!frame->isLocalFrame()) |
50 if (!caches) { | 57 continue; |
51 *errorString = "Cache Storage not available in global scope."; | 58 RefPtr<SecurityOrigin> documentOrigin = toLocalFrame(frame)->document()- >securityOrigin(); |
59 if (documentOrigin->toRawString() == securityOrigin) | |
60 return toLocalFrame(frame); | |
61 } | |
62 return 0; | |
63 } | |
64 | |
65 static DOMWindow* assertWindow(ErrorString* errorString, LocalFrame* frame) | |
horo
2015/04/01 03:39:07
remove "static"
dmurph
2015/04/02 20:59:03
Done.
| |
66 { | |
67 DOMWindow* window = frame ? frame->domWindow() : 0; | |
68 | |
69 if (!window) | |
70 *errorString = "No window for given frame found."; | |
71 | |
72 return window; | |
73 } | |
74 | |
75 WebServiceWorkerCacheStorage* assertCacheStorage(ErrorString* errorString, Scrip tState* state, DOMWindow& window) | |
76 { | |
77 TrackExceptionState exceptionState; | |
78 CacheStorage* cache = GlobalCacheStorage::caches(state, window, exceptionSta te); | |
79 if (exceptionState.hadException()) { | |
80 *errorString = "Exception while retrieving Cache Storage."; | |
52 return nullptr; | 81 return nullptr; |
53 } | 82 } |
54 return caches.release(); | 83 if (!cache) { |
84 *errorString = "Cache Storage not available."; | |
85 return nullptr; | |
86 } | |
87 WebServiceWorkerCacheStorage* webCache = cache->webCacheStorage(); | |
88 if (!webCache) { | |
89 *errorString = "Web cache not found on Cache Storage."; | |
90 return nullptr; | |
91 } | |
92 return webCache; | |
55 } | 93 } |
56 | 94 |
57 CString serviceWorkerCacheErrorString(WebServiceWorkerCacheError* error) | 95 CString serviceWorkerCacheErrorString(WebServiceWorkerCacheError* error) |
58 { | 96 { |
59 switch (*error) { | 97 switch (*error) { |
60 case WebServiceWorkerCacheErrorNotImplemented: | 98 case WebServiceWorkerCacheErrorNotImplemented: |
61 return CString("not implemented."); | 99 return CString("not implemented."); |
62 break; | 100 break; |
63 case WebServiceWorkerCacheErrorNotFound: | 101 case WebServiceWorkerCacheErrorNotFound: |
64 return CString("not found."); | 102 return CString("not found."); |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
283 { | 321 { |
284 m_callback->sendFailure(String::format("Error requesting cache names: %s ", serviceWorkerCacheErrorString(error).data())); | 322 m_callback->sendFailure(String::format("Error requesting cache names: %s ", serviceWorkerCacheErrorString(error).data())); |
285 } | 323 } |
286 | 324 |
287 private: | 325 private: |
288 RefPtrWillBePersistent<DeleteCacheCallback> m_callback; | 326 RefPtrWillBePersistent<DeleteCacheCallback> m_callback; |
289 }; | 327 }; |
290 | 328 |
291 } // namespace | 329 } // namespace |
292 | 330 |
293 InspectorServiceWorkerCacheAgent::InspectorServiceWorkerCacheAgent(ServiceWorker GlobalScope* scope) | 331 InspectorServiceWorkerCacheAgent::InspectorServiceWorkerCacheAgent(Page* page) |
294 : InspectorBaseAgent<blink::InspectorServiceWorkerCacheAgent, InspectorFront end::ServiceWorkerCache>("ServiceWorkerCache") | 332 : InspectorBaseAgent<InspectorServiceWorkerCacheAgent, InspectorFrontend::Ca cheStorage>("CacheStorage") |
295 , m_globalScope(scope) | 333 , m_page(page) |
296 { | 334 { |
297 } | 335 } |
298 | 336 |
299 InspectorServiceWorkerCacheAgent::~InspectorServiceWorkerCacheAgent() { } | 337 InspectorServiceWorkerCacheAgent::~InspectorServiceWorkerCacheAgent() { } |
300 | 338 |
301 DEFINE_TRACE(InspectorServiceWorkerCacheAgent) | 339 DEFINE_TRACE(InspectorServiceWorkerCacheAgent) |
302 { | 340 { |
303 InspectorBaseAgent::trace(visitor); | 341 InspectorBaseAgent::trace(visitor); |
304 } | 342 } |
305 | 343 |
306 void InspectorServiceWorkerCacheAgent::requestCacheNames(ErrorString* errorStrin g, PassRefPtrWillBeRawPtr<RequestCacheNamesCallback> callback) | 344 void InspectorServiceWorkerCacheAgent::requestCacheNames(ErrorString* errorStrin g, const String& securityOrigin, PassRefPtrWillBeRawPtr<RequestCacheNamesCallbac k> callback) |
307 { | 345 { |
308 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, m_globalScope); | 346 LocalFrame* frame = findFrameWithSecurityOrigin(m_page, securityOrigin); |
pfeldman
2015/04/01 07:01:09
This one is important: going into the frame, throu
dmurph
2015/04/02 20:59:03
I think I understand what you mean. I removed scr
| |
347 DOMWindow* window = assertWindow(errorString, frame); | |
348 if (!window) { | |
349 callback->sendFailure(*errorString); | |
350 return; | |
351 } | |
352 ScriptState* scriptState = ScriptState::forMainWorld(frame); | |
353 WebServiceWorkerCacheStorage* cache = assertCacheStorage(errorString, script State, *window); | |
309 if (!cache) { | 354 if (!cache) { |
310 callback->sendFailure(*errorString); | 355 callback->sendFailure(*errorString); |
311 return; | 356 return; |
312 } | 357 } |
313 cache->dispatchKeys(new RequestCacheNames(callback)); | 358 cache->dispatchKeys(new RequestCacheNames(callback)); |
314 } | 359 } |
315 | 360 |
316 | 361 void InspectorServiceWorkerCacheAgent::requestEntries(ErrorString* errorString, const String& securityOrigin, const String& cacheName, int skipCount, int pageSi ze, PassRefPtrWillBeRawPtr<RequestEntriesCallback> callback) |
317 void InspectorServiceWorkerCacheAgent::requestEntries(ErrorString* errorString, const String& cacheName, int skipCount, int pageSize, PassRefPtrWillBeRawPtr<Req uestEntriesCallback> callback) | |
318 { | 362 { |
319 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, m_globalScope); | 363 LocalFrame* frame = findFrameWithSecurityOrigin(m_page, securityOrigin); |
364 DOMWindow* window = assertWindow(errorString, frame); | |
365 if (!window) { | |
366 callback->sendFailure(*errorString); | |
367 return; | |
368 } | |
369 ScriptState* scriptState = ScriptState::forMainWorld(frame); | |
370 WebServiceWorkerCacheStorage* cache = assertCacheStorage(errorString, script State, *window); | |
320 if (!cache) { | 371 if (!cache) { |
321 callback->sendFailure(*errorString); | 372 callback->sendFailure(*errorString); |
322 return; | 373 return; |
323 } | 374 } |
324 DataRequestParams params; | 375 DataRequestParams params; |
325 params.cacheName = cacheName; | 376 params.cacheName = cacheName; |
326 params.pageSize = pageSize; | 377 params.pageSize = pageSize; |
327 params.skipCount = skipCount; | 378 params.skipCount = skipCount; |
328 cache->dispatchOpen(new GetCacheForRequestData(params, callback), WebString( cacheName)); | 379 cache->dispatchOpen(new GetCacheForRequestData(params, callback), WebString( cacheName)); |
329 } | 380 } |
330 | 381 |
331 void InspectorServiceWorkerCacheAgent::deleteCache(ErrorString* errorString, con st String& cacheName, PassRefPtrWillBeRawPtr<DeleteCacheCallback> callback) | 382 void InspectorServiceWorkerCacheAgent::deleteCache(ErrorString* errorString, con st String& securityOrigin, const String& cacheName, PassRefPtrWillBeRawPtr<Delet eCacheCallback> callback) |
332 { | 383 { |
333 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, m_globalScope); | 384 LocalFrame* frame = findFrameWithSecurityOrigin(m_page, securityOrigin); |
385 DOMWindow* window = assertWindow(errorString, frame); | |
386 if (!window) { | |
387 callback->sendFailure(*errorString); | |
388 return; | |
389 } | |
390 ScriptState* scriptState = ScriptState::forMainWorld(frame); | |
391 WebServiceWorkerCacheStorage* cache = assertCacheStorage(errorString, script State, *window); | |
334 if (!cache) { | 392 if (!cache) { |
335 callback->sendFailure(*errorString); | 393 callback->sendFailure(*errorString); |
336 return; | 394 return; |
337 } | 395 } |
338 cache->dispatchDelete(new DeleteCache(callback), WebString(cacheName)); | 396 cache->dispatchDelete(new DeleteCache(callback), WebString(cacheName)); |
339 } | 397 } |
340 | 398 |
341 } // namespace blink | 399 } // namespace blink |
OLD | NEW |