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