Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: content/renderer/websharedworker_proxy.cc

Issue 2422793002: HTML MessagePort as mojo::MessagePipeHandle (Closed)
Patch Set: Cleanup Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 11 matching lines...) Expand all
22 connect_listener_(nullptr), 22 connect_listener_(nullptr),
23 created_(false) { 23 created_(false) {
24 DCHECK_NE(MSG_ROUTING_NONE, route_id_); 24 DCHECK_NE(MSG_ROUTING_NONE, route_id_);
25 router_->AddRoute(route_id_, this); 25 router_->AddRoute(route_id_, this);
26 } 26 }
27 27
28 WebSharedWorkerProxy::~WebSharedWorkerProxy() { 28 WebSharedWorkerProxy::~WebSharedWorkerProxy() {
29 router_->RemoveRoute(route_id_); 29 router_->RemoveRoute(route_id_);
30 } 30 }
31 31
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, 32 void WebSharedWorkerProxy::connect(blink::WebMessagePortChannel* channel,
58 ConnectListener* listener) { 33 ConnectListener* listener) {
59 WebMessagePortChannelImpl* webchannel = 34 // Accept ownership of the channel.
60 static_cast<WebMessagePortChannelImpl*>(channel); 35 std::unique_ptr<WebMessagePortChannelImpl> channel_impl(
36 static_cast<WebMessagePortChannelImpl*>(channel));
61 37
62 int message_port_id = webchannel->message_port_id(); 38 DCHECK(!queued_connect_message_);
63 DCHECK_NE(MSG_ROUTING_NONE, message_port_id);
64 webchannel->QueueMessages();
65 39
66 Send(base::MakeUnique<ViewHostMsg_ConnectToWorker>(route_id_, 40 queued_connect_message_ = base::MakeUnique<ViewHostMsg_ConnectToWorker>(
67 message_port_id)); 41 route_id_, channel_impl->ReleaseMessagePort());
42 if (created_)
43 router_->Send(queued_connect_message_.release());
44
68 connect_listener_ = listener; 45 connect_listener_ = listener;
69 } 46 }
70 47
71 bool WebSharedWorkerProxy::OnMessageReceived(const IPC::Message& message) { 48 bool WebSharedWorkerProxy::OnMessageReceived(const IPC::Message& message) {
72 bool handled = true; 49 bool handled = true;
73 IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerProxy, message) 50 IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerProxy, message)
74 IPC_MESSAGE_HANDLER(ViewMsg_WorkerCreated, OnWorkerCreated) 51 IPC_MESSAGE_HANDLER(ViewMsg_WorkerCreated, OnWorkerCreated)
75 IPC_MESSAGE_HANDLER(ViewMsg_WorkerScriptLoadFailed, 52 IPC_MESSAGE_HANDLER(ViewMsg_WorkerScriptLoadFailed,
76 OnWorkerScriptLoadFailed) 53 OnWorkerScriptLoadFailed)
77 IPC_MESSAGE_HANDLER(ViewMsg_WorkerConnected, 54 IPC_MESSAGE_HANDLER(ViewMsg_WorkerConnected,
78 OnWorkerConnected) 55 OnWorkerConnected)
79 IPC_MESSAGE_UNHANDLED(handled = false) 56 IPC_MESSAGE_UNHANDLED(handled = false)
80 IPC_END_MESSAGE_MAP() 57 IPC_END_MESSAGE_MAP()
81 return handled; 58 return handled;
82 } 59 }
83 60
84 void WebSharedWorkerProxy::OnWorkerCreated() { 61 void WebSharedWorkerProxy::OnWorkerCreated() {
85 created_ = true; 62 created_ = true;
86 // The worker is created - now send off the WorkerMsg_Connect message. 63
87 SendQueuedMessages(); 64 // The worker is created - now send off the connect message.
65 if (queued_connect_message_)
66 router_->Send(queued_connect_message_.release());
88 } 67 }
89 68
90 void WebSharedWorkerProxy::OnWorkerScriptLoadFailed() { 69 void WebSharedWorkerProxy::OnWorkerScriptLoadFailed() {
91 if (connect_listener_) { 70 if (connect_listener_) {
92 // This can result in this object being freed. 71 // This can result in this object being freed.
93 connect_listener_->scriptLoadFailed(); 72 connect_listener_->scriptLoadFailed();
94 } 73 }
95 } 74 }
96 75
97 void WebSharedWorkerProxy::OnWorkerConnected() { 76 void WebSharedWorkerProxy::OnWorkerConnected() {
98 if (connect_listener_) { 77 if (connect_listener_) {
99 // This can result in this object being freed. 78 // This can result in this object being freed.
100 connect_listener_->connected(); 79 connect_listener_->connected();
101 } 80 }
102 } 81 }
103 82
104 } // namespace content 83 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698