Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / content / browser / message_port_service.cc
blob915ae483d3194cfdaff3501b56e1059639d386cf
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
3 // found in the LICENSE file.
5 #include "content/browser/message_port_service.h"
7 #include "content/common/message_port_messages.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "content/public/browser/message_port_delegate.h"
11 namespace content {
13 struct MessagePortService::MessagePort {
14 // |delegate| and |route_id| are what we need to send messages to the port.
15 // |delegate| is just a raw pointer since it notifies us by calling
16 // OnMessagePortDelegateClosing before it gets destroyed.
17 MessagePortDelegate* delegate;
18 int route_id;
19 // A globally unique id for this message port.
20 int message_port_id;
21 // The globally unique id of the entangled message port.
22 int entangled_message_port_id;
23 // If true, all messages to this message port are queued and not delivered.
24 // This is needed so that when a message port is sent between processes all
25 // pending message get transferred. There are two possibilities for pending
26 // messages: either they are already received by the child process, or they're
27 // in-flight. This flag ensures that the latter type get flushed through the
28 // system.
29 // This flag should only be set to true in response to
30 // MessagePortHostMsg_QueueMessages.
31 bool queue_for_inflight_messages;
32 // If true, all messages to this message port are queued and not delivered.
33 // This is needed so that when a message port is sent to a new process all
34 // messages are held in the browser process until the destination process is
35 // ready to receive messages. This flag is set true when a message port is
36 // transferred to a different process but there isn't immediately a
37 // MessagePortDelegate available for that new process. Once the
38 // destination process is ready to receive messages it sends
39 // MessagePortHostMsg_ReleaseMessages to set this flag to false.
40 bool hold_messages_for_destination;
41 // Returns true if messages should be queued for either reason.
42 bool queue_messages() const {
43 return queue_for_inflight_messages || hold_messages_for_destination;
45 // If true, the message port should be destroyed but was currently still
46 // waiting for a SendQueuedMessages message from a renderer. As soon as that
47 // message is received the port will actually be destroyed.
48 bool should_be_destroyed;
49 QueuedMessages queued_messages;
52 MessagePortService* MessagePortService::GetInstance() {
53 return Singleton<MessagePortService>::get();
56 MessagePortService::MessagePortService()
57 : next_message_port_id_(0) {
60 MessagePortService::~MessagePortService() {
63 void MessagePortService::UpdateMessagePort(int message_port_id,
64 MessagePortDelegate* delegate,
65 int routing_id) {
66 DCHECK_CURRENTLY_ON(BrowserThread::IO);
67 if (!message_ports_.count(message_port_id)) {
68 NOTREACHED();
69 return;
72 MessagePort& port = message_ports_[message_port_id];
73 port.delegate = delegate;
74 port.route_id = routing_id;
77 void MessagePortService::OnMessagePortDelegateClosing(
78 MessagePortDelegate* delegate) {
79 DCHECK_CURRENTLY_ON(BrowserThread::IO);
80 // Check if the (possibly) crashed process had any message ports.
81 for (MessagePorts::iterator iter = message_ports_.begin();
82 iter != message_ports_.end();) {
83 MessagePorts::iterator cur_item = iter++;
84 if (cur_item->second.delegate == delegate) {
85 Erase(cur_item->first);
90 void MessagePortService::Create(int route_id,
91 MessagePortDelegate* delegate,
92 int* message_port_id) {
93 DCHECK_CURRENTLY_ON(BrowserThread::IO);
94 *message_port_id = ++next_message_port_id_;
96 MessagePort port;
97 port.delegate = delegate;
98 port.route_id = route_id;
99 port.message_port_id = *message_port_id;
100 port.entangled_message_port_id = MSG_ROUTING_NONE;
101 port.queue_for_inflight_messages = false;
102 port.hold_messages_for_destination = false;
103 port.should_be_destroyed = false;
104 message_ports_[*message_port_id] = port;
107 void MessagePortService::Destroy(int message_port_id) {
108 DCHECK_CURRENTLY_ON(BrowserThread::IO);
109 if (!message_ports_.count(message_port_id)) {
110 NOTREACHED();
111 return;
114 DCHECK(message_ports_[message_port_id].queued_messages.empty());
116 Erase(message_port_id);
119 void MessagePortService::Entangle(int local_message_port_id,
120 int remote_message_port_id) {
121 DCHECK_CURRENTLY_ON(BrowserThread::IO);
122 if (!message_ports_.count(local_message_port_id) ||
123 !message_ports_.count(remote_message_port_id)) {
124 NOTREACHED();
125 return;
128 DCHECK(message_ports_[remote_message_port_id].entangled_message_port_id ==
129 MSG_ROUTING_NONE);
130 message_ports_[remote_message_port_id].entangled_message_port_id =
131 local_message_port_id;
134 void MessagePortService::PostMessage(
135 int sender_message_port_id,
136 const MessagePortMessage& message,
137 const std::vector<TransferredMessagePort>& sent_message_ports) {
138 DCHECK_CURRENTLY_ON(BrowserThread::IO);
139 if (!message_ports_.count(sender_message_port_id)) {
140 NOTREACHED();
141 return;
144 int entangled_message_port_id =
145 message_ports_[sender_message_port_id].entangled_message_port_id;
146 if (entangled_message_port_id == MSG_ROUTING_NONE)
147 return; // Process could have crashed.
149 if (!message_ports_.count(entangled_message_port_id)) {
150 NOTREACHED();
151 return;
154 PostMessageTo(entangled_message_port_id, message, sent_message_ports);
157 void MessagePortService::PostMessageTo(
158 int message_port_id,
159 const MessagePortMessage& message,
160 const std::vector<TransferredMessagePort>& sent_message_ports) {
161 if (!message_ports_.count(message_port_id)) {
162 NOTREACHED();
163 return;
165 for (size_t i = 0; i < sent_message_ports.size(); ++i) {
166 if (!message_ports_.count(sent_message_ports[i].id)) {
167 NOTREACHED();
168 return;
172 MessagePort& entangled_port = message_ports_[message_port_id];
173 if (entangled_port.queue_messages()) {
174 // If the target port is currently holding messages because the destination
175 // renderer isn't available yet, all message ports being sent should also be
176 // put in this state.
177 if (entangled_port.hold_messages_for_destination) {
178 for (const auto& port : sent_message_ports)
179 HoldMessages(port.id);
181 entangled_port.queued_messages.push_back(
182 std::make_pair(message, sent_message_ports));
183 return;
186 if (!entangled_port.delegate) {
187 NOTREACHED();
188 return;
191 // Now send the message to the entangled port.
192 entangled_port.delegate->SendMessage(entangled_port.route_id, message,
193 sent_message_ports);
196 void MessagePortService::QueueMessages(int message_port_id) {
197 DCHECK_CURRENTLY_ON(BrowserThread::IO);
198 if (!message_ports_.count(message_port_id)) {
199 NOTREACHED();
200 return;
203 MessagePort& port = message_ports_[message_port_id];
204 if (port.delegate) {
205 port.delegate->SendMessagesAreQueued(port.route_id);
206 port.queue_for_inflight_messages = true;
207 port.delegate = NULL;
211 void MessagePortService::SendQueuedMessages(
212 int message_port_id,
213 const QueuedMessages& queued_messages) {
214 DCHECK_CURRENTLY_ON(BrowserThread::IO);
215 if (!message_ports_.count(message_port_id)) {
216 NOTREACHED();
217 return;
220 // Send the queued messages to the port again. This time they'll reach the
221 // new location.
222 MessagePort& port = message_ports_[message_port_id];
223 port.queue_for_inflight_messages = false;
225 // If the port is currently holding messages waiting for the target renderer,
226 // all ports in messages being sent to the port should also be put on hold.
227 if (port.hold_messages_for_destination) {
228 for (const auto& message : queued_messages)
229 for (const TransferredMessagePort& sent_port : message.second)
230 HoldMessages(sent_port.id);
233 port.queued_messages.insert(port.queued_messages.begin(),
234 queued_messages.begin(),
235 queued_messages.end());
237 if (port.should_be_destroyed)
238 ClosePort(message_port_id);
239 else
240 SendQueuedMessagesIfPossible(message_port_id);
243 void MessagePortService::SendQueuedMessagesIfPossible(int message_port_id) {
244 DCHECK_CURRENTLY_ON(BrowserThread::IO);
245 if (!message_ports_.count(message_port_id)) {
246 NOTREACHED();
247 return;
250 MessagePort& port = message_ports_[message_port_id];
251 if (port.queue_messages() || !port.delegate)
252 return;
254 for (QueuedMessages::iterator iter = port.queued_messages.begin();
255 iter != port.queued_messages.end(); ++iter) {
256 PostMessageTo(message_port_id, iter->first, iter->second);
258 port.queued_messages.clear();
261 void MessagePortService::HoldMessages(int message_port_id) {
262 DCHECK_CURRENTLY_ON(BrowserThread::IO);
263 if (!message_ports_.count(message_port_id)) {
264 NOTREACHED();
265 return;
268 // Any ports in messages currently in the queue should also be put on hold.
269 for (const auto& message : message_ports_[message_port_id].queued_messages)
270 for (const TransferredMessagePort& sent_port : message.second)
271 HoldMessages(sent_port.id);
273 message_ports_[message_port_id].hold_messages_for_destination = true;
276 void MessagePortService::ClosePort(int message_port_id) {
277 DCHECK_CURRENTLY_ON(BrowserThread::IO);
278 if (!message_ports_.count(message_port_id)) {
279 NOTREACHED();
280 return;
283 if (message_ports_[message_port_id].queue_for_inflight_messages) {
284 message_ports_[message_port_id].should_be_destroyed = true;
285 return;
288 // First close any message ports in the queue for this message port.
289 for (const auto& message : message_ports_[message_port_id].queued_messages)
290 for (const TransferredMessagePort& sent_port : message.second)
291 ClosePort(sent_port.id);
293 Erase(message_port_id);
296 void MessagePortService::ReleaseMessages(int message_port_id) {
297 DCHECK_CURRENTLY_ON(BrowserThread::IO);
298 if (!message_ports_.count(message_port_id)) {
299 NOTREACHED();
300 return;
303 message_ports_[message_port_id].hold_messages_for_destination = false;
304 SendQueuedMessagesIfPossible(message_port_id);
307 void MessagePortService::Erase(int message_port_id) {
308 MessagePorts::iterator erase_item = message_ports_.find(message_port_id);
309 DCHECK(erase_item != message_ports_.end());
311 int entangled_id = erase_item->second.entangled_message_port_id;
312 if (entangled_id != MSG_ROUTING_NONE) {
313 // Do the disentanglement (and be paranoid about the other side existing
314 // just in case something unusual happened during entanglement).
315 if (message_ports_.count(entangled_id)) {
316 message_ports_[entangled_id].entangled_message_port_id = MSG_ROUTING_NONE;
319 message_ports_.erase(erase_item);
322 } // namespace content