blob: 136ca35ffe011688e170a3be843eaf85197be8d5 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Deymoc705cc82014-02-19 11:15:00 -080016
Gilad Arnold48415f12014-06-27 07:10:58 -070017#ifndef UPDATE_ENGINE_UPDATE_MANAGER_DEFAULT_POLICY_H_
18#define UPDATE_ENGINE_UPDATE_MANAGER_DEFAULT_POLICY_H_
19
Ben Chan02f7c1d2014-10-18 15:18:02 -070020#include <memory>
Gilad Arnold48415f12014-06-27 07:10:58 -070021#include <string>
Alex Deymoc705cc82014-02-19 11:15:00 -080022
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070023#include <base/time/time.h>
24
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/clock_interface.h"
Alex Deymo63784a52014-05-28 10:46:14 -070026#include "update_engine/update_manager/policy.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080027
Alex Deymo63784a52014-05-28 10:46:14 -070028namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -080029
Gilad Arnolda23e4082014-07-17 11:40:43 -070030// Auxiliary state class for DefaultPolicy evaluations.
31//
32// IMPORTANT: The use of a state object in policies is generally forbidden, as
33// it was a design decision to keep policy calls side-effect free. We make an
34// exception here to ensure that DefaultPolicy indeed serves as a safe (and
35// secure) fallback option. This practice should be avoided when imlpementing
36// other policies.
37class DefaultPolicyState {
38 public:
39 DefaultPolicyState() {}
40
41 bool IsLastCheckAllowedTimeSet() const {
42 return last_check_allowed_time_ != base::Time::Max();
43 }
44
45 // Sets/returns the point time on the monotonic time scale when the latest
46 // check allowed was recorded.
47 void set_last_check_allowed_time(base::Time timestamp) {
48 last_check_allowed_time_ = timestamp;
49 }
50 base::Time last_check_allowed_time() const {
51 return last_check_allowed_time_;
52 }
53
54 private:
55 base::Time last_check_allowed_time_ = base::Time::Max();
56};
57
Alex Deymoc705cc82014-02-19 11:15:00 -080058// The DefaultPolicy is a safe Policy implementation that doesn't fail. The
59// values returned by this policy are safe default in case of failure of the
Alex Deymo63784a52014-05-28 10:46:14 -070060// actual policy being used by the UpdateManager.
Alex Deymoc705cc82014-02-19 11:15:00 -080061class DefaultPolicy : public Policy {
62 public:
Gilad Arnolda23e4082014-07-17 11:40:43 -070063 explicit DefaultPolicy(chromeos_update_engine::ClockInterface* clock);
64 DefaultPolicy() : DefaultPolicy(nullptr) {}
Alex Deymo610277e2014-11-11 21:18:11 -080065 ~DefaultPolicy() override {}
Alex Deymoc705cc82014-02-19 11:15:00 -080066
67 // Policy overrides.
Alex Vakulenko157fe302014-08-11 15:59:58 -070068 EvalStatus UpdateCheckAllowed(
Alex Deymo0d11c602014-04-23 20:12:20 -070069 EvaluationContext* ec, State* state, std::string* error,
Gilad Arnolda23e4082014-07-17 11:40:43 -070070 UpdateCheckParams* result) const override;
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -070071
Aaron Wood23bd3392017-10-06 14:48:25 -070072 EvalStatus UpdateCanBeApplied(
73 EvaluationContext* ec,
74 State* state,
75 std::string* error,
76 chromeos_update_engine::ErrorCode* result,
77 chromeos_update_engine::InstallPlan* install_plan) const override;
78
Alex Vakulenko157fe302014-08-11 15:59:58 -070079 EvalStatus UpdateCanStart(
Gilad Arnolddc4bb262014-07-23 10:45:19 -070080 EvaluationContext* ec, State* state, std::string* error,
Gilad Arnold42f253b2014-06-25 12:39:17 -070081 UpdateDownloadParams* result,
Gilad Arnoldd78caf92014-09-24 09:28:14 -070082 UpdateState update_state) const override;
Alex Deymoc705cc82014-02-19 11:15:00 -080083
Alex Vakulenko157fe302014-08-11 15:59:58 -070084 EvalStatus UpdateDownloadAllowed(
Gilad Arnolddc4bb262014-07-23 10:45:19 -070085 EvaluationContext* ec, State* state, std::string* error,
86 bool* result) const override;
Gilad Arnold0adbc942014-05-12 10:35:43 -070087
Gilad Arnold78ecbfc2014-10-22 14:38:25 -070088 EvalStatus P2PEnabled(
89 EvaluationContext* ec, State* state, std::string* error,
90 bool* result) const override;
91
92 EvalStatus P2PEnabledChanged(
93 EvaluationContext* ec, State* state, std::string* error,
94 bool* result, bool prev_result) const override;
95
Gilad Arnoldb3b05442014-05-30 14:25:05 -070096 protected:
97 // Policy override.
Alex Vakulenko157fe302014-08-11 15:59:58 -070098 std::string PolicyName() const override { return "DefaultPolicy"; }
Gilad Arnoldb3b05442014-05-30 14:25:05 -070099
Alex Deymoc705cc82014-02-19 11:15:00 -0800100 private:
Gilad Arnolda23e4082014-07-17 11:40:43 -0700101 // A clock interface.
102 chromeos_update_engine::ClockInterface* clock_;
103
104 // An auxiliary state object.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700105 std::unique_ptr<DefaultPolicyState> aux_state_;
Gilad Arnolda23e4082014-07-17 11:40:43 -0700106
Alex Deymoc705cc82014-02-19 11:15:00 -0800107 DISALLOW_COPY_AND_ASSIGN(DefaultPolicy);
108};
109
Alex Deymo63784a52014-05-28 10:46:14 -0700110} // namespace chromeos_update_manager
Alex Deymoc705cc82014-02-19 11:15:00 -0800111
Gilad Arnold48415f12014-06-27 07:10:58 -0700112#endif // UPDATE_ENGINE_UPDATE_MANAGER_DEFAULT_POLICY_H_