| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_SERVICE_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_SERVICE_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_SERVICE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 RenderProcessHost* GetProcessForExtension(const std::string& extension_id); | 47 RenderProcessHost* GetProcessForExtension(const std::string& extension_id); |
| 48 | 48 |
| 49 // Register an extension and its corresponding renderer process. | 49 // Register an extension and its corresponding renderer process. |
| 50 void RegisterExtension(const std::string& extension_id, | 50 void RegisterExtension(const std::string& extension_id, |
| 51 int render_process_id); | 51 int render_process_id); |
| 52 | 52 |
| 53 // Add or remove |render_process_pid| as a listener for |event_name|. | 53 // Add or remove |render_process_pid| as a listener for |event_name|. |
| 54 void AddEventListener(std::string event_name, int render_process_id); | 54 void AddEventListener(std::string event_name, int render_process_id); |
| 55 void RemoveEventListener(std::string event_name, int render_process_id); | 55 void RemoveEventListener(std::string event_name, int render_process_id); |
| 56 | 56 |
| 57 // Closes an extension channel for test automation. | 57 // Closes the message channel associated with the given port, and notifies |
| 58 void CloseAutomationChannel(int port_id); | 58 // the other side. |
| 59 void CloseChannel(int port_id); |
| 59 | 60 |
| 60 // Sends a message from a renderer to the given port. | 61 // Sends a message from a renderer to the given port. |
| 61 // TODO(mpcomplete): include the source tab. | |
| 62 void PostMessageFromRenderer(int port_id, const std::string& message); | 62 void PostMessageFromRenderer(int port_id, const std::string& message); |
| 63 | 63 |
| 64 // Send an event to every registered extension renderer. | 64 // Send an event to every registered extension renderer. |
| 65 void DispatchEventToRenderers( | 65 void DispatchEventToRenderers( |
| 66 const std::string& event_name, const std::string& event_args); | 66 const std::string& event_name, const std::string& event_args); |
| 67 | 67 |
| 68 // NotificationObserver interface. | |
| 69 void Observe(NotificationType type, | |
| 70 const NotificationSource& source, | |
| 71 const NotificationDetails& details); | |
| 72 | |
| 73 // Given an extension's ID, opens a channel between the given automation | 68 // Given an extension's ID, opens a channel between the given automation |
| 74 // "port" and that extension. Returns a channel ID to be used for posting | 69 // "port" and that extension. Returns a channel ID to be used for posting |
| 75 // messages between the processes, or -1 if the extension doesn't exist. | 70 // messages between the processes, or -1 if the extension doesn't exist. |
| 76 int OpenAutomationChannelToExtension(int source_process_id, | 71 int OpenAutomationChannelToExtension(int source_process_id, |
| 77 int routing_id, | 72 int routing_id, |
| 78 const std::string& extension_id, | 73 const std::string& extension_id, |
| 79 IPC::Message::Sender* source); | 74 IPC::Message::Sender* source); |
| 80 | 75 |
| 76 // NotificationObserver interface. |
| 77 void Observe(NotificationType type, |
| 78 const NotificationSource& source, |
| 79 const NotificationDetails& details); |
| 80 |
| 81 // --- IO thread only: | 81 // --- IO thread only: |
| 82 | 82 |
| 83 // Given an extension's ID, opens a channel between the given renderer "port" | 83 // Given an extension's ID, opens a channel between the given renderer "port" |
| 84 // and that extension. Returns a channel ID to be used for posting messages | 84 // and that extension. Returns a channel ID to be used for posting messages |
| 85 // between the processes, or -1 if the extension doesn't exist. | 85 // between the processes, or -1 if the extension doesn't exist. |
| 86 // This runs on the IO thread so that it can be used in a synchronous IPC | 86 // This runs on the IO thread so that it can be used in a synchronous IPC |
| 87 // message. | 87 // message. |
| 88 int OpenChannelToExtension(int routing_id, const std::string& extension_id, | 88 int OpenChannelToExtension(int routing_id, const std::string& extension_id, |
| 89 ResourceMessageFilter* source); | 89 ResourceMessageFilter* source); |
| 90 | 90 |
| 91 private: | 91 private: |
| 92 // The connection between two ports. It is possible that both ports |
| 93 // refer to the same renderer. |
| 94 struct MessageChannel { |
| 95 IPC::Message::Sender* port1; |
| 96 IPC::Message::Sender* port2; |
| 97 }; |
| 98 |
| 99 // A map of channel ID to its channel object. |
| 100 typedef std::map<int, MessageChannel> MessageChannelMap; |
| 101 |
| 92 // Allocates a pair of port ids. | 102 // Allocates a pair of port ids. |
| 93 // NOTE: this can be called from any thread. | 103 // NOTE: this can be called from any thread. |
| 94 void AllocatePortIdPair(int* port1, int* port2); | 104 void AllocatePortIdPair(int* port1, int* port2); |
| 95 | 105 |
| 96 // Gets the process ID for the specified extension. | 106 // Gets the process ID for the specified extension. |
| 97 // NOTE: this can be called from any thread. | 107 // NOTE: this can be called from any thread. |
| 98 int GetProcessIdForExtension(const std::string& extension_id); | 108 int GetProcessIdForExtension(const std::string& extension_id); |
| 99 | 109 |
| 110 void CloseChannelImpl(MessageChannelMap::iterator channel_iter, int port_id); |
| 111 |
| 100 int OpenChannelToExtensionImpl(const std::string& extension_id, | 112 int OpenChannelToExtensionImpl(const std::string& extension_id, |
| 101 IPC::Message::Sender* source); | 113 IPC::Message::Sender* source); |
| 102 | 114 |
| 103 NotificationRegistrar registrar_; | 115 NotificationRegistrar registrar_; |
| 104 | 116 |
| 105 // The UI message loop, used for posting tasks. | 117 // The UI message loop, used for posting tasks. |
| 106 MessageLoop* ui_loop_; | 118 MessageLoop* ui_loop_; |
| 107 | 119 |
| 108 // A map of extension ID to the render_process_id that the extension lives in. | 120 // A map of extension ID to the render_process_id that the extension lives in. |
| 109 typedef std::map<std::string, int> ProcessIDMap; | 121 typedef std::map<std::string, int> ProcessIDMap; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 129 void OpenChannelOnUIThread(int source_routing_id, | 141 void OpenChannelOnUIThread(int source_routing_id, |
| 130 int source_port_id, int source_process_id, | 142 int source_port_id, int source_process_id, |
| 131 int dest_port_id, int dest_process_id); | 143 int dest_port_id, int dest_process_id); |
| 132 | 144 |
| 133 // Common between OpenChannelOnUIThread and | 145 // Common between OpenChannelOnUIThread and |
| 134 // OpenAutomationChannelToExtension. | 146 // OpenAutomationChannelToExtension. |
| 135 void OpenChannelOnUIThreadImpl( | 147 void OpenChannelOnUIThreadImpl( |
| 136 int source_routing_id, int source_port_id, IPC::Message::Sender* source, | 148 int source_routing_id, int source_port_id, IPC::Message::Sender* source, |
| 137 int dest_port_id, int dest_process_id, int source_process_id); | 149 int dest_port_id, int dest_process_id, int source_process_id); |
| 138 | 150 |
| 139 // The connection between two ports. It is possible that both ports | |
| 140 // refer to the same renderer. | |
| 141 struct MessageChannel { | |
| 142 IPC::Message::Sender* port1; | |
| 143 IPC::Message::Sender* port2; | |
| 144 }; | |
| 145 | |
| 146 // A map of channel ID to its channel object. | |
| 147 typedef std::map<int, MessageChannel> MessageChannelMap; | |
| 148 MessageChannelMap channels_; | 151 MessageChannelMap channels_; |
| 149 | 152 |
| 150 // True if Init has been called. | 153 // True if Init has been called. |
| 151 bool initialized_; | 154 bool initialized_; |
| 152 | 155 |
| 153 // For generating unique channel IDs. | 156 // For generating unique channel IDs. |
| 154 int next_port_id_; | 157 int next_port_id_; |
| 155 | 158 |
| 156 // Protects the next_port_id_ variable, since it can be | 159 // Protects the next_port_id_ variable, since it can be |
| 157 // used on the IO thread or the UI thread. | 160 // used on the IO thread or the UI thread. |
| 158 Lock next_port_id_lock_; | 161 Lock next_port_id_lock_; |
| 159 | 162 |
| 160 DISALLOW_COPY_AND_ASSIGN(ExtensionMessageService); | 163 DISALLOW_COPY_AND_ASSIGN(ExtensionMessageService); |
| 161 }; | 164 }; |
| 162 | 165 |
| 163 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_SERVICE_H_ | 166 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_SERVICE_H_ |
| OLD | NEW |