default_policy.h revision d78caf9baf2026a356130f4754af2c504f423945
1// Copyright (c) 2014 The Chromium OS Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#ifndef UPDATE_ENGINE_UPDATE_MANAGER_DEFAULT_POLICY_H_ 6#define UPDATE_ENGINE_UPDATE_MANAGER_DEFAULT_POLICY_H_ 7 8#include <string> 9 10#include <base/time/time.h> 11 12#include "update_engine/clock_interface.h" 13#include "update_engine/update_manager/policy.h" 14 15namespace chromeos_update_manager { 16 17// Auxiliary state class for DefaultPolicy evaluations. 18// 19// IMPORTANT: The use of a state object in policies is generally forbidden, as 20// it was a design decision to keep policy calls side-effect free. We make an 21// exception here to ensure that DefaultPolicy indeed serves as a safe (and 22// secure) fallback option. This practice should be avoided when imlpementing 23// other policies. 24class DefaultPolicyState { 25 public: 26 DefaultPolicyState() {} 27 28 bool IsLastCheckAllowedTimeSet() const { 29 return last_check_allowed_time_ != base::Time::Max(); 30 } 31 32 // Sets/returns the point time on the monotonic time scale when the latest 33 // check allowed was recorded. 34 void set_last_check_allowed_time(base::Time timestamp) { 35 last_check_allowed_time_ = timestamp; 36 } 37 base::Time last_check_allowed_time() const { 38 return last_check_allowed_time_; 39 } 40 41 private: 42 base::Time last_check_allowed_time_ = base::Time::Max(); 43}; 44 45// The DefaultPolicy is a safe Policy implementation that doesn't fail. The 46// values returned by this policy are safe default in case of failure of the 47// actual policy being used by the UpdateManager. 48class DefaultPolicy : public Policy { 49 public: 50 explicit DefaultPolicy(chromeos_update_engine::ClockInterface* clock); 51 DefaultPolicy() : DefaultPolicy(nullptr) {} 52 virtual ~DefaultPolicy() {} 53 54 // Policy overrides. 55 EvalStatus UpdateCheckAllowed( 56 EvaluationContext* ec, State* state, std::string* error, 57 UpdateCheckParams* result) const override; 58 59 EvalStatus UpdateCanStart( 60 EvaluationContext* ec, State* state, std::string* error, 61 UpdateDownloadParams* result, 62 UpdateState update_state) const override; 63 64 EvalStatus UpdateDownloadAllowed( 65 EvaluationContext* ec, State* state, std::string* error, 66 bool* result) const override; 67 68 protected: 69 // Policy override. 70 std::string PolicyName() const override { return "DefaultPolicy"; } 71 72 private: 73 // A clock interface. 74 chromeos_update_engine::ClockInterface* clock_; 75 76 // An auxiliary state object. 77 scoped_ptr<DefaultPolicyState> aux_state_; 78 79 DISALLOW_COPY_AND_ASSIGN(DefaultPolicy); 80}; 81 82} // namespace chromeos_update_manager 83 84#endif // UPDATE_ENGINE_UPDATE_MANAGER_DEFAULT_POLICY_H_ 85