Index: Source/modules/serviceworkers/NavigatorServiceWorker.cpp |
diff --git a/Source/modules/serviceworkers/NavigatorServiceWorker.cpp b/Source/modules/serviceworkers/NavigatorServiceWorker.cpp |
index 172589dabc8495c7d21d80b0122b92f60e03ac66..8d78b642fff1143b54212d9beb6a31e12a98c92a 100644 |
--- a/Source/modules/serviceworkers/NavigatorServiceWorker.cpp |
+++ b/Source/modules/serviceworkers/NavigatorServiceWorker.cpp |
@@ -34,6 +34,7 @@ |
#include "V8ServiceWorker.h" |
#include "bindings/v8/ScriptPromiseResolver.h" |
#include "core/dom/Document.h" |
+#include "core/dom/ExceptionCode.h" |
#include "core/loader/DocumentLoader.h" |
#include "core/loader/FrameLoaderClient.h" |
#include "core/page/Frame.h" |
@@ -41,10 +42,14 @@ |
#include "core/workers/SharedWorker.h" |
#include "modules/serviceworkers/CallbackPromiseAdapter.h" |
#include "modules/serviceworkers/ServiceWorker.h" |
-#include "public/platform/WebServiceWorkerRegistry.h" |
+#include "public/platform/WebServiceWorkerProvider.h" |
+#include "public/platform/WebServiceWorkerProviderClient.h" |
#include "public/platform/WebString.h" |
#include "public/platform/WebURL.h" |
+using WebKit::WebServiceWorkerProvider; |
+using WebKit::WebString; |
+ |
namespace WebCore { |
NavigatorServiceWorker::NavigatorServiceWorker(Navigator* navigator) |
@@ -61,6 +66,20 @@ const char* NavigatorServiceWorker::supplementName() |
return "NavigatorServiceWorker"; |
} |
+WebServiceWorkerProvider* NavigatorServiceWorker::serviceWorkerProvider() |
+{ |
+ if (!m_provider) { |
+ Frame* frame = m_navigator->frame(); |
+ if (!frame) |
+ return 0; |
+ |
+ FrameLoaderClient* client = frame->loader()->client(); |
+ // FIXME: This is temporarily hooked up here until we hook up to the loading process. |
+ m_provider = client->createServiceWorkerProvider(nullptr); |
+ } |
+ return m_provider.get(); |
+} |
+ |
NavigatorServiceWorker* NavigatorServiceWorker::from(Navigator* navigator) |
{ |
NavigatorServiceWorker* supplement = toNavigatorServiceWorker(navigator); |
@@ -76,20 +95,37 @@ ScriptPromise NavigatorServiceWorker::registerServiceWorker(ScriptExecutionConte |
return from(navigator)->registerServiceWorker(context, pattern, url, es); |
} |
- |
ScriptPromise NavigatorServiceWorker::registerServiceWorker(ScriptExecutionContext* scriptExecutionContext, const String& pattern, const String& scriptSrc, ExceptionState& es) |
{ |
ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled()); |
- FrameLoaderClient* client = m_navigator->frame()->loader()->client(); |
- // WTF? Surely there's a better way to resolve a url? |
- KURL scriptUrl = m_navigator->frame()->document()->completeURL(scriptSrc); |
- WebKit::WebServiceWorkerRegistry* peer = client->serviceWorkerRegistry(); |
- RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptExecutionContext); |
+ Frame* frame = m_navigator->frame(); |
+ |
+ if (!frame) { |
+ es.throwDOMException(InvalidStateError, "No document available."); |
+ return ScriptPromise(); |
+ } |
+ |
+ KURL documentUrl = frame->document()->url(); |
+ 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.
|
- if (peer) |
- peer->registerServiceWorker(pattern, scriptUrl, new CallbackPromiseAdapter(resolver, scriptExecutionContext)); |
+ KURL patternUrl = scriptExecutionContext->completeURL(pattern); |
+ 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
|
+ es.throwSecurityError("Can only register for patterns in the document's origin."); |
+ return ScriptPromise(); |
+ } |
+ |
+ KURL scriptUrl = scriptExecutionContext->completeURL(scriptSrc); |
+ if (SecurityOrigin::create(scriptUrl)->equal(documentOrigin.get())) { |
abarth-chromium
2013/10/11 18:08:14
Ditto.
|
+ es.throwSecurityError("Script must be in document's origin."); |
+ return ScriptPromise(); |
+ } |
+ |
+ RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptExecutionContext); |
+ WebKit::WebServiceWorkerProvider* provider = serviceWorkerProvider(); |
+ if (provider) |
abarth-chromium
2013/10/11 18:08:14
How can this function return |0|? The only case w
|
+ provider->registerServiceWorker(patternUrl, scriptUrl, new CallbackPromiseAdapter(resolver, scriptExecutionContext)); |
else |
- resolver->reject(PassRefPtr<ServiceWorker>(0)); |
+ resolver->reject(ScriptValue()); |
abarth-chromium
2013/10/11 18:08:14
I don't think this case can ever occur.
|
// call here? |
return resolver->promise(); |
} |
@@ -102,13 +138,27 @@ ScriptPromise NavigatorServiceWorker::unregisterServiceWorker(ScriptExecutionCon |
ScriptPromise NavigatorServiceWorker::unregisterServiceWorker(ScriptExecutionContext* scriptExecutionContext, const String& pattern, ExceptionState& es) |
{ |
ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled()); |
- FrameLoaderClient* client = m_navigator->frame()->loader()->client(); |
- WebKit::WebServiceWorkerRegistry* peer = client->serviceWorkerRegistry(); |
+ Frame* frame = m_navigator->frame(); |
+ if (!frame) { |
+ es.throwDOMException(InvalidStateError, "No document available."); |
+ return ScriptPromise(); |
+ } |
+ |
+ KURL documentUrl = frame->document()->url(); |
+ RefPtr<SecurityOrigin> documentOrigin = SecurityOrigin::create(documentUrl); |
abarth-chromium
2013/10/11 18:08:14
Same problem.
|
+ |
+ KURL patternUrl = scriptExecutionContext->completeURL(pattern); |
+ if (SecurityOrigin::create(patternUrl)->equal(documentOrigin.get())) { |
abarth-chromium
2013/10/11 18:08:14
ditto
|
+ es.throwSecurityError("Can only unregister for patterns in the document's origin."); |
+ return ScriptPromise(); |
+ } |
+ |
+ WebKit::WebServiceWorkerProvider* provider = serviceWorkerProvider(); |
RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptExecutionContext); |
- if (peer) |
- peer->unregisterServiceWorker(pattern, new CallbackPromiseAdapter(resolver, scriptExecutionContext)); |
+ if (provider) |
abarth-chromium
2013/10/11 18:08:14
ditto
|
+ provider->unregisterServiceWorker(patternUrl, new CallbackPromiseAdapter(resolver, scriptExecutionContext)); |
else |
- resolver->reject(PassRefPtr<ServiceWorker>(0)); |
+ resolver->reject(ScriptValue()); |
return resolver->promise(); |
} |