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