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 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 |
| 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 |
OLD | NEW |