OLD | NEW |
---|---|
(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/weak_ptr.h" | |
10 #include "base/message_loop.h" | |
11 #include "base/process.h" | |
12 | |
13 namespace extensions { | |
14 class NativeProcessLauncher; | |
15 | |
16 // Manages the native side of a connection between an extension and a native | |
17 // process. | |
18 // | |
19 // This class must only be created, called, and deleted on the FILE thread. | |
20 // Public methods typically accept callbacks which will be invoked on the UI | |
21 // thread. | |
22 #if defined(OS_WIN) | |
23 class NativeMessageProcessHost : public MessageLoopForIO::IOHandler { | |
Matt Perry
2012/08/22 00:02:42
nit: restructure as
class Foo
#if defined(OS_WIN)
eaugusti
2012/08/31 23:47:13
Done.
| |
24 #else | |
25 class NativeMessageProcessHost : public MessageLoopForIO::Watcher { | |
26 #endif // defined(OS_WIN) | |
27 public: | |
28 #if defined(OS_WIN) | |
29 typedef HANDLE FileHandle; | |
30 typedef win::base::ScopedHandle ScopedFileHandle; | |
31 #else | |
32 typedef int FileHandle; | |
33 typedef file_util::ScopedFD ScopedFileHandle; | |
34 #endif // defined(OS_WIN) | |
35 | |
36 typedef base::Callback<void(NativeMessageProcessHost* host)> CreateCallback; | |
37 | |
38 // Append any new types to the end. Changing the ordering will break native | |
39 // apps. | |
40 enum MessageType { | |
41 TYPE_SEND_MESSAGE_REQUEST, // Used when an extension is sending a one-off | |
42 // message to a native app. | |
43 TYPE_SEND_MESSAGE_RESPONSE, // Used by a native app to respond to a one-off | |
44 // message. | |
45 TYPE_CONNECT, // Used when an extension wants to establish a persistent | |
46 // connection with a native app. | |
47 TYPE_CONNECT_MESSAGE, // Used for messages after a connection has already | |
48 // been established. | |
49 NUM_MESSAGE_TYPES // The number of types of messages. | |
50 }; | |
51 | |
52 // Interface for classes that which to recieve messages from the native | |
53 // process. | |
54 class Client { | |
Matt Perry
2012/08/22 00:02:42
Add a comment that this can only be accessed on th
eaugusti
2012/08/31 23:47:13
Done.
| |
55 public: | |
56 virtual ~Client() {} | |
57 virtual void PostMessageFromNativeProcess(int port_id, | |
58 const std::string& message) = 0; | |
59 }; | |
60 | |
61 ~NativeMessageProcessHost(); | |
62 | |
63 // |type|must be TYPE_CONNECT or TYPE_SEND_MESSAGE_REQUEST. | |
Matt Perry
2012/08/22 00:02:42
space after |
eaugusti
2012/08/31 23:47:13
Done.
| |
64 // |callback| will be called with a NULL on error. | |
65 static void Create(base::WeakPtr<Client> weak_client, | |
66 const std::string& native_app_name, | |
67 const std::string& connection_message, | |
68 int destination_port, | |
69 MessageType type, | |
70 CreateCallback callback); | |
71 | |
72 // Create a NativeMessageProcessHost using the specified launcher. This allows | |
73 // for easy testing. | |
74 static void CreateWithLauncher(base::WeakPtr<Client> weak_client, | |
75 const std::string& native_app_name, | |
76 const std::string& connection_message, | |
77 int destination_port, | |
78 MessageType type, | |
79 CreateCallback callback, | |
80 const NativeProcessLauncher& launcher); | |
81 | |
82 // TYPE_SEND_MESSAGE_REQUEST will be sent via the connection message in | |
83 // NativeMessageProcessHost::Create, so only TYPE_CONNECT_MESSAGE is expected. | |
84 void Send(const std::string& json) { | |
85 SendImpl(TYPE_CONNECT_MESSAGE, json); | |
86 } | |
87 | |
88 // Try and read a message from |read_file_|. This should only be called in | |
89 // unittests when you know there is data in the file. | |
90 void ReadNowForTesting(); | |
91 | |
92 private: | |
93 NativeMessageProcessHost(base::WeakPtr<Client> weak_client, | |
94 int destination_port, | |
95 base::ProcessHandle native_process_handle, | |
96 FileHandle read_fd, | |
97 FileHandle write_fd, | |
98 bool is_send_message); | |
99 | |
100 // Initialize any IO watching that needs to occur between the native process. | |
101 void InitIO(); | |
102 | |
103 void SendImpl(MessageType type, const std::string& json); | |
Matt Perry
2012/08/22 00:02:42
add some method comments for these.
eaugusti
2012/08/31 23:47:13
Done.
| |
104 | |
105 bool WriteMessage(MessageType type, const std::string& message); | |
106 bool ReadMessage(MessageType* type, std::string* messgae); | |
107 | |
108 bool ReadData(FileHandle file, char* data, size_t bytes_to_write); | |
109 bool WriteData(FileHandle file, const char* data, size_t bytes_to_write); | |
110 | |
111 #if defined(OS_POSIX) | |
112 // MessageLoopForIO::Watcher | |
113 virtual void OnFileCanReadWithoutBlocking(int fd); | |
114 // We don't need to watch for writes. | |
115 virtual void OnFileCanWriteWithoutBlocking(int fd) {} | |
116 | |
117 MessageLoopForIO::FileDescriptorWatcher read_watcher_; | |
118 #endif // defined(OS_POSIX) | |
119 | |
120 base::WeakPtr<Client> weak_client_; | |
Matt Perry
2012/08/22 00:02:42
This can only be dereferenced on the UI thread. Pl
eaugusti
2012/08/31 23:47:13
... or both!
| |
121 int destination_port_; | |
Matt Perry
2012/08/22 00:02:42
comment this guy
eaugusti
2012/08/31 23:47:13
Done.
| |
122 base::ProcessHandle native_process_handle_; | |
123 | |
124 FileHandle read_file_; | |
125 FileHandle write_file_; | |
126 ScopedFileHandle scoped_read_file_; | |
127 ScopedFileHandle scoped_write_file_; | |
128 | |
129 // Only looking for one response. | |
130 bool is_send_message_; | |
131 | |
132 DISALLOW_COPY_AND_ASSIGN(NativeMessageProcessHost); | |
133 }; | |
134 | |
135 } // namespace extensions | |
136 | |
137 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H __ | |
OLD | NEW |