| 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/shared_worker/websharedworker_proxy.h" | 5 #include "content/renderer/shared_worker/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" |
| 12 #include "ipc/message_router.h" | 13 #include "ipc/message_router.h" |
| 13 | 14 |
| 14 namespace content { | 15 namespace content { |
| 15 | 16 |
| 16 WebSharedWorkerProxy::WebSharedWorkerProxy(IPC::MessageRouter* router, | 17 WebSharedWorkerProxy::WebSharedWorkerProxy( |
| 17 int route_id) | 18 std::unique_ptr<blink::WebSharedWorkerConnectListener> listener, |
| 18 : route_id_(route_id), | 19 ViewHostMsg_CreateWorker_Params params, |
| 19 router_(router), | 20 blink::WebMessagePortChannel* channel) |
| 21 : route_id_(MSG_ROUTING_NONE), |
| 22 router_(ChildThreadImpl::current()->GetRouter()), |
| 20 message_port_id_(MSG_ROUTING_NONE), | 23 message_port_id_(MSG_ROUTING_NONE), |
| 21 connect_listener_(nullptr) { | 24 listener_(std::move(listener)) { |
| 22 DCHECK_NE(MSG_ROUTING_NONE, route_id_); | 25 connect(params, channel); |
| 23 router_->AddRoute(route_id_, this); | |
| 24 } | 26 } |
| 25 | 27 |
| 26 WebSharedWorkerProxy::~WebSharedWorkerProxy() { | 28 WebSharedWorkerProxy::~WebSharedWorkerProxy() { |
| 29 DCHECK_NE(MSG_ROUTING_NONE, route_id_); |
| 27 router_->RemoveRoute(route_id_); | 30 router_->RemoveRoute(route_id_); |
| 28 } | 31 } |
| 29 | 32 |
| 30 void WebSharedWorkerProxy::connect(blink::WebMessagePortChannel* channel, | 33 void WebSharedWorkerProxy::connect(ViewHostMsg_CreateWorker_Params params, |
| 31 ConnectListener* listener) { | 34 blink::WebMessagePortChannel* channel) { |
| 35 // Send synchronous IPC to get |route_id|. |
| 36 // TODO(nhiroki): Stop using synchronous IPC (https://crbug.com/679654). |
| 37 ViewHostMsg_CreateWorker_Reply reply; |
| 38 router_->Send(new ViewHostMsg_CreateWorker(params, &reply)); |
| 39 route_id_ = reply.route_id; |
| 40 router_->AddRoute(route_id_, this); |
| 41 listener_->workerCreated(reply.error); |
| 42 |
| 32 DCHECK_EQ(MSG_ROUTING_NONE, message_port_id_); | 43 DCHECK_EQ(MSG_ROUTING_NONE, message_port_id_); |
| 33 WebMessagePortChannelImpl* webchannel = | 44 WebMessagePortChannelImpl* webchannel = |
| 34 static_cast<WebMessagePortChannelImpl*>(channel); | 45 static_cast<WebMessagePortChannelImpl*>(channel); |
| 35 message_port_id_ = webchannel->message_port_id(); | 46 message_port_id_ = webchannel->message_port_id(); |
| 36 DCHECK_NE(MSG_ROUTING_NONE, message_port_id_); | 47 DCHECK_NE(MSG_ROUTING_NONE, message_port_id_); |
| 37 webchannel->QueueMessages(); | 48 webchannel->QueueMessages(); |
| 38 connect_listener_ = listener; | 49 // |webchannel| is intentionally leaked here: it'll be removed at |
| 50 // WebMessagePortChannelImpl::OnMessagesQueued(). |
| 51 |
| 39 // An actual connection request will be issued on OnWorkerCreated(). | 52 // An actual connection request will be issued on OnWorkerCreated(). |
| 40 } | 53 } |
| 41 | 54 |
| 42 bool WebSharedWorkerProxy::OnMessageReceived(const IPC::Message& message) { | 55 bool WebSharedWorkerProxy::OnMessageReceived(const IPC::Message& message) { |
| 43 bool handled = true; | 56 bool handled = true; |
| 44 IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerProxy, message) | 57 IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerProxy, message) |
| 45 IPC_MESSAGE_HANDLER(ViewMsg_WorkerCreated, OnWorkerCreated) | 58 IPC_MESSAGE_HANDLER(ViewMsg_WorkerCreated, OnWorkerCreated) |
| 46 IPC_MESSAGE_HANDLER(ViewMsg_WorkerScriptLoadFailed, | 59 IPC_MESSAGE_HANDLER(ViewMsg_WorkerScriptLoadFailed, |
| 47 OnWorkerScriptLoadFailed) | 60 OnWorkerScriptLoadFailed) |
| 48 IPC_MESSAGE_HANDLER(ViewMsg_WorkerConnected, | 61 IPC_MESSAGE_HANDLER(ViewMsg_WorkerConnected, |
| 49 OnWorkerConnected) | 62 OnWorkerConnected) |
| 50 IPC_MESSAGE_UNHANDLED(handled = false) | 63 IPC_MESSAGE_UNHANDLED(handled = false) |
| 51 IPC_END_MESSAGE_MAP() | 64 IPC_END_MESSAGE_MAP() |
| 52 return handled; | 65 return handled; |
| 53 } | 66 } |
| 54 | 67 |
| 55 void WebSharedWorkerProxy::OnWorkerCreated() { | 68 void WebSharedWorkerProxy::OnWorkerCreated() { |
| 56 // connect() should be called before. | |
| 57 DCHECK_NE(MSG_ROUTING_NONE, message_port_id_); | |
| 58 | |
| 59 // The worker is created - now send off the connection request. | 69 // The worker is created - now send off the connection request. |
| 60 router_->Send( | 70 router_->Send( |
| 61 new ViewHostMsg_ConnectToWorker(route_id_, message_port_id_)); | 71 new ViewHostMsg_ConnectToWorker(route_id_, message_port_id_)); |
| 62 } | 72 } |
| 63 | 73 |
| 64 void WebSharedWorkerProxy::OnWorkerScriptLoadFailed() { | 74 void WebSharedWorkerProxy::OnWorkerScriptLoadFailed() { |
| 65 if (connect_listener_) { | 75 listener_->scriptLoadFailed(); |
| 66 // This can result in this object being freed. | 76 delete this; |
| 67 connect_listener_->scriptLoadFailed(); | |
| 68 } | |
| 69 } | 77 } |
| 70 | 78 |
| 71 void WebSharedWorkerProxy::OnWorkerConnected() { | 79 void WebSharedWorkerProxy::OnWorkerConnected() { |
| 72 if (connect_listener_) { | 80 listener_->connected(); |
| 73 // This can result in this object being freed. | 81 delete this; |
| 74 connect_listener_->connected(); | |
| 75 } | |
| 76 } | 82 } |
| 77 | 83 |
| 78 } // namespace content | 84 } // namespace content |
| OLD | NEW |