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/common/clock_interface.h"
26 #include "update_engine/update_manager/policy.h"
27 
28 namespace chromeos_update_manager {
29 
30 // 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.
37 class 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 
58 // 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
60 // actual policy being used by the UpdateManager.
61 class DefaultPolicy : public Policy {
62  public:
63   explicit DefaultPolicy(chromeos_update_engine::ClockInterface* clock);
64   DefaultPolicy() : DefaultPolicy(nullptr) {}
65   ~DefaultPolicy() override {}
66 
67   // Policy overrides.
68   EvalStatus UpdateCheckAllowed(
69       EvaluationContext* ec, State* state, std::string* error,
70       UpdateCheckParams* result) const override;
71 
72   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 
79   EvalStatus UpdateCanStart(
80       EvaluationContext* ec, State* state, std::string* error,
81       UpdateDownloadParams* result,
82       UpdateState update_state) const override;
83 
84   EvalStatus UpdateDownloadAllowed(
85       EvaluationContext* ec, State* state, std::string* error,
86       bool* result) const override;
87 
88   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 
96  protected:
97   // Policy override.
98   std::string PolicyName() const override { return "DefaultPolicy"; }
99 
100  private:
101   // A clock interface.
102   chromeos_update_engine::ClockInterface* clock_;
103 
104   // An auxiliary state object.
105   std::unique_ptr<DefaultPolicyState> aux_state_;
106 
107   DISALLOW_COPY_AND_ASSIGN(DefaultPolicy);
108 };
109 
110 }  // namespace chromeos_update_manager
111 
112 #endif  // UPDATE_ENGINE_UPDATE_MANAGER_DEFAULT_POLICY_H_
113