1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
// 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 <string>
#include <unordered_set>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "base/supports_user_data.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host_observer.h"
#include "extensions/browser/service_worker/worker_id.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/mojom/message_port.mojom.h"
#include "extensions/common/mojom/service_worker.mojom.h"
#include "extensions/common/mojom/service_worker_host.mojom.h"
#include "mojo/public/cpp/bindings/associated_receiver.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "third_party/blink/public/common/tokens/tokens.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_database.mojom.h"
class GURL;
namespace base {
class UnguessableToken;
}
namespace content {
class BrowserContext;
class RenderProcessHost;
} // namespace content
namespace extensions {
class Extension;
class ExtensionFunctionDispatcher;
// This class is the host of service worker execution context for extension
// in the renderer process. Lives on the UI thread.
class ServiceWorkerHost :
public mojom::ServiceWorkerHost,
public content::RenderProcessHostObserver {
public:
explicit ServiceWorkerHost(
content::RenderProcessHost* render_process_host,
mojo::PendingAssociatedReceiver<mojom::ServiceWorkerHost> receiver);
ServiceWorkerHost(const ServiceWorkerHost&) = delete;
ServiceWorkerHost& operator=(const ServiceWorkerHost&) = delete;
~ServiceWorkerHost() override;
static ServiceWorkerHost* GetWorkerFor(const WorkerId& worker);
static void BindReceiver(
int render_process_id,
mojo::PendingAssociatedReceiver<mojom::ServiceWorkerHost> receiver);
// Returns all ServiceWorkerHosts associated with RenderProcessHost `rph`.
// Returns an empty vector if there are none.
static std::vector<ServiceWorkerHost*> GetServiceWorkerHostList(
content::RenderProcessHost* rph);
// mojom::ServiceWorkerHost:
void DidInitializeServiceWorkerContext(
const ExtensionId& extension_id,
int64_t service_worker_version_id,
int worker_thread_id,
const blink::ServiceWorkerToken& service_worker_token,
mojo::PendingAssociatedRemote<mojom::EventDispatcher> event_dispatcher)
override;
void DidStartServiceWorkerContext(
const ExtensionId& extension_id,
const base::UnguessableToken& activation_token,
const GURL& service_worker_scope,
int64_t service_worker_version_id,
int worker_thread_id) override;
void DidStopServiceWorkerContext(
const ExtensionId& extension_id,
const base::UnguessableToken& activation_token,
const GURL& service_worker_scope,
int64_t service_worker_version_id,
int worker_thread_id) override;
void RequestWorker(mojom::RequestParamsPtr params,
RequestWorkerCallback callback) override;
void WorkerResponseAck(const base::Uuid& request_uuid) override;
void OpenChannelToExtension(
extensions::mojom::ExternalConnectionInfoPtr info,
extensions::mojom::ChannelType channel_type,
const std::string& channel_name,
const PortId& port_id,
mojo::PendingAssociatedRemote<extensions::mojom::MessagePort> port,
mojo::PendingAssociatedReceiver<extensions::mojom::MessagePortHost>
port_host) override;
void OpenChannelToNativeApp(
const std::string& native_app_name,
const PortId& port_id,
mojo::PendingAssociatedRemote<extensions::mojom::MessagePort> port,
mojo::PendingAssociatedReceiver<extensions::mojom::MessagePortHost>
port_host) override;
void OpenChannelToTab(
int32_t tab_id,
int32_t frame_id,
const std::optional<std::string>& document_id,
extensions::mojom::ChannelType channel_type,
const std::string& channel_name,
const PortId& port_id,
mojo::PendingAssociatedRemote<extensions::mojom::MessagePort> port,
mojo::PendingAssociatedReceiver<extensions::mojom::MessagePortHost>
port_host) override;
// Sends a message to the service worker in the renderer to update its
// permissions.
void UpdateExtensionPermissions(const Extension& extension,
const PermissionSet& permissions);
// Returns the mojo channel to the service worker. It may be null
// if the service worker doesn't have a live service worker matching
// the version id.
mojom::ServiceWorker* GetServiceWorker();
mojo::AssociatedReceiver<mojom::ServiceWorkerHost>& receiver_for_testing() {
return receiver_;
}
// content::RenderProcessHostObserver implementation.
void RenderProcessExited(
content::RenderProcessHost* host,
const content::ChildProcessTerminationInfo& info) override;
private:
// Returns the browser context associated with the render process this
// `ServiceWorkerHost` belongs to.
content::BrowserContext* GetBrowserContext();
void RemoteDisconnected();
// Destroys this instance by removing it from the ServiceWorkerHostList.
void Destroy();
// This is safe because ServiceWorkerHost is tied to the life time of
// RenderProcessHost.
const raw_ptr<content::RenderProcessHost> render_process_host_;
std::unique_ptr<ExtensionFunctionDispatcher> dispatcher_;
mojo::AssociatedReceiver<mojom::ServiceWorkerHost> receiver_{this};
mojo::AssociatedRemote<mojom::ServiceWorker> remote_;
WorkerId worker_id_;
};
} // namespace extensions
#endif // EXTENSIONS_BROWSER_SERVICE_WORKER_SERVICE_WORKER_HOST_H_
|