Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: chrome/browser/extensions/api/messaging/native_message_process_host.h

Issue 10818013: Native Messaging! (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: nit Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_
7
8 #include "base/file_util.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/message_loop.h"
12 #include "base/process.h"
13 #include "content/public/browser/browser_thread.h"
14
15 namespace extensions {
16 class NativeProcessLauncher;
17
18 // Manages the native side of a connection between an extension and a native
19 // process.
20 //
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
23 // thread.
24 class NativeMessageProcessHost
25 #if defined(OS_WIN)
26 : public MessageLoopForIO::IOHandler {
27 #else
28 : public MessageLoopForIO::Watcher {
29 #endif // defined(OS_WIN)
30 public:
31 class ScopedNativeProcessClose;
32
33 #if defined(OS_WIN)
34 typedef HANDLE FileHandle;
35 typedef base::win::ScopedHandle ScopedFileHandle;
36 #else
37 typedef int FileHandle;
38 typedef file_util::ScopedFD ScopedFileHandle;
39 #endif // defined(OS_WIN)
40
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
61 // process.
62 class Client {
63 public:
64 virtual ~Client() {}
65 // Called on the UI thread.
66 virtual void PostMessageFromNativeProcess(int port_id,
67 const std::string& message) = 0;
68 virtual void CloseChannel(int port_id, bool error) = 0;
69 };
70
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();
83
84 // |type| must be TYPE_CONNECT or TYPE_SEND_MESSAGE_REQUEST. |callback| will
85 // be called with an empty ScopedHost on error.
86 static void Create(base::WeakPtr<Client> weak_client_ui,
87 const std::string& native_app_name,
88 const std::string& connection_message,
89 int destination_port,
90 MessageType type,
91 CreateCallback callback);
92
93 // Create a NativeMessageProcessHost using the specified launcher. This allows
94 // for easy testing.
95 static void CreateWithLauncher(base::WeakPtr<Client> weak_client_ui,
96 const std::string& native_app_name,
97 const std::string& connection_message,
98 int destination_port,
99 MessageType type,
100 CreateCallback callback,
101 const NativeProcessLauncher& launcher);
102
103 // TYPE_SEND_MESSAGE_REQUEST will be sent via the connection message in
104 // NativeMessageProcessHost::Create, so only TYPE_CONNECT_MESSAGE is expected.
105 void Send(const std::string& json) {
106 SendImpl(TYPE_CONNECT_MESSAGE, json);
107 }
108
109 // 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.
111 void ReadNowForTesting();
112
113 private:
114 NativeMessageProcessHost(base::WeakPtr<Client> weak_client_ui,
115 int destination_port,
116 base::ProcessHandle native_process_handle,
117 FileHandle read_fd,
118 FileHandle write_fd,
119 bool is_send_message);
120
121 // Initialize any IO watching that needs to occur between the native process.
122 void InitIO();
123
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.
128 bool WriteMessage(MessageType type, const std::string& message);
129 bool WriteData(FileHandle file, const char* data, size_t bytes_to_write);
130
131 // Read a message/data from the native process.
132 bool ReadMessage(MessageType* type, std::string* messgae);
133 bool ReadData(FileHandle file, char* data, size_t bytes_to_write);
134
135 #if defined(OS_POSIX)
136 // MessageLoopForIO::Watcher
137 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
138 // We don't need to watch for writes.
139 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {}
140
141 MessageLoopForIO::FileDescriptorWatcher read_watcher_;
142 #endif // defined(OS_POSIX)
143
144 #if defined(OS_WIN)
145 // MessageLoopForIO::IOHandler
146 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context,
147 DWORD bytes_transfered,
148 DWORD error) OVERRIDE;
149
150 MessageLoopForIO::IOContext read_context_;
151 MessageLoopForIO::IOContext write_context_;
152 #endif // defined(OS_WIN)
153
154
155 // The Client messages will be posted to. Should only be accessed from the
156 // UI thread.
157 base::WeakPtr<Client> weak_client_ui_;
158
159 // The id of the port on the other side of this connection. This is passed to
160 // |weak_client_ui_| when posting messages.
161 int destination_port_;
162
163 base::ProcessHandle native_process_handle_;
164
165 FileHandle read_file_;
166 FileHandle write_file_;
167 ScopedFileHandle scoped_read_file_;
168 ScopedFileHandle scoped_write_file_;
169
170 // Only looking for one response.
171 bool is_send_message_;
172
173 DISALLOW_COPY_AND_ASSIGN(NativeMessageProcessHost);
174 };
175
176 } // namespace extensions
177
178 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H __
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698