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 "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 // This class must only be created, called, and deleted on the FILE thread. | 21 // This class must only be created, called, and deleted on the FILE thread. |
22 // Public methods typically accept callbacks which will be invoked on the UI | 22 // Public methods typically accept callbacks which will be invoked on the UI |
23 // thread. | 23 // thread. |
24 class NativeMessageProcessHost | 24 class NativeMessageProcessHost |
25 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
26 : public MessageLoopForIO::IOHandler { | 26 : public MessageLoopForIO::IOHandler { |
27 #else | 27 #else |
28 : public MessageLoopForIO::Watcher { | 28 : public MessageLoopForIO::Watcher { |
29 #endif // defined(OS_WIN) | 29 #endif // defined(OS_WIN) |
30 public: | 30 public: |
31 class ScopedNativeProcessClose; | |
32 | |
33 #if defined(OS_WIN) | 31 #if defined(OS_WIN) |
34 typedef HANDLE FileHandle; | 32 typedef HANDLE FileHandle; |
35 typedef base::win::ScopedHandle ScopedFileHandle; | 33 typedef base::win::ScopedHandle ScopedFileHandle; |
36 #else | 34 #else |
37 typedef int FileHandle; | 35 typedef int FileHandle; |
38 typedef file_util::ScopedFD ScopedFileHandle; | 36 typedef file_util::ScopedFD ScopedFileHandle; |
39 #endif // defined(OS_WIN) | 37 #endif // defined(OS_WIN) |
40 | 38 |
41 typedef scoped_ptr_malloc<NativeMessageProcessHost, ScopedNativeProcessClose> | |
42 ScopedHost; | |
43 | |
44 typedef base::Callback<void(ScopedHost host)> CreateCallback; | |
45 | |
46 // Append any new types to the end. Changing the ordering will break native | |
47 // apps. | |
48 enum MessageType { | |
49 TYPE_SEND_MESSAGE_REQUEST, // Used when an extension is sending a one-off | |
50 // message to a native app. | |
51 TYPE_SEND_MESSAGE_RESPONSE, // Used by a native app to respond to a one-off | |
52 // message. | |
53 TYPE_CONNECT, // Used when an extension wants to establish a persistent | |
54 // connection with a native app. | |
55 TYPE_CONNECT_MESSAGE, // Used for messages after a connection has already | |
56 // been established. | |
57 NUM_MESSAGE_TYPES // The number of types of messages. | |
58 }; | |
59 | |
60 // Interface for classes that which to recieve messages from the native | 39 // Interface for classes that which to recieve messages from the native |
61 // process. | 40 // process. |
62 class Client { | 41 class Client { |
63 public: | 42 public: |
64 virtual ~Client() {} | 43 virtual ~Client() {} |
65 // Called on the UI thread. | 44 // Called on the UI thread. |
66 virtual void PostMessageFromNativeProcess(int port_id, | 45 virtual void PostMessageFromNativeProcess(int port_id, |
67 const std::string& message) = 0; | 46 const std::string& message) = 0; |
68 virtual void CloseChannel(int port_id, bool error) = 0; | 47 virtual void CloseChannel(int port_id, bool error) = 0; |
69 }; | 48 }; |
70 | 49 |
71 // Desctruction functor that ensures a NativeMessageProcessHost is destroyed | |
72 // on the FILE thread. | |
73 class ScopedNativeProcessClose { | |
74 public: | |
75 inline void operator()(extensions::NativeMessageProcessHost* x) const { | |
76 content::BrowserThread::DeleteSoon(content::BrowserThread::FILE, | |
77 FROM_HERE, x); | |
78 } | |
79 }; | |
80 | |
81 | |
82 virtual ~NativeMessageProcessHost(); | 50 virtual ~NativeMessageProcessHost(); |
83 | 51 |
84 // |type| must be TYPE_CONNECT or TYPE_SEND_MESSAGE_REQUEST. |callback| will | 52 static scoped_ptr<NativeMessageProcessHost> Create( |
85 // be called with an empty ScopedHost on error. | 53 base::WeakPtr<Client> weak_client_ui, |
86 static void Create(base::WeakPtr<Client> weak_client_ui, | 54 const std::string& native_host_name, |
87 const std::string& native_app_name, | 55 int destination_port); |
88 const std::string& connection_message, | |
89 int destination_port, | |
90 MessageType type, | |
91 CreateCallback callback); | |
92 | 56 |
93 // Create a NativeMessageProcessHost using the specified launcher. This allows | 57 // Create using specified |launcher|. Used in tests. |
94 // for easy testing. | 58 static scoped_ptr<NativeMessageProcessHost> CreateWithLauncher( |
95 static void CreateWithLauncher(base::WeakPtr<Client> weak_client_ui, | 59 base::WeakPtr<Client> weak_client_ui, |
96 const std::string& native_app_name, | 60 const std::string& native_host_name, |
97 const std::string& connection_message, | 61 int destination_port, |
98 int destination_port, | 62 scoped_ptr<NativeProcessLauncher> launcher); |
99 MessageType type, | |
100 CreateCallback callback, | |
101 const NativeProcessLauncher& launcher); | |
102 | 63 |
103 // TYPE_SEND_MESSAGE_REQUEST will be sent via the connection message in | 64 // Send a message with the specified payload. |
104 // NativeMessageProcessHost::Create, so only TYPE_CONNECT_MESSAGE is expected. | 65 void Send(const std::string& json); |
105 void Send(const std::string& json) { | |
106 SendImpl(TYPE_CONNECT_MESSAGE, json); | |
107 } | |
108 | 66 |
109 // Try and read a single message from |read_file_|. This should only be called | 67 // Try and read a single message from |read_file_|. This should only be called |
110 // in unittests when you know there is data in the file. | 68 // in unittests when you know there is data in the file. |
111 void ReadNowForTesting(); | 69 void ReadNowForTesting(); |
112 | 70 |
113 private: | 71 private: |
114 NativeMessageProcessHost(base::WeakPtr<Client> weak_client_ui, | 72 NativeMessageProcessHost(base::WeakPtr<Client> weak_client_ui, |
| 73 const std::string& native_host_name, |
115 int destination_port, | 74 int destination_port, |
116 base::ProcessHandle native_process_handle, | 75 scoped_ptr<NativeProcessLauncher> launcher); |
117 FileHandle read_fd, | 76 |
118 FileHandle write_fd, | 77 // Starts the host process. |
119 bool is_send_message); | 78 void LaunchHostProcess(scoped_ptr<NativeProcessLauncher> launcher); |
120 | 79 |
121 // Initialize any IO watching that needs to occur between the native process. | 80 // Initialize any IO watching that needs to occur between the native process. |
122 void InitIO(); | 81 void InitIO(); |
123 | 82 |
124 // Send a message to the native process with the specified type and payload. | |
125 void SendImpl(MessageType type, const std::string& json); | |
126 | |
127 // Write a message/data to the native process. | 83 // Write a message/data to the native process. |
128 bool WriteMessage(MessageType type, const std::string& message); | 84 bool WriteMessage(const std::string& message); |
129 bool WriteData(FileHandle file, const char* data, size_t bytes_to_write); | 85 bool WriteData(FileHandle file, const char* data, size_t bytes_to_write); |
130 | 86 |
131 // Read a message/data from the native process. | 87 // Read a message/data from the native process. |
132 bool ReadMessage(MessageType* type, std::string* messgae); | 88 bool ReadMessage(std::string* message); |
133 bool ReadData(FileHandle file, char* data, size_t bytes_to_write); | 89 bool ReadData(FileHandle file, char* data, size_t bytes_to_write); |
134 | 90 |
135 #if defined(OS_POSIX) | 91 #if defined(OS_POSIX) |
136 // MessageLoopForIO::Watcher | 92 // MessageLoopForIO::Watcher |
137 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; | 93 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |
138 // We don't need to watch for writes. | 94 // We don't need to watch for writes. |
139 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {} | 95 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {} |
140 | 96 |
141 MessageLoopForIO::FileDescriptorWatcher read_watcher_; | 97 MessageLoopForIO::FileDescriptorWatcher read_watcher_; |
142 #endif // defined(OS_POSIX) | 98 #endif // defined(OS_POSIX) |
143 | 99 |
144 #if defined(OS_WIN) | 100 #if defined(OS_WIN) |
145 // MessageLoopForIO::IOHandler | 101 // MessageLoopForIO::IOHandler |
146 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, | 102 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, |
147 DWORD bytes_transfered, | 103 DWORD bytes_transfered, |
148 DWORD error) OVERRIDE; | 104 DWORD error) OVERRIDE; |
149 | 105 |
150 MessageLoopForIO::IOContext read_context_; | 106 MessageLoopForIO::IOContext read_context_; |
151 MessageLoopForIO::IOContext write_context_; | 107 MessageLoopForIO::IOContext write_context_; |
152 #endif // defined(OS_WIN) | 108 #endif // defined(OS_WIN) |
153 | 109 |
154 | 110 |
155 // The Client messages will be posted to. Should only be accessed from the | 111 // The Client messages will be posted to. Should only be accessed from the |
156 // UI thread. | 112 // UI thread. |
157 base::WeakPtr<Client> weak_client_ui_; | 113 base::WeakPtr<Client> weak_client_ui_; |
158 | 114 |
| 115 // Name of the native messaging host. |
| 116 std::string native_host_name_; |
| 117 |
159 // The id of the port on the other side of this connection. This is passed to | 118 // The id of the port on the other side of this connection. This is passed to |
160 // |weak_client_ui_| when posting messages. | 119 // |weak_client_ui_| when posting messages. |
161 int destination_port_; | 120 int destination_port_; |
162 | 121 |
163 base::ProcessHandle native_process_handle_; | 122 base::ProcessHandle native_process_handle_; |
164 | 123 |
165 FileHandle read_file_; | 124 FileHandle read_file_; |
166 FileHandle write_file_; | 125 FileHandle write_file_; |
167 ScopedFileHandle scoped_read_file_; | 126 ScopedFileHandle scoped_read_file_; |
168 ScopedFileHandle scoped_write_file_; | 127 ScopedFileHandle scoped_write_file_; |
169 | 128 |
170 // Only looking for one response. | |
171 bool is_send_message_; | |
172 | |
173 DISALLOW_COPY_AND_ASSIGN(NativeMessageProcessHost); | 129 DISALLOW_COPY_AND_ASSIGN(NativeMessageProcessHost); |
174 }; | 130 }; |
175 | 131 |
176 } // namespace extensions | 132 } // namespace extensions |
177 | 133 |
178 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H
__ | 134 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H
__ |
OLD | NEW |