OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 16 matching lines...) Expand all Loading... | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 #include "config.h" | 30 #include "config.h" |
31 #include "modules/serviceworkers/NavigatorServiceWorker.h" | 31 #include "modules/serviceworkers/NavigatorServiceWorker.h" |
32 | 32 |
33 #include "RuntimeEnabledFeatures.h" | 33 #include "RuntimeEnabledFeatures.h" |
34 #include "V8ServiceWorker.h" | 34 #include "V8ServiceWorker.h" |
35 #include "bindings/v8/ScriptPromiseResolver.h" | 35 #include "bindings/v8/ScriptPromiseResolver.h" |
36 #include "core/dom/Document.h" | 36 #include "core/dom/Document.h" |
37 #include "core/dom/ExceptionCode.h" | |
37 #include "core/loader/DocumentLoader.h" | 38 #include "core/loader/DocumentLoader.h" |
38 #include "core/loader/FrameLoaderClient.h" | 39 #include "core/loader/FrameLoaderClient.h" |
39 #include "core/page/Frame.h" | 40 #include "core/page/Frame.h" |
40 #include "core/page/Navigator.h" | 41 #include "core/page/Navigator.h" |
41 #include "core/workers/SharedWorker.h" | 42 #include "core/workers/SharedWorker.h" |
42 #include "modules/serviceworkers/CallbackPromiseAdapter.h" | 43 #include "modules/serviceworkers/CallbackPromiseAdapter.h" |
43 #include "modules/serviceworkers/ServiceWorker.h" | 44 #include "modules/serviceworkers/ServiceWorker.h" |
44 #include "public/platform/WebServiceWorkerRegistry.h" | 45 #include "public/platform/WebServiceWorkerProvider.h" |
46 #include "public/platform/WebServiceWorkerProviderClient.h" | |
45 #include "public/platform/WebString.h" | 47 #include "public/platform/WebString.h" |
46 #include "public/platform/WebURL.h" | 48 #include "public/platform/WebURL.h" |
47 | 49 |
50 using WebKit::WebServiceWorkerProvider; | |
51 using WebKit::WebString; | |
52 | |
48 namespace WebCore { | 53 namespace WebCore { |
49 | 54 |
50 NavigatorServiceWorker::NavigatorServiceWorker(Navigator* navigator) | 55 NavigatorServiceWorker::NavigatorServiceWorker(Navigator* navigator) |
51 : m_navigator(navigator) | 56 : DOMWindowProperty(navigator->frame()) |
abarth-chromium
2013/10/11 19:02:41
Yeah, that's fine.
| |
57 , m_navigator(navigator) | |
52 { | 58 { |
53 } | 59 } |
54 | 60 |
55 NavigatorServiceWorker::~NavigatorServiceWorker() | 61 NavigatorServiceWorker::~NavigatorServiceWorker() |
56 { | 62 { |
57 } | 63 } |
58 | 64 |
59 const char* NavigatorServiceWorker::supplementName() | 65 const char* NavigatorServiceWorker::supplementName() |
60 { | 66 { |
61 return "NavigatorServiceWorker"; | 67 return "NavigatorServiceWorker"; |
62 } | 68 } |
63 | 69 |
70 WebServiceWorkerProvider* NavigatorServiceWorker::ensureProvider() | |
71 { | |
72 if (!m_provider) { | |
73 Frame* frame = m_navigator->frame(); | |
74 ASSERT(frame); | |
abarth-chromium
2013/10/11 19:02:41
I would move this ASSERT above the "if (!m_provide
| |
75 | |
76 FrameLoaderClient* client = frame->loader()->client(); | |
77 // FIXME: This is temporarily hooked up here until we hook up to the loa ding process. | |
78 m_provider = client->createServiceWorkerProvider(nullptr); | |
79 } | |
80 return m_provider.get(); | |
81 } | |
82 | |
64 NavigatorServiceWorker* NavigatorServiceWorker::from(Navigator* navigator) | 83 NavigatorServiceWorker* NavigatorServiceWorker::from(Navigator* navigator) |
65 { | 84 { |
66 NavigatorServiceWorker* supplement = toNavigatorServiceWorker(navigator); | 85 NavigatorServiceWorker* supplement = toNavigatorServiceWorker(navigator); |
67 if (!supplement) { | 86 if (!supplement) { |
68 supplement = new NavigatorServiceWorker(navigator); | 87 supplement = new NavigatorServiceWorker(navigator); |
69 provideTo(navigator, supplementName(), adoptPtr(supplement)); | 88 provideTo(navigator, supplementName(), adoptPtr(supplement)); |
70 } | 89 } |
71 return supplement; | 90 return supplement; |
72 } | 91 } |
73 | 92 |
74 ScriptPromise NavigatorServiceWorker::registerServiceWorker(ScriptExecutionConte xt* context, Navigator* navigator, const String& pattern, const String& url, Exc eptionState& es) | 93 ScriptPromise NavigatorServiceWorker::registerServiceWorker(ScriptExecutionConte xt* context, Navigator* navigator, const String& pattern, const String& url, Exc eptionState& es) |
75 { | 94 { |
76 return from(navigator)->registerServiceWorker(context, pattern, url, es); | 95 return from(navigator)->registerServiceWorker(context, pattern, url, es); |
77 } | 96 } |
78 | 97 |
79 | |
80 ScriptPromise NavigatorServiceWorker::registerServiceWorker(ScriptExecutionConte xt* scriptExecutionContext, const String& pattern, const String& scriptSrc, Exce ptionState& es) | 98 ScriptPromise NavigatorServiceWorker::registerServiceWorker(ScriptExecutionConte xt* scriptExecutionContext, const String& pattern, const String& scriptSrc, Exce ptionState& es) |
81 { | 99 { |
82 ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled()); | 100 ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled()); |
83 FrameLoaderClient* client = m_navigator->frame()->loader()->client(); | 101 Frame* frame = m_navigator->frame(); |
84 // WTF? Surely there's a better way to resolve a url? | 102 |
85 KURL scriptUrl = m_navigator->frame()->document()->completeURL(scriptSrc); | 103 if (!frame) { |
86 WebKit::WebServiceWorkerRegistry* peer = client->serviceWorkerRegistry(); | 104 es.throwDOMException(InvalidStateError, "No document available."); |
105 return ScriptPromise(); | |
106 } | |
107 | |
108 RefPtr<SecurityOrigin> documentOrigin = frame->document()->securityOrigin(); | |
109 | |
110 KURL patternUrl = scriptExecutionContext->completeURL(pattern); | |
abarth-chromium
2013/10/11 19:02:41
patternUrl -> patternURL
| |
111 if (documentOrigin->canRequest(patternUrl)) { | |
112 es.throwSecurityError("Can only register for patterns in the document's origin."); | |
113 return ScriptPromise(); | |
114 } | |
115 | |
116 KURL scriptUrl = scriptExecutionContext->completeURL(scriptSrc); | |
abarth-chromium
2013/10/11 19:02:41
scriptUrl -> scriptURL
(Notice, for example, that
| |
117 if (documentOrigin->canRequest(scriptUrl)) { | |
118 es.throwSecurityError("Script must be in document's origin."); | |
119 return ScriptPromise(); | |
120 } | |
121 | |
87 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tExecutionContext); | 122 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tExecutionContext); |
88 | 123 ensureProvider()->registerServiceWorker(patternUrl, scriptUrl, new CallbackP romiseAdapter(resolver, scriptExecutionContext)); |
89 if (peer) | |
90 peer->registerServiceWorker(pattern, scriptUrl, new CallbackPromiseAdapt er(resolver, scriptExecutionContext)); | |
91 else | |
92 resolver->reject(PassRefPtr<ServiceWorker>(0)); | |
93 // call here? | |
94 return resolver->promise(); | 124 return resolver->promise(); |
95 } | 125 } |
96 | 126 |
97 ScriptPromise NavigatorServiceWorker::unregisterServiceWorker(ScriptExecutionCon text* context, Navigator* navigator, const String& pattern, ExceptionState& es) | 127 ScriptPromise NavigatorServiceWorker::unregisterServiceWorker(ScriptExecutionCon text* context, Navigator* navigator, const String& pattern, ExceptionState& es) |
98 { | 128 { |
99 return from(navigator)->unregisterServiceWorker(context, pattern, es); | 129 return from(navigator)->unregisterServiceWorker(context, pattern, es); |
100 } | 130 } |
101 | 131 |
102 ScriptPromise NavigatorServiceWorker::unregisterServiceWorker(ScriptExecutionCon text* scriptExecutionContext, const String& pattern, ExceptionState& es) | 132 ScriptPromise NavigatorServiceWorker::unregisterServiceWorker(ScriptExecutionCon text* scriptExecutionContext, const String& pattern, ExceptionState& es) |
103 { | 133 { |
104 ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled()); | 134 ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled()); |
105 FrameLoaderClient* client = m_navigator->frame()->loader()->client(); | 135 Frame* frame = m_navigator->frame(); |
106 WebKit::WebServiceWorkerRegistry* peer = client->serviceWorkerRegistry(); | 136 if (!frame) { |
137 es.throwDOMException(InvalidStateError, "No document available."); | |
138 return ScriptPromise(); | |
139 } | |
140 | |
141 RefPtr<SecurityOrigin> documentOrigin = frame->document()->securityOrigin(); | |
142 | |
143 KURL patternUrl = scriptExecutionContext->completeURL(pattern); | |
144 if (documentOrigin->canRequest(patternUrl)) { | |
145 es.throwSecurityError("Can only unregister for patterns in the document' s origin."); | |
146 return ScriptPromise(); | |
147 } | |
148 | |
107 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tExecutionContext); | 149 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tExecutionContext); |
108 if (peer) | 150 ensureProvider()->unregisterServiceWorker(patternUrl, new CallbackPromiseAda pter(resolver, scriptExecutionContext)); |
109 peer->unregisterServiceWorker(pattern, new CallbackPromiseAdapter(resolv er, scriptExecutionContext)); | |
110 else | |
111 resolver->reject(PassRefPtr<ServiceWorker>(0)); | |
112 return resolver->promise(); | 151 return resolver->promise(); |
113 } | 152 } |
114 | 153 |
154 void NavigatorServiceWorker::willDetachGlobalObjectFromFrame() | |
155 { | |
156 m_provider = nullptr; | |
157 DOMWindowProperty::willDetachGlobalObjectFromFrame(); | |
158 } | |
159 | |
160 | |
115 } // namespace WebCore | 161 } // namespace WebCore |
OLD | NEW |