| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_API_MESSAGING_MESSAGE_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_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/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" | 15 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" |
| 16 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h" |
| 16 #include "content/public/browser/notification_observer.h" | 17 #include "content/public/browser/notification_observer.h" |
| 17 #include "content/public/browser/notification_registrar.h" | 18 #include "content/public/browser/notification_registrar.h" |
| 18 | 19 |
| 19 class GURL; | 20 class GURL; |
| 20 class Profile; | 21 class Profile; |
| 21 | 22 |
| 22 namespace base { | 23 namespace base { |
| 23 class DictionaryValue; | 24 class DictionaryValue; |
| 24 } | 25 } |
| 25 | 26 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 47 // context owned by the connecting extension in that process/tab. | 48 // context owned by the connecting extension in that process/tab. |
| 48 // - Once the channel is established, either side can call postMessage to send | 49 // - Once the channel is established, either side can call postMessage to send |
| 49 // a message to the opposite side of the channel, which may have multiple | 50 // a message to the opposite side of the channel, which may have multiple |
| 50 // listeners. | 51 // listeners. |
| 51 // | 52 // |
| 52 // Terminology: | 53 // Terminology: |
| 53 // channel: connection between two ports | 54 // channel: connection between two ports |
| 54 // port: an IPC::Message::Process interface and an optional routing_id (in the | 55 // port: an IPC::Message::Process interface and an optional routing_id (in the |
| 55 // case that the port is a tab). The Process is usually either a | 56 // case that the port is a tab). The Process is usually either a |
| 56 // RenderProcessHost or a RenderViewHost. | 57 // RenderProcessHost or a RenderViewHost. |
| 57 class MessageService : public content::NotificationObserver, | 58 class MessageService : public ProfileKeyedAPI, |
| 59 public content::NotificationObserver, |
| 58 public NativeMessageProcessHost::Client { | 60 public NativeMessageProcessHost::Client { |
| 59 public: | 61 public: |
| 60 // A messaging channel. Note that the opening port can be the same as the | 62 // A messaging channel. Note that the opening port can be the same as the |
| 61 // receiver, if an extension background page wants to talk to its tab (for | 63 // receiver, if an extension background page wants to talk to its tab (for |
| 62 // example). | 64 // example). |
| 63 struct MessageChannel; | 65 struct MessageChannel; |
| 64 | 66 |
| 65 // One side of the communication handled by extensions::MessageService. | 67 // One side of the communication handled by extensions::MessageService. |
| 66 class MessagePort { | 68 class MessagePort { |
| 67 public: | 69 public: |
| (...skipping 27 matching lines...) Expand all Loading... |
| 95 MessagePort() {} | 97 MessagePort() {} |
| 96 | 98 |
| 97 private: | 99 private: |
| 98 DISALLOW_COPY_AND_ASSIGN(MessagePort); | 100 DISALLOW_COPY_AND_ASSIGN(MessagePort); |
| 99 }; | 101 }; |
| 100 | 102 |
| 101 // Allocates a pair of port ids. | 103 // Allocates a pair of port ids. |
| 102 // NOTE: this can be called from any thread. | 104 // NOTE: this can be called from any thread. |
| 103 static void AllocatePortIdPair(int* port1, int* port2); | 105 static void AllocatePortIdPair(int* port1, int* port2); |
| 104 | 106 |
| 105 explicit MessageService(LazyBackgroundTaskQueue* queue); | 107 explicit MessageService(Profile* profile); |
| 106 virtual ~MessageService(); | 108 virtual ~MessageService(); |
| 107 | 109 |
| 110 // ProfileKeyedAPI implementation. |
| 111 static ProfileKeyedAPIFactory<MessageService>* GetFactoryInstance(); |
| 112 |
| 108 // Given an extension's ID, opens a channel between the given renderer "port" | 113 // Given an extension's ID, opens a channel between the given renderer "port" |
| 109 // and every listening context owned by that extension. |channel_name| is | 114 // and every listening context owned by that extension. |channel_name| is |
| 110 // an optional identifier for use by extension developers. | 115 // an optional identifier for use by extension developers. |
| 111 void OpenChannelToExtension( | 116 void OpenChannelToExtension( |
| 112 int source_process_id, int source_routing_id, int receiver_port_id, | 117 int source_process_id, int source_routing_id, int receiver_port_id, |
| 113 const std::string& source_extension_id, | 118 const std::string& source_extension_id, |
| 114 const std::string& target_extension_id, | 119 const std::string& target_extension_id, |
| 115 const GURL& source_url, | 120 const GURL& source_url, |
| 116 const std::string& channel_name); | 121 const std::string& channel_name); |
| 117 | 122 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 138 // Sends a message to the given port. | 143 // Sends a message to the given port. |
| 139 void PostMessage(int port_id, const std::string& message); | 144 void PostMessage(int port_id, const std::string& message); |
| 140 | 145 |
| 141 // NativeMessageProcessHost::Client | 146 // NativeMessageProcessHost::Client |
| 142 virtual void PostMessageFromNativeProcess( | 147 virtual void PostMessageFromNativeProcess( |
| 143 int port_id, | 148 int port_id, |
| 144 const std::string& message) OVERRIDE; | 149 const std::string& message) OVERRIDE; |
| 145 | 150 |
| 146 private: | 151 private: |
| 147 friend class MockMessageService; | 152 friend class MockMessageService; |
| 153 friend class ProfileKeyedAPIFactory<MessageService>; |
| 148 struct OpenChannelParams; | 154 struct OpenChannelParams; |
| 149 | 155 |
| 150 // A map of channel ID to its channel object. | 156 // A map of channel ID to its channel object. |
| 151 typedef std::map<int, MessageChannel*> MessageChannelMap; | 157 typedef std::map<int, MessageChannel*> MessageChannelMap; |
| 152 | 158 |
| 153 // A map of channel ID to information about the extension that is waiting | 159 // A map of channel ID to information about the extension that is waiting |
| 154 // for that channel to open. Used for lazy background pages. | 160 // for that channel to open. Used for lazy background pages. |
| 155 typedef std::string ExtensionID; | 161 typedef std::string ExtensionID; |
| 156 typedef std::pair<Profile*, ExtensionID> PendingChannel; | 162 typedef std::pair<Profile*, ExtensionID> PendingChannel; |
| 157 typedef std::map<int, PendingChannel> PendingChannelMap; | 163 typedef std::map<int, PendingChannel> PendingChannelMap; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 if (host) | 201 if (host) |
| 196 CloseChannel(port_id, error_message); | 202 CloseChannel(port_id, error_message); |
| 197 } | 203 } |
| 198 void PendingPostMessage(int port_id, | 204 void PendingPostMessage(int port_id, |
| 199 const std::string& message, | 205 const std::string& message, |
| 200 extensions::ExtensionHost* host) { | 206 extensions::ExtensionHost* host) { |
| 201 if (host) | 207 if (host) |
| 202 PostMessage(port_id, message); | 208 PostMessage(port_id, message); |
| 203 } | 209 } |
| 204 | 210 |
| 211 Profile* profile_; |
| 212 |
| 213 // ProfileKeyedAPI implementation. |
| 214 static const char* service_name() { |
| 215 return "MessageService"; |
| 216 } |
| 217 |
| 205 content::NotificationRegistrar registrar_; | 218 content::NotificationRegistrar registrar_; |
| 206 MessageChannelMap channels_; | 219 MessageChannelMap channels_; |
| 207 PendingChannelMap pending_channels_; | 220 PendingChannelMap pending_channels_; |
| 208 | 221 |
| 209 // Weak pointer. Guaranteed to outlive this class. | 222 // Weak pointer. Guaranteed to outlive this class. |
| 210 LazyBackgroundTaskQueue* lazy_background_task_queue_; | 223 LazyBackgroundTaskQueue* lazy_background_task_queue_; |
| 211 | 224 |
| 212 base::WeakPtrFactory<MessageService> weak_factory_; | 225 base::WeakPtrFactory<MessageService> weak_factory_; |
| 213 | 226 |
| 214 DISALLOW_COPY_AND_ASSIGN(MessageService); | 227 DISALLOW_COPY_AND_ASSIGN(MessageService); |
| 215 }; | 228 }; |
| 216 | 229 |
| 217 } // namespace extensions | 230 } // namespace extensions |
| 218 | 231 |
| 219 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ | 232 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ |
| OLD | NEW |