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