summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/upgrade_detector_chromeos.cc
blob: 809ba4a73339798088f827fb4f3d58c671cbb220 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/upgrade_detector_chromeos.h"

#include "base/memory/singleton.h"
#include "chrome/browser/chromeos/cros/cros_library.h"

namespace {

// How long to wait (each cycle) before checking which severity level we should
// be at. Once we reach the highest severity, the timer will stop.
const int kNotifyCycleTimeMs = 20 * 60 * 1000;  // 20 minutes.

}  // namespace

UpgradeDetectorChromeos::UpgradeDetectorChromeos() {
  if (chromeos::CrosLibrary::Get())
    chromeos::CrosLibrary::Get()->GetUpdateLibrary()->AddObserver(this);
}

UpgradeDetectorChromeos::~UpgradeDetectorChromeos() {
  if (chromeos::CrosLibrary::Get())
    chromeos::CrosLibrary::Get()->GetUpdateLibrary()->RemoveObserver(this);
}

void UpgradeDetectorChromeos::UpdateStatusChanged(
    chromeos::UpdateLibrary* library) {
  if (library->status().status != chromeos::UPDATE_STATUS_UPDATED_NEED_REBOOT)
    return;

  NotifyUpgradeDetected();

  // ChromeOS shows upgrade arrow once the upgrade becomes available.
  NotifyOnUpgrade();

  // Setup timer to to move along the upgrade advisory system.
  upgrade_notification_timer_.Start(
      base::TimeDelta::FromMilliseconds(kNotifyCycleTimeMs),
      this, &UpgradeDetectorChromeos::NotifyOnUpgrade);
}

void UpgradeDetectorChromeos::NotifyOnUpgrade() {
  base::TimeDelta delta = base::Time::Now() - upgrade_detected_time();
  int64 time_passed = delta.InDays();

  const int kSevereThreshold = 7;
  const int kHighThreshold = 4;
  const int kElevatedThreshold = 2;
  const int kLowThreshold = 0;

  // These if statements must be sorted (highest interval first).
  if (time_passed >= kSevereThreshold) {
    set_upgrade_notification_stage(UPGRADE_ANNOYANCE_SEVERE);

    // We can't get any higher, baby.
    upgrade_notification_timer_.Stop();
  } else if (time_passed >= kHighThreshold) {
    set_upgrade_notification_stage(UPGRADE_ANNOYANCE_HIGH);
  } else if (time_passed >= kElevatedThreshold) {
    set_upgrade_notification_stage(UPGRADE_ANNOYANCE_ELEVATED);
  } else if (time_passed >= kLowThreshold) {
    set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW);
  } else {
    return;  // Not ready to recommend upgrade.
  }

  NotifyUpgradeRecommended();
}

// static
UpgradeDetectorChromeos* UpgradeDetectorChromeos::GetInstance() {
  return Singleton<UpgradeDetectorChromeos>::get();
}

// static
UpgradeDetector* UpgradeDetector::GetInstance() {
  return UpgradeDetectorChromeos::GetInstance();
}