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.
5 #include "chrome/browser/chromeos/upgrade_detector_chromeos.h"
7 #include "base/memory/singleton.h"
8 #include "chromeos/dbus/dbus_thread_manager.h"
9 #include "chromeos/dbus/update_engine_client.h"
11 using chromeos::DBusThreadManager
;
12 using chromeos::UpdateEngineClient
;
16 // How long to wait (each cycle) before checking which severity level we should
17 // be at. Once we reach the highest severity, the timer will stop.
18 const int kNotifyCycleTimeMs
= 20 * 60 * 1000; // 20 minutes.
22 class UpgradeDetectorChromeos::ChannelsRequester
{
24 typedef base::Callback
<void(const std::string
&, const std::string
&)>
25 OnChannelsReceivedCallback
;
27 ChannelsRequester() : weak_factory_(this) {}
29 void RequestChannels(const OnChannelsReceivedCallback
& callback
) {
30 UpdateEngineClient
* client
=
31 DBusThreadManager::Get()->GetUpdateEngineClient();
33 client
->GetChannel(true /* get_current_channel */,
34 base::Bind(&ChannelsRequester::SetCurrentChannel
,
35 weak_factory_
.GetWeakPtr()));
36 client
->GetChannel(false /* get_current_channel */,
37 base::Bind(&ChannelsRequester::SetTargetChannel
,
38 weak_factory_
.GetWeakPtr()));
42 void SetCurrentChannel(const std::string
& current_channel
) {
43 DCHECK(!current_channel
.empty());
44 current_channel_
= current_channel
;
45 TriggerCallbackIfReady();
48 void SetTargetChannel(const std::string
& target_channel
) {
49 DCHECK(!target_channel
.empty());
50 target_channel_
= target_channel
;
51 TriggerCallbackIfReady();
54 void TriggerCallbackIfReady() {
55 if (current_channel_
.empty() || target_channel_
.empty())
57 if (!callback_
.is_null())
58 callback_
.Run(current_channel_
, target_channel_
);
61 std::string current_channel_
;
62 std::string target_channel_
;
64 OnChannelsReceivedCallback callback_
;
66 base::WeakPtrFactory
<ChannelsRequester
> weak_factory_
;
68 DISALLOW_COPY_AND_ASSIGN(ChannelsRequester
);
71 UpgradeDetectorChromeos::UpgradeDetectorChromeos()
72 : initialized_(false), weak_factory_(this) {
75 UpgradeDetectorChromeos::~UpgradeDetectorChromeos() {
78 void UpgradeDetectorChromeos::Init() {
79 DBusThreadManager::Get()->GetUpdateEngineClient()->AddObserver(this);
83 void UpgradeDetectorChromeos::Shutdown() {
84 // Init() may not be called from tests.
87 DBusThreadManager::Get()->GetUpdateEngineClient()->RemoveObserver(this);
90 void UpgradeDetectorChromeos::UpdateStatusChanged(
91 const UpdateEngineClient::Status
& status
) {
92 if (status
.status
!= UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT
)
95 upgrade_detected_time_
= base::Time::Now();
97 channels_requester_
.reset(new UpgradeDetectorChromeos::ChannelsRequester());
98 channels_requester_
->RequestChannels(
99 base::Bind(&UpgradeDetectorChromeos::OnChannelsReceived
,
100 weak_factory_
.GetWeakPtr()));
103 void UpgradeDetectorChromeos::NotifyOnUpgrade() {
104 base::TimeDelta delta
= base::Time::Now() - upgrade_detected_time_
;
105 int64 time_passed
= delta
.InDays();
107 const int kSevereThreshold
= 7;
108 const int kHighThreshold
= 4;
109 const int kElevatedThreshold
= 2;
110 const int kLowThreshold
= 0;
112 // These if statements must be sorted (highest interval first).
113 if (time_passed
>= kSevereThreshold
) {
114 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_SEVERE
);
116 // We can't get any higher, baby.
117 upgrade_notification_timer_
.Stop();
118 } else if (time_passed
>= kHighThreshold
) {
119 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_HIGH
);
120 } else if (time_passed
>= kElevatedThreshold
) {
121 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_ELEVATED
);
122 } else if (time_passed
>= kLowThreshold
) {
123 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW
);
125 return; // Not ready to recommend upgrade.
128 NotifyUpgradeRecommended();
131 void UpgradeDetectorChromeos::OnChannelsReceived(
132 const std::string
& current_channel
,
133 const std::string
& target_channel
) {
134 // As current update engine status is UPDATE_STATUS_UPDATED_NEED_REBOOT
135 // and target channel is more stable than current channel, powerwash
136 // will be performed after reboot.
137 set_is_factory_reset_required(UpdateEngineClient::IsTargetChannelMoreStable(
138 current_channel
, target_channel
));
140 // ChromeOS shows upgrade arrow once the upgrade becomes available.
143 // Setup timer to to move along the upgrade advisory system.
144 upgrade_notification_timer_
.Start(
146 base::TimeDelta::FromMilliseconds(kNotifyCycleTimeMs
),
148 &UpgradeDetectorChromeos::NotifyOnUpgrade
);
152 UpgradeDetectorChromeos
* UpgradeDetectorChromeos::GetInstance() {
153 return base::Singleton
<UpgradeDetectorChromeos
>::get();
157 UpgradeDetector
* UpgradeDetector::GetInstance() {
158 return UpgradeDetectorChromeos::GetInstance();