| 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 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "chrome/browser/extensions/api/messaging/message_property_provider.h" | 16 #include "chrome/browser/extensions/api/messaging/message_property_provider.h" |
| 17 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" | 17 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" |
| 18 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h" | |
| 19 #include "chrome/common/extensions/api/messaging/message.h" | 18 #include "chrome/common/extensions/api/messaging/message.h" |
| 20 #include "content/public/browser/notification_observer.h" | 19 #include "content/public/browser/notification_observer.h" |
| 21 #include "content/public/browser/notification_registrar.h" | 20 #include "content/public/browser/notification_registrar.h" |
| 21 #include "extensions/browser/browser_context_keyed_api_factory.h" |
| 22 | 22 |
| 23 class GURL; | 23 class GURL; |
| 24 class Profile; | 24 class Profile; |
| 25 | 25 |
| 26 namespace base { | 26 namespace base { |
| 27 class DictionaryValue; | 27 class DictionaryValue; |
| 28 } | 28 } |
| 29 | 29 |
| 30 namespace content { | 30 namespace content { |
| 31 class BrowserContext; | 31 class BrowserContext; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 52 // context owned by the connecting extension in that process/tab. | 52 // context owned by the connecting extension in that process/tab. |
| 53 // - Once the channel is established, either side can call postMessage to send | 53 // - Once the channel is established, either side can call postMessage to send |
| 54 // a message to the opposite side of the channel, which may have multiple | 54 // a message to the opposite side of the channel, which may have multiple |
| 55 // listeners. | 55 // listeners. |
| 56 // | 56 // |
| 57 // Terminology: | 57 // Terminology: |
| 58 // channel: connection between two ports | 58 // channel: connection between two ports |
| 59 // port: an IPC::Message::Process interface and an optional routing_id (in the | 59 // port: an IPC::Message::Process interface and an optional routing_id (in the |
| 60 // case that the port is a tab). The Process is usually either a | 60 // case that the port is a tab). The Process is usually either a |
| 61 // RenderProcessHost or a RenderViewHost. | 61 // RenderProcessHost or a RenderViewHost. |
| 62 class MessageService : public ProfileKeyedAPI, | 62 class MessageService : public BrowserContextKeyedAPI, |
| 63 public content::NotificationObserver, | 63 public content::NotificationObserver, |
| 64 public NativeMessageProcessHost::Client { | 64 public NativeMessageProcessHost::Client { |
| 65 public: | 65 public: |
| 66 // A messaging channel. Note that the opening port can be the same as the | 66 // A messaging channel. Note that the opening port can be the same as the |
| 67 // receiver, if an extension background page wants to talk to its tab (for | 67 // receiver, if an extension background page wants to talk to its tab (for |
| 68 // example). | 68 // example). |
| 69 struct MessageChannel; | 69 struct MessageChannel; |
| 70 | 70 |
| 71 // One side of the communication handled by extensions::MessageService. | 71 // One side of the communication handled by extensions::MessageService. |
| 72 class MessagePort { | 72 class MessagePort { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 DISALLOW_COPY_AND_ASSIGN(MessagePort); | 105 DISALLOW_COPY_AND_ASSIGN(MessagePort); |
| 106 }; | 106 }; |
| 107 | 107 |
| 108 // Allocates a pair of port ids. | 108 // Allocates a pair of port ids. |
| 109 // NOTE: this can be called from any thread. | 109 // NOTE: this can be called from any thread. |
| 110 static void AllocatePortIdPair(int* port1, int* port2); | 110 static void AllocatePortIdPair(int* port1, int* port2); |
| 111 | 111 |
| 112 explicit MessageService(content::BrowserContext* context); | 112 explicit MessageService(content::BrowserContext* context); |
| 113 virtual ~MessageService(); | 113 virtual ~MessageService(); |
| 114 | 114 |
| 115 // ProfileKeyedAPI implementation. | 115 // BrowserContextKeyedAPI implementation. |
| 116 static ProfileKeyedAPIFactory<MessageService>* GetFactoryInstance(); | 116 static BrowserContextKeyedAPIFactory<MessageService>* GetFactoryInstance(); |
| 117 | 117 |
| 118 // Convenience method to get the MessageService for a browser context. | 118 // Convenience method to get the MessageService for a browser context. |
| 119 static MessageService* Get(content::BrowserContext* context); | 119 static MessageService* Get(content::BrowserContext* context); |
| 120 | 120 |
| 121 // Given an extension's ID, opens a channel between the given renderer "port" | 121 // Given an extension's ID, opens a channel between the given renderer "port" |
| 122 // and every listening context owned by that extension. |channel_name| is | 122 // and every listening context owned by that extension. |channel_name| is |
| 123 // an optional identifier for use by extension developers. | 123 // an optional identifier for use by extension developers. |
| 124 void OpenChannelToExtension( | 124 void OpenChannelToExtension( |
| 125 int source_process_id, int source_routing_id, int receiver_port_id, | 125 int source_process_id, int source_routing_id, int receiver_port_id, |
| 126 const std::string& source_extension_id, | 126 const std::string& source_extension_id, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 153 // port if the channel isn't pending. | 153 // port if the channel isn't pending. |
| 154 void PostMessage(int port_id, const Message& message); | 154 void PostMessage(int port_id, const Message& message); |
| 155 | 155 |
| 156 // NativeMessageProcessHost::Client | 156 // NativeMessageProcessHost::Client |
| 157 virtual void PostMessageFromNativeProcess( | 157 virtual void PostMessageFromNativeProcess( |
| 158 int port_id, | 158 int port_id, |
| 159 const std::string& message) OVERRIDE; | 159 const std::string& message) OVERRIDE; |
| 160 | 160 |
| 161 private: | 161 private: |
| 162 friend class MockMessageService; | 162 friend class MockMessageService; |
| 163 friend class ProfileKeyedAPIFactory<MessageService>; | 163 friend class BrowserContextKeyedAPIFactory<MessageService>; |
| 164 struct OpenChannelParams; | 164 struct OpenChannelParams; |
| 165 | 165 |
| 166 // A map of channel ID to its channel object. | 166 // A map of channel ID to its channel object. |
| 167 typedef std::map<int, MessageChannel*> MessageChannelMap; | 167 typedef std::map<int, MessageChannel*> MessageChannelMap; |
| 168 | 168 |
| 169 typedef std::pair<int, Message> PendingMessage; | 169 typedef std::pair<int, Message> PendingMessage; |
| 170 typedef std::vector<PendingMessage> PendingMessagesQueue; | 170 typedef std::vector<PendingMessage> PendingMessagesQueue; |
| 171 // A set of channel IDs waiting for TLS channel IDs to complete opening, | 171 // A set of channel IDs waiting for TLS channel IDs to complete opening, |
| 172 // and any pending messages queued to be sent on those channels. | 172 // and any pending messages queued to be sent on those channels. |
| 173 typedef std::map<int, PendingMessagesQueue> PendingTlsChannelIdMap; | 173 typedef std::map<int, PendingMessagesQueue> PendingTlsChannelIdMap; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 if (host) | 243 if (host) |
| 244 PostMessage(port_id, message); | 244 PostMessage(port_id, message); |
| 245 } | 245 } |
| 246 | 246 |
| 247 // Immediate dispatches a disconnect to |source| for |port_id|. Sets source's | 247 // Immediate dispatches a disconnect to |source| for |port_id|. Sets source's |
| 248 // runtime.lastMessage to |error_message|, if any. | 248 // runtime.lastMessage to |error_message|, if any. |
| 249 void DispatchOnDisconnect(content::RenderProcessHost* source, | 249 void DispatchOnDisconnect(content::RenderProcessHost* source, |
| 250 int port_id, | 250 int port_id, |
| 251 const std::string& error_message); | 251 const std::string& error_message); |
| 252 | 252 |
| 253 // ProfileKeyedAPI implementation. | 253 // BrowserContextKeyedAPI implementation. |
| 254 static const char* service_name() { | 254 static const char* service_name() { |
| 255 return "MessageService"; | 255 return "MessageService"; |
| 256 } | 256 } |
| 257 static const bool kServiceRedirectedInIncognito = true; | 257 static const bool kServiceRedirectedInIncognito = true; |
| 258 static const bool kServiceIsCreatedWithBrowserContext = false; | 258 static const bool kServiceIsCreatedWithBrowserContext = false; |
| 259 static const bool kServiceIsNULLWhileTesting = true; | 259 static const bool kServiceIsNULLWhileTesting = true; |
| 260 | 260 |
| 261 content::NotificationRegistrar registrar_; | 261 content::NotificationRegistrar registrar_; |
| 262 MessageChannelMap channels_; | 262 MessageChannelMap channels_; |
| 263 PendingTlsChannelIdMap pending_tls_channel_id_channels_; | 263 PendingTlsChannelIdMap pending_tls_channel_id_channels_; |
| 264 PendingLazyBackgroundPageChannelMap pending_lazy_background_page_channels_; | 264 PendingLazyBackgroundPageChannelMap pending_lazy_background_page_channels_; |
| 265 MessagePropertyProvider property_provider_; | 265 MessagePropertyProvider property_provider_; |
| 266 | 266 |
| 267 // Weak pointer. Guaranteed to outlive this class. | 267 // Weak pointer. Guaranteed to outlive this class. |
| 268 LazyBackgroundTaskQueue* lazy_background_task_queue_; | 268 LazyBackgroundTaskQueue* lazy_background_task_queue_; |
| 269 | 269 |
| 270 base::WeakPtrFactory<MessageService> weak_factory_; | 270 base::WeakPtrFactory<MessageService> weak_factory_; |
| 271 | 271 |
| 272 DISALLOW_COPY_AND_ASSIGN(MessageService); | 272 DISALLOW_COPY_AND_ASSIGN(MessageService); |
| 273 }; | 273 }; |
| 274 | 274 |
| 275 } // namespace extensions | 275 } // namespace extensions |
| 276 | 276 |
| 277 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ | 277 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ |
| OLD | NEW |