OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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. | |
4 | |
5 #ifndef CHROME_BROWSER_WORKER_HOST_MESSAGE_PORT_DISPATCHER_H_ | |
6 #define CHROME_BROWSER_WORKER_HOST_MESSAGE_PORT_DISPATCHER_H_ | |
7 #pragma once | |
8 | |
9 #include <map> | |
10 #include <utility> | |
11 #include <vector> | |
12 | |
13 #include "base/basictypes.h" | |
14 #include "base/callback.h" | |
15 #include "base/singleton.h" | |
16 #include "base/string16.h" | |
17 #include "base/task.h" | |
18 #include "chrome/common/notification_observer.h" | |
19 #include "chrome/common/notification_registrar.h" | |
20 #include "ipc/ipc_message.h" | |
21 | |
22 class MessagePortDispatcher : public NotificationObserver { | |
23 public: | |
24 typedef std::vector<std::pair<string16, std::vector<int> > > QueuedMessages; | |
25 | |
26 // Returns the MessagePortDispatcher singleton. | |
27 static MessagePortDispatcher* GetInstance(); | |
28 | |
29 bool OnMessageReceived(const IPC::Message& message, | |
30 IPC::Message::Sender* sender, | |
31 CallbackWithReturnValue<int>::Type* next_routing_id, | |
32 bool* message_was_ok); | |
33 | |
34 // Updates the information needed to reach a message port when it's sent to a | |
35 // (possibly different) process. | |
36 void UpdateMessagePort( | |
37 int message_port_id, | |
38 IPC::Message::Sender* sender, | |
39 int routing_id, | |
40 CallbackWithReturnValue<int>::Type* next_routing_id); | |
41 | |
42 // Attempts to send the queued messages for a message port. | |
43 void SendQueuedMessagesIfPossible(int message_port_id); | |
44 | |
45 bool Send(IPC::Message* message); | |
46 | |
47 private: | |
48 friend struct DefaultSingletonTraits<MessagePortDispatcher>; | |
49 | |
50 MessagePortDispatcher(); | |
51 ~MessagePortDispatcher(); | |
52 | |
53 // Message handlers. | |
54 void OnCreate(int* route_id, int* message_port_id); | |
55 void OnDestroy(int message_port_id); | |
56 void OnEntangle(int local_message_port_id, int remote_message_port_id); | |
57 void OnPostMessage(int sender_message_port_id, | |
58 const string16& message, | |
59 const std::vector<int>& sent_message_port_ids); | |
60 void OnQueueMessages(int message_port_id); | |
61 void OnSendQueuedMessages(int message_port_id, | |
62 const QueuedMessages& queued_messages); | |
63 | |
64 void PostMessageTo(int message_port_id, | |
65 const string16& message, | |
66 const std::vector<int>& sent_message_port_ids); | |
67 | |
68 // NotificationObserver interface. | |
69 virtual void Observe(NotificationType type, | |
70 const NotificationSource& source, | |
71 const NotificationDetails& details); | |
72 | |
73 // Handles the details of removing a message port id. Before calling this, | |
74 // verify that the message port id exists. | |
75 void Erase(int message_port_id); | |
76 | |
77 struct MessagePort; | |
78 typedef std::map<int, MessagePort> MessagePorts; | |
79 MessagePorts message_ports_; | |
80 | |
81 // We need globally unique identifiers for each message port. | |
82 int next_message_port_id_; | |
83 | |
84 // Valid only during IPC message dispatching. | |
85 IPC::Message::Sender* sender_; | |
86 CallbackWithReturnValue<int>::Type* next_routing_id_; | |
87 | |
88 NotificationRegistrar registrar_; | |
89 | |
90 DISALLOW_COPY_AND_ASSIGN(MessagePortDispatcher); | |
91 }; | |
92 | |
93 #endif // CHROME_BROWSER_WORKER_HOST_MESSAGE_PORT_DISPATCHER_H_ | |
OLD | NEW |