forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert ExtensionHostMsg_DidInitializeServiceWorkerContext to mojo
This CL is step 1 for adding extensions.mojom.ServiceWorkerHost and converts ExtensionHostMsg_DidInitializeServiceWorkerContext to mojo as a part of step 2. ServiceWorkerHost and EventRouter for extensions should be associated each other and bound to the worker thread. Currently EventRouter mojo interface is Legacy IPC associated, so we follow below steps to avoid flaky. Steps: 1) Get an associated interface for mojom::ServiceWorkerHost from the IPC::SyncMessageFilter as mojom::EventRouter 2) Convert ExtensionHostMsg_* to mojom::ServiceWorkerHost. 3) Add an AssociatedInterfaceRegistry for service worker and obtain intefaces for both mojom::ServiceWorkerHost and mojom::EventRouter. Bug: 1364183 Change-Id: Ie546fb2df1b130ea9b4d2e5bd1f05cb871b49900 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3990164 Reviewed-by: Chris Bookholt <bookholt@chromium.org> Commit-Queue: Yeunjoo Choi <ychoi@igalia.com> Reviewed-by: Devlin Cronin <rdevlin.cronin@chromium.org> Reviewed-by: Dave Tapuska <dtapuska@chromium.org> Cr-Commit-Position: refs/heads/main@{#1113217}
- Loading branch information
Showing
11 changed files
with
189 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "extensions/browser/service_worker/service_worker_host.h" | ||
|
||
#include "content/public/browser/browser_context.h" | ||
#include "content/public/browser/render_process_host.h" | ||
#include "extensions/browser/extension_registry.h" | ||
#include "extensions/browser/process_map.h" | ||
#include "extensions/browser/service_worker_task_queue.h" | ||
|
||
namespace extensions { | ||
|
||
namespace { | ||
const void* const kUserDataKey = &kUserDataKey; | ||
} // namespace | ||
|
||
ServiceWorkerHost::ServiceWorkerHost( | ||
content::RenderProcessHost* render_process_host) | ||
: render_process_host_(render_process_host) { | ||
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | ||
} | ||
|
||
ServiceWorkerHost::~ServiceWorkerHost() = default; | ||
|
||
// static | ||
void ServiceWorkerHost::BindReceiver( | ||
int render_process_id, | ||
mojo::PendingAssociatedReceiver<mojom::ServiceWorkerHost> receiver) { | ||
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | ||
auto* render_process_host = | ||
content::RenderProcessHost::FromID(render_process_id); | ||
if (!render_process_host) { | ||
return; | ||
} | ||
|
||
auto* service_worker_host = static_cast<ServiceWorkerHost*>( | ||
render_process_host->GetUserData(kUserDataKey)); | ||
if (!service_worker_host) { | ||
auto new_host = std::make_unique<ServiceWorkerHost>(render_process_host); | ||
service_worker_host = new_host.get(); | ||
render_process_host->SetUserData(kUserDataKey, std::move(new_host)); | ||
} | ||
|
||
service_worker_host->receiver_.Bind(std::move(receiver)); | ||
service_worker_host->receiver_.reset_on_disconnect(); | ||
} | ||
|
||
void ServiceWorkerHost::DidInitializeServiceWorkerContext( | ||
const ExtensionId& extension_id, | ||
int64_t service_worker_version_id, | ||
int worker_thread_id) { | ||
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | ||
content::BrowserContext* browser_context = GetBrowserContext(); | ||
ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context); | ||
DCHECK(registry); | ||
if (!registry->enabled_extensions().GetByID(extension_id)) { | ||
// This can happen if the extension is unloaded at this point. Just | ||
// checking the extension process (as below) is insufficient because | ||
// tearing down processes is async and happens after extension unload. | ||
return; | ||
} | ||
|
||
int render_process_id = render_process_host_->GetID(); | ||
if (!ProcessMap::Get(browser_context) | ||
->Contains(extension_id, render_process_id)) { | ||
// We check the process in addition to the registry to guard against | ||
// situations in which an extension may still be enabled, but no longer | ||
// running in a given process. | ||
return; | ||
} | ||
|
||
ServiceWorkerTaskQueue::Get(browser_context) | ||
->DidInitializeServiceWorkerContext(render_process_id, extension_id, | ||
service_worker_version_id, | ||
worker_thread_id); | ||
} | ||
|
||
content::BrowserContext* ServiceWorkerHost::GetBrowserContext() { | ||
return render_process_host_->GetBrowserContext(); | ||
} | ||
|
||
} // namespace extensions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef EXTENSIONS_BROWSER_SERVICE_WORKER_SERVICE_WORKER_HOST_H_ | ||
#define EXTENSIONS_BROWSER_SERVICE_WORKER_SERVICE_WORKER_HOST_H_ | ||
|
||
#include "base/memory/raw_ptr.h" | ||
#include "base/supports_user_data.h" | ||
#include "extensions/common/extension_id.h" | ||
#include "extensions/common/mojom/service_worker_host.mojom.h" | ||
#include "mojo/public/cpp/bindings/associated_receiver.h" | ||
|
||
namespace content { | ||
class BrowserContext; | ||
class RenderProcessHost; | ||
} // namespace content | ||
|
||
namespace extensions { | ||
|
||
// This class is the host of service worker execution context for extension | ||
// in the renderer process. Lives on the UI thread. | ||
class ServiceWorkerHost : public base::SupportsUserData::Data, | ||
public mojom::ServiceWorkerHost { | ||
public: | ||
explicit ServiceWorkerHost(content::RenderProcessHost* render_process_host); | ||
ServiceWorkerHost(const ServiceWorkerHost&) = delete; | ||
ServiceWorkerHost& operator=(const ServiceWorkerHost&) = delete; | ||
~ServiceWorkerHost() override; | ||
|
||
static void BindReceiver( | ||
int render_process_id, | ||
mojo::PendingAssociatedReceiver<mojom::ServiceWorkerHost> receiver); | ||
|
||
// mojom::ServiceWorkerHost: | ||
void DidInitializeServiceWorkerContext(const ExtensionId& extension_id, | ||
int64_t service_worker_version_id, | ||
int worker_thread_id) override; | ||
|
||
private: | ||
// Returns the browser context associated with the render process this | ||
// `ServiceWorkerHost` belongs to. | ||
content::BrowserContext* GetBrowserContext(); | ||
|
||
// This is safe because ServiceWorkerHost is tied to the life time of | ||
// RenderProcessHost. | ||
const raw_ptr<content::RenderProcessHost> render_process_host_; | ||
|
||
mojo::AssociatedReceiver<mojom::ServiceWorkerHost> receiver_{this}; | ||
}; | ||
|
||
} // namespace extensions | ||
|
||
#endif // EXTENSIONS_BROWSER_SERVICE_WORKER_SERVICE_WORKER_HOST_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
module extensions.mojom; | ||
|
||
// An interface for an extension service worker context. Implemented in the | ||
// browser process. | ||
interface ServiceWorkerHost { | ||
// Tells the browser that an extension service worker context was initialized, | ||
// but possibly didn't start executing its top-level JavaScript. | ||
DidInitializeServiceWorkerContext(string extension_id, | ||
int64 service_worker_version_id, | ||
int32 worker_thread_id); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters