Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/websharedworker_proxy.h" | 5 #include "content/renderer/websharedworker_proxy.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "content/child/child_thread_impl.h" | |
| 9 #include "content/child/webmessageportchannel_impl.h" | 10 #include "content/child/webmessageportchannel_impl.h" |
| 10 #include "content/common/view_messages.h" | 11 #include "content/common/view_messages.h" |
| 11 #include "content/common/worker_messages.h" | 12 #include "content/common/worker_messages.h" |
| 13 #include "ipc/ipc_sync_channel.h" | |
| 12 #include "ipc/message_router.h" | 14 #include "ipc/message_router.h" |
| 13 #include "third_party/WebKit/public/platform/WebURL.h" | 15 #include "third_party/WebKit/public/platform/WebURL.h" |
| 14 #include "third_party/WebKit/public/web/WebSharedWorkerClient.h" | 16 #include "third_party/WebKit/public/web/WebSharedWorkerClient.h" |
| 15 | 17 |
| 16 namespace content { | 18 namespace content { |
| 17 | 19 |
| 18 WebSharedWorkerProxy::WebSharedWorkerProxy(IPC::MessageRouter* router, | 20 WebSharedWorkerProxy::WebSharedWorkerProxy(int route_id) |
| 19 int route_id) | |
| 20 : route_id_(route_id), | 21 : route_id_(route_id), |
| 21 router_(router), | 22 router_(ChildThreadImpl::current()->GetRouter()), |
| 22 connect_listener_(nullptr), | 23 connect_listener_(nullptr), |
| 23 created_(false) { | 24 created_(false) { |
| 24 router_->AddRoute(route_id_, this); | 25 router_->AddRoute(route_id_, this); |
| 26 ChildThreadImpl::current()->channel()->GetRemoteAssociatedInterface( | |
| 27 &message_filter_); | |
| 25 } | 28 } |
| 26 | 29 |
| 27 WebSharedWorkerProxy::~WebSharedWorkerProxy() { | 30 WebSharedWorkerProxy::~WebSharedWorkerProxy() { |
| 28 Disconnect(); | 31 Disconnect(); |
| 29 } | 32 } |
| 30 | 33 |
| 31 void WebSharedWorkerProxy::Disconnect() { | 34 void WebSharedWorkerProxy::Disconnect() { |
| 32 if (route_id_ == MSG_ROUTING_NONE) | 35 if (route_id_ == MSG_ROUTING_NONE) |
| 33 return; | 36 return; |
| 34 | 37 |
| 35 // So the messages from WorkerContext (like WorkerContextDestroyed) do not | |
| 36 // come after nobody is listening. Since Worker and WorkerContext can | |
| 37 // terminate independently, already sent messages may still be in the pipe. | |
| 38 router_->RemoveRoute(route_id_); | |
| 39 | |
| 40 route_id_ = MSG_ROUTING_NONE; | 38 route_id_ = MSG_ROUTING_NONE; |
| 41 } | 39 } |
| 42 | 40 |
| 43 bool WebSharedWorkerProxy::Send(std::unique_ptr<IPC::Message> message) { | 41 bool WebSharedWorkerProxy::Send(int message_port_id) { |
|
nhiroki
2016/12/28 09:37:02
TODO(nhiroki): Make this void.
kinuko
2017/01/05 08:13:14
Doesn't 'Send' sound too generic?
nhiroki
2017/01/10 08:44:06
This function was removed by other cleanup CLs.
| |
| 44 // It's possible that messages will be sent before the worker is created, in | 42 // It's possible that messages will be sent before the worker is created, in |
| 45 // which case route_id_ will be none. Or the worker object can be interacted | 43 // which case route_id_ will be none. Or the worker object can be interacted |
| 46 // with before the browser process told us that it started, in which case we | 44 // with before the browser process told us that it started, in which case we |
| 47 // also want to queue the message. | 45 // also want to queue the message. |
| 48 if (!created_) { | 46 if (!created_) { |
| 49 queued_messages_.push_back(std::move(message)); | 47 queued_messages_.push_back(message_port_id); |
| 50 return true; | 48 return true; |
| 51 } | 49 } |
| 52 | 50 |
| 53 // For now we proxy all messages to the worker process through the browser. | |
| 54 // Revisit if we find this slow. | |
| 55 // TODO(jabdelmalek): handle sync messages if we need them. | 51 // TODO(jabdelmalek): handle sync messages if we need them. |
| 56 return router_->Send(message.release()); | 52 message_filter_->OnConnectToWorker(route_id_, message_port_id); |
| 53 return true; | |
| 57 } | 54 } |
| 58 | 55 |
| 59 void WebSharedWorkerProxy::SendQueuedMessages() { | 56 void WebSharedWorkerProxy::SendQueuedMessages() { |
| 60 DCHECK(created_); | 57 DCHECK(created_); |
| 61 DCHECK(queued_messages_.size()); | 58 DCHECK(queued_messages_.size()); |
| 62 std::vector<std::unique_ptr<IPC::Message>> queued_messages; | 59 for (size_t i = 0; i < queued_messages_.size(); ++i) |
|
shimazu
2017/01/05 02:24:03
How about range-based for?
nhiroki
2017/01/10 08:44:06
This part was removed by other cleanup CLs.
| |
| 63 queued_messages.swap(queued_messages_); | 60 Send(queued_messages_[i]); |
| 64 for (size_t i = 0; i < queued_messages.size(); ++i) { | |
| 65 queued_messages[i]->set_routing_id(route_id_); | |
| 66 Send(std::move(queued_messages[i])); | |
| 67 } | |
| 68 } | 61 } |
| 69 | 62 |
| 70 void WebSharedWorkerProxy::connect(blink::WebMessagePortChannel* channel, | 63 void WebSharedWorkerProxy::connect(blink::WebMessagePortChannel* channel, |
| 71 ConnectListener* listener) { | 64 ConnectListener* listener) { |
| 72 WebMessagePortChannelImpl* webchannel = | 65 WebMessagePortChannelImpl* webchannel = |
| 73 static_cast<WebMessagePortChannelImpl*>(channel); | 66 static_cast<WebMessagePortChannelImpl*>(channel); |
| 74 | 67 |
| 75 int message_port_id = webchannel->message_port_id(); | 68 int message_port_id = webchannel->message_port_id(); |
| 76 DCHECK_NE(MSG_ROUTING_NONE, message_port_id); | 69 DCHECK_NE(MSG_ROUTING_NONE, message_port_id); |
| 77 webchannel->QueueMessages(); | 70 webchannel->QueueMessages(); |
| 78 | 71 |
| 79 Send(base::MakeUnique<ViewHostMsg_ConnectToWorker>(route_id_, | 72 Send(message_port_id); |
| 80 message_port_id)); | |
| 81 connect_listener_ = listener; | 73 connect_listener_ = listener; |
| 82 } | 74 } |
| 83 | 75 |
| 84 bool WebSharedWorkerProxy::OnMessageReceived(const IPC::Message& message) { | 76 bool WebSharedWorkerProxy::OnMessageReceived(const IPC::Message& message) { |
| 85 bool handled = true; | 77 bool handled = true; |
| 86 IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerProxy, message) | 78 IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerProxy, message) |
| 87 IPC_MESSAGE_HANDLER(ViewMsg_WorkerCreated, OnWorkerCreated) | 79 IPC_MESSAGE_HANDLER(ViewMsg_WorkerCreated, OnWorkerCreated) |
| 88 IPC_MESSAGE_HANDLER(ViewMsg_WorkerScriptLoadFailed, | 80 IPC_MESSAGE_HANDLER(ViewMsg_WorkerScriptLoadFailed, |
| 89 OnWorkerScriptLoadFailed) | 81 OnWorkerScriptLoadFailed) |
| 90 IPC_MESSAGE_HANDLER(ViewMsg_WorkerConnected, | 82 IPC_MESSAGE_HANDLER(ViewMsg_WorkerConnected, |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 109 } | 101 } |
| 110 | 102 |
| 111 void WebSharedWorkerProxy::OnWorkerConnected() { | 103 void WebSharedWorkerProxy::OnWorkerConnected() { |
| 112 if (connect_listener_) { | 104 if (connect_listener_) { |
| 113 // This can result in this object being freed. | 105 // This can result in this object being freed. |
| 114 connect_listener_->connected(); | 106 connect_listener_->connected(); |
| 115 } | 107 } |
| 116 } | 108 } |
| 117 | 109 |
| 118 } // namespace content | 110 } // namespace content |
| OLD | NEW |