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_NATIVE_MESSAGE_PROCESS_HOST_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_ |
7 | 7 |
8 #include <queue> | 8 #include <queue> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/files/file.h" | 11 #include "base/files/file.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "base/process/process.h" | 15 #include "base/process/process.h" |
16 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" | 16 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" |
17 #include "extensions/browser/api/messaging/native_message_host.h" | |
17 #include "ui/gfx/native_widget_types.h" | 18 #include "ui/gfx/native_widget_types.h" |
18 | 19 |
19 class PrefService; | |
20 | |
21 namespace net { | 20 namespace net { |
22 | 21 |
23 class DrainableIOBuffer; | 22 class DrainableIOBuffer; |
24 class FileStream; | 23 class FileStream; |
25 class IOBuffer; | 24 class IOBuffer; |
26 class IOBufferWithSize; | 25 class IOBufferWithSize; |
27 | 26 |
28 } // namespace net | 27 } // namespace net |
29 | 28 |
30 namespace extensions { | 29 namespace extensions { |
31 | 30 |
32 // Manages the native side of a connection between an extension and a native | 31 // Manages the native side of a connection between an extension and a native |
33 // process. | 32 // process. |
34 // | 33 // |
35 // This class must only be created, called, and deleted on the IO thread. | 34 // This class must only be created, called, and deleted on the IO thread. |
36 // Public methods typically accept callbacks which will be invoked on the UI | 35 // Public methods typically accept callbacks which will be invoked on the UI |
37 // thread. | 36 // thread. |
38 class NativeMessageProcessHost | 37 class NativeMessageProcessHost : |
39 #if defined(OS_POSIX) | 38 #if defined(OS_POSIX) |
40 : public base::MessageLoopForIO::Watcher | 39 public base::MessageLoopForIO::Watcher, |
41 #endif // !defined(OS_POSIX) | 40 #endif // !defined(OS_POSIX) |
42 { | 41 public NativeMessageHost { |
43 public: | 42 public: |
44 // Interface for the object that receives messages from the native process. | 43 typedef NativeMessageHost::Client Client; |
Sergey Ulanov
2014/09/30 20:33:39
don't need this typedef.
kelvinp
2014/10/01 06:08:02
Done.
| |
45 class Client { | |
46 public: | |
47 virtual ~Client() {} | |
48 // Called on the UI thread. | |
49 virtual void PostMessageFromNativeProcess(int port_id, | |
50 const std::string& message) = 0; | |
51 virtual void CloseChannel(int port_id, | |
52 const std::string& error_message) = 0; | |
53 }; | |
54 | |
55 // Result returned from IsHostAllowed(). | |
56 enum PolicyPermission { | |
57 DISALLOW, // The host is not allowed. | |
58 ALLOW_SYSTEM_ONLY, // Allowed only when installed on system level. | |
59 ALLOW_ALL, // Allowed when installed on system or user level. | |
60 }; | |
61 | 44 |
62 virtual ~NativeMessageProcessHost(); | 45 virtual ~NativeMessageProcessHost(); |
63 | 46 |
64 // Returns policy permissions for the host with the specified name. | 47 // Create using specified |launcher|. Used in tests. |
65 static PolicyPermission IsHostAllowed(const PrefService* pref_service, | 48 static scoped_ptr<NativeMessageHost> CreateWithLauncher( |
66 const std::string& native_host_name); | |
67 | |
68 static scoped_ptr<NativeMessageProcessHost> Create( | |
69 gfx::NativeView native_view, | |
70 base::WeakPtr<Client> weak_client_ui, | |
71 const std::string& source_extension_id, | 49 const std::string& source_extension_id, |
72 const std::string& native_host_name, | 50 const std::string& native_host_name, |
73 int destination_port, | |
74 bool allow_user_level); | |
75 | |
76 // Create using specified |launcher|. Used in tests. | |
77 static scoped_ptr<NativeMessageProcessHost> CreateWithLauncher( | |
78 base::WeakPtr<Client> weak_client_ui, | |
79 const std::string& source_extension_id, | |
80 const std::string& native_host_name, | |
81 int destination_port, | |
82 scoped_ptr<NativeProcessLauncher> launcher); | 51 scoped_ptr<NativeProcessLauncher> launcher); |
83 | 52 |
84 // Send a message with the specified payload. | 53 // extensions::NativeMessageHost implementation. |
85 void Send(const std::string& json); | 54 virtual void OnMessage(const std::string& message) OVERRIDE; |
55 virtual void set_client(base::WeakPtr<Client> client) OVERRIDE; | |
56 virtual scoped_refptr<base::SingleThreadTaskRunner> task_runner() | |
57 const OVERRIDE; | |
86 | 58 |
87 #if defined(OS_POSIX) | 59 #if defined(OS_POSIX) |
88 // MessageLoopForIO::Watcher interface | 60 // MessageLoopForIO::Watcher interface |
89 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; | 61 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |
90 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; | 62 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; |
91 #endif // !defined(OS_POSIX) | 63 #endif // !defined(OS_POSIX) |
92 | 64 |
93 // Try and read a single message from |read_file_|. This should only be called | 65 // Try and read a single message from |read_file_|. This should only be called |
94 // in unittests when you know there is data in the file. | 66 // in unittests when you know there is data in the file. |
95 void ReadNowForTesting(); | 67 void ReadNowForTesting(); |
96 | 68 |
97 private: | 69 private: |
98 NativeMessageProcessHost(base::WeakPtr<Client> weak_client_ui, | 70 NativeMessageProcessHost(const std::string& source_extension_id, |
99 const std::string& source_extension_id, | |
100 const std::string& native_host_name, | 71 const std::string& native_host_name, |
101 int destination_port, | |
102 scoped_ptr<NativeProcessLauncher> launcher); | 72 scoped_ptr<NativeProcessLauncher> launcher); |
103 | 73 |
104 // Starts the host process. | 74 // Starts the host process. |
105 void LaunchHostProcess(); | 75 void LaunchHostProcess(); |
106 | 76 |
107 // Callback for NativeProcessLauncher::Launch(). | 77 // Callback for NativeProcessLauncher::Launch(). |
108 void OnHostProcessLaunched(NativeProcessLauncher::LaunchResult result, | 78 void OnHostProcessLaunched(NativeProcessLauncher::LaunchResult result, |
109 base::ProcessHandle process_handle, | 79 base::ProcessHandle process_handle, |
110 base::File read_file, | 80 base::File read_file, |
111 base::File write_file); | 81 base::File write_file); |
112 | 82 |
113 // Helper methods to read incoming messages. | 83 // Helper methods to read incoming messages. |
114 void WaitRead(); | 84 void WaitRead(); |
115 void DoRead(); | 85 void DoRead(); |
116 void OnRead(int result); | 86 void OnRead(int result); |
117 void HandleReadResult(int result); | 87 void HandleReadResult(int result); |
118 void ProcessIncomingData(const char* data, int data_size); | 88 void ProcessIncomingData(const char* data, int data_size); |
119 | 89 |
120 // Helper methods to write outgoing messages. | 90 // Helper methods to write outgoing messages. |
121 void DoWrite(); | 91 void DoWrite(); |
122 void HandleWriteResult(int result); | 92 void HandleWriteResult(int result); |
123 void OnWritten(int result); | 93 void OnWritten(int result); |
124 | 94 |
125 // Closes the connection. Called from OnError() and destructor. | 95 // Closes the connection. Called from OnError() and destructor. |
126 void Close(const std::string& error_message); | 96 void Close(const std::string& error_message); |
127 | 97 |
128 // The Client messages will be posted to. Should only be accessed from the | 98 // The Client messages will be posted to. Should only be accessed from the |
129 // UI thread. | 99 // UI thread. |
130 base::WeakPtr<Client> weak_client_ui_; | 100 base::WeakPtr<Client> weak_client_; |
131 | 101 |
132 // ID of the calling extension. | 102 // ID of the calling extension. |
133 std::string source_extension_id_; | 103 std::string source_extension_id_; |
134 | 104 |
135 // Name of the native messaging host. | 105 // Name of the native messaging host. |
136 std::string native_host_name_; | 106 std::string native_host_name_; |
137 | 107 |
138 // The id of the port on the other side of this connection. This is passed to | |
139 // |weak_client_ui_| when posting messages. | |
140 int destination_port_; | |
141 | |
142 // Launcher used to launch the native process. | 108 // Launcher used to launch the native process. |
143 scoped_ptr<NativeProcessLauncher> launcher_; | 109 scoped_ptr<NativeProcessLauncher> launcher_; |
144 | 110 |
145 // Set to true after the native messaging connection has been stopped, e.g. | 111 // Set to true after the native messaging connection has been stopped, e.g. |
146 // due to an error. | 112 // due to an error. |
147 bool closed_; | 113 bool closed_; |
148 | 114 |
149 base::ProcessHandle process_handle_; | 115 base::ProcessHandle process_handle_; |
150 | 116 |
151 // Input stream reader. | 117 // Input stream reader. |
(...skipping 18 matching lines...) Expand all Loading... | |
170 | 136 |
171 // Queue for outgoing messages. | 137 // Queue for outgoing messages. |
172 std::queue<scoped_refptr<net::IOBufferWithSize> > write_queue_; | 138 std::queue<scoped_refptr<net::IOBufferWithSize> > write_queue_; |
173 | 139 |
174 // The message that's currently being sent. | 140 // The message that's currently being sent. |
175 scoped_refptr<net::DrainableIOBuffer> current_write_buffer_; | 141 scoped_refptr<net::DrainableIOBuffer> current_write_buffer_; |
176 | 142 |
177 // Set to true when a write is pending. | 143 // Set to true when a write is pending. |
178 bool write_pending_; | 144 bool write_pending_; |
179 | 145 |
146 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
147 | |
180 DISALLOW_COPY_AND_ASSIGN(NativeMessageProcessHost); | 148 DISALLOW_COPY_AND_ASSIGN(NativeMessageProcessHost); |
181 }; | 149 }; |
182 | 150 |
183 } // namespace extensions | 151 } // namespace extensions |
184 | 152 |
185 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H _ | 153 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H _ |
OLD | NEW |