OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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. | |
4 | |
5 #include <windows.h> | |
6 #include <initguid.h> | |
7 #include <urlmon.h> | |
8 #pragma comment(lib, "urlmon.lib") | |
9 | |
10 #include "base/at_exit.h" | |
11 #include "base/base_paths.h" | |
12 #include "base/basictypes.h" | |
13 #include "base/command_line.h" | |
14 #include "base/file_util.h" | |
15 #include "base/logging.h" | |
16 #include "base/logging_win.h" | |
17 #include "base/memory/scoped_ptr.h" | |
18 #include "base/path_service.h" | |
19 #include "base/process/launch.h" | |
20 #include "base/strings/string16.h" | |
21 #include "base/strings/string_split.h" | |
22 #include "base/strings/string_util.h" | |
23 #include "chrome/common/chrome_switches.h" | |
24 #include "chrome/installer/launcher_support/chrome_launcher_support.h" | |
25 #include "chrome/installer/util/google_update_util.h" | |
26 #include "chrome/installer/util/html_dialog.h" | |
27 #include "chrome/installer/util/util_constants.h" | |
28 #include "third_party/omaha/src/omaha/base/extractor.h" | |
29 | |
30 namespace app_installer { | |
31 | |
32 enum ExitCode { | |
33 SUCCESS = 0, | |
34 COULD_NOT_GET_FILE_PATH, | |
35 COULD_NOT_READ_TAG, | |
36 INVALID_APP_ID, | |
37 EULA_CANCELLED, | |
38 COULD_NOT_FIND_CHROME, | |
39 COULD_NOT_GET_TMP_FILE_PATH, | |
40 FAILED_TO_DOWNLOAD_CHROME_SETUP, | |
41 FAILED_TO_LAUNCH_CHROME_SETUP, | |
42 }; | |
43 | |
44 namespace { | |
45 | |
46 // Log provider UUID. Required for logging to Sawbuck. | |
47 // {d82c3b59-bacd-4625-8282-4d570c4dad12} | |
48 DEFINE_GUID(kAppInstallerLogProvider, | |
49 0xd82c3b59, | |
50 0xbacd, | |
51 0x4625, | |
52 0x82, 0x82, 0x4d, 0x57, 0x0c, 0x4d, 0xad, 0x12); | |
53 | |
54 const char kEnvVariableUntrustedData[] = "GoogleUpdateUntrustedData"; | |
55 const int kUntrustedDataMaxLength = 4096; | |
56 | |
57 const char kInstallChromeApp[] = "install-chrome-app"; | |
58 | |
59 const wchar_t kDownloadAndEulaPage[] = | |
60 L"https://tools.google.com/dlpage/chromeappinstaller"; | |
61 | |
62 const wchar_t kSxSDownloadAndEulaPage[] = | |
63 L"https://tools.google.com/dlpage/chromeappinstaller?sxs=true"; | |
64 | |
65 const wchar_t kDialogDimensions[] = L"dialogWidth:750px;dialogHeight:500px"; | |
66 | |
67 // This uses HTMLDialog to show a Chrome download page as a modal dialog. | |
68 // The page includes the EULA and returns a download URL. | |
69 class DownloadAndEulaHTMLDialog { | |
70 public: | |
71 explicit DownloadAndEulaHTMLDialog(bool is_canary) { | |
72 dialog_.reset(installer::CreateNativeHTMLDialog( | |
73 is_canary ? kSxSDownloadAndEulaPage : kDownloadAndEulaPage, | |
74 base::string16())); | |
75 } | |
76 ~DownloadAndEulaHTMLDialog() {} | |
77 | |
78 // Shows the dialog and blocks for user input. Returns the string passed back | |
79 // by the web page via |window.returnValue|. | |
80 base::string16 ShowModal() { | |
81 Customizer customizer; | |
82 dialog_->ShowModal(NULL, &customizer); | |
83 return dialog_->GetExtraResult(); | |
84 } | |
85 | |
86 private: | |
87 class Customizer : public installer::HTMLDialog::CustomizationCallback { | |
88 public: | |
89 virtual void OnBeforeCreation(wchar_t** extra) OVERRIDE { | |
90 *extra = const_cast<wchar_t*>(kDialogDimensions); | |
91 } | |
92 | |
93 virtual void OnBeforeDisplay(void* window) OVERRIDE { | |
94 // Customize the window by removing the close button and replacing the | |
95 // existing 'e' icon with the standard informational icon. | |
96 if (!window) | |
97 return; | |
98 HWND top_window = static_cast<HWND>(window); | |
99 LONG_PTR style = GetWindowLongPtrW(top_window, GWL_STYLE); | |
100 SetWindowLongPtrW(top_window, GWL_STYLE, style & ~WS_SYSMENU); | |
101 HICON ico = LoadIcon(NULL, IDI_INFORMATION); | |
102 SendMessageW(top_window, WM_SETICON, ICON_SMALL, | |
103 reinterpret_cast<LPARAM>(ico)); | |
104 } | |
105 }; | |
106 | |
107 scoped_ptr<installer::HTMLDialog> dialog_; | |
108 DISALLOW_COPY_AND_ASSIGN(DownloadAndEulaHTMLDialog); | |
109 }; | |
110 | |
111 // Gets the tag appended to a file. This uses the same format as Omaha. | |
112 // Returns the empty string on failure. | |
113 std::string GetTag(const base::FilePath& file_name_path) { | |
114 base::string16 file_name = file_name_path.value(); | |
115 omaha::TagExtractor extractor; | |
116 if (!extractor.OpenFile(file_name.c_str())) | |
117 return std::string(); | |
118 | |
119 int tag_buffer_size = 0; | |
120 if (!extractor.ExtractTag(NULL, &tag_buffer_size) || tag_buffer_size <= 1) | |
121 return std::string(); | |
122 | |
123 scoped_ptr<char[]> tag_buffer(new char[tag_buffer_size]); | |
124 extractor.ExtractTag(tag_buffer.get(), &tag_buffer_size); | |
125 return std::string(tag_buffer.get(), tag_buffer_size - 1); | |
126 } | |
127 | |
128 bool IsNotPrintable(unsigned char c) { | |
129 return c < 32 || c >= 127; | |
palmer
2014/10/13 23:47:53
Nit: It feels better to stick to the standard ctyp
jackhou1
2014/10/14 01:13:55
Oo, nice. Done.
| |
130 } | |
131 | |
132 // Returns whether or not |s| consists of printable characters. | |
133 bool IsStringPrintable(const std::string& s) { | |
134 return std::find_if(s.begin(), s.end(), IsNotPrintable) == s.end(); | |
135 } | |
136 | |
137 bool IsIllegalUntrustedDataKeyChar(unsigned char c) { | |
138 return !(c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || | |
139 c >= '0' && c <= '9' || c == '-' || c == '_' || c == '$'); | |
140 } | |
141 | |
142 // Returns true if |key| from untrusted data is valid. | |
143 bool IsUntrustedDataKeyValid(const std::string& key) { | |
144 return std::find_if(key.begin(), key.end(), IsIllegalUntrustedDataKeyChar) | |
145 == key.end(); | |
146 } | |
147 | |
148 // Parses |data_string| as key-value pairs and overwrites |untrusted_data| with | |
palmer
2014/10/13 23:47:53
Document the format this function expects the key-
jackhou1
2014/10/14 01:13:55
Done.
| |
149 // the result. Returns true if the data could be parsed. | |
150 bool ParseUntrustedData( | |
151 const std::string& data_string, | |
152 std::map<std::string, std::string>* untrusted_data) { | |
palmer
2014/10/13 23:47:53
I'm confused about the terminology here. To securi
jackhou1
2014/10/14 01:13:55
This code, along with IsUntrustedDataKeyValid abov
| |
153 DCHECK(untrusted_data); | |
154 if (data_string.length() > kUntrustedDataMaxLength || | |
palmer
2014/10/13 23:47:54
Similarly, just |kMaxDataLength| or |kMaxValidData
jackhou1
2014/10/14 01:13:55
Done.
| |
155 !IsStringPrintable(data_string)) { | |
156 LOG(ERROR) << "Invalid value in untrusted data string."; | |
palmer
2014/10/13 23:47:54
Might be nice to log each failure separately, and
jackhou1
2014/10/14 01:13:55
Done.
| |
157 return false; | |
158 } | |
159 | |
160 DVLOG(1) << "Untrusted data string: " << data_string; | |
161 | |
162 std::vector<std::pair<std::string, std::string> > kv_pairs; | |
163 if (!base::SplitStringIntoKeyValuePairs(data_string, '=', '&', &kv_pairs)) { | |
164 LOG(ERROR) << "Failed to parse untrusted data: " << data_string; | |
165 return false; | |
166 } | |
167 | |
168 untrusted_data->clear(); | |
169 std::vector<std::pair<std::string, std::string> >::const_iterator it; | |
170 for (it = kv_pairs.begin(); it != kv_pairs.end(); ++it) { | |
171 const std::string& key(it->first); | |
172 const std::string& value(it->second); | |
173 if (IsUntrustedDataKeyValid(key) && IsStringPrintable(value)) | |
174 (*untrusted_data)[key] = value; | |
175 else | |
176 LOG(ERROR) << "Illegal character found in untrusted data."; | |
palmer
2014/10/13 23:47:53
Should return false here?
jackhou1
2014/10/14 01:13:55
Done.
| |
177 } | |
178 return true; | |
179 } | |
180 | |
181 // Returns the value corresponding to |key| in untrusted data passed from | |
182 // |tag|. |tag| should be a printable list of key-value pairs, e.g. | |
183 // "key1=value1&key2=value2". Returns an empty string if |key| is absent or if | |
184 // its value contains non-printable characters. | |
185 std::string GetUntrustedDataValueFromTag(const std::string& tag, | |
186 const std::string& key) { | |
187 std::map<std::string, std::string> untrusted_data; | |
188 if (ParseUntrustedData(tag, &untrusted_data)) | |
189 return untrusted_data[key]; | |
190 | |
191 return std::string(); | |
192 } | |
193 | |
194 bool IsValidAppId(const std::string& app_id) { | |
195 if (app_id.size() != 32) | |
196 return false; | |
197 | |
198 for (size_t i = 0; i < app_id.size(); ++i) { | |
199 char c = base::ToLowerASCII(app_id[i]); | |
200 if (c < 'a' || c > 'p') | |
201 return false; | |
202 } | |
203 | |
204 return true; | |
205 } | |
206 | |
207 base::FilePath GetChromeExePath(bool is_canary) { | |
208 return is_canary ? | |
209 chrome_launcher_support::GetAnyChromeSxSPath() : | |
210 chrome_launcher_support::GetAnyChromePath(); | |
211 } | |
212 | |
213 ExitCode GetChrome(bool is_canary) { | |
214 // Show UI to install Chrome. The UI returns a download URL. | |
215 base::string16 download_url = | |
216 DownloadAndEulaHTMLDialog(is_canary).ShowModal(); | |
217 if (download_url.empty()) | |
218 return EULA_CANCELLED; | |
219 | |
220 DVLOG(1) << "Chrome download url: " << download_url; | |
221 | |
222 // Get a temporary file path. | |
223 base::FilePath setup_file; | |
224 if (!base::CreateTemporaryFile(&setup_file)) | |
225 return COULD_NOT_GET_TMP_FILE_PATH; | |
226 | |
227 // Download the Chrome installer. | |
228 HRESULT hr = URLDownloadToFile( | |
229 NULL, download_url.c_str(), setup_file.value().c_str(), 0, NULL); | |
230 if (FAILED(hr)) { | |
231 LOG(ERROR) << "Download failed: Error " << std::hex << hr << ". " | |
232 << setup_file.value(); | |
233 return FAILED_TO_DOWNLOAD_CHROME_SETUP; | |
234 } | |
235 | |
236 // Install Chrome. Wait for the installer to finish before returning. | |
237 base::LaunchOptions options; | |
238 options.wait = true; | |
239 if (!base::LaunchProcess(CommandLine(setup_file), options, NULL)) { | |
240 base::DeleteFile(setup_file, false); | |
241 return FAILED_TO_LAUNCH_CHROME_SETUP; | |
242 } | |
243 | |
244 base::DeleteFile(setup_file, false); | |
245 return SUCCESS; | |
246 } | |
247 | |
248 } // namespace | |
249 | |
250 extern "C" | |
251 int WINAPI wWinMain(HINSTANCE instance, HINSTANCE prev_instance, | |
252 wchar_t* command_line, int show_command) { | |
253 base::AtExitManager exit_manager; | |
254 CommandLine::Init(0, NULL); | |
255 logging::LogEventProvider::Initialize(kAppInstallerLogProvider); | |
256 logging::LoggingSettings settings; | |
257 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; | |
258 logging::InitLogging(settings); | |
259 | |
260 std::string app_id = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
261 switches::kAppId); | |
262 const char* sxs = installer::switches::kChromeSxS; | |
263 // --chrome-sxs on the command line takes precedence over chrome-sxs in the | |
264 // tag. | |
265 bool is_canary = CommandLine::ForCurrentProcess()->HasSwitch(sxs); | |
266 | |
267 // --app-id on the command line inhibits tag parsing altogether. | |
268 if (app_id.empty()) { | |
269 base::FilePath current_exe; | |
270 if (!PathService::Get(base::FILE_EXE, ¤t_exe)) | |
271 return COULD_NOT_GET_FILE_PATH; | |
272 | |
273 std::string tag = GetTag(current_exe); | |
274 if (tag.empty()) | |
275 return COULD_NOT_READ_TAG; | |
276 | |
277 app_id = GetUntrustedDataValueFromTag(tag, switches::kAppId); | |
palmer
2014/10/13 23:47:54
Since you call |GetUntrustedDataValueFromTag| twic
jackhou1
2014/10/14 01:13:55
Done.
| |
278 | |
279 if (!is_canary) | |
280 is_canary = GetUntrustedDataValueFromTag(tag, sxs) == "1"; | |
281 } | |
282 | |
283 if (!IsValidAppId(app_id)) | |
284 return INVALID_APP_ID; | |
285 | |
286 base::FilePath chrome_path = GetChromeExePath(is_canary); | |
287 // If none found, show EULA, download, and install Chrome. | |
288 if (chrome_path.empty()) { | |
289 ExitCode get_chrome_result = GetChrome(is_canary); | |
290 if (get_chrome_result != SUCCESS) | |
291 return get_chrome_result; | |
292 | |
293 chrome_path = GetChromeExePath(is_canary); | |
294 if (chrome_path.empty()) | |
295 return COULD_NOT_FIND_CHROME; | |
296 } | |
297 | |
298 CommandLine cmd(chrome_path); | |
299 cmd.AppendSwitchASCII(kInstallChromeApp, app_id); | |
300 DVLOG(1) << "Install command: " << cmd.GetCommandLineString(); | |
301 bool launched = base::LaunchProcess(cmd, base::LaunchOptions(), NULL); | |
302 DVLOG(1) << "Launch " << (launched ? "success." : "failed."); | |
303 | |
304 return SUCCESS; | |
305 } | |
306 | |
307 } // namespace app_installer | |
OLD | NEW |