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 | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame^] | 12 | #include <base/time.h> |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 13 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 14 | #include "update_engine/policy_manager/boxed_value.h" |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame^] | 15 | #include "update_engine/policy_manager/event_loop.h" |
| 16 | #include "update_engine/policy_manager/variable.h" |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 17 | |
| 18 | namespace chromeos_policy_manager { |
| 19 | |
| 20 | // The EvaluationContext class is the interface between a policy implementation |
| 21 | // and the state. The EvaluationContext tracks the variables used by a policy |
| 22 | // request and caches the returned values, owning those cached values. |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame^] | 23 | class EvaluationContext : |
| 24 | public base::RefCounted<EvaluationContext>, |
| 25 | private BaseVariable::ObserverInterface { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 26 | public: |
| 27 | EvaluationContext() {} |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame^] | 28 | ~EvaluationContext(); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 29 | |
| 30 | // Returns a pointer to the value returned by the passed variable |var|. The |
| 31 | // EvaluationContext instance keeps the ownership of the returned object. The |
| 32 | // returned object is valid during the life of the EvaluationContext, even if |
| 33 | // the passed Variable changes it. |
| 34 | // |
| 35 | // In case of error, a NULL value is returned. |
| 36 | template<typename T> |
| 37 | const T* GetValue(Variable<T>* var); |
| 38 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame^] | 39 | // Schedules the passed |callback| closure to be called when a cached |
| 40 | // variable changes its value or a polling interval passes. If none of these |
| 41 | // events can happen, for example if there's no cached variable, this method |
| 42 | // returns false. |
| 43 | // |
| 44 | // Right before the passed closure is called the EvaluationContext is |
| 45 | // reseted, removing all the non-const cached values. |
| 46 | bool RunOnValueChangeOrTimeout(base::Closure callback); |
| 47 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 48 | private: |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame^] | 49 | // Removes all the Observers and timeout callbacks scheduled by |
| 50 | // RunOnValueChangeOrTimeout(). This method is idempotent. |
| 51 | void RemoveObserversAndTimeout(); |
| 52 | |
| 53 | // BaseVariable::ObserverInterface override. |
| 54 | void ValueChanged(BaseVariable* var); |
| 55 | |
| 56 | // Called from the main loop when the scheduled poll timeout has passed. |
| 57 | void OnPollTimeout(); |
| 58 | |
| 59 | // Removes the observers from the used Variables and cancels the poll timeout |
| 60 | // and executes the scheduled callback, if any. |
| 61 | void OnValueChangedOrPollTimeout(); |
| 62 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 63 | // The remaining time for the current evaluation. |
| 64 | base::TimeDelta RemainingTime() const; |
| 65 | |
| 66 | // A map to hold the cached values for every variable. |
| 67 | typedef std::map<BaseVariable*, BoxedValue> ValueCacheMap; |
| 68 | |
| 69 | // The cached values of the called Variables. |
| 70 | ValueCacheMap value_cache_; |
| 71 | |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame^] | 72 | // A pointer to a copy of the closure passed to RunOnValueChangeOrTimeout(). |
| 73 | scoped_ptr<base::Closure> value_changed_callback_; |
| 74 | |
| 75 | // The EventId returned by the event loop identifying the timeout callback. |
| 76 | // Used to cancel the timeout callback. |
| 77 | EventId poll_timeout_event_ = kEventIdNull; |
| 78 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 79 | DISALLOW_COPY_AND_ASSIGN(EvaluationContext); |
| 80 | }; |
| 81 | |
| 82 | } // namespace chromeos_policy_manager |
| 83 | |
| 84 | // Include the implementation of the template methods. |
| 85 | #include "update_engine/policy_manager/evaluation_context-inl.h" |
| 86 | |
Gilad Arnold | 2cbb385 | 2014-03-07 12:40:50 -0800 | [diff] [blame] | 87 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_EVALUATION_CONTEXT_H_ |