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

Side by Side Diff: chrome/browser/upgrade_detector_impl.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_impl.h ('k') | chrome/chrome_browser.gypi » ('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_impl.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/singleton.h" 11 #include "base/memory/singleton.h"
12 #include "base/string_number_conversions.h" 12 #include "base/string_number_conversions.h"
13 #include "base/string_util.h" 13 #include "base/string_util.h"
14 #include "base/task.h" 14 #include "base/task.h"
15 #include "base/time.h" 15 #include "base/time.h"
16 #include "base/utf_string_conversions.h" 16 #include "base/utf_string_conversions.h"
17 #include "chrome/browser/platform_util.h" 17 #include "chrome/browser/platform_util.h"
18 #include "chrome/browser/prefs/pref_service.h"
19 #include "chrome/common/chrome_switches.h" 18 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/chrome_version_info.h" 19 #include "chrome/common/chrome_version_info.h"
21 #include "chrome/common/pref_names.h"
22 #include "chrome/installer/util/browser_distribution.h" 20 #include "chrome/installer/util/browser_distribution.h"
23 #include "content/browser/browser_thread.h" 21 #include "content/browser/browser_thread.h"
24 #include "content/common/notification_service.h"
25 #include "content/common/notification_type.h"
26 #include "grit/theme_resources.h"
27 #include "ui/base/resource/resource_bundle.h" 22 #include "ui/base/resource/resource_bundle.h"
28 23
29 #if defined(OS_WIN) 24 #if defined(OS_WIN)
30 #include "chrome/installer/util/install_util.h" 25 #include "chrome/installer/util/install_util.h"
31 #elif defined(OS_MACOSX) 26 #elif defined(OS_MACOSX)
32 #include "chrome/browser/cocoa/keystone_glue.h" 27 #include "chrome/browser/cocoa/keystone_glue.h"
33 #elif defined(OS_POSIX) 28 #elif defined(OS_POSIX)
34 #include "base/process_util.h" 29 #include "base/process_util.h"
35 #include "base/version.h" 30 #include "base/version.h"
36 #endif 31 #endif
37 32
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 { 33 namespace {
44 34
45 // How long (in milliseconds) to wait (each cycle) before checking whether 35 // How long (in milliseconds) to wait (each cycle) before checking whether
46 // Chrome's been upgraded behind our back. 36 // Chrome's been upgraded behind our back.
47 const int kCheckForUpgradeMs = 2 * 60 * 60 * 1000; // 2 hours. 37 const int kCheckForUpgradeMs = 2 * 60 * 60 * 1000; // 2 hours.
48 38
49 // How long to wait (each cycle) before checking which severity level we should 39 // 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. 40 // be at. Once we reach the highest severity, the timer will stop.
51 const int kNotifyCycleTimeMs = 20 * 60 * 1000; // 20 minutes. 41 const int kNotifyCycleTimeMs = 20 * 60 * 1000; // 20 minutes.
52 42
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 } 140 }
151 } 141 }
152 142
153 private: 143 private:
154 Task* upgrade_detected_task_; 144 Task* upgrade_detected_task_;
155 bool* is_unstable_channel_; 145 bool* is_unstable_channel_;
156 }; 146 };
157 147
158 } // namespace 148 } // namespace
159 149
160 // static 150 UpgradeDetectorImpl::UpgradeDetectorImpl()
161 void UpgradeDetector::RegisterPrefs(PrefService* prefs) {
162 prefs->RegisterBooleanPref(prefs::kRestartLastSessionOnShutdown, false);
163 }
164
165 int UpgradeDetector::GetIconResourceID(UpgradeNotificationIconType type) {
166 bool badge = type == UPGRADE_ICON_TYPE_BADGE;
167 switch (upgrade_notification_stage_) {
168 case UPGRADE_ANNOYANCE_SEVERE:
169 return badge ? IDR_UPDATE_BADGE4 : IDR_UPDATE_MENU4;
170 case UPGRADE_ANNOYANCE_HIGH:
171 return badge ? IDR_UPDATE_BADGE3 : IDR_UPDATE_MENU3;
172 case UPGRADE_ANNOYANCE_ELEVATED:
173 return badge ? IDR_UPDATE_BADGE2 : IDR_UPDATE_MENU2;
174 case UPGRADE_ANNOYANCE_LOW:
175 return badge ? IDR_UPDATE_BADGE : IDR_UPDATE_MENU;
176 default:
177 return 0;
178 }
179 }
180
181 UpgradeDetector::UpgradeDetector()
182 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), 151 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)),
183 is_unstable_channel_(false), 152 is_unstable_channel_(false) {
184 upgrade_notification_stage_(UPGRADE_ANNOYANCE_NONE),
185 notify_upgrade_(false) {
186 CommandLine command_line(*CommandLine::ForCurrentProcess()); 153 CommandLine command_line(*CommandLine::ForCurrentProcess());
187 if (command_line.HasSwitch(switches::kDisableBackgroundNetworking)) 154 if (command_line.HasSwitch(switches::kDisableBackgroundNetworking))
188 return; 155 return;
189 // Windows: only enable upgrade notifications for official builds. 156 // Windows: only enable upgrade notifications for official builds.
190 // Mac: only enable them if the updater (Keystone) is present. 157 // Mac: only enable them if the updater (Keystone) is present.
191 // Linux (and other POSIX): always enable regardless of branding. 158 // Linux (and other POSIX): always enable regardless of branding.
192 #if (defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)) || defined(OS_POSIX) 159 #if (defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)) || defined(OS_POSIX)
193 #if defined(OS_MACOSX) 160 #if defined(OS_MACOSX)
194 if (keystone_glue::KeystoneEnabled()) 161 if (keystone_glue::KeystoneEnabled())
195 #endif 162 #endif
196 { 163 {
197 detect_upgrade_timer_.Start( 164 detect_upgrade_timer_.Start(
198 base::TimeDelta::FromMilliseconds(GetCheckForUpgradeEveryMs()), 165 base::TimeDelta::FromMilliseconds(GetCheckForUpgradeEveryMs()),
199 this, &UpgradeDetector::CheckForUpgrade); 166 this, &UpgradeDetectorImpl::CheckForUpgrade);
200 } 167 }
201 #endif 168 #endif
202 } 169 }
203 170
204 UpgradeDetector::~UpgradeDetector() { 171 UpgradeDetectorImpl::~UpgradeDetectorImpl() {
205 } 172 }
206 173
207 // static 174 void UpgradeDetectorImpl::CheckForUpgrade() {
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(); 175 method_factory_.RevokeAll();
221 Task* callback_task = 176 Task* callback_task =
222 method_factory_.NewRunnableMethod(&UpgradeDetector::UpgradeDetected); 177 method_factory_.NewRunnableMethod(&UpgradeDetectorImpl::UpgradeDetected);
223 // We use FILE as the thread to run the upgrade detection code on all 178 // 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 179 // 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 180 // while launching a background process and reading its output; on the Mac and
226 // on Windows checking for an upgrade requires reading a file. 181 // on Windows checking for an upgrade requires reading a file.
227 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 182 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
228 new DetectUpgradeTask(callback_task, 183 new DetectUpgradeTask(callback_task,
229 &is_unstable_channel_)); 184 &is_unstable_channel_));
230 #endif
231 } 185 }
232 186
233 void UpgradeDetector::UpgradeDetected() { 187 void UpgradeDetectorImpl::UpgradeDetected() {
234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
235 189
236 // Stop the recurring timer (that is checking for changes). 190 // Stop the recurring timer (that is checking for changes).
237 detect_upgrade_timer_.Stop(); 191 detect_upgrade_timer_.Stop();
238 192
239 upgrade_detected_time_ = base::Time::Now(); 193 NotifyUpgradeDetected();
240
241 NotificationService::current()->Notify(
242 NotificationType::UPGRADE_DETECTED,
243 Source<UpgradeDetector>(this),
244 NotificationService::NoDetails());
245 194
246 // Start the repeating timer for notifying the user after a certain period. 195 // 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 196 // The called function will eventually figure out that enough time has passed
248 // and stop the timer. 197 // and stop the timer.
249 int cycle_time = CmdLineInterval().empty() ? kNotifyCycleTimeMs : 198 int cycle_time = CmdLineInterval().empty() ? kNotifyCycleTimeMs :
250 kNotifyCycleTimeForTestingMs; 199 kNotifyCycleTimeForTestingMs;
251 upgrade_notification_timer_.Start( 200 upgrade_notification_timer_.Start(
252 base::TimeDelta::FromMilliseconds(cycle_time), 201 base::TimeDelta::FromMilliseconds(cycle_time),
253 this, &UpgradeDetector::NotifyOnUpgrade); 202 this, &UpgradeDetectorImpl::NotifyOnUpgrade);
254 } 203 }
255 204
256 void UpgradeDetector::NotifyOnUpgrade() { 205 void UpgradeDetectorImpl::NotifyOnUpgrade() {
257 base::TimeDelta delta = base::Time::Now() - upgrade_detected_time_; 206 base::TimeDelta delta = base::Time::Now() - upgrade_detected_time();
258 std::string interval = CmdLineInterval(); 207 std::string interval = CmdLineInterval();
259 208
260 // A command line interval implies testing, which we'll make more convenient 209 // A command line interval implies testing, which we'll make more convenient
261 // by switching to seconds of waiting instead of days between flipping 210 // by switching to seconds of waiting instead of days between flipping
262 // severity. This works in conjunction with the similar interval.empty() 211 // severity. This works in conjunction with the similar interval.empty()
263 // check below. 212 // check below.
264 int64 time_passed = interval.empty() ? delta.InHours() : delta.InSeconds(); 213 int64 time_passed = interval.empty() ? delta.InHours() : delta.InSeconds();
265 214
266 if (is_unstable_channel_) { 215 if (is_unstable_channel_) {
267 // There's only one threat level for unstable channels like dev and 216 // 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 217 // canary, and it hits after one hour. During testing, it hits after one
269 // minute. 218 // minute.
270 const int kUnstableThreshold = 1; 219 const int kUnstableThreshold = 1;
271 220
272 if (time_passed >= kUnstableThreshold) { 221 if (time_passed >= kUnstableThreshold) {
273 upgrade_notification_stage_ = UPGRADE_ANNOYANCE_LOW; 222 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW);
274 223
275 // That's as high as it goes. 224 // That's as high as it goes.
276 upgrade_notification_timer_.Stop(); 225 upgrade_notification_timer_.Stop();
277 } else { 226 } else {
278 return; // Not ready to recommend upgrade. 227 return; // Not ready to recommend upgrade.
279 } 228 }
280 } else { 229 } else {
281 const int kMultiplier = interval.empty() ? 24 : 1; 230 const int kMultiplier = interval.empty() ? 24 : 1;
282 // 14 days when not testing, otherwise 14 seconds. 231 // 14 days when not testing, otherwise 14 seconds.
283 const int kSevereThreshold = 14 * kMultiplier; 232 const int kSevereThreshold = 14 * kMultiplier;
284 const int kHighThreshold = 7 * kMultiplier; 233 const int kHighThreshold = 7 * kMultiplier;
285 const int kElevatedThreshold = 4 * kMultiplier; 234 const int kElevatedThreshold = 4 * kMultiplier;
286 const int kLowThreshold = 2 * kMultiplier; 235 const int kLowThreshold = 2 * kMultiplier;
287 236
288 // These if statements must be sorted (highest interval first). 237 // These if statements must be sorted (highest interval first).
289 if (time_passed >= kSevereThreshold) { 238 if (time_passed >= kSevereThreshold) {
290 upgrade_notification_stage_ = UPGRADE_ANNOYANCE_SEVERE; 239 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_SEVERE);
291 240
292 // We can't get any higher, baby. 241 // We can't get any higher, baby.
293 upgrade_notification_timer_.Stop(); 242 upgrade_notification_timer_.Stop();
294 } else if (time_passed >= kHighThreshold) { 243 } else if (time_passed >= kHighThreshold) {
295 upgrade_notification_stage_ = UPGRADE_ANNOYANCE_HIGH; 244 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_HIGH);
296 } else if (time_passed >= kElevatedThreshold) { 245 } else if (time_passed >= kElevatedThreshold) {
297 upgrade_notification_stage_ = UPGRADE_ANNOYANCE_ELEVATED; 246 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_ELEVATED);
298 } else if (time_passed >= kLowThreshold) { 247 } else if (time_passed >= kLowThreshold) {
299 upgrade_notification_stage_ = UPGRADE_ANNOYANCE_LOW; 248 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW);
300 } else { 249 } else {
301 return; // Not ready to recommend upgrade. 250 return; // Not ready to recommend upgrade.
302 } 251 }
303 } 252 }
304 253
305 notify_upgrade_ = true; 254 NotifyUpgradeRecommended();
255 }
306 256
307 NotificationService::current()->Notify( 257 // static
308 NotificationType::UPGRADE_RECOMMENDED, 258 UpgradeDetectorImpl* UpgradeDetectorImpl::GetInstance() {
309 Source<UpgradeDetector>(this), 259 return Singleton<UpgradeDetectorImpl>::get();
310 NotificationService::NoDetails());
311 } 260 }
261
262 // static
263 UpgradeDetector* UpgradeDetector::GetInstance() {
264 return UpgradeDetectorImpl::GetInstance();
265 }
OLDNEW
« no previous file with comments | « chrome/browser/upgrade_detector_impl.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698