| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/common/webmessageportchannel_impl.h" | 5 #include "chrome/common/webmessageportchannel_impl.h" |
| 6 | 6 |
| 7 #include "chrome/common/child_process.h" | 7 #include "chrome/common/child_process.h" |
| 8 #include "chrome/common/child_thread.h" | 8 #include "chrome/common/child_thread.h" |
| 9 #include "chrome/common/worker_messages.h" | 9 #include "chrome/common/worker_messages.h" |
| 10 #include "webkit/api/public/WebString.h" | 10 #include "webkit/api/public/WebString.h" |
| 11 #include "webkit/api/public/WebMessagePortChannelClient.h" | 11 #include "webkit/api/public/WebMessagePortChannelClient.h" |
| 12 | 12 |
| 13 using WebKit::WebMessagePortChannel; | 13 using WebKit::WebMessagePortChannel; |
| 14 using WebKit::WebMessagePortChannelArray; |
| 14 using WebKit::WebMessagePortChannelClient; | 15 using WebKit::WebMessagePortChannelClient; |
| 15 using WebKit::WebString; | 16 using WebKit::WebString; |
| 16 | 17 |
| 17 WebMessagePortChannelImpl::WebMessagePortChannelImpl() | 18 WebMessagePortChannelImpl::WebMessagePortChannelImpl() |
| 18 : client_(NULL), | 19 : client_(NULL), |
| 19 route_id_(MSG_ROUTING_NONE), | 20 route_id_(MSG_ROUTING_NONE), |
| 20 message_port_id_(MSG_ROUTING_NONE) { | 21 message_port_id_(MSG_ROUTING_NONE) { |
| 21 AddRef(); | 22 AddRef(); |
| 22 Init(); | 23 Init(); |
| 23 } | 24 } |
| 24 | 25 |
| 25 WebMessagePortChannelImpl::WebMessagePortChannelImpl( | 26 WebMessagePortChannelImpl::WebMessagePortChannelImpl( |
| 26 int route_id, | 27 int route_id, |
| 27 int message_port_id) | 28 int message_port_id) |
| 28 : client_(NULL), | 29 : client_(NULL), |
| 29 route_id_(route_id), | 30 route_id_(route_id), |
| 30 message_port_id_(message_port_id) { | 31 message_port_id_(message_port_id) { |
| 31 AddRef(); | 32 AddRef(); |
| 32 Init(); | 33 Init(); |
| 33 } | 34 } |
| 34 | 35 |
| 35 WebMessagePortChannelImpl::~WebMessagePortChannelImpl() { | 36 WebMessagePortChannelImpl::~WebMessagePortChannelImpl() { |
| 37 // If we have any queued messages with attached ports, manually destroy them. |
| 38 while (!message_queue_.empty()) { |
| 39 const std::vector<WebMessagePortChannelImpl*>& channel_array = |
| 40 message_queue_.front().ports; |
| 41 for (size_t i = 0; i < channel_array.size(); i++) { |
| 42 channel_array[i]->destroy(); |
| 43 } |
| 44 message_queue_.pop(); |
| 45 } |
| 46 |
| 36 if (message_port_id_ != MSG_ROUTING_NONE) | 47 if (message_port_id_ != MSG_ROUTING_NONE) |
| 37 Send(new WorkerProcessHostMsg_DestroyMessagePort(message_port_id_)); | 48 Send(new WorkerProcessHostMsg_DestroyMessagePort(message_port_id_)); |
| 38 | 49 |
| 39 if (route_id_ != MSG_ROUTING_NONE) | 50 if (route_id_ != MSG_ROUTING_NONE) |
| 40 ChildThread::current()->RemoveRoute(route_id_); | 51 ChildThread::current()->RemoveRoute(route_id_); |
| 41 } | 52 } |
| 42 | 53 |
| 43 void WebMessagePortChannelImpl::setClient(WebMessagePortChannelClient* client) { | 54 void WebMessagePortChannelImpl::setClient(WebMessagePortChannelClient* client) { |
| 44 // Must lock here since client_ is called on the main thread. | 55 // Must lock here since client_ is called on the main thread. |
| 45 AutoLock auto_lock(lock_); | 56 AutoLock auto_lock(lock_); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 58 // The message port ids might not be set up yet, if this channel wasn't | 69 // The message port ids might not be set up yet, if this channel wasn't |
| 59 // created on the main thread. So need to wait until we're on the main thread | 70 // created on the main thread. So need to wait until we're on the main thread |
| 60 // before getting the other message port id. | 71 // before getting the other message port id. |
| 61 scoped_refptr<WebMessagePortChannelImpl> webchannel = | 72 scoped_refptr<WebMessagePortChannelImpl> webchannel = |
| 62 static_cast<WebMessagePortChannelImpl*>(channel); | 73 static_cast<WebMessagePortChannelImpl*>(channel); |
| 63 Entangle(webchannel); | 74 Entangle(webchannel); |
| 64 } | 75 } |
| 65 | 76 |
| 66 void WebMessagePortChannelImpl::postMessage( | 77 void WebMessagePortChannelImpl::postMessage( |
| 67 const WebString& message, | 78 const WebString& message, |
| 68 WebMessagePortChannel* channel) { | 79 WebMessagePortChannelArray* channels) { |
| 69 if (MessageLoop::current() != ChildThread::current()->message_loop()) { | 80 if (MessageLoop::current() != ChildThread::current()->message_loop()) { |
| 70 ChildThread::current()->message_loop()->PostTask(FROM_HERE, | 81 ChildThread::current()->message_loop()->PostTask(FROM_HERE, |
| 71 NewRunnableMethod(this, &WebMessagePortChannelImpl::postMessage, | 82 NewRunnableMethod(this, &WebMessagePortChannelImpl::postMessage, |
| 72 message, channel)); | 83 message, channels)); |
| 73 return; | 84 return; |
| 74 } | 85 } |
| 75 | 86 |
| 76 WebMessagePortChannelImpl* webchannel = | 87 std::vector<int> message_port_ids(channels ? channels->size() : 0); |
| 77 static_cast<WebMessagePortChannelImpl*>(channel); | 88 if (channels) { |
| 78 | 89 for (size_t i = 0; i < channels->size(); ++i) { |
| 79 int message_port_id = MSG_ROUTING_NONE; | 90 WebMessagePortChannelImpl* webchannel = |
| 80 if (webchannel) { | 91 static_cast<WebMessagePortChannelImpl*>((*channels)[i]); |
| 81 message_port_id = webchannel->message_port_id(); | 92 message_port_ids[i] = webchannel->message_port_id(); |
| 82 webchannel->QueueMessages(); | 93 webchannel->QueueMessages(); |
| 83 DCHECK(message_port_id != MSG_ROUTING_NONE); | 94 DCHECK(message_port_ids[i] != MSG_ROUTING_NONE); |
| 95 } |
| 84 } | 96 } |
| 85 | 97 |
| 86 IPC::Message* msg = new WorkerProcessHostMsg_PostMessage( | 98 IPC::Message* msg = new WorkerProcessHostMsg_PostMessage( |
| 87 message_port_id_, message, message_port_id); | 99 message_port_id_, message, message_port_ids); |
| 88 | |
| 89 Send(msg); | 100 Send(msg); |
| 90 } | 101 } |
| 91 | 102 |
| 92 bool WebMessagePortChannelImpl::tryGetMessage( | 103 bool WebMessagePortChannelImpl::tryGetMessage( |
| 93 WebString* message, | 104 WebString* message, |
| 94 WebMessagePortChannel** channel) { | 105 WebMessagePortChannelArray& channels) { |
| 95 AutoLock auto_lock(lock_); | 106 AutoLock auto_lock(lock_); |
| 96 if (message_queue_.empty()) | 107 if (message_queue_.empty()) |
| 97 return false; | 108 return false; |
| 98 | 109 |
| 99 *message = message_queue_.front().message; | 110 *message = message_queue_.front().message; |
| 100 *channel = message_queue_.front().port.release(); | 111 const std::vector<WebMessagePortChannelImpl*>& channel_array = |
| 112 message_queue_.front().ports; |
| 113 WebMessagePortChannelArray result_ports(channel_array.size()); |
| 114 for (size_t i = 0; i < channel_array.size(); i++) { |
| 115 result_ports[i] = channel_array[i]; |
| 116 } |
| 117 |
| 118 channels.swap(result_ports); |
| 101 message_queue_.pop(); | 119 message_queue_.pop(); |
| 102 return true; | 120 return true; |
| 103 } | 121 } |
| 104 | 122 |
| 105 void WebMessagePortChannelImpl::Init() { | 123 void WebMessagePortChannelImpl::Init() { |
| 106 if (MessageLoop::current() != ChildThread::current()->message_loop()) { | 124 if (MessageLoop::current() != ChildThread::current()->message_loop()) { |
| 107 ChildThread::current()->message_loop()->PostTask(FROM_HERE, | 125 ChildThread::current()->message_loop()->PostTask(FROM_HERE, |
| 108 NewRunnableMethod(this, &WebMessagePortChannelImpl::Init)); | 126 NewRunnableMethod(this, &WebMessagePortChannelImpl::Init)); |
| 109 return; | 127 return; |
| 110 } | 128 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 ChildThread::current()->Send(message); | 173 ChildThread::current()->Send(message); |
| 156 } | 174 } |
| 157 | 175 |
| 158 void WebMessagePortChannelImpl::OnMessageReceived(const IPC::Message& message) { | 176 void WebMessagePortChannelImpl::OnMessageReceived(const IPC::Message& message) { |
| 159 IPC_BEGIN_MESSAGE_MAP(WebMessagePortChannelImpl, message) | 177 IPC_BEGIN_MESSAGE_MAP(WebMessagePortChannelImpl, message) |
| 160 IPC_MESSAGE_HANDLER(WorkerProcessMsg_Message, OnMessage) | 178 IPC_MESSAGE_HANDLER(WorkerProcessMsg_Message, OnMessage) |
| 161 IPC_MESSAGE_HANDLER(WorkerProcessMsg_MessagesQueued, OnMessagedQueued) | 179 IPC_MESSAGE_HANDLER(WorkerProcessMsg_MessagesQueued, OnMessagedQueued) |
| 162 IPC_END_MESSAGE_MAP() | 180 IPC_END_MESSAGE_MAP() |
| 163 } | 181 } |
| 164 | 182 |
| 165 void WebMessagePortChannelImpl::OnMessage(const string16& message, | 183 void WebMessagePortChannelImpl::OnMessage( |
| 166 int sent_message_port_id, | 184 const string16& message, |
| 167 int new_routing_id) { | 185 const std::vector<int>& sent_message_port_ids, |
| 186 const std::vector<int>& new_routing_ids) { |
| 168 AutoLock auto_lock(lock_); | 187 AutoLock auto_lock(lock_); |
| 169 Message msg; | 188 Message msg; |
| 170 msg.message = message; | 189 msg.message = message; |
| 171 msg.port = NULL; | 190 if (!sent_message_port_ids.empty()) { |
| 172 if (sent_message_port_id != MSG_ROUTING_NONE) { | 191 msg.ports.resize(sent_message_port_ids.size()); |
| 173 msg.port = new WebMessagePortChannelImpl( | 192 for (size_t i = 0; i < sent_message_port_ids.size(); ++i) { |
| 174 new_routing_id, sent_message_port_id); | 193 msg.ports[i] = new WebMessagePortChannelImpl( |
| 194 new_routing_ids[i], sent_message_port_ids[i]); |
| 195 } |
| 175 } | 196 } |
| 176 | 197 |
| 177 bool was_empty = message_queue_.empty(); | 198 bool was_empty = message_queue_.empty(); |
| 178 message_queue_.push(msg); | 199 message_queue_.push(msg); |
| 179 if (client_ && was_empty) | 200 if (client_ && was_empty) |
| 180 client_->messageAvailable(); | 201 client_->messageAvailable(); |
| 181 } | 202 } |
| 182 | 203 |
| 183 void WebMessagePortChannelImpl::OnMessagedQueued() { | 204 void WebMessagePortChannelImpl::OnMessagedQueued() { |
| 184 std::vector<std::pair<string16, int> > queued_messages; | 205 std::vector<QueuedMessage> queued_messages; |
| 185 | 206 |
| 186 { | 207 { |
| 187 AutoLock auto_lock(lock_); | 208 AutoLock auto_lock(lock_); |
| 188 queued_messages.reserve(message_queue_.size()); | 209 queued_messages.reserve(message_queue_.size()); |
| 189 while (!message_queue_.empty()) { | 210 while (!message_queue_.empty()) { |
| 190 string16 message = message_queue_.front().message; | 211 string16 message = message_queue_.front().message; |
| 191 int port = MSG_ROUTING_NONE; | 212 const std::vector<WebMessagePortChannelImpl*>& channel_array = |
| 192 if (message_queue_.front().port) | 213 message_queue_.front().ports; |
| 193 port = message_queue_.front().port->message_port_id(); | 214 std::vector<int> port_ids(channel_array.size()); |
| 194 | 215 for (size_t i = 0; i < channel_array.size(); ++i) { |
| 195 queued_messages.push_back(std::make_pair(message, port)); | 216 port_ids[i] = channel_array[i]->message_port_id(); |
| 217 } |
| 218 queued_messages.push_back(std::make_pair(message, port_ids)); |
| 196 message_queue_.pop(); | 219 message_queue_.pop(); |
| 197 } | 220 } |
| 198 } | 221 } |
| 199 | 222 |
| 200 Send(new WorkerProcessHostMsg_SendQueuedMessages( | 223 Send(new WorkerProcessHostMsg_SendQueuedMessages( |
| 201 message_port_id_, queued_messages)); | 224 message_port_id_, queued_messages)); |
| 202 | 225 |
| 203 message_port_id_ = MSG_ROUTING_NONE; | 226 message_port_id_ = MSG_ROUTING_NONE; |
| 204 | 227 |
| 205 Release(); | 228 Release(); |
| 206 ChildProcess::current()->ReleaseProcess(); | 229 ChildProcess::current()->ReleaseProcess(); |
| 207 } | 230 } |
| OLD | NEW |