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 #include "chromeos/dbus/update_engine_client.h" |
| 10 |
| 11 using chromeos::DBusThreadManager; |
| 12 using chromeos::UpdateEngineClient; |
9 | 13 |
10 namespace { | 14 namespace { |
11 | 15 |
12 // How long to wait (each cycle) before checking which severity level we should | 16 // 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. | 17 // be at. Once we reach the highest severity, the timer will stop. |
14 const int kNotifyCycleTimeMs = 20 * 60 * 1000; // 20 minutes. | 18 const int kNotifyCycleTimeMs = 20 * 60 * 1000; // 20 minutes. |
15 | 19 |
16 } // namespace | 20 } // namespace |
17 | 21 |
18 using chromeos::DBusThreadManager; | 22 class UpgradeDetectorChromeos::ChannelsRequester { |
19 using chromeos::UpdateEngineClient; | 23 public: |
| 24 typedef base::Callback<void(const std::string&, const std::string&)> |
| 25 OnChannelsReceivedCallback; |
20 | 26 |
21 UpgradeDetectorChromeos::UpgradeDetectorChromeos() : initialized_(false) { | 27 ChannelsRequester() : weak_factory_(this) {} |
| 28 |
| 29 void RequestChannels(const OnChannelsReceivedCallback& callback) { |
| 30 UpdateEngineClient* client = |
| 31 DBusThreadManager::Get()->GetUpdateEngineClient(); |
| 32 callback_ = callback; |
| 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())); |
| 39 } |
| 40 |
| 41 private: |
| 42 void SetCurrentChannel(const std::string& current_channel) { |
| 43 DCHECK(!current_channel.empty()); |
| 44 current_channel_ = current_channel; |
| 45 TriggerCallbackIfReady(); |
| 46 } |
| 47 |
| 48 void SetTargetChannel(const std::string& target_channel) { |
| 49 DCHECK(!target_channel.empty()); |
| 50 target_channel_ = target_channel; |
| 51 TriggerCallbackIfReady(); |
| 52 } |
| 53 |
| 54 void TriggerCallbackIfReady() { |
| 55 if (current_channel_.empty() || target_channel_.empty()) |
| 56 return; |
| 57 if (!callback_.is_null()) |
| 58 callback_.Run(current_channel_, target_channel_); |
| 59 } |
| 60 |
| 61 std::string current_channel_; |
| 62 std::string target_channel_; |
| 63 |
| 64 OnChannelsReceivedCallback callback_; |
| 65 |
| 66 base::WeakPtrFactory<ChannelsRequester> weak_factory_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(ChannelsRequester); |
| 69 }; |
| 70 |
| 71 UpgradeDetectorChromeos::UpgradeDetectorChromeos() |
| 72 : initialized_(false), weak_factory_(this) { |
22 } | 73 } |
23 | 74 |
24 UpgradeDetectorChromeos::~UpgradeDetectorChromeos() { | 75 UpgradeDetectorChromeos::~UpgradeDetectorChromeos() { |
25 } | 76 } |
26 | 77 |
27 void UpgradeDetectorChromeos::Init() { | 78 void UpgradeDetectorChromeos::Init() { |
28 DBusThreadManager::Get()->GetUpdateEngineClient()->AddObserver(this); | 79 DBusThreadManager::Get()->GetUpdateEngineClient()->AddObserver(this); |
29 initialized_ = true; | 80 initialized_ = true; |
30 } | 81 } |
31 | 82 |
32 void UpgradeDetectorChromeos::Shutdown() { | 83 void UpgradeDetectorChromeos::Shutdown() { |
33 // Init() may not be called from tests. | 84 // Init() may not be called from tests. |
34 if (!initialized_) | 85 if (!initialized_) |
35 return; | 86 return; |
36 DBusThreadManager::Get()->GetUpdateEngineClient()->RemoveObserver(this); | 87 DBusThreadManager::Get()->GetUpdateEngineClient()->RemoveObserver(this); |
37 } | 88 } |
38 | 89 |
39 void UpgradeDetectorChromeos::UpdateStatusChanged( | 90 void UpgradeDetectorChromeos::UpdateStatusChanged( |
40 const UpdateEngineClient::Status& status) { | 91 const UpdateEngineClient::Status& status) { |
41 if (status.status != UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT) | 92 if (status.status != UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT) |
42 return; | 93 return; |
43 | 94 |
44 upgrade_detected_time_ = base::Time::Now(); | 95 upgrade_detected_time_ = base::Time::Now(); |
45 | 96 |
46 // ChromeOS shows upgrade arrow once the upgrade becomes available. | 97 channels_requester_.reset(new UpgradeDetectorChromeos::ChannelsRequester()); |
47 NotifyOnUpgrade(); | 98 channels_requester_->RequestChannels( |
48 | 99 base::Bind(&UpgradeDetectorChromeos::OnChannelsReceived, |
49 // Setup timer to to move along the upgrade advisory system. | 100 weak_factory_.GetWeakPtr())); |
50 upgrade_notification_timer_.Start( | |
51 FROM_HERE, base::TimeDelta::FromMilliseconds(kNotifyCycleTimeMs), | |
52 this, &UpgradeDetectorChromeos::NotifyOnUpgrade); | |
53 } | 101 } |
54 | 102 |
55 void UpgradeDetectorChromeos::NotifyOnUpgrade() { | 103 void UpgradeDetectorChromeos::NotifyOnUpgrade() { |
56 base::TimeDelta delta = base::Time::Now() - upgrade_detected_time_; | 104 base::TimeDelta delta = base::Time::Now() - upgrade_detected_time_; |
57 int64 time_passed = delta.InDays(); | 105 int64 time_passed = delta.InDays(); |
58 | 106 |
59 const int kSevereThreshold = 7; | 107 const int kSevereThreshold = 7; |
60 const int kHighThreshold = 4; | 108 const int kHighThreshold = 4; |
61 const int kElevatedThreshold = 2; | 109 const int kElevatedThreshold = 2; |
62 const int kLowThreshold = 0; | 110 const int kLowThreshold = 0; |
(...skipping 10 matching lines...) Expand all Loading... |
73 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_ELEVATED); | 121 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_ELEVATED); |
74 } else if (time_passed >= kLowThreshold) { | 122 } else if (time_passed >= kLowThreshold) { |
75 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW); | 123 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW); |
76 } else { | 124 } else { |
77 return; // Not ready to recommend upgrade. | 125 return; // Not ready to recommend upgrade. |
78 } | 126 } |
79 | 127 |
80 NotifyUpgradeRecommended(); | 128 NotifyUpgradeRecommended(); |
81 } | 129 } |
82 | 130 |
| 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)); |
| 139 |
| 140 // ChromeOS shows upgrade arrow once the upgrade becomes available. |
| 141 NotifyOnUpgrade(); |
| 142 |
| 143 // Setup timer to to move along the upgrade advisory system. |
| 144 upgrade_notification_timer_.Start( |
| 145 FROM_HERE, |
| 146 base::TimeDelta::FromMilliseconds(kNotifyCycleTimeMs), |
| 147 this, |
| 148 &UpgradeDetectorChromeos::NotifyOnUpgrade); |
| 149 } |
| 150 |
83 // static | 151 // static |
84 UpgradeDetectorChromeos* UpgradeDetectorChromeos::GetInstance() { | 152 UpgradeDetectorChromeos* UpgradeDetectorChromeos::GetInstance() { |
85 return Singleton<UpgradeDetectorChromeos>::get(); | 153 return Singleton<UpgradeDetectorChromeos>::get(); |
86 } | 154 } |
87 | 155 |
88 // static | 156 // static |
89 UpgradeDetector* UpgradeDetector::GetInstance() { | 157 UpgradeDetector* UpgradeDetector::GetInstance() { |
90 return UpgradeDetectorChromeos::GetInstance(); | 158 return UpgradeDetectorChromeos::GetInstance(); |
91 } | 159 } |
OLD | NEW |