blob: 5623e94cf07138084b286658c4ef8e0f2e4a5230 [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"
[email protected]65412272010-12-21 20:03:2416#include "ipc/ipc_message.h"
17
[email protected]e6047e4a2012-10-25 01:34:4918namespace content {
meke6b83032014-12-19 23:35:3419class MessagePortDelegate;
[email protected]65412272010-12-21 20:03:2420
sgurund2a430602015-01-23 20:54:0521class CONTENT_EXPORT MessagePortService {
[email protected]65412272010-12-21 20:03:2422 public:
[email protected]fcf75d42013-12-03 20:11:2623 typedef std::vector<std::pair<base::string16, std::vector<int> > >
24 QueuedMessages;
[email protected]65412272010-12-21 20:03:2425
26 // Returns the MessagePortService singleton.
27 static MessagePortService* GetInstance();
28
29 // These methods correspond to the message port related IPCs.
[email protected]83fc55e42013-10-15 10:57:4930 void Create(int route_id,
meke6b83032014-12-19 23:35:3431 MessagePortDelegate* delegate,
[email protected]83fc55e42013-10-15 10:57:4932 int* message_port_id);
[email protected]65412272010-12-21 20:03:2433 void Destroy(int message_port_id);
34 void Entangle(int local_message_port_id, int remote_message_port_id);
35 void PostMessage(int sender_message_port_id,
[email protected]fcf75d42013-12-03 20:11:2636 const base::string16& message,
[email protected]65412272010-12-21 20:03:2437 const std::vector<int>& sent_message_port_ids);
38 void QueueMessages(int message_port_id);
39 void SendQueuedMessages(int message_port_id,
40 const QueuedMessages& queued_messages);
mekddb153282014-11-21 00:31:2041 void ReleaseMessages(int message_port_id);
[email protected]65412272010-12-21 20:03:2442
43 // Updates the information needed to reach a message port when it's sent to a
44 // (possibly different) process.
meke6b83032014-12-19 23:35:3445 void UpdateMessagePort(int message_port_id,
46 MessagePortDelegate* delegate,
47 int routing_id);
[email protected]65412272010-12-21 20:03:2448
mekddb153282014-11-21 00:31:2049 // The message port is being transferred to a new renderer process, but the
50 // code doing that isn't able to immediately update the message port with a
51 // new filter and routing_id. This queues up all messages sent to this port
52 // until later ReleaseMessages is called for this port (this will happen
53 // automatically as soon as a WebMessagePortChannelImpl instance is created
54 // for this port in the renderer. The browser side code is still responsible
55 // for updating the port with a new filter before that happens. If ultimately
56 // transfering the port to a new process fails, ClosePort should be called to
57 // clean up the port.
58 void HoldMessages(int message_port_id);
59
60 // Closes and cleans up the message port.
61 void ClosePort(int message_port_id);
62
meke6b83032014-12-19 23:35:3463 void OnMessagePortDelegateClosing(MessagePortDelegate* filter);
[email protected]65412272010-12-21 20:03:2464
65 // Attempts to send the queued messages for a message port.
66 void SendQueuedMessagesIfPossible(int message_port_id);
67
68 private:
69 friend struct DefaultSingletonTraits<MessagePortService>;
70
71 MessagePortService();
72 ~MessagePortService();
73
74 void PostMessageTo(int message_port_id,
[email protected]fcf75d42013-12-03 20:11:2675 const base::string16& message,
[email protected]65412272010-12-21 20:03:2476 const std::vector<int>& sent_message_port_ids);
77
78 // Handles the details of removing a message port id. Before calling this,
79 // verify that the message port id exists.
80 void Erase(int message_port_id);
81
82 struct MessagePort;
83 typedef std::map<int, MessagePort> MessagePorts;
84 MessagePorts message_ports_;
85
86 // We need globally unique identifiers for each message port.
87 int next_message_port_id_;
88
89 DISALLOW_COPY_AND_ASSIGN(MessagePortService);
90};
91
[email protected]e6047e4a2012-10-25 01:34:4992} // namespace content
93
[email protected]83fc55e42013-10-15 10:57:4994#endif // CONTENT_BROWSER_MESSAGE_PORT_SERVICE_H_