blob: 3d0342b4374da6af160dfc05cbd906b6d739eaab [file] [log] [blame]
[email protected]83fc55e42013-10-15 10:57:491// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]65412272010-12-21 20:03:242// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]83fc55e42013-10-15 10:57:495#ifndef CONTENT_BROWSER_MESSAGE_PORT_SERVICE_H_
6#define CONTENT_BROWSER_MESSAGE_PORT_SERVICE_H_
[email protected]65412272010-12-21 20:03:247
8#include <map>
9#include <utility>
10#include <vector>
11
12#include "base/basictypes.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/singleton.h"
[email protected]348fbaac2013-06-11 06:31:5114#include "base/strings/string16.h"
sgurund2a430602015-01-23 20:54:0515#include "content/common/content_export.h"
mek09ac29cee2015-02-27 19:12:3916#include "content/public/common/message_port_types.h"
[email protected]65412272010-12-21 20:03:2417#include "ipc/ipc_message.h"
18
[email protected]e6047e4a2012-10-25 01:34:4919namespace content {
meke6b83032014-12-19 23:35:3420class MessagePortDelegate;
[email protected]65412272010-12-21 20:03:2421
sgurund2a430602015-01-23 20:54:0522class CONTENT_EXPORT MessagePortService {
[email protected]65412272010-12-21 20:03:2423 public:
mek5b679c92015-02-28 02:38:0624 typedef std::vector<std::pair<content::MessagePortMessage,
25 std::vector<TransferredMessagePort>>>
[email protected]fcf75d42013-12-03 20:11:2626 QueuedMessages;
[email protected]65412272010-12-21 20:03:2427
28 // Returns the MessagePortService singleton.
29 static MessagePortService* GetInstance();
30
31 // These methods correspond to the message port related IPCs.
[email protected]83fc55e42013-10-15 10:57:4932 void Create(int route_id,
meke6b83032014-12-19 23:35:3433 MessagePortDelegate* delegate,
[email protected]83fc55e42013-10-15 10:57:4934 int* message_port_id);
[email protected]65412272010-12-21 20:03:2435 void Destroy(int message_port_id);
36 void Entangle(int local_message_port_id, int remote_message_port_id);
mek5b679c92015-02-28 02:38:0637 void PostMessage(
38 int sender_message_port_id,
39 const MessagePortMessage& message,
40 const std::vector<TransferredMessagePort>& sent_message_ports);
[email protected]65412272010-12-21 20:03:2441 void QueueMessages(int message_port_id);
42 void SendQueuedMessages(int message_port_id,
43 const QueuedMessages& queued_messages);
mekddb153282014-11-21 00:31:2044 void ReleaseMessages(int message_port_id);
[email protected]65412272010-12-21 20:03:2445
46 // Updates the information needed to reach a message port when it's sent to a
47 // (possibly different) process.
meke6b83032014-12-19 23:35:3448 void UpdateMessagePort(int message_port_id,
49 MessagePortDelegate* delegate,
50 int routing_id);
[email protected]65412272010-12-21 20:03:2451
mek9e86f80a2015-05-20 17:51:4152 // Returns the current information by which a message port can be reached.
53 // Either |delegate| or |routing_id| can be null, in which case that bit of
54 // information is not returned.
55 void GetMessagePortInfo(int message_port_id,
56 MessagePortDelegate** delegate,
57 int* routing_id);
58
mekddb153282014-11-21 00:31:2059 // The message port is being transferred to a new renderer process, but the
60 // code doing that isn't able to immediately update the message port with a
61 // new filter and routing_id. This queues up all messages sent to this port
62 // until later ReleaseMessages is called for this port (this will happen
63 // automatically as soon as a WebMessagePortChannelImpl instance is created
64 // for this port in the renderer. The browser side code is still responsible
65 // for updating the port with a new filter before that happens. If ultimately
66 // transfering the port to a new process fails, ClosePort should be called to
67 // clean up the port.
68 void HoldMessages(int message_port_id);
69
70 // Closes and cleans up the message port.
71 void ClosePort(int message_port_id);
72
meke6b83032014-12-19 23:35:3473 void OnMessagePortDelegateClosing(MessagePortDelegate* filter);
[email protected]65412272010-12-21 20:03:2474
75 // Attempts to send the queued messages for a message port.
76 void SendQueuedMessagesIfPossible(int message_port_id);
77
78 private:
79 friend struct DefaultSingletonTraits<MessagePortService>;
80
81 MessagePortService();
82 ~MessagePortService();
83
mek5b679c92015-02-28 02:38:0684 void PostMessageTo(
85 int message_port_id,
86 const MessagePortMessage& message,
87 const std::vector<TransferredMessagePort>& sent_message_ports);
[email protected]65412272010-12-21 20:03:2488
89 // Handles the details of removing a message port id. Before calling this,
90 // verify that the message port id exists.
91 void Erase(int message_port_id);
92
93 struct MessagePort;
94 typedef std::map<int, MessagePort> MessagePorts;
95 MessagePorts message_ports_;
96
97 // We need globally unique identifiers for each message port.
98 int next_message_port_id_;
99
100 DISALLOW_COPY_AND_ASSIGN(MessagePortService);
101};
102
[email protected]e6047e4a2012-10-25 01:34:49103} // namespace content
104
[email protected]83fc55e42013-10-15 10:57:49105#endif // CONTENT_BROWSER_MESSAGE_PORT_SERVICE_H_