| 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" | |
| 18 #include "content/public/browser/notification_observer.h" | 17 #include "content/public/browser/notification_observer.h" |
| 19 #include "content/public/browser/notification_registrar.h" | 18 #include "content/public/browser/notification_registrar.h" |
| 19 #include "extensions/browser/api/messaging/native_message_host.h" |
| 20 #include "extensions/browser/browser_context_keyed_api_factory.h" | 20 #include "extensions/browser/browser_context_keyed_api_factory.h" |
| 21 #include "extensions/common/api/messaging/message.h" | 21 #include "extensions/common/api/messaging/message.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 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 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 BrowserContextKeyedAPI, | 62 class MessageService : public BrowserContextKeyedAPI, |
| 63 public content::NotificationObserver, | 63 public content::NotificationObserver { |
| 64 public NativeMessageProcessHost::Client { | |
| 65 public: | 64 public: |
| 66 // A messaging channel. Note that the opening port can be the same as the | 65 // 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 | 66 // receiver, if an extension background page wants to talk to its tab (for |
| 68 // example). | 67 // example). |
| 69 struct MessageChannel; | 68 struct MessageChannel; |
| 70 | 69 |
| 71 // One side of the communication handled by extensions::MessageService. | 70 // One side of the communication handled by extensions::MessageService. |
| 72 class MessagePort { | 71 class MessagePort { |
| 73 public: | 72 public: |
| 74 virtual ~MessagePort() {} | 73 virtual ~MessagePort() {} |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 | 138 |
| 140 void OpenChannelToNativeApp( | 139 void OpenChannelToNativeApp( |
| 141 int source_process_id, | 140 int source_process_id, |
| 142 int source_routing_id, | 141 int source_routing_id, |
| 143 int receiver_port_id, | 142 int receiver_port_id, |
| 144 const std::string& source_extension_id, | 143 const std::string& source_extension_id, |
| 145 const std::string& native_app_name); | 144 const std::string& native_app_name); |
| 146 | 145 |
| 147 // Closes the message channel associated with the given port, and notifies | 146 // Closes the message channel associated with the given port, and notifies |
| 148 // the other side. | 147 // the other side. |
| 149 virtual void CloseChannel(int port_id, | 148 void CloseChannel(int port_id, const std::string& error_message); |
| 150 const std::string& error_message) OVERRIDE; | |
| 151 | 149 |
| 152 // Enqueues a message on a pending channel, or sends a message to the given | 150 // Enqueues a message on a pending channel, or sends a message to the given |
| 153 // port if the channel isn't pending. | 151 // port if the channel isn't pending. |
| 154 void PostMessage(int port_id, const Message& message); | 152 void PostMessage(int port_id, const Message& message); |
| 155 | 153 |
| 156 // NativeMessageProcessHost::Client | |
| 157 virtual void PostMessageFromNativeProcess( | |
| 158 int port_id, | |
| 159 const std::string& message) OVERRIDE; | |
| 160 | |
| 161 private: | 154 private: |
| 162 friend class MockMessageService; | 155 friend class MockMessageService; |
| 163 friend class BrowserContextKeyedAPIFactory<MessageService>; | 156 friend class BrowserContextKeyedAPIFactory<MessageService>; |
| 164 struct OpenChannelParams; | 157 struct OpenChannelParams; |
| 165 | 158 |
| 166 // A map of channel ID to its channel object. | 159 // A map of channel ID to its channel object. |
| 167 typedef std::map<int, MessageChannel*> MessageChannelMap; | 160 typedef std::map<int, MessageChannel*> MessageChannelMap; |
| 168 | 161 |
| 169 typedef std::pair<int, Message> PendingMessage; | 162 typedef std::pair<int, Message> PendingMessage; |
| 170 typedef std::vector<PendingMessage> PendingMessagesQueue; | 163 typedef std::vector<PendingMessage> PendingMessagesQueue; |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 LazyBackgroundTaskQueue* lazy_background_task_queue_; | 261 LazyBackgroundTaskQueue* lazy_background_task_queue_; |
| 269 | 262 |
| 270 base::WeakPtrFactory<MessageService> weak_factory_; | 263 base::WeakPtrFactory<MessageService> weak_factory_; |
| 271 | 264 |
| 272 DISALLOW_COPY_AND_ASSIGN(MessageService); | 265 DISALLOW_COPY_AND_ASSIGN(MessageService); |
| 273 }; | 266 }; |
| 274 | 267 |
| 275 } // namespace extensions | 268 } // namespace extensions |
| 276 | 269 |
| 277 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ | 270 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ |
| OLD | NEW |