| 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/lock.h" | 12 #include "base/lock.h" |
| 13 #include "base/ref_counted.h" | 13 #include "base/ref_counted.h" |
| 14 #include "chrome/common/notification_registrar.h" | 14 #include "chrome/common/notification_registrar.h" |
| 15 #include "chrome/browser/chrome_thread.h" |
| 15 #include "chrome/browser/extensions/extension_devtools_manager.h" | 16 #include "chrome/browser/extensions/extension_devtools_manager.h" |
| 16 #include "ipc/ipc_message.h" | 17 #include "ipc/ipc_message.h" |
| 17 | 18 |
| 18 class MessageLoop; | 19 class MessageLoop; |
| 19 class Profile; | 20 class Profile; |
| 20 class RenderProcessHost; | 21 class RenderProcessHost; |
| 21 class ResourceMessageFilter; | 22 class ResourceMessageFilter; |
| 22 class TabContents; | 23 class TabContents; |
| 23 class URLRequestContext; | 24 class URLRequestContext; |
| 24 | 25 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 37 // - Once the channel is established, either side can call postMessage to send | 38 // - Once the channel is established, either side can call postMessage to send |
| 38 // a message to the opposite side of the channel, which may have multiple | 39 // a message to the opposite side of the channel, which may have multiple |
| 39 // listeners. | 40 // listeners. |
| 40 // | 41 // |
| 41 // Terminology: | 42 // Terminology: |
| 42 // channel: connection between two ports | 43 // channel: connection between two ports |
| 43 // port: an IPC::Message::Sender interface and an optional routing_id (in the | 44 // port: an IPC::Message::Sender interface and an optional routing_id (in the |
| 44 // case that the port is a tab). The Sender is usually either a | 45 // case that the port is a tab). The Sender is usually either a |
| 45 // RenderProcessHost or a RenderViewHost. | 46 // RenderProcessHost or a RenderViewHost. |
| 46 class ExtensionMessageService | 47 class ExtensionMessageService |
| 47 : public base::RefCountedThreadSafe<ExtensionMessageService>, | 48 : public base::RefCountedThreadSafe< |
| 49 ExtensionMessageService, ChromeThread::DeleteOnUIThread>, |
| 48 public NotificationObserver { | 50 public NotificationObserver { |
| 49 public: | 51 public: |
| 50 // Javascript function name constants. | 52 // Javascript function name constants. |
| 51 static const char kDispatchOnConnect[]; | 53 static const char kDispatchOnConnect[]; |
| 52 static const char kDispatchOnDisconnect[]; | 54 static const char kDispatchOnDisconnect[]; |
| 53 static const char kDispatchOnMessage[]; | 55 static const char kDispatchOnMessage[]; |
| 54 static const char kDispatchEvent[]; | 56 static const char kDispatchEvent[]; |
| 55 static const char kDispatchError[]; | 57 static const char kDispatchError[]; |
| 56 | 58 |
| 57 // A messaging channel. Note that the opening port can be the same as the | 59 // A messaging channel. Note that the opening port can be the same as the |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 | 122 |
| 121 // Same as above, but opens a channel to the tab with the given ID. Messages | 123 // Same as above, but opens a channel to the tab with the given ID. Messages |
| 122 // are restricted to that tab, so if there are multiple tabs in that process, | 124 // are restricted to that tab, so if there are multiple tabs in that process, |
| 123 // only the targeted tab will receive messages. | 125 // only the targeted tab will receive messages. |
| 124 int OpenChannelToTab(int routing_id, int tab_id, | 126 int OpenChannelToTab(int routing_id, int tab_id, |
| 125 const std::string& extension_id, | 127 const std::string& extension_id, |
| 126 const std::string& channel_name, | 128 const std::string& channel_name, |
| 127 ResourceMessageFilter* source); | 129 ResourceMessageFilter* source); |
| 128 | 130 |
| 129 private: | 131 private: |
| 130 friend class base::RefCountedThreadSafe<ExtensionMessageService>; | 132 friend class ChromeThread; |
| 133 friend class DeleteTask<ExtensionMessageService>; |
| 131 | 134 |
| 132 // A map of channel ID to its channel object. | 135 // A map of channel ID to its channel object. |
| 133 typedef std::map<int, MessageChannel*> MessageChannelMap; | 136 typedef std::map<int, MessageChannel*> MessageChannelMap; |
| 134 | 137 |
| 135 ~ExtensionMessageService(); | 138 ~ExtensionMessageService(); |
| 136 | 139 |
| 137 // Allocates a pair of port ids. | 140 // Allocates a pair of port ids. |
| 138 // NOTE: this can be called from any thread. | 141 // NOTE: this can be called from any thread. |
| 139 void AllocatePortIdPair(int* port1, int* port2); | 142 void AllocatePortIdPair(int* port1, int* port2); |
| 140 | 143 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 | 191 |
| 189 // --- UI or IO thread: | 192 // --- UI or IO thread: |
| 190 | 193 |
| 191 // For generating unique channel IDs. | 194 // For generating unique channel IDs. |
| 192 int next_port_id_; | 195 int next_port_id_; |
| 193 | 196 |
| 194 // Protects the next_port_id_ variable, since it can be | 197 // Protects the next_port_id_ variable, since it can be |
| 195 // used on the IO thread or the UI thread. | 198 // used on the IO thread or the UI thread. |
| 196 Lock next_port_id_lock_; | 199 Lock next_port_id_lock_; |
| 197 | 200 |
| 201 // The thread creating this object. Should be UI thread. |
| 202 ChromeThread::ID thread_id_; |
| 203 |
| 198 DISALLOW_COPY_AND_ASSIGN(ExtensionMessageService); | 204 DISALLOW_COPY_AND_ASSIGN(ExtensionMessageService); |
| 199 }; | 205 }; |
| 200 | 206 |
| 201 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_SERVICE_H_ | 207 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_SERVICE_H_ |
| OLD | NEW |