upgrade_detector.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
1// Copyright (c) 2012 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "chrome/browser/upgrade_detector.h" 6 7#include "base/bind.h" 8#include "base/command_line.h" 9#include "base/prefs/pref_registry_simple.h" 10#include "chrome/browser/chrome_notification_types.h" 11#include "chrome/browser/lifetime/application_lifetime.h" 12#include "chrome/browser/ui/browser_otr_state.h" 13#include "chrome/common/chrome_switches.h" 14#include "chrome/common/pref_names.h" 15#include "content/public/browser/notification_service.h" 16#include "grit/theme_resources.h" 17 18// How long to wait between checks for whether the user has been idle. 19static const int kIdleRepeatingTimerWait = 10; // Minutes (seconds if testing). 20 21// How much idle time (since last input even was detected) must have passed 22// until we notify that a critical update has occurred. 23static const int kIdleAmount = 2; // Hours (or seconds, if testing). 24 25bool UseTestingIntervals() { 26 // If a command line parameter specifying how long the upgrade check should 27 // be, we assume it is for testing and switch to using seconds instead of 28 // hours. 29 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess(); 30 return !cmd_line.GetSwitchValueASCII( 31 switches::kCheckForUpdateIntervalSec).empty(); 32} 33 34// static 35void UpgradeDetector::RegisterPrefs(PrefRegistrySimple* registry) { 36 registry->RegisterBooleanPref(prefs::kRestartLastSessionOnShutdown, false); 37 registry->RegisterBooleanPref(prefs::kWasRestarted, false); 38 registry->RegisterBooleanPref(prefs::kAttemptedToEnableAutoupdate, false); 39} 40 41int UpgradeDetector::GetIconResourceID() { 42 switch (upgrade_notification_stage_) { 43 case UPGRADE_ANNOYANCE_NONE: 44 return 0; 45 case UPGRADE_ANNOYANCE_LOW: 46 return IDR_UPDATE_MENU_SEVERITY_LOW; 47 case UPGRADE_ANNOYANCE_ELEVATED: 48 return IDR_UPDATE_MENU_SEVERITY_MEDIUM; 49 case UPGRADE_ANNOYANCE_HIGH: 50 return IDR_UPDATE_MENU_SEVERITY_HIGH; 51 case UPGRADE_ANNOYANCE_SEVERE: 52 return IDR_UPDATE_MENU_SEVERITY_HIGH; 53 case UPGRADE_ANNOYANCE_CRITICAL: 54 return IDR_UPDATE_MENU_SEVERITY_HIGH; 55 } 56 NOTREACHED(); 57 return 0; 58} 59 60UpgradeDetector::UpgradeDetector() 61 : upgrade_available_(UPGRADE_AVAILABLE_NONE), 62 critical_update_acknowledged_(false), 63 upgrade_notification_stage_(UPGRADE_ANNOYANCE_NONE), 64 notify_upgrade_(false) { 65} 66 67UpgradeDetector::~UpgradeDetector() { 68} 69 70void UpgradeDetector::NotifyUpgradeDetected() { 71 upgrade_detected_time_ = base::Time::Now(); 72 critical_update_acknowledged_ = false; 73} 74 75void UpgradeDetector::NotifyUpgradeRecommended() { 76 notify_upgrade_ = true; 77 78 content::NotificationService::current()->Notify( 79 chrome::NOTIFICATION_UPGRADE_RECOMMENDED, 80 content::Source<UpgradeDetector>(this), 81 content::NotificationService::NoDetails()); 82 83 switch (upgrade_available_) { 84 case UPGRADE_NEEDED_OUTDATED_INSTALL: { 85 content::NotificationService::current()->Notify( 86 chrome::NOTIFICATION_OUTDATED_INSTALL, 87 content::Source<UpgradeDetector>(this), 88 content::NotificationService::NoDetails()); 89 break; 90 } 91 case UPGRADE_NEEDED_OUTDATED_INSTALL_NO_AU: { 92 content::NotificationService::current()->Notify( 93 chrome::NOTIFICATION_OUTDATED_INSTALL_NO_AU, 94 content::Source<UpgradeDetector>(this), 95 content::NotificationService::NoDetails()); 96 break; 97 } 98 case UPGRADE_AVAILABLE_CRITICAL: { 99 int idle_timer = UseTestingIntervals() ? 100 kIdleRepeatingTimerWait : 101 kIdleRepeatingTimerWait * 60; // To minutes. 102 idle_check_timer_.Start(FROM_HERE, 103 base::TimeDelta::FromSeconds(idle_timer), 104 this, &UpgradeDetector::CheckIdle); 105 break; 106 } 107 default: 108 break; 109 } 110} 111 112void UpgradeDetector::CheckIdle() { 113 // CalculateIdleState expects an interval in seconds. 114 int idle_time_allowed = UseTestingIntervals() ? kIdleAmount : 115 kIdleAmount * 60 * 60; 116 117 CalculateIdleState( 118 idle_time_allowed, base::Bind(&UpgradeDetector::IdleCallback, 119 base::Unretained(this))); 120} 121 122void UpgradeDetector::IdleCallback(IdleState state) { 123 // Don't proceed while an incognito window is open. The timer will still 124 // keep firing, so this function will get a chance to re-evaluate this. 125 if (chrome::IsOffTheRecordSessionActive()) 126 return; 127 128 switch (state) { 129 case IDLE_STATE_LOCKED: 130 // Computer is locked, auto-restart. 131 idle_check_timer_.Stop(); 132 chrome::AttemptRestart(); 133 break; 134 case IDLE_STATE_IDLE: 135 // Computer has been idle for long enough, show warning. 136 idle_check_timer_.Stop(); 137 content::NotificationService::current()->Notify( 138 chrome::NOTIFICATION_CRITICAL_UPGRADE_INSTALLED, 139 content::Source<UpgradeDetector>(this), 140 content::NotificationService::NoDetails()); 141 break; 142 case IDLE_STATE_ACTIVE: 143 case IDLE_STATE_UNKNOWN: 144 break; 145 default: 146 NOTREACHED(); // Need to add any new value above (either providing 147 // automatic restart or show notification to user). 148 break; 149 } 150} 151