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> |
11 | 11 |
12 #include "base/linked_ptr.h" | 12 #include "base/linked_ptr.h" |
13 #include "base/lock.h" | 13 #include "base/lock.h" |
14 #include "base/ref_counted.h" | 14 #include "base/ref_counted.h" |
15 #include "chrome/common/notification_registrar.h" | 15 #include "chrome/common/notification_registrar.h" |
16 #include "ipc/ipc_message.h" | 16 #include "ipc/ipc_message.h" |
17 | 17 |
18 class MessageLoop; | 18 class MessageLoop; |
19 class Profile; | 19 class Profile; |
20 class RenderProcessHost; | 20 class RenderProcessHost; |
21 class ResourceMessageFilter; | 21 class ResourceMessageFilter; |
| 22 class TabContents; |
22 class URLRequestContext; | 23 class URLRequestContext; |
23 | 24 |
24 // This class manages message and event passing between renderer processes. | 25 // This class manages message and event passing between renderer processes. |
25 // It maintains a list of processes that are listening to events and a set of | 26 // It maintains a list of processes that are listening to events and a set of |
26 // open channels. | 27 // open channels. |
27 // | 28 // |
28 // Messaging works this way: | 29 // Messaging works this way: |
29 // - An extension-owned script context (like a toolstrip or a content script) | 30 // - An extension-owned script context (like a toolstrip or a content script) |
30 // adds an event listener to the "onConnect" event. | 31 // adds an event listener to the "onConnect" event. |
31 // - Another context calls "extension.connect()" to open a channel to the | 32 // - Another context calls "extension.connect()" to open a channel to the |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 // the other side. | 76 // the other side. |
76 void CloseChannel(int port_id); | 77 void CloseChannel(int port_id); |
77 | 78 |
78 // Sends a message from a renderer to the given port. | 79 // Sends a message from a renderer to the given port. |
79 void PostMessageFromRenderer(int port_id, const std::string& message); | 80 void PostMessageFromRenderer(int port_id, const std::string& message); |
80 | 81 |
81 // Send an event to every registered extension renderer. | 82 // Send an event to every registered extension renderer. |
82 void DispatchEventToRenderers( | 83 void DispatchEventToRenderers( |
83 const std::string& event_name, const std::string& event_args); | 84 const std::string& event_name, const std::string& event_args); |
84 | 85 |
85 // Given an extension's ID, opens a channel between the given automation | 86 // Given an extension ID, opens a channel between the given |
86 // "port" and that extension. Returns a channel ID to be used for posting | 87 // automation "port" or DevTools service and that extension. the |
87 // messages between the processes, or -1 if the extension doesn't exist. | 88 // channel will be open to the extension process hosting the |
88 int OpenAutomationChannelToExtension(int source_process_id, | 89 // background page and tool strip. |
89 int routing_id, | 90 // |
90 const std::string& extension_id, | 91 // Returns a port ID to be used for posting messages between the |
91 const std::string& channel_name, | 92 // processes, or -1 if the extension doesn't exist. |
92 IPC::Message::Sender* source); | 93 int OpenSpecialChannelToExtension( |
| 94 const std::string& extension_id, const std::string& channel_name, |
| 95 IPC::Message::Sender* source); |
| 96 |
| 97 // Given an extension ID, opens a channel between the given DevTools |
| 98 // service and the content script for that extension running in the |
| 99 // designated tab. |
| 100 // |
| 101 // Returns a port ID identifying the DevTools end of the channel, to |
| 102 // be used for posting messages. May return -1 on failure, although |
| 103 // the code doesn't detect whether the extension actually exists. |
| 104 int OpenSpecialChannelToTab( |
| 105 const std::string& extension_id, const std::string& channel_name, |
| 106 TabContents* target_tab_contents, IPC::Message::Sender* source); |
93 | 107 |
94 // --- IO thread only: | 108 // --- IO thread only: |
95 | 109 |
96 // Given an extension's ID, opens a channel between the given renderer "port" | 110 // Given an extension's ID, opens a channel between the given renderer "port" |
97 // and every listening context owned by that extension. Returns a port ID | 111 // and every listening context owned by that extension. Returns a port ID |
98 // to be used for posting messages between the processes. |channel_name| is | 112 // to be used for posting messages between the processes. |channel_name| is |
99 // an optional identifier for use by extension developers. | 113 // an optional identifier for use by extension developers. |
100 // This runs on the IO thread so that it can be used in a synchronous IPC | 114 // This runs on the IO thread so that it can be used in a synchronous IPC |
101 // message. | 115 // message. |
102 int OpenChannelToExtension(int routing_id, const std::string& extension_id, | 116 int OpenChannelToExtension(int routing_id, const std::string& extension_id, |
(...skipping 27 matching lines...) Expand all Loading... |
130 // opened. | 144 // opened. |
131 void OpenChannelToExtensionOnUIThread( | 145 void OpenChannelToExtensionOnUIThread( |
132 int source_process_id, int source_routing_id, int receiver_port_id, | 146 int source_process_id, int source_routing_id, int receiver_port_id, |
133 const std::string& extension_id, const std::string& channel_name); | 147 const std::string& extension_id, const std::string& channel_name); |
134 | 148 |
135 void OpenChannelToTabOnUIThread( | 149 void OpenChannelToTabOnUIThread( |
136 int source_process_id, int source_routing_id, int receiver_port_id, | 150 int source_process_id, int source_routing_id, int receiver_port_id, |
137 int tab_id, const std::string& extension_id, | 151 int tab_id, const std::string& extension_id, |
138 const std::string& channel_name); | 152 const std::string& channel_name); |
139 | 153 |
140 // Common between OpenChannelOnUIThread and OpenAutomationChannelToExtension. | 154 // Common between OpenChannelOnUIThread and OpenSpecialChannelToExtension. |
141 bool OpenChannelOnUIThreadImpl( | 155 bool OpenChannelOnUIThreadImpl( |
142 IPC::Message::Sender* source, int source_process_id, int source_routing_id, | 156 IPC::Message::Sender* source, TabContents* source_contents, |
143 const MessagePort& receiver, int receiver_port_id, | 157 const MessagePort& receiver, int receiver_port_id, |
144 const std::string& extension_id, const std::string& channel_name); | 158 const std::string& extension_id, const std::string& channel_name); |
145 | 159 |
146 // NotificationObserver interface. | 160 // NotificationObserver interface. |
147 void Observe(NotificationType type, | 161 void Observe(NotificationType type, |
148 const NotificationSource& source, | 162 const NotificationSource& source, |
149 const NotificationDetails& details); | 163 const NotificationDetails& details); |
150 | 164 |
151 // An IPC sender that might be in our list of channels has closed. | 165 // An IPC sender that might be in our list of channels has closed. |
152 void OnSenderClosed(IPC::Message::Sender* sender); | 166 void OnSenderClosed(IPC::Message::Sender* sender); |
153 | 167 |
154 Profile* profile_; | 168 Profile* profile_; |
(...skipping 13 matching lines...) Expand all Loading... |
168 int next_port_id_; | 182 int next_port_id_; |
169 | 183 |
170 // Protects the next_port_id_ variable, since it can be | 184 // Protects the next_port_id_ variable, since it can be |
171 // used on the IO thread or the UI thread. | 185 // used on the IO thread or the UI thread. |
172 Lock next_port_id_lock_; | 186 Lock next_port_id_lock_; |
173 | 187 |
174 DISALLOW_COPY_AND_ASSIGN(ExtensionMessageService); | 188 DISALLOW_COPY_AND_ASSIGN(ExtensionMessageService); |
175 }; | 189 }; |
176 | 190 |
177 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_SERVICE_H_ | 191 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_SERVICE_H_ |
OLD | NEW |