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