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