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 "chrome/browser/ui/webui/help/version_updater_win.h" | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "base/version.h" | |
9 #include "base/win/windows_version.h" | |
10 #include "base/win/win_util.h" | |
11 #include "chrome/browser/ui/browser.h" | |
12 #include "chrome/browser/ui/browser_list.h" | |
13 #include "chrome/browser/ui/browser_window.h" | |
14 #include "chrome/common/chrome_version_info.h" | |
15 #include "chrome/installer/util/browser_distribution.h" | |
16 #include "chrome/installer/util/install_util.h" | |
17 #include "content/public/browser/browser_thread.h" | |
18 #include "content/public/browser/user_metrics.h" | |
19 #include "grit/chromium_strings.h" | |
20 #include "grit/generated_resources.h" | |
21 #include "ui/base/l10n/l10n_util.h" | |
22 #include "ui/views/widget/widget.h" | |
23 | |
24 using content::BrowserThread; | |
25 using content::UserMetricsAction; | |
26 | |
27 namespace { | |
28 | |
29 views::Widget* GetWidget() { | |
30 // Find an active browser so we can return it's native window as a widget. | |
31 Browser* browser = NULL; | |
32 for (BrowserList::const_iterator browser_iterator = BrowserList::begin(); | |
33 browser_iterator != BrowserList::end(); browser_iterator++) { | |
34 if ((*browser_iterator)->window()) { | |
35 browser = *browser_iterator; | |
36 break; | |
37 } | |
38 } | |
39 if (!browser) | |
40 return NULL; | |
41 return views::Widget::GetWidgetForNativeWindow( | |
42 browser->window()->GetNativeWindow()); | |
43 } | |
Roger Tawa OOO till Jul 10th
2012/07/05 16:13:22
This will find any browser object, not necessarily
MAD
2012/07/05 20:50:41
Yes, it's OK, all we need is A browser window... T
| |
44 | |
45 // We use this class to read the version of the FILE thread and then call back | |
Roger Tawa OOO till Jul 10th
2012/07/05 16:13:22
of --> on ?
MAD
2012/07/05 20:50:41
Done.
| |
46 // the version updater in the UI thread. We use a class so that we can control | |
47 // the lifespan of the Version independently of the lifespan of the version | |
48 // updater, which may die while asynchonicity is happening, thus the usage of | |
49 // the WeakPtr, which can only use from the thread that created it. | |
Roger Tawa OOO till Jul 10th
2012/07/05 16:13:22
use --> be used ?
MAD
2012/07/05 20:50:41
Done.
| |
50 class VersionReader | |
51 : public base::RefCountedThreadSafe<VersionReader> { | |
52 public: | |
53 explicit VersionReader(base::WeakPtr<VersionUpdaterWin> version_updater) | |
Roger Tawa OOO till Jul 10th
2012/07/05 16:13:22
const ref?
MAD
2012/07/05 20:50:41
Done.
| |
54 : version_updater_(version_updater) { | |
55 } | |
Roger Tawa OOO till Jul 10th
2012/07/05 16:13:22
add DCHECK to make sure we are on UI thread?
MAD
2012/07/05 20:50:41
Actually, we don't really care where this one come
| |
56 | |
57 void GetVersionFromFileThread() { | |
58 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
59 installed_version_.reset(InstallUtil::GetChromeVersion(dist, false)); | |
60 if (!installed_version_.get()) { | |
61 // User-level Chrome is not installed, check system-level. | |
62 installed_version_.reset(InstallUtil::GetChromeVersion(dist, true)); | |
63 } | |
64 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | |
65 &VersionReader::SetVersionInUIThread, this)); | |
Roger Tawa OOO till Jul 10th
2012/07/05 16:13:22
if installed_version_ is still null at line 64, is
MAD
2012/07/05 20:50:41
It doesn't actually ignore, it still makes a call
| |
66 } | |
67 | |
68 void SetVersionInUIThread() { | |
69 if (version_updater_.get() != NULL) | |
70 version_updater_->GotInstalledVersion(installed_version_.get()); | |
71 } | |
72 | |
73 private: | |
74 friend class base::RefCountedThreadSafe<VersionReader>; | |
75 | |
76 base::WeakPtr<VersionUpdaterWin> version_updater_; | |
77 scoped_ptr<Version> installed_version_; | |
78 }; | |
79 | |
80 } // namespace | |
81 | |
82 VersionUpdater* VersionUpdater::Create() { | |
83 return new VersionUpdaterWin; | |
84 } | |
85 | |
86 VersionUpdaterWin::VersionUpdaterWin() | |
87 : google_updater_(new GoogleUpdate()), | |
88 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
89 google_updater_->set_status_listener(this); | |
90 } | |
91 | |
92 VersionUpdaterWin::~VersionUpdaterWin() { | |
93 // The Google Updater will hold a pointer to us until it reports status, so we | |
94 // need to let it know that we will no longer be listening. | |
95 if (google_updater_) | |
96 google_updater_->set_status_listener(NULL); | |
97 } | |
98 | |
99 void VersionUpdaterWin::CheckForUpdate(const StatusCallback& callback) { | |
100 callback_ = callback; | |
101 | |
102 // On-demand updates for Chrome don't work in Vista RTM when UAC is turned | |
103 // off. So, in this case we just want the About box to not mention | |
104 // on-demand updates. Silent updates (in the background) should still | |
105 // work as before - enabling UAC or installing the latest service pack | |
106 // for Vista is another option. | |
107 if (!(base::win::GetVersion() == base::win::VERSION_VISTA && | |
108 (base::win::OSInfo::GetInstance()->service_pack().major == 0) && | |
109 !base::win::UserAccountControlIsEnabled())) { | |
Roger Tawa OOO till Jul 10th
2012/07/05 16:13:22
indent line 109 4 more spaces?
do you need to che
MAD
2012/07/05 20:50:41
Why? It's at the same logic level as 108, isn't it
| |
110 // This could happen if we already got results, but the page got refreshed. | |
111 if (!google_updater_) { | |
112 google_updater_ = new GoogleUpdate(); | |
113 google_updater_->set_status_listener(this); | |
114 } | |
115 UpdateStatus(UPGRADE_CHECK_STARTED, GOOGLE_UPDATE_NO_ERROR, string16()); | |
116 google_updater_->CheckForUpdate(false, GetWidget()); // Don't upgrade yet. | |
117 } | |
118 } | |
119 | |
120 void VersionUpdaterWin::RelaunchBrowser() const { | |
121 browser::AttemptRestart(); | |
122 } | |
123 | |
124 void VersionUpdaterWin::OnReportResults( | |
125 GoogleUpdateUpgradeResult result, GoogleUpdateErrorCode error_code, | |
126 const string16& error_message, const string16& version) { | |
127 // Drop the last reference to the object so that it gets cleaned up here. | |
128 google_updater_ = NULL; | |
129 UpdateStatus(result, error_code, error_message); | |
130 } | |
131 | |
132 void VersionUpdaterWin::UpdateStatus(GoogleUpdateUpgradeResult result, | |
133 GoogleUpdateErrorCode error_code, | |
134 const string16& error_message) { | |
135 #if !defined(GOOGLE_CHROME_BUILD) | |
136 // For Chromium builds it would show an error message. | |
137 // But it looks weird because in fact there is no error, | |
138 // just the update server is not available for non-official builds. | |
139 return; | |
140 #endif | |
Roger Tawa OOO till Jul 10th
2012/07/05 16:13:22
this is strange. why not reverse condition and pu
MAD
2012/07/05 20:50:41
Simply copied from:
http://code.google.com/searchf
| |
141 | |
142 Status status = UPDATED; | |
143 string16 message; | |
144 | |
145 switch (result) { | |
146 case UPGRADE_CHECK_STARTED: { | |
147 content::RecordAction(UserMetricsAction("UpgradeCheck_Started")); | |
148 status = CHECKING; | |
149 break; | |
150 } | |
151 case UPGRADE_STARTED: { | |
152 content::RecordAction(UserMetricsAction("Upgrade_Started")); | |
153 status = UPDATING; | |
154 break; | |
155 } | |
156 case UPGRADE_IS_AVAILABLE: { | |
157 content::RecordAction( | |
158 UserMetricsAction("UpgradeCheck_UpgradeIsAvailable")); | |
159 DCHECK(!google_updater_); // Should have been nulled out already. | |
160 google_updater_ = new GoogleUpdate(); | |
161 google_updater_->set_status_listener(this); | |
Roger Tawa OOO till Jul 10th
2012/07/05 16:13:22
lines 160 and 161 are repeated three times (if you
MAD
2012/07/05 20:50:41
Done.
| |
162 UpdateStatus(UPGRADE_STARTED, GOOGLE_UPDATE_NO_ERROR, string16()); | |
163 google_updater_->CheckForUpdate(true, GetWidget()); // Upgrade now. | |
Roger Tawa OOO till Jul 10th
2012/07/05 16:13:22
is the comment valid for this line ?
MAD
2012/07/05 20:50:41
Yes, this explains the passing of true...
Made it
| |
164 return; | |
165 } | |
166 case UPGRADE_ALREADY_UP_TO_DATE: { | |
167 // Google Update reported that Chrome is up-to-date. | |
168 // We need to confirm we are running the updated version. | |
169 // But we must defer version reading to the file thread... | |
170 // We'll handle the rest of this case within GotInstalledVersion below. | |
171 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind( | |
172 &VersionReader::GetVersionFromFileThread, | |
173 new VersionReader(weak_factory_.GetWeakPtr()))); | |
174 return; | |
175 } | |
176 case UPGRADE_SUCCESSFUL: { | |
177 content::RecordAction(UserMetricsAction("UpgradeCheck_Upgraded")); | |
178 status = NEARLY_UPDATED; | |
179 break; | |
180 } | |
181 case UPGRADE_ERROR: { | |
182 content::RecordAction(UserMetricsAction("UpgradeCheck_Error")); | |
183 status = FAILED; | |
184 if (!error_message.empty()) { | |
185 message = | |
186 l10n_util::GetStringFUTF16(IDS_ABOUT_BOX_ERROR_DURING_UPDATE_CHECK, | |
187 error_message); | |
188 } | |
189 if (error_code != GOOGLE_UPDATE_DISABLED_BY_POLICY) { | |
190 message = | |
191 l10n_util::GetStringFUTF16Int(IDS_UPGRADE_ERROR, error_code); | |
192 } else { | |
193 message = | |
194 l10n_util::GetStringUTF16(IDS_UPGRADE_DISABLED_BY_POLICY); | |
195 } | |
Roger Tawa OOO till Jul 10th
2012/07/05 16:13:22
I'm not sure I understand the logic of setting mes
MAD
2012/07/05 20:50:41
Good point, I don't know... :-)
This was stolen f
| |
196 break; | |
197 } | |
198 } | |
199 | |
200 // TODO(mad): Get proper progress value instead of passing 0. | |
Roger Tawa OOO till Jul 10th
2012/07/05 16:13:22
open a bug to track this?
MAD
2012/07/05 20:50:41
Done.
| |
201 callback_.Run(status, 0, message); | |
202 } | |
203 | |
204 void VersionUpdaterWin::GotInstalledVersion(Version* version) { | |
Roger Tawa OOO till Jul 10th
2012/07/05 16:13:22
use const Version*
MAD
2012/07/05 20:50:41
Done.
| |
205 // Make sure that we are running the latest version and if not, | |
206 // notify the user by setting the status to NEARLY_UPDATED. | |
207 // | |
208 // The extra version check is necessary on Windows because the application | |
209 // may be already up to date on disk though the running app is still | |
210 // out of date. | |
211 chrome::VersionInfo version_info; | |
212 scoped_ptr<Version> running_version( | |
213 Version::GetVersionFromString(version_info.Version())); | |
214 if (!version || (version->CompareTo(*running_version) <= 0)) { | |
215 content::RecordAction( | |
216 UserMetricsAction("UpgradeCheck_AlreadyUpToDate")); | |
217 callback_.Run(UPDATED, 0, string16()); | |
218 } else { | |
219 content::RecordAction(UserMetricsAction("UpgradeCheck_AlreadyUpgraded")); | |
220 callback_.Run(NEARLY_UPDATED, 0, string16()); | |
Roger Tawa OOO till Jul 10th
2012/07/05 16:13:22
The callback will be called in the file thread, wh
MAD
2012/07/05 20:50:41
Here, we should be in the UI thread... The file th
| |
221 } | |
222 } | |
OLD | NEW |