Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / message_port_service.h
blobd5e3379d57d7eb724bb937b7b57ef13e51e761c2
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 #ifndef CONTENT_BROWSER_MESSAGE_PORT_SERVICE_H_
6 #define CONTENT_BROWSER_MESSAGE_PORT_SERVICE_H_
8 #include <map>
9 #include <utility>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/memory/singleton.h"
14 #include "base/strings/string16.h"
15 #include "content/common/content_export.h"
16 #include "content/public/common/message_port_types.h"
17 #include "ipc/ipc_message.h"
19 namespace content {
20 class MessagePortDelegate;
22 class CONTENT_EXPORT MessagePortService {
23 public:
24 typedef std::vector<std::pair<content::MessagePortMessage,
25 std::vector<TransferredMessagePort>>>
26 QueuedMessages;
28 // Returns the MessagePortService singleton.
29 static MessagePortService* GetInstance();
31 // These methods correspond to the message port related IPCs.
32 void Create(int route_id,
33 MessagePortDelegate* delegate,
34 int* message_port_id);
35 void Destroy(int message_port_id);
36 void Entangle(int local_message_port_id, int remote_message_port_id);
37 void PostMessage(
38 int sender_message_port_id,
39 const MessagePortMessage& message,
40 const std::vector<TransferredMessagePort>& sent_message_ports);
41 void QueueMessages(int message_port_id);
42 void SendQueuedMessages(int message_port_id,
43 const QueuedMessages& queued_messages);
44 void ReleaseMessages(int message_port_id);
46 // Updates the information needed to reach a message port when it's sent to a
47 // (possibly different) process.
48 void UpdateMessagePort(int message_port_id,
49 MessagePortDelegate* delegate,
50 int routing_id);
52 // The message port is being transferred to a new renderer process, but the
53 // code doing that isn't able to immediately update the message port with a
54 // new filter and routing_id. This queues up all messages sent to this port
55 // until later ReleaseMessages is called for this port (this will happen
56 // automatically as soon as a WebMessagePortChannelImpl instance is created
57 // for this port in the renderer. The browser side code is still responsible
58 // for updating the port with a new filter before that happens. If ultimately
59 // transfering the port to a new process fails, ClosePort should be called to
60 // clean up the port.
61 void HoldMessages(int message_port_id);
63 // Closes and cleans up the message port.
64 void ClosePort(int message_port_id);
66 void OnMessagePortDelegateClosing(MessagePortDelegate* filter);
68 // Attempts to send the queued messages for a message port.
69 void SendQueuedMessagesIfPossible(int message_port_id);
71 private:
72 friend struct DefaultSingletonTraits<MessagePortService>;
74 MessagePortService();
75 ~MessagePortService();
77 void PostMessageTo(
78 int message_port_id,
79 const MessagePortMessage& message,
80 const std::vector<TransferredMessagePort>& sent_message_ports);
82 // Handles the details of removing a message port id. Before calling this,
83 // verify that the message port id exists.
84 void Erase(int message_port_id);
86 struct MessagePort;
87 typedef std::map<int, MessagePort> MessagePorts;
88 MessagePorts message_ports_;
90 // We need globally unique identifiers for each message port.
91 int next_message_port_id_;
93 DISALLOW_COPY_AND_ASSIGN(MessagePortService);
96 } // namespace content
98 #endif // CONTENT_BROWSER_MESSAGE_PORT_SERVICE_H_