Commit bf08f95f authored by Istiaque Ahmed's avatar Istiaque Ahmed Committed by Commit Bot

Make runtime.onInstalled work for Service Worker based extensions.

runtime.onInstalled depends on Dispatch*WithLazyListener. For
Service Workers, this wouldn't dispatch the event as the event
listener doens't exist. For event pages, this works as we
manually add a listener before calling Dispatch. This CL does
the same for service worker based extensions.

Bug: 888798
Change-Id: I75dddc32613862464ba2bb73fd972ec1b0d2b03a
Reviewed-on: https://chromium-review.googlesource.com/1247190Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Commit-Queue: Istiaque Ahmed <lazyboy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#595679}
parent 5cbcd475
...@@ -297,6 +297,13 @@ IN_PROC_BROWSER_TEST_P(ServiceWorkerBasedBackgroundTest, Basic) { ...@@ -297,6 +297,13 @@ IN_PROC_BROWSER_TEST_P(ServiceWorkerBasedBackgroundTest, Basic) {
EXPECT_TRUE(newtab_listener.WaitUntilSatisfied()); EXPECT_TRUE(newtab_listener.WaitUntilSatisfied());
} }
// Tests chrome.runtime.onInstalled fires for extension service workers.
IN_PROC_BROWSER_TEST_P(ServiceWorkerBasedBackgroundTest, OnInstalledEvent) {
ASSERT_TRUE(RunExtensionTest(
"service_worker/worker_based_background/events_on_installed"))
<< message_;
}
// Class that dispatches an event to |extension_id| right after a // Class that dispatches an event to |extension_id| right after a
// non-lazy listener to the event is added from the extension's Service Worker. // non-lazy listener to the event is added from the extension's Service Worker.
class EarlyWorkerMessageSender : public EventRouter::Observer { class EarlyWorkerMessageSender : public EventRouter::Observer {
......
{
"name": "Service worker with chrome.runtime.onInstalled event",
"version": "1.0",
"manifest_version": 2,
"background": {"service_worker_script": "service_worker_background.js"}
}
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
chrome.runtime.onInstalled.addListener(function() {
chrome.test.succeed();
});
...@@ -513,13 +513,37 @@ void EventRouter::DispatchEventToExtension(const std::string& extension_id, ...@@ -513,13 +513,37 @@ void EventRouter::DispatchEventToExtension(const std::string& extension_id,
void EventRouter::DispatchEventWithLazyListener(const std::string& extension_id, void EventRouter::DispatchEventWithLazyListener(const std::string& extension_id,
std::unique_ptr<Event> event) { std::unique_ptr<Event> event) {
DCHECK(!extension_id.empty()); DCHECK(!extension_id.empty());
const Extension* extension = ExtensionRegistry::Get(browser_context_)
->enabled_extensions()
.GetByID(extension_id);
if (!extension)
return;
const bool is_service_worker_based_background =
BackgroundInfo::IsServiceWorkerBased(extension);
std::string event_name = event->event_name; std::string event_name = event->event_name;
bool has_listener = ExtensionHasEventListener(extension_id, event_name); const bool has_listener = ExtensionHasEventListener(extension_id, event_name);
if (!has_listener) if (!has_listener) {
AddLazyEventListener(event_name, extension_id); if (is_service_worker_based_background) {
AddLazyServiceWorkerEventListener(
event_name, extension_id,
Extension::GetBaseURLFromExtensionId(extension_id));
} else {
AddLazyEventListener(event_name, extension_id);
}
}
DispatchEventToExtension(extension_id, std::move(event)); DispatchEventToExtension(extension_id, std::move(event));
if (!has_listener)
RemoveLazyEventListener(event_name, extension_id); if (!has_listener) {
if (is_service_worker_based_background) {
RemoveLazyServiceWorkerEventListener(
event_name, extension_id,
Extension::GetBaseURLFromExtensionId(extension_id));
} else {
RemoveLazyEventListener(event_name, extension_id);
}
}
} }
void EventRouter::DispatchEventImpl(const std::string& restrict_to_extension_id, void EventRouter::DispatchEventImpl(const std::string& restrict_to_extension_id,
......
...@@ -373,7 +373,7 @@ ...@@ -373,7 +373,7 @@
"runtime": { "runtime": {
"channel": "stable", "channel": "stable",
"extension_types": ["extension", "legacy_packaged_app", "platform_app"], "extension_types": ["extension", "legacy_packaged_app", "platform_app"],
"contexts": ["blessed_extension", "lock_screen_extension"] "contexts": ["blessed_extension", "lock_screen_extension", "extension_service_worker"]
}, },
"runtime.getManifest": { "runtime.getManifest": {
"contexts": [ "contexts": [
......
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