diff --git a/dom/base/nsContentUtils.cpp b/dom/base/nsContentUtils.cpp index 3a79aa1a5b16..4fc3cc43371c 100644 --- a/dom/base/nsContentUtils.cpp +++ b/dom/base/nsContentUtils.cpp @@ -3829,6 +3829,22 @@ nsContentUtils::DispatchChromeEvent(nsIDocument *aDoc, return rv; } +/* static */ +nsresult +nsContentUtils::DispatchFocusChromeEvent(nsPIDOMWindowOuter* aWindow) +{ + MOZ_ASSERT(aWindow); + + nsCOMPtr doc = aWindow->GetExtantDoc(); + if (!doc) { + return NS_ERROR_FAILURE; + } + + return DispatchChromeEvent(doc, aWindow, + NS_LITERAL_STRING("DOMServiceWorkerFocusClient"), + true, true); +} + nsresult nsContentUtils::DispatchEventOnlyToChrome(nsIDocument* aDoc, nsISupports* aTarget, diff --git a/dom/base/nsContentUtils.h b/dom/base/nsContentUtils.h index 949a79acf378..4a80a6caf3d6 100644 --- a/dom/base/nsContentUtils.h +++ b/dom/base/nsContentUtils.h @@ -1125,6 +1125,13 @@ public: bool aCancelable, bool *aDefaultAction = nullptr); + /** + * Helper function for dispatching a "DOMServiceWorkerFocusClient" event to + * the chrome event handler of the given DOM Window. This has the effect + * of focusing the corresponding tab and bringing the browser window + * to the foreground. + */ + static nsresult DispatchFocusChromeEvent(nsPIDOMWindowOuter* aWindow); /** * This method creates and dispatches a trusted event. diff --git a/dom/workers/ServiceWorkerClients.cpp b/dom/workers/ServiceWorkerClients.cpp index 70c00d85aaa6..48fff1425239 100644 --- a/dom/workers/ServiceWorkerClients.cpp +++ b/dom/workers/ServiceWorkerClients.cpp @@ -503,6 +503,11 @@ public: if (NS_SUCCEEDED(rv)) { MOZ_ASSERT(window); + rv = nsContentUtils::DispatchFocusChromeEvent(window); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + WorkerPrivate* workerPrivate = mPromiseProxy->GetWorkerPrivate(); MOZ_ASSERT(workerPrivate); diff --git a/dom/workers/ServiceWorkerWindowClient.cpp b/dom/workers/ServiceWorkerWindowClient.cpp index 70cfae95f46a..8b036f412b21 100644 --- a/dom/workers/ServiceWorkerWindowClient.cpp +++ b/dom/workers/ServiceWorkerWindowClient.cpp @@ -95,10 +95,7 @@ public: if (window) { nsCOMPtr doc = window->GetDocument(); if (doc) { - nsContentUtils::DispatchChromeEvent(doc, - window->GetOuterWindow(), - NS_LITERAL_STRING("DOMServiceWorkerFocusClient"), - true, true); + nsContentUtils::DispatchFocusChromeEvent(window->GetOuterWindow()); clientInfo.reset(new ServiceWorkerClientInfo(doc)); } }