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_frame/chrome_launcher.h"
11 #include "policy/policy_constants.h"
13 // Herein lies stuff selectively stolen from Chrome. We don't pull it in
14 // directly because all of it results in many things we don't want being
18 // These are the switches we will allow (along with their values) in the
19 // safe-for-Low-Integrity version of the Chrome command line.
20 // Including the chrome switch files pulls in a bunch of dependencies sadly, so
21 // we redefine things here:
22 const wchar_t* kAllowedSwitches
[] = {
23 L
"automation-channel",
26 L
"disable-background-mode",
27 L
"disable-popup-blocking",
28 L
"disable-print-preview",
29 L
"disable-renderer-accessibility",
30 L
"enable-experimental-extension-apis",
31 L
"force-renderer-accessibility",
32 L
"full-memory-crash-report",
34 L
"no-default-browser-check",
40 const wchar_t kWhitespaceChars
[] = {
41 0x0009, /* <control-0009> to <control-000D> */
47 0x0085, /* <control-0085> */
48 0x00A0, /* No-Break Space */
49 0x1680, /* Ogham Space Mark */
50 0x180E, /* Mongolian Vowel Separator */
51 0x2000, /* En Quad to Hair Space */
62 0x200C, /* Zero Width Non-Joiner */
63 0x2028, /* Line Separator */
64 0x2029, /* Paragraph Separator */
65 0x202F, /* Narrow No-Break Space */
66 0x205F, /* Medium Mathematical Space */
67 0x3000, /* Ideographic Space */
71 const wchar_t kLauncherExeBaseName
[] = L
"chrome_launcher.exe";
72 const wchar_t kBrowserProcessExecutableName
[] = L
"chrome.exe";
77 namespace chrome_launcher
{
79 std::wstring
TrimWhiteSpace(const wchar_t* input_str
) {
81 if (input_str
!= NULL
) {
82 std::wstring
str(input_str
);
84 const std::wstring::size_type first_good_char
=
85 str
.find_first_not_of(kWhitespaceChars
);
86 const std::wstring::size_type last_good_char
=
87 str
.find_last_not_of(kWhitespaceChars
);
89 if (first_good_char
!= std::wstring::npos
&&
90 last_good_char
!= std::wstring::npos
&&
91 last_good_char
>= first_good_char
) {
92 // + 1 because find_last_not_of returns the index, and we want the count
93 output
= str
.substr(first_good_char
,
94 last_good_char
- first_good_char
+ 1);
101 bool IsValidArgument(const std::wstring
& arg
) {
102 if (arg
.length() < 2) {
106 for (int i
= 0; i
< arraysize(kAllowedSwitches
); ++i
) {
107 size_t arg_length
= lstrlenW(kAllowedSwitches
[i
]);
108 if (arg
.find(kAllowedSwitches
[i
], 2) == 2) {
109 // The argument starts off right, now it must either end here, or be
110 // followed by an equals sign.
111 if (arg
.length() == (arg_length
+ 2) ||
112 (arg
.length() > (arg_length
+ 2) && arg
[arg_length
+2] == L
'=')) {
121 bool IsValidCommandLine(const wchar_t* command_line
) {
122 if (command_line
== NULL
) {
127 wchar_t** args
= NULL
;
128 args
= CommandLineToArgvW(command_line
, &num_args
);
131 // Note that we skip args[0] since that is just our executable name and
132 // doesn't get passed through to Chrome.
133 for (int i
= 1; i
< num_args
; ++i
) {
134 std::wstring trimmed_arg
= TrimWhiteSpace(args
[i
]);
135 if (!IsValidArgument(trimmed_arg
)) {
144 // Looks up optionally configured launch parameters for Chrome that may have
145 // been set via group policy.
146 void AppendAdditionalLaunchParameters(std::wstring
* command_line
) {
147 static const HKEY kRootKeys
[] = {
152 std::wstring
launch_params_value_name(
153 &policy::key::kAdditionalLaunchParameters
[0],
154 &policy::key::kAdditionalLaunchParameters
[
155 lstrlenA(policy::key::kAdditionalLaunchParameters
)]);
157 // Used for basic checks since CreateProcess doesn't support command lines
158 // longer than 0x8000 characters. If we surpass that length, we do not add the
159 // additional parameters. Because we need to add a space before the
160 // extra parameters, we use 0x7fff and not 0x8000.
161 const size_t kMaxChars
= 0x7FFF - command_line
->size();
165 for (int i
= 0; !found
&& i
< arraysize(kRootKeys
); ++i
) {
166 result
= ::RegOpenKeyExW(kRootKeys
[i
], policy::kRegistryChromePolicyKey
, 0,
167 KEY_QUERY_VALUE
, &key
);
168 if (result
== ERROR_SUCCESS
) {
171 result
= RegQueryValueExW(key
, launch_params_value_name
.c_str(),
172 0, &type
, NULL
, &size
);
173 if (result
== ERROR_SUCCESS
&& type
== REG_SZ
&& size
> 0 &&
174 (size
/ sizeof(wchar_t)) < kMaxChars
) {
175 // This size includes any terminating null character or characters
176 // unless the data was stored without them, so for safety we allocate
177 // one extra char and zero out the buffer.
178 wchar_t* value
= new wchar_t[(size
/ sizeof(wchar_t)) + 1];
179 memset(value
, 0, size
+ sizeof(wchar_t));
180 result
= RegQueryValueExW(key
, launch_params_value_name
.c_str(), 0,
181 &type
, reinterpret_cast<BYTE
*>(&value
[0]),
183 if (result
== ERROR_SUCCESS
) {
184 *command_line
+= L
' ';
185 *command_line
+= value
;
195 bool SanitizeAndLaunchChrome(const wchar_t* command_line
) {
196 bool success
= false;
197 if (IsValidCommandLine(command_line
)) {
198 std::wstring chrome_path
;
199 if (GetChromeExecutablePath(&chrome_path
)) {
200 const wchar_t* args
= PathGetArgs(command_line
);
202 // Build the command line string with the quoted path to chrome.exe.
203 std::wstring command_line
;
204 command_line
.reserve(chrome_path
.size() + 2);
205 command_line
.append(1, L
'\"').append(chrome_path
).append(1, L
'\"');
208 command_line
+= L
' ';
209 command_line
+= args
;
212 // Append parameters that might be set by group policy.
213 AppendAdditionalLaunchParameters(&command_line
);
215 STARTUPINFO startup_info
= {0};
216 startup_info
.cb
= sizeof(startup_info
);
217 startup_info
.dwFlags
= STARTF_USESHOWWINDOW
;
218 startup_info
.wShowWindow
= SW_SHOW
;
219 PROCESS_INFORMATION process_info
= {0};
220 if (CreateProcess(&chrome_path
[0], &command_line
[0],
221 NULL
, NULL
, FALSE
, 0, NULL
, NULL
,
222 &startup_info
, &process_info
)) {
224 CloseHandle(process_info
.hThread
);
225 CloseHandle(process_info
.hProcess
);
236 bool GetChromeExecutablePath(std::wstring
* chrome_path
) {
237 _ASSERT(chrome_path
);
239 wchar_t cur_path
[MAX_PATH
* 4] = {0};
240 // Assume that we are always built into an exe.
241 GetModuleFileName(NULL
, cur_path
, arraysize(cur_path
) / 2);
243 PathRemoveFileSpec(cur_path
);
245 bool success
= false;
246 if (PathAppend(cur_path
, kBrowserProcessExecutableName
)) {
247 if (!PathFileExists(cur_path
)) {
248 // The installation model for Chrome places the DLLs in a versioned
249 // sub-folder one down from the Chrome executable. If we fail to find
250 // chrome.exe in the current path, try looking one up and launching that
251 // instead. In practice, that means we back up two and append the
252 // executable name again.
253 PathRemoveFileSpec(cur_path
);
254 PathRemoveFileSpec(cur_path
);
255 PathAppend(cur_path
, kBrowserProcessExecutableName
);
258 if (PathFileExists(cur_path
)) {
259 *chrome_path
= cur_path
;
267 } // namespace chrome_launcher