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 we have a valid window handle. | |
James Hawkins
2012/07/10 20:52:34
nit: Do a find on all 'we' and rewrite those sente
MAD
2012/07/11 14:45:32
Done.
| |
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 // We use this class to read the version on the FILE thread and then call back | |
86 // the version updater in the UI thread. We use a class so that we can 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 base::WeakPtr<VersionUpdaterWin> version_updater_; | |
118 scoped_ptr<Version> installed_version_; | |
James Hawkins
2012/07/10 20:52:34
nit: Document member variables.
MAD
2012/07/11 14:45:32
Done.
| |
119 }; | |
120 | |
121 VersionUpdaterWin::VersionUpdaterWin() | |
122 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | |
123 window_(NULL) { | |
124 SetGoogleUpdater(); | |
125 } | |
126 | |
127 VersionUpdaterWin::~VersionUpdaterWin() { | |
128 // The Google Updater will hold a pointer to us until it reports status, so we | |
129 // need to let it know that we will no longer be listening. | |
130 if (google_updater_) | |
131 google_updater_->set_status_listener(NULL); | |
132 } | |
133 | |
134 void VersionUpdaterWin::CheckForUpdate(const StatusCallback& callback) { | |
135 callback_ = callback; | |
136 | |
137 // On-demand updates for Chrome don't work in Vista RTM when UAC is turned | |
138 // off. So, in this case we just want the About box to not mention | |
139 // on-demand updates. Silent updates (in the background) should still | |
140 // work as before - enabling UAC or installing the latest service pack | |
141 // for Vista is another option. | |
142 if (!(base::win::GetVersion() == base::win::VERSION_VISTA && | |
143 (base::win::OSInfo::GetInstance()->service_pack().major == 0) && | |
144 !base::win::UserAccountControlIsEnabled())) { | |
145 // This could happen if we already got results, but the page got refreshed. | |
146 if (!google_updater_) | |
147 SetGoogleUpdater(); | |
148 UpdateStatus(UPGRADE_CHECK_STARTED, GOOGLE_UPDATE_NO_ERROR, string16()); | |
149 EnsureValidWindow(); | |
150 google_updater_->CheckForUpdate(false, window_); // Don't upgrade yet. | |
151 } | |
152 } | |
153 | |
154 void VersionUpdaterWin::RelaunchBrowser() const { | |
155 browser::AttemptRestart(); | |
156 } | |
157 | |
158 void VersionUpdaterWin::OnReportResults( | |
159 GoogleUpdateUpgradeResult result, GoogleUpdateErrorCode error_code, | |
160 const string16& error_message, const string16& version) { | |
161 // Drop the last reference to the object so that it gets cleaned up here. | |
162 google_updater_ = NULL; | |
163 UpdateStatus(result, error_code, error_message); | |
164 } | |
165 | |
166 void VersionUpdaterWin::UpdateStatus(GoogleUpdateUpgradeResult result, | |
167 GoogleUpdateErrorCode error_code, | |
168 const string16& error_message) { | |
169 // For Chromium builds it would show an error message. | |
170 // But it looks weird because in fact there is no error, | |
171 // just the update server is not available for non-official builds. | |
172 #if defined(GOOGLE_CHROME_BUILD) | |
173 Status status = UPDATED; | |
174 string16 message; | |
175 | |
176 switch (result) { | |
177 case UPGRADE_CHECK_STARTED: { | |
178 content::RecordAction(UserMetricsAction("UpgradeCheck_Started")); | |
179 status = CHECKING; | |
180 break; | |
181 } | |
182 case UPGRADE_STARTED: { | |
183 content::RecordAction(UserMetricsAction("Upgrade_Started")); | |
184 status = UPDATING; | |
185 break; | |
186 } | |
187 case UPGRADE_IS_AVAILABLE: { | |
188 content::RecordAction( | |
189 UserMetricsAction("UpgradeCheck_UpgradeIsAvailable")); | |
190 DCHECK(!google_updater_); // Should have been nulled out already. | |
191 SetGoogleUpdater(); | |
192 UpdateStatus(UPGRADE_STARTED, GOOGLE_UPDATE_NO_ERROR, string16()); | |
193 EnsureValidWindow(); | |
194 google_updater_->CheckForUpdate(true, window_); // Upgrade now. | |
195 return; | |
196 } | |
197 case UPGRADE_ALREADY_UP_TO_DATE: { | |
198 // Google Update reported that Chrome is up-to-date. | |
199 // We need to confirm we are running the updated version. | |
200 // But we must defer version reading to the file thread... | |
201 // We'll handle the rest of this case within GotInstalledVersion below. | |
202 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind( | |
203 &VersionReader::GetVersionFromFileThread, | |
204 new VersionReader(weak_factory_.GetWeakPtr()))); | |
205 return; | |
206 } | |
207 case UPGRADE_SUCCESSFUL: { | |
208 content::RecordAction(UserMetricsAction("UpgradeCheck_Upgraded")); | |
209 status = NEARLY_UPDATED; | |
210 break; | |
211 } | |
212 case UPGRADE_ERROR: { | |
213 content::RecordAction(UserMetricsAction("UpgradeCheck_Error")); | |
214 status = FAILED; | |
215 if (error_code != GOOGLE_UPDATE_DISABLED_BY_POLICY) { | |
216 message = | |
217 l10n_util::GetStringFUTF16Int(IDS_UPGRADE_ERROR, error_code); | |
218 } else { | |
219 message = | |
220 l10n_util::GetStringUTF16(IDS_UPGRADE_DISABLED_BY_POLICY); | |
221 } | |
222 if (!error_message.empty()) { | |
223 message += | |
224 l10n_util::GetStringFUTF16(IDS_ABOUT_BOX_ERROR_DURING_UPDATE_CHECK, | |
225 error_message); | |
226 } | |
227 break; | |
228 } | |
229 } | |
230 | |
231 // TODO(mad): Get proper progress value instead of passing 0. | |
232 // http://crbug.com/136117 | |
233 callback_.Run(status, 0, message); | |
234 #endif // defined(GOOGLE_CHROME_BUILD) | |
235 } | |
236 | |
237 void VersionUpdaterWin::GotInstalledVersion(const Version* version) { | |
238 // This must be called on the UI thread so that we can call callback_. | |
239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
240 | |
241 // Make sure that we are running the latest version and if not, | |
242 // notify the user by setting the status to NEARLY_UPDATED. | |
243 // | |
244 // The extra version check is necessary on Windows because the application | |
245 // may be already up to date on disk though the running app is still | |
246 // out of date. | |
247 chrome::VersionInfo version_info; | |
248 scoped_ptr<Version> running_version( | |
249 Version::GetVersionFromString(version_info.Version())); | |
250 if (!version || (version->CompareTo(*running_version) <= 0)) { | |
251 content::RecordAction( | |
252 UserMetricsAction("UpgradeCheck_AlreadyUpToDate")); | |
253 callback_.Run(UPDATED, 0, string16()); | |
254 } else { | |
255 content::RecordAction(UserMetricsAction("UpgradeCheck_AlreadyUpgraded")); | |
256 callback_.Run(NEARLY_UPDATED, 0, string16()); | |
257 } | |
258 } | |
259 | |
260 void VersionUpdaterWin::SetGoogleUpdater() { | |
261 google_updater_ = new GoogleUpdate(); | |
262 google_updater_->set_status_listener(this); | |
263 } | |
264 | |
265 BOOL CALLBACK WindowEnumeration(HWND window, LPARAM param) { | |
266 if (IsWindowVisible(window)) { | |
267 HWND* returned_window = reinterpret_cast<HWND*>(param); | |
268 *returned_window = window; | |
269 return FALSE; | |
270 } | |
271 return TRUE; | |
272 } | |
273 | |
274 void VersionUpdaterWin::EnsureValidWindow() { | |
275 if (window_ != NULL) | |
276 return; | |
277 // We want the active window of the UI thread. | |
278 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
279 EnumThreadWindows(GetCurrentThreadId(), | |
280 WindowEnumeration, | |
281 reinterpret_cast<LPARAM>(&window_)); | |
282 DCHECK(window_ != NULL) << "Failed to find a valid window handle on thread: " | |
283 << GetCurrentThreadId(); | |
284 } | |
285 | |
286 } // namespace | |
287 | |
288 VersionUpdater* VersionUpdater::Create() { | |
289 return new VersionUpdaterWin; | |
290 } | |
OLD | NEW |