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 "content/public/browser/notification_observer.h" | 18 #include "content/public/browser/notification_observer.h" |
18 #include "content/public/browser/notification_registrar.h" | 19 #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 { |
64 public: | 65 public: |
65 // 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 |
66 // 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 |
67 // example). | 68 // example). |
68 struct MessageChannel; | 69 struct MessageChannel; |
69 | 70 |
70 // One side of the communication handled by extensions::MessageService. | 71 // One side of the communication handled by extensions::MessageService. |
71 class MessagePort { | 72 class MessagePort { |
72 public: | 73 public: |
73 virtual ~MessagePort() {} | 74 virtual ~MessagePort() {} |
(...skipping 23 matching lines...) Expand all Loading... |
97 // Get the RenderProcessHost (if any) associated with the port. | 98 // Get the RenderProcessHost (if any) associated with the port. |
98 virtual content::RenderProcessHost* GetRenderProcessHost(); | 99 virtual content::RenderProcessHost* GetRenderProcessHost(); |
99 | 100 |
100 protected: | 101 protected: |
101 MessagePort() {} | 102 MessagePort() {} |
102 | 103 |
103 private: | 104 private: |
104 DISALLOW_COPY_AND_ASSIGN(MessagePort); | 105 DISALLOW_COPY_AND_ASSIGN(MessagePort); |
105 }; | 106 }; |
106 | 107 |
107 enum PolicyPermission { | |
108 DISALLOW, // The host is not allowed. | |
109 ALLOW_SYSTEM_ONLY, // Allowed only when installed on system level. | |
110 ALLOW_ALL, // Allowed when installed on system or user level. | |
111 }; | |
112 | |
113 static PolicyPermission IsNativeMessagingHostAllowed( | |
114 const PrefService* pref_service, | |
115 const std::string& native_host_name); | |
116 | |
117 // Allocates a pair of port ids. | 108 // Allocates a pair of port ids. |
118 // NOTE: this can be called from any thread. | 109 // NOTE: this can be called from any thread. |
119 static void AllocatePortIdPair(int* port1, int* port2); | 110 static void AllocatePortIdPair(int* port1, int* port2); |
120 | 111 |
121 explicit MessageService(content::BrowserContext* context); | 112 explicit MessageService(content::BrowserContext* context); |
122 virtual ~MessageService(); | 113 virtual ~MessageService(); |
123 | 114 |
124 // BrowserContextKeyedAPI implementation. | 115 // BrowserContextKeyedAPI implementation. |
125 static BrowserContextKeyedAPIFactory<MessageService>* GetFactoryInstance(); | 116 static BrowserContextKeyedAPIFactory<MessageService>* GetFactoryInstance(); |
126 | 117 |
(...skipping 21 matching lines...) Expand all Loading... |
148 | 139 |
149 void OpenChannelToNativeApp( | 140 void OpenChannelToNativeApp( |
150 int source_process_id, | 141 int source_process_id, |
151 int source_routing_id, | 142 int source_routing_id, |
152 int receiver_port_id, | 143 int receiver_port_id, |
153 const std::string& source_extension_id, | 144 const std::string& source_extension_id, |
154 const std::string& native_app_name); | 145 const std::string& native_app_name); |
155 | 146 |
156 // Closes the message channel associated with the given port, and notifies | 147 // Closes the message channel associated with the given port, and notifies |
157 // the other side. | 148 // the other side. |
158 void CloseChannel(int port_id, const std::string& error_message); | 149 virtual void CloseChannel(int port_id, |
| 150 const std::string& error_message) override; |
159 | 151 |
160 // Enqueues a message on a pending channel, or sends a message to the given | 152 // Enqueues a message on a pending channel, or sends a message to the given |
161 // port if the channel isn't pending. | 153 // port if the channel isn't pending. |
162 void PostMessage(int port_id, const Message& message); | 154 void PostMessage(int port_id, const Message& message); |
163 | 155 |
| 156 // NativeMessageProcessHost::Client |
| 157 virtual void PostMessageFromNativeProcess( |
| 158 int port_id, |
| 159 const std::string& message) override; |
| 160 |
164 private: | 161 private: |
165 friend class MockMessageService; | 162 friend class MockMessageService; |
166 friend class BrowserContextKeyedAPIFactory<MessageService>; | 163 friend class BrowserContextKeyedAPIFactory<MessageService>; |
167 struct OpenChannelParams; | 164 struct OpenChannelParams; |
168 | 165 |
169 // A map of channel ID to its channel object. | 166 // A map of channel ID to its channel object. |
170 typedef std::map<int, MessageChannel*> MessageChannelMap; | 167 typedef std::map<int, MessageChannel*> MessageChannelMap; |
171 | 168 |
172 typedef std::pair<int, Message> PendingMessage; | 169 typedef std::pair<int, Message> PendingMessage; |
173 typedef std::vector<PendingMessage> PendingMessagesQueue; | 170 typedef std::vector<PendingMessage> PendingMessagesQueue; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 LazyBackgroundTaskQueue* lazy_background_task_queue_; | 268 LazyBackgroundTaskQueue* lazy_background_task_queue_; |
272 | 269 |
273 base::WeakPtrFactory<MessageService> weak_factory_; | 270 base::WeakPtrFactory<MessageService> weak_factory_; |
274 | 271 |
275 DISALLOW_COPY_AND_ASSIGN(MessageService); | 272 DISALLOW_COPY_AND_ASSIGN(MessageService); |
276 }; | 273 }; |
277 | 274 |
278 } // namespace extensions | 275 } // namespace extensions |
279 | 276 |
280 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ | 277 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ |
OLD | NEW |