OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/chromeos/upgrade_detector_chromeos.h" | 5 #include "chrome/browser/chromeos/upgrade_detector_chromeos.h" |
6 | 6 |
7 #include "base/memory/singleton.h" | 7 #include "base/memory/singleton.h" |
8 #include "chromeos/dbus/dbus_thread_manager.h" | 8 #include "chromeos/dbus/dbus_thread_manager.h" |
9 | 9 |
10 using chromeos::DBusThreadManager; | |
11 using chromeos::UpdateEngineClient; | |
12 | |
10 namespace { | 13 namespace { |
11 | 14 |
12 // How long to wait (each cycle) before checking which severity level we should | 15 // 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. | 16 // be at. Once we reach the highest severity, the timer will stop. |
14 const int kNotifyCycleTimeMs = 20 * 60 * 1000; // 20 minutes. | 17 const int kNotifyCycleTimeMs = 20 * 60 * 1000; // 20 minutes. |
15 | 18 |
19 bool IsTargetChannelMoreStable(const std::string& current_channel, | |
20 const std::string& target_channel) { | |
stevenjb
2014/10/01 22:26:44
This should either be moved to UpdateEngineClient
ygorshenin1
2014/10/02 14:21:35
Done.
| |
21 const auto& channels_list = UpdateEngineClient::kReleaseChannelsList; | |
22 auto cix = | |
23 std::find(channels_list.cbegin(), channels_list.cend(), current_channel); | |
24 auto tix = | |
25 std::find(channels_list.cbegin(), channels_list.cend(), target_channel); | |
26 return tix > cix; | |
27 } | |
28 | |
16 } // namespace | 29 } // namespace |
17 | 30 |
18 using chromeos::DBusThreadManager; | 31 class UpgradeDetectorChromeos::ChannelsRequester { |
19 using chromeos::UpdateEngineClient; | 32 public: |
33 ChannelsRequester() : weak_factory_(this) {} | |
20 | 34 |
21 UpgradeDetectorChromeos::UpgradeDetectorChromeos() : initialized_(false) { | 35 void RequestChannels(const base::Callback< |
36 void(const std::string&, const std::string&)>& callback) { | |
stevenjb
2014/10/01 22:26:44
This callback should be typedfed somewhere
ygorshenin1
2014/10/02 14:21:35
Done.
| |
37 UpdateEngineClient* client = | |
38 DBusThreadManager::Get()->GetUpdateEngineClient(); | |
39 callback_ = callback; | |
40 client->GetChannel(true /* get_current_channel */, | |
41 base::Bind(&ChannelsRequester::SetCurrentChannel, | |
42 weak_factory_.GetWeakPtr())); | |
43 client->GetChannel(false /* get_current_channel */, | |
44 base::Bind(&ChannelsRequester::SetTargetChannel, | |
45 weak_factory_.GetWeakPtr())); | |
46 } | |
47 | |
48 private: | |
49 void SetCurrentChannel(const std::string& current_channel) { | |
stevenjb
2014/10/01 22:26:44
DCHECK(!current_channel.empty()), otherwise an emp
ygorshenin1
2014/10/02 14:21:35
Done.
| |
50 current_channel_ = current_channel; | |
51 TriggerCallbackIfReady(); | |
52 } | |
53 | |
54 void SetTargetChannel(const std::string& target_channel) { | |
stevenjb
2014/10/01 22:26:44
Ditto.
ygorshenin1
2014/10/02 14:21:35
Done.
| |
55 target_channel_ = target_channel; | |
56 TriggerCallbackIfReady(); | |
57 } | |
58 | |
59 void TriggerCallbackIfReady() { | |
60 if (current_channel_.empty() || target_channel_.empty()) | |
61 return; | |
62 if (!callback_.is_null()) | |
63 callback_.Run(current_channel_, target_channel_); | |
64 } | |
65 | |
66 std::string current_channel_; | |
67 std::string target_channel_; | |
68 | |
69 base::Callback<void(const std::string&, const std::string&)> callback_; | |
70 | |
71 base::WeakPtrFactory<ChannelsRequester> weak_factory_; | |
72 | |
73 DISALLOW_COPY_AND_ASSIGN(ChannelsRequester); | |
74 }; | |
75 | |
76 UpgradeDetectorChromeos::UpgradeDetectorChromeos() | |
77 : initialized_(false), weak_factory_(this) { | |
22 } | 78 } |
23 | 79 |
24 UpgradeDetectorChromeos::~UpgradeDetectorChromeos() { | 80 UpgradeDetectorChromeos::~UpgradeDetectorChromeos() { |
25 } | 81 } |
26 | 82 |
27 void UpgradeDetectorChromeos::Init() { | 83 void UpgradeDetectorChromeos::Init() { |
28 DBusThreadManager::Get()->GetUpdateEngineClient()->AddObserver(this); | 84 DBusThreadManager::Get()->GetUpdateEngineClient()->AddObserver(this); |
29 initialized_ = true; | 85 initialized_ = true; |
30 } | 86 } |
31 | 87 |
32 void UpgradeDetectorChromeos::Shutdown() { | 88 void UpgradeDetectorChromeos::Shutdown() { |
33 // Init() may not be called from tests. | 89 // Init() may not be called from tests. |
34 if (!initialized_) | 90 if (!initialized_) |
35 return; | 91 return; |
36 DBusThreadManager::Get()->GetUpdateEngineClient()->RemoveObserver(this); | 92 DBusThreadManager::Get()->GetUpdateEngineClient()->RemoveObserver(this); |
37 } | 93 } |
38 | 94 |
39 void UpgradeDetectorChromeos::UpdateStatusChanged( | 95 void UpgradeDetectorChromeos::UpdateStatusChanged( |
40 const UpdateEngineClient::Status& status) { | 96 const UpdateEngineClient::Status& status) { |
41 if (status.status != UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT) | 97 if (status.status != UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT) |
42 return; | 98 return; |
43 | 99 |
44 upgrade_detected_time_ = base::Time::Now(); | 100 upgrade_detected_time_ = base::Time::Now(); |
45 | 101 |
46 // ChromeOS shows upgrade arrow once the upgrade becomes available. | 102 channels_requester_.reset(new UpgradeDetectorChromeos::ChannelsRequester()); |
47 NotifyOnUpgrade(); | 103 channels_requester_->RequestChannels( |
48 | 104 base::Bind(&UpgradeDetectorChromeos::OnChannelsReceived, |
49 // Setup timer to to move along the upgrade advisory system. | 105 weak_factory_.GetWeakPtr())); |
50 upgrade_notification_timer_.Start( | |
51 FROM_HERE, base::TimeDelta::FromMilliseconds(kNotifyCycleTimeMs), | |
52 this, &UpgradeDetectorChromeos::NotifyOnUpgrade); | |
53 } | 106 } |
54 | 107 |
55 void UpgradeDetectorChromeos::NotifyOnUpgrade() { | 108 void UpgradeDetectorChromeos::NotifyOnUpgrade() { |
56 base::TimeDelta delta = base::Time::Now() - upgrade_detected_time_; | 109 base::TimeDelta delta = base::Time::Now() - upgrade_detected_time_; |
57 int64 time_passed = delta.InDays(); | 110 int64 time_passed = delta.InDays(); |
58 | 111 |
59 const int kSevereThreshold = 7; | 112 const int kSevereThreshold = 7; |
60 const int kHighThreshold = 4; | 113 const int kHighThreshold = 4; |
61 const int kElevatedThreshold = 2; | 114 const int kElevatedThreshold = 2; |
62 const int kLowThreshold = 0; | 115 const int kLowThreshold = 0; |
(...skipping 10 matching lines...) Expand all Loading... | |
73 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_ELEVATED); | 126 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_ELEVATED); |
74 } else if (time_passed >= kLowThreshold) { | 127 } else if (time_passed >= kLowThreshold) { |
75 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW); | 128 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW); |
76 } else { | 129 } else { |
77 return; // Not ready to recommend upgrade. | 130 return; // Not ready to recommend upgrade. |
78 } | 131 } |
79 | 132 |
80 NotifyUpgradeRecommended(); | 133 NotifyUpgradeRecommended(); |
81 } | 134 } |
82 | 135 |
136 void UpgradeDetectorChromeos::OnChannelsReceived( | |
137 const std::string& current_channel, | |
138 const std::string& target_channel) { | |
139 set_is_factory_reset_required( | |
140 IsTargetChannelMoreStable(current_channel, target_channel)); | |
stevenjb
2014/10/01 22:26:44
Could you add a comment explaining why IsTargetCha
ygorshenin1
2014/10/02 14:21:35
Done.
| |
141 | |
142 // ChromeOS shows upgrade arrow once the upgrade becomes available. | |
143 NotifyOnUpgrade(); | |
144 | |
145 // Setup timer to to move along the upgrade advisory system. | |
146 upgrade_notification_timer_.Start( | |
147 FROM_HERE, | |
148 base::TimeDelta::FromMilliseconds(kNotifyCycleTimeMs), | |
149 this, | |
150 &UpgradeDetectorChromeos::NotifyOnUpgrade); | |
151 } | |
152 | |
83 // static | 153 // static |
84 UpgradeDetectorChromeos* UpgradeDetectorChromeos::GetInstance() { | 154 UpgradeDetectorChromeos* UpgradeDetectorChromeos::GetInstance() { |
85 return Singleton<UpgradeDetectorChromeos>::get(); | 155 return Singleton<UpgradeDetectorChromeos>::get(); |
86 } | 156 } |
87 | 157 |
88 // static | 158 // static |
89 UpgradeDetector* UpgradeDetector::GetInstance() { | 159 UpgradeDetector* UpgradeDetector::GetInstance() { |
90 return UpgradeDetectorChromeos::GetInstance(); | 160 return UpgradeDetectorChromeos::GetInstance(); |
91 } | 161 } |
OLD | NEW |