Commit e14cea54 authored by shimazu's avatar shimazu Committed by Commit bot

ServiceWorker: Shows a warning when adding event handlers asynchronously

As shown at 15 on [1], "The user agents are encouraged to show a warning that
the event listeners must be added on the very first evaluation of the worker
script.". This patch adds warning messages for the events.

[1]: https://w3c.github.io/ServiceWorker/spec/service_worker/index.html#run-service-worker-algorithm

BUG=594160

Review-Url: https://codereview.chromium.org/2319563003
Cr-Commit-Position: refs/heads/master@{#419967}
parent d5d0c8bf
Tests that a warining is shown in the console if addEventListner is called after initial evaluation of the service worker script.
Message count: 1
service-worker-lazy-addeventlistener.js:2 Event handler of 'install' event must be added on the initial evaluation of worker script.
setTimeout @ service-worker-lazy-addeventlistener.js:2
<html>
<head>
<script src="../inspector-test.js"></script>
<script src="../console-test.js"></script>
<script src="service-workers-test.js"></script>
<script>
function test()
{
var scriptURL = "http://127.0.0.1:8000/inspector/service-workers/resources/service-worker-lazy-addeventlistener.js";
var scope = "http://127.0.0.1:8000/inspector/service-workers/resources/lazy-scope";
InspectorTest.waitForConsoleMessages(1, () => {
InspectorTest.dumpConsoleMessages();
InspectorTest.completeTest();
});
InspectorTest.registerServiceWorker(scriptURL, scope);
}
</script>
</head>
<body onload="runTest()">
<p>Tests that a warining is shown in the console if addEventListner is called after initial evaluation of the service worker script.</p>
</body>
</html>
...@@ -160,13 +160,8 @@ void ServiceWorkerGlobalScope::setRegistration(std::unique_ptr<WebServiceWorkerR ...@@ -160,13 +160,8 @@ void ServiceWorkerGlobalScope::setRegistration(std::unique_ptr<WebServiceWorkerR
bool ServiceWorkerGlobalScope::addEventListenerInternal(const AtomicString& eventType, EventListener* listener, const AddEventListenerOptionsResolved& options) bool ServiceWorkerGlobalScope::addEventListenerInternal(const AtomicString& eventType, EventListener* listener, const AddEventListenerOptionsResolved& options)
{ {
if (m_didEvaluateScript) { if (m_didEvaluateScript) {
if (eventType == EventTypeNames::install) { String message = String::format("Event handler of '%s' event must be added on the initial evaluation of worker script.", eventType.utf8().data());
ConsoleMessage* consoleMessage = ConsoleMessage::create(JSMessageSource, WarningMessageLevel, "Event handler of 'install' event must be added on the initial evaluation of worker script."); addConsoleMessage(ConsoleMessage::create(JSMessageSource, WarningMessageLevel, message));
addConsoleMessage(consoleMessage);
} else if (eventType == EventTypeNames::activate) {
ConsoleMessage* consoleMessage = ConsoleMessage::create(JSMessageSource, WarningMessageLevel, "Event handler of 'activate' event must be added on the initial evaluation of worker script.");
addConsoleMessage(consoleMessage);
}
} }
return WorkerGlobalScope::addEventListenerInternal(eventType, listener, options); return WorkerGlobalScope::addEventListenerInternal(eventType, listener, options);
} }
......
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