Chromium Code Reviews| 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_ | |
|
Aaron Boodman
2012/08/15 03:39:21
Nit: Maybe call it 'native host process' to go alo
Matt Perry
2012/08/16 00:01:36
NativeMessageProcessHost?
eaugusti
2012/08/21 23:08:35
NativeMessageProcessHost makes more sense to me.
| |
| 6 #define CHROME_BROWSER_EXTENSIONS_NATIVE_MESSAGE_PROCESS_H_ | |
| 7 | |
| 8 #include <deque> | |
| 9 | |
| 10 #include "base/file_util.h" | |
| 11 #include "base/memory/weak_ptr.h" | |
| 12 #include "base/message_loop.h" | |
| 13 #include "base/process.h" | |
| 14 | |
| 15 namespace extensions { | |
| 16 class MessageService; | |
| 17 | |
| 18 // TODO(eriq): Only posix right now. | |
|
Aaron Boodman
2012/08/15 03:39:21
I suspect that the way this will likely shake out
eaugusti
2012/08/21 23:08:35
Once I factored it out, there is actually not that
| |
| 19 // This class lives on the FILE thread. | |
| 20 // This class ensures that all pipes will be closed and the process will be | |
|
Aaron Boodman
2012/08/15 03:39:21
This class comment could use sprucing. Something a
eaugusti
2012/08/21 23:08:35
Done.
| |
| 21 // killed upon deconstruction. | |
| 22 // | |
| 23 // Lifetime Management: | |
| 24 // This class is owned by a MessageService::MessagePort which lives on the UI | |
| 25 // thread. This class however lives on the FILE thread. | |
| 26 // The MessagePort will hold a raw pointer to this class, and post a task for | |
| 27 // the deletion of this class on the FILE thread from its destructor. | |
| 28 class NativeMessageProcess : public MessageLoopForIO::Watcher { | |
| 29 public: | |
| 30 // Append any new types to the end. Changing the ordering will break native | |
| 31 // apps. | |
| 32 enum MessageType { | |
| 33 TYPE_SEND_MESSAGE_REQUEST, // Used when an extension is sending a one-off | |
| 34 // message to a native app. | |
| 35 TYPE_SEND_MESSAGE_RESPONSE, // Used by a native app to respond to a one-off | |
| 36 // message. | |
| 37 TYPE_CONNECT, // Used when an extension wants to establish a persistent | |
| 38 // connection with a native app. | |
| 39 TYPE_CONNECT_MESSAGE, // Used for messages after a connection has already | |
| 40 // been established. | |
| 41 NUM_MESSAGE_TYPES // The number of types of messages. | |
| 42 }; | |
| 43 | |
| 44 // Must be called on the FILE thread. | |
|
Aaron Boodman
2012/08/15 03:39:21
These lines are not necessary everywhere since the
eaugusti
2012/08/21 23:08:35
Done.
| |
| 45 ~NativeMessageProcess(); | |
| 46 | |
| 47 // Must be called on the FILE thread. | |
| 48 // |type|must be TYPE_CONNECT or TYPE_SEND_MESSAGE_REQUEST. | |
| 49 // |callback| will be called on the UI thread and with a NULL on error. | |
|
Aaron Boodman
2012/08/15 03:39:21
The class header can explain that all callbacks ar
eaugusti
2012/08/21 23:08:35
Done.
| |
| 50 static void Create(base::WeakPtr<MessageService> weak_service, | |
|
Aaron Boodman
2012/08/15 03:39:21
The dependency upon MessageService should be facto
eaugusti
2012/08/21 23:08:35
Done.
| |
| 51 const std::string& native_app_name, | |
| 52 const std::string& connection_message, | |
| 53 int dest_port, | |
| 54 MessageType type, | |
| 55 base::Callback<void(NativeMessageProcess*)> callback); | |
| 56 | |
| 57 // TYPE_SEND_MESSAGE_REQUEST will be sent via the connection messege in | |
|
Matt Perry
2012/08/16 00:01:36
message*
eaugusti
2012/08/21 23:08:35
Done.
| |
| 58 // NativeMessageProcess::Create, so only TYPE_CONNECT_MESSAGE is expected. | |
| 59 void Send(const std::string& json) { | |
| 60 SendImpl(TYPE_CONNECT_MESSAGE, json); | |
| 61 } | |
| 62 | |
| 63 // MessageLoopForIO::Watcher | |
| 64 virtual void OnFileCanReadWithoutBlocking(int fd); | |
|
Aaron Boodman
2012/08/15 03:39:21
These can be private.
eaugusti
2012/08/21 23:08:35
Done.
| |
| 65 virtual void OnFileCanWriteWithoutBlocking(int fd); | |
| 66 | |
| 67 private: | |
| 68 struct MessageData { | |
|
Aaron Boodman
2012/08/15 03:39:21
A little header describing what this struct repres
eaugusti
2012/08/21 23:08:35
Done.
| |
| 69 MessageData(); | |
| 70 MessageData(MessageType message_type, std::string message_data); | |
| 71 | |
| 72 MessageType type; | |
| 73 std::string data; | |
| 74 }; | |
| 75 | |
| 76 NativeMessageProcess( | |
| 77 base::WeakPtr<MessageService> weak_service, | |
| 78 int dest_port, | |
| 79 base::ProcessHandle handle, | |
| 80 int read_fd, | |
| 81 int write_fd, | |
| 82 bool is_send_message); | |
| 83 | |
| 84 void SendImpl(MessageType type, const std::string& json); | |
| 85 | |
| 86 bool WriteMessage(int fd, const MessageData& data); | |
| 87 bool ReadMessage(int fd, MessageData* data); | |
| 88 | |
| 89 base::WeakPtr<MessageService> weak_service_; | |
| 90 int dest_port_; | |
| 91 std::deque<MessageData> pending_messages_; | |
| 92 base::ProcessHandle handle_; | |
| 93 | |
| 94 MessageLoopForIO::FileDescriptorWatcher read_watcher_; | |
| 95 MessageLoopForIO::FileDescriptorWatcher write_watcher_; | |
| 96 | |
| 97 bool watching_write_; | |
| 98 | |
| 99 int read_fd_; | |
| 100 int write_fd_; | |
| 101 file_util::ScopedFD scoped_read_fd_; | |
| 102 file_util::ScopedFD scoped_write_fd_; | |
| 103 | |
| 104 // Only looking for one response. | |
| 105 bool is_send_message_; | |
| 106 | |
| 107 DISALLOW_COPY_AND_ASSIGN(NativeMessageProcess); | |
| 108 }; | |
| 109 | |
| 110 } // namespace extensions | |
| 111 | |
| 112 #endif // CHROME_BROWSER_EXTENSIONS_NATIVE_MESSAGE_PROCESS_H_ | |
| OLD | NEW |