Chromium Code Reviews| 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/child/webmessageportchannel_impl.h" | 5 #include "content/child/webmessageportchannel_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop_proxy.h" | 8 #include "base/message_loop/message_loop_proxy.h" |
| 9 #include "base/values.h" | |
| 9 #include "content/child/child_process.h" | 10 #include "content/child/child_process.h" |
| 10 #include "content/child/child_thread_impl.h" | 11 #include "content/child/child_thread_impl.h" |
| 11 #include "content/common/message_port_messages.h" | 12 #include "content/common/message_port_messages.h" |
| 13 #include "content/public/child/v8_value_converter.h" | |
| 12 #include "third_party/WebKit/public/platform/WebMessagePortChannelClient.h" | 14 #include "third_party/WebKit/public/platform/WebMessagePortChannelClient.h" |
| 13 #include "third_party/WebKit/public/platform/WebString.h" | 15 #include "third_party/WebKit/public/platform/WebString.h" |
| 16 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" | |
| 17 #include "v8/include/v8.h" | |
| 14 | 18 |
| 15 using blink::WebMessagePortChannel; | 19 using blink::WebMessagePortChannel; |
| 16 using blink::WebMessagePortChannelArray; | 20 using blink::WebMessagePortChannelArray; |
| 17 using blink::WebMessagePortChannelClient; | 21 using blink::WebMessagePortChannelClient; |
| 18 using blink::WebString; | 22 using blink::WebString; |
| 19 | 23 |
| 20 namespace content { | 24 namespace content { |
| 21 | 25 |
| 22 WebMessagePortChannelImpl::WebMessagePortChannelImpl( | 26 WebMessagePortChannelImpl::WebMessagePortChannelImpl( |
| 23 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner) | 27 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner) |
| 24 : client_(NULL), | 28 : client_(NULL), |
| 25 route_id_(MSG_ROUTING_NONE), | 29 route_id_(MSG_ROUTING_NONE), |
| 26 message_port_id_(MSG_ROUTING_NONE), | 30 message_port_id_(MSG_ROUTING_NONE), |
| 31 send_messages_as_values_(false), | |
| 27 main_thread_task_runner_(main_thread_task_runner) { | 32 main_thread_task_runner_(main_thread_task_runner) { |
| 28 AddRef(); | 33 AddRef(); |
| 29 Init(); | 34 Init(); |
| 30 } | 35 } |
| 31 | 36 |
| 32 WebMessagePortChannelImpl::WebMessagePortChannelImpl( | 37 WebMessagePortChannelImpl::WebMessagePortChannelImpl( |
| 33 int route_id, | 38 int route_id, |
| 34 int message_port_id, | 39 int message_port_id, |
| 35 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner) | 40 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner) |
| 36 : client_(NULL), | 41 : client_(NULL), |
| 37 route_id_(route_id), | 42 route_id_(route_id), |
| 38 message_port_id_(message_port_id), | 43 message_port_id_(message_port_id), |
| 44 send_messages_as_values_(false), | |
| 39 main_thread_task_runner_(main_thread_task_runner) { | 45 main_thread_task_runner_(main_thread_task_runner) { |
| 40 AddRef(); | 46 AddRef(); |
| 41 Init(); | 47 Init(); |
| 42 } | 48 } |
| 43 | 49 |
| 44 WebMessagePortChannelImpl::~WebMessagePortChannelImpl() { | 50 WebMessagePortChannelImpl::~WebMessagePortChannelImpl() { |
| 45 // If we have any queued messages with attached ports, manually destroy them. | 51 // If we have any queued messages with attached ports, manually destroy them. |
| 46 while (!message_queue_.empty()) { | 52 while (!message_queue_.empty()) { |
| 47 const std::vector<WebMessagePortChannelImpl*>& channel_array = | 53 const std::vector<WebMessagePortChannelImpl*>& channel_array = |
| 48 message_queue_.front().ports; | 54 message_queue_.front().ports; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 106 | 112 |
| 107 void WebMessagePortChannelImpl::destroy() { | 113 void WebMessagePortChannelImpl::destroy() { |
| 108 setClient(NULL); | 114 setClient(NULL); |
| 109 | 115 |
| 110 // Release the object on the main thread, since the destructor might want to | 116 // Release the object on the main thread, since the destructor might want to |
| 111 // send an IPC, and that has to happen on the main thread. | 117 // send an IPC, and that has to happen on the main thread. |
| 112 main_thread_task_runner_->ReleaseSoon(FROM_HERE, this); | 118 main_thread_task_runner_->ReleaseSoon(FROM_HERE, this); |
| 113 } | 119 } |
| 114 | 120 |
| 115 void WebMessagePortChannelImpl::postMessage( | 121 void WebMessagePortChannelImpl::postMessage( |
| 116 const WebString& message, | 122 const WebString& message_as_string, |
| 117 WebMessagePortChannelArray* channels) { | 123 WebMessagePortChannelArray* channels) { |
| 124 MessagePortMessage message(message_as_string); | |
| 125 if (send_messages_as_values_) { | |
| 126 blink::WebSerializedScriptValue serialized_value = | |
| 127 blink::WebSerializedScriptValue::fromString(message_as_string); | |
| 128 v8::Handle<v8::Value> v8_value = serialized_value.deserialize(); | |
| 129 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | |
| 130 scoped_ptr<base::Value> message_as_value(converter->FromV8Value( | |
| 131 v8_value, v8::Isolate::GetCurrent()->GetCurrentContext())); | |
| 132 message = MessagePortMessage(message_as_value.get()); | |
| 133 } | |
| 118 if (!main_thread_task_runner_->BelongsToCurrentThread()) { | 134 if (!main_thread_task_runner_->BelongsToCurrentThread()) { |
| 119 main_thread_task_runner_->PostTask( | 135 main_thread_task_runner_->PostTask( |
| 120 FROM_HERE, | 136 FROM_HERE, base::Bind(&WebMessagePortChannelImpl::PostMessage, this, |
| 121 base::Bind( | 137 message, channels)); |
| 122 &WebMessagePortChannelImpl::PostMessage, this, | |
| 123 static_cast<base::string16>(message), channels)); | |
| 124 } else { | 138 } else { |
| 125 PostMessage(message, channels); | 139 PostMessage(message, channels); |
| 126 } | 140 } |
| 127 } | 141 } |
| 128 | 142 |
| 129 void WebMessagePortChannelImpl::PostMessage( | 143 void WebMessagePortChannelImpl::PostMessage( |
| 130 const base::string16& message, | 144 const MessagePortMessage& message, |
| 131 WebMessagePortChannelArray* channels) { | 145 WebMessagePortChannelArray* channels) { |
| 132 IPC::Message* msg = new MessagePortHostMsg_PostMessage( | 146 IPC::Message* msg = new MessagePortHostMsg_PostMessage( |
| 133 message_port_id_, message, ExtractMessagePortIDs(channels)); | 147 message_port_id_, message, ExtractMessagePortIDs(channels)); |
| 134 Send(msg); | 148 Send(msg); |
| 135 } | 149 } |
| 136 | 150 |
| 137 bool WebMessagePortChannelImpl::tryGetMessage( | 151 bool WebMessagePortChannelImpl::tryGetMessage( |
| 138 WebString* message, | 152 WebString* message, |
| 139 WebMessagePortChannelArray& channels) { | 153 WebMessagePortChannelArray& channels) { |
| 140 base::AutoLock auto_lock(lock_); | 154 base::AutoLock auto_lock(lock_); |
| 141 if (message_queue_.empty()) | 155 if (message_queue_.empty()) |
| 142 return false; | 156 return false; |
| 143 | 157 |
| 144 *message = message_queue_.front().message; | 158 const base::Value* message_as_value; |
| 159 if (message_queue_.front().message.message_as_value.Get(0, | |
|
scheib
2015/02/18 20:58:55
Seems a bit implicit, would read cleanly with some
Marijn Kruisselbrink
2015/02/26 23:12:54
Done.
| |
| 160 &message_as_value)) { | |
| 161 v8::HandleScope handle_scope(client_->scriptIsolate()); | |
| 162 v8::Context::Scope context_scope(client_->scriptContext()); | |
| 163 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | |
| 164 v8::Handle<v8::Value> v8_value = | |
| 165 converter->ToV8Value(message_as_value, client_->scriptContext()); | |
|
adamk
2015/02/18 21:34:46
This call can have side effects if there are sette
| |
| 166 blink::WebSerializedScriptValue serialized_value = | |
| 167 blink::WebSerializedScriptValue::serialize(v8_value); | |
| 168 *message = serialized_value.toString(); | |
| 169 } else { | |
| 170 *message = message_queue_.front().message.message_as_string; | |
| 171 } | |
| 145 const std::vector<WebMessagePortChannelImpl*>& channel_array = | 172 const std::vector<WebMessagePortChannelImpl*>& channel_array = |
| 146 message_queue_.front().ports; | 173 message_queue_.front().ports; |
| 147 WebMessagePortChannelArray result_ports(channel_array.size()); | 174 WebMessagePortChannelArray result_ports(channel_array.size()); |
| 148 for (size_t i = 0; i < channel_array.size(); i++) { | 175 for (size_t i = 0; i < channel_array.size(); i++) { |
| 149 result_ports[i] = channel_array[i]; | 176 result_ports[i] = channel_array[i]; |
| 150 } | 177 } |
| 151 | 178 |
| 152 channels.swap(result_ports); | 179 channels.swap(result_ports); |
| 153 message_queue_.pop(); | 180 message_queue_.pop(); |
| 154 return true; | 181 return true; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 bool handled = true; | 250 bool handled = true; |
| 224 IPC_BEGIN_MESSAGE_MAP(WebMessagePortChannelImpl, message) | 251 IPC_BEGIN_MESSAGE_MAP(WebMessagePortChannelImpl, message) |
| 225 IPC_MESSAGE_HANDLER(MessagePortMsg_Message, OnMessage) | 252 IPC_MESSAGE_HANDLER(MessagePortMsg_Message, OnMessage) |
| 226 IPC_MESSAGE_HANDLER(MessagePortMsg_MessagesQueued, OnMessagesQueued) | 253 IPC_MESSAGE_HANDLER(MessagePortMsg_MessagesQueued, OnMessagesQueued) |
| 227 IPC_MESSAGE_UNHANDLED(handled = false) | 254 IPC_MESSAGE_UNHANDLED(handled = false) |
| 228 IPC_END_MESSAGE_MAP() | 255 IPC_END_MESSAGE_MAP() |
| 229 return handled; | 256 return handled; |
| 230 } | 257 } |
| 231 | 258 |
| 232 void WebMessagePortChannelImpl::OnMessage( | 259 void WebMessagePortChannelImpl::OnMessage( |
| 233 const base::string16& message, | 260 const MessagePortMessage& message, |
| 234 const std::vector<int>& sent_message_port_ids, | 261 const std::vector<int>& sent_message_port_ids, |
| 235 const std::vector<int>& new_routing_ids) { | 262 const std::vector<int>& new_routing_ids) { |
| 236 base::AutoLock auto_lock(lock_); | 263 base::AutoLock auto_lock(lock_); |
| 237 Message msg; | 264 Message msg; |
| 238 msg.message = message; | 265 msg.message = message; |
| 239 if (!sent_message_port_ids.empty()) { | 266 if (!sent_message_port_ids.empty()) { |
| 240 msg.ports.resize(sent_message_port_ids.size()); | 267 msg.ports.resize(sent_message_port_ids.size()); |
| 241 for (size_t i = 0; i < sent_message_port_ids.size(); ++i) { | 268 for (size_t i = 0; i < sent_message_port_ids.size(); ++i) { |
| 242 msg.ports[i] = new WebMessagePortChannelImpl( | 269 msg.ports[i] = new WebMessagePortChannelImpl( |
| 243 new_routing_ids[i], | 270 new_routing_ids[i], sent_message_port_ids[i], |
| 244 sent_message_port_ids[i], | |
| 245 main_thread_task_runner_.get()); | 271 main_thread_task_runner_.get()); |
| 246 } | 272 } |
| 247 } | 273 } |
| 248 | 274 |
| 249 bool was_empty = message_queue_.empty(); | 275 bool was_empty = message_queue_.empty(); |
| 250 message_queue_.push(msg); | 276 message_queue_.push(msg); |
| 251 if (client_ && was_empty) | 277 if (client_ && was_empty) |
| 252 client_->messageAvailable(); | 278 client_->messageAvailable(); |
| 253 } | 279 } |
| 254 | 280 |
| 255 void WebMessagePortChannelImpl::OnMessagesQueued() { | 281 void WebMessagePortChannelImpl::OnMessagesQueued() { |
| 256 std::vector<QueuedMessage> queued_messages; | 282 std::vector<QueuedMessage> queued_messages; |
| 257 | 283 |
| 258 { | 284 { |
| 259 base::AutoLock auto_lock(lock_); | 285 base::AutoLock auto_lock(lock_); |
| 260 queued_messages.reserve(message_queue_.size()); | 286 queued_messages.reserve(message_queue_.size()); |
| 261 while (!message_queue_.empty()) { | 287 while (!message_queue_.empty()) { |
| 262 base::string16 message = message_queue_.front().message; | 288 MessagePortMessage message = message_queue_.front().message; |
| 263 const std::vector<WebMessagePortChannelImpl*>& channel_array = | 289 const std::vector<WebMessagePortChannelImpl*>& channel_array = |
| 264 message_queue_.front().ports; | 290 message_queue_.front().ports; |
| 265 std::vector<int> port_ids(channel_array.size()); | 291 std::vector<int> port_ids(channel_array.size()); |
| 266 for (size_t i = 0; i < channel_array.size(); ++i) { | 292 for (size_t i = 0; i < channel_array.size(); ++i) { |
| 267 port_ids[i] = channel_array[i]->message_port_id(); | 293 port_ids[i] = channel_array[i]->message_port_id(); |
| 268 channel_array[i]->QueueMessages(); | 294 channel_array[i]->QueueMessages(); |
| 269 } | 295 } |
| 270 queued_messages.push_back(std::make_pair(message, port_ids)); | 296 queued_messages.push_back(std::make_pair(message, port_ids)); |
| 271 message_queue_.pop(); | 297 message_queue_.pop(); |
| 272 } | 298 } |
| 273 } | 299 } |
| 274 | 300 |
| 275 Send(new MessagePortHostMsg_SendQueuedMessages( | 301 Send(new MessagePortHostMsg_SendQueuedMessages( |
| 276 message_port_id_, queued_messages)); | 302 message_port_id_, queued_messages)); |
| 277 | 303 |
| 278 message_port_id_ = MSG_ROUTING_NONE; | 304 message_port_id_ = MSG_ROUTING_NONE; |
| 279 | 305 |
| 280 Release(); | 306 Release(); |
| 281 ChildProcess::current()->ReleaseProcess(); | 307 ChildProcess::current()->ReleaseProcess(); |
| 282 } | 308 } |
| 283 | 309 |
| 284 WebMessagePortChannelImpl::Message::Message() {} | 310 WebMessagePortChannelImpl::Message::Message() {} |
| 285 | 311 |
| 286 WebMessagePortChannelImpl::Message::~Message() {} | 312 WebMessagePortChannelImpl::Message::~Message() {} |
| 287 | 313 |
| 288 } // namespace content | 314 } // namespace content |
| OLD | NEW |