Chromium Code Reviews| Index: chrome/browser/extensions/api/messaging/native_message_process_host.h |
| diff --git a/chrome/browser/extensions/api/messaging/native_message_process_host.h b/chrome/browser/extensions/api/messaging/native_message_process_host.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6aef58a8d0b9630c3e8941097452e5561598f406 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/messaging/native_message_process_host.h |
| @@ -0,0 +1,137 @@ |
| +// 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_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_ |
| +#define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_ |
| + |
| +#include "base/file_util.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/message_loop.h" |
| +#include "base/process.h" |
| + |
| +namespace extensions { |
| +class NativeProcessLauncher; |
| + |
| +// Manages the native side of a connection between an extension and a native |
| +// process. |
| +// |
| +// This class must only be created, called, and deleted on the FILE thread. |
| +// Public methods typically accept callbacks which will be invoked on the UI |
| +// thread. |
| +#if defined(OS_WIN) |
| +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.
|
| +#else |
| +class NativeMessageProcessHost : public MessageLoopForIO::Watcher { |
| +#endif // defined(OS_WIN) |
| + public: |
| +#if defined(OS_WIN) |
| + typedef HANDLE FileHandle; |
| + typedef win::base::ScopedHandle ScopedFileHandle; |
| +#else |
| + typedef int FileHandle; |
| + typedef file_util::ScopedFD ScopedFileHandle; |
| +#endif // defined(OS_WIN) |
| + |
| + typedef base::Callback<void(NativeMessageProcessHost* host)> CreateCallback; |
| + |
| + // 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. |
| + 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 |
| + // been established. |
| + NUM_MESSAGE_TYPES // The number of types of messages. |
| + }; |
| + |
| + // Interface for classes that which to recieve messages from the native |
| + // process. |
| + 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.
|
| + public: |
| + virtual ~Client() {} |
| + virtual void PostMessageFromNativeProcess(int port_id, |
| + const std::string& message) = 0; |
| + }; |
| + |
| + ~NativeMessageProcessHost(); |
| + |
| + // |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.
|
| + // |callback| will be called with a NULL on error. |
| + static void Create(base::WeakPtr<Client> weak_client, |
| + const std::string& native_app_name, |
| + const std::string& connection_message, |
| + int destination_port, |
| + MessageType type, |
| + CreateCallback callback); |
| + |
| + // Create a NativeMessageProcessHost using the specified launcher. This allows |
| + // for easy testing. |
| + static void CreateWithLauncher(base::WeakPtr<Client> weak_client, |
| + const std::string& native_app_name, |
| + const std::string& connection_message, |
| + int destination_port, |
| + MessageType type, |
| + CreateCallback callback, |
| + const NativeProcessLauncher& launcher); |
| + |
| + // TYPE_SEND_MESSAGE_REQUEST will be sent via the connection message in |
| + // NativeMessageProcessHost::Create, so only TYPE_CONNECT_MESSAGE is expected. |
| + void Send(const std::string& json) { |
| + SendImpl(TYPE_CONNECT_MESSAGE, json); |
| + } |
| + |
| + // Try and read a message from |read_file_|. This should only be called in |
| + // unittests when you know there is data in the file. |
| + void ReadNowForTesting(); |
| + |
| + private: |
| + NativeMessageProcessHost(base::WeakPtr<Client> weak_client, |
| + int destination_port, |
| + base::ProcessHandle native_process_handle, |
| + FileHandle read_fd, |
| + FileHandle write_fd, |
| + bool is_send_message); |
| + |
| + // Initialize any IO watching that needs to occur between the native process. |
| + void InitIO(); |
| + |
| + 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.
|
| + |
| + bool WriteMessage(MessageType type, const std::string& message); |
| + bool ReadMessage(MessageType* type, std::string* messgae); |
| + |
| + bool ReadData(FileHandle file, char* data, size_t bytes_to_write); |
| + bool WriteData(FileHandle file, const char* data, size_t bytes_to_write); |
| + |
| +#if defined(OS_POSIX) |
| + // MessageLoopForIO::Watcher |
| + virtual void OnFileCanReadWithoutBlocking(int fd); |
| + // We don't need to watch for writes. |
| + virtual void OnFileCanWriteWithoutBlocking(int fd) {} |
| + |
| + MessageLoopForIO::FileDescriptorWatcher read_watcher_; |
| +#endif // defined(OS_POSIX) |
| + |
| + 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!
|
| + int destination_port_; |
|
Matt Perry
2012/08/22 00:02:42
comment this guy
eaugusti
2012/08/31 23:47:13
Done.
|
| + base::ProcessHandle native_process_handle_; |
| + |
| + FileHandle read_file_; |
| + FileHandle write_file_; |
| + ScopedFileHandle scoped_read_file_; |
| + ScopedFileHandle scoped_write_file_; |
| + |
| + // Only looking for one response. |
| + bool is_send_message_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(NativeMessageProcessHost); |
| +}; |
| + |
| +} // namespace extensions |
| + |
| +#endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H__ |