1 // Copyright (c) 2011 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 // Herein lies stuff selectively stolen from Chrome. We don't pull it in
12 // directly because all of it results in many things we don't want being
16 // These are the switches we will allow (along with their values) in the
17 // safe-for-Low-Integrity version of the Chrome command line.
18 // Including the chrome switch files pulls in a bunch of dependencies sadly, so
19 // we redefine things here:
20 const wchar_t* kAllowedSwitches
[] = {
21 L
"automation-channel",
24 L
"disable-background-mode",
25 L
"disable-popup-blocking",
26 L
"disable-renderer-accessibility",
27 L
"enable-experimental-extension-apis",
28 L
"force-renderer-accessibility",
29 L
"full-memory-crash-report",
31 L
"no-default-browser-check",
37 const wchar_t kWhitespaceChars
[] = {
38 0x0009, /* <control-0009> to <control-000D> */
44 0x0085, /* <control-0085> */
45 0x00A0, /* No-Break Space */
46 0x1680, /* Ogham Space Mark */
47 0x180E, /* Mongolian Vowel Separator */
48 0x2000, /* En Quad to Hair Space */
59 0x200C, /* Zero Width Non-Joiner */
60 0x2028, /* Line Separator */
61 0x2029, /* Paragraph Separator */
62 0x202F, /* Narrow No-Break Space */
63 0x205F, /* Medium Mathematical Space */
64 0x3000, /* Ideographic Space */
68 const wchar_t kLauncherExeBaseName
[] = L
"chrome_launcher.exe";
69 const wchar_t kBrowserProcessExecutableName
[] = L
"chrome.exe";
74 namespace chrome_launcher
{
76 std::wstring
TrimWhiteSpace(const wchar_t* input_str
) {
78 if (input_str
!= NULL
) {
79 std::wstring
str(input_str
);
81 const std::wstring::size_type first_good_char
=
82 str
.find_first_not_of(kWhitespaceChars
);
83 const std::wstring::size_type last_good_char
=
84 str
.find_last_not_of(kWhitespaceChars
);
86 if (first_good_char
!= std::wstring::npos
&&
87 last_good_char
!= std::wstring::npos
&&
88 last_good_char
>= first_good_char
) {
89 // + 1 because find_last_not_of returns the index, and we want the count
90 output
= str
.substr(first_good_char
,
91 last_good_char
- first_good_char
+ 1);
98 bool IsValidArgument(const std::wstring
& arg
) {
99 if (arg
.length() < 2) {
103 for (int i
= 0; i
< arraysize(kAllowedSwitches
); ++i
) {
104 size_t arg_length
= lstrlenW(kAllowedSwitches
[i
]);
105 if (arg
.find(kAllowedSwitches
[i
], 2) == 2) {
106 // The argument starts off right, now it must either end here, or be
107 // followed by an equals sign.
108 if (arg
.length() == (arg_length
+ 2) ||
109 (arg
.length() > (arg_length
+ 2) && arg
[arg_length
+2] == L
'=')) {
118 bool IsValidCommandLine(const wchar_t* command_line
) {
119 if (command_line
== NULL
) {
124 wchar_t** args
= NULL
;
125 args
= CommandLineToArgvW(command_line
, &num_args
);
128 // Note that we skip args[0] since that is just our executable name and
129 // doesn't get passed through to Chrome.
130 for (int i
= 1; i
< num_args
; ++i
) {
131 std::wstring trimmed_arg
= TrimWhiteSpace(args
[i
]);
132 if (!IsValidArgument(trimmed_arg
.c_str())) {
141 bool SanitizeAndLaunchChrome(const wchar_t* command_line
) {
142 bool success
= false;
143 if (IsValidCommandLine(command_line
)) {
144 std::wstring chrome_path
;
145 if (GetChromeExecutablePath(&chrome_path
)) {
146 const wchar_t* args
= PathGetArgs(command_line
);
148 // Build the command line string with the quoted path to chrome.exe.
149 std::wstring command_line
;
150 command_line
.reserve(chrome_path
.size() + 2);
151 command_line
.append(1, L
'\"').append(chrome_path
).append(1, L
'\"');
154 command_line
+= L
' ';
155 command_line
+= args
;
158 STARTUPINFO startup_info
= {0};
159 startup_info
.cb
= sizeof(startup_info
);
160 startup_info
.dwFlags
= STARTF_USESHOWWINDOW
;
161 startup_info
.wShowWindow
= SW_SHOW
;
162 PROCESS_INFORMATION process_info
= {0};
163 if (CreateProcess(&chrome_path
[0], &command_line
[0],
164 NULL
, NULL
, FALSE
, 0, NULL
, NULL
,
165 &startup_info
, &process_info
)) {
167 CloseHandle(process_info
.hThread
);
168 CloseHandle(process_info
.hProcess
);
179 bool GetChromeExecutablePath(std::wstring
* chrome_path
) {
180 _ASSERT(chrome_path
);
182 wchar_t cur_path
[MAX_PATH
* 4] = {0};
183 // Assume that we are always built into an exe.
184 GetModuleFileName(NULL
, cur_path
, arraysize(cur_path
) / 2);
186 PathRemoveFileSpec(cur_path
);
188 bool success
= false;
189 if (PathAppend(cur_path
, kBrowserProcessExecutableName
)) {
190 if (!PathFileExists(cur_path
)) {
191 // The installation model for Chrome places the DLLs in a versioned
192 // sub-folder one down from the Chrome executable. If we fail to find
193 // chrome.exe in the current path, try looking one up and launching that
194 // instead. In practice, that means we back up two and append the
195 // executable name again.
196 PathRemoveFileSpec(cur_path
);
197 PathRemoveFileSpec(cur_path
);
198 PathAppend(cur_path
, kBrowserProcessExecutableName
);
201 if (PathFileExists(cur_path
)) {
202 *chrome_path
= cur_path
;
210 } // namespace chrome_launcher