#include "extensions/browser/api/messaging/extension_message_port.h"
#include <memory>
#include <optional>
#include <set>
#include <utility>
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/notreached.h"
#include "base/scoped_observation.h"
#include "base/strings/strcat.h"
#include "base/types/optional_util.h"
#include "base/types/pass_key.h"
#include "components/back_forward_cache/back_forward_cache_disable.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/content_features.h"
#include "extensions/browser/api/messaging/channel_endpoint.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/extension_web_contents_observer.h"
#include "extensions/browser/process_manager.h"
#include "extensions/browser/process_manager_observer.h"
#include "extensions/browser/service_worker/service_worker_host.h"
#include "extensions/common/api/messaging/message.h"
#include "extensions/common/api/messaging/messaging_endpoint.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/manifest_handlers/background_info.h"
#include "extensions/common/mojom/message_port.mojom-shared.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
namespace extensions {
namespace {
std::string PortIdToString(const extensions::PortId& port_id) { … }
PassKey;
const char kReceivingEndDoesntExistError[] = …
"Could not establish connection. Receiving end does not exist.";
const char kClosedWhileResponsePendingError[] = …;
const char kClosedWhenPageEntersBFCache[] = …;
}
class ExtensionMessagePort::FrameTracker : public content::WebContentsObserver,
public ProcessManagerObserver { … };
ExtensionMessagePort::ExtensionMessagePort(
base::WeakPtr<ChannelDelegate> channel_delegate,
const PortId& port_id,
const ExtensionId& extension_id,
content::RenderFrameHost* render_frame_host,
bool include_child_frames)
: … { … }
std::unique_ptr<ExtensionMessagePort> ExtensionMessagePort::CreateForExtension(
base::WeakPtr<ChannelDelegate> channel_delegate,
const PortId& port_id,
const ExtensionId& extension_id,
content::BrowserContext* browser_context) { … }
std::unique_ptr<ExtensionMessagePort> ExtensionMessagePort::CreateForEndpoint(
base::WeakPtr<ChannelDelegate> channel_delegate,
const PortId& port_id,
const ExtensionId& extension_id,
const ChannelEndpoint& endpoint,
mojo::PendingAssociatedRemote<extensions::mojom::MessagePort> message_port,
mojo::PendingAssociatedReceiver<extensions::mojom::MessagePortHost>
message_port_host) { … }
ExtensionMessagePort::ExtensionMessagePort(
base::WeakPtr<ChannelDelegate> channel_delegate,
const PortId& port_id,
const ExtensionId& extension_id,
content::BrowserContext* browser_context,
PassKey)
: … { … }
ExtensionMessagePort::~ExtensionMessagePort() = default;
void ExtensionMessagePort::Prune() { … }
void ExtensionMessagePort::RemoveCommonFrames(const MessagePort& port) { … }
bool ExtensionMessagePort::HasFrame(
const content::GlobalRenderFrameHostToken& frame_token) const { … }
bool ExtensionMessagePort::IsValidPort() { … }
void ExtensionMessagePort::RevalidatePort() { … }
void ExtensionMessagePort::DispatchOnConnect(
mojom::ChannelType channel_type,
const std::string& channel_name,
std::optional<base::Value::Dict> source_tab,
const ExtensionApiFrameIdMap::FrameData& source_frame,
int guest_process_id,
int guest_render_frame_routing_id,
const MessagingEndpoint& source_endpoint,
const std::string& target_extension_id,
const GURL& source_url,
std::optional<url::Origin> source_origin) { … }
void ExtensionMessagePort::OnConnectResponse(bool success) { … }
void ExtensionMessagePort::DispatchOnDisconnect(
const std::string& error_message) { … }
void ExtensionMessagePort::DispatchOnMessage(const Message& message) { … }
void ExtensionMessagePort::IncrementLazyKeepaliveCount(
Activity::Type activity_type) { … }
void ExtensionMessagePort::DecrementLazyKeepaliveCount(
Activity::Type activity_type) { … }
void ExtensionMessagePort::NotifyResponsePending() { … }
void ExtensionMessagePort::OpenPort(int process_id,
const PortContext& port_context) { … }
void ExtensionMessagePort::ClosePort(int process_id,
int routing_id,
int worker_thread_id) { … }
void ExtensionMessagePort::CloseChannel(
std::optional<std::string> error_message) { … }
void ExtensionMessagePort::RegisterFrame(
content::RenderFrameHost* render_frame_host) { … }
bool ExtensionMessagePort::UnregisterFrame(
content::RenderFrameHost* render_frame_host) { … }
bool ExtensionMessagePort::UnregisterFrame(
const content::GlobalRenderFrameHostToken& frame_token) { … }
bool ExtensionMessagePort::UnregisterFramesUnderMainFrame(
content::RenderFrameHost* main_frame,
std::optional<std::string> error_message) { … }
void ExtensionMessagePort::RegisterWorker(const WorkerId& worker_id) { … }
bool ExtensionMessagePort::UnregisterWorker(const WorkerId& worker_id) { … }
void ExtensionMessagePort::UnregisterWorker(int render_process_id,
int worker_thread_id) { … }
void ExtensionMessagePort::SendToPort(SendCallback send_callback) { … }
bool ExtensionMessagePort::IsServiceWorkerActivity(
Activity::Type activity_type) { … }
bool ExtensionMessagePort::ShouldSkipFrameForBFCache(
content::RenderFrameHost* render_frame_host) { … }
}