Chromium Code Reviews| 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 | |
| 113 // Convenience method to get the MessageService for a profile. | |
| 114 static MessageService* Get(Profile* profile); | |
| 115 | |
| 108 // Given an extension's ID, opens a channel between the given renderer "port" | 116 // 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 | 117 // and every listening context owned by that extension. |channel_name| is |
| 110 // an optional identifier for use by extension developers. | 118 // an optional identifier for use by extension developers. |
| 111 void OpenChannelToExtension( | 119 void OpenChannelToExtension( |
| 112 int source_process_id, int source_routing_id, int receiver_port_id, | 120 int source_process_id, int source_routing_id, int receiver_port_id, |
| 113 const std::string& source_extension_id, | 121 const std::string& source_extension_id, |
| 114 const std::string& target_extension_id, | 122 const std::string& target_extension_id, |
| 115 const GURL& source_url, | 123 const GURL& source_url, |
| 116 const std::string& channel_name); | 124 const std::string& channel_name); |
| 117 | 125 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 138 // Sends a message to the given port. | 146 // Sends a message to the given port. |
| 139 void PostMessage(int port_id, const std::string& message); | 147 void PostMessage(int port_id, const std::string& message); |
| 140 | 148 |
| 141 // NativeMessageProcessHost::Client | 149 // NativeMessageProcessHost::Client |
| 142 virtual void PostMessageFromNativeProcess( | 150 virtual void PostMessageFromNativeProcess( |
| 143 int port_id, | 151 int port_id, |
| 144 const std::string& message) OVERRIDE; | 152 const std::string& message) OVERRIDE; |
| 145 | 153 |
| 146 private: | 154 private: |
| 147 friend class MockMessageService; | 155 friend class MockMessageService; |
| 156 friend class ProfileKeyedAPIFactory<MessageService>; | |
| 148 struct OpenChannelParams; | 157 struct OpenChannelParams; |
| 149 | 158 |
| 150 // A map of channel ID to its channel object. | 159 // A map of channel ID to its channel object. |
| 151 typedef std::map<int, MessageChannel*> MessageChannelMap; | 160 typedef std::map<int, MessageChannel*> MessageChannelMap; |
| 152 | 161 |
| 153 // A map of channel ID to information about the extension that is waiting | 162 // A map of channel ID to information about the extension that is waiting |
| 154 // for that channel to open. Used for lazy background pages. | 163 // for that channel to open. Used for lazy background pages. |
| 155 typedef std::string ExtensionID; | 164 typedef std::string ExtensionID; |
| 156 typedef std::pair<Profile*, ExtensionID> PendingChannel; | 165 typedef std::pair<Profile*, ExtensionID> PendingChannel; |
| 157 typedef std::map<int, PendingChannel> PendingChannelMap; | 166 typedef std::map<int, PendingChannel> PendingChannelMap; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 195 if (host) | 204 if (host) |
| 196 CloseChannel(port_id, error_message); | 205 CloseChannel(port_id, error_message); |
| 197 } | 206 } |
| 198 void PendingPostMessage(int port_id, | 207 void PendingPostMessage(int port_id, |
| 199 const std::string& message, | 208 const std::string& message, |
| 200 extensions::ExtensionHost* host) { | 209 extensions::ExtensionHost* host) { |
| 201 if (host) | 210 if (host) |
| 202 PostMessage(port_id, message); | 211 PostMessage(port_id, message); |
| 203 } | 212 } |
| 204 | 213 |
| 214 Profile* profile_; | |
| 215 | |
| 216 // ProfileKeyedAPI implementation. | |
|
Yoyo Zhou
2013/04/30 22:33:04
Should also be redirected in incognito.
Patrick Riordan
2013/05/01 01:54:44
Done.
| |
| 217 static const char* service_name() { | |
| 218 return "MessageService"; | |
| 219 } | |
| 220 | |
| 205 content::NotificationRegistrar registrar_; | 221 content::NotificationRegistrar registrar_; |
| 206 MessageChannelMap channels_; | 222 MessageChannelMap channels_; |
| 207 PendingChannelMap pending_channels_; | 223 PendingChannelMap pending_channels_; |
| 208 | 224 |
| 209 // Weak pointer. Guaranteed to outlive this class. | 225 // Weak pointer. Guaranteed to outlive this class. |
| 210 LazyBackgroundTaskQueue* lazy_background_task_queue_; | 226 LazyBackgroundTaskQueue* lazy_background_task_queue_; |
| 211 | 227 |
| 212 base::WeakPtrFactory<MessageService> weak_factory_; | 228 base::WeakPtrFactory<MessageService> weak_factory_; |
| 213 | 229 |
| 214 DISALLOW_COPY_AND_ASSIGN(MessageService); | 230 DISALLOW_COPY_AND_ASSIGN(MessageService); |
| 215 }; | 231 }; |
| 216 | 232 |
| 217 } // namespace extensions | 233 } // namespace extensions |
| 218 | 234 |
| 219 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ | 235 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ |
| OLD | NEW |