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"
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"
23 #include "ui/views/win/hwnd_util.h"
26 namespace extensions
{
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
{
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
;
48 class Core
: public base::RefCountedThreadSafe
<Core
> {
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
);
57 friend class base::RefCountedThreadSafe
<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
,
67 base::File write_file
);
68 void CallCallbackOnIOThread(const LaunchedCallback
& callback
,
70 base::Process process
,
72 base::File write_file
);
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
)
92 allow_user_level_hosts_(allow_user_level_hosts
),
93 window_handle_(window_handle
) {
96 NativeProcessLauncherImpl::Core::~Core() {
100 void NativeProcessLauncherImpl::Core::Detach() {
101 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
105 void NativeProcessLauncherImpl::Core::Launch(
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(
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
);
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 "
132 PostErrorResult(callback
, RESULT_NOT_FOUND
);
136 scoped_ptr
<NativeMessagingHostManifest
> manifest
=
137 NativeMessagingHostManifest::Load(manifest_path
, &error_message
);
140 LOG(ERROR
) << "Failed to load manifest for native messaging host "
141 << native_host_name
<< ": " << error_message
;
142 PostErrorResult(callback
, RESULT_NOT_FOUND
);
146 if (manifest
->name() != native_host_name
) {
147 LOG(ERROR
) << "Failed to load manifest for native messaging host "
149 << ": Invalid name specified in the manifest.";
150 PostErrorResult(callback
, RESULT_NOT_FOUND
);
154 if (!manifest
->allowed_origins().MatchesSecurityOrigin(origin
)) {
155 // Not an allowed origin.
156 PostErrorResult(callback
, RESULT_FORBIDDEN
);
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.
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 "
169 PostErrorResult(callback
, RESULT_NOT_FOUND
);
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
)) {
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
);
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.
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());
202 PostErrorResult(callback
, RESULT_FAILED_TO_START
);
206 void NativeProcessLauncherImpl::Core::CallCallbackOnIOThread(
207 const LaunchedCallback
& callback
,
209 base::Process process
,
210 base::File read_file
,
211 base::File write_file
) {
212 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
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() {
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
);
260 scoped_ptr
<NativeProcessLauncher
> NativeProcessLauncher::CreateDefault(
261 bool allow_user_level_hosts
,
262 gfx::NativeView native_view
) {
263 intptr_t window_handle
= 0;
265 window_handle
= reinterpret_cast<intptr_t>(
266 views::HWNDForNativeView(native_view
));
268 return scoped_ptr
<NativeProcessLauncher
>(
269 new NativeProcessLauncherImpl(allow_user_level_hosts
, window_handle
));
272 } // namespace extensions