Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -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_EVALUATION_CONTEXT_H_ |
| 6 | #define UPDATE_ENGINE_UPDATE_MANAGER_EVALUATION_CONTEXT_H_ |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 7 | |
| 8 | #include <map> |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 9 | #include <memory> |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 10 | #include <string> |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 11 | |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 12 | #include <base/bind.h> |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 13 | #include <base/callback.h> |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 14 | #include <base/memory/ref_counted.h> |
Alex Deymo | db79953 | 2014-03-21 13:00:00 -0700 | [diff] [blame] | 15 | #include <base/memory/weak_ptr.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 16 | #include <base/time/time.h> |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 17 | |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 18 | #include "update_engine/clock_interface.h" |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 19 | #include "update_engine/update_manager/boxed_value.h" |
| 20 | #include "update_engine/update_manager/event_loop.h" |
| 21 | #include "update_engine/update_manager/variable.h" |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 22 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 23 | namespace chromeos_update_manager { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 24 | |
| 25 | // The EvaluationContext class is the interface between a policy implementation |
| 26 | // and the state. The EvaluationContext tracks the variables used by a policy |
| 27 | // request and caches the returned values, owning those cached values. |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 28 | // The same EvaluationContext should be re-used for all the evaluations of the |
| 29 | // same policy request (an AsyncPolicyRequest might involve several |
| 30 | // re-evaluations). Each evaluation of the EvaluationContext is run at a given |
| 31 | // point in time, which is used as a reference for the evaluation timeout and |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 32 | // the time based queries of the policy, such as |
| 33 | // Is{Wallclock,Monotonic}TimeGreaterThan(). |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 34 | // |
| 35 | // Example: |
| 36 | // |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 37 | // scoped_refptr<EvaluationContext> ec = new EvaluationContext(...); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 38 | // |
| 39 | // ... |
| 40 | // // The following call to ResetEvaluation() is optional. Use it to reset the |
| 41 | // // evaluation time if the EvaluationContext isn't used right after its |
| 42 | // // construction. |
| 43 | // ec->ResetEvaluation(); |
| 44 | // EvalStatus status = policy->SomeMethod(ec, state, &result, args...); |
| 45 | // |
| 46 | // ... |
| 47 | // // Run a closure when any of the used async variables changes its value or |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 48 | // // the timeout for re-query the values happens again. |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 49 | // ec->RunOnValueChangeOrTimeout(closure); |
| 50 | // // If the provided |closure| wants to re-evaluate the policy, it should |
| 51 | // // call ec->ResetEvaluation() to start a new evaluation. |
| 52 | // |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 53 | class EvaluationContext : public base::RefCounted<EvaluationContext>, |
| 54 | private BaseVariable::ObserverInterface { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 55 | public: |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 56 | EvaluationContext( |
| 57 | chromeos_update_engine::ClockInterface* clock, |
| 58 | base::TimeDelta evaluation_timeout, |
| 59 | base::TimeDelta expiration_timeout, |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 60 | std::unique_ptr<base::Callback<void(EvaluationContext*)>> unregister_cb); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 61 | EvaluationContext(chromeos_update_engine::ClockInterface* clock, |
| 62 | base::TimeDelta evaluation_timeout) |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 63 | : EvaluationContext( |
| 64 | clock, evaluation_timeout, base::TimeDelta::Max(), |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 65 | std::unique_ptr<base::Callback<void(EvaluationContext*)>>()) {} |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 66 | ~EvaluationContext(); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 67 | |
| 68 | // Returns a pointer to the value returned by the passed variable |var|. The |
| 69 | // EvaluationContext instance keeps the ownership of the returned object. The |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 70 | // returned object is valid during the life of the evaluation, even if the |
| 71 | // passed Variable changes it. |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 72 | // |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 73 | // In case of error, a null value is returned. |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 74 | template<typename T> |
| 75 | const T* GetValue(Variable<T>* var); |
| 76 | |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 77 | // Returns whether the evaluation time has surpassed |timestamp|, on either |
| 78 | // the ClockInterface::GetWallclockTime() or |
| 79 | // ClockInterface::GetMonotonicTime() scales, respectively. |
| 80 | bool IsWallclockTimeGreaterThan(base::Time timestamp); |
| 81 | bool IsMonotonicTimeGreaterThan(base::Time timestamp); |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 82 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 83 | // Returns whether the evaluation context has expired. |
| 84 | bool is_expired() const { return is_expired_; } |
| 85 | |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 86 | // TODO(deymo): Move the following methods to an interface only visible by the |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 87 | // UpdateManager class and not the policy implementations. |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 88 | |
| 89 | // Resets the EvaluationContext to its initial state removing all the |
| 90 | // non-const cached variables and re-setting the evaluation time. This should |
| 91 | // be called right before any new evaluation starts. |
| 92 | void ResetEvaluation(); |
| 93 | |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 94 | // Clears the expiration status of the EvaluationContext and resets its |
| 95 | // expiration timeout based on |expiration_timeout_|. This should be called if |
| 96 | // expiration occurred, prior to re-evaluating the policy. |
| 97 | void ResetExpiration(); |
| 98 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 99 | // Schedules the passed |callback| closure to be called when a cached |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 100 | // variable changes its value, a polling interval passes, or the context |
| 101 | // expiration occurs. If none of these events can happen, for example if |
| 102 | // there's no cached variable, this method returns false. |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 103 | // |
| 104 | // Right before the passed closure is called the EvaluationContext is |
| 105 | // reseted, removing all the non-const cached values. |
| 106 | bool RunOnValueChangeOrTimeout(base::Closure callback); |
| 107 | |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 108 | // Returns a textual representation of the evaluation context, |
| 109 | // including the variables and their values. This is intended only |
| 110 | // to help with debugging and the format may change in the future. |
| 111 | std::string DumpContext() const; |
| 112 | |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 113 | // Removes all the Observers callbacks and timeout events scheduled by |
| 114 | // RunOnValueChangeOrTimeout(). Also releases and returns the closure |
| 115 | // associated with these events. This method is idempotent. |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 116 | std::unique_ptr<base::Closure> RemoveObserversAndTimeout(); |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 117 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 118 | private: |
Gilad Arnold | fb794f4 | 2014-07-01 15:36:31 -0700 | [diff] [blame] | 119 | friend class UmEvaluationContextTest; |
| 120 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 121 | // BaseVariable::ObserverInterface override. |
| 122 | void ValueChanged(BaseVariable* var); |
| 123 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 124 | // Called from the main loop when a scheduled timeout has passed. |
| 125 | void OnTimeout(); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 126 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 127 | // Removes the observers from the used Variables and cancels the timeout, |
| 128 | // then executes the scheduled callback. |
| 129 | void OnValueChangedOrTimeout(); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 130 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 131 | // If |monotonic_deadline| is not Time::Max(), returns the remaining time |
| 132 | // until it is reached, or zero if it has passed. Otherwise, returns |
| 133 | // TimeDelta::Max(). |
| 134 | base::TimeDelta RemainingTime(base::Time monotonic_deadline) const; |
| 135 | |
| 136 | // Returns a monotonic clock timestamp at which |timeout| will have elapsed |
| 137 | // since the current time. |
| 138 | base::Time MonotonicDeadline(base::TimeDelta timeout); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 139 | |
| 140 | // A map to hold the cached values for every variable. |
| 141 | typedef std::map<BaseVariable*, BoxedValue> ValueCacheMap; |
| 142 | |
| 143 | // The cached values of the called Variables. |
| 144 | ValueCacheMap value_cache_; |
| 145 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 146 | // A callback used for triggering re-evaluation upon a value change or poll |
| 147 | // timeout, or notifying about the evaluation context expiration. It is up to |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 148 | // the caller to determine whether or not expiration occurred via |
| 149 | // is_expired(). |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 150 | std::unique_ptr<base::Closure> callback_; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 151 | |
| 152 | // The EventId returned by the event loop identifying the timeout callback. |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 153 | // Used for canceling the timeout callback. |
| 154 | EventId timeout_event_ = kEventIdNull; |
| 155 | |
| 156 | // Whether a timeout event firing marks the expiration of the evaluation |
| 157 | // context. |
| 158 | bool timeout_marks_expiration_; |
| 159 | |
| 160 | // Whether the evaluation context has indeed expired. |
| 161 | bool is_expired_ = false; |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 162 | |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 163 | // Pointer to the mockable clock interface; |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 164 | chromeos_update_engine::ClockInterface* const clock_; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 165 | |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 166 | // The timestamps when the evaluation of this EvaluationContext started, |
| 167 | // corresponding to ClockInterface::GetWallclockTime() and |
| 168 | // ClockInterface::GetMonotonicTime(), respectively. These values are reset |
| 169 | // every time ResetEvaluation() is called. |
| 170 | base::Time evaluation_start_wallclock_; |
| 171 | base::Time evaluation_start_monotonic_; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 172 | |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 173 | // The timestamps when a reevaluation should be triggered due to various |
| 174 | // expected value changes, corresponding to ClockInterface::GetWallclockTime() |
| 175 | // and ClockInterface::GetMonotonicTIme(), respectively. These timestamps are |
| 176 | // greater or equal to corresponding |evaluation_start_{wallclock,monotonic}_| |
| 177 | // counterparts since they are in the future; however, they may be smaller |
| 178 | // than the current corresponding times during the course of evaluation. |
| 179 | base::Time reevaluation_time_wallclock_; |
| 180 | base::Time reevaluation_time_monotonic_; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 181 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 182 | // The timeout of an evaluation. |
Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 183 | const base::TimeDelta evaluation_timeout_; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 184 | |
| 185 | // The timestamp in the ClockInterface::GetMonotonicTime() scale at which the |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 186 | // current evaluation should finish. |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 187 | base::Time evaluation_monotonic_deadline_; |
| 188 | |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 189 | // The expiration timeout of the evaluation context. |
| 190 | const base::TimeDelta expiration_timeout_; |
| 191 | |
| 192 | // The monotonic clock deadline at which expiration occurs. |
| 193 | base::Time expiration_monotonic_deadline_; |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 194 | |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 195 | // A callback for unregistering the context upon destruction. |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 196 | std::unique_ptr<base::Callback<void(EvaluationContext*)>> unregister_cb_; |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 197 | |
Alex Deymo | db79953 | 2014-03-21 13:00:00 -0700 | [diff] [blame] | 198 | base::WeakPtrFactory<EvaluationContext> weak_ptr_factory_; |
| 199 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 200 | DISALLOW_COPY_AND_ASSIGN(EvaluationContext); |
| 201 | }; |
| 202 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 203 | } // namespace chromeos_update_manager |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 204 | |
| 205 | // Include the implementation of the template methods. |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 206 | #include "update_engine/update_manager/evaluation_context-inl.h" |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 207 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 208 | #endif // UPDATE_ENGINE_UPDATE_MANAGER_EVALUATION_CONTEXT_H_ |