blob: dc7e53fb5a1de72f2e0be636c491469ba04b96eb [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 Deymodb799532014-03-21 13:00:00 -070012#include <base/memory/weak_ptr.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070013#include <base/time/time.h>
Alex Deymo7b948f02014-03-10 17:01:10 -070014
Alex Deymo23949d42014-02-05 15:20:59 -080015#include "update_engine/policy_manager/boxed_value.h"
Alex Deymo53556ec2014-03-17 10:05:57 -070016#include "update_engine/policy_manager/event_loop.h"
17#include "update_engine/policy_manager/variable.h"
Alex Deymo23949d42014-02-05 15:20:59 -080018
19namespace 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 Deymo53556ec2014-03-17 10:05:57 -070024class EvaluationContext :
25 public base::RefCounted<EvaluationContext>,
26 private BaseVariable::ObserverInterface {
Alex Deymo23949d42014-02-05 15:20:59 -080027 public:
Alex Deymodb799532014-03-21 13:00:00 -070028 EvaluationContext() : weak_ptr_factory_(this) {}
Alex Deymo53556ec2014-03-17 10:05:57 -070029 ~EvaluationContext();
Alex Deymo23949d42014-02-05 15:20:59 -080030
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 Deymo53556ec2014-03-17 10:05:57 -070040 // 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 Deymo23949d42014-02-05 15:20:59 -080049 private:
Alex Deymo53556ec2014-03-17 10:05:57 -070050 // 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 Deymo23949d42014-02-05 15:20:59 -080064 // 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 Deymo53556ec2014-03-17 10:05:57 -070073 // 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 Deymodb799532014-03-21 13:00:00 -070080 base::WeakPtrFactory<EvaluationContext> weak_ptr_factory_;
81
Alex Deymo23949d42014-02-05 15:20:59 -080082 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 Arnold2cbb3852014-03-07 12:40:50 -080090#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_EVALUATION_CONTEXT_H_