Skip to content

Commit

Permalink
[extensions] Move IPC validation code into MessageService directly
Browse files Browse the repository at this point in the history
Add a new file messsage_service_bindings.cc that contains the IPC
validation code that was in messaging_api_message_filter.cc. This
will be used as common code for the mojoify of these messages.

Bug: 993189
Change-Id: I05b83b34ffc836eb96ada4c554650f98982daef8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4936623
Commit-Queue: Dave Tapuska <dtapuska@chromium.org>
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Reviewed-by: danakj <danakj@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1210212}
  • Loading branch information
dtapuska authored and Chromium LUCI CQ committed Oct 16, 2023
1 parent acd16cf commit 33417dc
Show file tree
Hide file tree
Showing 8 changed files with 575 additions and 435 deletions.
1 change: 1 addition & 0 deletions extensions/browser/api/messaging/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ source_set("messaging") {
"message_port.h",
"message_service.cc",
"message_service.h",
"message_service_bindings.cc",
"messaging_api_message_filter.cc",
"messaging_api_message_filter.h",
"messaging_delegate.cc",
Expand Down
3 changes: 3 additions & 0 deletions extensions/browser/api/messaging/OWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# For security review.
per-file message_service_bindings.*=set noparent
per-file message_service_bindings.*=file://ipc/SECURITY_OWNERS
41 changes: 13 additions & 28 deletions extensions/browser/api/messaging/message_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ void MessageService::OpenChannelToExtension(
OnOpenChannelAllowed(std::move(params), true);
}

void MessageService::OpenChannelToNativeApp(
void MessageService::OpenChannelToNativeAppImpl(
const ChannelEndpoint& source,
const PortId& source_port_id,
const std::string& native_app_name) {
Expand Down Expand Up @@ -549,14 +549,14 @@ void MessageService::OpenChannelToNativeApp(
// BUILDFLAG(IS_CHROMEOS))
}

void MessageService::OpenChannelToTab(const ChannelEndpoint& source,
const PortId& source_port_id,
int tab_id,
int frame_id,
const std::string& document_id,
const std::string& extension_id,
mojom::ChannelType channel_type,
const std::string& channel_name) {
void MessageService::OpenChannelToTabImpl(const ChannelEndpoint& source,
const PortId& source_port_id,
int tab_id,
int frame_id,
const std::string& document_id,
const std::string& extension_id,
mojom::ChannelType channel_type,
const std::string& channel_name) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK_GE(frame_id, -1);
DCHECK(source_port_id.is_opener);
Expand Down Expand Up @@ -777,9 +777,9 @@ void MessageService::AddChannel(std::unique_ptr<MessageChannel> channel,
pending_lazy_context_channels_.erase(channel_id);
}

void MessageService::OpenPort(const PortId& port_id,
int process_id,
const PortContext& port_context) {
void MessageService::OpenPortImpl(const PortId& port_id,
int process_id,
const PortContext& port_context) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(!port_id.is_opener);

Expand All @@ -790,19 +790,6 @@ void MessageService::OpenPort(const PortId& port_id,

it->second->receiver->OpenPort(process_id, port_context);
}

void MessageService::ClosePort(const PortId& port_id,
int process_id,
const PortContext& context,
bool force_close) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
int routing_id = context.frame ? context.frame->routing_id : MSG_ROUTING_NONE;
int worker_thread_id =
context.worker ? context.worker->thread_id : kMainThreadId;
ClosePortImpl(port_id, process_id, routing_id, worker_thread_id, force_close,
std::string());
}

void MessageService::CloseChannel(const PortId& port_id,
const std::string& error_message) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
Expand Down Expand Up @@ -947,9 +934,7 @@ void MessageService::DispatchMessage(const PortId& source_port_id,
dest_port->DispatchOnMessage(message);
}

void MessageService::NotifyResponsePending(const PortId& port_id,
int process_id,
const PortContext& port_context) {
void MessageService::NotifyResponsePending(const PortId& port_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(!port_id.is_opener);

Expand Down
52 changes: 38 additions & 14 deletions extensions/browser/api/messaging/message_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "extensions/common/extension_id.h"

class GURL;
struct ExtensionMsg_ExternalConnectionInfo;

namespace content {
class BrowserContext;
Expand Down Expand Up @@ -100,39 +101,40 @@ class MessageService : public BrowserContextKeyedAPI,
mojom::ChannelType channel_type,
const std::string& channel_name);

// Same as above, but opens a channel to the tab with the given ID. Messages
// are restricted to that tab, so if there are multiple tabs in that process,
// only the targeted tab will receive messages.
using ExternalConnectionInfo = ExtensionMsg_ExternalConnectionInfo;
void OpenChannelToExtension(const ChannelEndpoint& source,
const PortId& source_port_id,
const ExternalConnectionInfo& info,
mojom::ChannelType channel_type,
const std::string& channel_name);
void OpenChannelToNativeApp(const ChannelEndpoint& source,
const PortId& source_port_id,
const std::string& native_app_name);
void OpenChannelToTab(const ChannelEndpoint& source,
const PortId& source_port_id,
int tab_id,
int frame_id,
const std::string& document_id,
const std::string& extension_id,
mojom::ChannelType channel_type,
const std::string& channel_name);

void OpenChannelToNativeApp(const ChannelEndpoint& source,
const PortId& source_port_id,
const std::string& native_app_name);

// Marks the given port as opened by |port_context| in the render process
// with id |process_id|.
void OpenPort(const PortId& port_id,
int process_id,
void OpenPort(content::RenderProcessHost* process,
const PortId& port_id,
const PortContext& port_context);

// Closes the given port in the given |port_context|. If this was the last
// context or if |force_close| is true, then the other side is closed as well.
void ClosePort(const PortId& port_id,
int process_id,
void ClosePort(content::RenderProcessHost* process,
const PortId& port_id,
const PortContext& port_context,
bool force_close);

// Notifies the port that one of the receivers of a message indicated that
// they plan to respond to the message later.
void NotifyResponsePending(const PortId& port_id,
int process_id,
void NotifyResponsePending(content::RenderProcessHost* process,
const PortId& port_id,
const PortContext& port_context);

// Returns the number of open channels for test.
Expand All @@ -147,6 +149,28 @@ class MessageService : public BrowserContextKeyedAPI,
friend class BrowserContextKeyedAPIFactory<MessageService>;
struct OpenChannelParams;

// Same as `OpenChannelToExtension`, but opens a channel to the tab with the
// given ID. Messages are restricted to that tab, so if there are multiple
// tabs in that process, only the targeted tab will receive messages.
void OpenChannelToTabImpl(const ChannelEndpoint& source,
const PortId& source_port_id,
int tab_id,
int frame_id,
const std::string& document_id,
const std::string& extension_id,
mojom::ChannelType channel_type,
const std::string& channel_name);
void OpenChannelToNativeAppImpl(const ChannelEndpoint& source,
const PortId& source_port_id,
const std::string& native_app_name);
void OpenPortImpl(const PortId& port_id,
int process_id,
const PortContext& port_context);

// Notifies the port that one of the receivers of a message indicated that
// they plan to respond to the message later.
void NotifyResponsePending(const PortId& port_id);

// A map of channel ID to its channel object.
using MessageChannelMap =
std::map<ChannelId, std::unique_ptr<MessageChannel>>;
Expand Down

0 comments on commit 33417dc

Please sign in to comment.