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 views::Widget* GetWidget() { | |
James Hawkins
2012/07/06 21:03:44
Optional nit: Technically you can forward declare
James Hawkins
2012/07/06 21:03:44
nit: Document the function, specifically *which* w
MAD
2012/07/09 15:07:12
Done.
MAD
2012/07/09 15:07:12
I can't, I use views::Widget::GetWidgetForNativeWi
| |
33 // Find an active browser so we can return it's native window as a widget. | |
34 Browser* browser = NULL; | |
35 for (BrowserList::const_iterator browser_iterator = BrowserList::begin(); | |
36 browser_iterator != BrowserList::end(); browser_iterator++) { | |
37 if ((*browser_iterator)->window()) { | |
38 browser = *browser_iterator; | |
39 break; | |
40 } | |
41 } | |
42 if (!browser) | |
43 return NULL; | |
44 return views::Widget::GetWidgetForNativeWindow( | |
45 browser->window()->GetNativeWindow()); | |
46 } | |
47 | |
48 class VersionUpdaterWin : public VersionUpdater, | |
James Hawkins
2012/07/06 21:03:44
nit: Document the class.
MAD
2012/07/09 15:07:12
Done.
| |
49 public GoogleUpdateStatusListener { | |
50 private: | |
51 friend class VersionReader; | |
52 friend class VersionUpdater; | |
53 | |
54 // Clients must use VersionUpdater::Create(). | |
55 VersionUpdaterWin(); | |
56 virtual ~VersionUpdaterWin(); | |
57 | |
58 // VersionUpdater implementation. | |
59 virtual void CheckForUpdate(const StatusCallback& callback) OVERRIDE; | |
60 virtual void RelaunchBrowser() const OVERRIDE; | |
61 | |
62 // GoogleUpdateStatusListener implementation. | |
63 virtual void OnReportResults(GoogleUpdateUpgradeResult result, | |
64 GoogleUpdateErrorCode error_code, | |
65 const string16& error_message, | |
66 const string16& version) OVERRIDE; | |
67 | |
68 // Update the UI to show the status of the upgrade. | |
69 void UpdateStatus(GoogleUpdateUpgradeResult result, | |
70 GoogleUpdateErrorCode error_code, | |
71 const string16& error_message); | |
72 | |
73 // Got the intalled version so we can complete the handling of the | |
James Hawkins
2012/07/06 21:03:44
nit: Don't use pronouns in comments (we). Pronoun
MAD
2012/07/09 15:07:12
Done.
| |
74 // UPGRADE_ALREADY_UP_TO_DATE result case on th UI thread. | |
75 void GotInstalledVersion(const Version* version); | |
76 | |
77 // Little helper function to reset google_updater_. | |
78 void SetGoogleUpdater(); | |
79 | |
80 // The class that communicates with Google Update to find out if an update is | |
81 // available and asks it to start an upgrade. | |
82 scoped_refptr<GoogleUpdate> google_updater_; | |
83 | |
84 // Used for callbacks. | |
85 base::WeakPtrFactory<VersionUpdaterWin> weak_factory_; | |
86 | |
87 // Callback used to communicate update status to the client. | |
88 StatusCallback callback_; | |
89 | |
90 DISALLOW_COPY_AND_ASSIGN(VersionUpdaterWin); | |
91 }; | |
92 | |
93 // We use this class to read the version on the FILE thread and then call back | |
94 // the version updater in the UI thread. We use a class so that we can control | |
95 // the lifespan of the Version independently of the lifespan of the version | |
96 // updater, which may die while asynchonicity is happening, thus the usage of | |
97 // the WeakPtr, which can only be used from the thread that created it. | |
98 class VersionReader | |
99 : public base::RefCountedThreadSafe<VersionReader> { | |
100 public: | |
101 explicit VersionReader( | |
102 const base::WeakPtr<VersionUpdaterWin>& version_updater) | |
103 : version_updater_(version_updater) { | |
104 } | |
105 | |
106 void GetVersionFromFileThread() { | |
107 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
James Hawkins
2012/07/06 21:03:44
Optional nit: Technically you can forward declare
MAD
2012/07/09 15:07:12
But then I won't be able to call BrowserDistribut
| |
108 installed_version_.reset(InstallUtil::GetChromeVersion(dist, false)); | |
109 if (!installed_version_.get()) { | |
110 // User-level Chrome is not installed, check system-level. | |
111 installed_version_.reset(InstallUtil::GetChromeVersion(dist, true)); | |
112 } | |
113 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | |
114 &VersionReader::SetVersionInUIThread, this)); | |
115 } | |
116 | |
117 void SetVersionInUIThread() { | |
118 if (version_updater_.get() != NULL) | |
119 version_updater_->GotInstalledVersion(installed_version_.get()); | |
120 } | |
121 | |
122 private: | |
123 friend class base::RefCountedThreadSafe<VersionReader>; | |
124 | |
125 base::WeakPtr<VersionUpdaterWin> version_updater_; | |
126 scoped_ptr<Version> installed_version_; | |
127 }; | |
128 | |
129 VersionUpdaterWin::VersionUpdaterWin() | |
130 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
131 SetGoogleUpdater(); | |
132 } | |
133 | |
134 VersionUpdaterWin::~VersionUpdaterWin() { | |
135 // The Google Updater will hold a pointer to us until it reports status, so we | |
136 // need to let it know that we will no longer be listening. | |
137 if (google_updater_) | |
138 google_updater_->set_status_listener(NULL); | |
139 } | |
140 | |
141 void VersionUpdaterWin::CheckForUpdate(const StatusCallback& callback) { | |
142 callback_ = callback; | |
143 | |
144 // On-demand updates for Chrome don't work in Vista RTM when UAC is turned | |
145 // off. So, in this case we just want the About box to not mention | |
146 // on-demand updates. Silent updates (in the background) should still | |
147 // work as before - enabling UAC or installing the latest service pack | |
148 // for Vista is another option. | |
149 if (!(base::win::GetVersion() == base::win::VERSION_VISTA && | |
150 (base::win::OSInfo::GetInstance()->service_pack().major == 0) && | |
151 !base::win::UserAccountControlIsEnabled())) { | |
152 // This could happen if we already got results, but the page got refreshed. | |
153 if (!google_updater_) | |
154 SetGoogleUpdater(); | |
155 UpdateStatus(UPGRADE_CHECK_STARTED, GOOGLE_UPDATE_NO_ERROR, string16()); | |
156 google_updater_->CheckForUpdate(false /* Don't upgrade yet */, GetWidget()); | |
157 } | |
158 } | |
159 | |
160 void VersionUpdaterWin::RelaunchBrowser() const { | |
161 browser::AttemptRestart(); | |
162 } | |
163 | |
164 void VersionUpdaterWin::OnReportResults( | |
165 GoogleUpdateUpgradeResult result, GoogleUpdateErrorCode error_code, | |
166 const string16& error_message, const string16& version) { | |
167 // Drop the last reference to the object so that it gets cleaned up here. | |
168 google_updater_ = NULL; | |
169 UpdateStatus(result, error_code, error_message); | |
170 } | |
171 | |
172 void VersionUpdaterWin::UpdateStatus(GoogleUpdateUpgradeResult result, | |
173 GoogleUpdateErrorCode error_code, | |
174 const string16& error_message) { | |
175 // For Chromium builds it would show an error message. | |
176 // But it looks weird because in fact there is no error, | |
177 // just the update server is not available for non-official builds. | |
178 #if defined(GOOGLE_CHROME_BUILD) | |
179 Status status = UPDATED; | |
180 string16 message; | |
181 | |
182 switch (result) { | |
183 case UPGRADE_CHECK_STARTED: { | |
184 content::RecordAction(UserMetricsAction("UpgradeCheck_Started")); | |
185 status = CHECKING; | |
186 break; | |
187 } | |
188 case UPGRADE_STARTED: { | |
189 content::RecordAction(UserMetricsAction("Upgrade_Started")); | |
190 status = UPDATING; | |
191 break; | |
192 } | |
193 case UPGRADE_IS_AVAILABLE: { | |
194 content::RecordAction( | |
195 UserMetricsAction("UpgradeCheck_UpgradeIsAvailable")); | |
196 DCHECK(!google_updater_); // Should have been nulled out already. | |
197 SetGoogleUpdater(); | |
198 UpdateStatus(UPGRADE_STARTED, GOOGLE_UPDATE_NO_ERROR, string16()); | |
199 google_updater_->CheckForUpdate(true /* Upgrade now */, GetWidget()); | |
200 return; | |
201 } | |
202 case UPGRADE_ALREADY_UP_TO_DATE: { | |
203 // Google Update reported that Chrome is up-to-date. | |
204 // We need to confirm we are running the updated version. | |
205 // But we must defer version reading to the file thread... | |
206 // We'll handle the rest of this case within GotInstalledVersion below. | |
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 | |
James Hawkins
2012/07/06 21:03:44
nit: #endif // defined(GOOGLE_CHROME_BUILD)
MAD
2012/07/09 15:07:12
Done.
| |
240 } | |
241 | |
242 void VersionUpdaterWin::GotInstalledVersion(const Version* version) { | |
243 // This must be called on the UI thread so that we can call callback_. | |
244 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
245 | |
246 // Make sure that we are running the latest version 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 } // namespace | |
271 | |
272 VersionUpdater* VersionUpdater::Create() { | |
273 return new VersionUpdaterWin; | |
274 } | |
OLD | NEW |