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