| 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 "chrome/browser/extensions/api/profile_keyed_api_factory.h" |
| 17 #include "content/public/browser/notification_observer.h" | 17 #include "content/public/browser/notification_observer.h" |
| 18 #include "content/public/browser/notification_registrar.h" | 18 #include "content/public/browser/notification_registrar.h" |
| 19 | 19 |
| 20 class GURL; | 20 class GURL; |
| 21 class Profile; | 21 class Profile; |
| 22 | 22 |
| 23 namespace base { | 23 namespace base { |
| 24 class DictionaryValue; | 24 class DictionaryValue; |
| 25 class ListValue; | |
| 26 } | 25 } |
| 27 | 26 |
| 28 namespace content { | 27 namespace content { |
| 29 class RenderProcessHost; | 28 class RenderProcessHost; |
| 30 class WebContents; | 29 class WebContents; |
| 31 } | 30 } |
| 32 | 31 |
| 33 namespace extensions { | 32 namespace extensions { |
| 34 class Extension; | 33 class Extension; |
| 35 class ExtensionHost; | 34 class ExtensionHost; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 const std::string& source_extension_id, | 75 const std::string& source_extension_id, |
| 77 const std::string& target_extension_id, | 76 const std::string& target_extension_id, |
| 78 const GURL& source_url) {} | 77 const GURL& source_url) {} |
| 79 | 78 |
| 80 // Notify the port that the channel has been closed. If |error_message| is | 79 // Notify the port that the channel has been closed. If |error_message| is |
| 81 // non-empty, it indicates an error occurred while opening the connection. | 80 // non-empty, it indicates an error occurred while opening the connection. |
| 82 virtual void DispatchOnDisconnect(int source_port_id, | 81 virtual void DispatchOnDisconnect(int source_port_id, |
| 83 const std::string& error_message) {} | 82 const std::string& error_message) {} |
| 84 | 83 |
| 85 // Dispatch a message to this end of the communication. | 84 // Dispatch a message to this end of the communication. |
| 86 virtual void DispatchOnMessage(scoped_ptr<base::ListValue> message, | 85 virtual void DispatchOnMessage(const std::string& message, |
| 87 int target_port_id) = 0; | 86 int target_port_id) = 0; |
| 88 | 87 |
| 89 // MessagPorts that target extensions will need to adjust their keepalive | 88 // MessagPorts that target extensions will need to adjust their keepalive |
| 90 // counts for their lazy background page. | 89 // counts for their lazy background page. |
| 91 virtual void IncrementLazyKeepaliveCount() {} | 90 virtual void IncrementLazyKeepaliveCount() {} |
| 92 virtual void DecrementLazyKeepaliveCount() {} | 91 virtual void DecrementLazyKeepaliveCount() {} |
| 93 | 92 |
| 94 // Get the RenderProcessHost (if any) associated with the port. | 93 // Get the RenderProcessHost (if any) associated with the port. |
| 95 virtual content::RenderProcessHost* GetRenderProcessHost(); | 94 virtual content::RenderProcessHost* GetRenderProcessHost(); |
| 96 | 95 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 int receiver_port_id, | 137 int receiver_port_id, |
| 139 const std::string& source_extension_id, | 138 const std::string& source_extension_id, |
| 140 const std::string& native_app_name); | 139 const std::string& native_app_name); |
| 141 | 140 |
| 142 // Closes the message channel associated with the given port, and notifies | 141 // Closes the message channel associated with the given port, and notifies |
| 143 // the other side. | 142 // the other side. |
| 144 virtual void CloseChannel(int port_id, | 143 virtual void CloseChannel(int port_id, |
| 145 const std::string& error_message) OVERRIDE; | 144 const std::string& error_message) OVERRIDE; |
| 146 | 145 |
| 147 // Sends a message to the given port. | 146 // Sends a message to the given port. |
| 148 void PostMessage(int port_id, scoped_ptr<base::ListValue> message); | 147 void PostMessage(int port_id, const std::string& message); |
| 149 | 148 |
| 150 // NativeMessageProcessHost::Client | 149 // NativeMessageProcessHost::Client |
| 151 virtual void PostMessageFromNativeProcess( | 150 virtual void PostMessageFromNativeProcess( |
| 152 int port_id, | 151 int port_id, |
| 153 scoped_ptr<base::ListValue> message) OVERRIDE; | 152 const std::string& message) OVERRIDE; |
| 154 | 153 |
| 155 private: | 154 private: |
| 156 friend class MockMessageService; | 155 friend class MockMessageService; |
| 157 friend class ProfileKeyedAPIFactory<MessageService>; | 156 friend class ProfileKeyedAPIFactory<MessageService>; |
| 158 struct OpenChannelParams; | 157 struct OpenChannelParams; |
| 159 | 158 |
| 160 // A map of channel ID to its channel object. | 159 // A map of channel ID to its channel object. |
| 161 typedef std::map<int, MessageChannel*> MessageChannelMap; | 160 typedef std::map<int, MessageChannel*> MessageChannelMap; |
| 162 | 161 |
| 163 // 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 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 void PendingOpenChannel(scoped_ptr<OpenChannelParams> params, | 198 void PendingOpenChannel(scoped_ptr<OpenChannelParams> params, |
| 200 int source_process_id, | 199 int source_process_id, |
| 201 extensions::ExtensionHost* host); | 200 extensions::ExtensionHost* host); |
| 202 void PendingCloseChannel(int port_id, | 201 void PendingCloseChannel(int port_id, |
| 203 const std::string& error_message, | 202 const std::string& error_message, |
| 204 extensions::ExtensionHost* host) { | 203 extensions::ExtensionHost* host) { |
| 205 if (host) | 204 if (host) |
| 206 CloseChannel(port_id, error_message); | 205 CloseChannel(port_id, error_message); |
| 207 } | 206 } |
| 208 void PendingPostMessage(int port_id, | 207 void PendingPostMessage(int port_id, |
| 209 scoped_ptr<base::ListValue> message, | 208 const std::string& message, |
| 210 extensions::ExtensionHost* host) { | 209 extensions::ExtensionHost* host) { |
| 211 if (host) | 210 if (host) |
| 212 PostMessage(port_id, message.Pass()); | 211 PostMessage(port_id, message); |
| 213 } | 212 } |
| 214 | 213 |
| 215 // ProfileKeyedAPI implementation. | 214 // ProfileKeyedAPI implementation. |
| 216 static const char* service_name() { | 215 static const char* service_name() { |
| 217 return "MessageService"; | 216 return "MessageService"; |
| 218 } | 217 } |
| 219 static const bool kServiceRedirectedInIncognito = true; | 218 static const bool kServiceRedirectedInIncognito = true; |
| 220 | 219 |
| 221 content::NotificationRegistrar registrar_; | 220 content::NotificationRegistrar registrar_; |
| 222 MessageChannelMap channels_; | 221 MessageChannelMap channels_; |
| 223 PendingChannelMap pending_channels_; | 222 PendingChannelMap pending_channels_; |
| 224 | 223 |
| 225 // Weak pointer. Guaranteed to outlive this class. | 224 // Weak pointer. Guaranteed to outlive this class. |
| 226 LazyBackgroundTaskQueue* lazy_background_task_queue_; | 225 LazyBackgroundTaskQueue* lazy_background_task_queue_; |
| 227 | 226 |
| 228 base::WeakPtrFactory<MessageService> weak_factory_; | 227 base::WeakPtrFactory<MessageService> weak_factory_; |
| 229 | 228 |
| 230 DISALLOW_COPY_AND_ASSIGN(MessageService); | 229 DISALLOW_COPY_AND_ASSIGN(MessageService); |
| 231 }; | 230 }; |
| 232 | 231 |
| 233 } // namespace extensions | 232 } // namespace extensions |
| 234 | 233 |
| 235 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ | 234 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ |
| OLD | NEW |