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

Side by Side Diff: content/child/webmessageportchannel_impl.cc

Issue 1974613002: Remove code that was only used by navigator.connect. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix comment Created 4 years, 7 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
« no previous file with comments | « content/child/webmessageportchannel_impl.h ('k') | content/common/frame_messages.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/child/webmessageportchannel_impl.h" 5 #include "content/child/webmessageportchannel_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/values.h"
12 #include "content/child/child_process.h" 11 #include "content/child/child_process.h"
13 #include "content/child/child_thread_impl.h" 12 #include "content/child/child_thread_impl.h"
14 #include "content/common/message_port_messages.h" 13 #include "content/common/message_port_messages.h"
15 #include "content/public/child/v8_value_converter.h"
16 #include "third_party/WebKit/public/platform/WebMessagePortChannelClient.h" 14 #include "third_party/WebKit/public/platform/WebMessagePortChannelClient.h"
17 #include "third_party/WebKit/public/platform/WebString.h" 15 #include "third_party/WebKit/public/platform/WebString.h"
18 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" 16 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h"
19 #include "v8/include/v8.h" 17 #include "v8/include/v8.h"
20 18
21 using blink::WebMessagePortChannel; 19 using blink::WebMessagePortChannel;
22 using blink::WebMessagePortChannelArray; 20 using blink::WebMessagePortChannelArray;
23 using blink::WebMessagePortChannelClient; 21 using blink::WebMessagePortChannelClient;
24 using blink::WebString; 22 using blink::WebString;
25 23
26 namespace content { 24 namespace content {
27 25
28 WebMessagePortChannelImpl::WebMessagePortChannelImpl( 26 WebMessagePortChannelImpl::WebMessagePortChannelImpl(
29 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner) 27 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner)
30 : client_(NULL), 28 : client_(NULL),
31 route_id_(MSG_ROUTING_NONE), 29 route_id_(MSG_ROUTING_NONE),
32 message_port_id_(MSG_ROUTING_NONE), 30 message_port_id_(MSG_ROUTING_NONE),
33 send_messages_as_values_(false),
34 main_thread_task_runner_(main_thread_task_runner) { 31 main_thread_task_runner_(main_thread_task_runner) {
35 AddRef(); 32 AddRef();
36 Init(); 33 Init();
37 } 34 }
38 35
39 WebMessagePortChannelImpl::WebMessagePortChannelImpl( 36 WebMessagePortChannelImpl::WebMessagePortChannelImpl(
40 int route_id, 37 int route_id,
41 const TransferredMessagePort& port, 38 int port_id,
42 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner) 39 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner)
43 : client_(NULL), 40 : client_(NULL),
44 route_id_(route_id), 41 route_id_(route_id),
45 message_port_id_(port.id), 42 message_port_id_(port_id),
46 send_messages_as_values_(port.send_messages_as_values),
47 main_thread_task_runner_(main_thread_task_runner) { 43 main_thread_task_runner_(main_thread_task_runner) {
48 AddRef(); 44 AddRef();
49 Init(); 45 Init();
50 } 46 }
51 47
52 WebMessagePortChannelImpl::~WebMessagePortChannelImpl() { 48 WebMessagePortChannelImpl::~WebMessagePortChannelImpl() {
53 // If we have any queued messages with attached ports, manually destroy them. 49 // If we have any queued messages with attached ports, manually destroy them.
54 while (!message_queue_.empty()) { 50 while (!message_queue_.empty()) {
55 const WebMessagePortChannelArray& channel_array = 51 const WebMessagePortChannelArray& channel_array =
56 message_queue_.front().ports; 52 message_queue_.front().ports;
(...skipping 21 matching lines...) Expand all
78 new WebMessagePortChannelImpl(main_thread_task_runner); 74 new WebMessagePortChannelImpl(main_thread_task_runner);
79 75
80 impl1->Entangle(impl2); 76 impl1->Entangle(impl2);
81 impl2->Entangle(impl1); 77 impl2->Entangle(impl1);
82 78
83 *channel1 = impl1; 79 *channel1 = impl1;
84 *channel2 = impl2; 80 *channel2 = impl2;
85 } 81 }
86 82
87 // static 83 // static
88 std::vector<TransferredMessagePort> 84 std::vector<int>
89 WebMessagePortChannelImpl::ExtractMessagePortIDs( 85 WebMessagePortChannelImpl::ExtractMessagePortIDs(
90 std::unique_ptr<WebMessagePortChannelArray> channels) { 86 std::unique_ptr<WebMessagePortChannelArray> channels) {
91 std::vector<TransferredMessagePort> message_ports; 87 std::vector<int> message_ports;
92 if (channels) 88 if (channels)
93 message_ports = ExtractMessagePortIDs(*channels); 89 message_ports = ExtractMessagePortIDs(*channels);
94 return message_ports; 90 return message_ports;
95 } 91 }
96 92
97 // static 93 // static
98 std::vector<TransferredMessagePort> 94 std::vector<int>
99 WebMessagePortChannelImpl::ExtractMessagePortIDs( 95 WebMessagePortChannelImpl::ExtractMessagePortIDs(
100 const WebMessagePortChannelArray& channels) { 96 const WebMessagePortChannelArray& channels) {
101 std::vector<TransferredMessagePort> message_ports(channels.size()); 97 std::vector<int> message_ports(channels.size());
102 for (size_t i = 0; i < channels.size(); ++i) { 98 for (size_t i = 0; i < channels.size(); ++i) {
103 WebMessagePortChannelImpl* webchannel = 99 WebMessagePortChannelImpl* webchannel =
104 static_cast<WebMessagePortChannelImpl*>(channels[i]); 100 static_cast<WebMessagePortChannelImpl*>(channels[i]);
105 // The message port ids might not be set up yet if this channel 101 // The message port ids might not be set up yet if this channel
106 // wasn't created on the main thread. 102 // wasn't created on the main thread.
107 DCHECK(webchannel->main_thread_task_runner_->BelongsToCurrentThread()); 103 DCHECK(webchannel->main_thread_task_runner_->BelongsToCurrentThread());
108 message_ports[i].id = webchannel->message_port_id(); 104 message_ports[i] = webchannel->message_port_id();
109 message_ports[i].send_messages_as_values =
110 webchannel->send_messages_as_values_;
111 webchannel->QueueMessages(); 105 webchannel->QueueMessages();
112 DCHECK(message_ports[i].id != MSG_ROUTING_NONE); 106 DCHECK(message_ports[i] != MSG_ROUTING_NONE);
113 } 107 }
114 return message_ports; 108 return message_ports;
115 } 109 }
116 110
117 // static 111 // static
118 std::vector<TransferredMessagePort> 112 std::vector<int>
119 WebMessagePortChannelImpl::ExtractMessagePortIDsWithoutQueueing( 113 WebMessagePortChannelImpl::ExtractMessagePortIDsWithoutQueueing(
120 std::unique_ptr<WebMessagePortChannelArray> channels) { 114 std::unique_ptr<WebMessagePortChannelArray> channels) {
121 if (!channels) 115 if (!channels)
122 return std::vector<TransferredMessagePort>(); 116 return std::vector<int>();
123 117
124 std::vector<TransferredMessagePort> message_ports(channels->size()); 118 std::vector<int> message_ports(channels->size());
125 for (size_t i = 0; i < channels->size(); ++i) { 119 for (size_t i = 0; i < channels->size(); ++i) {
126 WebMessagePortChannelImpl* webchannel = 120 WebMessagePortChannelImpl* webchannel =
127 static_cast<WebMessagePortChannelImpl*>((*channels)[i]); 121 static_cast<WebMessagePortChannelImpl*>((*channels)[i]);
128 // The message port ids might not be set up yet if this channel 122 // The message port ids might not be set up yet if this channel
129 // wasn't created on the main thread. 123 // wasn't created on the main thread.
130 DCHECK(webchannel->main_thread_task_runner_->BelongsToCurrentThread()); 124 DCHECK(webchannel->main_thread_task_runner_->BelongsToCurrentThread());
131 message_ports[i].id = webchannel->message_port_id(); 125 message_ports[i] = webchannel->message_port_id();
132 message_ports[i].send_messages_as_values =
133 webchannel->send_messages_as_values_;
134 // Don't queue messages, but do increase the child processes ref-count to 126 // Don't queue messages, but do increase the child processes ref-count to
135 // ensure this child process stays alive long enough to receive all 127 // ensure this child process stays alive long enough to receive all
136 // in-flight messages. 128 // in-flight messages.
137 ChildProcess::current()->AddRefProcess(); 129 ChildProcess::current()->AddRefProcess();
138 DCHECK(message_ports[i].id != MSG_ROUTING_NONE); 130 DCHECK(message_ports[i] != MSG_ROUTING_NONE);
139 } 131 }
140 return message_ports; 132 return message_ports;
141 } 133 }
142 134
143 // static 135 // static
144 WebMessagePortChannelArray WebMessagePortChannelImpl::CreatePorts( 136 WebMessagePortChannelArray WebMessagePortChannelImpl::CreatePorts(
145 const std::vector<TransferredMessagePort>& message_ports, 137 const std::vector<int>& message_ports,
146 const std::vector<int>& new_routing_ids, 138 const std::vector<int>& new_routing_ids,
147 const scoped_refptr<base::SingleThreadTaskRunner>& 139 const scoped_refptr<base::SingleThreadTaskRunner>&
148 main_thread_task_runner) { 140 main_thread_task_runner) {
149 DCHECK_EQ(message_ports.size(), new_routing_ids.size()); 141 DCHECK_EQ(message_ports.size(), new_routing_ids.size());
150 WebMessagePortChannelArray channels(message_ports.size()); 142 WebMessagePortChannelArray channels(message_ports.size());
151 for (size_t i = 0; i < message_ports.size() && i < new_routing_ids.size(); 143 for (size_t i = 0; i < message_ports.size() && i < new_routing_ids.size();
152 ++i) { 144 ++i) {
153 channels[i] = new WebMessagePortChannelImpl( 145 channels[i] = new WebMessagePortChannelImpl(
154 new_routing_ids[i], message_ports[i], 146 new_routing_ids[i], message_ports[i],
155 main_thread_task_runner); 147 main_thread_task_runner);
156 } 148 }
157 return channels; 149 return channels;
158 } 150 }
159 151
160 void WebMessagePortChannelImpl::setClient(WebMessagePortChannelClient* client) { 152 void WebMessagePortChannelImpl::setClient(WebMessagePortChannelClient* client) {
161 // Must lock here since client_ is called on the main thread. 153 // Must lock here since client_ is called on the main thread.
162 base::AutoLock auto_lock(lock_); 154 base::AutoLock auto_lock(lock_);
163 client_ = client; 155 client_ = client;
164 } 156 }
165 157
166 void WebMessagePortChannelImpl::destroy() { 158 void WebMessagePortChannelImpl::destroy() {
167 setClient(NULL); 159 setClient(NULL);
168 160
169 // Release the object on the main thread, since the destructor might want to 161 // Release the object on the main thread, since the destructor might want to
170 // send an IPC, and that has to happen on the main thread. 162 // send an IPC, and that has to happen on the main thread.
171 main_thread_task_runner_->ReleaseSoon(FROM_HERE, this); 163 main_thread_task_runner_->ReleaseSoon(FROM_HERE, this);
172 } 164 }
173 165
174 void WebMessagePortChannelImpl::postMessage( 166 void WebMessagePortChannelImpl::postMessage(
175 const WebString& message_as_string, 167 const WebString& message,
176 WebMessagePortChannelArray* channels_ptr) { 168 WebMessagePortChannelArray* channels_ptr) {
177 MessagePortMessage message(message_as_string);
178 std::unique_ptr<WebMessagePortChannelArray> channels(channels_ptr); 169 std::unique_ptr<WebMessagePortChannelArray> channels(channels_ptr);
179 if (send_messages_as_values_) {
180 blink::WebSerializedScriptValue serialized_value =
181 blink::WebSerializedScriptValue::fromString(message_as_string);
182 v8::Local<v8::Value> v8_value = serialized_value.deserialize();
183 std::unique_ptr<V8ValueConverter> converter(V8ValueConverter::create());
184 converter->SetDateAllowed(true);
185 converter->SetRegExpAllowed(true);
186 std::unique_ptr<base::Value> message_as_value(converter->FromV8Value(
187 v8_value, v8::Isolate::GetCurrent()->GetCurrentContext()));
188 message = MessagePortMessage(std::move(message_as_value));
189 }
190 if (!main_thread_task_runner_->BelongsToCurrentThread()) { 170 if (!main_thread_task_runner_->BelongsToCurrentThread()) {
191 main_thread_task_runner_->PostTask( 171 main_thread_task_runner_->PostTask(
192 FROM_HERE, base::Bind(&WebMessagePortChannelImpl::SendPostMessage, this, 172 FROM_HERE, base::Bind(&WebMessagePortChannelImpl::SendPostMessage, this,
193 message, base::Passed(std::move(channels)))); 173 message, base::Passed(std::move(channels))));
194 } else { 174 } else {
195 SendPostMessage(message, std::move(channels)); 175 SendPostMessage(message, std::move(channels));
196 } 176 }
197 } 177 }
198 178
199 void WebMessagePortChannelImpl::SendPostMessage( 179 void WebMessagePortChannelImpl::SendPostMessage(
200 const MessagePortMessage& message, 180 const base::string16& message,
201 std::unique_ptr<WebMessagePortChannelArray> channels) { 181 std::unique_ptr<WebMessagePortChannelArray> channels) {
202 IPC::Message* msg = new MessagePortHostMsg_PostMessage( 182 IPC::Message* msg = new MessagePortHostMsg_PostMessage(
203 message_port_id_, message, ExtractMessagePortIDs(std::move(channels))); 183 message_port_id_, message, ExtractMessagePortIDs(std::move(channels)));
204 Send(msg); 184 Send(msg);
205 } 185 }
206 186
207 bool WebMessagePortChannelImpl::tryGetMessage( 187 bool WebMessagePortChannelImpl::tryGetMessage(
208 WebString* message, 188 WebString* message,
209 WebMessagePortChannelArray& channels) { 189 WebMessagePortChannelArray& channels) {
210 base::AutoLock auto_lock(lock_); 190 base::AutoLock auto_lock(lock_);
211 if (message_queue_.empty()) 191 if (message_queue_.empty())
212 return false; 192 return false;
213 193
214 const MessagePortMessage& data = message_queue_.front().message; 194 *message = message_queue_.front().message;
215 DCHECK(data.is_string() != data.is_value());
216 if (data.is_value()) {
217 v8::HandleScope handle_scope(client_->scriptIsolate());
218 v8::Context::Scope context_scope(
219 client_->scriptContextForMessageConversion());
220 std::unique_ptr<V8ValueConverter> converter(V8ValueConverter::create());
221 converter->SetDateAllowed(true);
222 converter->SetRegExpAllowed(true);
223 v8::Local<v8::Value> v8_value = converter->ToV8Value(
224 data.as_value(), client_->scriptContextForMessageConversion());
225 blink::WebSerializedScriptValue serialized_value =
226 blink::WebSerializedScriptValue::serialize(v8_value);
227 *message = serialized_value.toString();
228 } else {
229 *message = message_queue_.front().message.message_as_string;
230 }
231 channels = message_queue_.front().ports; 195 channels = message_queue_.front().ports;
232 message_queue_.pop(); 196 message_queue_.pop();
233 return true; 197 return true;
234 } 198 }
235 199
236 void WebMessagePortChannelImpl::Init() { 200 void WebMessagePortChannelImpl::Init() {
237 if (!main_thread_task_runner_->BelongsToCurrentThread()) { 201 if (!main_thread_task_runner_->BelongsToCurrentThread()) {
238 main_thread_task_runner_->PostTask( 202 main_thread_task_runner_->PostTask(
239 FROM_HERE, base::Bind(&WebMessagePortChannelImpl::Init, this)); 203 FROM_HERE, base::Bind(&WebMessagePortChannelImpl::Init, this));
240 return; 204 return;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 bool handled = true; 266 bool handled = true;
303 IPC_BEGIN_MESSAGE_MAP(WebMessagePortChannelImpl, message) 267 IPC_BEGIN_MESSAGE_MAP(WebMessagePortChannelImpl, message)
304 IPC_MESSAGE_HANDLER(MessagePortMsg_Message, OnMessage) 268 IPC_MESSAGE_HANDLER(MessagePortMsg_Message, OnMessage)
305 IPC_MESSAGE_HANDLER(MessagePortMsg_MessagesQueued, OnMessagesQueued) 269 IPC_MESSAGE_HANDLER(MessagePortMsg_MessagesQueued, OnMessagesQueued)
306 IPC_MESSAGE_UNHANDLED(handled = false) 270 IPC_MESSAGE_UNHANDLED(handled = false)
307 IPC_END_MESSAGE_MAP() 271 IPC_END_MESSAGE_MAP()
308 return handled; 272 return handled;
309 } 273 }
310 274
311 void WebMessagePortChannelImpl::OnMessage( 275 void WebMessagePortChannelImpl::OnMessage(
312 const MessagePortMessage& message, 276 const base::string16& message,
313 const std::vector<TransferredMessagePort>& sent_message_ports, 277 const std::vector<int>& sent_message_ports,
314 const std::vector<int>& new_routing_ids) { 278 const std::vector<int>& new_routing_ids) {
315 base::AutoLock auto_lock(lock_); 279 base::AutoLock auto_lock(lock_);
316 Message msg; 280 Message msg;
317 msg.message = message; 281 msg.message = message;
318 msg.ports = CreatePorts(sent_message_ports, new_routing_ids, 282 msg.ports = CreatePorts(sent_message_ports, new_routing_ids,
319 main_thread_task_runner_.get()); 283 main_thread_task_runner_.get());
320 284
321 bool was_empty = message_queue_.empty(); 285 bool was_empty = message_queue_.empty();
322 message_queue_.push(msg); 286 message_queue_.push(msg);
323 if (client_ && was_empty) 287 if (client_ && was_empty)
324 client_->messageAvailable(); 288 client_->messageAvailable();
325 } 289 }
326 290
327 void WebMessagePortChannelImpl::OnMessagesQueued() { 291 void WebMessagePortChannelImpl::OnMessagesQueued() {
328 std::vector<QueuedMessage> queued_messages; 292 std::vector<QueuedMessage> queued_messages;
329 293
330 { 294 {
331 base::AutoLock auto_lock(lock_); 295 base::AutoLock auto_lock(lock_);
332 queued_messages.reserve(message_queue_.size()); 296 queued_messages.reserve(message_queue_.size());
333 while (!message_queue_.empty()) { 297 while (!message_queue_.empty()) {
334 MessagePortMessage message = message_queue_.front().message; 298 base::string16 message = message_queue_.front().message;
335 std::vector<TransferredMessagePort> ports = 299 std::vector<int> ports =
336 ExtractMessagePortIDs(message_queue_.front().ports); 300 ExtractMessagePortIDs(message_queue_.front().ports);
337 queued_messages.push_back(std::make_pair(message, ports)); 301 queued_messages.push_back(std::make_pair(message, ports));
338 message_queue_.pop(); 302 message_queue_.pop();
339 } 303 }
340 } 304 }
341 305
342 Send(new MessagePortHostMsg_SendQueuedMessages( 306 Send(new MessagePortHostMsg_SendQueuedMessages(
343 message_port_id_, queued_messages)); 307 message_port_id_, queued_messages));
344 308
345 message_port_id_ = MSG_ROUTING_NONE; 309 message_port_id_ = MSG_ROUTING_NONE;
346 310
347 Release(); 311 Release();
348 ChildProcess::current()->ReleaseProcess(); 312 ChildProcess::current()->ReleaseProcess();
349 } 313 }
350 314
351 WebMessagePortChannelImpl::Message::Message() {} 315 WebMessagePortChannelImpl::Message::Message() {}
352 316
353 WebMessagePortChannelImpl::Message::Message(const Message& other) = default; 317 WebMessagePortChannelImpl::Message::Message(const Message& other) = default;
354 318
355 WebMessagePortChannelImpl::Message::~Message() {} 319 WebMessagePortChannelImpl::Message::~Message() {}
356 320
357 } // namespace content 321 } // namespace content
OLDNEW
« no previous file with comments | « content/child/webmessageportchannel_impl.h ('k') | content/common/frame_messages.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698