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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "content/common/child_process.h" | 8 #include "content/common/child_process.h" |
9 #include "content/common/child_thread.h" | 9 #include "content/common/child_thread.h" |
10 #include "content/common/worker_messages.h" | 10 #include "content/common/worker_messages.h" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 // created on the main thread. So need to wait until we're on the main thread | 73 // created on the main thread. So need to wait until we're on the main thread |
74 // before getting the other message port id. | 74 // before getting the other message port id. |
75 scoped_refptr<WebMessagePortChannelImpl> webchannel( | 75 scoped_refptr<WebMessagePortChannelImpl> webchannel( |
76 static_cast<WebMessagePortChannelImpl*>(channel)); | 76 static_cast<WebMessagePortChannelImpl*>(channel)); |
77 Entangle(webchannel); | 77 Entangle(webchannel); |
78 } | 78 } |
79 | 79 |
80 void WebMessagePortChannelImpl::postMessage( | 80 void WebMessagePortChannelImpl::postMessage( |
81 const WebString& message, | 81 const WebString& message, |
82 WebMessagePortChannelArray* channels) { | 82 WebMessagePortChannelArray* channels) { |
83 if (MessageLoop::current() != ChildThread::current()->message_loop()) { | 83 if (base::MessageLoop::current() != ChildThread::current()->message_loop()) { |
84 ChildThread::current()->message_loop()->PostTask( | 84 ChildThread::current()->message_loop()->PostTask( |
85 FROM_HERE, | 85 FROM_HERE, |
86 base::Bind(&WebMessagePortChannelImpl::postMessage, this, | 86 base::Bind( |
87 message, channels)); | 87 &WebMessagePortChannelImpl::postMessage, this, message, channels)); |
88 return; | 88 return; |
89 } | 89 } |
90 | 90 |
91 std::vector<int> message_port_ids(channels ? channels->size() : 0); | 91 std::vector<int> message_port_ids(channels ? channels->size() : 0); |
92 if (channels) { | 92 if (channels) { |
93 // Extract the port IDs from the source array, then free it. | 93 // Extract the port IDs from the source array, then free it. |
94 for (size_t i = 0; i < channels->size(); ++i) { | 94 for (size_t i = 0; i < channels->size(); ++i) { |
95 WebMessagePortChannelImpl* webchannel = | 95 WebMessagePortChannelImpl* webchannel = |
96 static_cast<WebMessagePortChannelImpl*>((*channels)[i]); | 96 static_cast<WebMessagePortChannelImpl*>((*channels)[i]); |
97 message_port_ids[i] = webchannel->message_port_id(); | 97 message_port_ids[i] = webchannel->message_port_id(); |
(...skipping 22 matching lines...) Expand all Loading... |
120 for (size_t i = 0; i < channel_array.size(); i++) { | 120 for (size_t i = 0; i < channel_array.size(); i++) { |
121 result_ports[i] = channel_array[i]; | 121 result_ports[i] = channel_array[i]; |
122 } | 122 } |
123 | 123 |
124 channels.swap(result_ports); | 124 channels.swap(result_ports); |
125 message_queue_.pop(); | 125 message_queue_.pop(); |
126 return true; | 126 return true; |
127 } | 127 } |
128 | 128 |
129 void WebMessagePortChannelImpl::Init() { | 129 void WebMessagePortChannelImpl::Init() { |
130 if (MessageLoop::current() != ChildThread::current()->message_loop()) { | 130 if (base::MessageLoop::current() != ChildThread::current()->message_loop()) { |
131 ChildThread::current()->message_loop()->PostTask( | 131 ChildThread::current()->message_loop()->PostTask( |
132 FROM_HERE, | 132 FROM_HERE, base::Bind(&WebMessagePortChannelImpl::Init, this)); |
133 base::Bind(&WebMessagePortChannelImpl::Init, this)); | |
134 return; | 133 return; |
135 } | 134 } |
136 | 135 |
137 if (route_id_ == MSG_ROUTING_NONE) { | 136 if (route_id_ == MSG_ROUTING_NONE) { |
138 DCHECK(message_port_id_ == MSG_ROUTING_NONE); | 137 DCHECK(message_port_id_ == MSG_ROUTING_NONE); |
139 Send(new WorkerProcessHostMsg_CreateMessagePort( | 138 Send(new WorkerProcessHostMsg_CreateMessagePort( |
140 &route_id_, &message_port_id_)); | 139 &route_id_, &message_port_id_)); |
141 } | 140 } |
142 | 141 |
143 ChildThread::current()->AddRoute(route_id_, this); | 142 ChildThread::current()->AddRoute(route_id_, this); |
144 } | 143 } |
145 | 144 |
146 void WebMessagePortChannelImpl::Entangle( | 145 void WebMessagePortChannelImpl::Entangle( |
147 scoped_refptr<WebMessagePortChannelImpl> channel) { | 146 scoped_refptr<WebMessagePortChannelImpl> channel) { |
148 if (MessageLoop::current() != ChildThread::current()->message_loop()) { | 147 if (base::MessageLoop::current() != ChildThread::current()->message_loop()) { |
149 ChildThread::current()->message_loop()->PostTask( | 148 ChildThread::current()->message_loop()->PostTask( |
150 FROM_HERE, | 149 FROM_HERE, |
151 base::Bind(&WebMessagePortChannelImpl::Entangle, this, channel)); | 150 base::Bind(&WebMessagePortChannelImpl::Entangle, this, channel)); |
152 return; | 151 return; |
153 } | 152 } |
154 | 153 |
155 Send(new WorkerProcessHostMsg_Entangle( | 154 Send(new WorkerProcessHostMsg_Entangle( |
156 message_port_id_, channel->message_port_id())); | 155 message_port_id_, channel->message_port_id())); |
157 } | 156 } |
158 | 157 |
159 void WebMessagePortChannelImpl::QueueMessages() { | 158 void WebMessagePortChannelImpl::QueueMessages() { |
160 if (MessageLoop::current() != ChildThread::current()->message_loop()) { | 159 if (base::MessageLoop::current() != ChildThread::current()->message_loop()) { |
161 ChildThread::current()->message_loop()->PostTask( | 160 ChildThread::current()->message_loop()->PostTask( |
162 FROM_HERE, | 161 FROM_HERE, base::Bind(&WebMessagePortChannelImpl::QueueMessages, this)); |
163 base::Bind(&WebMessagePortChannelImpl::QueueMessages, this)); | |
164 return; | 162 return; |
165 } | 163 } |
166 // This message port is being sent elsewhere (perhaps to another process). | 164 // This message port is being sent elsewhere (perhaps to another process). |
167 // The new endpoint needs to receive the queued messages, including ones that | 165 // The new endpoint needs to receive the queued messages, including ones that |
168 // could still be in-flight. So we tell the browser to queue messages, and it | 166 // could still be in-flight. So we tell the browser to queue messages, and it |
169 // sends us an ack, whose receipt we know means that no more messages are | 167 // sends us an ack, whose receipt we know means that no more messages are |
170 // in-flight. We then send the queued messages to the browser, which prepends | 168 // in-flight. We then send the queued messages to the browser, which prepends |
171 // them to the ones it queued and it sends them to the new endpoint. | 169 // them to the ones it queued and it sends them to the new endpoint. |
172 Send(new WorkerProcessHostMsg_QueueMessages(message_port_id_)); | 170 Send(new WorkerProcessHostMsg_QueueMessages(message_port_id_)); |
173 | 171 |
174 // The process could potentially go away while we're still waiting for | 172 // The process could potentially go away while we're still waiting for |
175 // in-flight messages. Ensure it stays alive. | 173 // in-flight messages. Ensure it stays alive. |
176 ChildProcess::current()->AddRefProcess(); | 174 ChildProcess::current()->AddRefProcess(); |
177 } | 175 } |
178 | 176 |
179 void WebMessagePortChannelImpl::Send(IPC::Message* message) { | 177 void WebMessagePortChannelImpl::Send(IPC::Message* message) { |
180 if (MessageLoop::current() != ChildThread::current()->message_loop()) { | 178 if (base::MessageLoop::current() != ChildThread::current()->message_loop()) { |
181 DCHECK(!message->is_sync()); | 179 DCHECK(!message->is_sync()); |
182 ChildThread::current()->message_loop()->PostTask( | 180 ChildThread::current()->message_loop()->PostTask( |
183 FROM_HERE, | 181 FROM_HERE, |
184 base::Bind(&WebMessagePortChannelImpl::Send, this, message)); | 182 base::Bind(&WebMessagePortChannelImpl::Send, this, message)); |
185 return; | 183 return; |
186 } | 184 } |
187 | 185 |
188 ChildThread::current()->Send(message); | 186 ChildThread::current()->Send(message); |
189 } | 187 } |
190 | 188 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 | 243 |
246 Release(); | 244 Release(); |
247 ChildProcess::current()->ReleaseProcess(); | 245 ChildProcess::current()->ReleaseProcess(); |
248 } | 246 } |
249 | 247 |
250 WebMessagePortChannelImpl::Message::Message() {} | 248 WebMessagePortChannelImpl::Message::Message() {} |
251 | 249 |
252 WebMessagePortChannelImpl::Message::~Message() {} | 250 WebMessagePortChannelImpl::Message::~Message() {} |
253 | 251 |
254 } // namespace content | 252 } // namespace content |
OLD | NEW |