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 |
| 31 | // the time based queries of the policy, such as IsTimeGreaterThan(). |
| 32 | // |
| 33 | // Example: |
| 34 | // |
| 35 | // scoped_refptr<EvaluationContext> ec = new EvaluationContext; |
| 36 | // |
| 37 | // ... |
| 38 | // // The following call to ResetEvaluation() is optional. Use it to reset the |
| 39 | // // evaluation time if the EvaluationContext isn't used right after its |
| 40 | // // construction. |
| 41 | // ec->ResetEvaluation(); |
| 42 | // EvalStatus status = policy->SomeMethod(ec, state, &result, args...); |
| 43 | // |
| 44 | // ... |
| 45 | // // Run a closure when any of the used async variables changes its value or |
| 46 | // // the timeout for requery the values happens again. |
| 47 | // ec->RunOnValueChangeOrTimeout(closure); |
| 48 | // // If the provided |closure| wants to re-evaluate the policy, it should |
| 49 | // // call ec->ResetEvaluation() to start a new evaluation. |
| 50 | // |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame^] | 51 | class EvaluationContext : public base::RefCounted<EvaluationContext>, |
| 52 | private BaseVariable::ObserverInterface { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 53 | public: |
Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 54 | explicit EvaluationContext(chromeos_update_engine::ClockInterface* clock, |
| 55 | base::TimeDelta evaluation_timeout); |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 56 | ~EvaluationContext(); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 57 | |
| 58 | // Returns a pointer to the value returned by the passed variable |var|. The |
| 59 | // EvaluationContext instance keeps the ownership of the returned object. The |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 60 | // returned object is valid during the life of the evaluation, even if the |
| 61 | // passed Variable changes it. |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 62 | // |
| 63 | // In case of error, a NULL value is returned. |
| 64 | template<typename T> |
| 65 | const T* GetValue(Variable<T>* var); |
| 66 | |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 67 | // Returns whether the passed |timestamp| is greater than the evaluation |
| 68 | // time. The |timestamp| value should be in the same scale as the values |
| 69 | // returned by ClockInterface::GetWallclockTime(). |
| 70 | bool IsTimeGreaterThan(base::Time timestamp); |
| 71 | |
| 72 | // 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] | 73 | // UpdateManager class and not the policy implementations. |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 74 | |
| 75 | // Resets the EvaluationContext to its initial state removing all the |
| 76 | // non-const cached variables and re-setting the evaluation time. This should |
| 77 | // be called right before any new evaluation starts. |
| 78 | void ResetEvaluation(); |
| 79 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 80 | // Schedules the passed |callback| closure to be called when a cached |
| 81 | // variable changes its value or a polling interval passes. If none of these |
| 82 | // events can happen, for example if there's no cached variable, this method |
| 83 | // returns false. |
| 84 | // |
| 85 | // Right before the passed closure is called the EvaluationContext is |
| 86 | // reseted, removing all the non-const cached values. |
| 87 | bool RunOnValueChangeOrTimeout(base::Closure callback); |
| 88 | |
David Zeuthen | c149028 | 2014-04-29 16:25:03 -0700 | [diff] [blame] | 89 | // Returns a textual representation of the evaluation context, |
| 90 | // including the variables and their values. This is intended only |
| 91 | // to help with debugging and the format may change in the future. |
| 92 | std::string DumpContext() const; |
| 93 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 94 | private: |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 95 | // Removes all the Observers and timeout callbacks scheduled by |
| 96 | // RunOnValueChangeOrTimeout(). This method is idempotent. |
| 97 | void RemoveObserversAndTimeout(); |
| 98 | |
| 99 | // BaseVariable::ObserverInterface override. |
| 100 | void ValueChanged(BaseVariable* var); |
| 101 | |
| 102 | // Called from the main loop when the scheduled poll timeout has passed. |
| 103 | void OnPollTimeout(); |
| 104 | |
| 105 | // Removes the observers from the used Variables and cancels the poll timeout |
| 106 | // and executes the scheduled callback, if any. |
| 107 | void OnValueChangedOrPollTimeout(); |
| 108 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 109 | // The remaining time for the current evaluation. |
| 110 | base::TimeDelta RemainingTime() const; |
| 111 | |
| 112 | // A map to hold the cached values for every variable. |
| 113 | typedef std::map<BaseVariable*, BoxedValue> ValueCacheMap; |
| 114 | |
| 115 | // The cached values of the called Variables. |
| 116 | ValueCacheMap value_cache_; |
| 117 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 118 | // A pointer to a copy of the closure passed to RunOnValueChangeOrTimeout(). |
| 119 | scoped_ptr<base::Closure> value_changed_callback_; |
| 120 | |
| 121 | // The EventId returned by the event loop identifying the timeout callback. |
| 122 | // Used to cancel the timeout callback. |
| 123 | EventId poll_timeout_event_ = kEventIdNull; |
| 124 | |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 125 | // Pointer to the mockable clock interface; |
| 126 | chromeos_update_engine::ClockInterface* clock_; |
| 127 | |
| 128 | // The timestamp when the evaluation of this EvaluationContext started. This |
| 129 | // value is reset every time ResetEvaluation() is called. The time source |
| 130 | // used is the ClockInterface::GetWallclockTime(). |
| 131 | base::Time evaluation_start_; |
| 132 | |
| 133 | // The timestamp measured on the GetWallclockTime() scale, when a reevaluation |
| 134 | // should be triggered due to IsTimeGreaterThan() calls value changes. This |
| 135 | // timestamp is greater or equal to |evaluation_start_| since it is a |
| 136 | // timestamp in the future, but it can be lower than the current |
| 137 | // GetWallclockTime() at some point of the evaluation. |
| 138 | base::Time reevaluation_time_; |
| 139 | |
| 140 | // The timeout of an evaluation, used to compute the RemainingTime() of an |
| 141 | // evaluation. |
Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 142 | const base::TimeDelta evaluation_timeout_; |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 143 | |
| 144 | // The timestamp in the ClockInterface::GetMonotonicTime() scale at which the |
| 145 | // current evaluation should finish. This is used to compute the |
| 146 | // RemainingTime(). |
| 147 | base::Time evaluation_monotonic_deadline_; |
| 148 | |
Alex Deymo | db79953 | 2014-03-21 13:00:00 -0700 | [diff] [blame] | 149 | base::WeakPtrFactory<EvaluationContext> weak_ptr_factory_; |
| 150 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 151 | DISALLOW_COPY_AND_ASSIGN(EvaluationContext); |
| 152 | }; |
| 153 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 154 | } // namespace chromeos_update_manager |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 155 | |
| 156 | // Include the implementation of the template methods. |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 157 | #include "update_engine/update_manager/evaluation_context-inl.h" |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 158 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame^] | 159 | #endif // UPDATE_ENGINE_UPDATE_MANAGER_EVALUATION_CONTEXT_H_ |