Chromium Code Reviews| 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 : m_navigator(navigator) |
| 52 { | 57 { |
| 53 } | 58 } |
| 54 | 59 |
| 55 NavigatorServiceWorker::~NavigatorServiceWorker() | 60 NavigatorServiceWorker::~NavigatorServiceWorker() |
| 56 { | 61 { |
| 57 } | 62 } |
| 58 | 63 |
| 59 const char* NavigatorServiceWorker::supplementName() | 64 const char* NavigatorServiceWorker::supplementName() |
| 60 { | 65 { |
| 61 return "NavigatorServiceWorker"; | 66 return "NavigatorServiceWorker"; |
| 62 } | 67 } |
| 63 | 68 |
| 69 WebServiceWorkerProvider* NavigatorServiceWorker::serviceWorkerProvider() | |
| 70 { | |
| 71 if (!m_provider) { | |
| 72 Frame* frame = m_navigator->frame(); | |
| 73 if (!frame) | |
| 74 return 0; | |
| 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 KURL documentUrl = frame->document()->url(); | |
| 109 RefPtr<SecurityOrigin> documentOrigin = SecurityOrigin::create(documentUrl); | |
|
abarth-chromium
2013/10/11 18:08:14
Instead of creating a new security origin from the
alecflett
2013/10/11 18:52:23
Done.
| |
| 110 | |
| 111 KURL patternUrl = scriptExecutionContext->completeURL(pattern); | |
| 112 if (SecurityOrigin::create(patternUrl)->equal(documentOrigin.get())) { | |
|
abarth-chromium
2013/10/11 18:08:14
It's extremely rare that you want to call Security
alecflett
2013/10/11 18:52:23
Thanks I really wasn't sure what was appropriate t
| |
| 113 es.throwSecurityError("Can only register for patterns in the document's origin."); | |
| 114 return ScriptPromise(); | |
| 115 } | |
| 116 | |
| 117 KURL scriptUrl = scriptExecutionContext->completeURL(scriptSrc); | |
| 118 if (SecurityOrigin::create(scriptUrl)->equal(documentOrigin.get())) { | |
|
abarth-chromium
2013/10/11 18:08:14
Ditto.
| |
| 119 es.throwSecurityError("Script must be in document's origin."); | |
| 120 return ScriptPromise(); | |
| 121 } | |
| 122 | |
| 87 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tExecutionContext); | 123 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tExecutionContext); |
| 88 | 124 WebKit::WebServiceWorkerProvider* provider = serviceWorkerProvider(); |
| 89 if (peer) | 125 if (provider) |
|
abarth-chromium
2013/10/11 18:08:14
How can this function return |0|? The only case w
| |
| 90 peer->registerServiceWorker(pattern, scriptUrl, new CallbackPromiseAdapt er(resolver, scriptExecutionContext)); | 126 provider->registerServiceWorker(patternUrl, scriptUrl, new CallbackPromi seAdapter(resolver, scriptExecutionContext)); |
| 91 else | 127 else |
| 92 resolver->reject(PassRefPtr<ServiceWorker>(0)); | 128 resolver->reject(ScriptValue()); |
|
abarth-chromium
2013/10/11 18:08:14
I don't think this case can ever occur.
| |
| 93 // call here? | 129 // call here? |
| 94 return resolver->promise(); | 130 return resolver->promise(); |
| 95 } | 131 } |
| 96 | 132 |
| 97 ScriptPromise NavigatorServiceWorker::unregisterServiceWorker(ScriptExecutionCon text* context, Navigator* navigator, const String& pattern, ExceptionState& es) | 133 ScriptPromise NavigatorServiceWorker::unregisterServiceWorker(ScriptExecutionCon text* context, Navigator* navigator, const String& pattern, ExceptionState& es) |
| 98 { | 134 { |
| 99 return from(navigator)->unregisterServiceWorker(context, pattern, es); | 135 return from(navigator)->unregisterServiceWorker(context, pattern, es); |
| 100 } | 136 } |
| 101 | 137 |
| 102 ScriptPromise NavigatorServiceWorker::unregisterServiceWorker(ScriptExecutionCon text* scriptExecutionContext, const String& pattern, ExceptionState& es) | 138 ScriptPromise NavigatorServiceWorker::unregisterServiceWorker(ScriptExecutionCon text* scriptExecutionContext, const String& pattern, ExceptionState& es) |
| 103 { | 139 { |
| 104 ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled()); | 140 ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled()); |
| 105 FrameLoaderClient* client = m_navigator->frame()->loader()->client(); | 141 Frame* frame = m_navigator->frame(); |
| 106 WebKit::WebServiceWorkerRegistry* peer = client->serviceWorkerRegistry(); | 142 if (!frame) { |
| 143 es.throwDOMException(InvalidStateError, "No document available."); | |
| 144 return ScriptPromise(); | |
| 145 } | |
| 146 | |
| 147 KURL documentUrl = frame->document()->url(); | |
| 148 RefPtr<SecurityOrigin> documentOrigin = SecurityOrigin::create(documentUrl); | |
|
abarth-chromium
2013/10/11 18:08:14
Same problem.
| |
| 149 | |
| 150 KURL patternUrl = scriptExecutionContext->completeURL(pattern); | |
| 151 if (SecurityOrigin::create(patternUrl)->equal(documentOrigin.get())) { | |
|
abarth-chromium
2013/10/11 18:08:14
ditto
| |
| 152 es.throwSecurityError("Can only unregister for patterns in the document' s origin."); | |
| 153 return ScriptPromise(); | |
| 154 } | |
| 155 | |
| 156 WebKit::WebServiceWorkerProvider* provider = serviceWorkerProvider(); | |
| 107 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tExecutionContext); | 157 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tExecutionContext); |
| 108 if (peer) | 158 if (provider) |
|
abarth-chromium
2013/10/11 18:08:14
ditto
| |
| 109 peer->unregisterServiceWorker(pattern, new CallbackPromiseAdapter(resolv er, scriptExecutionContext)); | 159 provider->unregisterServiceWorker(patternUrl, new CallbackPromiseAdapter (resolver, scriptExecutionContext)); |
| 110 else | 160 else |
| 111 resolver->reject(PassRefPtr<ServiceWorker>(0)); | 161 resolver->reject(ScriptValue()); |
| 112 return resolver->promise(); | 162 return resolver->promise(); |
| 113 } | 163 } |
| 114 | 164 |
| 115 } // namespace WebCore | 165 } // namespace WebCore |
| OLD | NEW |