blob: 059256e2c6545b2ff1d5d81073008b25016cd846 [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 Deymo1f012912014-04-24 19:08:04 -070012#include <base/memory/scoped_ptr.h>
Alex Deymodb799532014-03-21 13:00:00 -070013#include <base/memory/weak_ptr.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070014#include <base/time/time.h>
Alex Deymo7b948f02014-03-10 17:01:10 -070015
Alex Deymo41a75a72014-04-15 15:36:22 -070016#include "update_engine/clock_interface.h"
Alex Deymo23949d42014-02-05 15:20:59 -080017#include "update_engine/policy_manager/boxed_value.h"
Alex Deymo53556ec2014-03-17 10:05:57 -070018#include "update_engine/policy_manager/event_loop.h"
19#include "update_engine/policy_manager/variable.h"
Alex Deymo23949d42014-02-05 15:20:59 -080020
21namespace chromeos_policy_manager {
22
23// The EvaluationContext class is the interface between a policy implementation
24// and the state. The EvaluationContext tracks the variables used by a policy
25// request and caches the returned values, owning those cached values.
Alex Deymo41a75a72014-04-15 15:36:22 -070026// The same EvaluationContext should be re-used for all the evaluations of the
27// same policy request (an AsyncPolicyRequest might involve several
28// re-evaluations). Each evaluation of the EvaluationContext is run at a given
29// point in time, which is used as a reference for the evaluation timeout and
30// the time based queries of the policy, such as IsTimeGreaterThan().
31//
32// Example:
33//
34// scoped_refptr<EvaluationContext> ec = new EvaluationContext;
35//
36// ...
37// // The following call to ResetEvaluation() is optional. Use it to reset the
38// // evaluation time if the EvaluationContext isn't used right after its
39// // construction.
40// ec->ResetEvaluation();
41// EvalStatus status = policy->SomeMethod(ec, state, &result, args...);
42//
43// ...
44// // Run a closure when any of the used async variables changes its value or
45// // the timeout for requery the values happens again.
46// ec->RunOnValueChangeOrTimeout(closure);
47// // If the provided |closure| wants to re-evaluate the policy, it should
48// // call ec->ResetEvaluation() to start a new evaluation.
49//
Alex Deymo53556ec2014-03-17 10:05:57 -070050class EvaluationContext :
51 public base::RefCounted<EvaluationContext>,
52 private BaseVariable::ObserverInterface {
Alex Deymo23949d42014-02-05 15:20:59 -080053 public:
Alex Deymo41a75a72014-04-15 15:36:22 -070054 explicit EvaluationContext(chromeos_update_engine::ClockInterface* clock);
Alex Deymo53556ec2014-03-17 10:05:57 -070055 ~EvaluationContext();
Alex Deymo23949d42014-02-05 15:20:59 -080056
57 // Returns a pointer to the value returned by the passed variable |var|. The
58 // EvaluationContext instance keeps the ownership of the returned object. The
Alex Deymo41a75a72014-04-15 15:36:22 -070059 // returned object is valid during the life of the evaluation, even if the
60 // passed Variable changes it.
Alex Deymo23949d42014-02-05 15:20:59 -080061 //
62 // In case of error, a NULL value is returned.
63 template<typename T>
64 const T* GetValue(Variable<T>* var);
65
Alex Deymo41a75a72014-04-15 15:36:22 -070066 // Returns whether the passed |timestamp| is greater than the evaluation
67 // time. The |timestamp| value should be in the same scale as the values
68 // returned by ClockInterface::GetWallclockTime().
69 bool IsTimeGreaterThan(base::Time timestamp);
70
71 // TODO(deymo): Move the following methods to an interface only visible by the
72 // PolicyManager class and not the policy implementations.
73
74 // Resets the EvaluationContext to its initial state removing all the
75 // non-const cached variables and re-setting the evaluation time. This should
76 // be called right before any new evaluation starts.
77 void ResetEvaluation();
78
Alex Deymo53556ec2014-03-17 10:05:57 -070079 // Schedules the passed |callback| closure to be called when a cached
80 // variable changes its value or a polling interval passes. If none of these
81 // events can happen, for example if there's no cached variable, this method
82 // returns false.
83 //
84 // Right before the passed closure is called the EvaluationContext is
85 // reseted, removing all the non-const cached values.
86 bool RunOnValueChangeOrTimeout(base::Closure callback);
87
David Zeuthenc1490282014-04-29 16:25:03 -070088 // Returns a textual representation of the evaluation context,
89 // including the variables and their values. This is intended only
90 // to help with debugging and the format may change in the future.
91 std::string DumpContext() const;
92
Alex Deymo23949d42014-02-05 15:20:59 -080093 private:
Alex Deymo53556ec2014-03-17 10:05:57 -070094 // Removes all the Observers and timeout callbacks scheduled by
95 // RunOnValueChangeOrTimeout(). This method is idempotent.
96 void RemoveObserversAndTimeout();
97
98 // BaseVariable::ObserverInterface override.
99 void ValueChanged(BaseVariable* var);
100
101 // Called from the main loop when the scheduled poll timeout has passed.
102 void OnPollTimeout();
103
104 // Removes the observers from the used Variables and cancels the poll timeout
105 // and executes the scheduled callback, if any.
106 void OnValueChangedOrPollTimeout();
107
Alex Deymo23949d42014-02-05 15:20:59 -0800108 // The remaining time for the current evaluation.
109 base::TimeDelta RemainingTime() const;
110
111 // A map to hold the cached values for every variable.
112 typedef std::map<BaseVariable*, BoxedValue> ValueCacheMap;
113
114 // The cached values of the called Variables.
115 ValueCacheMap value_cache_;
116
Alex Deymo53556ec2014-03-17 10:05:57 -0700117 // A pointer to a copy of the closure passed to RunOnValueChangeOrTimeout().
118 scoped_ptr<base::Closure> value_changed_callback_;
119
120 // The EventId returned by the event loop identifying the timeout callback.
121 // Used to cancel the timeout callback.
122 EventId poll_timeout_event_ = kEventIdNull;
123
Alex Deymo41a75a72014-04-15 15:36:22 -0700124 // Pointer to the mockable clock interface;
125 chromeos_update_engine::ClockInterface* clock_;
126
127 // The timestamp when the evaluation of this EvaluationContext started. This
128 // value is reset every time ResetEvaluation() is called. The time source
129 // used is the ClockInterface::GetWallclockTime().
130 base::Time evaluation_start_;
131
132 // The timestamp measured on the GetWallclockTime() scale, when a reevaluation
133 // should be triggered due to IsTimeGreaterThan() calls value changes. This
134 // timestamp is greater or equal to |evaluation_start_| since it is a
135 // timestamp in the future, but it can be lower than the current
136 // GetWallclockTime() at some point of the evaluation.
137 base::Time reevaluation_time_;
138
139 // The timeout of an evaluation, used to compute the RemainingTime() of an
140 // evaluation.
141 // TODO(deymo): Receive the timeout from the PolicyManager. crbug.com/363790
142 base::TimeDelta evaluation_timeout_ = base::TimeDelta::FromSeconds(5);
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 Deymodb799532014-03-21 13:00:00 -0700149 base::WeakPtrFactory<EvaluationContext> weak_ptr_factory_;
150
Alex Deymo23949d42014-02-05 15:20:59 -0800151 DISALLOW_COPY_AND_ASSIGN(EvaluationContext);
152};
153
154} // namespace chromeos_policy_manager
155
156// Include the implementation of the template methods.
157#include "update_engine/policy_manager/evaluation_context-inl.h"
158
Gilad Arnold2cbb3852014-03-07 12:40:50 -0800159#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_EVALUATION_CONTEXT_H_