Chromium Code Reviews| Index: chrome/browser/extensions/native_message_process.h |
| diff --git a/chrome/browser/extensions/native_message_process.h b/chrome/browser/extensions/native_message_process.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b9a34f0e7edc3aa724ee4afcd6d8ad948c26ad34 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/native_message_process.h |
| @@ -0,0 +1,107 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_BROWSER_EXTENSIONS_NATIVE_MESSAGE_PROCESS_H_ |
| +#define CHROME_BROWSER_EXTENSIONS_NATIVE_MESSAGE_PROCESS_H_ |
| + |
| +#include <deque> |
| + |
| +#include "base/file_util.h" |
| +#include "base/message_loop.h" |
| +#include "base/process.h" |
| + |
| +namespace extensions { |
| +class MessageService; |
| + |
| +// TODO(eriq): Only posix right now. |
| +// This class ensures that all pipes will be closed and the process will be |
| +// killed upon deconstruction. |
| +class NativeMessageProcess : public MessageLoopForIO::Watcher { |
| + public: |
| + // Append any new types to the end. Changing the ordering will break native |
| + // apps. |
| + enum MessageType { |
| + TYPE_SEND_MESSAGE_REQUEST, // Used when an extension is sending a one-off |
| + // message to a native app. |
| + TYPE_SEND_MESSAGE_RESPONSE, // Used by a native app to respond to a one-off |
| + // message. |
|
Matt Perry
2012/08/09 02:13:00
indenting is off here
eaugusti
2012/08/13 23:22:34
Done.
|
| + TYPE_CONNECT, // Used when an extension wants to establish a persistent |
| + // connection with a native app. |
| + 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
|
| + // been established. |
| + NUM_MESSAGE_TYPES // The number of types of messages. |
| + }; |
| + |
| + ~NativeMessageProcess(); |
| + |
| + // 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
|
| + // |type|must be TYPE_CONNECT or TYPE_SEND_MESSAGE_REQUEST. |
| + static NativeMessageProcess* Create(const std::string& native_app_name, |
| + 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.
|
| + MessageService* service, |
| + int dest_port, |
| + MessageType type); |
| + |
| + // TYPE_SEND_MESSAGE_REQUEST will be sent via the connection messege in |
| + // NativeMessageProcess::Create, so only TYPE_CONNECT_MESSAGE is expected. |
| + void Send(const std::string& json) { |
| + Send(TYPE_CONNECT_MESSAGE, json); |
| + } |
| + |
| + // MessageLoopForIO::Watcher |
| + virtual void OnFileCanReadWithoutBlocking(int fd); |
| + virtual void OnFileCanWriteWithoutBlocking(int fd); |
| + |
| + private: |
| + struct MessageData { |
| + MessageData(); |
| + MessageData(MessageType message_type, std::string message_data); |
| + |
| + MessageType type; |
| + std::string data; |
| + }; |
| + |
| + // Ensures the termination of |handle|. Must be called on the FILE thread. |
| + static void KillProcess(base::ProcessHandle handle); |
| + |
| + NativeMessageProcess( |
| + 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.
|
| + int dest_port, |
| + base::ProcessHandle handle, |
| + int read_fd, |
| + int write_fd, |
| + bool is_send_message); |
| + |
| + void Send(MessageType type, const std::string& json); |
| + |
| + // Called on the FILE thread to start watching |write_fd_|. |
| + void WatchWrite(); |
| + |
| + bool WriteMessage(int fd, const MessageData& data); |
| + bool ReadMessage(int fd, MessageData* data); |
| + |
| + MessageService* service_; |
| + int dest_port_; |
| + std::deque<MessageData> pending_messages_; |
| + base::ProcessHandle handle_; |
| + |
| + MessageLoopForIO::FileDescriptorWatcher read_watcher_; |
| + MessageLoopForIO::FileDescriptorWatcher write_watcher_; |
| + |
| + bool watching_write_; |
| + |
| + int read_fd_; |
| + int write_fd_; |
| + file_util::ScopedFD scoped_read_fd_; |
| + file_util::ScopedFD scoped_write_fd_; |
| + |
| + // Only looking for one response. |
| + bool is_send_message_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(NativeMessageProcess); |
| +}; |
| + |
| +} // namespace extensions |
| + |
| +#endif // CHROME_BROWSER_EXTENSIONS_NATIVE_MESSAGE_PROCESS_H_ |