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 #ifndef UPDATE_ENGINE_UPDATE_MANAGER_DEFAULT_POLICY_H_ 18 #define UPDATE_ENGINE_UPDATE_MANAGER_DEFAULT_POLICY_H_ 19 20 #include <memory> 21 #include <string> 22 23 #include <base/time/time.h> 24 25 #include "update_engine/update_manager/policy.h" 26 27 namespace chromeos_update_manager { 28 29 // Auxiliary state class for DefaultPolicy evaluations. 30 // 31 // IMPORTANT: The use of a state object in policies is generally forbidden, as 32 // it was a design decision to keep policy calls side-effect free. We make an 33 // exception here to ensure that DefaultPolicy indeed serves as a safe (and 34 // secure) fallback option. This practice should be avoided when imlpementing 35 // other policies. 36 class DefaultPolicyState { 37 public: DefaultPolicyState()38 DefaultPolicyState() {} 39 IsLastCheckAllowedTimeSet()40 bool IsLastCheckAllowedTimeSet() const { 41 return last_check_allowed_time_ != base::Time::Max(); 42 } 43 44 // Sets/returns the point time on the monotonic time scale when the latest 45 // check allowed was recorded. set_last_check_allowed_time(base::Time timestamp)46 void set_last_check_allowed_time(base::Time timestamp) { 47 last_check_allowed_time_ = timestamp; 48 } last_check_allowed_time()49 base::Time last_check_allowed_time() const { 50 return last_check_allowed_time_; 51 } 52 53 private: 54 base::Time last_check_allowed_time_ = base::Time::Max(); 55 }; 56 57 // The DefaultPolicy is a safe Policy implementation that doesn't fail. The 58 // values returned by this policy are safe default in case of failure of the 59 // actual policy being used by the UpdateManager. 60 class DefaultPolicy : public Policy { 61 public: DefaultPolicy()62 DefaultPolicy() : aux_state_(new DefaultPolicyState()) {} 63 ~DefaultPolicy() override = default; 64 65 // Policy overrides. 66 EvalStatus UpdateCheckAllowed(EvaluationContext* ec, 67 State* state, 68 std::string* error, 69 UpdateCheckParams* result) const override; 70 71 EvalStatus UpdateCanBeApplied( 72 EvaluationContext* ec, 73 State* state, 74 std::string* error, 75 chromeos_update_engine::ErrorCode* result, 76 chromeos_update_engine::InstallPlan* install_plan) const override; 77 78 EvalStatus UpdateCanStart(EvaluationContext* ec, 79 State* state, 80 std::string* error, 81 UpdateDownloadParams* result, 82 UpdateState update_state) const override; 83 84 EvalStatus P2PEnabled(EvaluationContext* ec, 85 State* state, 86 std::string* error, 87 bool* result) const override; 88 89 EvalStatus P2PEnabledChanged(EvaluationContext* ec, 90 State* state, 91 std::string* error, 92 bool* result, 93 bool prev_result) const override; 94 95 protected: 96 // Policy override. PolicyName()97 std::string PolicyName() const override { return "DefaultPolicy"; } 98 99 private: 100 // An auxiliary state object. 101 std::unique_ptr<DefaultPolicyState> aux_state_; 102 103 DISALLOW_COPY_AND_ASSIGN(DefaultPolicy); 104 }; 105 106 } // namespace chromeos_update_manager 107 108 #endif // UPDATE_ENGINE_UPDATE_MANAGER_DEFAULT_POLICY_H_ 109