| 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 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" | 5 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/command_line.h" | |
| 9 #include "base/logging.h" | 8 #include "base/logging.h" |
| 10 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 11 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 12 #include "base/process_util.h" | 11 #include "base/process_util.h" |
| 13 #include "base/string_split.h" | 12 #include "base/sequenced_task_runner.h" |
| 14 #include "base/threading/sequenced_worker_pool.h" | 13 #include "base/threading/sequenced_worker_pool.h" |
| 15 #include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest
.h" | |
| 16 #include "chrome/common/chrome_paths.h" | 14 #include "chrome/common/chrome_paths.h" |
| 17 #include "chrome/common/chrome_switches.h" | |
| 18 #include "googleurl/src/gurl.h" | |
| 19 | 15 |
| 20 namespace extensions { | 16 namespace extensions { |
| 21 | 17 |
| 22 namespace { | 18 namespace { |
| 23 | 19 |
| 24 const char kNativeHostsDirectoryName[] = "native_hosts"; | 20 const char kNativeHostsDirectoryName[] = "native_hosts"; |
| 25 | 21 |
| 26 base::FilePath GetHostManifestPathFromCommandLine( | |
| 27 const std::string& native_host_name) { | |
| 28 const std::string& value = | |
| 29 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 30 switches::kNativeMessagingHosts); | |
| 31 if (value.empty()) | |
| 32 return base::FilePath(); | |
| 33 | |
| 34 std::vector<std::string> hosts; | |
| 35 base::SplitString(value, ',', &hosts); | |
| 36 for (size_t i = 0; i < hosts.size(); ++i) { | |
| 37 std::vector<std::string> key_and_value; | |
| 38 base::SplitString(hosts[i], '=', &key_and_value); | |
| 39 if (key_and_value.size() != 2) | |
| 40 continue; | |
| 41 if (key_and_value[0] == native_host_name) | |
| 42 return base::FilePath::FromUTF8Unsafe(key_and_value[1]); | |
| 43 } | |
| 44 | |
| 45 return base::FilePath(); | |
| 46 } | |
| 47 | |
| 48 | |
| 49 // Default implementation on NativeProcessLauncher interface. | 22 // Default implementation on NativeProcessLauncher interface. |
| 50 class NativeProcessLauncherImpl : public NativeProcessLauncher { | 23 class NativeProcessLauncherImpl : public NativeProcessLauncher { |
| 51 public: | 24 public: |
| 52 NativeProcessLauncherImpl(); | 25 NativeProcessLauncherImpl(); |
| 53 virtual ~NativeProcessLauncherImpl(); | 26 virtual ~NativeProcessLauncherImpl(); |
| 54 | 27 |
| 55 virtual void Launch(const GURL& origin, | 28 virtual void Launch(const std::string& native_host_name, |
| 56 const std::string& native_host_name, | |
| 57 LaunchedCallback callback) const OVERRIDE; | 29 LaunchedCallback callback) const OVERRIDE; |
| 58 | 30 |
| 59 private: | 31 private: |
| 60 class Core : public base::RefCountedThreadSafe<Core> { | 32 class Core : public base::RefCountedThreadSafe<Core> { |
| 61 public: | 33 public: |
| 62 Core(); | 34 Core(); |
| 63 void Launch(const GURL& origin, | 35 void Launch(const std::string& native_host_name, |
| 64 const std::string& native_host_name, | |
| 65 LaunchedCallback callback); | 36 LaunchedCallback callback); |
| 66 void Detach(); | 37 void Detach(); |
| 67 | 38 |
| 68 private: | 39 private: |
| 69 friend class base::RefCountedThreadSafe<Core>; | 40 friend class base::RefCountedThreadSafe<Core>; |
| 70 virtual ~Core(); | 41 virtual ~Core(); |
| 71 | 42 |
| 72 void DoLaunchOnThreadPool(const GURL& origin, | 43 void DoLaunchOnThreadPool(const std::string& native_host_name, |
| 73 const std::string& native_host_name, | |
| 74 LaunchedCallback callback); | 44 LaunchedCallback callback); |
| 75 void CallCallbackOnIOThread(LaunchedCallback callback, | 45 void CallCallbackOnIOThread(LaunchedCallback callback, |
| 76 bool result, | 46 bool result, |
| 77 base::PlatformFile read_file, | 47 base::PlatformFile read_file, |
| 78 base::PlatformFile write_file); | 48 base::PlatformFile write_file); |
| 79 | 49 |
| 80 bool detached_; | 50 bool detached_; |
| 81 | 51 |
| 82 DISALLOW_COPY_AND_ASSIGN(Core); | 52 DISALLOW_COPY_AND_ASSIGN(Core); |
| 83 }; | 53 }; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 94 NativeProcessLauncherImpl::Core::~Core() { | 64 NativeProcessLauncherImpl::Core::~Core() { |
| 95 DCHECK(detached_); | 65 DCHECK(detached_); |
| 96 } | 66 } |
| 97 | 67 |
| 98 void NativeProcessLauncherImpl::Core::Detach() { | 68 void NativeProcessLauncherImpl::Core::Detach() { |
| 99 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 69 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 100 detached_ = true; | 70 detached_ = true; |
| 101 } | 71 } |
| 102 | 72 |
| 103 void NativeProcessLauncherImpl::Core::Launch( | 73 void NativeProcessLauncherImpl::Core::Launch( |
| 104 const GURL& origin, | |
| 105 const std::string& native_host_name, | 74 const std::string& native_host_name, |
| 106 LaunchedCallback callback) { | 75 LaunchedCallback callback) { |
| 107 content::BrowserThread::PostBlockingPoolTask( | 76 content::BrowserThread::PostBlockingPoolTask( |
| 108 FROM_HERE, base::Bind(&Core::DoLaunchOnThreadPool, this, | 77 FROM_HERE, base::Bind(&Core::DoLaunchOnThreadPool, this, |
| 109 origin, native_host_name, callback)); | 78 native_host_name, callback)); |
| 110 } | 79 } |
| 111 | 80 |
| 112 void NativeProcessLauncherImpl::Core::DoLaunchOnThreadPool( | 81 void NativeProcessLauncherImpl::Core::DoLaunchOnThreadPool( |
| 113 const GURL& origin, | |
| 114 const std::string& native_host_name, | 82 const std::string& native_host_name, |
| 115 LaunchedCallback callback) { | 83 LaunchedCallback callback) { |
| 116 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 84 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 117 | 85 |
| 118 std::string error_message; | 86 base::FilePath native_host_program; |
| 119 scoped_ptr<NativeMessagingHostManifest> manifest; | 87 base::FilePath native_host_registry; |
| 88 CHECK(PathService::Get(chrome::DIR_USER_DATA, &native_host_registry)); |
| 89 native_host_registry = |
| 90 native_host_registry.AppendASCII(kNativeHostsDirectoryName); |
| 91 native_host_program = native_host_registry.AppendASCII(native_host_name); |
| 120 | 92 |
| 121 if (!NativeMessagingHostManifest::IsValidName(native_host_name)) { | 93 // Make sure that the client is not trying to invoke something outside of the |
| 122 error_message = "Invalid native host name: " + native_host_name; | 94 // proper directory. Eg. '../../dangerous_something.exe'. |
| 123 } else { | 95 if (!file_util::ContainsPath(native_host_registry, native_host_program)) { |
| 124 // First check if the manifest location is specified in the command line. | 96 LOG(ERROR) << "Could not find native host: " << native_host_name; |
| 125 base::FilePath path = GetHostManifestPathFromCommandLine(native_host_name); | |
| 126 if (!path.empty()) { | |
| 127 manifest = NativeMessagingHostManifest::Load(path, &error_message); | |
| 128 } else { | |
| 129 // Try loading the manifest from the default location. | |
| 130 manifest = FindAndLoadManifest(native_host_name, &error_message); | |
| 131 } | |
| 132 | |
| 133 if (manifest && manifest->name() != native_host_name) { | |
| 134 error_message = "Name specified in the manifest does not match."; | |
| 135 manifest.reset(); | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 if (!manifest) { | |
| 140 // TODO(sergeyu): Report the error to the application. | |
| 141 LOG(ERROR) << "Failed to load manifest for native messaging host " | |
| 142 << native_host_name << ": " << error_message; | |
| 143 content::BrowserThread::PostTask( | 97 content::BrowserThread::PostTask( |
| 144 content::BrowserThread::IO, FROM_HERE, | 98 content::BrowserThread::IO, FROM_HERE, |
| 145 base::Bind(&NativeProcessLauncherImpl::Core::CallCallbackOnIOThread, | 99 base::Bind(&NativeProcessLauncherImpl::Core::CallCallbackOnIOThread, |
| 146 this, callback, false, | 100 this, callback, false, |
| 147 base::kInvalidPlatformFileValue, | 101 base::kInvalidPlatformFileValue, |
| 148 base::kInvalidPlatformFileValue)); | 102 base::kInvalidPlatformFileValue)); |
| 149 return; | 103 return; |
| 150 } | 104 } |
| 151 | 105 |
| 152 if (!manifest->allowed_origins().MatchesSecurityOrigin(origin)) { | |
| 153 // Not an allowed origin. | |
| 154 content::BrowserThread::PostTask( | |
| 155 content::BrowserThread::IO, FROM_HERE, | |
| 156 base::Bind(&NativeProcessLauncherImpl::Core::CallCallbackOnIOThread, | |
| 157 this, callback, false, | |
| 158 base::kInvalidPlatformFileValue, | |
| 159 base::kInvalidPlatformFileValue)); | |
| 160 return; | |
| 161 } | |
| 162 | |
| 163 base::PlatformFile read_file; | 106 base::PlatformFile read_file; |
| 164 base::PlatformFile write_file; | 107 base::PlatformFile write_file; |
| 165 bool result = NativeProcessLauncher::LaunchNativeProcess( | 108 bool result = NativeProcessLauncher::LaunchNativeProcess( |
| 166 manifest->path(), &read_file, &write_file); | 109 native_host_program, &read_file, &write_file); |
| 167 | 110 |
| 168 content::BrowserThread::PostTask( | 111 content::BrowserThread::PostTask( |
| 169 content::BrowserThread::IO, FROM_HERE, | 112 content::BrowserThread::IO, FROM_HERE, |
| 170 base::Bind(&NativeProcessLauncherImpl::Core::CallCallbackOnIOThread, | 113 base::Bind(&NativeProcessLauncherImpl::Core::CallCallbackOnIOThread, |
| 171 this, callback, result, read_file, write_file)); | 114 this, callback, result, read_file, write_file)); |
| 172 } | 115 } |
| 173 | 116 |
| 174 void NativeProcessLauncherImpl::Core::CallCallbackOnIOThread( | 117 void NativeProcessLauncherImpl::Core::CallCallbackOnIOThread( |
| 175 LaunchedCallback callback, | 118 LaunchedCallback callback, |
| 176 bool result, | 119 bool result, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 189 } | 132 } |
| 190 | 133 |
| 191 NativeProcessLauncherImpl::NativeProcessLauncherImpl() | 134 NativeProcessLauncherImpl::NativeProcessLauncherImpl() |
| 192 : core_(new Core()) { | 135 : core_(new Core()) { |
| 193 } | 136 } |
| 194 | 137 |
| 195 NativeProcessLauncherImpl::~NativeProcessLauncherImpl() { | 138 NativeProcessLauncherImpl::~NativeProcessLauncherImpl() { |
| 196 core_->Detach(); | 139 core_->Detach(); |
| 197 } | 140 } |
| 198 | 141 |
| 199 void NativeProcessLauncherImpl::Launch(const GURL& origin, | 142 void NativeProcessLauncherImpl::Launch(const std::string& native_host_name, |
| 200 const std::string& native_host_name, | |
| 201 LaunchedCallback callback) const { | 143 LaunchedCallback callback) const { |
| 202 core_->Launch(origin, native_host_name, callback); | 144 core_->Launch(native_host_name, callback); |
| 203 } | 145 } |
| 204 | 146 |
| 205 } // namespace | 147 } // namespace |
| 206 | 148 |
| 207 // static | 149 // static |
| 208 scoped_ptr<NativeProcessLauncher> NativeProcessLauncher::CreateDefault() { | 150 scoped_ptr<NativeProcessLauncher> NativeProcessLauncher::CreateDefault() { |
| 209 return scoped_ptr<NativeProcessLauncher>(new NativeProcessLauncherImpl()); | 151 return scoped_ptr<NativeProcessLauncher>(new NativeProcessLauncherImpl()); |
| 210 } | 152 } |
| 211 | 153 |
| 212 } // namespace extensions | 154 } // namespace extensions |
| OLD | NEW |