| 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 converter->SetDateAllowed(true); |
| 131 converter->SetRegExpAllowed(true); |
| 132 scoped_ptr<base::Value> message_as_value(converter->FromV8Value( |
| 133 v8_value, v8::Isolate::GetCurrent()->GetCurrentContext())); |
| 134 message = MessagePortMessage(message_as_value.Pass()); |
| 135 } |
| 118 if (!main_thread_task_runner_->BelongsToCurrentThread()) { | 136 if (!main_thread_task_runner_->BelongsToCurrentThread()) { |
| 119 main_thread_task_runner_->PostTask( | 137 main_thread_task_runner_->PostTask( |
| 120 FROM_HERE, | 138 FROM_HERE, base::Bind(&WebMessagePortChannelImpl::PostMessage, this, |
| 121 base::Bind( | 139 message, channels)); |
| 122 &WebMessagePortChannelImpl::PostMessage, this, | |
| 123 static_cast<base::string16>(message), channels)); | |
| 124 } else { | 140 } else { |
| 125 PostMessage(message, channels); | 141 PostMessage(message, channels); |
| 126 } | 142 } |
| 127 } | 143 } |
| 128 | 144 |
| 129 void WebMessagePortChannelImpl::PostMessage( | 145 void WebMessagePortChannelImpl::PostMessage( |
| 130 const base::string16& message, | 146 const MessagePortMessage& message, |
| 131 WebMessagePortChannelArray* channels) { | 147 WebMessagePortChannelArray* channels) { |
| 132 IPC::Message* msg = new MessagePortHostMsg_PostMessage( | 148 IPC::Message* msg = new MessagePortHostMsg_PostMessage( |
| 133 message_port_id_, message, ExtractMessagePortIDs(channels)); | 149 message_port_id_, message, ExtractMessagePortIDs(channels)); |
| 134 Send(msg); | 150 Send(msg); |
| 135 } | 151 } |
| 136 | 152 |
| 137 bool WebMessagePortChannelImpl::tryGetMessage( | 153 bool WebMessagePortChannelImpl::tryGetMessage( |
| 138 WebString* message, | 154 WebString* message, |
| 139 WebMessagePortChannelArray& channels) { | 155 WebMessagePortChannelArray& channels) { |
| 140 base::AutoLock auto_lock(lock_); | 156 base::AutoLock auto_lock(lock_); |
| 141 if (message_queue_.empty()) | 157 if (message_queue_.empty()) |
| 142 return false; | 158 return false; |
| 143 | 159 |
| 144 *message = message_queue_.front().message; | 160 const MessagePortMessage& data = message_queue_.front().message; |
| 161 DCHECK(data.is_string() != data.is_value()); |
| 162 if (data.is_value()) { |
| 163 v8::HandleScope handle_scope(client_->scriptIsolate()); |
| 164 v8::Context::Scope context_scope( |
| 165 client_->scriptContextForMessageConversion()); |
| 166 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
| 167 converter->SetDateAllowed(true); |
| 168 converter->SetRegExpAllowed(true); |
| 169 v8::Handle<v8::Value> v8_value = converter->ToV8Value( |
| 170 data.as_value(), client_->scriptContextForMessageConversion()); |
| 171 blink::WebSerializedScriptValue serialized_value = |
| 172 blink::WebSerializedScriptValue::serialize(v8_value); |
| 173 *message = serialized_value.toString(); |
| 174 } else { |
| 175 *message = message_queue_.front().message.message_as_string; |
| 176 } |
| 145 const std::vector<WebMessagePortChannelImpl*>& channel_array = | 177 const std::vector<WebMessagePortChannelImpl*>& channel_array = |
| 146 message_queue_.front().ports; | 178 message_queue_.front().ports; |
| 147 WebMessagePortChannelArray result_ports(channel_array.size()); | 179 WebMessagePortChannelArray result_ports(channel_array.size()); |
| 148 for (size_t i = 0; i < channel_array.size(); i++) { | 180 for (size_t i = 0; i < channel_array.size(); i++) { |
| 149 result_ports[i] = channel_array[i]; | 181 result_ports[i] = channel_array[i]; |
| 150 } | 182 } |
| 151 | 183 |
| 152 channels.swap(result_ports); | 184 channels.swap(result_ports); |
| 153 message_queue_.pop(); | 185 message_queue_.pop(); |
| 154 return true; | 186 return true; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 bool handled = true; | 255 bool handled = true; |
| 224 IPC_BEGIN_MESSAGE_MAP(WebMessagePortChannelImpl, message) | 256 IPC_BEGIN_MESSAGE_MAP(WebMessagePortChannelImpl, message) |
| 225 IPC_MESSAGE_HANDLER(MessagePortMsg_Message, OnMessage) | 257 IPC_MESSAGE_HANDLER(MessagePortMsg_Message, OnMessage) |
| 226 IPC_MESSAGE_HANDLER(MessagePortMsg_MessagesQueued, OnMessagesQueued) | 258 IPC_MESSAGE_HANDLER(MessagePortMsg_MessagesQueued, OnMessagesQueued) |
| 227 IPC_MESSAGE_UNHANDLED(handled = false) | 259 IPC_MESSAGE_UNHANDLED(handled = false) |
| 228 IPC_END_MESSAGE_MAP() | 260 IPC_END_MESSAGE_MAP() |
| 229 return handled; | 261 return handled; |
| 230 } | 262 } |
| 231 | 263 |
| 232 void WebMessagePortChannelImpl::OnMessage( | 264 void WebMessagePortChannelImpl::OnMessage( |
| 233 const base::string16& message, | 265 const MessagePortMessage& message, |
| 234 const std::vector<int>& sent_message_port_ids, | 266 const std::vector<int>& sent_message_port_ids, |
| 235 const std::vector<int>& new_routing_ids) { | 267 const std::vector<int>& new_routing_ids) { |
| 236 base::AutoLock auto_lock(lock_); | 268 base::AutoLock auto_lock(lock_); |
| 237 Message msg; | 269 Message msg; |
| 238 msg.message = message; | 270 msg.message = message; |
| 239 if (!sent_message_port_ids.empty()) { | 271 if (!sent_message_port_ids.empty()) { |
| 240 msg.ports.resize(sent_message_port_ids.size()); | 272 msg.ports.resize(sent_message_port_ids.size()); |
| 241 for (size_t i = 0; i < sent_message_port_ids.size(); ++i) { | 273 for (size_t i = 0; i < sent_message_port_ids.size(); ++i) { |
| 242 msg.ports[i] = new WebMessagePortChannelImpl( | 274 msg.ports[i] = new WebMessagePortChannelImpl( |
| 243 new_routing_ids[i], | 275 new_routing_ids[i], sent_message_port_ids[i], |
| 244 sent_message_port_ids[i], | |
| 245 main_thread_task_runner_.get()); | 276 main_thread_task_runner_.get()); |
| 246 } | 277 } |
| 247 } | 278 } |
| 248 | 279 |
| 249 bool was_empty = message_queue_.empty(); | 280 bool was_empty = message_queue_.empty(); |
| 250 message_queue_.push(msg); | 281 message_queue_.push(msg); |
| 251 if (client_ && was_empty) | 282 if (client_ && was_empty) |
| 252 client_->messageAvailable(); | 283 client_->messageAvailable(); |
| 253 } | 284 } |
| 254 | 285 |
| 255 void WebMessagePortChannelImpl::OnMessagesQueued() { | 286 void WebMessagePortChannelImpl::OnMessagesQueued() { |
| 256 std::vector<QueuedMessage> queued_messages; | 287 std::vector<QueuedMessage> queued_messages; |
| 257 | 288 |
| 258 { | 289 { |
| 259 base::AutoLock auto_lock(lock_); | 290 base::AutoLock auto_lock(lock_); |
| 260 queued_messages.reserve(message_queue_.size()); | 291 queued_messages.reserve(message_queue_.size()); |
| 261 while (!message_queue_.empty()) { | 292 while (!message_queue_.empty()) { |
| 262 base::string16 message = message_queue_.front().message; | 293 MessagePortMessage message = message_queue_.front().message; |
| 263 const std::vector<WebMessagePortChannelImpl*>& channel_array = | 294 const std::vector<WebMessagePortChannelImpl*>& channel_array = |
| 264 message_queue_.front().ports; | 295 message_queue_.front().ports; |
| 265 std::vector<int> port_ids(channel_array.size()); | 296 std::vector<int> port_ids(channel_array.size()); |
| 266 for (size_t i = 0; i < channel_array.size(); ++i) { | 297 for (size_t i = 0; i < channel_array.size(); ++i) { |
| 267 port_ids[i] = channel_array[i]->message_port_id(); | 298 port_ids[i] = channel_array[i]->message_port_id(); |
| 268 channel_array[i]->QueueMessages(); | 299 channel_array[i]->QueueMessages(); |
| 269 } | 300 } |
| 270 queued_messages.push_back(std::make_pair(message, port_ids)); | 301 queued_messages.push_back(std::make_pair(message, port_ids)); |
| 271 message_queue_.pop(); | 302 message_queue_.pop(); |
| 272 } | 303 } |
| 273 } | 304 } |
| 274 | 305 |
| 275 Send(new MessagePortHostMsg_SendQueuedMessages( | 306 Send(new MessagePortHostMsg_SendQueuedMessages( |
| 276 message_port_id_, queued_messages)); | 307 message_port_id_, queued_messages)); |
| 277 | 308 |
| 278 message_port_id_ = MSG_ROUTING_NONE; | 309 message_port_id_ = MSG_ROUTING_NONE; |
| 279 | 310 |
| 280 Release(); | 311 Release(); |
| 281 ChildProcess::current()->ReleaseProcess(); | 312 ChildProcess::current()->ReleaseProcess(); |
| 282 } | 313 } |
| 283 | 314 |
| 284 WebMessagePortChannelImpl::Message::Message() {} | 315 WebMessagePortChannelImpl::Message::Message() {} |
| 285 | 316 |
| 286 WebMessagePortChannelImpl::Message::~Message() {} | 317 WebMessagePortChannelImpl::Message::~Message() {} |
| 287 | 318 |
| 288 } // namespace content | 319 } // namespace content |
| OLD | NEW |