| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2014 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 16 |  | 
| Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 17 | #include "update_engine/update_manager/boxed_value.h" | 
|  | 18 |  | 
| Ben Chan | dcf5a1d | 2014-08-07 09:34:12 -0700 | [diff] [blame] | 19 | #include <stdint.h> | 
|  | 20 |  | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 21 | #include <set> | 
|  | 22 | #include <string> | 
|  | 23 |  | 
|  | 24 | #include <base/strings/string_number_conversions.h> | 
|  | 25 | #include <base/time/time.h> | 
| Miriam Polzer | a02a1f1 | 2020-08-27 08:30:14 +0200 | [diff] [blame] | 26 | #include <base/version.h> | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 27 |  | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 28 | #include "update_engine/common/connection_utils.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 29 | #include "update_engine/common/utils.h" | 
| Marton Hunyady | 0e0e354 | 2018-02-21 18:51:39 +0100 | [diff] [blame] | 30 | #include "update_engine/update_manager/rollback_prefs.h" | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 31 | #include "update_engine/update_manager/shill_provider.h" | 
|  | 32 | #include "update_engine/update_manager/updater_provider.h" | 
| Adolfo Victoria | 94ffe13 | 2018-06-28 16:14:56 -0700 | [diff] [blame] | 33 | #include "update_engine/update_manager/weekly_time.h" | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 34 |  | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 35 | using chromeos_update_engine::ConnectionTethering; | 
|  | 36 | using chromeos_update_engine::ConnectionType; | 
|  | 37 | using chromeos_update_engine::connection_utils::StringForConnectionType; | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 38 | using std::set; | 
|  | 39 | using std::string; | 
|  | 40 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 41 | namespace chromeos_update_manager { | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 42 |  | 
|  | 43 | // Template instantiation for common types; used in BoxedValue::ToString(). | 
|  | 44 | // Keep in sync with boxed_value_unitttest.cc. | 
|  | 45 |  | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 46 | template <> | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 47 | string BoxedValue::ValuePrinter<string>(const void* value) { | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 48 | const string* val = reinterpret_cast<const string*>(value); | 
|  | 49 | return *val; | 
|  | 50 | } | 
|  | 51 |  | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 52 | template <> | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 53 | string BoxedValue::ValuePrinter<int>(const void* value) { | 
| Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 54 | const int* val = reinterpret_cast<const int*>(value); | 
| Jakub Pawlowski | 7e1dcf7 | 2018-07-26 00:29:42 -0700 | [diff] [blame] | 55 | return base::NumberToString(*val); | 
| Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 56 | } | 
|  | 57 |  | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 58 | template <> | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 59 | string BoxedValue::ValuePrinter<unsigned int>(const void* value) { | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 60 | const unsigned int* val = reinterpret_cast<const unsigned int*>(value); | 
| Jakub Pawlowski | 7e1dcf7 | 2018-07-26 00:29:42 -0700 | [diff] [blame] | 61 | return base::NumberToString(*val); | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 62 | } | 
|  | 63 |  | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 64 | template <> | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 65 | string BoxedValue::ValuePrinter<int64_t>(const void* value) { | 
| Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 66 | const int64_t* val = reinterpret_cast<const int64_t*>(value); | 
| Jakub Pawlowski | 7e1dcf7 | 2018-07-26 00:29:42 -0700 | [diff] [blame] | 67 | return base::NumberToString(*val); | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 68 | } | 
|  | 69 |  | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 70 | template <> | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 71 | string BoxedValue::ValuePrinter<uint64_t>(const void* value) { | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 72 | const uint64_t* val = reinterpret_cast<const uint64_t*>(value); | 
| Amin Hassani | 2e4eda5 | 2019-01-07 14:01:17 -0800 | [diff] [blame] | 73 | return base::NumberToString(*val); | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 74 | } | 
|  | 75 |  | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 76 | template <> | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 77 | string BoxedValue::ValuePrinter<bool>(const void* value) { | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 78 | const bool* val = reinterpret_cast<const bool*>(value); | 
|  | 79 | return *val ? "true" : "false"; | 
|  | 80 | } | 
|  | 81 |  | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 82 | template <> | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 83 | string BoxedValue::ValuePrinter<double>(const void* value) { | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 84 | const double* val = reinterpret_cast<const double*>(value); | 
| Jakub Pawlowski | 7e1dcf7 | 2018-07-26 00:29:42 -0700 | [diff] [blame] | 85 | return base::NumberToString(*val); | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 86 | } | 
|  | 87 |  | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 88 | template <> | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 89 | string BoxedValue::ValuePrinter<base::Time>(const void* value) { | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 90 | const base::Time* val = reinterpret_cast<const base::Time*>(value); | 
|  | 91 | return chromeos_update_engine::utils::ToString(*val); | 
|  | 92 | } | 
|  | 93 |  | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 94 | template <> | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 95 | string BoxedValue::ValuePrinter<base::TimeDelta>(const void* value) { | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 96 | const base::TimeDelta* val = reinterpret_cast<const base::TimeDelta*>(value); | 
|  | 97 | return chromeos_update_engine::utils::FormatTimeDelta(*val); | 
|  | 98 | } | 
|  | 99 |  | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 100 | template <> | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 101 | string BoxedValue::ValuePrinter<ConnectionType>(const void* value) { | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 102 | const ConnectionType* val = reinterpret_cast<const ConnectionType*>(value); | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 103 | return StringForConnectionType(*val); | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 104 | } | 
|  | 105 |  | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 106 | template <> | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 107 | string BoxedValue::ValuePrinter<set<ConnectionType>>(const void* value) { | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 108 | string ret = ""; | 
|  | 109 | const set<ConnectionType>* val = | 
|  | 110 | reinterpret_cast<const set<ConnectionType>*>(value); | 
|  | 111 | for (auto& it : *val) { | 
|  | 112 | ConnectionType type = it; | 
|  | 113 | if (ret.size() > 0) | 
|  | 114 | ret += ","; | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 115 | ret += StringForConnectionType(type); | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 116 | } | 
|  | 117 | return ret; | 
|  | 118 | } | 
|  | 119 |  | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 120 | template <> | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 121 | string BoxedValue::ValuePrinter<ConnectionTethering>(const void* value) { | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 122 | const ConnectionTethering* val = | 
|  | 123 | reinterpret_cast<const ConnectionTethering*>(value); | 
|  | 124 | switch (*val) { | 
|  | 125 | case ConnectionTethering::kNotDetected: | 
|  | 126 | return "Not Detected"; | 
|  | 127 | case ConnectionTethering::kSuspected: | 
|  | 128 | return "Suspected"; | 
|  | 129 | case ConnectionTethering::kConfirmed: | 
|  | 130 | return "Confirmed"; | 
|  | 131 | case ConnectionTethering::kUnknown: | 
|  | 132 | return "Unknown"; | 
|  | 133 | } | 
|  | 134 | NOTREACHED(); | 
|  | 135 | return "Unknown"; | 
|  | 136 | } | 
|  | 137 |  | 
| Marton Hunyady | 0e0e354 | 2018-02-21 18:51:39 +0100 | [diff] [blame] | 138 | template <> | 
|  | 139 | string BoxedValue::ValuePrinter<RollbackToTargetVersion>(const void* value) { | 
|  | 140 | const RollbackToTargetVersion* val = | 
|  | 141 | reinterpret_cast<const RollbackToTargetVersion*>(value); | 
|  | 142 | switch (*val) { | 
|  | 143 | case RollbackToTargetVersion::kUnspecified: | 
|  | 144 | return "Unspecified"; | 
|  | 145 | case RollbackToTargetVersion::kDisabled: | 
|  | 146 | return "Disabled"; | 
| Marton Hunyady | d4bc462 | 2018-08-30 15:52:23 +0200 | [diff] [blame] | 147 | case RollbackToTargetVersion::kRollbackAndPowerwash: | 
|  | 148 | return "Rollback and powerwash"; | 
| Marton Hunyady | 31aefb9 | 2018-08-29 16:17:03 +0200 | [diff] [blame] | 149 | case RollbackToTargetVersion::kRollbackAndRestoreIfPossible: | 
|  | 150 | return "Rollback and restore if possible"; | 
| Marton Hunyady | 0e0e354 | 2018-02-21 18:51:39 +0100 | [diff] [blame] | 151 | case RollbackToTargetVersion::kMaxValue: | 
|  | 152 | NOTREACHED(); | 
|  | 153 | return "Max value"; | 
|  | 154 | } | 
|  | 155 | NOTREACHED(); | 
|  | 156 | return "Unknown"; | 
|  | 157 | } | 
|  | 158 |  | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 159 | template <> | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 160 | string BoxedValue::ValuePrinter<Stage>(const void* value) { | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 161 | const Stage* val = reinterpret_cast<const Stage*>(value); | 
|  | 162 | switch (*val) { | 
| Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 163 | case Stage::kIdle: | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 164 | return "Idle"; | 
|  | 165 | case Stage::kCheckingForUpdate: | 
|  | 166 | return "Checking For Update"; | 
|  | 167 | case Stage::kUpdateAvailable: | 
|  | 168 | return "Update Available"; | 
|  | 169 | case Stage::kDownloading: | 
|  | 170 | return "Downloading"; | 
|  | 171 | case Stage::kVerifying: | 
|  | 172 | return "Verifying"; | 
|  | 173 | case Stage::kFinalizing: | 
|  | 174 | return "Finalizing"; | 
|  | 175 | case Stage::kUpdatedNeedReboot: | 
|  | 176 | return "Updated, Need Reboot"; | 
|  | 177 | case Stage::kReportingErrorEvent: | 
|  | 178 | return "Reporting Error Event"; | 
|  | 179 | case Stage::kAttemptingRollback: | 
|  | 180 | return "Attempting Rollback"; | 
| Amin Hassani | 70a90f5 | 2020-09-15 15:30:09 -0700 | [diff] [blame] | 181 | case Stage::kCleanupPreviousUpdate: | 
|  | 182 | return "Cleanup Previous Update"; | 
| David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 183 | } | 
|  | 184 | NOTREACHED(); | 
|  | 185 | return "Unknown"; | 
|  | 186 | } | 
|  | 187 |  | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 188 | template <> | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 189 | string BoxedValue::ValuePrinter<UpdateRequestStatus>(const void* value) { | 
|  | 190 | const UpdateRequestStatus* val = | 
|  | 191 | reinterpret_cast<const UpdateRequestStatus*>(value); | 
|  | 192 | switch (*val) { | 
|  | 193 | case UpdateRequestStatus::kNone: | 
|  | 194 | return "None"; | 
|  | 195 | case UpdateRequestStatus::kInteractive: | 
|  | 196 | return "Interactive"; | 
|  | 197 | case UpdateRequestStatus::kPeriodic: | 
|  | 198 | return "Periodic"; | 
|  | 199 | } | 
|  | 200 | NOTREACHED(); | 
|  | 201 | return "Unknown"; | 
|  | 202 | } | 
|  | 203 |  | 
| Aaron Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 204 | template <> | 
|  | 205 | string BoxedValue::ValuePrinter<UpdateRestrictions>(const void* value) { | 
|  | 206 | const UpdateRestrictions* val = | 
|  | 207 | reinterpret_cast<const UpdateRestrictions*>(value); | 
|  | 208 |  | 
|  | 209 | if (*val == UpdateRestrictions::kNone) { | 
|  | 210 | return "None"; | 
|  | 211 | } | 
|  | 212 | string retval = "Flags:"; | 
|  | 213 | if (*val & kRestrictDownloading) { | 
|  | 214 | retval += " RestrictDownloading"; | 
|  | 215 | } | 
|  | 216 | return retval; | 
|  | 217 | } | 
|  | 218 |  | 
| Adolfo Victoria | 94ffe13 | 2018-06-28 16:14:56 -0700 | [diff] [blame] | 219 | template <> | 
|  | 220 | string BoxedValue::ValuePrinter<WeeklyTimeInterval>(const void* value) { | 
|  | 221 | const WeeklyTimeInterval* val = | 
|  | 222 | reinterpret_cast<const WeeklyTimeInterval*>(value); | 
|  | 223 | return val->ToString(); | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | template <> | 
|  | 227 | string BoxedValue::ValuePrinter<WeeklyTimeIntervalVector>(const void* value) { | 
|  | 228 | const WeeklyTimeIntervalVector* val = | 
|  | 229 | reinterpret_cast<const WeeklyTimeIntervalVector*>(value); | 
|  | 230 |  | 
|  | 231 | string retval = "Disallowed intervals:\n"; | 
|  | 232 | for (const auto& interval : *val) { | 
|  | 233 | retval += interval.ToString() + "\n"; | 
|  | 234 | } | 
|  | 235 | return retval; | 
|  | 236 | } | 
|  | 237 |  | 
| Miriam Polzer | f197cdb | 2020-08-27 08:18:29 +0200 | [diff] [blame] | 238 | template <> | 
|  | 239 | string BoxedValue::ValuePrinter<ChannelDowngradeBehavior>(const void* value) { | 
|  | 240 | const ChannelDowngradeBehavior* val = | 
|  | 241 | reinterpret_cast<const ChannelDowngradeBehavior*>(value); | 
|  | 242 | switch (*val) { | 
|  | 243 | case ChannelDowngradeBehavior::kUnspecified: | 
|  | 244 | return "Unspecified"; | 
|  | 245 | case ChannelDowngradeBehavior::kWaitForVersionToCatchUp: | 
|  | 246 | return "Wait for the target channel to catch up"; | 
|  | 247 | case ChannelDowngradeBehavior::kRollback: | 
|  | 248 | return "Roll back and powerwash on channel downgrade"; | 
|  | 249 | case ChannelDowngradeBehavior::kAllowUserToConfigure: | 
|  | 250 | return "User decides on channel downgrade behavior"; | 
|  | 251 | } | 
|  | 252 | NOTREACHED(); | 
|  | 253 | return "Unknown"; | 
|  | 254 | } | 
|  | 255 |  | 
| Miriam Polzer | a02a1f1 | 2020-08-27 08:30:14 +0200 | [diff] [blame] | 256 | template <> | 
|  | 257 | string BoxedValue::ValuePrinter<base::Version>(const void* value) { | 
|  | 258 | const base::Version* val = reinterpret_cast<const base::Version*>(value); | 
|  | 259 | if (val->IsValid()) | 
|  | 260 | return val->GetString(); | 
|  | 261 | return "Unknown"; | 
|  | 262 | } | 
|  | 263 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 264 | }  // namespace chromeos_update_manager |