#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "extensions/browser/api/messaging/message_service.h"
#include <stdint.h>
#include <limits>
#include <memory>
#include <utility>
#include "base/containers/contains.h"
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/overloaded.h"
#include "base/json/json_writer.h"
#include "base/lazy_instance.h"
#include "base/ranges/algorithm.h"
#include "base/values.h"
#include "build/build_config.h"
#include "components/back_forward_cache/back_forward_cache_disable.h"
#include "content/public/browser/back_forward_cache.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/child_process_host.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/api/extensions_api_client.h"
#include "extensions/browser/api/messaging/channel_endpoint.h"
#include "extensions/browser/api/messaging/extension_message_port.h"
#include "extensions/browser/api/messaging/message_port.h"
#include "extensions/browser/api/messaging/messaging_delegate.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_api_frame_id_map.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/extension_util.h"
#include "extensions/browser/extension_web_contents_observer.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/browser/guest_view/web_view/web_view_guest.h"
#include "extensions/browser/message_service_api.h"
#include "extensions/browser/pref_names.h"
#include "extensions/browser/process_manager.h"
#include "extensions/common/api/messaging/messaging_endpoint.h"
#include "extensions/common/api/messaging/port_context.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/manifest_handlers/background_info.h"
#include "extensions/common/manifest_handlers/externally_connectable.h"
#include "extensions/common/manifest_handlers/incognito_info.h"
#include "extensions/common/mojom/message_port.mojom-shared.h"
#include "extensions/common/permissions/permissions_data.h"
#include "url/gurl.h"
BrowserContext;
BrowserThread;
SiteInstance;
WebContents;
namespace extensions {
namespace {
const char kReceivingEndDoesntExistError[] = …;
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || \
BUILDFLAG(IS_CHROMEOS)
const char kMissingPermissionError[] = …;
const char kProhibitedByPoliciesError[] = …;
#endif
LazyContextId LazyContextIdFor(content::BrowserContext* browser_context,
const Extension* extension) { … }
const Extension* GetExtensionForNativeAppChannel(
const ChannelEndpoint& source) { … }
void MaybeDisableBackForwardCacheForMessaging(content::RenderFrameHost* host) { … }
constexpr const char* kDefaultSWExtendedLifetimeList[] = …;
std::vector<url::Origin> GetServiceWorkerExtendedLifetimeOrigins(
content::BrowserContext* browser_context) { … }
}
struct MessageService::MessageChannel { … };
struct MessageService::OpenChannelParams { … };
MessageService::MessageService(BrowserContext* context)
: … { … }
MessageService::~MessageService() { … }
class MessageServiceFactory
: public BrowserContextKeyedAPIFactory<MessageService>,
public MessageServiceApi { … };
static base::LazyInstance<MessageServiceFactory>::DestructorAtExit g_factory = …;
BrowserContextKeyedAPIFactory<MessageService>*
MessageService::GetFactoryInstance() { … }
MessageService* MessageService::Get(BrowserContext* context) { … }
void MessageService::OpenChannelToExtension(
const ChannelEndpoint& source,
const PortId& source_port_id,
const MessagingEndpoint& source_endpoint,
std::unique_ptr<MessagePort> opener_port,
const std::string& target_extension_id,
const GURL& source_url,
mojom::ChannelType channel_type,
const std::string& channel_name) { … }
void MessageService::OpenChannelToNativeAppImpl(
const ChannelEndpoint& source,
const PortId& source_port_id,
const std::string& native_app_name,
mojo::PendingAssociatedRemote<extensions::mojom::MessagePort> port,
mojo::PendingAssociatedReceiver<extensions::mojom::MessagePortHost>
port_host) { … }
void MessageService::OpenChannelToTabImpl(
const ChannelEndpoint& source,
const PortId& source_port_id,
int tab_id,
int frame_id,
const std::string& document_id,
const ExtensionId& extension_id,
mojom::ChannelType channel_type,
const std::string& channel_name,
mojo::PendingAssociatedRemote<extensions::mojom::MessagePort> port,
mojo::PendingAssociatedReceiver<extensions::mojom::MessagePortHost>
port_host) { … }
void MessageService::OpenChannelImpl(BrowserContext* browser_context,
std::unique_ptr<OpenChannelParams> params,
const Extension* target_extension,
bool did_enqueue) { … }
void MessageService::AddChannel(std::unique_ptr<MessageChannel> channel,
const PortId& receiver_port_id) { … }
void MessageService::OpenPortImpl(const PortId& port_id,
int process_id,
const PortContext& port_context) { … }
void MessageService::CloseChannel(const PortId& port_id,
const std::string& error_message) { … }
void MessageService::ClosePort(const PortId& port_id,
int process_id,
const PortContext& port_context,
bool force_close) { … }
void MessageService::ClosePortImpl(const PortId& port_id,
int process_id,
int routing_id,
int worker_thread_id,
bool force_close,
const std::string& error_message) { … }
void MessageService::CloseChannelImpl(MessageChannelMap::iterator channel_iter,
const PortId& closing_port_id,
const std::string& error_message,
bool notify_other_port) { … }
void MessageService::PostMessage(const PortId& source_port_id,
const Message& message) { … }
void MessageService::EnqueuePendingMessage(const PortId& source_port_id,
const ChannelId& channel_id,
const Message& message) { … }
void MessageService::EnqueuePendingMessageForLazyBackgroundLoad(
const PortId& source_port_id,
const ChannelId& channel_id,
const Message& message) { … }
void MessageService::DispatchMessage(const PortId& source_port_id,
MessageChannel* channel,
const Message& message) { … }
void MessageService::NotifyResponsePending(const PortId& port_id) { … }
bool MessageService::MaybeAddPendingLazyContextOpenChannelTask(
BrowserContext* context,
const Extension* extension,
std::unique_ptr<OpenChannelParams>* params,
const PendingMessagesQueue& pending_messages) { … }
void MessageService::OnOpenChannelAllowed(
std::unique_ptr<OpenChannelParams> params,
bool allowed) { … }
void MessageService::PendingLazyContextOpenChannel(
std::unique_ptr<OpenChannelParams> params,
std::unique_ptr<LazyContextTaskQueue::ContextInfo> context_info) { … }
void MessageService::DispatchPendingMessages(const PendingMessagesQueue& queue,
const ChannelId& channel_id) { … }
}