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 | // |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 16 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 17 | #include "update_engine/update_manager/real_system_provider.h" |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 18 | |
Xiyuan Xia | ed9bd92 | 2016-04-07 14:45:16 -0700 | [diff] [blame] | 19 | #include <base/bind.h> |
| 20 | #include <base/callback.h> |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 21 | #include <base/logging.h> |
Gilad Arnold | 48e1361 | 2014-05-16 10:18:05 -0700 | [diff] [blame] | 22 | #include <base/time/time.h> |
Daniel Erat | 04df23a | 2018-03-29 17:55:35 -0700 | [diff] [blame] | 23 | #include <kiosk-app/dbus-proxies.h> |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 24 | |
Miriam Polzer | 8db5249 | 2020-09-17 14:06:46 +0200 | [diff] [blame] | 25 | #include "update_engine/common/boot_control_interface.h" |
| 26 | #include "update_engine/common/hardware_interface.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 27 | #include "update_engine/common/utils.h" |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 28 | #include "update_engine/cros/omaha_request_params.h" |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 29 | #include "update_engine/update_manager/generic_variables.h" |
Xiyuan Xia | ed9bd92 | 2016-04-07 14:45:16 -0700 | [diff] [blame] | 30 | #include "update_engine/update_manager/variable.h" |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 31 | |
| 32 | using std::string; |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 33 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 34 | namespace chromeos_update_manager { |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 35 | |
Xiyuan Xia | ed9bd92 | 2016-04-07 14:45:16 -0700 | [diff] [blame] | 36 | namespace { |
| 37 | |
Alex Deymo | 857ded1 | 2016-05-12 19:06:21 -0700 | [diff] [blame] | 38 | // The maximum number of consecutive failures before returning the default |
| 39 | // constructor value for T instead of failure. |
| 40 | const int kRetryPollVariableMaxRetry = 5; |
| 41 | |
| 42 | // The polling interval to be used whenever GetValue() returns an error. |
| 43 | const int kRetryPollVariableRetryIntervalSeconds = 5 * 60; |
| 44 | |
Xiyuan Xia | ed9bd92 | 2016-04-07 14:45:16 -0700 | [diff] [blame] | 45 | // The RetryPollVariable variable is a polling variable that allows the function |
| 46 | // returning the value to fail a few times and shortens the polling rate when |
| 47 | // that happens. |
| 48 | template <typename T> |
| 49 | class RetryPollVariable : public Variable<T> { |
| 50 | public: |
| 51 | RetryPollVariable(const string& name, |
| 52 | const base::TimeDelta poll_interval, |
| 53 | base::Callback<bool(T* res)> func) |
| 54 | : Variable<T>(name, poll_interval), |
| 55 | func_(func), |
| 56 | base_interval_(poll_interval) { |
Alex Deymo | 857ded1 | 2016-05-12 19:06:21 -0700 | [diff] [blame] | 57 | DCHECK_LT(kRetryPollVariableRetryIntervalSeconds, |
| 58 | base_interval_.InSeconds()); |
Xiyuan Xia | ed9bd92 | 2016-04-07 14:45:16 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | protected: |
| 62 | // Variable override. |
| 63 | const T* GetValue(base::TimeDelta /* timeout */, |
| 64 | string* /* errmsg */) override { |
| 65 | std::unique_ptr<T> result(new T()); |
| 66 | if (!func_.Run(result.get())) { |
Alex Deymo | 857ded1 | 2016-05-12 19:06:21 -0700 | [diff] [blame] | 67 | if (failed_attempts_ >= kRetryPollVariableMaxRetry) { |
Miriam Polzer | 213e2be | 2020-05-29 10:25:09 +0200 | [diff] [blame] | 68 | // Give up on the retries and set back the desired polling interval. |
Xiyuan Xia | ed9bd92 | 2016-04-07 14:45:16 -0700 | [diff] [blame] | 69 | this->SetPollInterval(base_interval_); |
Miriam Polzer | 213e2be | 2020-05-29 10:25:09 +0200 | [diff] [blame] | 70 | // Release the result instead of returning a |nullptr| to indicate that |
| 71 | // the result could not be fetched. |
Xiyuan Xia | ed9bd92 | 2016-04-07 14:45:16 -0700 | [diff] [blame] | 72 | return result.release(); |
| 73 | } |
| 74 | this->SetPollInterval( |
Alex Deymo | 857ded1 | 2016-05-12 19:06:21 -0700 | [diff] [blame] | 75 | base::TimeDelta::FromSeconds(kRetryPollVariableRetryIntervalSeconds)); |
Xiyuan Xia | ed9bd92 | 2016-04-07 14:45:16 -0700 | [diff] [blame] | 76 | failed_attempts_++; |
| 77 | return nullptr; |
| 78 | } |
| 79 | failed_attempts_ = 0; |
| 80 | this->SetPollInterval(base_interval_); |
| 81 | return result.release(); |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | // The function to be called, stored as a base::Callback. |
| 86 | base::Callback<bool(T*)> func_; |
| 87 | |
| 88 | // The desired polling interval when |func_| works and returns true. |
| 89 | base::TimeDelta base_interval_; |
| 90 | |
| 91 | // The number of consecutive failed attempts made. |
| 92 | int failed_attempts_ = 0; |
| 93 | |
Xiyuan Xia | ed9bd92 | 2016-04-07 14:45:16 -0700 | [diff] [blame] | 94 | DISALLOW_COPY_AND_ASSIGN(RetryPollVariable); |
| 95 | }; |
| 96 | |
| 97 | } // namespace |
| 98 | |
Alex Deymo | 42c30c3 | 2014-04-24 18:41:18 -0700 | [diff] [blame] | 99 | bool RealSystemProvider::Init() { |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 100 | var_is_normal_boot_mode_.reset(new ConstCopyVariable<bool>( |
Miriam Polzer | 8db5249 | 2020-09-17 14:06:46 +0200 | [diff] [blame] | 101 | "is_normal_boot_mode", system_state_->hardware()->IsNormalBootMode())); |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 102 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 103 | var_is_official_build_.reset(new ConstCopyVariable<bool>( |
Miriam Polzer | 8db5249 | 2020-09-17 14:06:46 +0200 | [diff] [blame] | 104 | "is_official_build", system_state_->hardware()->IsOfficialBuild())); |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 105 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 106 | var_is_oobe_complete_.reset(new CallCopyVariable<bool>( |
| 107 | "is_oobe_complete", |
| 108 | base::Bind(&chromeos_update_engine::HardwareInterface::IsOOBEComplete, |
Miriam Polzer | 8db5249 | 2020-09-17 14:06:46 +0200 | [diff] [blame] | 109 | base::Unretained(system_state_->hardware()), |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 110 | nullptr))); |
Gilad Arnold | 48e1361 | 2014-05-16 10:18:05 -0700 | [diff] [blame] | 111 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 112 | var_num_slots_.reset(new ConstCopyVariable<unsigned int>( |
Miriam Polzer | 8db5249 | 2020-09-17 14:06:46 +0200 | [diff] [blame] | 113 | "num_slots", system_state_->boot_control()->GetNumSlots())); |
Gilad Arnold | bfc44f7 | 2014-07-09 14:41:39 -0700 | [diff] [blame] | 114 | |
Xiyuan Xia | ed9bd92 | 2016-04-07 14:45:16 -0700 | [diff] [blame] | 115 | var_kiosk_required_platform_version_.reset(new RetryPollVariable<string>( |
Xiyuan Xia | 6e30bc5 | 2016-02-24 15:35:42 -0800 | [diff] [blame] | 116 | "kiosk_required_platform_version", |
| 117 | base::TimeDelta::FromHours(5), // Same as Chrome's CWS poll. |
| 118 | base::Bind(&RealSystemProvider::GetKioskAppRequiredPlatformVersion, |
| 119 | base::Unretained(this)))); |
| 120 | |
Miriam Polzer | 8db5249 | 2020-09-17 14:06:46 +0200 | [diff] [blame] | 121 | var_chromeos_version_.reset(new ConstCopyVariable<base::Version>( |
| 122 | "chromeos_version", |
| 123 | base::Version(system_state_->request_params()->app_version()))); |
| 124 | |
Alex Deymo | bd04b14 | 2014-03-18 15:00:05 -0700 | [diff] [blame] | 125 | return true; |
| 126 | } |
| 127 | |
Xiyuan Xia | ed9bd92 | 2016-04-07 14:45:16 -0700 | [diff] [blame] | 128 | bool RealSystemProvider::GetKioskAppRequiredPlatformVersion( |
| 129 | string* required_platform_version) { |
Xiyuan Xia | 6e30bc5 | 2016-02-24 15:35:42 -0800 | [diff] [blame] | 130 | brillo::ErrorPtr error; |
Daniel Erat | 04df23a | 2018-03-29 17:55:35 -0700 | [diff] [blame] | 131 | if (!kiosk_app_proxy_->GetRequiredPlatformVersion(required_platform_version, |
| 132 | &error)) { |
Xiyuan Xia | 6e30bc5 | 2016-02-24 15:35:42 -0800 | [diff] [blame] | 133 | LOG(WARNING) << "Failed to get kiosk required platform version"; |
Xiyuan Xia | ed9bd92 | 2016-04-07 14:45:16 -0700 | [diff] [blame] | 134 | required_platform_version->clear(); |
| 135 | return false; |
Xiyuan Xia | 6e30bc5 | 2016-02-24 15:35:42 -0800 | [diff] [blame] | 136 | } |
Xiyuan Xia | 6e30bc5 | 2016-02-24 15:35:42 -0800 | [diff] [blame] | 137 | |
Xiyuan Xia | ed9bd92 | 2016-04-07 14:45:16 -0700 | [diff] [blame] | 138 | return true; |
Xiyuan Xia | 6e30bc5 | 2016-02-24 15:35:42 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 141 | } // namespace chromeos_update_manager |