Index: content/child/webmessageportchannel_impl.cc |
diff --git a/content/child/webmessageportchannel_impl.cc b/content/child/webmessageportchannel_impl.cc |
index 725e5956288946d470953bbd991a0bc7bf03fbe1..9aff263a1dde0b6aa750e03f7e6704df42a72ff0 100644 |
--- a/content/child/webmessageportchannel_impl.cc |
+++ b/content/child/webmessageportchannel_impl.cc |
@@ -6,11 +6,15 @@ |
#include "base/bind.h" |
#include "base/message_loop/message_loop_proxy.h" |
+#include "base/values.h" |
#include "content/child/child_process.h" |
#include "content/child/child_thread_impl.h" |
#include "content/common/message_port_messages.h" |
+#include "content/public/child/v8_value_converter.h" |
#include "third_party/WebKit/public/platform/WebMessagePortChannelClient.h" |
#include "third_party/WebKit/public/platform/WebString.h" |
+#include "third_party/WebKit/public/web/WebSerializedScriptValue.h" |
+#include "v8/include/v8.h" |
using blink::WebMessagePortChannel; |
using blink::WebMessagePortChannelArray; |
@@ -24,6 +28,7 @@ WebMessagePortChannelImpl::WebMessagePortChannelImpl( |
: client_(NULL), |
route_id_(MSG_ROUTING_NONE), |
message_port_id_(MSG_ROUTING_NONE), |
+ send_messages_as_values_(false), |
main_thread_task_runner_(main_thread_task_runner) { |
AddRef(); |
Init(); |
@@ -36,6 +41,7 @@ WebMessagePortChannelImpl::WebMessagePortChannelImpl( |
: client_(NULL), |
route_id_(route_id), |
message_port_id_(message_port_id), |
+ send_messages_as_values_(false), |
main_thread_task_runner_(main_thread_task_runner) { |
AddRef(); |
Init(); |
@@ -113,21 +119,31 @@ void WebMessagePortChannelImpl::destroy() { |
} |
void WebMessagePortChannelImpl::postMessage( |
- const WebString& message, |
+ const WebString& message_as_string, |
WebMessagePortChannelArray* channels) { |
+ MessagePortMessage message(message_as_string); |
+ if (send_messages_as_values_) { |
+ blink::WebSerializedScriptValue serialized_value = |
+ blink::WebSerializedScriptValue::fromString(message_as_string); |
+ v8::Handle<v8::Value> v8_value = serialized_value.deserialize(); |
+ scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
+ converter->SetDateAllowed(true); |
+ converter->SetRegExpAllowed(true); |
+ scoped_ptr<base::Value> message_as_value(converter->FromV8Value( |
+ v8_value, v8::Isolate::GetCurrent()->GetCurrentContext())); |
+ message = MessagePortMessage(message_as_value.Pass()); |
+ } |
if (!main_thread_task_runner_->BelongsToCurrentThread()) { |
main_thread_task_runner_->PostTask( |
- FROM_HERE, |
- base::Bind( |
- &WebMessagePortChannelImpl::PostMessage, this, |
- static_cast<base::string16>(message), channels)); |
+ FROM_HERE, base::Bind(&WebMessagePortChannelImpl::PostMessage, this, |
+ message, channels)); |
} else { |
PostMessage(message, channels); |
} |
} |
void WebMessagePortChannelImpl::PostMessage( |
- const base::string16& message, |
+ const MessagePortMessage& message, |
WebMessagePortChannelArray* channels) { |
IPC::Message* msg = new MessagePortHostMsg_PostMessage( |
message_port_id_, message, ExtractMessagePortIDs(channels)); |
@@ -141,7 +157,23 @@ bool WebMessagePortChannelImpl::tryGetMessage( |
if (message_queue_.empty()) |
return false; |
- *message = message_queue_.front().message; |
+ const MessagePortMessage& data = message_queue_.front().message; |
+ DCHECK(data.is_string() != data.is_value()); |
+ if (data.is_value()) { |
+ v8::HandleScope handle_scope(client_->scriptIsolate()); |
+ v8::Context::Scope context_scope( |
+ client_->scriptContextForMessageConversion()); |
+ scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
+ converter->SetDateAllowed(true); |
+ converter->SetRegExpAllowed(true); |
+ v8::Handle<v8::Value> v8_value = converter->ToV8Value( |
+ data.as_value(), client_->scriptContextForMessageConversion()); |
+ blink::WebSerializedScriptValue serialized_value = |
+ blink::WebSerializedScriptValue::serialize(v8_value); |
+ *message = serialized_value.toString(); |
+ } else { |
+ *message = message_queue_.front().message.message_as_string; |
+ } |
const std::vector<WebMessagePortChannelImpl*>& channel_array = |
message_queue_.front().ports; |
WebMessagePortChannelArray result_ports(channel_array.size()); |
@@ -230,7 +262,7 @@ bool WebMessagePortChannelImpl::OnMessageReceived(const IPC::Message& message) { |
} |
void WebMessagePortChannelImpl::OnMessage( |
- const base::string16& message, |
+ const MessagePortMessage& message, |
const std::vector<int>& sent_message_port_ids, |
const std::vector<int>& new_routing_ids) { |
base::AutoLock auto_lock(lock_); |
@@ -240,8 +272,7 @@ void WebMessagePortChannelImpl::OnMessage( |
msg.ports.resize(sent_message_port_ids.size()); |
for (size_t i = 0; i < sent_message_port_ids.size(); ++i) { |
msg.ports[i] = new WebMessagePortChannelImpl( |
- new_routing_ids[i], |
- sent_message_port_ids[i], |
+ new_routing_ids[i], sent_message_port_ids[i], |
main_thread_task_runner_.get()); |
} |
} |
@@ -259,7 +290,7 @@ void WebMessagePortChannelImpl::OnMessagesQueued() { |
base::AutoLock auto_lock(lock_); |
queued_messages.reserve(message_queue_.size()); |
while (!message_queue_.empty()) { |
- base::string16 message = message_queue_.front().message; |
+ MessagePortMessage message = message_queue_.front().message; |
const std::vector<WebMessagePortChannelImpl*>& channel_array = |
message_queue_.front().ports; |
std::vector<int> port_ids(channel_array.size()); |