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