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_HANDLER(ViewMsg_WorkerDestroyed, OnWorkerDestroyed) | 58 IPC_MESSAGE_HANDLER(ViewMsg_WorkerDestroyed, OnWorkerDestroyed) |
64 IPC_MESSAGE_HANDLER(ViewMsg_CountFeatureOnSharedWorker, OnCountFeature) | 59 IPC_MESSAGE_HANDLER(ViewMsg_CountFeatureOnSharedWorker, OnCountFeature) |
65 IPC_MESSAGE_UNHANDLED(handled = false) | 60 IPC_MESSAGE_UNHANDLED(handled = false) |
66 IPC_END_MESSAGE_MAP() | 61 IPC_END_MESSAGE_MAP() |
67 return handled; | 62 return handled; |
68 } | 63 } |
69 | 64 |
70 void WebSharedWorkerProxy::OnWorkerCreated() { | 65 void WebSharedWorkerProxy::OnWorkerCreated() { |
71 // The worker is created - now send off the connection request. | 66 DCHECK(message_port_.GetHandle().is_valid()); |
72 router_->Send( | 67 |
73 new ViewHostMsg_ConnectToWorker(route_id_, message_port_id_)); | 68 // The worker is created - now send off the connect message. |
| 69 router_->Send(new ViewHostMsg_ConnectToWorker(route_id_, message_port_)); |
74 } | 70 } |
75 | 71 |
76 void WebSharedWorkerProxy::OnWorkerScriptLoadFailed() { | 72 void WebSharedWorkerProxy::OnWorkerScriptLoadFailed() { |
77 listener_->scriptLoadFailed(); | 73 listener_->scriptLoadFailed(); |
78 delete this; | 74 delete this; |
79 } | 75 } |
80 | 76 |
81 void WebSharedWorkerProxy::OnWorkerConnected( | 77 void WebSharedWorkerProxy::OnWorkerConnected( |
82 const std::set<uint32_t>& used_features) { | 78 const std::set<uint32_t>& used_features) { |
83 listener_->connected(); | 79 listener_->connected(); |
84 for (uint32_t feature : used_features) | 80 for (uint32_t feature : used_features) |
85 listener_->countFeature(feature); | 81 listener_->countFeature(feature); |
86 } | 82 } |
87 | 83 |
88 void WebSharedWorkerProxy::OnWorkerDestroyed() { | 84 void WebSharedWorkerProxy::OnWorkerDestroyed() { |
89 delete this; | 85 delete this; |
90 } | 86 } |
91 | 87 |
92 void WebSharedWorkerProxy::OnCountFeature(uint32_t feature) { | 88 void WebSharedWorkerProxy::OnCountFeature(uint32_t feature) { |
93 listener_->countFeature(feature); | 89 listener_->countFeature(feature); |
94 } | 90 } |
95 | 91 |
96 } // namespace content | 92 } // namespace content |
OLD | NEW |