Commit 265dddeb authored by dominicc@chromium.org's avatar dominicc@chromium.org

Make navigator.serviceWorker.ready resolve with the controller.

The specification for ready is here:
https://github.com/slightlyoff/ServiceWorker/issues/223

ready is a convenience that lets the page talk to an installed
ServiceWorker, without having to track the details of whether that
Service Worker is controlling the page, or merely installed but
waiting for the page to reload.

This implements the trivial ready case, where the page is
controlled by a Service Worker and ready can produce that one
immediately.

BUG=363967

Review URL: https://codereview.chromium.org/309233002

git-svn-id: svn://svn.chromium.org/blink/trunk@175486 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent c7bee909
This is a testharness.js-based test.
FAIL ready in a controlled document assert_unreached: Unregister should not fail: NotSupportedError Reached unreachable code
Harness: the test ran to completion.
<!DOCTYPE html>
<title>Service Worker: ready in a controlled document</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="resources/test-helpers.js"></script>
<body>
<script>
var t = async_test('ready in a controlled document');
t.step(function() {
var url = 'resources/worker-no-op.js';
var scope = 'resources/blank.html';
service_worker_unregister_and_register(t, url, scope, onRegister);
function onRegister(worker) {
worker.addEventListener('statechange', t.step_func(function(event) {
if (event.target.state == 'active')
onActive();
}));
}
var controller;
function onActive() {
with_iframe(scope, t.step_func(function(frame) {
var w = frame.contentWindow;
controller = w.navigator.serviceWorker.controller;
w.navigator.serviceWorker.ready.then(t.step_func(onReady));
}));
}
function onReady(serviceWorker) {
assert_equals(serviceWorker, controller,
'ready should resolve to the controller of a ' +
'controlled document');
service_worker_unregister_and_done(t, scope);
}
});
</script>
This is a testharness.js-based test.
PASS ready in a controlled document
Harness: the test ran to completion.
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include "RuntimeEnabledFeatures.h" #include "RuntimeEnabledFeatures.h"
#include "bindings/v8/CallbackPromiseAdapter.h" #include "bindings/v8/CallbackPromiseAdapter.h"
#include "bindings/v8/ScriptPromise.h"
#include "bindings/v8/ScriptPromiseResolverWithContext.h" #include "bindings/v8/ScriptPromiseResolverWithContext.h"
#include "bindings/v8/ScriptState.h" #include "bindings/v8/ScriptState.h"
#include "bindings/v8/SerializedScriptValue.h" #include "bindings/v8/SerializedScriptValue.h"
...@@ -141,6 +142,19 @@ ScriptPromise ServiceWorkerContainer::unregisterServiceWorker(ScriptState* scrip ...@@ -141,6 +142,19 @@ ScriptPromise ServiceWorkerContainer::unregisterServiceWorker(ScriptState* scrip
return promise; return promise;
} }
ScriptPromise ServiceWorkerContainer::ready(ScriptState* scriptState)
{
if (m_controller.get()) {
RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
ScriptPromise promise = resolver->promise();
resolver->resolve(m_controller.get());
return promise;
}
// FIXME: Elaborate the implementation when the "waiting" property
// or replace() is implemented.
return ScriptPromise();
}
void ServiceWorkerContainer::setCurrentServiceWorker(blink::WebServiceWorker* serviceWorker) void ServiceWorkerContainer::setCurrentServiceWorker(blink::WebServiceWorker* serviceWorker)
{ {
setController(serviceWorker); setController(serviceWorker);
......
...@@ -64,11 +64,12 @@ public: ...@@ -64,11 +64,12 @@ public:
void detachClient(); void detachClient();
PassRefPtrWillBeRawPtr<ServiceWorker> controller() { return m_controller.get(); }
ScriptPromise ready(ScriptState*);
ScriptPromise registerServiceWorker(ScriptState*, const String& pattern, const Dictionary&); ScriptPromise registerServiceWorker(ScriptState*, const String& pattern, const Dictionary&);
ScriptPromise unregisterServiceWorker(ScriptState*, const String& scope = String()); ScriptPromise unregisterServiceWorker(ScriptState*, const String& scope = String());
PassRefPtrWillBeRawPtr<ServiceWorker> controller() { return m_controller.get(); }
// WebServiceWorkerProviderClient overrides. // WebServiceWorkerProviderClient overrides.
virtual void setController(blink::WebServiceWorker*) OVERRIDE; virtual void setController(blink::WebServiceWorker*) OVERRIDE;
virtual void dispatchMessageEvent(const blink::WebString& message, const blink::WebMessagePortChannelArray&) OVERRIDE; virtual void dispatchMessageEvent(const blink::WebString& message, const blink::WebMessagePortChannelArray&) OVERRIDE;
......
...@@ -31,8 +31,10 @@ ...@@ -31,8 +31,10 @@
[ [
RuntimeEnabled=ServiceWorker RuntimeEnabled=ServiceWorker
] interface ServiceWorkerContainer { ] interface ServiceWorkerContainer {
readonly attribute ServiceWorker controller;
[CallWith=ScriptState] readonly attribute Promise ready;
[CallWith=ScriptState, ImplementedAs=registerServiceWorker] Promise register(DOMString url, optional Dictionary options); [CallWith=ScriptState, ImplementedAs=registerServiceWorker] Promise register(DOMString url, optional Dictionary options);
[CallWith=ScriptState, ImplementedAs=unregisterServiceWorker] Promise unregister(optional DOMString scope); [CallWith=ScriptState, ImplementedAs=unregisterServiceWorker] Promise unregister(optional DOMString scope);
readonly attribute ServiceWorker controller;
}; };
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment