blob: 6c361a4c24346d2fff362c6144d99189c2514c1b [file] [log] [blame]
Alex Deymoc705cc82014-02-19 11:15:00 -08001// 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
5#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_INL_H
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_INL_H
7
Alex Deymo7b948f02014-03-10 17:01:10 -07008#include <base/bind.h>
9
Alex Deymoc705cc82014-02-19 11:15:00 -080010#include "update_engine/policy_manager/evaluation_context.h"
11
12namespace chromeos_policy_manager {
13
Alex Deymoc705cc82014-02-19 11:15:00 -080014template<typename T, typename R, typename... Args>
Alex Deymo7b948f02014-03-10 17:01:10 -070015EvalStatus PolicyManager::EvaluatePolicy(EvaluationContext* ec,
16 T policy_method, R* result,
17 Args... args) {
Alex Deymoc705cc82014-02-19 11:15:00 -080018 std::string error;
19
20 // First try calling the actual policy.
Alex Deymo7b948f02014-03-10 17:01:10 -070021 EvalStatus status = (policy_.get()->*policy_method)(ec, state_.get(), &error,
Alex Deymo2de23f52014-02-26 14:30:13 -080022 result, args...);
Alex Deymoc705cc82014-02-19 11:15:00 -080023
Alex Deymoe636c3c2014-03-11 19:02:08 -070024 if (status == EvalStatus::kFailed) {
Alex Deymoc705cc82014-02-19 11:15:00 -080025 LOG(WARNING) << "PolicyRequest() failed with error: " << error;
26 error.clear();
Alex Deymo7b948f02014-03-10 17:01:10 -070027 status = (default_policy_.*policy_method)(ec, state_.get(), &error,
Alex Deymo2de23f52014-02-26 14:30:13 -080028 result, args...);
Alex Deymoc705cc82014-02-19 11:15:00 -080029
Alex Deymoe636c3c2014-03-11 19:02:08 -070030 if (status == EvalStatus::kFailed) {
Alex Deymoc705cc82014-02-19 11:15:00 -080031 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 Deymo7b948f02014-03-10 17:01:10 -070038
39template<typename T, typename R, typename... Args>
40void 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
62template<typename T, typename R, typename... Args>
63EvalStatus 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
71template<typename T, typename R, typename... Args>
72void 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 Deymoc705cc82014-02-19 11:15:00 -080083} // namespace chromeos_policy_manager
84
85#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_INL_H