Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Use of this source code is governed by a BSD-style license that can be | |
| 2 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 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/string16.h" | |
| 9 #include "base/version.h" | |
| 10 #include "base/win/windows_version.h" | |
| 11 #include "base/win/win_util.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 reset google_updater_. | |
| 63 void SetGoogleUpdater(); | |
| 64 | |
| 65 // Make sure a valid window handle is set in window_. | |
| 66 void EnsureValidWindow(); | |
| 67 | |
| 68 // The class that communicates with Google Update to find out if an update is | |
| 69 // available and asks it to start an upgrade. | |
| 70 scoped_refptr<GoogleUpdate> google_updater_; | |
| 71 | |
| 72 // Used for callbacks. | |
| 73 base::WeakPtrFactory<VersionUpdaterWin> weak_factory_; | |
| 74 | |
| 75 // Callback used to communicate update status to the client. | |
| 76 StatusCallback callback_; | |
| 77 | |
| 78 // A handle to a window from the UI thread which is used to pass to | |
| 79 // GoogleUpdate to potentially be used for the elevation UI. | |
| 80 HWND window_; | |
| 81 | |
| 82 DISALLOW_COPY_AND_ASSIGN(VersionUpdaterWin); | |
| 83 }; | |
| 84 | |
| 85 // This class is used to read the version on the FILE thread and then call back | |
| 86 // the version updater in the UI thread. Using a class helps better control | |
| 87 // the lifespan of the Version independently of the lifespan of the version | |
| 88 // updater, which may die while asynchonicity is happening, thus the usage of | |
| 89 // the WeakPtr, which can only be used from the thread that created it. | |
| 90 class VersionReader | |
| 91 : public base::RefCountedThreadSafe<VersionReader> { | |
| 92 public: | |
| 93 explicit VersionReader( | |
| 94 const base::WeakPtr<VersionUpdaterWin>& version_updater) | |
| 95 : version_updater_(version_updater) { | |
| 96 } | |
| 97 | |
| 98 void GetVersionFromFileThread() { | |
| 99 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
| 100 installed_version_.reset(InstallUtil::GetChromeVersion(dist, false)); | |
| 101 if (!installed_version_.get()) { | |
| 102 // User-level Chrome is not installed, check system-level. | |
| 103 installed_version_.reset(InstallUtil::GetChromeVersion(dist, true)); | |
| 104 } | |
| 105 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | |
| 106 &VersionReader::SetVersionInUIThread, this)); | |
| 107 } | |
| 108 | |
| 109 void SetVersionInUIThread() { | |
| 110 if (version_updater_.get() != NULL) | |
| 111 version_updater_->GotInstalledVersion(installed_version_.get()); | |
| 112 } | |
| 113 | |
| 114 private: | |
| 115 friend class base::RefCountedThreadSafe<VersionReader>; | |
| 116 | |
| 117 // The version updater that must be called back when we are done. | |
| 118 // We use a weak pointer in case the updater gets destroyed while waiting. | |
| 119 base::WeakPtr<VersionUpdaterWin> version_updater_; | |
| 120 | |
| 121 // This is the version that gets read in the FILE thread and set on the | |
| 122 // the updater in the UI thread. | |
| 123 scoped_ptr<Version> installed_version_; | |
| 124 }; | |
| 125 | |
| 126 VersionUpdaterWin::VersionUpdaterWin() | |
| 127 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | |
| 128 window_(NULL) { | |
| 129 SetGoogleUpdater(); | |
| 130 } | |
| 131 | |
| 132 VersionUpdaterWin::~VersionUpdaterWin() { | |
| 133 // The Google Updater will hold a pointer to the listener until it reports | |
| 134 // status, so that pointer must be cleared when the listener is destoyed. | |
| 135 if (google_updater_) | |
| 136 google_updater_->set_status_listener(NULL); | |
| 137 } | |
| 138 | |
| 139 void VersionUpdaterWin::CheckForUpdate(const StatusCallback& callback) { | |
| 140 callback_ = callback; | |
| 141 | |
| 142 // On-demand updates for Chrome don't work in Vista RTM when UAC is turned | |
| 143 // off. So, in this case, the version updater must not mention | |
| 144 // on-demand updates. Silent updates (in the background) should still | |
| 145 // work as before - enabling UAC or installing the latest service pack | |
| 146 // for Vista is another option. | |
| 147 if (!(base::win::GetVersion() == base::win::VERSION_VISTA && | |
| 148 (base::win::OSInfo::GetInstance()->service_pack().major == 0) && | |
| 149 !base::win::UserAccountControlIsEnabled())) { | |
| 150 // This could happen if the page got refreshed after results were returned. | |
| 151 if (!google_updater_) | |
| 152 SetGoogleUpdater(); | |
| 153 UpdateStatus(UPGRADE_CHECK_STARTED, GOOGLE_UPDATE_NO_ERROR, string16()); | |
|
Ben Goodger (Google)
2012/07/11 16:11:01
HWND parent = NULL;
GetElevationParent(&parent);
g
MAD
2012/07/11 18:01:19
Good idee...
Done!
| |
| 154 EnsureValidWindow(); | |
| 155 google_updater_->CheckForUpdate(false, window_); // Don't upgrade yet. | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 void VersionUpdaterWin::RelaunchBrowser() const { | |
| 160 browser::AttemptRestart(); | |
| 161 } | |
| 162 | |
| 163 void VersionUpdaterWin::OnReportResults( | |
| 164 GoogleUpdateUpgradeResult result, GoogleUpdateErrorCode error_code, | |
| 165 const string16& error_message, const string16& version) { | |
| 166 // Drop the last reference to the object so that it gets cleaned up here. | |
| 167 google_updater_ = NULL; | |
| 168 UpdateStatus(result, error_code, error_message); | |
| 169 } | |
| 170 | |
| 171 void VersionUpdaterWin::UpdateStatus(GoogleUpdateUpgradeResult result, | |
| 172 GoogleUpdateErrorCode error_code, | |
| 173 const string16& error_message) { | |
| 174 // For Chromium builds it would show an error message. | |
| 175 // But it looks weird because in fact there is no error, | |
| 176 // just the update server is not available for non-official builds. | |
| 177 #if defined(GOOGLE_CHROME_BUILD) | |
| 178 Status status = UPDATED; | |
| 179 string16 message; | |
| 180 | |
| 181 switch (result) { | |
| 182 case UPGRADE_CHECK_STARTED: { | |
| 183 content::RecordAction(UserMetricsAction("UpgradeCheck_Started")); | |
| 184 status = CHECKING; | |
| 185 break; | |
| 186 } | |
| 187 case UPGRADE_STARTED: { | |
| 188 content::RecordAction(UserMetricsAction("Upgrade_Started")); | |
| 189 status = UPDATING; | |
| 190 break; | |
| 191 } | |
| 192 case UPGRADE_IS_AVAILABLE: { | |
| 193 content::RecordAction( | |
| 194 UserMetricsAction("UpgradeCheck_UpgradeIsAvailable")); | |
| 195 DCHECK(!google_updater_); // Should have been nulled out already. | |
| 196 SetGoogleUpdater(); | |
| 197 UpdateStatus(UPGRADE_STARTED, GOOGLE_UPDATE_NO_ERROR, string16()); | |
| 198 EnsureValidWindow(); | |
| 199 google_updater_->CheckForUpdate(true, window_); // Upgrade now. | |
| 200 return; | |
| 201 } | |
| 202 case UPGRADE_ALREADY_UP_TO_DATE: { | |
| 203 // Google Update reported that Chrome is up-to-date. | |
| 204 // To confirm the updated version is running, the reading | |
| 205 // must be done on the file thread. The rest of this case | |
| 206 // will be handled within GotInstalledVersion. | |
| 207 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind( | |
| 208 &VersionReader::GetVersionFromFileThread, | |
| 209 new VersionReader(weak_factory_.GetWeakPtr()))); | |
| 210 return; | |
| 211 } | |
| 212 case UPGRADE_SUCCESSFUL: { | |
| 213 content::RecordAction(UserMetricsAction("UpgradeCheck_Upgraded")); | |
| 214 status = NEARLY_UPDATED; | |
| 215 break; | |
| 216 } | |
| 217 case UPGRADE_ERROR: { | |
| 218 content::RecordAction(UserMetricsAction("UpgradeCheck_Error")); | |
| 219 status = FAILED; | |
| 220 if (error_code != GOOGLE_UPDATE_DISABLED_BY_POLICY) { | |
| 221 message = | |
| 222 l10n_util::GetStringFUTF16Int(IDS_UPGRADE_ERROR, error_code); | |
| 223 } else { | |
| 224 message = | |
| 225 l10n_util::GetStringUTF16(IDS_UPGRADE_DISABLED_BY_POLICY); | |
| 226 } | |
| 227 if (!error_message.empty()) { | |
| 228 message += | |
| 229 l10n_util::GetStringFUTF16(IDS_ABOUT_BOX_ERROR_DURING_UPDATE_CHECK, | |
| 230 error_message); | |
| 231 } | |
| 232 break; | |
| 233 } | |
| 234 } | |
| 235 | |
| 236 // TODO(mad): Get proper progress value instead of passing 0. | |
| 237 // http://crbug.com/136117 | |
| 238 callback_.Run(status, 0, message); | |
| 239 #endif // defined(GOOGLE_CHROME_BUILD) | |
| 240 } | |
| 241 | |
| 242 void VersionUpdaterWin::GotInstalledVersion(const Version* version) { | |
| 243 // This must be called on the UI thread so that callback_ can be called. | |
| 244 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 245 | |
| 246 // Make sure that the latest version is running and if not, | |
| 247 // notify the user by setting the status to NEARLY_UPDATED. | |
| 248 // | |
| 249 // The extra version check is necessary on Windows because the application | |
| 250 // may be already up to date on disk though the running app is still | |
| 251 // out of date. | |
| 252 chrome::VersionInfo version_info; | |
| 253 scoped_ptr<Version> running_version( | |
| 254 Version::GetVersionFromString(version_info.Version())); | |
| 255 if (!version || (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::SetGoogleUpdater() { | |
| 266 google_updater_ = new GoogleUpdate(); | |
| 267 google_updater_->set_status_listener(this); | |
| 268 } | |
| 269 | |
| 270 BOOL CALLBACK WindowEnumeration(HWND window, LPARAM param) { | |
| 271 if (IsWindowVisible(window)) { | |
| 272 HWND* returned_window = reinterpret_cast<HWND*>(param); | |
| 273 *returned_window = window; | |
| 274 return FALSE; | |
| 275 } | |
| 276 return TRUE; | |
| 277 } | |
| 278 | |
| 279 void VersionUpdaterWin::EnsureValidWindow() { | |
| 280 if (window_ != NULL) | |
| 281 return; | |
| 282 // Look for a visible window belonging to the UI thread. | |
| 283 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 284 EnumThreadWindows(GetCurrentThreadId(), | |
| 285 WindowEnumeration, | |
| 286 reinterpret_cast<LPARAM>(&window_)); | |
| 287 DCHECK(window_ != NULL) << "Failed to find a valid window handle on thread: " | |
| 288 << GetCurrentThreadId(); | |
| 289 } | |
| 290 | |
| 291 } // namespace | |
| 292 | |
| 293 VersionUpdater* VersionUpdater::Create() { | |
| 294 return new VersionUpdaterWin; | |
| 295 } | |
| OLD | NEW |