| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 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 | // | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 16 |  | 
| Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_UPDATE_MANAGER_POLICY_UTILS_H_ | 
|  | 18 | #define UPDATE_ENGINE_UPDATE_MANAGER_POLICY_UTILS_H_ | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 19 |  | 
| Aaron Wood | c73fdc1 | 2017-12-06 11:09:15 -0800 | [diff] [blame] | 20 | #include <string> | 
|  | 21 | #include <vector> | 
|  | 22 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 23 | #include "update_engine/update_manager/policy.h" | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 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 |  | 
| Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 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> | 
|  | 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 | LOG(INFO) << "decision by " << policy->PolicyRequestName(policy_method); | 
|  | 59 | return status; | 
|  | 60 | } | 
|  | 61 | } | 
|  | 62 | return EvalStatus::kContinue; | 
| Aaron Wood | c73fdc1 | 2017-12-06 11:09:15 -0800 | [diff] [blame] | 63 | } | 
| Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 64 |  | 
|  | 65 | // Base class implementation that returns |EvalStatus::kContinue| for all | 
|  | 66 | // decisions, to be used as a base-class for various Policy facets that only | 
|  | 67 | // pertain to certain situations. This might be better folded into Policy | 
|  | 68 | // instead of using pure-virtual methods on that class. | 
|  | 69 | class PolicyImplBase : public Policy { | 
|  | 70 | public: | 
|  | 71 | // Policy overrides. | 
|  | 72 | EvalStatus UpdateCheckAllowed(EvaluationContext* ec, | 
|  | 73 | State* state, | 
|  | 74 | std::string* error, | 
|  | 75 | UpdateCheckParams* result) const override { | 
|  | 76 | return EvalStatus::kContinue; | 
|  | 77 | }; | 
|  | 78 |  | 
|  | 79 | EvalStatus UpdateCanBeApplied( | 
|  | 80 | EvaluationContext* ec, | 
|  | 81 | State* state, | 
|  | 82 | std::string* error, | 
|  | 83 | chromeos_update_engine::ErrorCode* result, | 
|  | 84 | chromeos_update_engine::InstallPlan* install_plan) const override { | 
|  | 85 | return EvalStatus::kContinue; | 
|  | 86 | }; | 
|  | 87 |  | 
|  | 88 | EvalStatus UpdateCanStart(EvaluationContext* ec, | 
|  | 89 | State* state, | 
|  | 90 | std::string* error, | 
|  | 91 | UpdateDownloadParams* result, | 
|  | 92 | UpdateState update_state) const override { | 
|  | 93 | return EvalStatus::kContinue; | 
|  | 94 | }; | 
|  | 95 |  | 
|  | 96 | EvalStatus UpdateDownloadAllowed(EvaluationContext* ec, | 
|  | 97 | State* state, | 
|  | 98 | std::string* error, | 
|  | 99 | bool* result) const override { | 
|  | 100 | return EvalStatus::kContinue; | 
|  | 101 | }; | 
|  | 102 |  | 
|  | 103 | EvalStatus P2PEnabled(EvaluationContext* ec, | 
|  | 104 | State* state, | 
|  | 105 | std::string* error, | 
|  | 106 | bool* result) const override { | 
|  | 107 | return EvalStatus::kContinue; | 
|  | 108 | }; | 
|  | 109 |  | 
|  | 110 | EvalStatus P2PEnabledChanged(EvaluationContext* ec, | 
|  | 111 | State* state, | 
|  | 112 | std::string* error, | 
|  | 113 | bool* result, | 
|  | 114 | bool prev_result) const override { | 
|  | 115 | return EvalStatus::kContinue; | 
|  | 116 | }; | 
|  | 117 | }; | 
|  | 118 |  | 
|  | 119 | }  // namespace chromeos_update_manager | 
|  | 120 |  | 
| Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 121 | #endif  // UPDATE_ENGINE_UPDATE_MANAGER_POLICY_UTILS_H_ |