1 // Copyright (c) 2012 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 CONTENT_BROWSER_WORKER_HOST_MESSAGE_PORT_SERVICE_H_ 6 #define CONTENT_BROWSER_WORKER_HOST_MESSAGE_PORT_SERVICE_H_ 7 8 #include <map> 9 #include <utility> 10 #include <vector> 11 12 #include "base/basictypes.h" 13 #include "base/memory/singleton.h" 14 #include "base/strings/string16.h" 15 #include "ipc/ipc_message.h" 16 17 namespace content { 18 class WorkerMessageFilter; 19 20 class MessagePortService { 21 public: 22 typedef std::vector<std::pair<string16, std::vector<int> > > QueuedMessages; 23 24 // Returns the MessagePortService singleton. 25 static MessagePortService* GetInstance(); 26 27 // These methods correspond to the message port related IPCs. 28 void Create(int route_id, WorkerMessageFilter* filter, int* message_port_id); 29 void Destroy(int message_port_id); 30 void Entangle(int local_message_port_id, int remote_message_port_id); 31 void PostMessage(int sender_message_port_id, 32 const string16& message, 33 const std::vector<int>& sent_message_port_ids); 34 void QueueMessages(int message_port_id); 35 void SendQueuedMessages(int message_port_id, 36 const QueuedMessages& queued_messages); 37 38 // Updates the information needed to reach a message port when it's sent to a 39 // (possibly different) process. 40 void UpdateMessagePort( 41 int message_port_id, 42 WorkerMessageFilter* filter, 43 int routing_id); 44 45 void OnWorkerMessageFilterClosing(WorkerMessageFilter* filter); 46 47 // Attempts to send the queued messages for a message port. 48 void SendQueuedMessagesIfPossible(int message_port_id); 49 50 private: 51 friend struct DefaultSingletonTraits<MessagePortService>; 52 53 MessagePortService(); 54 ~MessagePortService(); 55 56 void PostMessageTo(int message_port_id, 57 const string16& message, 58 const std::vector<int>& sent_message_port_ids); 59 60 // Handles the details of removing a message port id. Before calling this, 61 // verify that the message port id exists. 62 void Erase(int message_port_id); 63 64 struct MessagePort; 65 typedef std::map<int, MessagePort> MessagePorts; 66 MessagePorts message_ports_; 67 68 // We need globally unique identifiers for each message port. 69 int next_message_port_id_; 70 71 DISALLOW_COPY_AND_ASSIGN(MessagePortService); 72 }; 73 74 } // namespace content 75 76 #endif // CONTENT_BROWSER_WORKER_HOST_MESSAGE_PORT_SERVICE_H_ 77