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/files/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 COULD_NOT_PARSE_TAG, | |
37 INVALID_APP_ID, | |
38 EULA_CANCELLED, | |
39 COULD_NOT_FIND_CHROME, | |
40 COULD_NOT_GET_TMP_FILE_PATH, | |
41 FAILED_TO_DOWNLOAD_CHROME_SETUP, | |
42 FAILED_TO_LAUNCH_CHROME_SETUP, | |
43 }; | |
44 | |
45 namespace { | |
46 | |
47 // Log provider UUID. Required for logging to Sawbuck. | |
48 // {d82c3b59-bacd-4625-8282-4d570c4dad12} | |
49 DEFINE_GUID(kAppInstallerLogProvider, | |
50 0xd82c3b59, | |
51 0xbacd, | |
52 0x4625, | |
53 0x82, 0x82, 0x4d, 0x57, 0x0c, 0x4d, 0xad, 0x12); | |
54 | |
55 const int kMaxTagLength = 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 attached to a file by dl.google.com. This uses the same format | |
112 // as Omaha. 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 | |
126 --tag_buffer_size; // Disregard trailing '\0'. | |
127 if (tag_buffer_size > kMaxTagLength) { | |
grt (UTC plus 2)
2014/10/14 13:04:08
can't this check be moved up to line 122 so that i
jackhou1
2014/10/14 23:59:39
Done.
| |
128 LOG(ERROR) << "Tag length (" << tag_buffer_size << ") exceeds maximum (" | |
129 << kMaxTagLength << ")."; | |
130 return std::string(); | |
131 } | |
132 return std::string(tag_buffer.get(), tag_buffer_size); | |
133 } | |
134 | |
135 bool IsStringPrintable(const std::string& s) { | |
136 return std::find_if_not(s.begin(), s.end(), isprint) == s.end(); | |
137 } | |
138 | |
139 bool IsLegalDataKeyChar(int c) { | |
140 return isalnum(c) || c == '-' || c == '_' || c == '$'; | |
141 } | |
142 | |
143 bool IsDataKeyValid(const std::string& key) { | |
144 return std::find_if_not(key.begin(), key.end(), IsLegalDataKeyChar) == | |
145 key.end(); | |
146 } | |
147 | |
148 // Parses |tag| as key-value pairs and overwrites |parsed_pairs| with | |
149 // the result. |tag| should be a '&'-delimited list of '='-separated | |
150 // key-value pairs, e.g. "key1=value1&key2=value2". | |
151 // Returns true if the data could be parsed. | |
152 bool ParseTag( | |
153 const std::string& tag, | |
154 std::map<std::string, std::string>* parsed_pairs) { | |
155 DCHECK(parsed_pairs); | |
156 | |
157 std::vector<std::pair<std::string, std::string> > kv_pairs; | |
158 if (!base::SplitStringIntoKeyValuePairs(tag, '=', '&', &kv_pairs)) { | |
159 LOG(ERROR) << "Failed to parse tag: " << tag; | |
160 return false; | |
161 } | |
162 | |
163 parsed_pairs->clear(); | |
164 std::vector<std::pair<std::string, std::string> >::const_iterator it; | |
165 for (it = kv_pairs.begin(); it != kv_pairs.end(); ++it) { | |
grt (UTC plus 2)
2014/10/14 13:04:08
this can now be simplified to:
for (const auto&
jackhou1
2014/10/14 23:59:39
Done.
(Yay C++11!)
| |
166 const std::string& key(it->first); | |
167 const std::string& value(it->second); | |
168 if (IsDataKeyValid(key) && IsStringPrintable(value)) { | |
169 (*parsed_pairs)[key] = value; | |
170 } else { | |
171 LOG(ERROR) << "Illegal character found in tag."; | |
172 return false; | |
173 } | |
174 } | |
175 return true; | |
176 } | |
177 | |
178 bool IsValidAppId(const std::string& app_id) { | |
179 if (app_id.size() != 32) | |
180 return false; | |
181 | |
182 for (size_t i = 0; i < app_id.size(); ++i) { | |
183 char c = base::ToLowerASCII(app_id[i]); | |
184 if (c < 'a' || c > 'p') | |
185 return false; | |
186 } | |
187 | |
188 return true; | |
189 } | |
190 | |
191 base::FilePath GetChromeExePath(bool is_canary) { | |
192 return is_canary ? | |
193 chrome_launcher_support::GetAnyChromeSxSPath() : | |
194 chrome_launcher_support::GetAnyChromePath(); | |
195 } | |
196 | |
197 ExitCode GetChrome(bool is_canary) { | |
198 // Show UI to install Chrome. The UI returns a download URL. | |
199 base::string16 download_url = | |
200 DownloadAndEulaHTMLDialog(is_canary).ShowModal(); | |
201 if (download_url.empty()) | |
202 return EULA_CANCELLED; | |
203 | |
204 DVLOG(1) << "Chrome download url: " << download_url; | |
205 | |
206 // Get a temporary file path. | |
207 base::FilePath setup_file; | |
208 if (!base::CreateTemporaryFile(&setup_file)) | |
209 return COULD_NOT_GET_TMP_FILE_PATH; | |
210 | |
211 // Download the Chrome installer. | |
212 HRESULT hr = URLDownloadToFile( | |
213 NULL, download_url.c_str(), setup_file.value().c_str(), 0, NULL); | |
214 if (FAILED(hr)) { | |
215 LOG(ERROR) << "Download failed: Error " << std::hex << hr << ". " | |
216 << setup_file.value(); | |
217 return FAILED_TO_DOWNLOAD_CHROME_SETUP; | |
218 } | |
219 | |
220 // Install Chrome. Wait for the installer to finish before returning. | |
221 base::LaunchOptions options; | |
222 options.wait = true; | |
223 if (!base::LaunchProcess(CommandLine(setup_file), options, NULL)) { | |
224 base::DeleteFile(setup_file, false); | |
225 return FAILED_TO_LAUNCH_CHROME_SETUP; | |
226 } | |
227 | |
228 base::DeleteFile(setup_file, false); | |
229 return SUCCESS; | |
230 } | |
231 | |
232 } // namespace | |
233 | |
234 extern "C" | |
235 int WINAPI wWinMain(HINSTANCE instance, HINSTANCE prev_instance, | |
236 wchar_t* command_line, int show_command) { | |
237 base::AtExitManager exit_manager; | |
238 CommandLine::Init(0, NULL); | |
239 logging::LogEventProvider::Initialize(kAppInstallerLogProvider); | |
240 logging::LoggingSettings settings; | |
241 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; | |
242 logging::InitLogging(settings); | |
243 | |
244 std::string app_id = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
245 switches::kAppId); | |
246 const char* sxs = installer::switches::kChromeSxS; | |
247 // --chrome-sxs on the command line takes precedence over chrome-sxs in the | |
248 // tag. | |
249 bool is_canary = CommandLine::ForCurrentProcess()->HasSwitch(sxs); | |
250 | |
251 // --app-id on the command line inhibits tag parsing altogether. | |
252 if (app_id.empty()) { | |
253 base::FilePath current_exe; | |
254 if (!PathService::Get(base::FILE_EXE, ¤t_exe)) | |
255 return COULD_NOT_GET_FILE_PATH; | |
256 | |
257 // Get the tag added by dl.google.com. Note that this is passed in via URL | |
258 // parameters when requesting a file to download, so it must be validated | |
259 // before use. | |
260 std::string tag = GetTag(current_exe); | |
261 if (tag.empty()) | |
262 return COULD_NOT_READ_TAG; | |
263 | |
264 DVLOG(1) << "Tag: " << tag; | |
265 | |
266 std::map<std::string, std::string> parsed_pairs; | |
267 if (!ParseTag(tag, &parsed_pairs)) { | |
grt (UTC plus 2)
2014/10/14 13:04:08
nit: omit braces
jackhou1
2014/10/14 23:59:39
Done.
| |
268 return COULD_NOT_PARSE_TAG; | |
269 } | |
270 | |
271 app_id = parsed_pairs[switches::kAppId]; | |
272 | |
273 if (!is_canary) | |
274 is_canary = parsed_pairs[sxs] == "1"; | |
275 } | |
276 | |
277 if (!IsValidAppId(app_id)) | |
278 return INVALID_APP_ID; | |
279 | |
280 base::FilePath chrome_path = GetChromeExePath(is_canary); | |
281 // If none found, show EULA, download, and install Chrome. | |
282 if (chrome_path.empty()) { | |
283 ExitCode get_chrome_result = GetChrome(is_canary); | |
284 if (get_chrome_result != SUCCESS) | |
285 return get_chrome_result; | |
286 | |
287 chrome_path = GetChromeExePath(is_canary); | |
288 if (chrome_path.empty()) | |
289 return COULD_NOT_FIND_CHROME; | |
290 } | |
291 | |
292 CommandLine cmd(chrome_path); | |
293 cmd.AppendSwitchASCII(kInstallChromeApp, app_id); | |
294 DVLOG(1) << "Install command: " << cmd.GetCommandLineString(); | |
295 bool launched = base::LaunchProcess(cmd, base::LaunchOptions(), NULL); | |
296 DVLOG(1) << "Launch " << (launched ? "success." : "failed."); | |
297 | |
298 return SUCCESS; | |
299 } | |
300 | |
301 } // namespace app_installer | |
OLD | NEW |