blob: 544a37136d80395d2e0e4ba3d495675a7df18d2c [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
Alex Deymo63784a52014-05-28 10:46:14 -07005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_INL_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_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 Deymo63784a52014-05-28 10:46:14 -070012#include "update_engine/update_manager/evaluation_context.h"
13#include "update_engine/update_manager/event_loop.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080014
Alex Deymo63784a52014-05-28 10:46:14 -070015namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -080016
Alex Deymoe75e0252014-04-08 14:00:11 -070017template<typename R, typename... Args>
Alex Deymo63784a52014-05-28 10:46:14 -070018EvalStatus UpdateManager::EvaluatePolicy(
Alex Deymoe75e0252014-04-08 14:00:11 -070019 EvaluationContext* ec,
Gilad Arnold13a82432014-05-19 12:52:44 -070020 EvalStatus (Policy::*policy_method)(EvaluationContext*, State*,
21 std::string*, R*,
22 Args...) const,
Alex Deymoe75e0252014-04-08 14:00:11 -070023 R* result, Args... args) {
Alex Deymoc705cc82014-02-19 11:15:00 -080024 std::string error;
25
26 // First try calling the actual policy.
Alex Deymo7b948f02014-03-10 17:01:10 -070027 EvalStatus status = (policy_.get()->*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) {
Gilad Arnold897b5e52014-05-21 09:37:18 -070031 LOG(WARNING) << "Policy request failed: " << error;
Alex Deymoc705cc82014-02-19 11:15:00 -080032 error.clear();
Alex Deymo7b948f02014-03-10 17:01:10 -070033 status = (default_policy_.*policy_method)(ec, state_.get(), &error,
Alex Deymo2de23f52014-02-26 14:30:13 -080034 result, args...);
Alex Deymoc705cc82014-02-19 11:15:00 -080035
Alex Deymoe636c3c2014-03-11 19:02:08 -070036 if (status == EvalStatus::kFailed) {
Gilad Arnold897b5e52014-05-21 09:37:18 -070037 LOG(WARNING) << "Request to default policy also failed: " << error;
Alex Deymoc705cc82014-02-19 11:15:00 -080038 }
39 }
40 // TODO(deymo): Log the actual state used from the EvaluationContext.
41 return status;
42}
43
Alex Deymoe75e0252014-04-08 14:00:11 -070044template<typename R, typename... Args>
Alex Deymo63784a52014-05-28 10:46:14 -070045void UpdateManager::OnPolicyReadyToEvaluate(
Alex Deymo7b948f02014-03-10 17:01:10 -070046 scoped_refptr<EvaluationContext> ec,
47 base::Callback<void(EvalStatus status, const R& result)> callback,
Gilad Arnold13a82432014-05-19 12:52:44 -070048 EvalStatus (Policy::*policy_method)(EvaluationContext*, State*,
49 std::string*, R*,
50 Args...) const,
Alex Deymoe75e0252014-04-08 14:00:11 -070051 Args... args) {
Alex Deymo41a75a72014-04-15 15:36:22 -070052 ec->ResetEvaluation();
Alex Deymo7b948f02014-03-10 17:01:10 -070053 R result;
54 EvalStatus status = EvaluatePolicy(ec, policy_method, &result, args...);
55
56 if (status != EvalStatus::kAskMeAgainLater) {
57 // AsyncPolicyRequest finished.
58 callback.Run(status, result);
59 return;
60 }
Alex Deymo53556ec2014-03-17 10:05:57 -070061 // Re-schedule the policy request based on used variables.
Alex Deymo7b948f02014-03-10 17:01:10 -070062 base::Closure closure = base::Bind(
Alex Deymo63784a52014-05-28 10:46:14 -070063 &UpdateManager::OnPolicyReadyToEvaluate<R, Args...>,
Alex Deymo7b948f02014-03-10 17:01:10 -070064 base::Unretained(this), ec, callback, policy_method, args...);
Alex Deymo53556ec2014-03-17 10:05:57 -070065
66 if (!ec->RunOnValueChangeOrTimeout(closure)) {
67 // The policy method didn't use any non-const variable nor there's any
68 // time-based event that will change the status of evaluation. We call the
69 // callback with EvalStatus::kAskMeAgainLater.
70 LOG(ERROR) << "Policy implementation didn't use any non-const variable "
71 "but returned kAskMeAgainLater.";
72 callback.Run(EvalStatus::kAskMeAgainLater, result);
73 return;
74 }
Alex Deymo7b948f02014-03-10 17:01:10 -070075}
76
Gilad Arnold13a82432014-05-19 12:52:44 -070077template<typename R, typename... ActualArgs, typename... ExpectedArgs>
Alex Deymo63784a52014-05-28 10:46:14 -070078EvalStatus UpdateManager::PolicyRequest(
Gilad Arnold13a82432014-05-19 12:52:44 -070079 EvalStatus (Policy::*policy_method)(EvaluationContext*, State*,
80 std::string*, R*,
81 ExpectedArgs...) const,
82 R* result, ActualArgs... args) {
Alex Deymo41a75a72014-04-15 15:36:22 -070083 scoped_refptr<EvaluationContext> ec(new EvaluationContext(clock_));
Alex Deymo7b948f02014-03-10 17:01:10 -070084 // A PolicyRequest allways consists on a single evaluation on a new
85 // EvaluationContext.
Gilad Arnold13a82432014-05-19 12:52:44 -070086 // IMPORTANT: To ensure that ActualArgs can be converted to ExpectedArgs, we
87 // explicitly instantiate EvaluatePolicy with the latter in lieu of the
88 // former.
Gilad Arnold897b5e52014-05-21 09:37:18 -070089 EvalStatus ret = EvaluatePolicy<R, ExpectedArgs...>(ec, policy_method, result,
90 args...);
91 // Sync policy requests must not block, if they do then this is an error.
92 DCHECK(EvalStatus::kAskMeAgainLater != ret);
93 LOG_IF(WARNING, EvalStatus::kAskMeAgainLater == ret)
94 << "Sync request used with an async policy";
95 return ret;
Alex Deymo7b948f02014-03-10 17:01:10 -070096}
97
Gilad Arnold13a82432014-05-19 12:52:44 -070098template<typename R, typename... ActualArgs, typename... ExpectedArgs>
Alex Deymo63784a52014-05-28 10:46:14 -070099void UpdateManager::AsyncPolicyRequest(
Alex Deymo7b948f02014-03-10 17:01:10 -0700100 base::Callback<void(EvalStatus, const R& result)> callback,
Gilad Arnold13a82432014-05-19 12:52:44 -0700101 EvalStatus (Policy::*policy_method)(EvaluationContext*, State*,
102 std::string*, R*,
103 ExpectedArgs...) const,
104 ActualArgs... args) {
Alex Deymo41a75a72014-04-15 15:36:22 -0700105 scoped_refptr<EvaluationContext> ec = new EvaluationContext(clock_);
Gilad Arnold13a82432014-05-19 12:52:44 -0700106 // IMPORTANT: To ensure that ActualArgs can be converted to ExpectedArgs, we
Alex Deymo63784a52014-05-28 10:46:14 -0700107 // explicitly instantiate UpdateManager::OnPolicyReadyToEvaluate with the
Gilad Arnold13a82432014-05-19 12:52:44 -0700108 // latter in lieu of the former.
Alex Deymo7b948f02014-03-10 17:01:10 -0700109 base::Closure closure = base::Bind(
Alex Deymo63784a52014-05-28 10:46:14 -0700110 &UpdateManager::OnPolicyReadyToEvaluate<R, ExpectedArgs...>,
Alex Deymo7b948f02014-03-10 17:01:10 -0700111 base::Unretained(this), ec, callback, policy_method, args...);
112 RunFromMainLoop(closure);
113}
114
Alex Deymo63784a52014-05-28 10:46:14 -0700115} // namespace chromeos_update_manager
Alex Deymoc705cc82014-02-19 11:15:00 -0800116
Alex Deymo63784a52014-05-28 10:46:14 -0700117#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_INL_H_