Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / extensions / api / messaging / native_process_launcher.cc
bloba6bf208202dc3b421764812f3bd63e219326b494
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.
5 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h"
7 #include "base/basictypes.h"
8 #include "base/bind.h"
9 #include "base/callback.h"
10 #include "base/command_line.h"
11 #include "base/files/file_util.h"
12 #include "base/logging.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h"
16 #include "base/threading/sequenced_worker_pool.h"
17 #include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "url/gurl.h"
22 #if defined(OS_WIN)
23 #include "ui/views/win/hwnd_util.h"
24 #endif
26 namespace extensions {
28 namespace {
30 #if defined(OS_WIN)
31 // Name of the command line switch used to pass handle of the native view to
32 // the native messaging host.
33 const char kParentWindowSwitchName[] = "parent-window";
34 #endif // defined(OS_WIN)
36 // Default implementation on NativeProcessLauncher interface.
37 class NativeProcessLauncherImpl : public NativeProcessLauncher {
38 public:
39 NativeProcessLauncherImpl(bool allow_user_level_hosts,
40 intptr_t native_window);
41 ~NativeProcessLauncherImpl() override;
43 void Launch(const GURL& origin,
44 const std::string& native_host_name,
45 const LaunchedCallback& callback) const override;
47 private:
48 class Core : public base::RefCountedThreadSafe<Core> {
49 public:
50 Core(bool allow_user_level_hosts, intptr_t native_window);
51 void Launch(const GURL& origin,
52 const std::string& native_host_name,
53 const LaunchedCallback& callback);
54 void Detach();
56 private:
57 friend class base::RefCountedThreadSafe<Core>;
58 virtual ~Core();
60 void DoLaunchOnThreadPool(const GURL& origin,
61 const std::string& native_host_name,
62 const LaunchedCallback& callback);
63 void PostErrorResult(const LaunchedCallback& callback, LaunchResult error);
64 void PostResult(const LaunchedCallback& callback,
65 base::Process process,
66 base::File read_file,
67 base::File write_file);
68 void CallCallbackOnIOThread(const LaunchedCallback& callback,
69 LaunchResult result,
70 base::Process process,
71 base::File read_file,
72 base::File write_file);
74 bool detached_;
76 bool allow_user_level_hosts_;
78 // Handle of the native window corresponding to the extension.
79 intptr_t window_handle_;
81 DISALLOW_COPY_AND_ASSIGN(Core);
84 scoped_refptr<Core> core_;
86 DISALLOW_COPY_AND_ASSIGN(NativeProcessLauncherImpl);
89 NativeProcessLauncherImpl::Core::Core(bool allow_user_level_hosts,
90 intptr_t window_handle)
91 : detached_(false),
92 allow_user_level_hosts_(allow_user_level_hosts),
93 window_handle_(window_handle) {
96 NativeProcessLauncherImpl::Core::~Core() {
97 DCHECK(detached_);
100 void NativeProcessLauncherImpl::Core::Detach() {
101 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
102 detached_ = true;
105 void NativeProcessLauncherImpl::Core::Launch(
106 const GURL& origin,
107 const std::string& native_host_name,
108 const LaunchedCallback& callback) {
109 content::BrowserThread::PostBlockingPoolTask(
110 FROM_HERE, base::Bind(&Core::DoLaunchOnThreadPool, this,
111 origin, native_host_name, callback));
114 void NativeProcessLauncherImpl::Core::DoLaunchOnThreadPool(
115 const GURL& origin,
116 const std::string& native_host_name,
117 const LaunchedCallback& callback) {
118 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
120 if (!NativeMessagingHostManifest::IsValidName(native_host_name)) {
121 PostErrorResult(callback, RESULT_INVALID_NAME);
122 return;
125 std::string error_message;
126 base::FilePath manifest_path =
127 FindManifest(native_host_name, allow_user_level_hosts_, &error_message);
129 if (manifest_path.empty()) {
130 LOG(ERROR) << "Can't find manifest for native messaging host "
131 << native_host_name;
132 PostErrorResult(callback, RESULT_NOT_FOUND);
133 return;
136 scoped_ptr<NativeMessagingHostManifest> manifest =
137 NativeMessagingHostManifest::Load(manifest_path, &error_message);
139 if (!manifest) {
140 LOG(ERROR) << "Failed to load manifest for native messaging host "
141 << native_host_name << ": " << error_message;
142 PostErrorResult(callback, RESULT_NOT_FOUND);
143 return;
146 if (manifest->name() != native_host_name) {
147 LOG(ERROR) << "Failed to load manifest for native messaging host "
148 << native_host_name
149 << ": Invalid name specified in the manifest.";
150 PostErrorResult(callback, RESULT_NOT_FOUND);
151 return;
154 if (!manifest->allowed_origins().MatchesSecurityOrigin(origin)) {
155 // Not an allowed origin.
156 PostErrorResult(callback, RESULT_FORBIDDEN);
157 return;
160 base::FilePath host_path = manifest->path();
161 if (!host_path.IsAbsolute()) {
162 // On Windows host path is allowed to be relative to the location of the
163 // manifest file. On all other platforms the path must be absolute.
164 #if defined(OS_WIN)
165 host_path = manifest_path.DirName().Append(host_path);
166 #else // defined(OS_WIN)
167 LOG(ERROR) << "Native messaging host path must be absolute for "
168 << native_host_name;
169 PostErrorResult(callback, RESULT_NOT_FOUND);
170 return;
171 #endif // !defined(OS_WIN)
174 // In case when the manifest file is there, but the host binary doesn't exist
175 // report the NOT_FOUND error.
176 if (!base::PathExists(host_path)) {
177 LOG(ERROR)
178 << "Found manifest, but not the binary for native messaging host "
179 << native_host_name << ". Host path specified in the manifest: "
180 << host_path.AsUTF8Unsafe();
181 PostErrorResult(callback, RESULT_NOT_FOUND);
182 return;
185 base::CommandLine command_line(host_path);
186 command_line.AppendArg(origin.spec());
188 // Pass handle of the native view window to the native messaging host. This
189 // way the host will be able to create properly focused UI windows.
190 #if defined(OS_WIN)
191 command_line.AppendSwitchASCII(kParentWindowSwitchName,
192 base::Int64ToString(window_handle_));
193 #endif // !defined(OS_WIN)
195 base::Process process;
196 base::File read_file;
197 base::File write_file;
198 if (NativeProcessLauncher::LaunchNativeProcess(
199 command_line, &process, &read_file, &write_file)) {
200 PostResult(callback, process.Pass(), read_file.Pass(), write_file.Pass());
201 } else {
202 PostErrorResult(callback, RESULT_FAILED_TO_START);
206 void NativeProcessLauncherImpl::Core::CallCallbackOnIOThread(
207 const LaunchedCallback& callback,
208 LaunchResult result,
209 base::Process process,
210 base::File read_file,
211 base::File write_file) {
212 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
213 if (detached_)
214 return;
216 callback.Run(result, process.Pass(), read_file.Pass(), write_file.Pass());
219 void NativeProcessLauncherImpl::Core::PostErrorResult(
220 const LaunchedCallback& callback,
221 LaunchResult error) {
222 content::BrowserThread::PostTask(
223 content::BrowserThread::IO, FROM_HERE,
224 base::Bind(&NativeProcessLauncherImpl::Core::CallCallbackOnIOThread, this,
225 callback, error, Passed(base::Process()),
226 Passed(base::File()), Passed(base::File())));
229 void NativeProcessLauncherImpl::Core::PostResult(
230 const LaunchedCallback& callback,
231 base::Process process,
232 base::File read_file,
233 base::File write_file) {
234 content::BrowserThread::PostTask(
235 content::BrowserThread::IO, FROM_HERE,
236 base::Bind(&NativeProcessLauncherImpl::Core::CallCallbackOnIOThread, this,
237 callback, RESULT_SUCCESS, Passed(&process),
238 Passed(&read_file), Passed(&write_file)));
241 NativeProcessLauncherImpl::NativeProcessLauncherImpl(
242 bool allow_user_level_hosts,
243 intptr_t window_handle)
244 : core_(new Core(allow_user_level_hosts, window_handle)) {
247 NativeProcessLauncherImpl::~NativeProcessLauncherImpl() {
248 core_->Detach();
251 void NativeProcessLauncherImpl::Launch(const GURL& origin,
252 const std::string& native_host_name,
253 const LaunchedCallback& callback) const {
254 core_->Launch(origin, native_host_name, callback);
257 } // namespace
259 // static
260 scoped_ptr<NativeProcessLauncher> NativeProcessLauncher::CreateDefault(
261 bool allow_user_level_hosts,
262 gfx::NativeView native_view) {
263 intptr_t window_handle = 0;
264 #if defined(OS_WIN)
265 window_handle = reinterpret_cast<intptr_t>(
266 views::HWNDForNativeView(native_view));
267 #endif
268 return scoped_ptr<NativeProcessLauncher>(
269 new NativeProcessLauncherImpl(allow_user_level_hosts, window_handle));
272 } // namespace extensions