| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_ |
| 7 | 7 |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 #else | 36 #else |
| 37 typedef int FileHandle; | 37 typedef int FileHandle; |
| 38 typedef file_util::ScopedFD ScopedFileHandle; | 38 typedef file_util::ScopedFD ScopedFileHandle; |
| 39 #endif // defined(OS_WIN) | 39 #endif // defined(OS_WIN) |
| 40 | 40 |
| 41 typedef scoped_ptr_malloc<NativeMessageProcessHost, ScopedNativeProcessClose> | 41 typedef scoped_ptr_malloc<NativeMessageProcessHost, ScopedNativeProcessClose> |
| 42 ScopedHost; | 42 ScopedHost; |
| 43 | 43 |
| 44 typedef base::Callback<void(ScopedHost host)> CreateCallback; | 44 typedef base::Callback<void(ScopedHost host)> CreateCallback; |
| 45 | 45 |
| 46 // Append any new types to the end. Changing the ordering will break native | |
| 47 // apps. | |
| 48 enum MessageType { | |
| 49 TYPE_SEND_MESSAGE_REQUEST, // Used when an extension is sending a one-off | |
| 50 // message to a native app. | |
| 51 TYPE_SEND_MESSAGE_RESPONSE, // Used by a native app to respond to a one-off | |
| 52 // message. | |
| 53 TYPE_CONNECT, // Used when an extension wants to establish a persistent | |
| 54 // connection with a native app. | |
| 55 TYPE_CONNECT_MESSAGE, // Used for messages after a connection has already | |
| 56 // been established. | |
| 57 NUM_MESSAGE_TYPES // The number of types of messages. | |
| 58 }; | |
| 59 | |
| 60 // Interface for classes that which to recieve messages from the native | 46 // Interface for classes that which to recieve messages from the native |
| 61 // process. | 47 // process. |
| 62 class Client { | 48 class Client { |
| 63 public: | 49 public: |
| 64 virtual ~Client() {} | 50 virtual ~Client() {} |
| 65 // Called on the UI thread. | 51 // Called on the UI thread. |
| 66 virtual void PostMessageFromNativeProcess(int port_id, | 52 virtual void PostMessageFromNativeProcess(int port_id, |
| 67 const std::string& message) = 0; | 53 const std::string& message) = 0; |
| 68 virtual void CloseChannel(int port_id, bool error) = 0; | 54 virtual void CloseChannel(int port_id, bool error) = 0; |
| 69 }; | 55 }; |
| 70 | 56 |
| 71 // Desctruction functor that ensures a NativeMessageProcessHost is destroyed | 57 // Desctruction functor that ensures a NativeMessageProcessHost is destroyed |
| 72 // on the FILE thread. | 58 // on the FILE thread. |
| 73 class ScopedNativeProcessClose { | 59 class ScopedNativeProcessClose { |
| 74 public: | 60 public: |
| 75 inline void operator()(extensions::NativeMessageProcessHost* x) const { | 61 inline void operator()(extensions::NativeMessageProcessHost* x) const { |
| 76 content::BrowserThread::DeleteSoon(content::BrowserThread::FILE, | 62 content::BrowserThread::DeleteSoon(content::BrowserThread::FILE, |
| 77 FROM_HERE, x); | 63 FROM_HERE, x); |
| 78 } | 64 } |
| 79 }; | 65 }; |
| 80 | 66 |
| 81 | 67 |
| 82 virtual ~NativeMessageProcessHost(); | 68 virtual ~NativeMessageProcessHost(); |
| 83 | 69 |
| 84 // |type| must be TYPE_CONNECT or TYPE_SEND_MESSAGE_REQUEST. |callback| will | 70 // |callback| will be called with an empty ScopedHost on error. |
| 85 // be called with an empty ScopedHost on error. | |
| 86 static void Create(base::WeakPtr<Client> weak_client_ui, | 71 static void Create(base::WeakPtr<Client> weak_client_ui, |
| 87 const std::string& native_app_name, | 72 const std::string& native_app_name, |
| 88 const std::string& connection_message, | |
| 89 int destination_port, | 73 int destination_port, |
| 90 MessageType type, | |
| 91 CreateCallback callback); | 74 CreateCallback callback); |
| 92 | 75 |
| 93 // Create a NativeMessageProcessHost using the specified launcher. This allows | 76 // Create a NativeMessageProcessHost using the specified launcher. This allows |
| 94 // for easy testing. | 77 // for easy testing. |
| 95 static void CreateWithLauncher(base::WeakPtr<Client> weak_client_ui, | 78 static void CreateWithLauncher(base::WeakPtr<Client> weak_client_ui, |
| 96 const std::string& native_app_name, | 79 const std::string& native_app_name, |
| 97 const std::string& connection_message, | |
| 98 int destination_port, | 80 int destination_port, |
| 99 MessageType type, | |
| 100 CreateCallback callback, | 81 CreateCallback callback, |
| 101 const NativeProcessLauncher& launcher); | 82 const NativeProcessLauncher& launcher); |
| 102 | 83 |
| 103 // TYPE_SEND_MESSAGE_REQUEST will be sent via the connection message in | 84 // Send a message with the specified payload. |
| 104 // NativeMessageProcessHost::Create, so only TYPE_CONNECT_MESSAGE is expected. | 85 void Send(const std::string& json); |
| 105 void Send(const std::string& json) { | |
| 106 SendImpl(TYPE_CONNECT_MESSAGE, json); | |
| 107 } | |
| 108 | 86 |
| 109 // Try and read a single message from |read_file_|. This should only be called | 87 // Try and read a single message from |read_file_|. This should only be called |
| 110 // in unittests when you know there is data in the file. | 88 // in unittests when you know there is data in the file. |
| 111 void ReadNowForTesting(); | 89 void ReadNowForTesting(); |
| 112 | 90 |
| 113 private: | 91 private: |
| 114 NativeMessageProcessHost(base::WeakPtr<Client> weak_client_ui, | 92 NativeMessageProcessHost(base::WeakPtr<Client> weak_client_ui, |
| 115 int destination_port, | 93 int destination_port, |
| 116 base::ProcessHandle native_process_handle, | 94 base::ProcessHandle native_process_handle, |
| 117 FileHandle read_fd, | 95 FileHandle read_fd, |
| 118 FileHandle write_fd, | 96 FileHandle write_fd); |
| 119 bool is_send_message); | |
| 120 | 97 |
| 121 // Initialize any IO watching that needs to occur between the native process. | 98 // Initialize any IO watching that needs to occur between the native process. |
| 122 void InitIO(); | 99 void InitIO(); |
| 123 | 100 |
| 124 // Send a message to the native process with the specified type and payload. | |
| 125 void SendImpl(MessageType type, const std::string& json); | |
| 126 | |
| 127 // Write a message/data to the native process. | 101 // Write a message/data to the native process. |
| 128 bool WriteMessage(MessageType type, const std::string& message); | 102 bool WriteMessage(const std::string& message); |
| 129 bool WriteData(FileHandle file, const char* data, size_t bytes_to_write); | 103 bool WriteData(FileHandle file, const char* data, size_t bytes_to_write); |
| 130 | 104 |
| 131 // Read a message/data from the native process. | 105 // Read a message/data from the native process. |
| 132 bool ReadMessage(MessageType* type, std::string* messgae); | 106 bool ReadMessage(std::string* message); |
| 133 bool ReadData(FileHandle file, char* data, size_t bytes_to_write); | 107 bool ReadData(FileHandle file, char* data, size_t bytes_to_write); |
| 134 | 108 |
| 135 #if defined(OS_POSIX) | 109 #if defined(OS_POSIX) |
| 136 // MessageLoopForIO::Watcher | 110 // MessageLoopForIO::Watcher |
| 137 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; | 111 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |
| 138 // We don't need to watch for writes. | 112 // We don't need to watch for writes. |
| 139 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {} | 113 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {} |
| 140 | 114 |
| 141 MessageLoopForIO::FileDescriptorWatcher read_watcher_; | 115 MessageLoopForIO::FileDescriptorWatcher read_watcher_; |
| 142 #endif // defined(OS_POSIX) | 116 #endif // defined(OS_POSIX) |
| (...skipping 26 matching lines...) Expand all Loading... |
| 169 | 143 |
| 170 // Only looking for one response. | 144 // Only looking for one response. |
| 171 bool is_send_message_; | 145 bool is_send_message_; |
| 172 | 146 |
| 173 DISALLOW_COPY_AND_ASSIGN(NativeMessageProcessHost); | 147 DISALLOW_COPY_AND_ASSIGN(NativeMessageProcessHost); |
| 174 }; | 148 }; |
| 175 | 149 |
| 176 } // namespace extensions | 150 } // namespace extensions |
| 177 | 151 |
| 178 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H
__ | 152 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H
__ |
| OLD | NEW |