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 | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 5 | #ifndef UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_INL_H_ |
| 6 | #define UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_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 | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 12 | #include "update_engine/update_manager/evaluation_context.h" |
| 13 | #include "update_engine/update_manager/event_loop.h" |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 14 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 15 | namespace chromeos_update_manager { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 16 | |
Alex Deymo | e75e025 | 2014-04-08 14:00:11 -0700 | [diff] [blame] | 17 | template<typename R, typename... Args> |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 18 | EvalStatus UpdateManager::EvaluatePolicy( |
Alex Deymo | e75e025 | 2014-04-08 14:00:11 -0700 | [diff] [blame] | 19 | EvaluationContext* ec, |
Gilad Arnold | 13a8243 | 2014-05-19 12:52:44 -0700 | [diff] [blame] | 20 | EvalStatus (Policy::*policy_method)(EvaluationContext*, State*, |
| 21 | std::string*, R*, |
| 22 | Args...) const, |
Alex Deymo | e75e025 | 2014-04-08 14:00:11 -0700 | [diff] [blame] | 23 | R* result, Args... args) { |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame^] | 24 | // If expiration timeout fired, dump the context and reset expiration. |
| 25 | // IMPORTANT: We must still proceed with evaluation of the policy in this |
| 26 | // case, so that the evaluation time (and corresponding reevaluation timeouts) |
| 27 | // are readjusted. |
| 28 | if (ec->is_expired()) { |
| 29 | LOG(WARNING) << "Request timed out, evaluation context: " |
| 30 | << ec->DumpContext(); |
| 31 | ec->ResetExpiration(); |
| 32 | } |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 33 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 34 | // Reset the evaluation context. |
| 35 | ec->ResetEvaluation(); |
| 36 | |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame^] | 37 | const std::string policy_name = policy_->PolicyRequestName(policy_method); |
| 38 | LOG(INFO) << policy_name << ": START"; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 39 | |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame^] | 40 | // First try calling the actual policy. |
| 41 | std::string error; |
| 42 | EvalStatus status = (policy_.get()->*policy_method)(ec, state_.get(), &error, |
| 43 | result, args...); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 44 | // If evaluating the main policy failed, defer to the default policy. |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 45 | if (status == EvalStatus::kFailed) { |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame^] | 46 | LOG(WARNING) << "Evaluating policy failed: " << error |
| 47 | << "\nEvaluation context: " << ec->DumpContext(); |
| 48 | error.clear(); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 49 | status = (default_policy_.*policy_method)(ec, state_.get(), &error, result, |
| 50 | args...); |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame^] | 51 | if (status == EvalStatus::kFailed) { |
| 52 | LOG(WARNING) << "Evaluating default policy failed: " << error; |
| 53 | } else if (status == EvalStatus::kAskMeAgainLater) { |
| 54 | LOG(ERROR) |
| 55 | << "Default policy would block; this is a bug, forcing failure."; |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 56 | status = EvalStatus::kFailed; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 57 | } |
| 58 | } |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 59 | |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame^] | 60 | LOG(INFO) << policy_name << ": END"; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 61 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 62 | return status; |
| 63 | } |
| 64 | |
Alex Deymo | e75e025 | 2014-04-08 14:00:11 -0700 | [diff] [blame] | 65 | template<typename R, typename... Args> |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 66 | void UpdateManager::OnPolicyReadyToEvaluate( |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 67 | scoped_refptr<EvaluationContext> ec, |
| 68 | base::Callback<void(EvalStatus status, const R& result)> callback, |
Gilad Arnold | 13a8243 | 2014-05-19 12:52:44 -0700 | [diff] [blame] | 69 | EvalStatus (Policy::*policy_method)(EvaluationContext*, State*, |
| 70 | std::string*, R*, |
| 71 | Args...) const, |
Alex Deymo | e75e025 | 2014-04-08 14:00:11 -0700 | [diff] [blame] | 72 | Args... args) { |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 73 | // Evaluate the policy. |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 74 | R result; |
| 75 | EvalStatus status = EvaluatePolicy(ec, policy_method, &result, args...); |
| 76 | |
| 77 | if (status != EvalStatus::kAskMeAgainLater) { |
| 78 | // AsyncPolicyRequest finished. |
| 79 | callback.Run(status, result); |
| 80 | return; |
| 81 | } |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 82 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 83 | // Re-schedule the policy request based on used variables. |
| 84 | base::Closure reeval_callback = base::Bind( |
| 85 | &UpdateManager::OnPolicyReadyToEvaluate<R, Args...>, |
| 86 | base::Unretained(this), ec, callback, |
| 87 | policy_method, args...); |
| 88 | if (ec->RunOnValueChangeOrTimeout(reeval_callback)) |
| 89 | return; // Reevaluation scheduled successfully. |
| 90 | |
| 91 | // Scheduling a reevaluation can fail because policy method didn't use any |
| 92 | // non-const variable nor there's any time-based event that will change the |
| 93 | // status of evaluation. Alternatively, this may indicate an error in the use |
| 94 | // of the scheduling interface. |
| 95 | LOG(ERROR) << "Failed to schedule a reevaluation of policy " |
| 96 | << policy_->PolicyRequestName(policy_method) << "; this is a bug."; |
| 97 | callback.Run(status, result); |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Gilad Arnold | 13a8243 | 2014-05-19 12:52:44 -0700 | [diff] [blame] | 100 | template<typename R, typename... ActualArgs, typename... ExpectedArgs> |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 101 | EvalStatus UpdateManager::PolicyRequest( |
Gilad Arnold | 13a8243 | 2014-05-19 12:52:44 -0700 | [diff] [blame] | 102 | EvalStatus (Policy::*policy_method)(EvaluationContext*, State*, |
| 103 | std::string*, R*, |
| 104 | ExpectedArgs...) const, |
| 105 | R* result, ActualArgs... args) { |
Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 106 | scoped_refptr<EvaluationContext> ec( |
| 107 | new EvaluationContext(clock_, evaluation_timeout_)); |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 108 | // A PolicyRequest always consists on a single evaluation on a new |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 109 | // EvaluationContext. |
Gilad Arnold | 13a8243 | 2014-05-19 12:52:44 -0700 | [diff] [blame] | 110 | // IMPORTANT: To ensure that ActualArgs can be converted to ExpectedArgs, we |
| 111 | // explicitly instantiate EvaluatePolicy with the latter in lieu of the |
| 112 | // former. |
Gilad Arnold | 897b5e5 | 2014-05-21 09:37:18 -0700 | [diff] [blame] | 113 | EvalStatus ret = EvaluatePolicy<R, ExpectedArgs...>(ec, policy_method, result, |
| 114 | args...); |
| 115 | // Sync policy requests must not block, if they do then this is an error. |
| 116 | DCHECK(EvalStatus::kAskMeAgainLater != ret); |
| 117 | LOG_IF(WARNING, EvalStatus::kAskMeAgainLater == ret) |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 118 | << "Sync request used with an async policy; this is a bug"; |
Gilad Arnold | 897b5e5 | 2014-05-21 09:37:18 -0700 | [diff] [blame] | 119 | return ret; |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Gilad Arnold | 13a8243 | 2014-05-19 12:52:44 -0700 | [diff] [blame] | 122 | template<typename R, typename... ActualArgs, typename... ExpectedArgs> |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 123 | void UpdateManager::AsyncPolicyRequest( |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 124 | base::Callback<void(EvalStatus, const R& result)> callback, |
Gilad Arnold | 13a8243 | 2014-05-19 12:52:44 -0700 | [diff] [blame] | 125 | EvalStatus (Policy::*policy_method)(EvaluationContext*, State*, |
| 126 | std::string*, R*, |
| 127 | ExpectedArgs...) const, |
| 128 | ActualArgs... args) { |
Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 129 | scoped_refptr<EvaluationContext> ec = |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame^] | 130 | new EvaluationContext(clock_, evaluation_timeout_, expiration_timeout_); |
Gilad Arnold | 13a8243 | 2014-05-19 12:52:44 -0700 | [diff] [blame] | 131 | // IMPORTANT: To ensure that ActualArgs can be converted to ExpectedArgs, we |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 132 | // explicitly instantiate UpdateManager::OnPolicyReadyToEvaluate with the |
Gilad Arnold | 13a8243 | 2014-05-19 12:52:44 -0700 | [diff] [blame] | 133 | // latter in lieu of the former. |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 134 | base::Closure eval_callback = base::Bind( |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 135 | &UpdateManager::OnPolicyReadyToEvaluate<R, ExpectedArgs...>, |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 136 | base::Unretained(this), ec, callback, policy_method, args...); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 137 | RunFromMainLoop(eval_callback); |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 140 | } // namespace chromeos_update_manager |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 141 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 142 | #endif // UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_INL_H_ |