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> |
| 26 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 27 | #include "update_engine/common/utils.h" |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 28 | #include "update_engine/connection_utils.h" |
Marton Hunyady | 0e0e354 | 2018-02-21 18:51:39 +0100 | [diff] [blame] | 29 | #include "update_engine/update_manager/rollback_prefs.h" |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 30 | #include "update_engine/update_manager/shill_provider.h" |
| 31 | #include "update_engine/update_manager/updater_provider.h" |
Adolfo Victoria | 94ffe13 | 2018-06-28 16:14:56 -0700 | [diff] [blame] | 32 | #include "update_engine/update_manager/weekly_time.h" |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 33 | |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 34 | using chromeos_update_engine::ConnectionTethering; |
| 35 | using chromeos_update_engine::ConnectionType; |
| 36 | using chromeos_update_engine::connection_utils::StringForConnectionType; |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 37 | using std::set; |
| 38 | using std::string; |
| 39 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 40 | namespace chromeos_update_manager { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 41 | |
| 42 | // Template instantiation for common types; used in BoxedValue::ToString(). |
| 43 | // Keep in sync with boxed_value_unitttest.cc. |
| 44 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 45 | template <> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 46 | string BoxedValue::ValuePrinter<string>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 47 | const string* val = reinterpret_cast<const string*>(value); |
| 48 | return *val; |
| 49 | } |
| 50 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 51 | template <> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 52 | string BoxedValue::ValuePrinter<int>(const void* value) { |
Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 53 | const int* val = reinterpret_cast<const int*>(value); |
Amin Hassani | 2e4eda5 | 2019-01-07 14:01:17 -0800 | [diff] [blame] | 54 | #if BASE_VER < 576279 |
Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 55 | return base::IntToString(*val); |
Amin Hassani | 2e4eda5 | 2019-01-07 14:01:17 -0800 | [diff] [blame] | 56 | #else |
Jakub Pawlowski | 7e1dcf7 | 2018-07-26 00:29:42 -0700 | [diff] [blame] | 57 | return base::NumberToString(*val); |
Amin Hassani | 2e4eda5 | 2019-01-07 14:01:17 -0800 | [diff] [blame] | 58 | #endif |
Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 61 | template <> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 62 | string BoxedValue::ValuePrinter<unsigned int>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 63 | const unsigned int* val = reinterpret_cast<const unsigned int*>(value); |
Amin Hassani | 2e4eda5 | 2019-01-07 14:01:17 -0800 | [diff] [blame] | 64 | #if BASE_VER < 576279 |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 65 | return base::UintToString(*val); |
Amin Hassani | 2e4eda5 | 2019-01-07 14:01:17 -0800 | [diff] [blame] | 66 | #else |
Jakub Pawlowski | 7e1dcf7 | 2018-07-26 00:29:42 -0700 | [diff] [blame] | 67 | return base::NumberToString(*val); |
Amin Hassani | 2e4eda5 | 2019-01-07 14:01:17 -0800 | [diff] [blame] | 68 | #endif |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 71 | template <> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 72 | string BoxedValue::ValuePrinter<int64_t>(const void* value) { |
Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 73 | const int64_t* val = reinterpret_cast<const int64_t*>(value); |
Amin Hassani | 2e4eda5 | 2019-01-07 14:01:17 -0800 | [diff] [blame] | 74 | #if BASE_VER < 576279 |
Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 75 | return base::Int64ToString(*val); |
Amin Hassani | 2e4eda5 | 2019-01-07 14:01:17 -0800 | [diff] [blame] | 76 | #else |
Jakub Pawlowski | 7e1dcf7 | 2018-07-26 00:29:42 -0700 | [diff] [blame] | 77 | return base::NumberToString(*val); |
Amin Hassani | 2e4eda5 | 2019-01-07 14:01:17 -0800 | [diff] [blame] | 78 | #endif |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 81 | template <> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 82 | string BoxedValue::ValuePrinter<uint64_t>(const void* value) { |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 83 | const uint64_t* val = reinterpret_cast<const uint64_t*>(value); |
Amin Hassani | 2e4eda5 | 2019-01-07 14:01:17 -0800 | [diff] [blame] | 84 | #if BASE_VER < 576279 |
| 85 | return base::Uint64ToString(*val); |
| 86 | #else |
| 87 | return base::NumberToString(*val); |
| 88 | #endif |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 91 | template <> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 92 | string BoxedValue::ValuePrinter<bool>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 93 | const bool* val = reinterpret_cast<const bool*>(value); |
| 94 | return *val ? "true" : "false"; |
| 95 | } |
| 96 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 97 | template <> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 98 | string BoxedValue::ValuePrinter<double>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 99 | const double* val = reinterpret_cast<const double*>(value); |
Amin Hassani | 2e4eda5 | 2019-01-07 14:01:17 -0800 | [diff] [blame] | 100 | #if BASE_VER < 576279 |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 101 | return base::DoubleToString(*val); |
Amin Hassani | 2e4eda5 | 2019-01-07 14:01:17 -0800 | [diff] [blame] | 102 | #else |
Jakub Pawlowski | 7e1dcf7 | 2018-07-26 00:29:42 -0700 | [diff] [blame] | 103 | return base::NumberToString(*val); |
Amin Hassani | 2e4eda5 | 2019-01-07 14:01:17 -0800 | [diff] [blame] | 104 | #endif |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 107 | template <> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 108 | string BoxedValue::ValuePrinter<base::Time>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 109 | const base::Time* val = reinterpret_cast<const base::Time*>(value); |
| 110 | return chromeos_update_engine::utils::ToString(*val); |
| 111 | } |
| 112 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 113 | template <> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 114 | string BoxedValue::ValuePrinter<base::TimeDelta>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 115 | const base::TimeDelta* val = reinterpret_cast<const base::TimeDelta*>(value); |
| 116 | return chromeos_update_engine::utils::FormatTimeDelta(*val); |
| 117 | } |
| 118 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 119 | template <> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 120 | string BoxedValue::ValuePrinter<ConnectionType>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 121 | const ConnectionType* val = reinterpret_cast<const ConnectionType*>(value); |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 122 | return StringForConnectionType(*val); |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 125 | template <> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 126 | string BoxedValue::ValuePrinter<set<ConnectionType>>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 127 | string ret = ""; |
| 128 | const set<ConnectionType>* val = |
| 129 | reinterpret_cast<const set<ConnectionType>*>(value); |
| 130 | for (auto& it : *val) { |
| 131 | ConnectionType type = it; |
| 132 | if (ret.size() > 0) |
| 133 | ret += ","; |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 134 | ret += StringForConnectionType(type); |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 135 | } |
| 136 | return ret; |
| 137 | } |
| 138 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 139 | template <> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 140 | string BoxedValue::ValuePrinter<ConnectionTethering>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 141 | const ConnectionTethering* val = |
| 142 | reinterpret_cast<const ConnectionTethering*>(value); |
| 143 | switch (*val) { |
| 144 | case ConnectionTethering::kNotDetected: |
| 145 | return "Not Detected"; |
| 146 | case ConnectionTethering::kSuspected: |
| 147 | return "Suspected"; |
| 148 | case ConnectionTethering::kConfirmed: |
| 149 | return "Confirmed"; |
| 150 | case ConnectionTethering::kUnknown: |
| 151 | return "Unknown"; |
| 152 | } |
| 153 | NOTREACHED(); |
| 154 | return "Unknown"; |
| 155 | } |
| 156 | |
Marton Hunyady | 0e0e354 | 2018-02-21 18:51:39 +0100 | [diff] [blame] | 157 | template <> |
| 158 | string BoxedValue::ValuePrinter<RollbackToTargetVersion>(const void* value) { |
| 159 | const RollbackToTargetVersion* val = |
| 160 | reinterpret_cast<const RollbackToTargetVersion*>(value); |
| 161 | switch (*val) { |
| 162 | case RollbackToTargetVersion::kUnspecified: |
| 163 | return "Unspecified"; |
| 164 | case RollbackToTargetVersion::kDisabled: |
| 165 | return "Disabled"; |
Marton Hunyady | d4bc462 | 2018-08-30 15:52:23 +0200 | [diff] [blame] | 166 | case RollbackToTargetVersion::kRollbackAndPowerwash: |
| 167 | return "Rollback and powerwash"; |
Marton Hunyady | 31aefb9 | 2018-08-29 16:17:03 +0200 | [diff] [blame] | 168 | case RollbackToTargetVersion::kRollbackAndRestoreIfPossible: |
| 169 | return "Rollback and restore if possible"; |
Marton Hunyady | 0e0e354 | 2018-02-21 18:51:39 +0100 | [diff] [blame] | 170 | case RollbackToTargetVersion::kMaxValue: |
| 171 | NOTREACHED(); |
| 172 | return "Max value"; |
| 173 | } |
| 174 | NOTREACHED(); |
| 175 | return "Unknown"; |
| 176 | } |
| 177 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 178 | template <> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 179 | string BoxedValue::ValuePrinter<Stage>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 180 | const Stage* val = reinterpret_cast<const Stage*>(value); |
| 181 | switch (*val) { |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 182 | case Stage::kIdle: |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 183 | return "Idle"; |
| 184 | case Stage::kCheckingForUpdate: |
| 185 | return "Checking For Update"; |
| 186 | case Stage::kUpdateAvailable: |
| 187 | return "Update Available"; |
| 188 | case Stage::kDownloading: |
| 189 | return "Downloading"; |
| 190 | case Stage::kVerifying: |
| 191 | return "Verifying"; |
| 192 | case Stage::kFinalizing: |
| 193 | return "Finalizing"; |
| 194 | case Stage::kUpdatedNeedReboot: |
| 195 | return "Updated, Need Reboot"; |
| 196 | case Stage::kReportingErrorEvent: |
| 197 | return "Reporting Error Event"; |
| 198 | case Stage::kAttemptingRollback: |
| 199 | return "Attempting Rollback"; |
| 200 | } |
| 201 | NOTREACHED(); |
| 202 | return "Unknown"; |
| 203 | } |
| 204 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 205 | template <> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 206 | string BoxedValue::ValuePrinter<UpdateRequestStatus>(const void* value) { |
| 207 | const UpdateRequestStatus* val = |
| 208 | reinterpret_cast<const UpdateRequestStatus*>(value); |
| 209 | switch (*val) { |
| 210 | case UpdateRequestStatus::kNone: |
| 211 | return "None"; |
| 212 | case UpdateRequestStatus::kInteractive: |
| 213 | return "Interactive"; |
| 214 | case UpdateRequestStatus::kPeriodic: |
| 215 | return "Periodic"; |
| 216 | } |
| 217 | NOTREACHED(); |
| 218 | return "Unknown"; |
| 219 | } |
| 220 | |
Aaron Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 221 | template <> |
| 222 | string BoxedValue::ValuePrinter<UpdateRestrictions>(const void* value) { |
| 223 | const UpdateRestrictions* val = |
| 224 | reinterpret_cast<const UpdateRestrictions*>(value); |
| 225 | |
| 226 | if (*val == UpdateRestrictions::kNone) { |
| 227 | return "None"; |
| 228 | } |
| 229 | string retval = "Flags:"; |
| 230 | if (*val & kRestrictDownloading) { |
| 231 | retval += " RestrictDownloading"; |
| 232 | } |
| 233 | return retval; |
| 234 | } |
| 235 | |
Adolfo Victoria | 94ffe13 | 2018-06-28 16:14:56 -0700 | [diff] [blame] | 236 | template <> |
| 237 | string BoxedValue::ValuePrinter<WeeklyTimeInterval>(const void* value) { |
| 238 | const WeeklyTimeInterval* val = |
| 239 | reinterpret_cast<const WeeklyTimeInterval*>(value); |
| 240 | return val->ToString(); |
| 241 | } |
| 242 | |
| 243 | template <> |
| 244 | string BoxedValue::ValuePrinter<WeeklyTimeIntervalVector>(const void* value) { |
| 245 | const WeeklyTimeIntervalVector* val = |
| 246 | reinterpret_cast<const WeeklyTimeIntervalVector*>(value); |
| 247 | |
| 248 | string retval = "Disallowed intervals:\n"; |
| 249 | for (const auto& interval : *val) { |
| 250 | retval += interval.ToString() + "\n"; |
| 251 | } |
| 252 | return retval; |
| 253 | } |
| 254 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 255 | } // namespace chromeos_update_manager |