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 "chrome/app_installer/win/app_installer_util.h" |
| 6 |
5 #include <windows.h> | 7 #include <windows.h> |
6 #include <initguid.h> | |
7 #include <urlmon.h> | 8 #include <urlmon.h> |
8 #pragma comment(lib, "urlmon.lib") | 9 #pragma comment(lib, "urlmon.lib") |
9 | 10 |
10 #include "base/at_exit.h" | |
11 #include "base/base_paths.h" | 11 #include "base/base_paths.h" |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/logging_win.h" | |
17 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
18 #include "base/path_service.h" | 17 #include "base/path_service.h" |
19 #include "base/process/launch.h" | 18 #include "base/process/launch.h" |
20 #include "base/strings/string16.h" | 19 #include "base/strings/string16.h" |
21 #include "base/strings/string_split.h" | 20 #include "base/strings/string_split.h" |
22 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
23 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
24 #include "chrome/installer/launcher_support/chrome_launcher_support.h" | 23 #include "chrome/installer/launcher_support/chrome_launcher_support.h" |
25 #include "chrome/installer/util/google_update_util.h" | 24 #include "chrome/installer/util/google_update_util.h" |
26 #include "chrome/installer/util/html_dialog.h" | 25 #include "chrome/installer/util/html_dialog.h" |
27 #include "chrome/installer/util/util_constants.h" | 26 #include "chrome/installer/util/util_constants.h" |
28 #include "third_party/omaha/src/omaha/base/extractor.h" | 27 #include "third_party/omaha/src/omaha/base/extractor.h" |
29 | 28 |
30 namespace app_installer { | 29 namespace app_installer { |
31 | 30 |
32 enum ExitCode { | 31 const char kInstallChromeApp[] = "install-chrome-app"; |
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 | 32 |
45 namespace { | 33 namespace { |
46 | 34 |
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; | 35 const int kMaxTagLength = 4096; |
56 | 36 |
57 const char kInstallChromeApp[] = "install-chrome-app"; | |
58 | |
59 const wchar_t kDownloadAndEulaPage[] = | 37 const wchar_t kDownloadAndEulaPage[] = |
60 L"https://tools.google.com/dlpage/chromeappinstaller"; | 38 L"https://tools.google.com/dlpage/chromeappinstaller"; |
61 | 39 |
62 const wchar_t kSxSDownloadAndEulaPage[] = | 40 const wchar_t kSxSDownloadAndEulaPage[] = |
63 L"https://tools.google.com/dlpage/chromeappinstaller?sxs=true"; | 41 L"https://tools.google.com/dlpage/chromeappinstaller?sxs=true"; |
64 | 42 |
65 const wchar_t kDialogDimensions[] = L"dialogWidth:750px;dialogHeight:500px"; | 43 const wchar_t kDialogDimensions[] = L"dialogWidth:750px;dialogHeight:500px"; |
66 | 44 |
67 // This uses HTMLDialog to show a Chrome download page as a modal dialog. | 45 // This uses HTMLDialog to show a Chrome download page as a modal dialog. |
68 // The page includes the EULA and returns a download URL. | 46 // The page includes the EULA and returns a download URL. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 HICON ico = LoadIcon(NULL, IDI_INFORMATION); | 79 HICON ico = LoadIcon(NULL, IDI_INFORMATION); |
102 SendMessageW( | 80 SendMessageW( |
103 top_window, WM_SETICON, ICON_SMALL, reinterpret_cast<LPARAM>(ico)); | 81 top_window, WM_SETICON, ICON_SMALL, reinterpret_cast<LPARAM>(ico)); |
104 } | 82 } |
105 }; | 83 }; |
106 | 84 |
107 scoped_ptr<installer::HTMLDialog> dialog_; | 85 scoped_ptr<installer::HTMLDialog> dialog_; |
108 DISALLOW_COPY_AND_ASSIGN(DownloadAndEulaHTMLDialog); | 86 DISALLOW_COPY_AND_ASSIGN(DownloadAndEulaHTMLDialog); |
109 }; | 87 }; |
110 | 88 |
| 89 bool IsStringPrintable(const std::string& s) { |
| 90 return std::find_if_not(s.begin(), s.end(), isprint) == s.end(); |
| 91 } |
| 92 |
| 93 bool IsLegalDataKeyChar(int c) { |
| 94 return isalnum(c) || c == '-' || c == '_' || c == '$'; |
| 95 } |
| 96 |
| 97 bool IsDataKeyValid(const std::string& key) { |
| 98 return std::find_if_not(key.begin(), key.end(), IsLegalDataKeyChar) == |
| 99 key.end(); |
| 100 } |
| 101 |
| 102 } // namespace |
| 103 |
111 // Gets the tag attached to a file by dl.google.com. This uses the same format | 104 // 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. | 105 // as Omaha. Returns the empty string on failure. |
113 std::string GetTag(const base::FilePath& file_name_path) { | 106 std::string GetTag(const base::FilePath& file_name_path) { |
114 base::string16 file_name = file_name_path.value(); | 107 base::string16 file_name = file_name_path.value(); |
115 omaha::TagExtractor extractor; | 108 omaha::TagExtractor extractor; |
116 if (!extractor.OpenFile(file_name.c_str())) | 109 if (!extractor.OpenFile(file_name.c_str())) |
117 return std::string(); | 110 return std::string(); |
118 | 111 |
119 int tag_buffer_size = 0; | 112 int tag_buffer_size = 0; |
120 if (!extractor.ExtractTag(NULL, &tag_buffer_size) || tag_buffer_size <= 1) | 113 if (!extractor.ExtractTag(NULL, &tag_buffer_size) || tag_buffer_size <= 1) |
121 return std::string(); | 114 return std::string(); |
122 | 115 |
123 if (tag_buffer_size - 1 > kMaxTagLength) { | 116 if (tag_buffer_size - 1 > kMaxTagLength) { |
124 LOG(ERROR) << "Tag length (" << tag_buffer_size - 1 << ") exceeds maximum (" | 117 LOG(ERROR) << "Tag length (" << tag_buffer_size - 1 << ") exceeds maximum (" |
125 << kMaxTagLength << ")."; | 118 << kMaxTagLength << ")."; |
126 return std::string(); | 119 return std::string(); |
127 } | 120 } |
128 | 121 |
129 scoped_ptr<char[]> tag_buffer(new char[tag_buffer_size]); | 122 scoped_ptr<char[]> tag_buffer(new char[tag_buffer_size]); |
130 extractor.ExtractTag(tag_buffer.get(), &tag_buffer_size); | 123 extractor.ExtractTag(tag_buffer.get(), &tag_buffer_size); |
131 | 124 |
132 return std::string(tag_buffer.get(), tag_buffer_size - 1); | 125 return std::string(tag_buffer.get(), tag_buffer_size - 1); |
133 } | 126 } |
134 | 127 |
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 | 128 // Parses |tag| as key-value pairs and overwrites |parsed_pairs| with |
149 // the result. |tag| should be a '&'-delimited list of '='-separated | 129 // the result. |tag| should be a '&'-delimited list of '='-separated |
150 // key-value pairs, e.g. "key1=value1&key2=value2". | 130 // key-value pairs, e.g. "key1=value1&key2=value2". |
151 // Returns true if the data could be parsed. | 131 // Returns true if the data could be parsed. |
152 bool ParseTag(const std::string& tag, | 132 bool ParseTag(const std::string& tag, |
153 std::map<std::string, std::string>* parsed_pairs) { | 133 std::map<std::string, std::string>* parsed_pairs) { |
154 DCHECK(parsed_pairs); | 134 DCHECK(parsed_pairs); |
155 | 135 |
156 base::StringPairs kv_pairs; | 136 base::StringPairs kv_pairs; |
157 if (!base::SplitStringIntoKeyValuePairs(tag, '=', '&', &kv_pairs)) { | 137 if (!base::SplitStringIntoKeyValuePairs(tag, '=', '&', &kv_pairs)) { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 | 196 |
217 // Install Chrome. Wait for the installer to finish before returning. | 197 // Install Chrome. Wait for the installer to finish before returning. |
218 base::LaunchOptions options; | 198 base::LaunchOptions options; |
219 options.wait = true; | 199 options.wait = true; |
220 bool launch_success = | 200 bool launch_success = |
221 base::LaunchProcess(base::CommandLine(setup_file), options, NULL); | 201 base::LaunchProcess(base::CommandLine(setup_file), options, NULL); |
222 base::DeleteFile(setup_file, false); | 202 base::DeleteFile(setup_file, false); |
223 return launch_success ? SUCCESS : FAILED_TO_LAUNCH_CHROME_SETUP; | 203 return launch_success ? SUCCESS : FAILED_TO_LAUNCH_CHROME_SETUP; |
224 } | 204 } |
225 | 205 |
226 } // namespace | |
227 | |
228 extern "C" | |
229 int WINAPI wWinMain(HINSTANCE instance, | |
230 HINSTANCE prev_instance, | |
231 wchar_t* command_line, | |
232 int show_command) { | |
233 base::AtExitManager exit_manager; | |
234 base::CommandLine::Init(0, NULL); | |
235 logging::LogEventProvider::Initialize(kAppInstallerLogProvider); | |
236 logging::LoggingSettings settings; | |
237 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; | |
238 logging::InitLogging(settings); | |
239 | |
240 std::string app_id = | |
241 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
242 switches::kAppId); | |
243 const char* sxs = installer::switches::kChromeSxS; | |
244 // --chrome-sxs on the command line takes precedence over chrome-sxs in the | |
245 // tag. | |
246 bool is_canary = base::CommandLine::ForCurrentProcess()->HasSwitch(sxs); | |
247 | |
248 // --app-id on the command line inhibits tag parsing altogether. | |
249 if (app_id.empty()) { | |
250 base::FilePath current_exe; | |
251 if (!PathService::Get(base::FILE_EXE, ¤t_exe)) | |
252 return COULD_NOT_GET_FILE_PATH; | |
253 | |
254 // Get the tag added by dl.google.com. Note that this is passed in via URL | |
255 // parameters when requesting a file to download, so it must be validated | |
256 // before use. | |
257 std::string tag = GetTag(current_exe); | |
258 if (tag.empty()) | |
259 return COULD_NOT_READ_TAG; | |
260 | |
261 DVLOG(1) << "Tag: " << tag; | |
262 | |
263 std::map<std::string, std::string> parsed_pairs; | |
264 if (!ParseTag(tag, &parsed_pairs)) | |
265 return COULD_NOT_PARSE_TAG; | |
266 | |
267 auto result = parsed_pairs.find(switches::kAppId); | |
268 if (result != parsed_pairs.end()) | |
269 app_id = result->second; | |
270 | |
271 if (!is_canary) { | |
272 result = parsed_pairs.find(sxs); | |
273 is_canary = result != parsed_pairs.end() && result->second == "1"; | |
274 } | |
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 base::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 | 206 } // namespace app_installer |
OLD | NEW |