Commit b46ff780 authored by nhiroki's avatar nhiroki Committed by Commit bot

SharedWorker: Remove message forwarding mechanism

SharedWorker service has a mechanism to forward a message sent from a document
to a worker. With this mechanism, a sender creates a nested IPC message and
sends it to the SharedWorker service in the browser process. The service
interprets the nested message and forwards it to an appropriate receiver.

This mechanism is available for any IPC messages but it has been used only for
worker connection message. If we use it only for one message type, we don't have
to manage such a complex mechanism. Therefore, this CL removes the mechanism and
uses a plain IPC message for connecting to a worker.

This simplification also makes it easier to mojofy SharedWorker.

BUG=612308

Review-Url: https://codereview.chromium.org/2601893002
Cr-Commit-Position: refs/heads/master@{#441785}
parent b2127040
...@@ -98,12 +98,14 @@ void SharedWorkerHost::Start(bool pause_on_start) { ...@@ -98,12 +98,14 @@ void SharedWorkerHost::Start(bool pause_on_start) {
info.filter()->Send(new ViewMsg_WorkerCreated(info.route_id())); info.filter()->Send(new ViewMsg_WorkerCreated(info.route_id()));
} }
bool SharedWorkerHost::FilterMessage(const IPC::Message& message, bool SharedWorkerHost::FilterConnectionMessage(
SharedWorkerMessageFilter* filter) { int route_id,
if (!IsAvailable() || !HasFilter(filter, message.routing_id())) int sent_message_port_id,
SharedWorkerMessageFilter* incoming_filter) {
if (!IsAvailable() || !HasFilter(incoming_filter, route_id))
return false; return false;
RelayMessage(message, filter); Connect(route_id, sent_message_port_id, incoming_filter);
return true; return true;
} }
...@@ -222,42 +224,6 @@ void SharedWorkerHost::AllowIndexedDB(const GURL& url, ...@@ -222,42 +224,6 @@ void SharedWorkerHost::AllowIndexedDB(const GURL& url,
url, name, instance_->resource_context(), GetRenderFrameIDsForWorker()); url, name, instance_->resource_context(), GetRenderFrameIDsForWorker());
} }
void SharedWorkerHost::RelayMessage(
const IPC::Message& message,
SharedWorkerMessageFilter* incoming_filter) {
if (!instance_)
return;
if (message.type() == WorkerMsg_Connect::ID) {
// Crack the SharedWorker Connect message to setup routing for the port.
WorkerMsg_Connect::Param param;
if (!WorkerMsg_Connect::Read(&message, &param))
return;
int sent_message_port_id = std::get<0>(param);
int new_routing_id = std::get<1>(param);
DCHECK(container_render_filter_);
new_routing_id = container_render_filter_->GetNextRoutingID();
MessagePortService::GetInstance()->UpdateMessagePort(
sent_message_port_id,
container_render_filter_->message_port_message_filter(),
new_routing_id);
SetMessagePortID(
incoming_filter, message.routing_id(), sent_message_port_id);
// Resend the message with the new routing id.
Send(new WorkerMsg_Connect(
worker_route_id_, sent_message_port_id, new_routing_id));
// Send any queued messages for the sent port.
MessagePortService::GetInstance()->SendQueuedMessagesIfPossible(
sent_message_port_id);
} else {
IPC::Message* new_message = new IPC::Message(message);
new_message->set_routing_id(worker_route_id_);
Send(new_message);
return;
}
}
void SharedWorkerHost::TerminateWorker() { void SharedWorkerHost::TerminateWorker() {
termination_message_sent_ = true; termination_message_sent_ = true;
if (!closed_) if (!closed_)
...@@ -311,6 +277,26 @@ bool SharedWorkerHost::HasFilter(SharedWorkerMessageFilter* filter, ...@@ -311,6 +277,26 @@ bool SharedWorkerHost::HasFilter(SharedWorkerMessageFilter* filter,
return false; return false;
} }
void SharedWorkerHost::Connect(int route_id,
int sent_message_port_id,
SharedWorkerMessageFilter* incoming_filter) {
DCHECK(IsAvailable());
DCHECK(HasFilter(incoming_filter, route_id));
DCHECK(container_render_filter_);
int new_routing_id = container_render_filter_->GetNextRoutingID();
MessagePortService::GetInstance()->UpdateMessagePort(
sent_message_port_id,
container_render_filter_->message_port_message_filter(), new_routing_id);
SetMessagePortID(incoming_filter, route_id, sent_message_port_id);
Send(new WorkerMsg_Connect(worker_route_id_, sent_message_port_id,
new_routing_id));
// Send any queued messages for the sent port.
MessagePortService::GetInstance()->SendQueuedMessagesIfPossible(
sent_message_port_id);
}
void SharedWorkerHost::SetMessagePortID(SharedWorkerMessageFilter* filter, void SharedWorkerHost::SetMessagePortID(SharedWorkerMessageFilter* filter,
int route_id, int route_id,
int message_port_id) { int message_port_id) {
......
...@@ -45,8 +45,9 @@ class SharedWorkerHost { ...@@ -45,8 +45,9 @@ class SharedWorkerHost {
// Returns true iff the given message from a renderer process was forwarded to // Returns true iff the given message from a renderer process was forwarded to
// the worker. // the worker.
bool FilterMessage(const IPC::Message& message, bool FilterConnectionMessage(int route_id,
SharedWorkerMessageFilter* filter); int sent_message_port_id,
SharedWorkerMessageFilter* incoming_filter);
// Handles the shutdown of the filter. If the worker has no other client, // Handles the shutdown of the filter. If the worker has no other client,
// sends TerminateWorkerContext message to shut it down. // sends TerminateWorkerContext message to shut it down.
...@@ -109,21 +110,20 @@ class SharedWorkerHost { ...@@ -109,21 +110,20 @@ class SharedWorkerHost {
using FilterList = std::list<FilterInfo>; using FilterList = std::list<FilterInfo>;
// Relays |message| to the SharedWorker. Takes care of parsing the message if
// it contains a message port and sending it a valid route id.
void RelayMessage(const IPC::Message& message,
SharedWorkerMessageFilter* incoming_filter);
// Return a vector of all the render process/render frame IDs. // Return a vector of all the render process/render frame IDs.
std::vector<std::pair<int, int> > GetRenderFrameIDsForWorker(); std::vector<std::pair<int, int> > GetRenderFrameIDsForWorker();
void RemoveFilters(SharedWorkerMessageFilter* filter); void RemoveFilters(SharedWorkerMessageFilter* filter);
bool HasFilter(SharedWorkerMessageFilter* filter, int route_id) const; bool HasFilter(SharedWorkerMessageFilter* filter, int route_id) const;
void Connect(int route_id,
int sent_message_port_id,
SharedWorkerMessageFilter* incoming_filter);
void SetMessagePortID(SharedWorkerMessageFilter* filter, void SetMessagePortID(SharedWorkerMessageFilter* filter,
int route_id, int route_id,
int message_port_id); int message_port_id);
void AllowFileSystemResponse(std::unique_ptr<IPC::Message> reply_msg, void AllowFileSystemResponse(std::unique_ptr<IPC::Message> reply_msg,
bool allowed); bool allowed);
std::unique_ptr<SharedWorkerInstance> instance_; std::unique_ptr<SharedWorkerInstance> instance_;
scoped_refptr<WorkerDocumentSet> worker_document_set_; scoped_refptr<WorkerDocumentSet> worker_document_set_;
FilterList filters_; FilterList filters_;
......
...@@ -50,7 +50,7 @@ bool SharedWorkerMessageFilter::OnMessageReceived(const IPC::Message& message) { ...@@ -50,7 +50,7 @@ bool SharedWorkerMessageFilter::OnMessageReceived(const IPC::Message& message) {
IPC_BEGIN_MESSAGE_MAP(SharedWorkerMessageFilter, message) IPC_BEGIN_MESSAGE_MAP(SharedWorkerMessageFilter, message)
// Only sent from renderer for now, until we have nested workers. // Only sent from renderer for now, until we have nested workers.
IPC_MESSAGE_HANDLER(ViewHostMsg_CreateWorker, OnCreateWorker) IPC_MESSAGE_HANDLER(ViewHostMsg_CreateWorker, OnCreateWorker)
IPC_MESSAGE_HANDLER(ViewHostMsg_ForwardToWorker, OnForwardToWorker) IPC_MESSAGE_HANDLER(ViewHostMsg_ConnectToWorker, OnConnectToWorker)
// Only sent from renderer. // Only sent from renderer.
IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentDetached, OnDocumentDetached) IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentDetached, OnDocumentDetached)
// Only sent from SharedWorker in renderer. // Only sent from SharedWorker in renderer.
...@@ -88,8 +88,10 @@ void SharedWorkerMessageFilter::OnCreateWorker( ...@@ -88,8 +88,10 @@ void SharedWorkerMessageFilter::OnCreateWorker(
WorkerStoragePartitionId(partition_)); WorkerStoragePartitionId(partition_));
} }
void SharedWorkerMessageFilter::OnForwardToWorker(const IPC::Message& message) { void SharedWorkerMessageFilter::OnConnectToWorker(int route_id,
SharedWorkerServiceImpl::GetInstance()->ForwardToWorker(message, this); int sent_message_port_id) {
SharedWorkerServiceImpl::GetInstance()->ConnectToWorker(
route_id, sent_message_port_id, this);
} }
void SharedWorkerMessageFilter::OnDocumentDetached( void SharedWorkerMessageFilter::OnDocumentDetached(
......
...@@ -46,7 +46,7 @@ class CONTENT_EXPORT SharedWorkerMessageFilter : public BrowserMessageFilter { ...@@ -46,7 +46,7 @@ class CONTENT_EXPORT SharedWorkerMessageFilter : public BrowserMessageFilter {
// Message handlers. // Message handlers.
void OnCreateWorker(const ViewHostMsg_CreateWorker_Params& params, void OnCreateWorker(const ViewHostMsg_CreateWorker_Params& params,
ViewHostMsg_CreateWorker_Reply* reply); ViewHostMsg_CreateWorker_Reply* reply);
void OnForwardToWorker(const IPC::Message& message); void OnConnectToWorker(int route_id, int sent_message_port_id);
void OnDocumentDetached(unsigned long long document_id); void OnDocumentDetached(unsigned long long document_id);
void OnWorkerContextClosed(int worker_route_id); void OnWorkerContextClosed(int worker_route_id);
void OnWorkerContextDestroyed(int worker_route_id); void OnWorkerContextDestroyed(int worker_route_id);
......
...@@ -324,13 +324,15 @@ blink::WebWorkerCreationError SharedWorkerServiceImpl::CreateWorker( ...@@ -324,13 +324,15 @@ blink::WebWorkerCreationError SharedWorkerServiceImpl::CreateWorker(
return ReserveRenderProcessToCreateWorker(std::move(pending_instance)); return ReserveRenderProcessToCreateWorker(std::move(pending_instance));
} }
void SharedWorkerServiceImpl::ForwardToWorker( void SharedWorkerServiceImpl::ConnectToWorker(
const IPC::Message& message, int route_id,
int sent_message_port_id,
SharedWorkerMessageFilter* filter) { SharedWorkerMessageFilter* filter) {
for (WorkerHostMap::const_iterator iter = worker_hosts_.begin(); for (WorkerHostMap::const_iterator iter = worker_hosts_.begin();
iter != worker_hosts_.end(); iter != worker_hosts_.end();
++iter) { ++iter) {
if (iter->second->FilterMessage(message, filter)) if (iter->second->FilterConnectionMessage(route_id, sent_message_port_id,
filter))
return; return;
} }
} }
......
...@@ -56,7 +56,8 @@ class CONTENT_EXPORT SharedWorkerServiceImpl ...@@ -56,7 +56,8 @@ class CONTENT_EXPORT SharedWorkerServiceImpl
SharedWorkerMessageFilter* filter, SharedWorkerMessageFilter* filter,
ResourceContext* resource_context, ResourceContext* resource_context,
const WorkerStoragePartitionId& partition_id); const WorkerStoragePartitionId& partition_id);
void ForwardToWorker(const IPC::Message& message, void ConnectToWorker(int route_id,
int sent_message_port_id,
SharedWorkerMessageFilter* filter); SharedWorkerMessageFilter* filter);
void DocumentDetached(unsigned long long document_id, void DocumentDetached(unsigned long long document_id,
SharedWorkerMessageFilter* filter); SharedWorkerMessageFilter* filter);
......
...@@ -298,9 +298,8 @@ class MockSharedWorkerConnector { ...@@ -298,9 +298,8 @@ class MockSharedWorkerConnector {
} }
void SendConnect() { void SendConnect() {
EXPECT_TRUE( EXPECT_TRUE(
renderer_host_->OnMessageReceived(new ViewHostMsg_ForwardToWorker( renderer_host_->OnMessageReceived(new ViewHostMsg_ConnectToWorker(
WorkerMsg_Connect(create_worker_reply_.route_id, remote_port_id_, create_worker_reply_.route_id, remote_port_id_)));
MSG_ROUTING_NONE))));
} }
void SendSendQueuedMessages( void SendSendQueuedMessages(
const std::vector<QueuedMessage>& queued_messages) { const std::vector<QueuedMessage>& queued_messages) {
......
...@@ -699,10 +699,11 @@ IPC_SYNC_MESSAGE_CONTROL1_1(ViewHostMsg_CreateWorker, ...@@ -699,10 +699,11 @@ IPC_SYNC_MESSAGE_CONTROL1_1(ViewHostMsg_CreateWorker,
// is detached). // is detached).
IPC_MESSAGE_CONTROL1(ViewHostMsg_DocumentDetached, uint64_t /* document_id */) IPC_MESSAGE_CONTROL1(ViewHostMsg_DocumentDetached, uint64_t /* document_id */)
// Wraps an IPC message that's destined to the worker on the renderer->browser // A renderer sends this to the browser process when it wants to connect to a
// hop. // worker.
IPC_MESSAGE_CONTROL1(ViewHostMsg_ForwardToWorker, IPC_MESSAGE_CONTROL2(ViewHostMsg_ConnectToWorker,
IPC::Message /* message */) int /* route_id */,
int /* sent_message_port_id */)
// Tells the browser that a specific Appcache manifest in the current page // Tells the browser that a specific Appcache manifest in the current page
// was accessed. // was accessed.
......
...@@ -40,7 +40,7 @@ bool WebSharedWorkerProxy::Send(std::unique_ptr<IPC::Message> message) { ...@@ -40,7 +40,7 @@ bool WebSharedWorkerProxy::Send(std::unique_ptr<IPC::Message> message) {
// For now we proxy all messages to the worker process through the browser. // For now we proxy all messages to the worker process through the browser.
// Revisit if we find this slow. // Revisit if we find this slow.
// TODO(jabdelmalek): handle sync messages if we need them. // TODO(jabdelmalek): handle sync messages if we need them.
return router_->Send(new ViewHostMsg_ForwardToWorker(*message)); return router_->Send(message.release());
} }
void WebSharedWorkerProxy::SendQueuedMessages() { void WebSharedWorkerProxy::SendQueuedMessages() {
...@@ -63,8 +63,8 @@ void WebSharedWorkerProxy::connect(blink::WebMessagePortChannel* channel, ...@@ -63,8 +63,8 @@ void WebSharedWorkerProxy::connect(blink::WebMessagePortChannel* channel,
DCHECK_NE(MSG_ROUTING_NONE, message_port_id); DCHECK_NE(MSG_ROUTING_NONE, message_port_id);
webchannel->QueueMessages(); webchannel->QueueMessages();
Send(base::MakeUnique<WorkerMsg_Connect>(route_id_, message_port_id, Send(base::MakeUnique<ViewHostMsg_ConnectToWorker>(route_id_,
MSG_ROUTING_NONE)); message_port_id));
connect_listener_ = listener; connect_listener_ = listener;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment