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 //
16 
17 #include "update_engine/update_manager/real_system_provider.h"
18 
19 #include <base/bind.h>
20 #include <base/callback.h>
21 #include <base/logging.h>
22 #include <base/time/time.h>
23 #include <kiosk-app/dbus-proxies.h>
24 
25 #include "update_engine/common/boot_control_interface.h"
26 #include "update_engine/common/hardware_interface.h"
27 #include "update_engine/common/system_state.h"
28 #include "update_engine/common/utils.h"
29 #include "update_engine/cros/omaha_request_params.h"
30 #include "update_engine/update_manager/generic_variables.h"
31 #include "update_engine/update_manager/variable.h"
32 
33 using chromeos_update_engine::SystemState;
34 using std::string;
35 
36 namespace chromeos_update_manager {
37 
38 namespace {
39 
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 
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:
RetryPollVariable(const string & name,const base::TimeDelta poll_interval,base::Callback<bool (T * res)> func)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) {
59     DCHECK_LT(kRetryPollVariableRetryIntervalSeconds,
60               base_interval_.InSeconds());
61   }
62 
63  protected:
64   // Variable override.
GetValue(base::TimeDelta,string *)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())) {
69       if (failed_attempts_ >= kRetryPollVariableMaxRetry) {
70         // Give up on the retries and set back the desired polling interval.
71         this->SetPollInterval(base_interval_);
72         // Release the result instead of returning a |nullptr| to indicate that
73         // the result could not be fetched.
74         return result.release();
75       }
76       this->SetPollInterval(
77           base::TimeDelta::FromSeconds(kRetryPollVariableRetryIntervalSeconds));
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 
96   DISALLOW_COPY_AND_ASSIGN(RetryPollVariable);
97 };
98 
99 }  // namespace
100 
Init()101 bool RealSystemProvider::Init() {
102   var_is_normal_boot_mode_.reset(new ConstCopyVariable<bool>(
103       "is_normal_boot_mode",
104       SystemState::Get()->hardware()->IsNormalBootMode()));
105 
106   var_is_official_build_.reset(new ConstCopyVariable<bool>(
107       "is_official_build", SystemState::Get()->hardware()->IsOfficialBuild()));
108 
109   var_is_oobe_complete_.reset(new CallCopyVariable<bool>(
110       "is_oobe_complete",
111       base::Bind(&chromeos_update_engine::HardwareInterface::IsOOBEComplete,
112                  base::Unretained(SystemState::Get()->hardware()),
113                  nullptr)));
114 
115   var_num_slots_.reset(new ConstCopyVariable<unsigned int>(
116       "num_slots", SystemState::Get()->boot_control()->GetNumSlots()));
117 
118   var_kiosk_required_platform_version_.reset(new RetryPollVariable<string>(
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 
124   var_chromeos_version_.reset(new ConstCopyVariable<base::Version>(
125       "chromeos_version",
126       base::Version(SystemState::Get()->request_params()->app_version())));
127 
128   return true;
129 }
130 
GetKioskAppRequiredPlatformVersion(string * required_platform_version)131 bool RealSystemProvider::GetKioskAppRequiredPlatformVersion(
132     string* required_platform_version) {
133   brillo::ErrorPtr error;
134   if (!kiosk_app_proxy_->GetRequiredPlatformVersion(required_platform_version,
135                                                     &error)) {
136     LOG(WARNING) << "Failed to get kiosk required platform version";
137     required_platform_version->clear();
138     return false;
139   }
140 
141   return true;
142 }
143 
144 }  // namespace chromeos_update_manager
145