blob: f19df5541dfaa797df989b4bc82653c14693081b [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
Gilad Arnold2cbb3852014-03-07 12:40:50 -08005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_INL_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_INL_H_
Alex Deymoc705cc82014-02-19 11:15:00 -08007
Alex Deymo53556ec2014-03-17 10:05:57 -07008#include <string>
9
Alex Deymo7b948f02014-03-10 17:01:10 -070010#include <base/bind.h>
11
Alex Deymoc705cc82014-02-19 11:15:00 -080012#include "update_engine/policy_manager/evaluation_context.h"
Alex Deymo53556ec2014-03-17 10:05:57 -070013#include "update_engine/policy_manager/event_loop.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080014
15namespace chromeos_policy_manager {
16
Alex Deymoe75e0252014-04-08 14:00:11 -070017template<typename R, typename... Args>
18EvalStatus PolicyManager::EvaluatePolicy(
19 EvaluationContext* ec,
20 EvalStatus (Policy::*policy_method)(EvaluationContext* ec,
21 State* state,
22 std::string* error,
23 R* result,
24 Args... args) const,
25 R* result, Args... args) {
Alex Deymoc705cc82014-02-19 11:15:00 -080026 std::string error;
27
28 // First try calling the actual policy.
Alex Deymo7b948f02014-03-10 17:01:10 -070029 EvalStatus status = (policy_.get()->*policy_method)(ec, state_.get(), &error,
Alex Deymo2de23f52014-02-26 14:30:13 -080030 result, args...);
Alex Deymoc705cc82014-02-19 11:15:00 -080031
Alex Deymoe636c3c2014-03-11 19:02:08 -070032 if (status == EvalStatus::kFailed) {
Alex Deymoc705cc82014-02-19 11:15:00 -080033 LOG(WARNING) << "PolicyRequest() failed with error: " << error;
34 error.clear();
Alex Deymo7b948f02014-03-10 17:01:10 -070035 status = (default_policy_.*policy_method)(ec, state_.get(), &error,
Alex Deymo2de23f52014-02-26 14:30:13 -080036 result, args...);
Alex Deymoc705cc82014-02-19 11:15:00 -080037
Alex Deymoe636c3c2014-03-11 19:02:08 -070038 if (status == EvalStatus::kFailed) {
Alex Deymoc705cc82014-02-19 11:15:00 -080039 LOG(WARNING) << "Request to DefaultPolicy also failed, passing error.";
40 }
41 }
42 // TODO(deymo): Log the actual state used from the EvaluationContext.
43 return status;
44}
45
Alex Deymoe75e0252014-04-08 14:00:11 -070046template<typename R, typename... Args>
Alex Deymo7b948f02014-03-10 17:01:10 -070047void PolicyManager::OnPolicyReadyToEvaluate(
48 scoped_refptr<EvaluationContext> ec,
49 base::Callback<void(EvalStatus status, const R& result)> callback,
Alex Deymoe75e0252014-04-08 14:00:11 -070050 EvalStatus (Policy::*policy_method)(EvaluationContext* ec,
51 State* state,
52 std::string* error,
53 R* result,
54 Args... args) const,
55 Args... args) {
Alex Deymo7b948f02014-03-10 17:01:10 -070056 R result;
57 EvalStatus status = EvaluatePolicy(ec, policy_method, &result, args...);
58
59 if (status != EvalStatus::kAskMeAgainLater) {
60 // AsyncPolicyRequest finished.
61 callback.Run(status, result);
62 return;
63 }
Alex Deymo53556ec2014-03-17 10:05:57 -070064 // Re-schedule the policy request based on used variables.
Alex Deymo7b948f02014-03-10 17:01:10 -070065 base::Closure closure = base::Bind(
Alex Deymoe75e0252014-04-08 14:00:11 -070066 &PolicyManager::OnPolicyReadyToEvaluate<R, Args...>,
Alex Deymo7b948f02014-03-10 17:01:10 -070067 base::Unretained(this), ec, callback, policy_method, args...);
Alex Deymo53556ec2014-03-17 10:05:57 -070068
69 if (!ec->RunOnValueChangeOrTimeout(closure)) {
70 // The policy method didn't use any non-const variable nor there's any
71 // time-based event that will change the status of evaluation. We call the
72 // callback with EvalStatus::kAskMeAgainLater.
73 LOG(ERROR) << "Policy implementation didn't use any non-const variable "
74 "but returned kAskMeAgainLater.";
75 callback.Run(EvalStatus::kAskMeAgainLater, result);
76 return;
77 }
Alex Deymo7b948f02014-03-10 17:01:10 -070078}
79
Alex Deymoe75e0252014-04-08 14:00:11 -070080template<typename R, typename... Args>
81EvalStatus PolicyManager::PolicyRequest(
82 EvalStatus (Policy::*policy_method)(EvaluationContext* ec,
83 State* state,
84 std::string* error,
85 R* result,
86 Args... args) const,
87 R* result, Args... args) {
Alex Deymo7b948f02014-03-10 17:01:10 -070088 scoped_refptr<EvaluationContext> ec(new EvaluationContext);
89 // A PolicyRequest allways consists on a single evaluation on a new
90 // EvaluationContext.
91 return EvaluatePolicy(ec, policy_method, result, args...);
92}
93
Alex Deymoe75e0252014-04-08 14:00:11 -070094template<typename R, typename... Args>
Alex Deymo7b948f02014-03-10 17:01:10 -070095void PolicyManager::AsyncPolicyRequest(
96 base::Callback<void(EvalStatus, const R& result)> callback,
Alex Deymoe75e0252014-04-08 14:00:11 -070097 EvalStatus (Policy::*policy_method)(EvaluationContext* ec,
98 State* state,
99 std::string* error,
100 R* result,
101 Args... args) const,
102 Args... args) {
Alex Deymo7b948f02014-03-10 17:01:10 -0700103 scoped_refptr<EvaluationContext> ec = new EvaluationContext;
104 base::Closure closure = base::Bind(
Alex Deymoe75e0252014-04-08 14:00:11 -0700105 &PolicyManager::OnPolicyReadyToEvaluate<R, Args...>,
Alex Deymo7b948f02014-03-10 17:01:10 -0700106 base::Unretained(this), ec, callback, policy_method, args...);
107 RunFromMainLoop(closure);
108}
109
Alex Deymoc705cc82014-02-19 11:15:00 -0800110} // namespace chromeos_policy_manager
111
Gilad Arnold2cbb3852014-03-07 12:40:50 -0800112#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_INL_H_