Index: chrome/browser/extensions/api/messaging/native_message_process_host.cc |
diff --git a/chrome/browser/extensions/api/messaging/native_message_process_host.cc b/chrome/browser/extensions/api/messaging/native_message_process_host.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..93e99b99f2f4fb8b06b1e25418b5fb2d4c33d76a |
--- /dev/null |
+++ b/chrome/browser/extensions/api/messaging/native_message_process_host.cc |
@@ -0,0 +1,203 @@ |
+// 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. |
+ |
+#include "chrome/browser/extensions/api/messaging/native_message_process_host.h" |
+ |
+#include "base/file_path.h" |
+#include "base/logging.h" |
+#include "base/path_service.h" |
+#include "base/pickle.h" |
+#include "base/process_util.h" |
+#include "base/values.h" |
+#include "chrome/browser/extensions/api/messaging/native_process_launcher.h" |
+#include "chrome/common/chrome_paths.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/common/result_codes.h" |
+ |
+namespace { |
+ |
+const int kExitTimeoutMS = 5000; |
+const uint32 kMaxMessageDataLength = 10 * 1024 * 1024; |
+const char kNativeHostsDirectoryName[] = "Native Hosts"; |
+ |
+} // namespace |
+ |
+namespace extensions { |
+ |
+NativeMessageProcessHost::NativeMessageProcessHost( |
+ base::WeakPtr<Client> weak_client, |
+ int destination_port, |
+ base::ProcessHandle native_process_handle, |
+ FileHandle read_file, |
+ FileHandle write_file, |
+ bool is_send_message) |
+ : weak_client_(weak_client), |
+ destination_port_(destination_port), |
+ native_process_handle_(native_process_handle), |
+ read_file_(read_file), |
+ write_file_(write_file), |
+ is_send_message_(is_send_message) { |
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
+ scoped_read_file_.reset(&read_file_); |
+ scoped_write_file_.reset(&write_file_); |
+ InitIO(); |
+} |
+ |
+NativeMessageProcessHost::~NativeMessageProcessHost() { |
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
+ // Give the process some time to shutdown, then try and kill it. |
+ content::BrowserThread::PostDelayedTask( |
+ content::BrowserThread::FILE, |
+ FROM_HERE, |
+ base::Bind(base::IgnoreResult(&base::KillProcess), |
+ native_process_handle_, |
+ content::RESULT_CODE_NORMAL_EXIT, |
+ false /* don't wait for exit */), |
+ base::TimeDelta::FromMilliseconds(kExitTimeoutMS)); |
+} |
+ |
+// static |
+void NativeMessageProcessHost::Create(base::WeakPtr<Client> weak_client, |
+ const std::string& native_app_name, |
+ const std::string& connection_message, |
+ int destination_port, |
+ MessageType type, |
+ CreateCallback callback) { |
+ CreateWithLauncher(weak_client, native_app_name, connection_message, |
+ destination_port, type, callback, |
+ NativeProcessLauncher()); |
+} |
+ |
+// static |
+void NativeMessageProcessHost::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) { |
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
+ DCHECK(type == TYPE_SEND_MESSAGE_REQUEST || type == TYPE_CONNECT); |
+ |
+ FilePath native_host_program; |
+ FilePath native_host_registry; |
+ CHECK(PathService::Get(chrome::DIR_USER_DATA, &native_host_registry)); |
+ native_host_registry = |
+ native_host_registry.Append(kNativeHostsDirectoryName); |
+ native_host_program = native_host_registry.Append(native_app_name); |
+ |
+ NativeMessageProcessHost* process = NULL; |
+ |
+ // Make sure that the client is not trying to invoke something outside of the |
+ // proper directory. Eg. '../../dangerous_something.exe'. |
+ if (!file_util::ContainsPath(native_host_registry, native_host_program)) { |
+ LOG(ERROR) << "Could not find native host: " << native_app_name; |
+ content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
+ base::Bind(callback, process)); |
+ return; |
+ } |
+ |
+ FileHandle read_handle; |
+ FileHandle write_handle; |
+ base::ProcessHandle native_process_handle; |
+ |
+ if (!launcher.LaunchNativeProcess(native_host_program, |
+ &native_process_handle, |
+ &read_handle, |
+ &write_handle)) { |
+ content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
+ base::Bind(callback, process)); |
+ return; |
+ } |
+ |
+ process = new NativeMessageProcessHost(weak_client, destination_port, |
+ native_process_handle, read_handle, |
Matt Perry
2012/08/22 00:02:42
indent either 4 spaces or to align with the (
eaugusti
2012/08/31 23:47:13
Done.
|
+ write_handle, |
+ type == TYPE_SEND_MESSAGE_REQUEST); |
+ |
+ process->SendImpl(type, connection_message); |
+ |
+ content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
+ base::Bind(callback, process)); |
Matt Perry
2012/08/22 00:02:42
The good news is that since MessageService uses a
eaugusti
2012/08/31 23:47:13
Nice catch, but we also have to make sure process
|
+} |
+ |
+void NativeMessageProcessHost::SendImpl(MessageType type, |
+ const std::string& json) { |
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
+ WriteMessage(type, json); |
+} |
+ |
+bool NativeMessageProcessHost::WriteMessage(MessageType type, |
+ const std::string& message) { |
Matt Perry
2012/08/22 00:02:42
bad indent
eaugusti
2012/08/31 23:47:13
Done.
|
+ Pickle pickle; |
+ |
+ // Pickles will always pad bytes to 32-bit alignment, so just use a unit32. |
+ pickle.WriteUInt32(type); |
+ |
+ // Pickles write the length of a string before it as a uint32. |
+ pickle.WriteString(message); |
+ |
+ // Make sure that the pickle doesn't do any unexpected padding. |
+ CHECK(8 + message.length() == pickle.payload_size()); |
+ |
+ if (!WriteData(write_file_, const_cast<const Pickle*>(&pickle)->payload(), |
+ pickle.payload_size())) { |
+ LOG(ERROR) << "Error writing message to the native client."; |
+ return false; |
+ } |
+ |
+ return true; |
+} |
+ |
+bool NativeMessageProcessHost::ReadMessage(MessageType* type, |
+ std::string* message) { |
Matt Perry
2012/08/22 00:02:42
bad indent
eaugusti
2012/08/31 23:47:13
Done.
|
+ // Read the type (uint32) and length (uint32). |
+ char message_meta_data[8]; |
+ if (!ReadData(read_file_, message_meta_data, 8)) { |
+ LOG(ERROR) << "Error reading the message type and length."; |
+ return false; |
+ } |
+ |
+ Pickle pickle; |
+ pickle.WriteBytes(message_meta_data, 8); |
Matt Perry
2012/08/22 00:02:42
Use "Pickle pickle(message_meta_data, 8)" to avoid
eaugusti
2012/08/31 23:47:13
We can't use that because then Pickle assumes that
|
+ PickleIterator pickle_it(pickle); |
+ uint32 int_type; |
+ uint32 data_length; |
+ if (!pickle_it.ReadUInt32(&int_type) || !pickle_it.ReadUInt32(&data_length)) { |
+ LOG(ERROR) << "Error getting the message type and length from the pickle."; |
+ return false; |
+ } |
+ |
+ if (int_type < 0 || int_type >= NUM_MESSAGE_TYPES) { |
+ LOG(ERROR) << type << " is not a valid message type."; |
+ return false; |
+ } |
+ |
+ if ((is_send_message_ && (int_type != TYPE_SEND_MESSAGE_RESPONSE)) || |
+ (!is_send_message_ && (int_type != TYPE_CONNECT_MESSAGE))) { |
+ LOG(ERROR) << "Recieved a message of type " << int_type << ". " |
+ << "Expecting a message of type " |
+ << (is_send_message_ ? TYPE_SEND_MESSAGE_RESPONSE : |
+ TYPE_CONNECT_MESSAGE); |
+ return false; |
+ } |
+ *type = static_cast<MessageType>(int_type); |
+ |
+ if (data_length > kMaxMessageDataLength) { |
+ LOG(ERROR) << data_length << " is too large for the length of a message. " |
+ << "Max message size is " << kMaxMessageDataLength; |
+ return false; |
+ } |
+ |
+ message->resize(data_length, '\0'); |
+ if (!ReadData(read_file_, &(*message)[0], data_length)) { |
+ LOG(ERROR) << "Error reading the json data."; |
+ return false; |
+ } |
+ |
+ return true; |
+} |
+ |
+} // namespace extensions |