| 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 "third_party/WebKit/WebKit/chromium/public/WebString.h" | 10 #include "third_party/WebKit/WebKit/chromium/public/WebString.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 | 46 |
| 47 if (message_port_id_ != MSG_ROUTING_NONE) | 47 if (message_port_id_ != MSG_ROUTING_NONE) |
| 48 Send(new WorkerProcessHostMsg_DestroyMessagePort(message_port_id_)); | 48 Send(new WorkerProcessHostMsg_DestroyMessagePort(message_port_id_)); |
| 49 | 49 |
| 50 if (route_id_ != MSG_ROUTING_NONE) | 50 if (route_id_ != MSG_ROUTING_NONE) |
| 51 ChildThread::current()->RemoveRoute(route_id_); | 51 ChildThread::current()->RemoveRoute(route_id_); |
| 52 } | 52 } |
| 53 | 53 |
| 54 void WebMessagePortChannelImpl::setClient(WebMessagePortChannelClient* client) { | 54 void WebMessagePortChannelImpl::setClient(WebMessagePortChannelClient* client) { |
| 55 // Must lock here since client_ is called on the main thread. | 55 // Must lock here since client_ is called on the main thread. |
| 56 AutoLock auto_lock(lock_); | 56 base::AutoLock auto_lock(lock_); |
| 57 client_ = client; | 57 client_ = client; |
| 58 } | 58 } |
| 59 | 59 |
| 60 void WebMessagePortChannelImpl::destroy() { | 60 void WebMessagePortChannelImpl::destroy() { |
| 61 setClient(NULL); | 61 setClient(NULL); |
| 62 | 62 |
| 63 // Release the object on the main thread, since the destructor might want to | 63 // Release the object on the main thread, since the destructor might want to |
| 64 // send an IPC, and that has to happen on the main thread. | 64 // send an IPC, and that has to happen on the main thread. |
| 65 ChildThread::current()->message_loop()->ReleaseSoon(FROM_HERE, this); | 65 ChildThread::current()->message_loop()->ReleaseSoon(FROM_HERE, this); |
| 66 } | 66 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 } | 98 } |
| 99 | 99 |
| 100 IPC::Message* msg = new WorkerProcessHostMsg_PostMessage( | 100 IPC::Message* msg = new WorkerProcessHostMsg_PostMessage( |
| 101 message_port_id_, message, message_port_ids); | 101 message_port_id_, message, message_port_ids); |
| 102 Send(msg); | 102 Send(msg); |
| 103 } | 103 } |
| 104 | 104 |
| 105 bool WebMessagePortChannelImpl::tryGetMessage( | 105 bool WebMessagePortChannelImpl::tryGetMessage( |
| 106 WebString* message, | 106 WebString* message, |
| 107 WebMessagePortChannelArray& channels) { | 107 WebMessagePortChannelArray& channels) { |
| 108 AutoLock auto_lock(lock_); | 108 base::AutoLock auto_lock(lock_); |
| 109 if (message_queue_.empty()) | 109 if (message_queue_.empty()) |
| 110 return false; | 110 return false; |
| 111 | 111 |
| 112 *message = message_queue_.front().message; | 112 *message = message_queue_.front().message; |
| 113 const std::vector<WebMessagePortChannelImpl*>& channel_array = | 113 const std::vector<WebMessagePortChannelImpl*>& channel_array = |
| 114 message_queue_.front().ports; | 114 message_queue_.front().ports; |
| 115 WebMessagePortChannelArray result_ports(channel_array.size()); | 115 WebMessagePortChannelArray result_ports(channel_array.size()); |
| 116 for (size_t i = 0; i < channel_array.size(); i++) { | 116 for (size_t i = 0; i < channel_array.size(); i++) { |
| 117 result_ports[i] = channel_array[i]; | 117 result_ports[i] = channel_array[i]; |
| 118 } | 118 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 IPC_MESSAGE_HANDLER(WorkerProcessMsg_MessagesQueued, OnMessagedQueued) | 187 IPC_MESSAGE_HANDLER(WorkerProcessMsg_MessagesQueued, OnMessagedQueued) |
| 188 IPC_MESSAGE_UNHANDLED(handled = false) | 188 IPC_MESSAGE_UNHANDLED(handled = false) |
| 189 IPC_END_MESSAGE_MAP() | 189 IPC_END_MESSAGE_MAP() |
| 190 return handled; | 190 return handled; |
| 191 } | 191 } |
| 192 | 192 |
| 193 void WebMessagePortChannelImpl::OnMessage( | 193 void WebMessagePortChannelImpl::OnMessage( |
| 194 const string16& message, | 194 const string16& message, |
| 195 const std::vector<int>& sent_message_port_ids, | 195 const std::vector<int>& sent_message_port_ids, |
| 196 const std::vector<int>& new_routing_ids) { | 196 const std::vector<int>& new_routing_ids) { |
| 197 AutoLock auto_lock(lock_); | 197 base::AutoLock auto_lock(lock_); |
| 198 Message msg; | 198 Message msg; |
| 199 msg.message = message; | 199 msg.message = message; |
| 200 if (!sent_message_port_ids.empty()) { | 200 if (!sent_message_port_ids.empty()) { |
| 201 msg.ports.resize(sent_message_port_ids.size()); | 201 msg.ports.resize(sent_message_port_ids.size()); |
| 202 for (size_t i = 0; i < sent_message_port_ids.size(); ++i) { | 202 for (size_t i = 0; i < sent_message_port_ids.size(); ++i) { |
| 203 msg.ports[i] = new WebMessagePortChannelImpl( | 203 msg.ports[i] = new WebMessagePortChannelImpl( |
| 204 new_routing_ids[i], sent_message_port_ids[i]); | 204 new_routing_ids[i], sent_message_port_ids[i]); |
| 205 } | 205 } |
| 206 } | 206 } |
| 207 | 207 |
| 208 bool was_empty = message_queue_.empty(); | 208 bool was_empty = message_queue_.empty(); |
| 209 message_queue_.push(msg); | 209 message_queue_.push(msg); |
| 210 if (client_ && was_empty) | 210 if (client_ && was_empty) |
| 211 client_->messageAvailable(); | 211 client_->messageAvailable(); |
| 212 } | 212 } |
| 213 | 213 |
| 214 void WebMessagePortChannelImpl::OnMessagedQueued() { | 214 void WebMessagePortChannelImpl::OnMessagedQueued() { |
| 215 std::vector<QueuedMessage> queued_messages; | 215 std::vector<QueuedMessage> queued_messages; |
| 216 | 216 |
| 217 { | 217 { |
| 218 AutoLock auto_lock(lock_); | 218 base::AutoLock auto_lock(lock_); |
| 219 queued_messages.reserve(message_queue_.size()); | 219 queued_messages.reserve(message_queue_.size()); |
| 220 while (!message_queue_.empty()) { | 220 while (!message_queue_.empty()) { |
| 221 string16 message = message_queue_.front().message; | 221 string16 message = message_queue_.front().message; |
| 222 const std::vector<WebMessagePortChannelImpl*>& channel_array = | 222 const std::vector<WebMessagePortChannelImpl*>& channel_array = |
| 223 message_queue_.front().ports; | 223 message_queue_.front().ports; |
| 224 std::vector<int> port_ids(channel_array.size()); | 224 std::vector<int> port_ids(channel_array.size()); |
| 225 for (size_t i = 0; i < channel_array.size(); ++i) { | 225 for (size_t i = 0; i < channel_array.size(); ++i) { |
| 226 port_ids[i] = channel_array[i]->message_port_id(); | 226 port_ids[i] = channel_array[i]->message_port_id(); |
| 227 } | 227 } |
| 228 queued_messages.push_back(std::make_pair(message, port_ids)); | 228 queued_messages.push_back(std::make_pair(message, port_ids)); |
| 229 message_queue_.pop(); | 229 message_queue_.pop(); |
| 230 } | 230 } |
| 231 } | 231 } |
| 232 | 232 |
| 233 Send(new WorkerProcessHostMsg_SendQueuedMessages( | 233 Send(new WorkerProcessHostMsg_SendQueuedMessages( |
| 234 message_port_id_, queued_messages)); | 234 message_port_id_, queued_messages)); |
| 235 | 235 |
| 236 message_port_id_ = MSG_ROUTING_NONE; | 236 message_port_id_ = MSG_ROUTING_NONE; |
| 237 | 237 |
| 238 Release(); | 238 Release(); |
| 239 ChildProcess::current()->ReleaseProcess(); | 239 ChildProcess::current()->ReleaseProcess(); |
| 240 } | 240 } |
| 241 | 241 |
| 242 WebMessagePortChannelImpl::Message::Message() {} | 242 WebMessagePortChannelImpl::Message::Message() {} |
| 243 | 243 |
| 244 WebMessagePortChannelImpl::Message::~Message() {} | 244 WebMessagePortChannelImpl::Message::~Message() {} |
| OLD | NEW |