Index: chrome/browser/extensions/api/native_message/native_message_api.cc |
diff --git a/chrome/browser/extensions/api/native_message/native_message_api.cc b/chrome/browser/extensions/api/native_message/native_message_api.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..481174da2afeb560b6bcd2fa9416b177140bd609 |
--- /dev/null |
+++ b/chrome/browser/extensions/api/native_message/native_message_api.cc |
@@ -0,0 +1,84 @@ |
+// 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/native_message/native_message_api.h" |
+ |
+#include "base/bind.h" |
+#include "base/command_line.h" |
+#include "base/file_path.h" |
+#include "base/file_util.h" |
+#include "base/logging.h" |
+#include "base/path_service.h" |
+#include "base/threading/simple_thread.h" |
+#include "base/values.h" |
+#include "chrome/browser/extensions/api/native_message/native_thread_delegate.h" |
+#include "chrome/common/chrome_paths.h" |
+#include "chrome/common/chrome_switches.h" |
+#include "content/public/browser/browser_thread.h" |
+ |
+namespace { |
+ |
+const char kNativeClientDir[] = "native-hosts"; |
Matt Perry
2012/07/23 23:47:03
Chrome's data directories are formatted like "Nati
|
+ |
+} // namespace |
+ |
+SendNativeMessageFunction::SendNativeMessageFunction() {} |
+ |
+SendNativeMessageFunction::~SendNativeMessageFunction() { |
+} |
+ |
+void SendNativeMessageFunction::SendResponseOnUIThread( |
+ bool success, scoped_ptr<std::string> error, |
+ scoped_ptr<DictionaryValue> result) { |
+ CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
+ |
+ if (error.get() && !error->empty()) { |
+ CHECK(!success); |
+ SetError(*error.get()); |
+ LOG(ERROR) << *error; |
+ } |
+ if (success) { |
+ SetResult(result->DeepCopy()); |
+ } |
+ SendResponse(success); |
+ |
+ content::BrowserThread::PostBlockingPoolTask( |
+ FROM_HERE, |
+ base::Bind(&SendNativeMessageFunction::JoinThreadOnBackgroundThread, |
+ this)); |
+} |
+ |
+bool SendNativeMessageFunction::RunImpl() { |
+ if (!CommandLine::ForCurrentProcess()->HasSwitch( |
Aaron Boodman
2012/07/23 22:30:17
Can you also make this only work on the dev channe
Matt Perry
2012/07/23 23:47:03
Wouldn't it just be a matter of adding extension.s
|
+ switches::kEnableNativeMessaging)) |
+ return false; |
+ |
+ DictionaryValue* data; |
+ std::string native_app_name; |
+ // TODO(eriq): There is nothing to prevent the user from pathing away from the |
+ // intended directory. Eg. '../../dangerous_something.exe'. |
Aaron Boodman
2012/07/23 22:30:17
Good point. You can easily defend against that by
|
+ EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &native_app_name)); |
+ // args_ maintains ownership of |data|, it is safe to pass by reference. |
+ EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &data)); |
+ |
+ FilePath path; |
+ CHECK(PathService::Get(chrome::DIR_USER_DATA, &path)); |
+ path = path.Append(kNativeClientDir); |
+ path = path.Append(native_app_name); |
+ |
+ thread_delegate_.reset(new NativeThreadDelegate( |
+ data, scoped_ptr<FilePath>(new FilePath(path)), |
+ base::Bind(&SendNativeMessageFunction::SendResponseOnUIThread, |
+ this))); |
+ thread_.reset(new base::DelegateSimpleThread(thread_delegate_.get(), |
+ "test_prefix")); |
Matt Perry
2012/07/23 23:47:03
Have you seen base::WorkerPool? It might be better
|
+ thread_->Start(); |
+ |
+ return true; |
+} |
+ |
+void SendNativeMessageFunction::JoinThreadOnBackgroundThread() { |
+ CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
+ thread_->Join(); |
+} |