Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(688)

Side by Side Diff: chrome/browser/upgrade_detector.cc

Issue 7046096: Refactor UpgradeDetector. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sync and address finnur's comments in set 2 Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/upgrade_detector.h ('k') | chrome/browser/upgrade_detector_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/upgrade_detector.h" 5 #include "chrome/browser/upgrade_detector.h"
6 6
7 #include <string>
8
9 #include "base/command_line.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/singleton.h"
12 #include "base/string_number_conversions.h"
13 #include "base/string_util.h"
14 #include "base/task.h"
15 #include "base/time.h"
16 #include "base/utf_string_conversions.h"
17 #include "chrome/browser/platform_util.h"
18 #include "chrome/browser/prefs/pref_service.h" 7 #include "chrome/browser/prefs/pref_service.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/chrome_version_info.h"
21 #include "chrome/common/pref_names.h" 8 #include "chrome/common/pref_names.h"
22 #include "chrome/installer/util/browser_distribution.h"
23 #include "content/browser/browser_thread.h"
24 #include "content/common/notification_service.h" 9 #include "content/common/notification_service.h"
25 #include "content/common/notification_type.h" 10 #include "content/common/notification_type.h"
26 #include "grit/theme_resources.h" 11 #include "grit/theme_resources.h"
27 #include "ui/base/resource/resource_bundle.h"
28
29 #if defined(OS_WIN)
30 #include "chrome/installer/util/install_util.h"
31 #elif defined(OS_MACOSX)
32 #include "chrome/browser/cocoa/keystone_glue.h"
33 #elif defined(OS_POSIX)
34 #include "base/process_util.h"
35 #include "base/version.h"
36 #endif
37
38 #if defined(OS_CHROMEOS)
39 #include "chrome/browser/chromeos/cros/cros_library.h"
40 #include "chrome/browser/chromeos/cros/update_library.h"
41 #endif
42
43 namespace {
44
45 // How long (in milliseconds) to wait (each cycle) before checking whether
46 // Chrome's been upgraded behind our back.
47 const int kCheckForUpgradeMs = 2 * 60 * 60 * 1000; // 2 hours.
48
49 // How long to wait (each cycle) before checking which severity level we should
50 // be at. Once we reach the highest severity, the timer will stop.
51 const int kNotifyCycleTimeMs = 20 * 60 * 1000; // 20 minutes.
52
53 // Same as kNotifyCycleTimeMs but only used during testing.
54 const int kNotifyCycleTimeForTestingMs = 500; // Half a second.
55
56 std::string CmdLineInterval() {
57 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
58 return cmd_line.GetSwitchValueASCII(switches::kCheckForUpdateIntervalSec);
59 }
60
61 // How often to check for an upgrade.
62 int GetCheckForUpgradeEveryMs() {
63 // Check for a value passed via the command line.
64 int interval_ms;
65 std::string interval = CmdLineInterval();
66 if (!interval.empty() && base::StringToInt(interval, &interval_ms))
67 return interval_ms * 1000; // Command line value is in seconds.
68
69 return kCheckForUpgradeMs;
70 }
71
72 // This task checks the currently running version of Chrome against the
73 // installed version. If the installed version is newer, it runs the passed
74 // callback task. Otherwise it just deletes the task.
75 class DetectUpgradeTask : public Task {
76 public:
77 explicit DetectUpgradeTask(Task* upgrade_detected_task,
78 bool* is_unstable_channel)
79 : upgrade_detected_task_(upgrade_detected_task),
80 is_unstable_channel_(is_unstable_channel) {
81 }
82
83 virtual ~DetectUpgradeTask() {
84 if (upgrade_detected_task_) {
85 // This has to get deleted on the same thread it was created.
86 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
87 new DeleteTask<Task>(upgrade_detected_task_));
88 }
89 }
90
91 virtual void Run() {
92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
93
94 scoped_ptr<Version> installed_version;
95
96 #if defined(OS_WIN)
97 // Get the version of the currently *installed* instance of Chrome,
98 // which might be newer than the *running* instance if we have been
99 // upgraded in the background.
100 // TODO(tommi): Check if using the default distribution is always the right
101 // thing to do.
102 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
103 installed_version.reset(InstallUtil::GetChromeVersion(dist, false));
104 if (!installed_version.get()) {
105 // User level Chrome is not installed, check system level.
106 installed_version.reset(InstallUtil::GetChromeVersion(dist, true));
107 }
108 #elif defined(OS_MACOSX)
109 installed_version.reset(
110 Version::GetVersionFromString(UTF16ToASCII(
111 keystone_glue::CurrentlyInstalledVersion())));
112 #elif defined(OS_POSIX)
113 // POSIX but not Mac OS X: Linux, etc.
114 CommandLine command_line(*CommandLine::ForCurrentProcess());
115 command_line.AppendSwitch(switches::kProductVersion);
116 std::string reply;
117 if (!base::GetAppOutput(command_line, &reply)) {
118 DLOG(ERROR) << "Failed to get current file version";
119 return;
120 }
121
122 installed_version.reset(Version::GetVersionFromString(reply));
123 #endif
124
125 platform_util::Channel channel = platform_util::GetChannel();
126 *is_unstable_channel_ = channel == platform_util::CHANNEL_DEV ||
127 channel == platform_util::CHANNEL_CANARY;
128
129 // Get the version of the currently *running* instance of Chrome.
130 chrome::VersionInfo version_info;
131 if (!version_info.is_valid()) {
132 NOTREACHED() << "Failed to get current file version";
133 return;
134 }
135 scoped_ptr<Version> running_version(
136 Version::GetVersionFromString(version_info.Version()));
137 if (running_version.get() == NULL) {
138 NOTREACHED() << "Failed to parse version info";
139 return;
140 }
141
142 // |installed_version| may be NULL when the user downgrades on Linux (by
143 // switching from dev to beta channel, for example). The user needs a
144 // restart in this case as well. See http://crbug.com/46547
145 if (!installed_version.get() ||
146 (installed_version->CompareTo(*running_version) > 0)) {
147 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
148 upgrade_detected_task_);
149 upgrade_detected_task_ = NULL;
150 }
151 }
152
153 private:
154 Task* upgrade_detected_task_;
155 bool* is_unstable_channel_;
156 };
157
158 } // namespace
159 12
160 // static 13 // static
161 void UpgradeDetector::RegisterPrefs(PrefService* prefs) { 14 void UpgradeDetector::RegisterPrefs(PrefService* prefs) {
162 prefs->RegisterBooleanPref(prefs::kRestartLastSessionOnShutdown, false); 15 prefs->RegisterBooleanPref(prefs::kRestartLastSessionOnShutdown, false);
163 } 16 }
164 17
165 int UpgradeDetector::GetIconResourceID(UpgradeNotificationIconType type) { 18 int UpgradeDetector::GetIconResourceID(UpgradeNotificationIconType type) {
166 bool badge = type == UPGRADE_ICON_TYPE_BADGE; 19 bool badge = type == UPGRADE_ICON_TYPE_BADGE;
167 switch (upgrade_notification_stage_) { 20 switch (upgrade_notification_stage_) {
168 case UPGRADE_ANNOYANCE_SEVERE: 21 case UPGRADE_ANNOYANCE_SEVERE:
169 return badge ? IDR_UPDATE_BADGE4 : IDR_UPDATE_MENU4; 22 return badge ? IDR_UPDATE_BADGE4 : IDR_UPDATE_MENU4;
170 case UPGRADE_ANNOYANCE_HIGH: 23 case UPGRADE_ANNOYANCE_HIGH:
171 return badge ? IDR_UPDATE_BADGE3 : IDR_UPDATE_MENU3; 24 return badge ? IDR_UPDATE_BADGE3 : IDR_UPDATE_MENU3;
172 case UPGRADE_ANNOYANCE_ELEVATED: 25 case UPGRADE_ANNOYANCE_ELEVATED:
173 return badge ? IDR_UPDATE_BADGE2 : IDR_UPDATE_MENU2; 26 return badge ? IDR_UPDATE_BADGE2 : IDR_UPDATE_MENU2;
174 case UPGRADE_ANNOYANCE_LOW: 27 case UPGRADE_ANNOYANCE_LOW:
175 return badge ? IDR_UPDATE_BADGE : IDR_UPDATE_MENU; 28 return badge ? IDR_UPDATE_BADGE : IDR_UPDATE_MENU;
176 default: 29 default:
177 return 0; 30 return 0;
178 } 31 }
179 } 32 }
180 33
181 UpgradeDetector::UpgradeDetector() 34 UpgradeDetector::UpgradeDetector()
182 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), 35 : upgrade_notification_stage_(UPGRADE_ANNOYANCE_NONE),
183 is_unstable_channel_(false),
184 upgrade_notification_stage_(UPGRADE_ANNOYANCE_NONE),
185 notify_upgrade_(false) { 36 notify_upgrade_(false) {
186 CommandLine command_line(*CommandLine::ForCurrentProcess());
187 if (command_line.HasSwitch(switches::kDisableBackgroundNetworking))
188 return;
189 // Windows: only enable upgrade notifications for official builds.
190 // Mac: only enable them if the updater (Keystone) is present.
191 // Linux (and other POSIX): always enable regardless of branding.
192 #if (defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)) || defined(OS_POSIX)
193 #if defined(OS_MACOSX)
194 if (keystone_glue::KeystoneEnabled())
195 #endif
196 {
197 detect_upgrade_timer_.Start(
198 base::TimeDelta::FromMilliseconds(GetCheckForUpgradeEveryMs()),
199 this, &UpgradeDetector::CheckForUpgrade);
200 }
201 #endif
202 } 37 }
203 38
204 UpgradeDetector::~UpgradeDetector() { 39 UpgradeDetector::~UpgradeDetector() {
205 } 40 }
206 41
207 // static 42 void UpgradeDetector::NotifyUpgradeDetected() {
208 UpgradeDetector* UpgradeDetector::GetInstance() {
209 return Singleton<UpgradeDetector>::get();
210 }
211
212 void UpgradeDetector::CheckForUpgrade() {
213 #if defined(OS_CHROMEOS)
214 // For ChromeOS, check update library status to detect upgrade.
215 if (chromeos::CrosLibrary::Get()->GetUpdateLibrary()->status().status ==
216 chromeos::UPDATE_STATUS_UPDATED_NEED_REBOOT) {
217 UpgradeDetected();
218 }
219 #else
220 method_factory_.RevokeAll();
221 Task* callback_task =
222 method_factory_.NewRunnableMethod(&UpgradeDetector::UpgradeDetected);
223 // We use FILE as the thread to run the upgrade detection code on all
224 // platforms. For Linux, this is because we don't want to block the UI thread
225 // while launching a background process and reading its output; on the Mac and
226 // on Windows checking for an upgrade requires reading a file.
227 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
228 new DetectUpgradeTask(callback_task,
229 &is_unstable_channel_));
230 #endif
231 }
232
233 void UpgradeDetector::UpgradeDetected() {
234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
235
236 // Stop the recurring timer (that is checking for changes).
237 detect_upgrade_timer_.Stop();
238
239 upgrade_detected_time_ = base::Time::Now(); 43 upgrade_detected_time_ = base::Time::Now();
240 44
241 NotificationService::current()->Notify( 45 NotificationService::current()->Notify(
242 NotificationType::UPGRADE_DETECTED, 46 NotificationType::UPGRADE_DETECTED,
243 Source<UpgradeDetector>(this), 47 Source<UpgradeDetector>(this),
244 NotificationService::NoDetails()); 48 NotificationService::NoDetails());
245
246 // Start the repeating timer for notifying the user after a certain period.
247 // The called function will eventually figure out that enough time has passed
248 // and stop the timer.
249 int cycle_time = CmdLineInterval().empty() ? kNotifyCycleTimeMs :
250 kNotifyCycleTimeForTestingMs;
251 upgrade_notification_timer_.Start(
252 base::TimeDelta::FromMilliseconds(cycle_time),
253 this, &UpgradeDetector::NotifyOnUpgrade);
254 } 49 }
255 50
256 void UpgradeDetector::NotifyOnUpgrade() { 51 void UpgradeDetector::NotifyUpgradeRecommended() {
257 base::TimeDelta delta = base::Time::Now() - upgrade_detected_time_;
258 std::string interval = CmdLineInterval();
259
260 // A command line interval implies testing, which we'll make more convenient
261 // by switching to seconds of waiting instead of days between flipping
262 // severity. This works in conjunction with the similar interval.empty()
263 // check below.
264 int64 time_passed = interval.empty() ? delta.InHours() : delta.InSeconds();
265
266 if (is_unstable_channel_) {
267 // There's only one threat level for unstable channels like dev and
268 // canary, and it hits after one hour. During testing, it hits after one
269 // minute.
270 const int kUnstableThreshold = 1;
271
272 if (time_passed >= kUnstableThreshold) {
273 upgrade_notification_stage_ = UPGRADE_ANNOYANCE_LOW;
274
275 // That's as high as it goes.
276 upgrade_notification_timer_.Stop();
277 } else {
278 return; // Not ready to recommend upgrade.
279 }
280 } else {
281 const int kMultiplier = interval.empty() ? 24 : 1;
282 // 14 days when not testing, otherwise 14 seconds.
283 const int kSevereThreshold = 14 * kMultiplier;
284 const int kHighThreshold = 7 * kMultiplier;
285 const int kElevatedThreshold = 4 * kMultiplier;
286 const int kLowThreshold = 2 * kMultiplier;
287
288 // These if statements must be sorted (highest interval first).
289 if (time_passed >= kSevereThreshold) {
290 upgrade_notification_stage_ = UPGRADE_ANNOYANCE_SEVERE;
291
292 // We can't get any higher, baby.
293 upgrade_notification_timer_.Stop();
294 } else if (time_passed >= kHighThreshold) {
295 upgrade_notification_stage_ = UPGRADE_ANNOYANCE_HIGH;
296 } else if (time_passed >= kElevatedThreshold) {
297 upgrade_notification_stage_ = UPGRADE_ANNOYANCE_ELEVATED;
298 } else if (time_passed >= kLowThreshold) {
299 upgrade_notification_stage_ = UPGRADE_ANNOYANCE_LOW;
300 } else {
301 return; // Not ready to recommend upgrade.
302 }
303 }
304
305 notify_upgrade_ = true; 52 notify_upgrade_ = true;
306 53
307 NotificationService::current()->Notify( 54 NotificationService::current()->Notify(
308 NotificationType::UPGRADE_RECOMMENDED, 55 NotificationType::UPGRADE_RECOMMENDED,
309 Source<UpgradeDetector>(this), 56 Source<UpgradeDetector>(this),
310 NotificationService::NoDetails()); 57 NotificationService::NoDetails());
311 } 58 }
OLDNEW
« no previous file with comments | « chrome/browser/upgrade_detector.h ('k') | chrome/browser/upgrade_detector_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698