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/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
hashimoto
2014/06/12 13:03:36
nit: No need to include this?
rvargas (doing something else)
2014/06/13 02:49:10
Done.
| |
14 #include "base/process/process.h" | 14 #include "base/process/process.h" |
15 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" | 15 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" |
16 #include "ui/gfx/native_widget_types.h" | 16 #include "ui/gfx/native_widget_types.h" |
17 | 17 |
18 class PrefService; | 18 class PrefService; |
19 | 19 |
20 namespace net { | 20 namespace net { |
21 | 21 |
22 class DrainableIOBuffer; | 22 class DrainableIOBuffer; |
23 class FileStream; | 23 class FileStream; |
24 class IOBuffer; | 24 class IOBuffer; |
25 class IOBufferWithSize; | 25 class IOBufferWithSize; |
26 | 26 |
27 } // namespace net | 27 } // namespace net |
28 | 28 |
29 namespace extensions { | 29 namespace extensions { |
30 | 30 |
31 // 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 |
32 // process. | 32 // process. |
33 // | 33 // |
34 // 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. |
35 // 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 |
36 // thread. | 36 // thread. |
37 class NativeMessageProcessHost | 37 class NativeMessageProcessHost { |
38 #if defined(OS_POSIX) | |
39 : public base::MessageLoopForIO::Watcher | |
40 #endif // !defined(OS_POSIX) | |
41 { | |
42 public: | 38 public: |
43 // Interface for the object that receives messages from the native process. | 39 // Interface for the object that receives messages from the native process. |
44 class Client { | 40 class Client { |
45 public: | 41 public: |
46 virtual ~Client() {} | 42 virtual ~Client() {} |
47 // Called on the UI thread. | 43 // Called on the UI thread. |
48 virtual void PostMessageFromNativeProcess(int port_id, | 44 virtual void PostMessageFromNativeProcess(int port_id, |
49 const std::string& message) = 0; | 45 const std::string& message) = 0; |
50 virtual void CloseChannel(int port_id, | 46 virtual void CloseChannel(int port_id, |
51 const std::string& error_message) = 0; | 47 const std::string& error_message) = 0; |
(...skipping 24 matching lines...) Expand all Loading... | |
76 static scoped_ptr<NativeMessageProcessHost> CreateWithLauncher( | 72 static scoped_ptr<NativeMessageProcessHost> CreateWithLauncher( |
77 base::WeakPtr<Client> weak_client_ui, | 73 base::WeakPtr<Client> weak_client_ui, |
78 const std::string& source_extension_id, | 74 const std::string& source_extension_id, |
79 const std::string& native_host_name, | 75 const std::string& native_host_name, |
80 int destination_port, | 76 int destination_port, |
81 scoped_ptr<NativeProcessLauncher> launcher); | 77 scoped_ptr<NativeProcessLauncher> launcher); |
82 | 78 |
83 // Send a message with the specified payload. | 79 // Send a message with the specified payload. |
84 void Send(const std::string& json); | 80 void Send(const std::string& json); |
85 | 81 |
86 #if defined(OS_POSIX) | 82 // Use regular Read instead of ReadNonBlocking when reading from a file. |
87 // MessageLoopForIO::Watcher interface | 83 void DontWaitToReadForTesting(); |
88 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; | |
89 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; | |
90 #endif // !defined(OS_POSIX) | |
91 | |
92 // Try and read a single message from |read_file_|. This should only be called | |
93 // in unittests when you know there is data in the file. | |
94 void ReadNowForTesting(); | |
95 | 84 |
96 private: | 85 private: |
97 NativeMessageProcessHost(base::WeakPtr<Client> weak_client_ui, | 86 NativeMessageProcessHost(base::WeakPtr<Client> weak_client_ui, |
98 const std::string& source_extension_id, | 87 const std::string& source_extension_id, |
99 const std::string& native_host_name, | 88 const std::string& native_host_name, |
100 int destination_port, | 89 int destination_port, |
101 scoped_ptr<NativeProcessLauncher> launcher); | 90 scoped_ptr<NativeProcessLauncher> launcher); |
102 | 91 |
103 // Starts the host process. | 92 // Starts the host process. |
104 void LaunchHostProcess(); | 93 void LaunchHostProcess(); |
(...skipping 29 matching lines...) Expand all Loading... | |
134 // Name of the native messaging host. | 123 // Name of the native messaging host. |
135 std::string native_host_name_; | 124 std::string native_host_name_; |
136 | 125 |
137 // The id of the port on the other side of this connection. This is passed to | 126 // The id of the port on the other side of this connection. This is passed to |
138 // |weak_client_ui_| when posting messages. | 127 // |weak_client_ui_| when posting messages. |
139 int destination_port_; | 128 int destination_port_; |
140 | 129 |
141 // Launcher used to launch the native process. | 130 // Launcher used to launch the native process. |
142 scoped_ptr<NativeProcessLauncher> launcher_; | 131 scoped_ptr<NativeProcessLauncher> launcher_; |
143 | 132 |
144 // Set to true after the native messaging connection has been stopped, e.g. | |
145 // due to an error. | |
146 bool closed_; | |
147 | |
148 base::ProcessHandle process_handle_; | 133 base::ProcessHandle process_handle_; |
149 | 134 |
150 // Input stream reader. | 135 // Input stream reader. |
151 scoped_ptr<net::FileStream> read_stream_; | 136 scoped_ptr<net::FileStream> read_stream_; |
152 | 137 |
153 #if defined(OS_POSIX) | |
154 // TODO(rvargas): Remove these members, maybe merging the functionality to | |
155 // net::FileStream. | |
156 base::PlatformFile read_file_; | |
157 base::MessageLoopForIO::FileDescriptorWatcher read_watcher_; | |
158 #endif // !defined(OS_POSIX) | |
159 | |
160 // Write stream. | 138 // Write stream. |
161 scoped_ptr<net::FileStream> write_stream_; | 139 scoped_ptr<net::FileStream> write_stream_; |
162 | 140 |
163 // Read buffer passed to FileStream::Read(). | 141 // Read buffer passed to FileStream::Read(). |
164 scoped_refptr<net::IOBuffer> read_buffer_; | 142 scoped_refptr<net::IOBuffer> read_buffer_; |
165 | 143 |
144 // Set to true after the native messaging connection has been stopped, e.g. | |
145 // due to an error. | |
146 bool closed_; | |
147 | |
166 // Set to true when a read is pending. | 148 // Set to true when a read is pending. |
167 bool read_pending_; | 149 bool read_pending_; |
168 | 150 |
151 // Set to true when a write is pending. | |
152 bool write_pending_; | |
153 | |
154 // Set to true to use regular reads during a test. | |
155 bool direct_read_for_test_; | |
156 | |
169 // Buffer for incomplete incoming messages. | 157 // Buffer for incomplete incoming messages. |
170 std::string incoming_data_; | 158 std::string incoming_data_; |
171 | 159 |
172 // Queue for outgoing messages. | 160 // Queue for outgoing messages. |
173 std::queue<scoped_refptr<net::IOBufferWithSize> > write_queue_; | 161 std::queue<scoped_refptr<net::IOBufferWithSize> > write_queue_; |
174 | 162 |
175 // The message that's currently being sent. | 163 // The message that's currently being sent. |
176 scoped_refptr<net::DrainableIOBuffer> current_write_buffer_; | 164 scoped_refptr<net::DrainableIOBuffer> current_write_buffer_; |
177 | 165 |
178 // Set to true when a write is pending. | |
179 bool write_pending_; | |
180 | |
181 DISALLOW_COPY_AND_ASSIGN(NativeMessageProcessHost); | 166 DISALLOW_COPY_AND_ASSIGN(NativeMessageProcessHost); |
182 }; | 167 }; |
183 | 168 |
184 } // namespace extensions | 169 } // namespace extensions |
185 | 170 |
186 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H _ | 171 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H _ |
OLD | NEW |