blob: 81574d49ecb20bef6c1a4b9719a3dba7f06571fa [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 Arnold48415f12014-06-27 07:10:58 -07005#ifndef UPDATE_ENGINE_UPDATE_MANAGER_EVALUATION_CONTEXT_H_
6#define UPDATE_ENGINE_UPDATE_MANAGER_EVALUATION_CONTEXT_H_
Alex Deymo23949d42014-02-05 15:20:59 -08007
8#include <map>
Gilad Arnold48415f12014-06-27 07:10:58 -07009#include <string>
Alex Deymo23949d42014-02-05 15:20:59 -080010
Alex Deymo53556ec2014-03-17 10:05:57 -070011#include <base/callback.h>
Alex Deymo7b948f02014-03-10 17:01:10 -070012#include <base/memory/ref_counted.h>
Alex Deymo1f012912014-04-24 19:08:04 -070013#include <base/memory/scoped_ptr.h>
Alex Deymodb799532014-03-21 13:00:00 -070014#include <base/memory/weak_ptr.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070015#include <base/time/time.h>
Alex Deymo7b948f02014-03-10 17:01:10 -070016
Alex Deymo41a75a72014-04-15 15:36:22 -070017#include "update_engine/clock_interface.h"
Alex Deymo63784a52014-05-28 10:46:14 -070018#include "update_engine/update_manager/boxed_value.h"
19#include "update_engine/update_manager/event_loop.h"
20#include "update_engine/update_manager/variable.h"
Alex Deymo23949d42014-02-05 15:20:59 -080021
Alex Deymo63784a52014-05-28 10:46:14 -070022namespace chromeos_update_manager {
Alex Deymo23949d42014-02-05 15:20:59 -080023
24// The EvaluationContext class is the interface between a policy implementation
25// and the state. The EvaluationContext tracks the variables used by a policy
26// request and caches the returned values, owning those cached values.
Alex Deymo41a75a72014-04-15 15:36:22 -070027// The same EvaluationContext should be re-used for all the evaluations of the
28// same policy request (an AsyncPolicyRequest might involve several
29// re-evaluations). Each evaluation of the EvaluationContext is run at a given
30// point in time, which is used as a reference for the evaluation timeout and
Gilad Arnolda65fced2014-07-23 09:01:31 -070031// the time based queries of the policy, such as
32// Is{Wallclock,Monotonic}TimeGreaterThan().
Alex Deymo41a75a72014-04-15 15:36:22 -070033//
34// Example:
35//
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070036// scoped_refptr<EvaluationContext> ec = new EvaluationContext(...);
Alex Deymo41a75a72014-04-15 15:36:22 -070037//
38// ...
39// // The following call to ResetEvaluation() is optional. Use it to reset the
40// // evaluation time if the EvaluationContext isn't used right after its
41// // construction.
42// ec->ResetEvaluation();
43// EvalStatus status = policy->SomeMethod(ec, state, &result, args...);
44//
45// ...
46// // Run a closure when any of the used async variables changes its value or
Alex Vakulenko072359c2014-07-18 11:41:07 -070047// // the timeout for re-query the values happens again.
Alex Deymo41a75a72014-04-15 15:36:22 -070048// ec->RunOnValueChangeOrTimeout(closure);
49// // If the provided |closure| wants to re-evaluate the policy, it should
50// // call ec->ResetEvaluation() to start a new evaluation.
51//
Gilad Arnold48415f12014-06-27 07:10:58 -070052class EvaluationContext : public base::RefCounted<EvaluationContext>,
53 private BaseVariable::ObserverInterface {
Alex Deymo23949d42014-02-05 15:20:59 -080054 public:
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070055 EvaluationContext(chromeos_update_engine::ClockInterface* clock,
56 base::TimeDelta evaluation_timeout,
57 base::TimeDelta expiration_timeout);
58 EvaluationContext(chromeos_update_engine::ClockInterface* clock,
59 base::TimeDelta evaluation_timeout)
60 : EvaluationContext(clock, evaluation_timeout, base::TimeDelta::Max()) {}
Alex Deymo53556ec2014-03-17 10:05:57 -070061 ~EvaluationContext();
Alex Deymo23949d42014-02-05 15:20:59 -080062
63 // Returns a pointer to the value returned by the passed variable |var|. The
64 // EvaluationContext instance keeps the ownership of the returned object. The
Alex Deymo41a75a72014-04-15 15:36:22 -070065 // returned object is valid during the life of the evaluation, even if the
66 // passed Variable changes it.
Alex Deymo23949d42014-02-05 15:20:59 -080067 //
68 // In case of error, a NULL value is returned.
69 template<typename T>
70 const T* GetValue(Variable<T>* var);
71
Gilad Arnolda65fced2014-07-23 09:01:31 -070072 // Returns whether the evaluation time has surpassed |timestamp|, on either
73 // the ClockInterface::GetWallclockTime() or
74 // ClockInterface::GetMonotonicTime() scales, respectively.
75 bool IsWallclockTimeGreaterThan(base::Time timestamp);
76 bool IsMonotonicTimeGreaterThan(base::Time timestamp);
Alex Deymo41a75a72014-04-15 15:36:22 -070077
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070078 // Returns whether the evaluation context has expired.
79 bool is_expired() const { return is_expired_; }
80
Alex Deymo41a75a72014-04-15 15:36:22 -070081 // TODO(deymo): Move the following methods to an interface only visible by the
Alex Deymo63784a52014-05-28 10:46:14 -070082 // UpdateManager class and not the policy implementations.
Alex Deymo41a75a72014-04-15 15:36:22 -070083
84 // Resets the EvaluationContext to its initial state removing all the
85 // non-const cached variables and re-setting the evaluation time. This should
86 // be called right before any new evaluation starts.
87 void ResetEvaluation();
88
Gilad Arnoldfd45a732014-08-07 15:53:46 -070089 // Clears the expiration status of the EvaluationContext and resets its
90 // expiration timeout based on |expiration_timeout_|. This should be called if
91 // expiration occurred, prior to re-evaluating the policy.
92 void ResetExpiration();
93
Alex Deymo53556ec2014-03-17 10:05:57 -070094 // Schedules the passed |callback| closure to be called when a cached
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070095 // variable changes its value, a polling interval passes, or the context
96 // expiration occurs. If none of these events can happen, for example if
97 // there's no cached variable, this method returns false.
Alex Deymo53556ec2014-03-17 10:05:57 -070098 //
99 // Right before the passed closure is called the EvaluationContext is
100 // reseted, removing all the non-const cached values.
101 bool RunOnValueChangeOrTimeout(base::Closure callback);
102
David Zeuthenc1490282014-04-29 16:25:03 -0700103 // Returns a textual representation of the evaluation context,
104 // including the variables and their values. This is intended only
105 // to help with debugging and the format may change in the future.
106 std::string DumpContext() const;
107
Alex Deymo23949d42014-02-05 15:20:59 -0800108 private:
Gilad Arnoldfb794f42014-07-01 15:36:31 -0700109 friend class UmEvaluationContextTest;
110
Alex Deymo53556ec2014-03-17 10:05:57 -0700111 // Removes all the Observers and timeout callbacks scheduled by
112 // RunOnValueChangeOrTimeout(). This method is idempotent.
113 void RemoveObserversAndTimeout();
114
115 // BaseVariable::ObserverInterface override.
116 void ValueChanged(BaseVariable* var);
117
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700118 // Called from the main loop when a scheduled timeout has passed.
119 void OnTimeout();
Alex Deymo53556ec2014-03-17 10:05:57 -0700120
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700121 // Removes the observers from the used Variables and cancels the timeout,
122 // then executes the scheduled callback.
123 void OnValueChangedOrTimeout();
Alex Deymo53556ec2014-03-17 10:05:57 -0700124
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700125 // If |monotonic_deadline| is not Time::Max(), returns the remaining time
126 // until it is reached, or zero if it has passed. Otherwise, returns
127 // TimeDelta::Max().
128 base::TimeDelta RemainingTime(base::Time monotonic_deadline) const;
129
130 // Returns a monotonic clock timestamp at which |timeout| will have elapsed
131 // since the current time.
132 base::Time MonotonicDeadline(base::TimeDelta timeout);
Alex Deymo23949d42014-02-05 15:20:59 -0800133
134 // A map to hold the cached values for every variable.
135 typedef std::map<BaseVariable*, BoxedValue> ValueCacheMap;
136
137 // The cached values of the called Variables.
138 ValueCacheMap value_cache_;
139
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700140 // A callback used for triggering re-evaluation upon a value change or poll
141 // timeout, or notifying about the evaluation context expiration. It is up to
Alex Vakulenko072359c2014-07-18 11:41:07 -0700142 // the caller to determine whether or not expiration occurred via
143 // is_expired().
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700144 scoped_ptr<base::Closure> callback_;
Alex Deymo53556ec2014-03-17 10:05:57 -0700145
146 // The EventId returned by the event loop identifying the timeout callback.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700147 // Used for canceling the timeout callback.
148 EventId timeout_event_ = kEventIdNull;
149
150 // Whether a timeout event firing marks the expiration of the evaluation
151 // context.
152 bool timeout_marks_expiration_;
153
154 // Whether the evaluation context has indeed expired.
155 bool is_expired_ = false;
Alex Deymo53556ec2014-03-17 10:05:57 -0700156
Alex Deymo41a75a72014-04-15 15:36:22 -0700157 // Pointer to the mockable clock interface;
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700158 chromeos_update_engine::ClockInterface* const clock_;
Alex Deymo41a75a72014-04-15 15:36:22 -0700159
Gilad Arnolda65fced2014-07-23 09:01:31 -0700160 // The timestamps when the evaluation of this EvaluationContext started,
161 // corresponding to ClockInterface::GetWallclockTime() and
162 // ClockInterface::GetMonotonicTime(), respectively. These values are reset
163 // every time ResetEvaluation() is called.
164 base::Time evaluation_start_wallclock_;
165 base::Time evaluation_start_monotonic_;
Alex Deymo41a75a72014-04-15 15:36:22 -0700166
Gilad Arnolda65fced2014-07-23 09:01:31 -0700167 // The timestamps when a reevaluation should be triggered due to various
168 // expected value changes, corresponding to ClockInterface::GetWallclockTime()
169 // and ClockInterface::GetMonotonicTIme(), respectively. These timestamps are
170 // greater or equal to corresponding |evaluation_start_{wallclock,monotonic}_|
171 // counterparts since they are in the future; however, they may be smaller
172 // than the current corresponding times during the course of evaluation.
173 base::Time reevaluation_time_wallclock_;
174 base::Time reevaluation_time_monotonic_;
Alex Deymo41a75a72014-04-15 15:36:22 -0700175
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700176 // The timeout of an evaluation.
Gilad Arnoldb2271992014-06-19 12:35:24 -0700177 const base::TimeDelta evaluation_timeout_;
Alex Deymo41a75a72014-04-15 15:36:22 -0700178
179 // The timestamp in the ClockInterface::GetMonotonicTime() scale at which the
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700180 // current evaluation should finish.
Alex Deymo41a75a72014-04-15 15:36:22 -0700181 base::Time evaluation_monotonic_deadline_;
182
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700183 // The expiration timeout of the evaluation context.
184 const base::TimeDelta expiration_timeout_;
185
186 // The monotonic clock deadline at which expiration occurs.
187 base::Time expiration_monotonic_deadline_;
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700188
Alex Deymodb799532014-03-21 13:00:00 -0700189 base::WeakPtrFactory<EvaluationContext> weak_ptr_factory_;
190
Alex Deymo23949d42014-02-05 15:20:59 -0800191 DISALLOW_COPY_AND_ASSIGN(EvaluationContext);
192};
193
Alex Deymo63784a52014-05-28 10:46:14 -0700194} // namespace chromeos_update_manager
Alex Deymo23949d42014-02-05 15:20:59 -0800195
196// Include the implementation of the template methods.
Alex Deymo63784a52014-05-28 10:46:14 -0700197#include "update_engine/update_manager/evaluation_context-inl.h"
Alex Deymo23949d42014-02-05 15:20:59 -0800198
Gilad Arnold48415f12014-06-27 07:10:58 -0700199#endif // UPDATE_ENGINE_UPDATE_MANAGER_EVALUATION_CONTEXT_H_