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