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 "chrome/app_installer/win/app_installer_util.h" | |
6 | |
7 #include <windows.h> | |
8 #include <urlmon.h> | |
9 #include <winhttp.h> | |
10 #pragma comment(lib, "urlmon.lib") | |
11 #pragma comment(lib, "winhttp.lib") | |
12 | |
13 #include "base/base_paths.h" | |
14 #include "base/basictypes.h" | |
15 #include "base/command_line.h" | |
16 #include "base/files/file_util.h" | |
17 #include "base/json/json_string_value_serializer.h" | |
18 #include "base/logging.h" | |
19 #include "base/memory/scoped_ptr.h" | |
20 #include "base/path_service.h" | |
21 #include "base/process/launch.h" | |
22 #include "base/strings/string16.h" | |
23 #include "base/strings/string_split.h" | |
24 #include "base/strings/string_util.h" | |
25 #include "base/strings/stringprintf.h" | |
26 #include "base/strings/sys_string_conversions.h" | |
27 #include "base/values.h" | |
28 #include "base/win/scoped_handle.h" | |
29 #include "chrome/common/chrome_switches.h" | |
30 #include "chrome/installer/launcher_support/chrome_launcher_support.h" | |
31 #include "chrome/installer/util/google_update_util.h" | |
32 #include "chrome/installer/util/html_dialog.h" | |
33 #include "chrome/installer/util/master_preferences_constants.h" | |
34 #include "chrome/installer/util/util_constants.h" | |
35 #include "net/base/escape.h" | |
36 #include "third_party/omaha/src/omaha/base/extractor.h" | |
37 | |
38 namespace app_installer { | |
39 | |
40 const char kInstallChromeApp[] = "install-chrome-app"; | |
41 | |
42 namespace { | |
43 | |
44 // Copied from google_chrome_distribution.cc. | |
45 const char kBrowserAppGuid[] = "{8A69D345-D564-463c-AFF1-A69D9E530F96}"; | |
46 | |
47 // Copied frome google_chrome_sxs_distribution.cc. | |
48 const char kSxSBrowserAppGuid[] = "{4ea16ac7-fd5a-47c3-875b-dbf4a2008c20}"; | |
49 | |
50 const int kMaxTagLength = 4096; | |
51 | |
52 const wchar_t kDownloadAndEulaPage[] = | |
53 L"https://tools.google.com/dlpage/chromeappinstaller"; | |
54 | |
55 const wchar_t kSxSDownloadAndEulaPage[] = | |
56 L"https://tools.google.com/dlpage/chromeappinstaller?sxs=true"; | |
57 | |
58 const wchar_t kDialogDimensions[] = L"dialogWidth:750px;dialogHeight:500px"; | |
59 | |
60 // This uses HTMLDialog to show a Chrome download page as a modal dialog. | |
61 // The page includes the EULA and returns a download URL. | |
62 class DownloadAndEulaHTMLDialog { | |
63 public: | |
64 explicit DownloadAndEulaHTMLDialog(bool is_canary, | |
65 const std::string& dialog_arguments) { | |
66 dialog_.reset(installer::CreateNativeHTMLDialog( | |
67 is_canary ? kSxSDownloadAndEulaPage : kDownloadAndEulaPage, | |
68 base::SysUTF8ToWide(dialog_arguments))); | |
69 } | |
70 ~DownloadAndEulaHTMLDialog() {} | |
71 | |
72 // Shows the dialog and blocks for user input. Returns the string passed back | |
73 // by the web page via |window.returnValue|. | |
74 base::string16 ShowModal() { | |
75 Customizer customizer; | |
76 dialog_->ShowModal(NULL, &customizer); | |
77 return dialog_->GetExtraResult(); | |
78 } | |
79 | |
80 private: | |
81 class Customizer : public installer::HTMLDialog::CustomizationCallback { | |
82 public: | |
83 void OnBeforeCreation(wchar_t** extra) override { | |
84 *extra = const_cast<wchar_t*>(kDialogDimensions); | |
85 } | |
86 | |
87 void OnBeforeDisplay(void* window) override { | |
88 // Customize the window by removing the close button and replacing the | |
89 // existing 'e' icon with the standard informational icon. | |
90 if (!window) | |
91 return; | |
92 HWND top_window = static_cast<HWND>(window); | |
93 LONG_PTR style = GetWindowLongPtrW(top_window, GWL_STYLE); | |
94 SetWindowLongPtrW(top_window, GWL_STYLE, style & ~WS_SYSMENU); | |
95 HICON ico = LoadIcon(NULL, IDI_INFORMATION); | |
96 SendMessageW( | |
97 top_window, WM_SETICON, ICON_SMALL, reinterpret_cast<LPARAM>(ico)); | |
98 } | |
99 }; | |
100 | |
101 scoped_ptr<installer::HTMLDialog> dialog_; | |
102 DISALLOW_COPY_AND_ASSIGN(DownloadAndEulaHTMLDialog); | |
103 }; | |
104 | |
105 bool IsStringPrintable(const std::string& s) { | |
106 return std::find_if_not(s.begin(), s.end(), isprint) == s.end(); | |
107 } | |
108 | |
109 bool IsLegalDataKeyChar(int c) { | |
110 return isalnum(c) || c == '-' || c == '_' || c == '$'; | |
111 } | |
112 | |
113 bool IsDataKeyValid(const std::string& key) { | |
114 return std::find_if_not(key.begin(), key.end(), IsLegalDataKeyChar) == | |
115 key.end(); | |
116 } | |
117 | |
118 struct WinHttpHandleTraits { | |
119 typedef HINTERNET Handle; | |
120 static bool CloseHandle(Handle handle) { | |
121 return WinHttpCloseHandle(handle) != FALSE; | |
122 } | |
123 static bool IsHandleValid(Handle handle) { return !!handle; } | |
124 static Handle NullHandle() { return 0; } | |
125 | |
126 private: | |
127 DISALLOW_IMPLICIT_CONSTRUCTORS(WinHttpHandleTraits); | |
128 }; | |
129 typedef base::win::GenericScopedHandle<WinHttpHandleTraits, | |
130 base::win::DummyVerifierTraits> | |
131 ScopedWinHttpHandle; | |
132 | |
133 // Reads the data portion of an HTTP response. WinHttpReceiveResponse() must | |
134 // have already succeeded on this request. | |
135 bool ReadHttpData(HINTERNET request_handle, | |
136 std::vector<uint8_t>* response_data) { | |
137 BOOL result = TRUE; | |
138 do { | |
139 // Check for available data. | |
140 DWORD bytes_available = 0; | |
141 result = WinHttpQueryDataAvailable(request_handle, &bytes_available); | |
142 if (!result) { | |
143 PLOG(ERROR); | |
144 break; | |
145 } | |
146 | |
147 if (bytes_available == 0) | |
148 break; | |
149 | |
150 do { | |
151 // Allocate space for the buffer. | |
152 size_t offset = response_data->size(); | |
153 response_data->resize(offset + bytes_available); | |
154 | |
155 // Read the data. | |
156 DWORD bytes_read = 0; | |
157 result = WinHttpReadData(request_handle, &((*response_data)[offset]), | |
158 bytes_available, &bytes_read); | |
159 // MSDN for WinHttpQueryDataAvailable says: | |
160 // The amount of data that remains is not recalculated until all | |
161 // available data indicated by the call to WinHttpQueryDataAvailable is | |
162 // read. | |
163 // So we should either read all of |bytes_available| or bail out here. | |
164 if (!result) { | |
165 PLOG(ERROR); | |
166 response_data->resize(offset); | |
167 return false; | |
168 } | |
169 | |
170 // MSDN for WinHttpReadData says: | |
171 // If you are using WinHttpReadData synchronously, and the return value | |
172 // is TRUE and the number of bytes read is zero, the transfer has been | |
173 // completed and there are no more bytes to read on the handle. | |
174 // Not sure if that's possible while |bytes_available| > 0, but better to | |
175 // check and break out of both loops in this case. | |
176 if (!bytes_read) { | |
177 response_data->resize(offset); | |
178 return true; | |
179 } | |
180 | |
181 response_data->resize(offset + bytes_read); | |
182 bytes_available -= bytes_read; | |
183 } while (bytes_available); | |
184 } while (true); | |
185 | |
186 return result != FALSE; | |
187 } | |
188 | |
189 } // namespace | |
190 | |
191 // Gets the tag attached to a file by dl.google.com. This uses the same format | |
192 // as Omaha. Returns the empty string on failure. | |
193 std::string GetTag(const base::FilePath& file_name_path) { | |
194 base::string16 file_name = file_name_path.value(); | |
195 omaha::TagExtractor extractor; | |
196 if (!extractor.OpenFile(file_name.c_str())) | |
197 return std::string(); | |
198 | |
199 int tag_buffer_size = 0; | |
200 if (!extractor.ExtractTag(NULL, &tag_buffer_size) || tag_buffer_size <= 1) | |
201 return std::string(); | |
202 | |
203 if (tag_buffer_size - 1 > kMaxTagLength) { | |
204 LOG(ERROR) << "Tag length (" << tag_buffer_size - 1 << ") exceeds maximum (" | |
205 << kMaxTagLength << ")."; | |
206 return std::string(); | |
207 } | |
208 | |
209 scoped_ptr<char[]> tag_buffer(new char[tag_buffer_size]); | |
210 extractor.ExtractTag(tag_buffer.get(), &tag_buffer_size); | |
211 | |
212 return std::string(tag_buffer.get(), tag_buffer_size - 1); | |
213 } | |
214 | |
215 // Parses |tag| as key-value pairs and overwrites |parsed_pairs| with | |
216 // the result. |tag| should be a '&'-delimited list of '='-separated | |
217 // key-value pairs, e.g. "key1=value1&key2=value2". | |
218 // Returns true if the data could be parsed. | |
219 bool ParseTag(const std::string& tag, | |
220 std::map<std::string, std::string>* parsed_pairs) { | |
221 DCHECK(parsed_pairs); | |
222 | |
223 base::StringPairs kv_pairs; | |
224 if (!base::SplitStringIntoKeyValuePairs(tag, '=', '&', &kv_pairs)) { | |
225 LOG(ERROR) << "Failed to parse tag: " << tag; | |
226 return false; | |
227 } | |
228 | |
229 parsed_pairs->clear(); | |
230 for (const auto& pair : kv_pairs) { | |
231 const std::string& key(pair.first); | |
232 const std::string& value(pair.second); | |
233 if (IsDataKeyValid(key) && IsStringPrintable(value)) { | |
234 (*parsed_pairs)[key] = value; | |
235 } else { | |
236 LOG(ERROR) << "Illegal character found in tag."; | |
237 return false; | |
238 } | |
239 } | |
240 return true; | |
241 } | |
242 | |
243 bool IsValidAppId(const std::string& app_id) { | |
244 if (app_id.size() != 32) | |
245 return false; | |
246 | |
247 for (size_t i = 0; i < app_id.size(); ++i) { | |
248 char c = base::ToLowerASCII(app_id[i]); | |
249 if (c < 'a' || c > 'p') | |
250 return false; | |
251 } | |
252 | |
253 return true; | |
254 } | |
255 | |
256 bool FetchUrl(const base::string16& user_agent, | |
257 const base::string16& server_name, | |
258 uint16_t server_port, | |
259 const base::string16& object_name, | |
260 std::vector<uint8_t>* response_data) { | |
261 DCHECK(response_data); | |
262 | |
263 ScopedWinHttpHandle session_handle( | |
264 WinHttpOpen(user_agent.c_str(), WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, | |
265 WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0)); | |
266 if (!session_handle.IsValid()) { | |
267 PLOG(ERROR); | |
268 return false; | |
269 } | |
270 | |
271 ScopedWinHttpHandle connection_handle(WinHttpConnect( | |
272 session_handle.Get(), server_name.c_str(), server_port, 0)); | |
273 if (!connection_handle.IsValid()) { | |
274 PLOG(ERROR); | |
275 return false; | |
276 } | |
277 | |
278 ScopedWinHttpHandle request_handle(WinHttpOpenRequest( | |
279 connection_handle.Get(), L"GET", object_name.c_str(), NULL, | |
280 WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE)); | |
281 if (!request_handle.IsValid()) { | |
282 PLOG(ERROR); | |
283 return false; | |
284 } | |
285 | |
286 if (!WinHttpSendRequest(request_handle.Get(), WINHTTP_NO_ADDITIONAL_HEADERS, | |
287 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0)) { | |
288 PLOG(ERROR); | |
289 return false; | |
290 } | |
291 | |
292 if (!WinHttpReceiveResponse(request_handle.Get(), NULL)) { | |
293 PLOG(ERROR); | |
294 return false; | |
295 } | |
296 | |
297 response_data->clear(); | |
298 return ReadHttpData(request_handle.Get(), response_data); | |
299 } | |
300 | |
301 ExitCode GetChrome(bool is_canary, const std::string& inline_install_json) { | |
302 // Show UI to install Chrome. The UI returns a download URL. | |
303 base::string16 download_url = | |
304 DownloadAndEulaHTMLDialog(is_canary, inline_install_json).ShowModal(); | |
305 if (download_url.empty()) | |
306 return EULA_CANCELLED; | |
307 | |
308 DVLOG(1) << "Chrome download url: " << download_url; | |
309 | |
310 // Get a temporary file path. | |
311 base::FilePath setup_file; | |
312 if (!base::CreateTemporaryFile(&setup_file)) | |
313 return COULD_NOT_GET_TMP_FILE_PATH; | |
314 | |
315 // Download the Chrome installer. | |
316 HRESULT hr = URLDownloadToFile( | |
317 NULL, download_url.c_str(), setup_file.value().c_str(), 0, NULL); | |
318 if (FAILED(hr)) { | |
319 LOG(ERROR) << "Download failed: Error " << std::hex << hr << ". " | |
320 << setup_file.value(); | |
321 return FAILED_TO_DOWNLOAD_CHROME_SETUP; | |
322 } | |
323 | |
324 // Construct the command line to pass to the installer so that it will not | |
325 // launch Chrome upon completion. | |
326 base::DictionaryValue installerdata_dict; | |
327 base::DictionaryValue* distribution = new base::DictionaryValue(); | |
328 installerdata_dict.Set("distribution", distribution); | |
329 distribution->SetBoolean( | |
330 installer::master_preferences::kCreateAllShortcuts, false); | |
331 distribution->SetBoolean( | |
332 installer::master_preferences::kDoNotCreateDesktopShortcut, true); | |
333 distribution->SetBoolean( | |
334 installer::master_preferences::kDoNotCreateQuickLaunchShortcut, true); | |
335 distribution->SetBoolean( | |
336 installer::master_preferences::kDoNotCreateTaskbarShortcut, true); | |
337 distribution->SetBoolean( | |
338 installer::master_preferences::kDoNotLaunchChrome, true); | |
339 distribution->SetBoolean( | |
340 installer::master_preferences::kDoNotRegisterForUpdateLaunch, true); | |
341 distribution->SetBoolean( | |
342 installer::master_preferences::kDistroImportHistoryPref, false); | |
343 distribution->SetBoolean( | |
344 installer::master_preferences::kDistroImportSearchPref, false); | |
345 distribution->SetBoolean( | |
346 installer::master_preferences::kMakeChromeDefault, false); | |
347 distribution->SetBoolean( | |
348 installer::master_preferences::kSuppressFirstRunDefaultBrowserPrompt, | |
349 true); | |
350 std::string installerdata; | |
351 JSONStringValueSerializer serializer(&installerdata); | |
352 bool serialize_success = serializer.Serialize(installerdata_dict); | |
353 DCHECK(serialize_success); | |
354 std::string installerdata_url_encoded = | |
355 net::EscapeQueryParamValue(installerdata, false); | |
356 std::string appargs = | |
357 base::StringPrintf("appguid=%s&installerdata=%s", | |
358 is_canary ? kSxSBrowserAppGuid : kBrowserAppGuid, | |
359 installerdata_url_encoded.c_str()); | |
360 base::CommandLine command_line(setup_file); | |
361 command_line.AppendArg("/appargs"); | |
362 command_line.AppendArg(appargs); | |
363 command_line.AppendArg("/install"); // Must be last. | |
364 | |
365 DVLOG(1) << "Chrome installer command line: " | |
366 << command_line.GetCommandLineString(); | |
367 | |
368 // Install Chrome. Wait for the installer to finish before returning. | |
369 base::LaunchOptions options; | |
370 options.wait = true; | |
371 bool launch_success = base::LaunchProcess(command_line, options).IsValid(); | |
372 base::DeleteFile(setup_file, false); | |
373 return launch_success ? SUCCESS : FAILED_TO_LAUNCH_CHROME_SETUP; | |
374 } | |
375 | |
376 } // namespace app_installer | |
OLD | NEW |