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 <memory> | 8 #include <memory> |
9 #include <queue> | 9 #include <queue> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/files/file.h" | 12 #include "base/files/file.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/process/process.h" | 15 #include "base/process/process.h" |
16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
17 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" | 17 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" |
18 #include "extensions/browser/api/messaging/native_message_host.h" | 18 #include "extensions/browser/api/messaging/native_message_host.h" |
19 #include "ui/gfx/native_widget_types.h" | 19 #include "ui/gfx/native_widget_types.h" |
20 | 20 |
| 21 #if defined(OS_POSIX) |
| 22 #include "base/files/file_descriptor_watcher_posix.h" |
| 23 #endif |
| 24 |
21 namespace net { | 25 namespace net { |
22 | 26 |
23 class DrainableIOBuffer; | 27 class DrainableIOBuffer; |
24 class FileStream; | 28 class FileStream; |
25 class IOBuffer; | 29 class IOBuffer; |
26 class IOBufferWithSize; | 30 class IOBufferWithSize; |
27 | 31 |
28 } // namespace net | 32 } // namespace net |
29 | 33 |
30 namespace extensions { | 34 namespace extensions { |
31 | 35 |
32 // Manages the native side of a connection between an extension and a native | 36 // Manages the native side of a connection between an extension and a native |
33 // process. | 37 // process. |
34 // | 38 // |
35 // This class must only be created, called, and deleted on the IO thread. | 39 // 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 | 40 // Public methods typically accept callbacks which will be invoked on the UI |
37 // thread. | 41 // thread. |
38 class NativeMessageProcessHost : | 42 class NativeMessageProcessHost : public NativeMessageHost { |
39 #if defined(OS_POSIX) | |
40 public base::MessageLoopForIO::Watcher, | |
41 #endif // !defined(OS_POSIX) | |
42 public NativeMessageHost { | |
43 public: | 43 public: |
44 ~NativeMessageProcessHost() override; | 44 ~NativeMessageProcessHost() override; |
45 | 45 |
46 // Create using specified |launcher|. Used in tests. | 46 // Create using specified |launcher|. Used in tests. |
47 static std::unique_ptr<NativeMessageHost> CreateWithLauncher( | 47 static std::unique_ptr<NativeMessageHost> CreateWithLauncher( |
48 const std::string& source_extension_id, | 48 const std::string& source_extension_id, |
49 const std::string& native_host_name, | 49 const std::string& native_host_name, |
50 std::unique_ptr<NativeProcessLauncher> launcher); | 50 std::unique_ptr<NativeProcessLauncher> launcher); |
51 | 51 |
52 // extensions::NativeMessageHost implementation. | 52 // extensions::NativeMessageHost implementation. |
53 void OnMessage(const std::string& message) override; | 53 void OnMessage(const std::string& message) override; |
54 void Start(Client* client) override; | 54 void Start(Client* client) override; |
55 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const override; | 55 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const override; |
56 | 56 |
57 #if defined(OS_POSIX) | |
58 // MessageLoopForIO::Watcher interface | |
59 void OnFileCanReadWithoutBlocking(int fd) override; | |
60 void OnFileCanWriteWithoutBlocking(int fd) override; | |
61 #endif // !defined(OS_POSIX) | |
62 | |
63 // Try and read a single message from |read_file_|. This should only be called | 57 // Try and read a single message from |read_file_|. This should only be called |
64 // in unittests when you know there is data in the file. | 58 // in unittests when you know there is data in the file. |
65 void ReadNowForTesting(); | 59 void ReadNowForTesting(); |
66 | 60 |
67 private: | 61 private: |
68 NativeMessageProcessHost(const std::string& source_extension_id, | 62 NativeMessageProcessHost(const std::string& source_extension_id, |
69 const std::string& native_host_name, | 63 const std::string& native_host_name, |
70 std::unique_ptr<NativeProcessLauncher> launcher); | 64 std::unique_ptr<NativeProcessLauncher> launcher); |
71 | 65 |
72 // Starts the host process. | 66 // Starts the host process. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 // due to an error. | 104 // due to an error. |
111 bool closed_; | 105 bool closed_; |
112 | 106 |
113 base::Process process_; | 107 base::Process process_; |
114 | 108 |
115 // Input stream reader. | 109 // Input stream reader. |
116 std::unique_ptr<net::FileStream> read_stream_; | 110 std::unique_ptr<net::FileStream> read_stream_; |
117 | 111 |
118 #if defined(OS_POSIX) | 112 #if defined(OS_POSIX) |
119 base::PlatformFile read_file_; | 113 base::PlatformFile read_file_; |
120 base::MessageLoopForIO::FileDescriptorWatcher read_watcher_; | 114 std::unique_ptr<base::FileDescriptorWatcher::Controller> read_controller_; |
121 #endif // !defined(OS_POSIX) | 115 #endif // !defined(OS_POSIX) |
122 | 116 |
123 // Write stream. | 117 // Write stream. |
124 std::unique_ptr<net::FileStream> write_stream_; | 118 std::unique_ptr<net::FileStream> write_stream_; |
125 | 119 |
126 // Read buffer passed to FileStream::Read(). | 120 // Read buffer passed to FileStream::Read(). |
127 scoped_refptr<net::IOBuffer> read_buffer_; | 121 scoped_refptr<net::IOBuffer> read_buffer_; |
128 | 122 |
129 // Set to true when a read is pending. | 123 // Set to true when a read is pending. |
130 bool read_pending_; | 124 bool read_pending_; |
(...skipping 13 matching lines...) Expand all Loading... |
144 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 138 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
145 | 139 |
146 base::WeakPtrFactory<NativeMessageProcessHost> weak_factory_; | 140 base::WeakPtrFactory<NativeMessageProcessHost> weak_factory_; |
147 | 141 |
148 DISALLOW_COPY_AND_ASSIGN(NativeMessageProcessHost); | 142 DISALLOW_COPY_AND_ASSIGN(NativeMessageProcessHost); |
149 }; | 143 }; |
150 | 144 |
151 } // namespace extensions | 145 } // namespace extensions |
152 | 146 |
153 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H
_ | 147 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H
_ |
OLD | NEW |