OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/extensions/api/native_message/native_message_api.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/command_line.h" | |
9 #include "base/file_path.h" | |
10 #include "base/file_util.h" | |
11 #include "base/logging.h" | |
12 #include "base/path_service.h" | |
13 #include "base/threading/simple_thread.h" | |
14 #include "base/values.h" | |
15 #include "chrome/browser/extensions/api/native_message/native_thread_delegate.h" | |
16 #include "chrome/common/chrome_paths.h" | |
17 #include "chrome/common/chrome_switches.h" | |
18 #include "content/public/browser/browser_thread.h" | |
19 | |
20 namespace { | |
21 | |
22 const char kNativeClientDir[] = "native-hosts"; | |
Matt Perry
2012/07/23 23:47:03
Chrome's data directories are formatted like "Nati
| |
23 | |
24 } // namespace | |
25 | |
26 SendNativeMessageFunction::SendNativeMessageFunction() {} | |
27 | |
28 SendNativeMessageFunction::~SendNativeMessageFunction() { | |
29 } | |
30 | |
31 void SendNativeMessageFunction::SendResponseOnUIThread( | |
32 bool success, scoped_ptr<std::string> error, | |
33 scoped_ptr<DictionaryValue> result) { | |
34 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
35 | |
36 if (error.get() && !error->empty()) { | |
37 CHECK(!success); | |
38 SetError(*error.get()); | |
39 LOG(ERROR) << *error; | |
40 } | |
41 if (success) { | |
42 SetResult(result->DeepCopy()); | |
43 } | |
44 SendResponse(success); | |
45 | |
46 content::BrowserThread::PostBlockingPoolTask( | |
47 FROM_HERE, | |
48 base::Bind(&SendNativeMessageFunction::JoinThreadOnBackgroundThread, | |
49 this)); | |
50 } | |
51 | |
52 bool SendNativeMessageFunction::RunImpl() { | |
53 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
| |
54 switches::kEnableNativeMessaging)) | |
55 return false; | |
56 | |
57 DictionaryValue* data; | |
58 std::string native_app_name; | |
59 // TODO(eriq): There is nothing to prevent the user from pathing away from the | |
60 // intended directory. Eg. '../../dangerous_something.exe'. | |
Aaron Boodman
2012/07/23 22:30:17
Good point. You can easily defend against that by
| |
61 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &native_app_name)); | |
62 // args_ maintains ownership of |data|, it is safe to pass by reference. | |
63 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &data)); | |
64 | |
65 FilePath path; | |
66 CHECK(PathService::Get(chrome::DIR_USER_DATA, &path)); | |
67 path = path.Append(kNativeClientDir); | |
68 path = path.Append(native_app_name); | |
69 | |
70 thread_delegate_.reset(new NativeThreadDelegate( | |
71 data, scoped_ptr<FilePath>(new FilePath(path)), | |
72 base::Bind(&SendNativeMessageFunction::SendResponseOnUIThread, | |
73 this))); | |
74 thread_.reset(new base::DelegateSimpleThread(thread_delegate_.get(), | |
75 "test_prefix")); | |
Matt Perry
2012/07/23 23:47:03
Have you seen base::WorkerPool? It might be better
| |
76 thread_->Start(); | |
77 | |
78 return true; | |
79 } | |
80 | |
81 void SendNativeMessageFunction::JoinThreadOnBackgroundThread() { | |
82 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
83 thread_->Join(); | |
84 } | |
OLD | NEW |