Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 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 | |
Gilad Arnold | 2cbb385 | 2014-03-07 12:40:50 -0800 | [diff] [blame] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_INL_H_ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_INL_H_ |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 7 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame^] | 8 | #include <string> |
| 9 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 10 | #include <base/bind.h> |
| 11 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 12 | #include "update_engine/policy_manager/evaluation_context.h" |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame^] | 13 | #include "update_engine/policy_manager/event_loop.h" |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 14 | |
| 15 | namespace chromeos_policy_manager { |
| 16 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 17 | template<typename T, typename R, typename... Args> |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 18 | EvalStatus PolicyManager::EvaluatePolicy(EvaluationContext* ec, |
| 19 | T policy_method, R* result, |
| 20 | Args... args) { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 21 | std::string error; |
| 22 | |
| 23 | // First try calling the actual policy. |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 24 | EvalStatus status = (policy_.get()->*policy_method)(ec, state_.get(), &error, |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 25 | result, args...); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 26 | |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 27 | if (status == EvalStatus::kFailed) { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 28 | LOG(WARNING) << "PolicyRequest() failed with error: " << error; |
| 29 | error.clear(); |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 30 | status = (default_policy_.*policy_method)(ec, state_.get(), &error, |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 31 | result, args...); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 32 | |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 33 | if (status == EvalStatus::kFailed) { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 34 | LOG(WARNING) << "Request to DefaultPolicy also failed, passing error."; |
| 35 | } |
| 36 | } |
| 37 | // TODO(deymo): Log the actual state used from the EvaluationContext. |
| 38 | return status; |
| 39 | } |
| 40 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 41 | template<typename T, typename R, typename... Args> |
| 42 | void PolicyManager::OnPolicyReadyToEvaluate( |
| 43 | scoped_refptr<EvaluationContext> ec, |
| 44 | base::Callback<void(EvalStatus status, const R& result)> callback, |
| 45 | T policy_method, Args... args) { |
| 46 | R result; |
| 47 | EvalStatus status = EvaluatePolicy(ec, policy_method, &result, args...); |
| 48 | |
| 49 | if (status != EvalStatus::kAskMeAgainLater) { |
| 50 | // AsyncPolicyRequest finished. |
| 51 | callback.Run(status, result); |
| 52 | return; |
| 53 | } |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame^] | 54 | // Re-schedule the policy request based on used variables. |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 55 | base::Closure closure = base::Bind( |
| 56 | &PolicyManager::OnPolicyReadyToEvaluate<T, R, Args...>, |
| 57 | base::Unretained(this), ec, callback, policy_method, args...); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame^] | 58 | |
| 59 | if (!ec->RunOnValueChangeOrTimeout(closure)) { |
| 60 | // The policy method didn't use any non-const variable nor there's any |
| 61 | // time-based event that will change the status of evaluation. We call the |
| 62 | // callback with EvalStatus::kAskMeAgainLater. |
| 63 | LOG(ERROR) << "Policy implementation didn't use any non-const variable " |
| 64 | "but returned kAskMeAgainLater."; |
| 65 | callback.Run(EvalStatus::kAskMeAgainLater, result); |
| 66 | return; |
| 67 | } |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | template<typename T, typename R, typename... Args> |
| 71 | EvalStatus PolicyManager::PolicyRequest(T policy_method, R* result, |
| 72 | Args... args) { |
| 73 | scoped_refptr<EvaluationContext> ec(new EvaluationContext); |
| 74 | // A PolicyRequest allways consists on a single evaluation on a new |
| 75 | // EvaluationContext. |
| 76 | return EvaluatePolicy(ec, policy_method, result, args...); |
| 77 | } |
| 78 | |
| 79 | template<typename T, typename R, typename... Args> |
| 80 | void PolicyManager::AsyncPolicyRequest( |
| 81 | base::Callback<void(EvalStatus, const R& result)> callback, |
| 82 | T policy_method, Args... args) { |
| 83 | |
| 84 | scoped_refptr<EvaluationContext> ec = new EvaluationContext; |
| 85 | base::Closure closure = base::Bind( |
| 86 | &PolicyManager::OnPolicyReadyToEvaluate<T, R, Args...>, |
| 87 | base::Unretained(this), ec, callback, policy_method, args...); |
| 88 | RunFromMainLoop(closure); |
| 89 | } |
| 90 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 91 | } // namespace chromeos_policy_manager |
| 92 | |
Gilad Arnold | 2cbb385 | 2014-03-07 12:40:50 -0800 | [diff] [blame] | 93 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_INL_H_ |