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