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_POLICY_UTILS_H_
18 #define UPDATE_ENGINE_UPDATE_MANAGER_POLICY_UTILS_H_
19
20 #include <string>
21 #include <vector>
22
23 #include "update_engine/update_manager/policy.h"
24
25 // Checks that the passed pointer value is not null, returning kFailed on the
26 // current context and setting the *error description when it is null. The
27 // intended use is to validate variable failures while using
28 // EvaluationContext::GetValue, for example:
29 //
30 // const int* my_value = ec->GetValue(state->my_provider()->var_my_value());
31 // POLICY_CHECK_VALUE_AND_FAIL(my_value, error);
32 //
33 #define POLICY_CHECK_VALUE_AND_FAIL(ptr, error) \
34 do { \
35 if ((ptr) == nullptr) { \
36 *(error) = #ptr " is required but is null."; \
37 return EvalStatus::kFailed; \
38 } \
39 } while (false)
40
41 namespace chromeos_update_manager {
42
43 // Call the passed-in Policy method on a series of Policy implementations, until
44 // one of them renders a decision by returning a value other than
45 // |EvalStatus::kContinue|.
46 template <typename T, typename R, typename... Args>
ConsultPolicies(const std::vector<Policy const * > policies,T policy_method,EvaluationContext * ec,State * state,std::string * error,R * result,Args...args)47 EvalStatus ConsultPolicies(const std::vector<Policy const*> policies,
48 T policy_method,
49 EvaluationContext* ec,
50 State* state,
51 std::string* error,
52 R* result,
53 Args... args) {
54 for (auto policy : policies) {
55 EvalStatus status =
56 (policy->*policy_method)(ec, state, error, result, args...);
57 if (status != EvalStatus::kContinue) {
58 return status;
59 }
60 }
61 return EvalStatus::kContinue;
62 }
63
64 // Base class implementation that returns |EvalStatus::kContinue| for all
65 // decisions, to be used as a base-class for various Policy facets that only
66 // pertain to certain situations. This might be better folded into Policy
67 // instead of using pure-virtual methods on that class.
68 class PolicyImplBase : public Policy {
69 public:
70 // Policy overrides.
UpdateCheckAllowed(EvaluationContext * ec,State * state,std::string * error,UpdateCheckParams * result)71 EvalStatus UpdateCheckAllowed(EvaluationContext* ec,
72 State* state,
73 std::string* error,
74 UpdateCheckParams* result) const override {
75 return EvalStatus::kContinue;
76 };
77
UpdateCanBeApplied(EvaluationContext * ec,State * state,std::string * error,chromeos_update_engine::ErrorCode * result,chromeos_update_engine::InstallPlan * install_plan)78 EvalStatus UpdateCanBeApplied(
79 EvaluationContext* ec,
80 State* state,
81 std::string* error,
82 chromeos_update_engine::ErrorCode* result,
83 chromeos_update_engine::InstallPlan* install_plan) const override {
84 return EvalStatus::kContinue;
85 };
86
UpdateCanStart(EvaluationContext * ec,State * state,std::string * error,UpdateDownloadParams * result,UpdateState update_state)87 EvalStatus UpdateCanStart(EvaluationContext* ec,
88 State* state,
89 std::string* error,
90 UpdateDownloadParams* result,
91 UpdateState update_state) const override {
92 return EvalStatus::kContinue;
93 };
94
P2PEnabled(EvaluationContext * ec,State * state,std::string * error,bool * result)95 EvalStatus P2PEnabled(EvaluationContext* ec,
96 State* state,
97 std::string* error,
98 bool* result) const override {
99 return EvalStatus::kContinue;
100 };
101
P2PEnabledChanged(EvaluationContext * ec,State * state,std::string * error,bool * result,bool prev_result)102 EvalStatus P2PEnabledChanged(EvaluationContext* ec,
103 State* state,
104 std::string* error,
105 bool* result,
106 bool prev_result) const override {
107 return EvalStatus::kContinue;
108 };
109 };
110
111 } // namespace chromeos_update_manager
112
113 #endif // UPDATE_ENGINE_UPDATE_MANAGER_POLICY_UTILS_H_
114