Commit 232cb64e authored by kinuko@chromium.org's avatar kinuko@chromium.org

Add blink-side binding code for navigator.serviceWorker.current

Test code is basically copied from falken@'s:
https://github.com/mattto/ServiceWorker/tree/master/examples/layout-tests
(minus one obsolete FIXME, plus another FIXME)

BUG=366538
TEST=http/tests/serviceworker/current-on-load.html
TEST=http/tests/serviceworker/current-on-reload.html

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

git-svn-id: svn://svn.chromium.org/blink/trunk@173716 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent aa27ad17
...@@ -49,6 +49,7 @@ PASS window.cached_navigator_battery.onchargingchange is null ...@@ -49,6 +49,7 @@ PASS window.cached_navigator_battery.onchargingchange is null
PASS window.cached_navigator_battery.onchargingtimechange is null PASS window.cached_navigator_battery.onchargingtimechange is null
PASS window.cached_navigator_battery.ondischargingtimechange is null PASS window.cached_navigator_battery.ondischargingtimechange is null
PASS window.cached_navigator_battery.onlevelchange is null PASS window.cached_navigator_battery.onlevelchange is null
PASS window.cached_navigator_serviceWorker.current is null
PASS window.cached_performance.onwebkitresourcetimingbufferfull is null PASS window.cached_performance.onwebkitresourcetimingbufferfull is null
PASS window.cached_performance_navigation.redirectCount is 0 PASS window.cached_performance_navigation.redirectCount is 0
PASS window.cached_performance_navigation.type is 0 PASS window.cached_performance_navigation.type is 0
......
...@@ -49,6 +49,7 @@ PASS window.cached_navigator_battery.onchargingchange is null ...@@ -49,6 +49,7 @@ PASS window.cached_navigator_battery.onchargingchange is null
PASS window.cached_navigator_battery.onchargingtimechange is null PASS window.cached_navigator_battery.onchargingtimechange is null
PASS window.cached_navigator_battery.ondischargingtimechange is null PASS window.cached_navigator_battery.ondischargingtimechange is null
PASS window.cached_navigator_battery.onlevelchange is null PASS window.cached_navigator_battery.onlevelchange is null
PASS window.cached_navigator_serviceWorker.current is null
PASS window.cached_performance.onwebkitresourcetimingbufferfull is null PASS window.cached_performance.onwebkitresourcetimingbufferfull is null
PASS window.cached_performance_navigation.redirectCount is 0 PASS window.cached_performance_navigation.redirectCount is 0
PASS window.cached_performance_navigation.type is 0 PASS window.cached_performance_navigation.type is 0
......
...@@ -49,6 +49,7 @@ PASS window.cached_navigator_battery.onchargingchange is null ...@@ -49,6 +49,7 @@ PASS window.cached_navigator_battery.onchargingchange is null
PASS window.cached_navigator_battery.onchargingtimechange is null PASS window.cached_navigator_battery.onchargingtimechange is null
PASS window.cached_navigator_battery.ondischargingtimechange is null PASS window.cached_navigator_battery.ondischargingtimechange is null
PASS window.cached_navigator_battery.onlevelchange is null PASS window.cached_navigator_battery.onlevelchange is null
PASS window.cached_navigator_serviceWorker.current is null
PASS window.cached_performance.onwebkitresourcetimingbufferfull is null PASS window.cached_performance.onwebkitresourcetimingbufferfull is null
PASS window.cached_performance_navigation.redirectCount is 0 PASS window.cached_performance_navigation.redirectCount is 0
PASS window.cached_performance_navigation.type is 0 PASS window.cached_performance_navigation.type is 0
......
...@@ -56,6 +56,7 @@ PASS oldChildWindow.navigator.onLine is newChildWindow.navigator.onLine ...@@ -56,6 +56,7 @@ PASS oldChildWindow.navigator.onLine is newChildWindow.navigator.onLine
PASS oldChildWindow.navigator.platform is newChildWindow.navigator.platform PASS oldChildWindow.navigator.platform is newChildWindow.navigator.platform
PASS oldChildWindow.navigator.product is newChildWindow.navigator.product PASS oldChildWindow.navigator.product is newChildWindow.navigator.product
PASS oldChildWindow.navigator.productSub is newChildWindow.navigator.productSub PASS oldChildWindow.navigator.productSub is newChildWindow.navigator.productSub
PASS oldChildWindow.navigator.serviceWorker.current is newChildWindow.navigator.serviceWorker.current
PASS oldChildWindow.navigator.userAgent is newChildWindow.navigator.userAgent PASS oldChildWindow.navigator.userAgent is newChildWindow.navigator.userAgent
PASS oldChildWindow.navigator.vendor is newChildWindow.navigator.vendor PASS oldChildWindow.navigator.vendor is newChildWindow.navigator.vendor
PASS oldChildWindow.navigator.vendorSub is newChildWindow.navigator.vendorSub PASS oldChildWindow.navigator.vendorSub is newChildWindow.navigator.vendorSub
......
...@@ -56,6 +56,7 @@ PASS childWindow.navigator.onLine is window.navigator.onLine ...@@ -56,6 +56,7 @@ PASS childWindow.navigator.onLine is window.navigator.onLine
PASS childWindow.navigator.platform is window.navigator.platform PASS childWindow.navigator.platform is window.navigator.platform
PASS childWindow.navigator.product is window.navigator.product PASS childWindow.navigator.product is window.navigator.product
PASS childWindow.navigator.productSub is window.navigator.productSub PASS childWindow.navigator.productSub is window.navigator.productSub
FAIL childWindow.navigator.serviceWorker.current should be null. Threw exception TypeError: Cannot read property 'current' of null
PASS childWindow.navigator.userAgent is '' PASS childWindow.navigator.userAgent is ''
PASS childWindow.navigator.vendor is window.navigator.vendor PASS childWindow.navigator.vendor is window.navigator.vendor
PASS childWindow.navigator.vendorSub is '' PASS childWindow.navigator.vendorSub is ''
......
...@@ -55,6 +55,7 @@ PASS childWindow.navigator.onLine is window.navigator.onLine ...@@ -55,6 +55,7 @@ PASS childWindow.navigator.onLine is window.navigator.onLine
PASS childWindow.navigator.platform is window.navigator.platform PASS childWindow.navigator.platform is window.navigator.platform
PASS childWindow.navigator.product is window.navigator.product PASS childWindow.navigator.product is window.navigator.product
PASS childWindow.navigator.productSub is window.navigator.productSub PASS childWindow.navigator.productSub is window.navigator.productSub
PASS childWindow.navigator.serviceWorker.current is null
PASS childWindow.navigator.userAgent is '' PASS childWindow.navigator.userAgent is ''
PASS childWindow.navigator.vendor is window.navigator.vendor PASS childWindow.navigator.vendor is window.navigator.vendor
PASS childWindow.navigator.vendorSub is '' PASS childWindow.navigator.vendorSub is ''
......
FAIL current is set for a controlled document assert_unreached: Unregister should not fail: NotSupportedError Reached unreachable code
<!DOCTYPE html>
<title>Service Worker: Current on load</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('current is set for a controlled document');
t.step(function() {
var scope = 'resources/blank.html'
navigator.serviceWorker.unregister(scope).then(
doTest,
unreached_rejection(t, 'Unregister should not fail')
);
function doTest() {
navigator.serviceWorker.register(
'resources/worker-no-op.js', {scope: scope}
).then(
onRegister,
unreached_rejection(t, 'Registration should succeed, but failed')
);
}
function onRegister(worker) {
worker.addEventListener('statechange', t.step_func(function(event) {
if (event.target.state == 'active')
onActive();
}));
}
function onActive() {
with_iframe(scope, t.step_func(function(frame) {
var w = frame.contentWindow;
assert_true(w.navigator.serviceWorker.current instanceof w.ServiceWorker,
'current should be a ServiceWorker object');
t.done();
}));
}
});
</script>
</body>
FAIL current is set upon reload after registration assert_unreached: Unregister should not fail: NotSupportedError Reached unreachable code
<!DOCTYPE html>
<title>Service Worker: Current on reload</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="resources/test-helpers.js"></script>
<script src="resources/testutils.js"></script>
<body>
<script>
var t = async_test('current is set upon reload after registration');
t.step(function() {
var reloaded = false;
var scope = 'resources/blank.html';
navigator.serviceWorker.unregister(scope).then(
doTest,
unreached_rejection(t, 'Unregister should not fail')
);
function doTest() {
with_iframe(scope, t.step_func(onIframeLoad));
}
function onIframeLoad(frame) {
var w = frame.contentWindow;
function onActive(event) {
assert_equals(w.navigator.serviceWorker.current, null,
'current should be null until the document is reloaded');
reloaded = true;
w.location.reload();
}
function onRegister(worker) {
worker.addEventListener('statechange', t.step_func(function(event) {
if (event.target.state == 'active')
onActive();
}));
}
if (reloaded) {
assert_true(w.navigator.serviceWorker.current instanceof w.ServiceWorker,
'current should be a ServiceWorker object upon reload');
t.done();
return;
}
w.navigator.serviceWorker.register(
'worker-no-op.js', {scope: 'blank.html'}
).then(
t.step_func(onRegister),
unreached_rejection(t, 'Registration should succeed, but failed')
);
}
});
</script>
</body>
...@@ -10,9 +10,8 @@ function service_worker_test(url, description) { ...@@ -10,9 +10,8 @@ function service_worker_test(url, description) {
messageChannel.port1.onmessage = t.step_func(onMessage); messageChannel.port1.onmessage = t.step_func(onMessage);
worker.postMessage({port:messageChannel.port2}, [messageChannel.port2]); worker.postMessage({port:messageChannel.port2}, [messageChannel.port2]);
}), }),
t.step_func(function(reason) { unreached_rejection(t, 'Registration should succeed, but failed')
assert_unreached('Registration should succeed, but failed: ' + reason.name); );
}));
function onMessage(e) { function onMessage(e) {
assert_equals(e.data, 'pass'); assert_equals(e.data, 'pass');
...@@ -38,3 +37,13 @@ function unreached_rejection(test, prefix) { ...@@ -38,3 +37,13 @@ function unreached_rejection(test, prefix) {
assert_unreached(prefix + ': ' + reason.name); assert_unreached(prefix + ': ' + reason.name);
}); });
} }
// FIXME: Clean up the iframe when the test completes.
function with_iframe(url, f) {
var frame = document.createElement('iframe');
frame.src = url;
frame.onload = function() {
f(frame);
};
document.body.appendChild(frame);
}
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include "bindings/v8/ScriptWrappable.h" #include "bindings/v8/ScriptWrappable.h"
#include "core/dom/ContextLifecycleObserver.h" #include "core/dom/ContextLifecycleObserver.h"
#include "core/frame/DOMWindowLifecycleObserver.h" #include "core/frame/DOMWindowLifecycleObserver.h"
#include "modules/serviceworkers/ServiceWorker.h"
#include "public/platform/WebServiceWorkerProviderClient.h" #include "public/platform/WebServiceWorkerProviderClient.h"
#include "wtf/Forward.h" #include "wtf/Forward.h"
#include "wtf/PassRefPtr.h" #include "wtf/PassRefPtr.h"
...@@ -66,6 +67,8 @@ public: ...@@ -66,6 +67,8 @@ public:
ScriptPromise registerServiceWorker(ExecutionContext*, const String& pattern, const Dictionary&); ScriptPromise registerServiceWorker(ExecutionContext*, const String& pattern, const Dictionary&);
ScriptPromise unregisterServiceWorker(ExecutionContext*, const String& scope = String()); ScriptPromise unregisterServiceWorker(ExecutionContext*, const String& scope = String());
PassRefPtr<ServiceWorker> current() { return m_current; }
// WebServiceWorkerProviderClient overrides. // WebServiceWorkerProviderClient overrides.
virtual void setCurrentServiceWorker(blink::WebServiceWorker*) OVERRIDE; virtual void setCurrentServiceWorker(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;
......
...@@ -33,4 +33,6 @@ ...@@ -33,4 +33,6 @@
] interface ServiceWorkerContainer { ] interface ServiceWorkerContainer {
[CallWith=ExecutionContext, ImplementedAs=registerServiceWorker] Promise register(DOMString url, optional Dictionary options); [CallWith=ExecutionContext, ImplementedAs=registerServiceWorker] Promise register(DOMString url, optional Dictionary options);
[CallWith=ExecutionContext, ImplementedAs=unregisterServiceWorker] Promise unregister(optional DOMString scope); [CallWith=ExecutionContext, ImplementedAs=unregisterServiceWorker] Promise unregister(optional DOMString scope);
readonly attribute ServiceWorker current;
}; };
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