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/webmessageportchannel_impl.h" | 9 #include "content/child/webmessageportchannel_impl.h" |
10 #include "content/common/view_messages.h" | 10 #include "content/common/view_messages.h" |
11 #include "content/common/worker_messages.h" | 11 #include "content/common/worker_messages.h" |
12 #include "ipc/message_router.h" | 12 #include "ipc/message_router.h" |
13 #include "third_party/WebKit/public/platform/WebURL.h" | |
14 #include "third_party/WebKit/public/web/WebSharedWorkerClient.h" | |
15 | 13 |
16 namespace content { | 14 namespace content { |
17 | 15 |
18 WebSharedWorkerProxy::WebSharedWorkerProxy(IPC::MessageRouter* router, | 16 WebSharedWorkerProxy::WebSharedWorkerProxy(IPC::MessageRouter* router, |
19 int route_id) | 17 int route_id) |
20 : route_id_(route_id), | 18 : route_id_(route_id), |
21 router_(router), | 19 router_(router), |
22 connect_listener_(nullptr), | 20 message_port_id_(MSG_ROUTING_NONE), |
23 created_(false) { | 21 connect_listener_(nullptr) { |
24 DCHECK_NE(MSG_ROUTING_NONE, route_id_); | 22 DCHECK_NE(MSG_ROUTING_NONE, route_id_); |
25 router_->AddRoute(route_id_, this); | 23 router_->AddRoute(route_id_, this); |
26 } | 24 } |
27 | 25 |
28 WebSharedWorkerProxy::~WebSharedWorkerProxy() { | 26 WebSharedWorkerProxy::~WebSharedWorkerProxy() { |
29 router_->RemoveRoute(route_id_); | 27 router_->RemoveRoute(route_id_); |
30 } | 28 } |
31 | 29 |
32 bool WebSharedWorkerProxy::Send(std::unique_ptr<IPC::Message> message) { | |
33 // The worker object can be interacted with before the browser process told us | |
34 // that it started, in which case we want to queue the message. | |
35 if (!created_) { | |
36 queued_messages_.push_back(std::move(message)); | |
37 return true; | |
38 } | |
39 | |
40 // For now we proxy all messages to the worker process through the browser. | |
41 // Revisit if we find this slow. | |
42 // TODO(jabdelmalek): handle sync messages if we need them. | |
43 return router_->Send(message.release()); | |
44 } | |
45 | |
46 void WebSharedWorkerProxy::SendQueuedMessages() { | |
47 DCHECK(created_); | |
48 DCHECK(queued_messages_.size()); | |
49 std::vector<std::unique_ptr<IPC::Message>> queued_messages; | |
50 queued_messages.swap(queued_messages_); | |
51 for (size_t i = 0; i < queued_messages.size(); ++i) { | |
52 queued_messages[i]->set_routing_id(route_id_); | |
53 Send(std::move(queued_messages[i])); | |
54 } | |
55 } | |
56 | |
57 void WebSharedWorkerProxy::connect(blink::WebMessagePortChannel* channel, | 30 void WebSharedWorkerProxy::connect(blink::WebMessagePortChannel* channel, |
58 ConnectListener* listener) { | 31 ConnectListener* listener) { |
horo
2017/01/06 06:59:45
DCHECK_EQ(MSG_ROUTING_NONE, message_port_id_);
nhiroki
2017/01/06 07:20:47
Done.
| |
59 WebMessagePortChannelImpl* webchannel = | 32 WebMessagePortChannelImpl* webchannel = |
60 static_cast<WebMessagePortChannelImpl*>(channel); | 33 static_cast<WebMessagePortChannelImpl*>(channel); |
61 | 34 message_port_id_ = webchannel->message_port_id(); |
62 int message_port_id = webchannel->message_port_id(); | 35 DCHECK_NE(MSG_ROUTING_NONE, message_port_id_); |
63 DCHECK_NE(MSG_ROUTING_NONE, message_port_id); | |
64 webchannel->QueueMessages(); | 36 webchannel->QueueMessages(); |
65 | |
66 Send(base::MakeUnique<ViewHostMsg_ConnectToWorker>(route_id_, | |
67 message_port_id)); | |
68 connect_listener_ = listener; | 37 connect_listener_ = listener; |
38 // An actual connection request will be issued on OnWorkerCreated(). | |
69 } | 39 } |
70 | 40 |
71 bool WebSharedWorkerProxy::OnMessageReceived(const IPC::Message& message) { | 41 bool WebSharedWorkerProxy::OnMessageReceived(const IPC::Message& message) { |
72 bool handled = true; | 42 bool handled = true; |
73 IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerProxy, message) | 43 IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerProxy, message) |
74 IPC_MESSAGE_HANDLER(ViewMsg_WorkerCreated, OnWorkerCreated) | 44 IPC_MESSAGE_HANDLER(ViewMsg_WorkerCreated, OnWorkerCreated) |
75 IPC_MESSAGE_HANDLER(ViewMsg_WorkerScriptLoadFailed, | 45 IPC_MESSAGE_HANDLER(ViewMsg_WorkerScriptLoadFailed, |
76 OnWorkerScriptLoadFailed) | 46 OnWorkerScriptLoadFailed) |
77 IPC_MESSAGE_HANDLER(ViewMsg_WorkerConnected, | 47 IPC_MESSAGE_HANDLER(ViewMsg_WorkerConnected, |
78 OnWorkerConnected) | 48 OnWorkerConnected) |
79 IPC_MESSAGE_UNHANDLED(handled = false) | 49 IPC_MESSAGE_UNHANDLED(handled = false) |
80 IPC_END_MESSAGE_MAP() | 50 IPC_END_MESSAGE_MAP() |
81 return handled; | 51 return handled; |
82 } | 52 } |
83 | 53 |
84 void WebSharedWorkerProxy::OnWorkerCreated() { | 54 void WebSharedWorkerProxy::OnWorkerCreated() { |
85 created_ = true; | 55 // connect() should be called before. |
86 // The worker is created - now send off the WorkerMsg_Connect message. | 56 DCHECK_NE(MSG_ROUTING_NONE, message_port_id_); |
87 SendQueuedMessages(); | 57 |
58 // The worker is created - now send off the connection request. | |
59 router_->Send( | |
60 new ViewHostMsg_ConnectToWorker(route_id_, message_port_id_)); | |
88 } | 61 } |
89 | 62 |
90 void WebSharedWorkerProxy::OnWorkerScriptLoadFailed() { | 63 void WebSharedWorkerProxy::OnWorkerScriptLoadFailed() { |
91 if (connect_listener_) { | 64 if (connect_listener_) { |
92 // This can result in this object being freed. | 65 // This can result in this object being freed. |
93 connect_listener_->scriptLoadFailed(); | 66 connect_listener_->scriptLoadFailed(); |
94 } | 67 } |
95 } | 68 } |
96 | 69 |
97 void WebSharedWorkerProxy::OnWorkerConnected() { | 70 void WebSharedWorkerProxy::OnWorkerConnected() { |
98 if (connect_listener_) { | 71 if (connect_listener_) { |
99 // This can result in this object being freed. | 72 // This can result in this object being freed. |
100 connect_listener_->connected(); | 73 connect_listener_->connected(); |
101 } | 74 } |
102 } | 75 } |
103 | 76 |
104 } // namespace content | 77 } // namespace content |
OLD | NEW |