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_NATIVE_MESSAGE_PROCESS_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_NATIVE_MESSAGE_PROCESS_H_ | |
7 | |
8 #include <deque> | |
9 | |
10 #include "base/file_util.h" | |
11 #include "base/message_loop.h" | |
12 #include "base/process.h" | |
13 | |
14 namespace extensions { | |
15 class MessageService; | |
16 | |
17 // TODO(eriq): Only posix right now. | |
18 // This class ensures that all pipes will be closed and the process will be | |
19 // killed upon deconstruction. | |
20 class NativeMessageProcess : public MessageLoopForIO::Watcher { | |
21 public: | |
22 // Append any new types to the end. Changing the ordering will break native | |
23 // apps. | |
24 enum MessageType { | |
25 TYPE_SEND_MESSAGE_REQUEST, // Used when an extension is sending a one-off | |
26 // message to a native app. | |
27 TYPE_SEND_MESSAGE_RESPONSE, // Used by a native app to respond to a one-off | |
28 // message. | |
Matt Perry
2012/08/09 02:13:00
indenting is off here
eaugusti
2012/08/13 23:22:34
Done.
| |
29 TYPE_CONNECT, // Used when an extension wants to establish a persistent | |
30 // connection with a native app. | |
31 TYPE_CONNECT_MESSAGE, // Used for messages after a connection has already | |
Matt Perry
2012/08/09 02:13:00
Do we need to expose the difference between connec
Aaron Boodman
2012/08/09 18:36:12
We could do that. I was just mirroring the API we
eaugusti
2012/08/13 23:22:34
I think the app knowing the difference between sen
| |
32 // been established. | |
33 NUM_MESSAGE_TYPES // The number of types of messages. | |
34 }; | |
35 | |
36 ~NativeMessageProcess(); | |
37 | |
38 // Must be called on the FILE thread. | |
Matt Perry
2012/08/09 02:13:00
Is there a reason you chose the FILE thread (say,
eaugusti
2012/08/13 23:22:34
On Posix, the FILE and IO threads both have an IO
| |
39 // |type|must be TYPE_CONNECT or TYPE_SEND_MESSAGE_REQUEST. | |
40 static NativeMessageProcess* Create(const std::string& native_app_name, | |
41 const std::string& connection_message, | |
Matt Perry
2012/08/09 02:13:00
indent should align with first param after (
eaugusti
2012/08/13 23:22:34
Done.
| |
42 MessageService* service, | |
43 int dest_port, | |
44 MessageType type); | |
45 | |
46 // TYPE_SEND_MESSAGE_REQUEST will be sent via the connection messege in | |
47 // NativeMessageProcess::Create, so only TYPE_CONNECT_MESSAGE is expected. | |
48 void Send(const std::string& json) { | |
49 Send(TYPE_CONNECT_MESSAGE, json); | |
50 } | |
51 | |
52 // MessageLoopForIO::Watcher | |
53 virtual void OnFileCanReadWithoutBlocking(int fd); | |
54 virtual void OnFileCanWriteWithoutBlocking(int fd); | |
55 | |
56 private: | |
57 struct MessageData { | |
58 MessageData(); | |
59 MessageData(MessageType message_type, std::string message_data); | |
60 | |
61 MessageType type; | |
62 std::string data; | |
63 }; | |
64 | |
65 // Ensures the termination of |handle|. Must be called on the FILE thread. | |
66 static void KillProcess(base::ProcessHandle handle); | |
67 | |
68 NativeMessageProcess( | |
69 MessageService* service, | |
Matt Perry
2012/08/09 02:13:00
indent to 4 from the N
see http://google-stylegui
eaugusti
2012/08/13 23:22:34
Done.
| |
70 int dest_port, | |
71 base::ProcessHandle handle, | |
72 int read_fd, | |
73 int write_fd, | |
74 bool is_send_message); | |
75 | |
76 void Send(MessageType type, const std::string& json); | |
77 | |
78 // Called on the FILE thread to start watching |write_fd_|. | |
79 void WatchWrite(); | |
80 | |
81 bool WriteMessage(int fd, const MessageData& data); | |
82 bool ReadMessage(int fd, MessageData* data); | |
83 | |
84 MessageService* service_; | |
85 int dest_port_; | |
86 std::deque<MessageData> pending_messages_; | |
87 base::ProcessHandle handle_; | |
88 | |
89 MessageLoopForIO::FileDescriptorWatcher read_watcher_; | |
90 MessageLoopForIO::FileDescriptorWatcher write_watcher_; | |
91 | |
92 bool watching_write_; | |
93 | |
94 int read_fd_; | |
95 int write_fd_; | |
96 file_util::ScopedFD scoped_read_fd_; | |
97 file_util::ScopedFD scoped_write_fd_; | |
98 | |
99 // Only looking for one response. | |
100 bool is_send_message_; | |
101 | |
102 DISALLOW_COPY_AND_ASSIGN(NativeMessageProcess); | |
103 }; | |
104 | |
105 } // namespace extensions | |
106 | |
107 #endif // CHROME_BROWSER_EXTENSIONS_NATIVE_MESSAGE_PROCESS_H_ | |
OLD | NEW |