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