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.h" | |
13 #include "chrome/browser/ui/browser.h" | |
14 #include "chrome/browser/ui/browser_list.h" | |
15 #include "chrome/browser/ui/browser_window.h" | |
16 #include "chrome/browser/ui/webui/help/version_updater.h" | |
17 #include "chrome/common/chrome_version_info.h" | |
18 #include "chrome/installer/util/browser_distribution.h" | |
19 #include "chrome/installer/util/install_util.h" | |
20 #include "content/public/browser/browser_thread.h" | |
21 #include "content/public/browser/user_metrics.h" | |
22 #include "grit/chromium_strings.h" | |
23 #include "grit/generated_resources.h" | |
24 #include "ui/base/l10n/l10n_util.h" | |
25 #include "ui/views/widget/widget.h" | |
26 | |
27 using content::BrowserThread; | |
28 using content::UserMetricsAction; | |
29 | |
30 namespace { | |
31 | |
32 // Returns the Widget of the window of an active browser, any browser. | |
33 // This is to make sure that the GoogleUpdate uses a parent window from | |
34 // from Chrome when it needs elevation. | |
35 views::Widget* GetWidget() { | |
Ben Goodger (Google)
2012/07/09 15:44:57
rather than use BrowserList here, since this code
MAD
2012/07/09 18:48:58
Done.
| |
36 // Find an active browser so we can return it's native window as a widget. | |
37 Browser* browser = NULL; | |
38 for (BrowserList::const_iterator browser_iterator = BrowserList::begin(); | |
39 browser_iterator != BrowserList::end(); browser_iterator++) { | |
40 if ((*browser_iterator)->window()) { | |
41 browser = *browser_iterator; | |
42 break; | |
43 } | |
44 } | |
45 if (!browser) | |
46 return NULL; | |
47 return views::Widget::GetWidgetForNativeWindow( | |
48 browser->window()->GetNativeWindow()); | |
49 } | |
50 | |
51 // Windows implementation of version update functionality, used by the WebUI | |
52 // About/Help page. | |
53 class VersionUpdaterWin : public VersionUpdater, | |
54 public GoogleUpdateStatusListener { | |
55 private: | |
56 friend class VersionReader; | |
57 friend class VersionUpdater; | |
58 | |
59 // Clients must use VersionUpdater::Create(). | |
60 VersionUpdaterWin(); | |
61 virtual ~VersionUpdaterWin(); | |
62 | |
63 // VersionUpdater implementation. | |
64 virtual void CheckForUpdate(const StatusCallback& callback) OVERRIDE; | |
65 virtual void RelaunchBrowser() const OVERRIDE; | |
66 | |
67 // GoogleUpdateStatusListener implementation. | |
68 virtual void OnReportResults(GoogleUpdateUpgradeResult result, | |
69 GoogleUpdateErrorCode error_code, | |
70 const string16& error_message, | |
71 const string16& version) OVERRIDE; | |
72 | |
73 // Update the UI to show the status of the upgrade. | |
74 void UpdateStatus(GoogleUpdateUpgradeResult result, | |
75 GoogleUpdateErrorCode error_code, | |
76 const string16& error_message); | |
77 | |
78 // Got the intalled version so the handling of the UPGRADE_ALREADY_UP_TO_DATE | |
79 // result case can now be completeb on the UI thread. | |
80 void GotInstalledVersion(const Version* version); | |
81 | |
82 // Little helper function to reset google_updater_. | |
83 void SetGoogleUpdater(); | |
84 | |
85 // The class that communicates with Google Update to find out if an update is | |
86 // available and asks it to start an upgrade. | |
87 scoped_refptr<GoogleUpdate> google_updater_; | |
88 | |
89 // Used for callbacks. | |
90 base::WeakPtrFactory<VersionUpdaterWin> weak_factory_; | |
91 | |
92 // Callback used to communicate update status to the client. | |
93 StatusCallback callback_; | |
94 | |
95 DISALLOW_COPY_AND_ASSIGN(VersionUpdaterWin); | |
96 }; | |
97 | |
98 // We use this class to read the version on the FILE thread and then call back | |
99 // the version updater in the UI thread. We use a class so that we can control | |
100 // the lifespan of the Version independently of the lifespan of the version | |
101 // updater, which may die while asynchonicity is happening, thus the usage of | |
102 // the WeakPtr, which can only be used from the thread that created it. | |
103 class VersionReader | |
104 : public base::RefCountedThreadSafe<VersionReader> { | |
105 public: | |
106 explicit VersionReader( | |
107 const base::WeakPtr<VersionUpdaterWin>& version_updater) | |
108 : version_updater_(version_updater) { | |
109 } | |
110 | |
111 void GetVersionFromFileThread() { | |
112 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
113 installed_version_.reset(InstallUtil::GetChromeVersion(dist, false)); | |
114 if (!installed_version_.get()) { | |
115 // User-level Chrome is not installed, check system-level. | |
116 installed_version_.reset(InstallUtil::GetChromeVersion(dist, true)); | |
117 } | |
118 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | |
119 &VersionReader::SetVersionInUIThread, this)); | |
120 } | |
121 | |
122 void SetVersionInUIThread() { | |
123 if (version_updater_.get() != NULL) | |
124 version_updater_->GotInstalledVersion(installed_version_.get()); | |
125 } | |
126 | |
127 private: | |
128 friend class base::RefCountedThreadSafe<VersionReader>; | |
129 | |
130 base::WeakPtr<VersionUpdaterWin> version_updater_; | |
131 scoped_ptr<Version> installed_version_; | |
132 }; | |
133 | |
134 VersionUpdaterWin::VersionUpdaterWin() | |
135 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
136 SetGoogleUpdater(); | |
137 } | |
138 | |
139 VersionUpdaterWin::~VersionUpdaterWin() { | |
140 // The Google Updater will hold a pointer to us until it reports status, so we | |
141 // need to let it know that we will no longer be listening. | |
142 if (google_updater_) | |
143 google_updater_->set_status_listener(NULL); | |
144 } | |
145 | |
146 void VersionUpdaterWin::CheckForUpdate(const StatusCallback& callback) { | |
147 callback_ = callback; | |
148 | |
149 // On-demand updates for Chrome don't work in Vista RTM when UAC is turned | |
150 // off. So, in this case we just want the About box to not mention | |
151 // on-demand updates. Silent updates (in the background) should still | |
152 // work as before - enabling UAC or installing the latest service pack | |
153 // for Vista is another option. | |
154 if (!(base::win::GetVersion() == base::win::VERSION_VISTA && | |
155 (base::win::OSInfo::GetInstance()->service_pack().major == 0) && | |
156 !base::win::UserAccountControlIsEnabled())) { | |
157 // This could happen if we already got results, but the page got refreshed. | |
158 if (!google_updater_) | |
159 SetGoogleUpdater(); | |
160 UpdateStatus(UPGRADE_CHECK_STARTED, GOOGLE_UPDATE_NO_ERROR, string16()); | |
161 google_updater_->CheckForUpdate(false /* Don't upgrade yet */, GetWidget()); | |
Ben Goodger (Google)
2012/07/09 15:44:57
Also, why does CheckForUpdate() take a views::Widg
MAD
2012/07/09 18:48:58
I don't know this code too well... My guess is tha
| |
162 } | |
163 } | |
164 | |
165 void VersionUpdaterWin::RelaunchBrowser() const { | |
166 browser::AttemptRestart(); | |
167 } | |
168 | |
169 void VersionUpdaterWin::OnReportResults( | |
170 GoogleUpdateUpgradeResult result, GoogleUpdateErrorCode error_code, | |
171 const string16& error_message, const string16& version) { | |
172 // Drop the last reference to the object so that it gets cleaned up here. | |
173 google_updater_ = NULL; | |
174 UpdateStatus(result, error_code, error_message); | |
175 } | |
176 | |
177 void VersionUpdaterWin::UpdateStatus(GoogleUpdateUpgradeResult result, | |
178 GoogleUpdateErrorCode error_code, | |
179 const string16& error_message) { | |
180 // For Chromium builds it would show an error message. | |
181 // But it looks weird because in fact there is no error, | |
182 // just the update server is not available for non-official builds. | |
183 #if defined(GOOGLE_CHROME_BUILD) | |
184 Status status = UPDATED; | |
185 string16 message; | |
186 | |
187 switch (result) { | |
188 case UPGRADE_CHECK_STARTED: { | |
189 content::RecordAction(UserMetricsAction("UpgradeCheck_Started")); | |
190 status = CHECKING; | |
191 break; | |
192 } | |
193 case UPGRADE_STARTED: { | |
194 content::RecordAction(UserMetricsAction("Upgrade_Started")); | |
195 status = UPDATING; | |
196 break; | |
197 } | |
198 case UPGRADE_IS_AVAILABLE: { | |
199 content::RecordAction( | |
200 UserMetricsAction("UpgradeCheck_UpgradeIsAvailable")); | |
201 DCHECK(!google_updater_); // Should have been nulled out already. | |
202 SetGoogleUpdater(); | |
203 UpdateStatus(UPGRADE_STARTED, GOOGLE_UPDATE_NO_ERROR, string16()); | |
204 google_updater_->CheckForUpdate(true /* Upgrade now */, GetWidget()); | |
205 return; | |
206 } | |
207 case UPGRADE_ALREADY_UP_TO_DATE: { | |
208 // Google Update reported that Chrome is up-to-date. | |
209 // We need to confirm we are running the updated version. | |
210 // But we must defer version reading to the file thread... | |
211 // We'll handle the rest of this case within GotInstalledVersion below. | |
212 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind( | |
213 &VersionReader::GetVersionFromFileThread, | |
214 new VersionReader(weak_factory_.GetWeakPtr()))); | |
215 return; | |
216 } | |
217 case UPGRADE_SUCCESSFUL: { | |
218 content::RecordAction(UserMetricsAction("UpgradeCheck_Upgraded")); | |
219 status = NEARLY_UPDATED; | |
220 break; | |
221 } | |
222 case UPGRADE_ERROR: { | |
223 content::RecordAction(UserMetricsAction("UpgradeCheck_Error")); | |
224 status = FAILED; | |
225 if (error_code != GOOGLE_UPDATE_DISABLED_BY_POLICY) { | |
226 message = | |
227 l10n_util::GetStringFUTF16Int(IDS_UPGRADE_ERROR, error_code); | |
228 } else { | |
229 message = | |
230 l10n_util::GetStringUTF16(IDS_UPGRADE_DISABLED_BY_POLICY); | |
231 } | |
232 if (!error_message.empty()) { | |
233 message += | |
234 l10n_util::GetStringFUTF16(IDS_ABOUT_BOX_ERROR_DURING_UPDATE_CHECK, | |
235 error_message); | |
236 } | |
237 break; | |
238 } | |
239 } | |
240 | |
241 // TODO(mad): Get proper progress value instead of passing 0. | |
242 // http://crbug.com/136117 | |
243 callback_.Run(status, 0, message); | |
244 #endif // defined(GOOGLE_CHROME_BUILD) | |
245 } | |
246 | |
247 void VersionUpdaterWin::GotInstalledVersion(const Version* version) { | |
248 // This must be called on the UI thread so that we can call callback_. | |
249 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
250 | |
251 // Make sure that we are running the latest version and if not, | |
252 // notify the user by setting the status to NEARLY_UPDATED. | |
253 // | |
254 // The extra version check is necessary on Windows because the application | |
255 // may be already up to date on disk though the running app is still | |
256 // out of date. | |
257 chrome::VersionInfo version_info; | |
258 scoped_ptr<Version> running_version( | |
259 Version::GetVersionFromString(version_info.Version())); | |
260 if (!version || (version->CompareTo(*running_version) <= 0)) { | |
261 content::RecordAction( | |
262 UserMetricsAction("UpgradeCheck_AlreadyUpToDate")); | |
263 callback_.Run(UPDATED, 0, string16()); | |
264 } else { | |
265 content::RecordAction(UserMetricsAction("UpgradeCheck_AlreadyUpgraded")); | |
266 callback_.Run(NEARLY_UPDATED, 0, string16()); | |
267 } | |
268 } | |
269 | |
270 void VersionUpdaterWin::SetGoogleUpdater() { | |
271 google_updater_ = new GoogleUpdate(); | |
272 google_updater_->set_status_listener(this); | |
273 } | |
274 | |
275 } // namespace | |
276 | |
277 VersionUpdater* VersionUpdater::Create() { | |
278 return new VersionUpdaterWin; | |
279 } | |
OLD | NEW |