OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <windows.h> | 5 #include <windows.h> |
6 #include <initguid.h> | 6 #include <initguid.h> |
7 #include <urlmon.h> | |
8 #pragma comment(lib, "urlmon.lib") | |
9 | 7 |
10 #include "base/at_exit.h" | 8 #include "base/at_exit.h" |
11 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
12 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
13 #include "base/command_line.h" | 11 #include "base/command_line.h" |
14 #include "base/files/file_util.h" | |
15 #include "base/logging.h" | 12 #include "base/logging.h" |
16 #include "base/logging_win.h" | 13 #include "base/logging_win.h" |
17 #include "base/memory/scoped_ptr.h" | |
18 #include "base/path_service.h" | 14 #include "base/path_service.h" |
19 #include "base/process/launch.h" | 15 #include "base/process/launch.h" |
20 #include "base/strings/string16.h" | 16 #include "chrome/app_installer/win/app_installer_util.h" |
21 #include "base/strings/string_split.h" | |
22 #include "base/strings/string_util.h" | |
23 #include "chrome/common/chrome_switches.h" | 17 #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" | 18 #include "chrome/installer/util/util_constants.h" |
28 #include "third_party/omaha/src/omaha/base/extractor.h" | |
29 | 19 |
30 namespace app_installer { | 20 namespace app_installer { |
31 | 21 |
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 { | 22 namespace { |
46 | 23 |
47 // Log provider UUID. Required for logging to Sawbuck. | 24 // Log provider UUID. Required for logging to Sawbuck. |
48 // {d82c3b59-bacd-4625-8282-4d570c4dad12} | 25 // {d82c3b59-bacd-4625-8282-4d570c4dad12} |
49 DEFINE_GUID(kAppInstallerLogProvider, | 26 DEFINE_GUID(kAppInstallerLogProvider, |
50 0xd82c3b59, | 27 0xd82c3b59, |
51 0xbacd, | 28 0xbacd, |
52 0x4625, | 29 0x4625, |
53 0x82, 0x82, 0x4d, 0x57, 0x0c, 0x4d, 0xad, 0x12); | 30 0x82, 0x82, 0x4d, 0x57, 0x0c, 0x4d, 0xad, 0x12); |
54 | 31 |
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 void OnBeforeCreation(wchar_t** extra) override { | |
90 *extra = const_cast<wchar_t*>(kDialogDimensions); | |
91 } | |
92 | |
93 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( | |
103 top_window, WM_SETICON, ICON_SMALL, 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 if (tag_buffer_size - 1 > kMaxTagLength) { | |
124 LOG(ERROR) << "Tag length (" << tag_buffer_size - 1 << ") exceeds maximum (" | |
125 << kMaxTagLength << ")."; | |
126 return std::string(); | |
127 } | |
128 | |
129 scoped_ptr<char[]> tag_buffer(new char[tag_buffer_size]); | |
130 extractor.ExtractTag(tag_buffer.get(), &tag_buffer_size); | |
131 | |
132 return std::string(tag_buffer.get(), tag_buffer_size - 1); | |
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(const std::string& tag, | |
153 std::map<std::string, std::string>* parsed_pairs) { | |
154 DCHECK(parsed_pairs); | |
155 | |
156 base::StringPairs kv_pairs; | |
157 if (!base::SplitStringIntoKeyValuePairs(tag, '=', '&', &kv_pairs)) { | |
158 LOG(ERROR) << "Failed to parse tag: " << tag; | |
159 return false; | |
160 } | |
161 | |
162 parsed_pairs->clear(); | |
163 for (const auto& pair : kv_pairs) { | |
164 const std::string& key(pair.first); | |
165 const std::string& value(pair.second); | |
166 if (IsDataKeyValid(key) && IsStringPrintable(value)) { | |
167 (*parsed_pairs)[key] = value; | |
168 } else { | |
169 LOG(ERROR) << "Illegal character found in tag."; | |
170 return false; | |
171 } | |
172 } | |
173 return true; | |
174 } | |
175 | |
176 bool IsValidAppId(const std::string& app_id) { | |
177 if (app_id.size() != 32) | |
178 return false; | |
179 | |
180 for (size_t i = 0; i < app_id.size(); ++i) { | |
181 char c = base::ToLowerASCII(app_id[i]); | |
182 if (c < 'a' || c > 'p') | |
183 return false; | |
184 } | |
185 | |
186 return true; | |
187 } | |
188 | |
189 base::FilePath GetChromeExePath(bool is_canary) { | |
190 return is_canary ? chrome_launcher_support::GetAnyChromeSxSPath() | |
191 : chrome_launcher_support::GetAnyChromePath(); | |
192 } | |
193 | |
194 ExitCode GetChrome(bool is_canary) { | |
195 // Show UI to install Chrome. The UI returns a download URL. | |
196 base::string16 download_url = | |
197 DownloadAndEulaHTMLDialog(is_canary).ShowModal(); | |
198 if (download_url.empty()) | |
199 return EULA_CANCELLED; | |
200 | |
201 DVLOG(1) << "Chrome download url: " << download_url; | |
202 | |
203 // Get a temporary file path. | |
204 base::FilePath setup_file; | |
205 if (!base::CreateTemporaryFile(&setup_file)) | |
206 return COULD_NOT_GET_TMP_FILE_PATH; | |
207 | |
208 // Download the Chrome installer. | |
209 HRESULT hr = URLDownloadToFile( | |
210 NULL, download_url.c_str(), setup_file.value().c_str(), 0, NULL); | |
211 if (FAILED(hr)) { | |
212 LOG(ERROR) << "Download failed: Error " << std::hex << hr << ". " | |
213 << setup_file.value(); | |
214 return FAILED_TO_DOWNLOAD_CHROME_SETUP; | |
215 } | |
216 | |
217 // Install Chrome. Wait for the installer to finish before returning. | |
218 base::LaunchOptions options; | |
219 options.wait = true; | |
220 bool launch_success = | |
221 base::LaunchProcess(base::CommandLine(setup_file), options, NULL); | |
222 base::DeleteFile(setup_file, false); | |
223 return launch_success ? SUCCESS : FAILED_TO_LAUNCH_CHROME_SETUP; | |
224 } | |
225 | |
226 } // namespace | 32 } // namespace |
227 | 33 |
228 extern "C" | 34 extern "C" |
229 int WINAPI wWinMain(HINSTANCE instance, | 35 int WINAPI wWinMain(HINSTANCE instance, |
230 HINSTANCE prev_instance, | 36 HINSTANCE prev_instance, |
231 wchar_t* command_line, | 37 wchar_t* command_line, |
232 int show_command) { | 38 int show_command) { |
233 base::AtExitManager exit_manager; | 39 base::AtExitManager exit_manager; |
234 base::CommandLine::Init(0, NULL); | 40 base::CommandLine::Init(0, NULL); |
235 logging::LogEventProvider::Initialize(kAppInstallerLogProvider); | 41 logging::LogEventProvider::Initialize(kAppInstallerLogProvider); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 base::CommandLine cmd(chrome_path); | 98 base::CommandLine cmd(chrome_path); |
293 cmd.AppendSwitchASCII(kInstallChromeApp, app_id); | 99 cmd.AppendSwitchASCII(kInstallChromeApp, app_id); |
294 DVLOG(1) << "Install command: " << cmd.GetCommandLineString(); | 100 DVLOG(1) << "Install command: " << cmd.GetCommandLineString(); |
295 bool launched = base::LaunchProcess(cmd, base::LaunchOptions(), NULL); | 101 bool launched = base::LaunchProcess(cmd, base::LaunchOptions(), NULL); |
296 DVLOG(1) << "Launch " << (launched ? "success." : "failed."); | 102 DVLOG(1) << "Launch " << (launched ? "success." : "failed."); |
297 | 103 |
298 return SUCCESS; | 104 return SUCCESS; |
299 } | 105 } |
300 | 106 |
301 } // namespace app_installer | 107 } // namespace app_installer |
OLD | NEW |