[email protected] | 83fc55e4 | 2013-10-15 10:57:49 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | 6541227 | 2010-12-21 20:03:24 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 83fc55e4 | 2013-10-15 10:57:49 | [diff] [blame] | 5 | #ifndef CONTENT_BROWSER_MESSAGE_PORT_SERVICE_H_ |
| 6 | #define CONTENT_BROWSER_MESSAGE_PORT_SERVICE_H_ |
[email protected] | 6541227 | 2010-12-21 20:03:24 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | #include <utility> |
| 10 | #include <vector> |
| 11 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 13 | #include "base/memory/singleton.h" |
[email protected] | 348fbaac | 2013-06-11 06:31:51 | [diff] [blame] | 14 | #include "base/strings/string16.h" |
sgurun | d2a43060 | 2015-01-23 20:54:05 | [diff] [blame] | 15 | #include "content/common/content_export.h" |
[email protected] | 6541227 | 2010-12-21 20:03:24 | [diff] [blame] | 16 | #include "ipc/ipc_message.h" |
| 17 | |
[email protected] | e6047e4a | 2012-10-25 01:34:49 | [diff] [blame] | 18 | namespace content { |
mek | e6b8303 | 2014-12-19 23:35:34 | [diff] [blame] | 19 | class MessagePortDelegate; |
[email protected] | 6541227 | 2010-12-21 20:03:24 | [diff] [blame] | 20 | |
sgurun | d2a43060 | 2015-01-23 20:54:05 | [diff] [blame] | 21 | class CONTENT_EXPORT MessagePortService { |
[email protected] | 6541227 | 2010-12-21 20:03:24 | [diff] [blame] | 22 | public: |
mek | d27aef56 | 2016-05-13 18:33:13 | [diff] [blame] | 23 | typedef std::vector<std::pair<base::string16, std::vector<int>>> |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 24 | QueuedMessages; |
[email protected] | 6541227 | 2010-12-21 20:03:24 | [diff] [blame] | 25 | |
| 26 | // Returns the MessagePortService singleton. |
| 27 | static MessagePortService* GetInstance(); |
| 28 | |
| 29 | // These methods correspond to the message port related IPCs. |
[email protected] | 83fc55e4 | 2013-10-15 10:57:49 | [diff] [blame] | 30 | void Create(int route_id, |
mek | e6b8303 | 2014-12-19 23:35:34 | [diff] [blame] | 31 | MessagePortDelegate* delegate, |
[email protected] | 83fc55e4 | 2013-10-15 10:57:49 | [diff] [blame] | 32 | int* message_port_id); |
[email protected] | 6541227 | 2010-12-21 20:03:24 | [diff] [blame] | 33 | void Destroy(int message_port_id); |
| 34 | void Entangle(int local_message_port_id, int remote_message_port_id); |
mek | 5b679c9 | 2015-02-28 02:38:06 | [diff] [blame] | 35 | void PostMessage( |
| 36 | int sender_message_port_id, |
mek | d27aef56 | 2016-05-13 18:33:13 | [diff] [blame] | 37 | const base::string16& message, |
| 38 | const std::vector<int>& sent_message_ports); |
[email protected] | 6541227 | 2010-12-21 20:03:24 | [diff] [blame] | 39 | void QueueMessages(int message_port_id); |
| 40 | void SendQueuedMessages(int message_port_id, |
| 41 | const QueuedMessages& queued_messages); |
mek | ddb15328 | 2014-11-21 00:31:20 | [diff] [blame] | 42 | void ReleaseMessages(int message_port_id); |
[email protected] | 6541227 | 2010-12-21 20:03:24 | [diff] [blame] | 43 | |
| 44 | // Updates the information needed to reach a message port when it's sent to a |
| 45 | // (possibly different) process. |
mek | e6b8303 | 2014-12-19 23:35:34 | [diff] [blame] | 46 | void UpdateMessagePort(int message_port_id, |
| 47 | MessagePortDelegate* delegate, |
| 48 | int routing_id); |
[email protected] | 6541227 | 2010-12-21 20:03:24 | [diff] [blame] | 49 | |
mek | 9e86f80a | 2015-05-20 17:51:41 | [diff] [blame] | 50 | // Returns the current information by which a message port can be reached. |
| 51 | // Either |delegate| or |routing_id| can be null, in which case that bit of |
| 52 | // information is not returned. |
| 53 | void GetMessagePortInfo(int message_port_id, |
| 54 | MessagePortDelegate** delegate, |
| 55 | int* routing_id); |
| 56 | |
mek | ddb15328 | 2014-11-21 00:31:20 | [diff] [blame] | 57 | // The message port is being transferred to a new renderer process, but the |
| 58 | // code doing that isn't able to immediately update the message port with a |
| 59 | // new filter and routing_id. This queues up all messages sent to this port |
| 60 | // until later ReleaseMessages is called for this port (this will happen |
| 61 | // automatically as soon as a WebMessagePortChannelImpl instance is created |
| 62 | // for this port in the renderer. The browser side code is still responsible |
| 63 | // for updating the port with a new filter before that happens. If ultimately |
| 64 | // transfering the port to a new process fails, ClosePort should be called to |
| 65 | // clean up the port. |
| 66 | void HoldMessages(int message_port_id); |
| 67 | |
nhiroki | f6da9a0 | 2016-03-22 00:26:26 | [diff] [blame] | 68 | // Returns true if messages for a message port are on hold. |
| 69 | bool AreMessagesHeld(int message_port_id); |
| 70 | |
mek | ddb15328 | 2014-11-21 00:31:20 | [diff] [blame] | 71 | // Closes and cleans up the message port. |
| 72 | void ClosePort(int message_port_id); |
| 73 | |
mek | e6b8303 | 2014-12-19 23:35:34 | [diff] [blame] | 74 | void OnMessagePortDelegateClosing(MessagePortDelegate* filter); |
[email protected] | 6541227 | 2010-12-21 20:03:24 | [diff] [blame] | 75 | |
| 76 | // Attempts to send the queued messages for a message port. |
| 77 | void SendQueuedMessagesIfPossible(int message_port_id); |
| 78 | |
| 79 | private: |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 80 | friend struct base::DefaultSingletonTraits<MessagePortService>; |
[email protected] | 6541227 | 2010-12-21 20:03:24 | [diff] [blame] | 81 | |
| 82 | MessagePortService(); |
| 83 | ~MessagePortService(); |
| 84 | |
mek | 5b679c9 | 2015-02-28 02:38:06 | [diff] [blame] | 85 | void PostMessageTo( |
| 86 | int message_port_id, |
mek | d27aef56 | 2016-05-13 18:33:13 | [diff] [blame] | 87 | const base::string16& message, |
| 88 | const std::vector<int>& sent_message_ports); |
[email protected] | 6541227 | 2010-12-21 20:03:24 | [diff] [blame] | 89 | |
| 90 | // Handles the details of removing a message port id. Before calling this, |
| 91 | // verify that the message port id exists. |
| 92 | void Erase(int message_port_id); |
| 93 | |
| 94 | struct MessagePort; |
| 95 | typedef std::map<int, MessagePort> MessagePorts; |
| 96 | MessagePorts message_ports_; |
| 97 | |
| 98 | // We need globally unique identifiers for each message port. |
| 99 | int next_message_port_id_; |
| 100 | |
| 101 | DISALLOW_COPY_AND_ASSIGN(MessagePortService); |
| 102 | }; |
| 103 | |
[email protected] | e6047e4a | 2012-10-25 01:34:49 | [diff] [blame] | 104 | } // namespace content |
| 105 | |
[email protected] | 83fc55e4 | 2013-10-15 10:57:49 | [diff] [blame] | 106 | #endif // CONTENT_BROWSER_MESSAGE_PORT_SERVICE_H_ |