OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/browser/message_port_service.h" | 5 #include "content/browser/message_port_service.h" |
6 | 6 |
7 #include "content/common/message_port_messages.h" | 7 #include "content/common/message_port_messages.h" |
8 #include "content/public/browser/browser_thread.h" | 8 #include "content/public/browser/browser_thread.h" |
9 #include "content/public/browser/message_port_delegate.h" | 9 #include "content/public/browser/message_port_delegate.h" |
10 | 10 |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 | 127 |
128 DCHECK(message_ports_[remote_message_port_id].entangled_message_port_id == | 128 DCHECK(message_ports_[remote_message_port_id].entangled_message_port_id == |
129 MSG_ROUTING_NONE); | 129 MSG_ROUTING_NONE); |
130 message_ports_[remote_message_port_id].entangled_message_port_id = | 130 message_ports_[remote_message_port_id].entangled_message_port_id = |
131 local_message_port_id; | 131 local_message_port_id; |
132 } | 132 } |
133 | 133 |
134 void MessagePortService::PostMessage( | 134 void MessagePortService::PostMessage( |
135 int sender_message_port_id, | 135 int sender_message_port_id, |
136 const MessagePortMessage& message, | 136 const MessagePortMessage& message, |
137 const std::vector<int>& sent_message_port_ids) { | 137 const std::vector<TransferredMessagePort>& sent_message_ports) { |
138 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 138 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
139 if (!message_ports_.count(sender_message_port_id)) { | 139 if (!message_ports_.count(sender_message_port_id)) { |
140 NOTREACHED(); | 140 NOTREACHED(); |
141 return; | 141 return; |
142 } | 142 } |
143 | 143 |
144 int entangled_message_port_id = | 144 int entangled_message_port_id = |
145 message_ports_[sender_message_port_id].entangled_message_port_id; | 145 message_ports_[sender_message_port_id].entangled_message_port_id; |
146 if (entangled_message_port_id == MSG_ROUTING_NONE) | 146 if (entangled_message_port_id == MSG_ROUTING_NONE) |
147 return; // Process could have crashed. | 147 return; // Process could have crashed. |
148 | 148 |
149 if (!message_ports_.count(entangled_message_port_id)) { | 149 if (!message_ports_.count(entangled_message_port_id)) { |
150 NOTREACHED(); | 150 NOTREACHED(); |
151 return; | 151 return; |
152 } | 152 } |
153 | 153 |
154 PostMessageTo(entangled_message_port_id, message, sent_message_port_ids); | 154 PostMessageTo(entangled_message_port_id, message, sent_message_ports); |
155 } | 155 } |
156 | 156 |
157 void MessagePortService::PostMessageTo( | 157 void MessagePortService::PostMessageTo( |
158 int message_port_id, | 158 int message_port_id, |
159 const MessagePortMessage& message, | 159 const MessagePortMessage& message, |
160 const std::vector<int>& sent_message_port_ids) { | 160 const std::vector<TransferredMessagePort>& sent_message_ports) { |
161 if (!message_ports_.count(message_port_id)) { | 161 if (!message_ports_.count(message_port_id)) { |
162 NOTREACHED(); | 162 NOTREACHED(); |
163 return; | 163 return; |
164 } | 164 } |
165 for (size_t i = 0; i < sent_message_port_ids.size(); ++i) { | 165 for (size_t i = 0; i < sent_message_ports.size(); ++i) { |
166 if (!message_ports_.count(sent_message_port_ids[i])) { | 166 if (!message_ports_.count(sent_message_ports[i].id)) { |
167 NOTREACHED(); | 167 NOTREACHED(); |
168 return; | 168 return; |
169 } | 169 } |
170 } | 170 } |
171 | 171 |
172 MessagePort& entangled_port = message_ports_[message_port_id]; | 172 MessagePort& entangled_port = message_ports_[message_port_id]; |
173 | |
174 std::vector<MessagePort*> sent_ports(sent_message_port_ids.size()); | |
175 for (size_t i = 0; i < sent_message_port_ids.size(); ++i) | |
176 sent_ports[i] = &message_ports_[sent_message_port_ids[i]]; | |
177 | |
178 if (entangled_port.queue_messages()) { | 173 if (entangled_port.queue_messages()) { |
179 // If the target port is currently holding messages because the destination | 174 // If the target port is currently holding messages because the destination |
180 // renderer isn't available yet, all message ports being sent should also be | 175 // renderer isn't available yet, all message ports being sent should also be |
181 // put in this state. | 176 // put in this state. |
182 if (entangled_port.hold_messages_for_destination) { | 177 if (entangled_port.hold_messages_for_destination) { |
183 for (int sent_message_port_id : sent_message_port_ids) | 178 for (const auto& port : sent_message_ports) |
184 HoldMessages(sent_message_port_id); | 179 HoldMessages(port.id); |
185 } | 180 } |
186 entangled_port.queued_messages.push_back( | 181 entangled_port.queued_messages.push_back( |
187 std::make_pair(message, sent_message_port_ids)); | 182 std::make_pair(message, sent_message_ports)); |
188 return; | 183 return; |
189 } | 184 } |
190 | 185 |
191 if (!entangled_port.delegate) { | 186 if (!entangled_port.delegate) { |
192 NOTREACHED(); | 187 NOTREACHED(); |
193 return; | 188 return; |
194 } | 189 } |
195 | 190 |
196 // Now send the message to the entangled port. | 191 // Now send the message to the entangled port. |
197 entangled_port.delegate->SendMessage(entangled_port.route_id, message, | 192 entangled_port.delegate->SendMessage(entangled_port.route_id, message, |
198 sent_message_port_ids); | 193 sent_message_ports); |
199 } | 194 } |
200 | 195 |
201 void MessagePortService::QueueMessages(int message_port_id) { | 196 void MessagePortService::QueueMessages(int message_port_id) { |
202 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 197 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
203 if (!message_ports_.count(message_port_id)) { | 198 if (!message_ports_.count(message_port_id)) { |
204 NOTREACHED(); | 199 NOTREACHED(); |
205 return; | 200 return; |
206 } | 201 } |
207 | 202 |
208 MessagePort& port = message_ports_[message_port_id]; | 203 MessagePort& port = message_ports_[message_port_id]; |
(...skipping 15 matching lines...) Expand all Loading... |
224 | 219 |
225 // Send the queued messages to the port again. This time they'll reach the | 220 // Send the queued messages to the port again. This time they'll reach the |
226 // new location. | 221 // new location. |
227 MessagePort& port = message_ports_[message_port_id]; | 222 MessagePort& port = message_ports_[message_port_id]; |
228 port.queue_for_inflight_messages = false; | 223 port.queue_for_inflight_messages = false; |
229 | 224 |
230 // If the port is currently holding messages waiting for the target renderer, | 225 // If the port is currently holding messages waiting for the target renderer, |
231 // all ports in messages being sent to the port should also be put on hold. | 226 // all ports in messages being sent to the port should also be put on hold. |
232 if (port.hold_messages_for_destination) { | 227 if (port.hold_messages_for_destination) { |
233 for (const auto& message : queued_messages) | 228 for (const auto& message : queued_messages) |
234 for (int sent_message_port_id : message.second) | 229 for (const TransferredMessagePort& sent_port : message.second) |
235 HoldMessages(sent_message_port_id); | 230 HoldMessages(sent_port.id); |
236 } | 231 } |
237 | 232 |
238 port.queued_messages.insert(port.queued_messages.begin(), | 233 port.queued_messages.insert(port.queued_messages.begin(), |
239 queued_messages.begin(), | 234 queued_messages.begin(), |
240 queued_messages.end()); | 235 queued_messages.end()); |
241 | 236 |
242 if (port.should_be_destroyed) | 237 if (port.should_be_destroyed) |
243 ClosePort(message_port_id); | 238 ClosePort(message_port_id); |
244 else | 239 else |
245 SendQueuedMessagesIfPossible(message_port_id); | 240 SendQueuedMessagesIfPossible(message_port_id); |
(...skipping 19 matching lines...) Expand all Loading... |
265 | 260 |
266 void MessagePortService::HoldMessages(int message_port_id) { | 261 void MessagePortService::HoldMessages(int message_port_id) { |
267 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 262 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
268 if (!message_ports_.count(message_port_id)) { | 263 if (!message_ports_.count(message_port_id)) { |
269 NOTREACHED(); | 264 NOTREACHED(); |
270 return; | 265 return; |
271 } | 266 } |
272 | 267 |
273 // Any ports in messages currently in the queue should also be put on hold. | 268 // Any ports in messages currently in the queue should also be put on hold. |
274 for (const auto& message : message_ports_[message_port_id].queued_messages) | 269 for (const auto& message : message_ports_[message_port_id].queued_messages) |
275 for (int sent_message_port_id : message.second) | 270 for (const TransferredMessagePort& sent_port : message.second) |
276 HoldMessages(sent_message_port_id); | 271 HoldMessages(sent_port.id); |
277 | 272 |
278 message_ports_[message_port_id].hold_messages_for_destination = true; | 273 message_ports_[message_port_id].hold_messages_for_destination = true; |
279 } | 274 } |
280 | 275 |
281 void MessagePortService::ClosePort(int message_port_id) { | 276 void MessagePortService::ClosePort(int message_port_id) { |
282 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 277 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
283 if (!message_ports_.count(message_port_id)) { | 278 if (!message_ports_.count(message_port_id)) { |
284 NOTREACHED(); | 279 NOTREACHED(); |
285 return; | 280 return; |
286 } | 281 } |
287 | 282 |
288 if (message_ports_[message_port_id].queue_for_inflight_messages) { | 283 if (message_ports_[message_port_id].queue_for_inflight_messages) { |
289 message_ports_[message_port_id].should_be_destroyed = true; | 284 message_ports_[message_port_id].should_be_destroyed = true; |
290 return; | 285 return; |
291 } | 286 } |
292 | 287 |
293 // First close any message ports in the queue for this message port. | 288 // First close any message ports in the queue for this message port. |
294 for (const auto& message : message_ports_[message_port_id].queued_messages) | 289 for (const auto& message : message_ports_[message_port_id].queued_messages) |
295 for (int sent_message_port_id : message.second) | 290 for (const TransferredMessagePort& sent_port : message.second) |
296 ClosePort(sent_message_port_id); | 291 ClosePort(sent_port.id); |
297 | 292 |
298 Erase(message_port_id); | 293 Erase(message_port_id); |
299 } | 294 } |
300 | 295 |
301 void MessagePortService::ReleaseMessages(int message_port_id) { | 296 void MessagePortService::ReleaseMessages(int message_port_id) { |
302 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 297 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
303 if (!message_ports_.count(message_port_id)) { | 298 if (!message_ports_.count(message_port_id)) { |
304 NOTREACHED(); | 299 NOTREACHED(); |
305 return; | 300 return; |
306 } | 301 } |
(...skipping 11 matching lines...) Expand all Loading... |
318 // Do the disentanglement (and be paranoid about the other side existing | 313 // Do the disentanglement (and be paranoid about the other side existing |
319 // just in case something unusual happened during entanglement). | 314 // just in case something unusual happened during entanglement). |
320 if (message_ports_.count(entangled_id)) { | 315 if (message_ports_.count(entangled_id)) { |
321 message_ports_[entangled_id].entangled_message_port_id = MSG_ROUTING_NONE; | 316 message_ports_[entangled_id].entangled_message_port_id = MSG_ROUTING_NONE; |
322 } | 317 } |
323 } | 318 } |
324 message_ports_.erase(erase_item); | 319 message_ports_.erase(erase_item); |
325 } | 320 } |
326 | 321 |
327 } // namespace content | 322 } // namespace content |
OLD | NEW |