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