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 #include "base/memory/ref_counted.h" 6 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/weak_ptr.h" 8 #include "base/strings/string16.h" 9 #include "base/version.h" 10 #include "base/win/win_util.h" 11 #include "base/win/windows_version.h" 12 #include "chrome/browser/google/google_update_win.h" 13 #include "chrome/browser/lifetime/application_lifetime.h" 14 #include "chrome/browser/ui/browser.h" 15 #include "chrome/browser/ui/webui/help/version_updater.h" 16 #include "chrome/common/chrome_version_info.h" 17 #include "chrome/installer/util/browser_distribution.h" 18 #include "chrome/installer/util/install_util.h" 19 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/user_metrics.h" 21 #include "grit/chromium_strings.h" 22 #include "grit/generated_resources.h" 23 #include "ui/base/l10n/l10n_util.h" 24 #include "ui/views/widget/widget.h" 25 26 using content::BrowserThread; 27 using content::UserMetricsAction; 28 29 namespace { 30 31 // Windows implementation of version update functionality, used by the WebUI 32 // About/Help page. 33 class VersionUpdaterWin : public VersionUpdater, 34 public GoogleUpdateStatusListener { 35 private: 36 friend class VersionReader; 37 friend class VersionUpdater; 38 39 // Clients must use VersionUpdater::Create(). 40 VersionUpdaterWin(); 41 virtual ~VersionUpdaterWin(); 42 43 // VersionUpdater implementation. 44 virtual void CheckForUpdate(const StatusCallback& callback) OVERRIDE; 45 virtual void RelaunchBrowser() const OVERRIDE; 46 47 // GoogleUpdateStatusListener implementation. 48 virtual void OnReportResults(GoogleUpdateUpgradeResult result, 49 GoogleUpdateErrorCode error_code, 50 const string16& error_message, 51 const string16& version) OVERRIDE; 52 53 // Update the UI to show the status of the upgrade. 54 void UpdateStatus(GoogleUpdateUpgradeResult result, 55 GoogleUpdateErrorCode error_code, 56 const string16& error_message); 57 58 // Got the intalled version so the handling of the UPGRADE_ALREADY_UP_TO_DATE 59 // result case can now be completeb on the UI thread. 60 void GotInstalledVersion(const Version& version); 61 62 // Little helper function to create google_updater_. 63 void CreateGoogleUpdater(); 64 65 // Helper function to clear google_updater_. 66 void ClearGoogleUpdater(); 67 68 // Returns a window that can be used for elevation. 69 HWND GetElevationParent(); 70 71 // The class that communicates with Google Update to find out if an update is 72 // available and asks it to start an upgrade. 73 scoped_refptr<GoogleUpdate> google_updater_; 74 75 // Used for callbacks. 76 base::WeakPtrFactory<VersionUpdaterWin> weak_factory_; 77 78 // Callback used to communicate update status to the client. 79 StatusCallback callback_; 80 81 DISALLOW_COPY_AND_ASSIGN(VersionUpdaterWin); 82 }; 83 84 // This class is used to read the version on the FILE thread and then call back 85 // the version updater in the UI thread. Using a class helps better control 86 // the lifespan of the Version independently of the lifespan of the version 87 // updater, which may die while asynchonicity is happening, thus the usage of 88 // the WeakPtr, which can only be used from the thread that created it. 89 class VersionReader 90 : public base::RefCountedThreadSafe<VersionReader> { 91 public: 92 explicit VersionReader( 93 const base::WeakPtr<VersionUpdaterWin>& version_updater) 94 : version_updater_(version_updater) { 95 } 96 97 void GetVersionFromFileThread() { 98 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 99 InstallUtil::GetChromeVersion(dist, false, &installed_version_); 100 if (!installed_version_.IsValid()) { 101 // User-level Chrome is not installed, check system-level. 102 InstallUtil::GetChromeVersion(dist, true, &installed_version_); 103 } 104 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( 105 &VersionReader::SetVersionInUIThread, this)); 106 } 107 108 void SetVersionInUIThread() { 109 if (version_updater_.get() != NULL) 110 version_updater_->GotInstalledVersion(installed_version_); 111 } 112 113 private: 114 friend class base::RefCountedThreadSafe<VersionReader>; 115 116 // The version updater that must be called back when we are done. 117 // We use a weak pointer in case the updater gets destroyed while waiting. 118 base::WeakPtr<VersionUpdaterWin> version_updater_; 119 120 // This is the version that gets read in the FILE thread and set on the 121 // the updater in the UI thread. 122 Version installed_version_; 123 }; 124 125 VersionUpdaterWin::VersionUpdaterWin() 126 : weak_factory_(this) { 127 CreateGoogleUpdater(); 128 } 129 130 VersionUpdaterWin::~VersionUpdaterWin() { 131 // The Google Updater will hold a pointer to the listener until it reports 132 // status, so that pointer must be cleared when the listener is destoyed. 133 ClearGoogleUpdater(); 134 } 135 136 void VersionUpdaterWin::CheckForUpdate(const StatusCallback& callback) { 137 callback_ = callback; 138 139 // On-demand updates for Chrome don't work in Vista RTM when UAC is turned 140 // off. So, in this case, the version updater must not mention 141 // on-demand updates. Silent updates (in the background) should still 142 // work as before - enabling UAC or installing the latest service pack 143 // for Vista is another option. 144 if (!(base::win::GetVersion() == base::win::VERSION_VISTA && 145 (base::win::OSInfo::GetInstance()->service_pack().major == 0) && 146 !base::win::UserAccountControlIsEnabled())) { 147 // This could happen if the page got refreshed after results were returned. 148 if (!google_updater_) 149 CreateGoogleUpdater(); 150 UpdateStatus(UPGRADE_CHECK_STARTED, GOOGLE_UPDATE_NO_ERROR, string16()); 151 // Specify false to not upgrade yet. 152 google_updater_->CheckForUpdate(false, GetElevationParent()); 153 } 154 } 155 156 void VersionUpdaterWin::RelaunchBrowser() const { 157 chrome::AttemptRestart(); 158 } 159 160 void VersionUpdaterWin::OnReportResults( 161 GoogleUpdateUpgradeResult result, GoogleUpdateErrorCode error_code, 162 const string16& error_message, const string16& version) { 163 // Drop the last reference to the object so that it gets cleaned up here. 164 ClearGoogleUpdater(); 165 UpdateStatus(result, error_code, error_message); 166 } 167 168 void VersionUpdaterWin::UpdateStatus(GoogleUpdateUpgradeResult result, 169 GoogleUpdateErrorCode error_code, 170 const string16& error_message) { 171 // For Chromium builds it would show an error message. 172 // But it looks weird because in fact there is no error, 173 // just the update server is not available for non-official builds. 174 #if defined(GOOGLE_CHROME_BUILD) 175 Status status = UPDATED; 176 string16 message; 177 178 switch (result) { 179 case UPGRADE_CHECK_STARTED: { 180 content::RecordAction(UserMetricsAction("UpgradeCheck_Started")); 181 status = CHECKING; 182 break; 183 } 184 case UPGRADE_STARTED: { 185 content::RecordAction(UserMetricsAction("Upgrade_Started")); 186 status = UPDATING; 187 break; 188 } 189 case UPGRADE_IS_AVAILABLE: { 190 content::RecordAction( 191 UserMetricsAction("UpgradeCheck_UpgradeIsAvailable")); 192 DCHECK(!google_updater_); // Should have been nulled out already. 193 CreateGoogleUpdater(); 194 UpdateStatus(UPGRADE_STARTED, GOOGLE_UPDATE_NO_ERROR, string16()); 195 // Specify true to upgrade now. 196 google_updater_->CheckForUpdate(true, GetElevationParent()); 197 return; 198 } 199 case UPGRADE_ALREADY_UP_TO_DATE: { 200 // Google Update reported that Chrome is up-to-date. 201 // To confirm the updated version is running, the reading 202 // must be done on the file thread. The rest of this case 203 // will be handled within GotInstalledVersion. 204 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind( 205 &VersionReader::GetVersionFromFileThread, 206 new VersionReader(weak_factory_.GetWeakPtr()))); 207 return; 208 } 209 case UPGRADE_SUCCESSFUL: { 210 content::RecordAction(UserMetricsAction("UpgradeCheck_Upgraded")); 211 status = NEARLY_UPDATED; 212 break; 213 } 214 case UPGRADE_ERROR: { 215 content::RecordAction(UserMetricsAction("UpgradeCheck_Error")); 216 status = FAILED; 217 if (error_code == GOOGLE_UPDATE_DISABLED_BY_POLICY) { 218 message = 219 l10n_util::GetStringUTF16(IDS_UPGRADE_DISABLED_BY_POLICY); 220 } else if (error_code == GOOGLE_UPDATE_DISABLED_BY_POLICY_AUTO_ONLY) { 221 message = 222 l10n_util::GetStringUTF16(IDS_UPGRADE_DISABLED_BY_POLICY_MANUAL); 223 } else { 224 message = 225 l10n_util::GetStringFUTF16Int(IDS_UPGRADE_ERROR, error_code); 226 } 227 228 if (!error_message.empty()) { 229 message += 230 l10n_util::GetStringFUTF16(IDS_ABOUT_BOX_ERROR_DURING_UPDATE_CHECK, 231 error_message); 232 } 233 break; 234 } 235 } 236 237 // TODO(mad): Get proper progress value instead of passing 0. 238 // http://crbug.com/136117 239 callback_.Run(status, 0, message); 240 #endif // defined(GOOGLE_CHROME_BUILD) 241 } 242 243 void VersionUpdaterWin::GotInstalledVersion(const Version& version) { 244 // This must be called on the UI thread so that callback_ can be called. 245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 246 247 // Make sure that the latest version is running and if not, 248 // notify the user by setting the status to NEARLY_UPDATED. 249 // 250 // The extra version check is necessary on Windows because the application 251 // may be already up to date on disk though the running app is still 252 // out of date. 253 chrome::VersionInfo version_info; 254 Version running_version(version_info.Version()); 255 if (!version.IsValid() || version.CompareTo(running_version) <= 0) { 256 content::RecordAction( 257 UserMetricsAction("UpgradeCheck_AlreadyUpToDate")); 258 callback_.Run(UPDATED, 0, string16()); 259 } else { 260 content::RecordAction(UserMetricsAction("UpgradeCheck_AlreadyUpgraded")); 261 callback_.Run(NEARLY_UPDATED, 0, string16()); 262 } 263 } 264 265 void VersionUpdaterWin::CreateGoogleUpdater() { 266 ClearGoogleUpdater(); 267 google_updater_ = new GoogleUpdate(); 268 google_updater_->set_status_listener(this); 269 } 270 271 void VersionUpdaterWin::ClearGoogleUpdater() { 272 if (google_updater_) { 273 google_updater_->set_status_listener(NULL); 274 google_updater_ = NULL; 275 } 276 } 277 278 BOOL CALLBACK WindowEnumeration(HWND window, LPARAM param) { 279 if (IsWindowVisible(window)) { 280 HWND* returned_window = reinterpret_cast<HWND*>(param); 281 *returned_window = window; 282 return FALSE; 283 } 284 return TRUE; 285 } 286 287 HWND VersionUpdaterWin::GetElevationParent() { 288 // Look for a visible window belonging to the UI thread. 289 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 290 HWND window = NULL; 291 EnumThreadWindows(GetCurrentThreadId(), 292 WindowEnumeration, 293 reinterpret_cast<LPARAM>(&window)); 294 #if !defined(USE_AURA) 295 // If using Aura, we might not have a Visible window in this process. In 296 // theory Google update can cope with that. 297 DCHECK(window != NULL) << "Failed to find a valid window handle on thread: " 298 << GetCurrentThreadId(); 299 #endif 300 return window; 301 } 302 303 } // namespace 304 305 VersionUpdater* VersionUpdater::Create() { 306 return new VersionUpdaterWin; 307 } 308