1 // Copyright (c) 2012 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 // This file defines specific implementation of BrowserDistribution class for 6 // Google Chrome. 7 8 #include "chrome/installer/util/google_chrome_distribution.h" 9 10 #include <windows.h> 11 #include <msi.h> 12 13 #include "base/files/file_path.h" 14 #include "base/path_service.h" 15 #include "base/strings/string_util.h" 16 #include "base/strings/stringprintf.h" 17 #include "base/strings/utf_string_conversions.h" 18 #include "base/win/registry.h" 19 #include "base/win/windows_version.h" 20 #include "chrome/common/chrome_icon_resources_win.h" 21 #include "chrome/common/net/test_server_locations.h" 22 #include "chrome/installer/util/app_registration_data.h" 23 #include "chrome/installer/util/channel_info.h" 24 #include "chrome/installer/util/google_update_constants.h" 25 #include "chrome/installer/util/google_update_settings.h" 26 #include "chrome/installer/util/helper.h" 27 #include "chrome/installer/util/install_util.h" 28 #include "chrome/installer/util/l10n_string_util.h" 29 #include "chrome/installer/util/uninstall_metrics.h" 30 #include "chrome/installer/util/updating_app_registration_data.h" 31 #include "chrome/installer/util/util_constants.h" 32 #include "chrome/installer/util/wmi.h" 33 #include "content/public/common/result_codes.h" 34 35 #include "installer_util_strings.h" // NOLINT 36 37 namespace { 38 39 const wchar_t kChromeGuid[] = L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; 40 const wchar_t kBrowserAppId[] = L"Chrome"; 41 const wchar_t kBrowserProgIdPrefix[] = L"ChromeHTML"; 42 const wchar_t kBrowserProgIdDesc[] = L"Chrome HTML Document"; 43 const wchar_t kCommandExecuteImplUuid[] = 44 L"{5C65F4B0-3651-4514-B207-D10CB699B14B}"; 45 46 // Substitute the locale parameter in uninstall URL with whatever 47 // Google Update tells us is the locale. In case we fail to find 48 // the locale, we use US English. 49 base::string16 LocalizeUrl(const wchar_t* url) { 50 base::string16 language; 51 if (!GoogleUpdateSettings::GetLanguage(&language)) 52 language = L"en-US"; // Default to US English. 53 return ReplaceStringPlaceholders(url, language.c_str(), NULL); 54 } 55 56 base::string16 GetUninstallSurveyUrl() { 57 const wchar_t kSurveyUrl[] = L"http://www.google.com/support/chrome/bin/" 58 L"request.py?hl=$1&contact_type=uninstall"; 59 return LocalizeUrl(kSurveyUrl); 60 } 61 62 } // namespace 63 64 GoogleChromeDistribution::GoogleChromeDistribution() 65 : BrowserDistribution(CHROME_BROWSER, 66 make_scoped_ptr( 67 new UpdatingAppRegistrationData(kChromeGuid))) { 68 } 69 70 GoogleChromeDistribution::GoogleChromeDistribution( 71 scoped_ptr<AppRegistrationData> app_reg_data) 72 : BrowserDistribution(CHROME_BROWSER, app_reg_data.Pass()) { 73 } 74 75 void GoogleChromeDistribution::DoPostUninstallOperations( 76 const Version& version, 77 const base::FilePath& local_data_path, 78 const base::string16& distribution_data) { 79 // Send the Chrome version and OS version as params to the form. 80 // It would be nice to send the locale, too, but I don't see an 81 // easy way to get that in the existing code. It's something we 82 // can add later, if needed. 83 // We depend on installed_version.GetString() not having spaces or other 84 // characters that need escaping: 0.2.13.4. Should that change, we will 85 // need to escape the string before using it in a URL. 86 const base::string16 kVersionParam = L"crversion"; 87 const base::string16 kOSParam = L"os"; 88 base::win::OSInfo::VersionNumber version_number = 89 base::win::OSInfo::GetInstance()->version_number(); 90 base::string16 os_version = base::StringPrintf(L"%d.%d.%d", 91 version_number.major, version_number.minor, version_number.build); 92 93 base::FilePath iexplore; 94 if (!PathService::Get(base::DIR_PROGRAM_FILES, &iexplore)) 95 return; 96 97 iexplore = iexplore.AppendASCII("Internet Explorer"); 98 iexplore = iexplore.AppendASCII("iexplore.exe"); 99 100 base::string16 command = iexplore.value() + L" " + GetUninstallSurveyUrl() + 101 L"&" + kVersionParam + L"=" + base::UTF8ToWide(version.GetString()) + 102 L"&" + kOSParam + L"=" + os_version; 103 104 base::string16 uninstall_metrics; 105 if (installer::ExtractUninstallMetricsFromFile(local_data_path, 106 &uninstall_metrics)) { 107 // The user has opted into anonymous usage data collection, so append 108 // metrics and distribution data. 109 command += uninstall_metrics; 110 if (!distribution_data.empty()) { 111 command += L"&"; 112 command += distribution_data; 113 } 114 } 115 116 int pid = 0; 117 // The reason we use WMI to launch the process is because the uninstall 118 // process runs inside a Job object controlled by the shell. As long as there 119 // are processes running, the shell will not close the uninstall applet. WMI 120 // allows us to escape from the Job object so the applet will close. 121 installer::WMIProcess::Launch(command, &pid); 122 } 123 124 base::string16 GoogleChromeDistribution::GetActiveSetupGuid() { 125 return GetAppGuid(); 126 } 127 128 base::string16 GoogleChromeDistribution::GetBaseAppName() { 129 // I'd really like to return L ## PRODUCT_FULLNAME_STRING; but that's no good 130 // since it'd be "Chromium" in a non-Chrome build, which isn't at all what I 131 // want. Sigh. 132 return L"Google Chrome"; 133 } 134 135 base::string16 GoogleChromeDistribution::GetShortcutName( 136 ShortcutType shortcut_type) { 137 int string_id = IDS_PRODUCT_NAME_BASE; 138 switch (shortcut_type) { 139 case SHORTCUT_CHROME_ALTERNATE: 140 string_id = IDS_OEM_MAIN_SHORTCUT_NAME_BASE; 141 break; 142 case SHORTCUT_APP_LAUNCHER: 143 string_id = IDS_APP_LIST_SHORTCUT_NAME_BASE; 144 break; 145 default: 146 DCHECK_EQ(shortcut_type, SHORTCUT_CHROME); 147 break; 148 } 149 return installer::GetLocalizedString(string_id); 150 } 151 152 int GoogleChromeDistribution::GetIconIndex(ShortcutType shortcut_type) { 153 if (shortcut_type == SHORTCUT_APP_LAUNCHER) 154 return icon_resources::kAppLauncherIndex; 155 DCHECK(shortcut_type == SHORTCUT_CHROME || 156 shortcut_type == SHORTCUT_CHROME_ALTERNATE) << shortcut_type; 157 return icon_resources::kApplicationIndex; 158 } 159 160 base::string16 GoogleChromeDistribution::GetBaseAppId() { 161 return kBrowserAppId; 162 } 163 164 base::string16 GoogleChromeDistribution::GetBrowserProgIdPrefix() { 165 return kBrowserProgIdPrefix; 166 } 167 168 base::string16 GoogleChromeDistribution::GetBrowserProgIdDesc() { 169 return kBrowserProgIdDesc; 170 } 171 172 base::string16 GoogleChromeDistribution::GetInstallSubDir() { 173 base::string16 sub_dir(installer::kGoogleChromeInstallSubDir1); 174 sub_dir.append(L"\\"); 175 sub_dir.append(installer::kGoogleChromeInstallSubDir2); 176 return sub_dir; 177 } 178 179 base::string16 GoogleChromeDistribution::GetPublisherName() { 180 const base::string16& publisher_name = 181 installer::GetLocalizedString(IDS_ABOUT_VERSION_COMPANY_NAME_BASE); 182 return publisher_name; 183 } 184 185 base::string16 GoogleChromeDistribution::GetAppDescription() { 186 const base::string16& app_description = 187 installer::GetLocalizedString(IDS_SHORTCUT_TOOLTIP_BASE); 188 return app_description; 189 } 190 191 std::string GoogleChromeDistribution::GetSafeBrowsingName() { 192 return "googlechrome"; 193 } 194 195 std::string GoogleChromeDistribution::GetNetworkStatsServer() const { 196 return chrome_common_net::kEchoTestServerLocation; 197 } 198 199 base::string16 GoogleChromeDistribution::GetDistributionData(HKEY root_key) { 200 base::string16 sub_key(google_update::kRegPathClientState); 201 sub_key.append(L"\\"); 202 sub_key.append(GetAppGuid()); 203 204 base::win::RegKey client_state_key( 205 root_key, sub_key.c_str(), KEY_READ | KEY_WOW64_32KEY); 206 base::string16 result; 207 base::string16 brand_value; 208 if (client_state_key.ReadValue(google_update::kRegRLZBrandField, 209 &brand_value) == ERROR_SUCCESS) { 210 result = google_update::kRegRLZBrandField; 211 result.append(L"="); 212 result.append(brand_value); 213 result.append(L"&"); 214 } 215 216 base::string16 client_value; 217 if (client_state_key.ReadValue(google_update::kRegClientField, 218 &client_value) == ERROR_SUCCESS) { 219 result.append(google_update::kRegClientField); 220 result.append(L"="); 221 result.append(client_value); 222 result.append(L"&"); 223 } 224 225 base::string16 ap_value; 226 // If we fail to read the ap key, send up "&ap=" anyway to indicate 227 // that this was probably a stable channel release. 228 client_state_key.ReadValue(google_update::kRegApField, &ap_value); 229 result.append(google_update::kRegApField); 230 result.append(L"="); 231 result.append(ap_value); 232 233 return result; 234 } 235 236 base::string16 GoogleChromeDistribution::GetUninstallLinkName() { 237 const base::string16& link_name = 238 installer::GetLocalizedString(IDS_UNINSTALL_CHROME_BASE); 239 return link_name; 240 } 241 242 base::string16 GoogleChromeDistribution::GetUninstallRegPath() { 243 return L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" 244 L"Google Chrome"; 245 } 246 247 base::string16 GoogleChromeDistribution::GetIconFilename() { 248 return installer::kChromeExe; 249 } 250 251 bool GoogleChromeDistribution::GetCommandExecuteImplClsid( 252 base::string16* handler_class_uuid) { 253 if (handler_class_uuid) 254 *handler_class_uuid = kCommandExecuteImplUuid; 255 return true; 256 } 257 258 bool GoogleChromeDistribution::AppHostIsSupported() { 259 return true; 260 } 261 262 // This method checks if we need to change "ap" key in Google Update to try 263 // full installer as fall back method in case incremental installer fails. 264 // - If incremental installer fails we append a magic string ("-full"), if 265 // it is not present already, so that Google Update server next time will send 266 // full installer to update Chrome on the local machine 267 // - If we are currently running full installer, we remove this magic 268 // string (if it is present) regardless of whether installer failed or not. 269 // There is no fall-back for full installer :) 270 void GoogleChromeDistribution::UpdateInstallStatus(bool system_install, 271 installer::ArchiveType archive_type, 272 installer::InstallStatus install_status) { 273 GoogleUpdateSettings::UpdateInstallStatus(system_install, 274 archive_type, InstallUtil::GetInstallReturnCode(install_status), 275 GetAppGuid()); 276 } 277 278 bool GoogleChromeDistribution::ShouldSetExperimentLabels() { 279 return true; 280 } 281 282 bool GoogleChromeDistribution::HasUserExperiments() { 283 return true; 284 } 285