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