upgrade_detector_chromeos.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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/chromeos/upgrade_detector_chromeos.h"
6
7#include "base/memory/singleton.h"
8#include "chromeos/dbus/dbus_thread_manager.h"
9
10namespace {
11
12// How long to wait (each cycle) before checking which severity level we should
13// be at. Once we reach the highest severity, the timer will stop.
14const int kNotifyCycleTimeMs = 20 * 60 * 1000;  // 20 minutes.
15
16}  // namespace
17
18using chromeos::DBusThreadManager;
19using chromeos::UpdateEngineClient;
20
21UpgradeDetectorChromeos::UpgradeDetectorChromeos() : initialized_(false) {
22}
23
24UpgradeDetectorChromeos::~UpgradeDetectorChromeos() {
25}
26
27void UpgradeDetectorChromeos::Init() {
28  DBusThreadManager::Get()->GetUpdateEngineClient()->AddObserver(this);
29  initialized_ = true;
30}
31
32void UpgradeDetectorChromeos::Shutdown() {
33  // Init() may not be called from tests.
34  if (!initialized_)
35    return;
36  DBusThreadManager::Get()->GetUpdateEngineClient()->RemoveObserver(this);
37}
38
39void UpgradeDetectorChromeos::UpdateStatusChanged(
40    const UpdateEngineClient::Status& status) {
41  if (status.status != UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT)
42    return;
43
44  NotifyUpgradeDetected();
45
46  // ChromeOS shows upgrade arrow once the upgrade becomes available.
47  NotifyOnUpgrade();
48
49  // Setup timer to to move along the upgrade advisory system.
50  upgrade_notification_timer_.Start(
51      FROM_HERE, base::TimeDelta::FromMilliseconds(kNotifyCycleTimeMs),
52      this, &UpgradeDetectorChromeos::NotifyOnUpgrade);
53}
54
55void UpgradeDetectorChromeos::NotifyOnUpgrade() {
56  base::TimeDelta delta = base::Time::Now() - upgrade_detected_time();
57  int64 time_passed = delta.InDays();
58
59  const int kSevereThreshold = 7;
60  const int kHighThreshold = 4;
61  const int kElevatedThreshold = 2;
62  const int kLowThreshold = 0;
63
64  // These if statements must be sorted (highest interval first).
65  if (time_passed >= kSevereThreshold) {
66    set_upgrade_notification_stage(UPGRADE_ANNOYANCE_SEVERE);
67
68    // We can't get any higher, baby.
69    upgrade_notification_timer_.Stop();
70  } else if (time_passed >= kHighThreshold) {
71    set_upgrade_notification_stage(UPGRADE_ANNOYANCE_HIGH);
72  } else if (time_passed >= kElevatedThreshold) {
73    set_upgrade_notification_stage(UPGRADE_ANNOYANCE_ELEVATED);
74  } else if (time_passed >= kLowThreshold) {
75    set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW);
76  } else {
77    return;  // Not ready to recommend upgrade.
78  }
79
80  NotifyUpgradeRecommended();
81}
82
83// static
84UpgradeDetectorChromeos* UpgradeDetectorChromeos::GetInstance() {
85  return Singleton<UpgradeDetectorChromeos>::get();
86}
87
88// static
89UpgradeDetector* UpgradeDetector::GetInstance() {
90  return UpgradeDetectorChromeos::GetInstance();
91}
92