| 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/common/webmessageportchannel_impl.h" | 5 #include "content/common/webmessageportchannel_impl.h" |
| 6 | 6 |
| 7 #include "content/common/child_process.h" | 7 #include "content/common/child_process.h" |
| 8 #include "content/common/child_thread.h" | 8 #include "content/common/child_thread.h" |
| 9 #include "content/common/worker_messages.h" | 9 #include "content/common/worker_messages.h" |
| 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSerializedScriptVa
lue.h" |
| 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMessagePortChannel
.h" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMessagePortChannel
Client.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMessagePortChannel
Client.h" |
| 12 | 14 |
| 13 using WebKit::WebMessagePortChannel; | 15 using WebKit::WebMessagePortChannel; |
| 14 using WebKit::WebMessagePortChannelArray; | 16 #ifdef WebTransferableReceipt_h |
| 17 using WebKit::WebMessagePortReceipt; |
| 18 #endif |
| 19 using WebKit::WebTransferableReceiptArray; |
| 15 using WebKit::WebMessagePortChannelClient; | 20 using WebKit::WebMessagePortChannelClient; |
| 21 using WebKit::WebSerializedScriptValue; |
| 16 using WebKit::WebString; | 22 using WebKit::WebString; |
| 17 | 23 |
| 18 WebMessagePortChannelImpl::WebMessagePortChannelImpl() | 24 WebMessagePortChannelImpl::WebMessagePortChannelImpl() |
| 19 : client_(NULL), | 25 : client_(NULL), |
| 20 route_id_(MSG_ROUTING_NONE), | 26 route_id_(MSG_ROUTING_NONE), |
| 21 message_port_id_(MSG_ROUTING_NONE) { | 27 message_port_id_(MSG_ROUTING_NONE) { |
| 22 AddRef(); | 28 AddRef(); |
| 23 Init(); | 29 Init(); |
| 24 } | 30 } |
| 25 | 31 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 void WebMessagePortChannelImpl::entangle(WebMessagePortChannel* channel) { | 74 void WebMessagePortChannelImpl::entangle(WebMessagePortChannel* channel) { |
| 69 // The message port ids might not be set up yet, if this channel wasn't | 75 // The message port ids might not be set up yet, if this channel wasn't |
| 70 // created on the main thread. So need to wait until we're on the main thread | 76 // created on the main thread. So need to wait until we're on the main thread |
| 71 // before getting the other message port id. | 77 // before getting the other message port id. |
| 72 scoped_refptr<WebMessagePortChannelImpl> webchannel( | 78 scoped_refptr<WebMessagePortChannelImpl> webchannel( |
| 73 static_cast<WebMessagePortChannelImpl*>(channel)); | 79 static_cast<WebMessagePortChannelImpl*>(channel)); |
| 74 Entangle(webchannel); | 80 Entangle(webchannel); |
| 75 } | 81 } |
| 76 | 82 |
| 77 void WebMessagePortChannelImpl::postMessage( | 83 void WebMessagePortChannelImpl::postMessage( |
| 78 const WebString& message, | 84 const WebString& raw_message, |
| 79 WebMessagePortChannelArray* channels) { | 85 WebTransferableReceiptArray* receipts) { |
| 80 if (MessageLoop::current() != ChildThread::current()->message_loop()) { | 86 if (MessageLoop::current() != ChildThread::current()->message_loop()) { |
| 81 ChildThread::current()->message_loop()->PostTask(FROM_HERE, | 87 ChildThread::current()->message_loop()->PostTask(FROM_HERE, |
| 82 NewRunnableMethod(this, &WebMessagePortChannelImpl::postMessage, | 88 NewRunnableMethod(this, &WebMessagePortChannelImpl::postMessage, |
| 83 message, channels)); | 89 raw_message, receipts)); |
| 84 return; | 90 return; |
| 85 } | 91 } |
| 86 | 92 |
| 87 std::vector<int> message_port_ids(channels ? channels->size() : 0); | 93 size_t unflattenable_count = 0; |
| 88 if (channels) { | 94 #ifndef WebTransferableReceipt_h |
| 95 WebString message = raw_message; |
| 96 unflattenable_count = receipts ? receipts->size() : 0; |
| 97 #else |
| 98 WebString message = receipts ? WebSerializedScriptValue::flattenReceiptList( |
| 99 raw_message, *receipts, &unflattenable_count) : raw_message; |
| 100 #endif |
| 101 std::vector<int> message_port_ids(unflattenable_count); |
| 102 size_t next_unflattenable = 0; |
| 103 if (receipts) { |
| 89 // Extract the port IDs from the source array, then free it. | 104 // Extract the port IDs from the source array, then free it. |
| 90 for (size_t i = 0; i < channels->size(); ++i) { | 105 for (size_t i = 0; i < receipts->size(); ++i) { |
| 91 WebMessagePortChannelImpl* webchannel = | 106 #ifndef WebTransferableReceipt_h |
| 92 static_cast<WebMessagePortChannelImpl*>((*channels)[i]); | 107 { |
| 93 message_port_ids[i] = webchannel->message_port_id(); | 108 #else |
| 94 webchannel->QueueMessages(); | 109 if (!WebSerializedScriptValue::isFlattenable((*receipts)[i])) { |
| 95 DCHECK(message_port_ids[i] != MSG_ROUTING_NONE); | 110 DCHECK(toWebMessagePortReceipt((*receipts)[i])); |
| 111 #endif |
| 112 DCHECK(next_unflattenable < unflattenable_count); |
| 113 WebMessagePortChannelImpl* webchannel = |
| 114 #ifndef WebTransferableReceipt_h |
| 115 static_cast<WebMessagePortChannelImpl*>((*receipts)[i]); |
| 116 #else |
| 117 static_cast<WebMessagePortChannelImpl*>( |
| 118 static_cast<WebMessagePortReceipt*>((*receipts)[i])->channel()); |
| 119 #endif |
| 120 message_port_ids[next_unflattenable] = webchannel->message_port_id(); |
| 121 webchannel->QueueMessages(); |
| 122 DCHECK(message_port_ids[next_unflattenable] != MSG_ROUTING_NONE); |
| 123 ++next_unflattenable; |
| 124 } |
| 125 #ifdef WebTransferableReceipt_h |
| 126 delete ((*receipts)[i]); |
| 127 (*receipts)[i] = 0; |
| 128 #endif |
| 96 } | 129 } |
| 97 delete channels; | 130 delete receipts; |
| 98 } | 131 } |
| 132 DCHECK(next_unflattenable == unflattenable_count); |
| 99 | 133 |
| 100 IPC::Message* msg = new WorkerProcessHostMsg_PostMessage( | 134 IPC::Message* msg = new WorkerProcessHostMsg_PostMessage( |
| 101 message_port_id_, message, message_port_ids); | 135 message_port_id_, message, message_port_ids); |
| 102 Send(msg); | 136 Send(msg); |
| 103 } | 137 } |
| 104 | 138 |
| 139 #ifndef WebTransferableReceipt_h |
| 105 bool WebMessagePortChannelImpl::tryGetMessage( | 140 bool WebMessagePortChannelImpl::tryGetMessage( |
| 141 #else |
| 142 bool WebMessagePortChannelImpl::tryGetMessageWithNewReceipts( |
| 143 #endif |
| 106 WebString* message, | 144 WebString* message, |
| 107 WebMessagePortChannelArray& channels) { | 145 WebTransferableReceiptArray& receipts) { |
| 108 base::AutoLock auto_lock(lock_); | 146 base::AutoLock auto_lock(lock_); |
| 109 if (message_queue_.empty()) | 147 if (message_queue_.empty()) |
| 110 return false; | 148 return false; |
| 111 | 149 |
| 112 *message = message_queue_.front().message; | 150 *message = message_queue_.front().message; |
| 113 const std::vector<WebMessagePortChannelImpl*>& channel_array = | 151 const std::vector<WebMessagePortChannelImpl*>& channel_array = |
| 114 message_queue_.front().ports; | 152 message_queue_.front().ports; |
| 115 WebMessagePortChannelArray result_ports(channel_array.size()); | 153 WebTransferableReceiptArray result_ports(channel_array.size()); |
| 116 for (size_t i = 0; i < channel_array.size(); i++) { | 154 for (size_t i = 0; i < channel_array.size(); i++) { |
| 155 #ifndef WebTransferableReceipt_h |
| 117 result_ports[i] = channel_array[i]; | 156 result_ports[i] = channel_array[i]; |
| 157 #else |
| 158 result_ports[i] = new WebMessagePortReceipt(channel_array[i]); |
| 159 #endif |
| 118 } | 160 } |
| 119 | 161 |
| 120 channels.swap(result_ports); | 162 receipts.swap(result_ports); |
| 121 message_queue_.pop(); | 163 message_queue_.pop(); |
| 122 return true; | 164 return true; |
| 123 } | 165 } |
| 124 | 166 |
| 125 void WebMessagePortChannelImpl::Init() { | 167 void WebMessagePortChannelImpl::Init() { |
| 126 if (MessageLoop::current() != ChildThread::current()->message_loop()) { | 168 if (MessageLoop::current() != ChildThread::current()->message_loop()) { |
| 127 ChildThread::current()->message_loop()->PostTask(FROM_HERE, | 169 ChildThread::current()->message_loop()->PostTask(FROM_HERE, |
| 128 NewRunnableMethod(this, &WebMessagePortChannelImpl::Init)); | 170 NewRunnableMethod(this, &WebMessagePortChannelImpl::Init)); |
| 129 return; | 171 return; |
| 130 } | 172 } |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 | 277 |
| 236 message_port_id_ = MSG_ROUTING_NONE; | 278 message_port_id_ = MSG_ROUTING_NONE; |
| 237 | 279 |
| 238 Release(); | 280 Release(); |
| 239 ChildProcess::current()->ReleaseProcess(); | 281 ChildProcess::current()->ReleaseProcess(); |
| 240 } | 282 } |
| 241 | 283 |
| 242 WebMessagePortChannelImpl::Message::Message() {} | 284 WebMessagePortChannelImpl::Message::Message() {} |
| 243 | 285 |
| 244 WebMessagePortChannelImpl::Message::~Message() {} | 286 WebMessagePortChannelImpl::Message::~Message() {} |
| OLD | NEW |