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 | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 8 | #include <base/bind.h> |
| 9 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 10 | #include "update_engine/policy_manager/evaluation_context.h" |
| 11 | |
| 12 | namespace chromeos_policy_manager { |
| 13 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 14 | template<typename T, typename R, typename... Args> |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 15 | EvalStatus PolicyManager::EvaluatePolicy(EvaluationContext* ec, |
| 16 | T policy_method, R* result, |
| 17 | Args... args) { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 18 | std::string error; |
| 19 | |
| 20 | // First try calling the actual policy. |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 21 | EvalStatus status = (policy_.get()->*policy_method)(ec, state_.get(), &error, |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 22 | result, args...); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 23 | |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 24 | if (status == EvalStatus::kFailed) { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 25 | LOG(WARNING) << "PolicyRequest() failed with error: " << error; |
| 26 | error.clear(); |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 27 | status = (default_policy_.*policy_method)(ec, state_.get(), &error, |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 28 | result, args...); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 29 | |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 30 | if (status == EvalStatus::kFailed) { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 31 | LOG(WARNING) << "Request to DefaultPolicy also failed, passing error."; |
| 32 | } |
| 33 | } |
| 34 | // TODO(deymo): Log the actual state used from the EvaluationContext. |
| 35 | return status; |
| 36 | } |
| 37 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 38 | |
| 39 | template<typename T, typename R, typename... Args> |
| 40 | void PolicyManager::OnPolicyReadyToEvaluate( |
| 41 | scoped_refptr<EvaluationContext> ec, |
| 42 | base::Callback<void(EvalStatus status, const R& result)> callback, |
| 43 | T policy_method, Args... args) { |
| 44 | R result; |
| 45 | EvalStatus status = EvaluatePolicy(ec, policy_method, &result, args...); |
| 46 | |
| 47 | if (status != EvalStatus::kAskMeAgainLater) { |
| 48 | // AsyncPolicyRequest finished. |
| 49 | callback.Run(status, result); |
| 50 | return; |
| 51 | } |
| 52 | // Re-schedule the policy request. |
| 53 | // TODO(deymo): Use the information gathered on the EvaluationContext to |
| 54 | // hook on proper events from changes on the State in order to re-evaluate |
| 55 | // the policy after some period of time. |
| 56 | base::Closure closure = base::Bind( |
| 57 | &PolicyManager::OnPolicyReadyToEvaluate<T, R, Args...>, |
| 58 | base::Unretained(this), ec, callback, policy_method, args...); |
| 59 | RunFromMainLoopAfterTimeout(closure, base::TimeDelta::FromSeconds(20)); |
| 60 | } |
| 61 | |
| 62 | template<typename T, typename R, typename... Args> |
| 63 | EvalStatus PolicyManager::PolicyRequest(T policy_method, R* result, |
| 64 | Args... args) { |
| 65 | scoped_refptr<EvaluationContext> ec(new EvaluationContext); |
| 66 | // A PolicyRequest allways consists on a single evaluation on a new |
| 67 | // EvaluationContext. |
| 68 | return EvaluatePolicy(ec, policy_method, result, args...); |
| 69 | } |
| 70 | |
| 71 | template<typename T, typename R, typename... Args> |
| 72 | void PolicyManager::AsyncPolicyRequest( |
| 73 | base::Callback<void(EvalStatus, const R& result)> callback, |
| 74 | T policy_method, Args... args) { |
| 75 | |
| 76 | scoped_refptr<EvaluationContext> ec = new EvaluationContext; |
| 77 | base::Closure closure = base::Bind( |
| 78 | &PolicyManager::OnPolicyReadyToEvaluate<T, R, Args...>, |
| 79 | base::Unretained(this), ec, callback, policy_method, args...); |
| 80 | RunFromMainLoop(closure); |
| 81 | } |
| 82 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 83 | } // namespace chromeos_policy_manager |
| 84 | |
Gilad Arnold | 2cbb385 | 2014-03-07 12:40:50 -0800 | [diff] [blame] | 85 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_INL_H_ |