blob: 9e8b3c2a6d8b349781de33ded78977e0d825c0a4 [file] [log] [blame]
Alex Deymo23949d42014-02-05 15:20:59 -08001// 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 Arnold2cbb3852014-03-07 12:40:50 -08005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_EVALUATION_CONTEXT_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_EVALUATION_CONTEXT_H_
Alex Deymo23949d42014-02-05 15:20:59 -08007
8#include <map>
9
Alex Deymo53556ec2014-03-17 10:05:57 -070010#include <base/callback.h>
Alex Deymo7b948f02014-03-10 17:01:10 -070011#include <base/memory/ref_counted.h>
Alex Deymo53556ec2014-03-17 10:05:57 -070012#include <base/time.h>
Alex Deymo7b948f02014-03-10 17:01:10 -070013
Alex Deymo23949d42014-02-05 15:20:59 -080014#include "update_engine/policy_manager/boxed_value.h"
Alex Deymo53556ec2014-03-17 10:05:57 -070015#include "update_engine/policy_manager/event_loop.h"
16#include "update_engine/policy_manager/variable.h"
Alex Deymo23949d42014-02-05 15:20:59 -080017
18namespace 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 Deymo53556ec2014-03-17 10:05:57 -070023class EvaluationContext :
24 public base::RefCounted<EvaluationContext>,
25 private BaseVariable::ObserverInterface {
Alex Deymo23949d42014-02-05 15:20:59 -080026 public:
27 EvaluationContext() {}
Alex Deymo53556ec2014-03-17 10:05:57 -070028 ~EvaluationContext();
Alex Deymo23949d42014-02-05 15:20:59 -080029
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 Deymo53556ec2014-03-17 10:05:57 -070039 // 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 Deymo23949d42014-02-05 15:20:59 -080048 private:
Alex Deymo53556ec2014-03-17 10:05:57 -070049 // 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 Deymo23949d42014-02-05 15:20:59 -080063 // 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 Deymo53556ec2014-03-17 10:05:57 -070072 // 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 Deymo23949d42014-02-05 15:20:59 -080079 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 Arnold2cbb3852014-03-07 12:40:50 -080087#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_EVALUATION_CONTEXT_H_