blob: 3be843712317d17a9ea865aad74409acf92c067f [file] [log] [blame]
Alex Deymoc705cc82014-02-19 11:15:00 -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_POLICY_MANAGER_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_H_
Alex Deymoc705cc82014-02-19 11:15:00 -08007
Alex Deymo7b948f02014-03-10 17:01:10 -07008#include <base/callback.h>
9#include <base/memory/ref_counted.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080010#include <base/memory/scoped_ptr.h>
11
Alex Deymo41a75a72014-04-15 15:36:22 -070012#include "update_engine/clock_interface.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080013#include "update_engine/policy_manager/default_policy.h"
14#include "update_engine/policy_manager/policy.h"
Alex Deymo2de23f52014-02-26 14:30:13 -080015#include "update_engine/policy_manager/state.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080016
17namespace chromeos_policy_manager {
18
19// The main Policy Manager singleton class.
20class PolicyManager {
21 public:
Alex Deymo41a75a72014-04-15 15:36:22 -070022 explicit PolicyManager(chromeos_update_engine::ClockInterface* clock)
23 : clock_(clock) {}
Alex Deymoc705cc82014-02-19 11:15:00 -080024
Gilad Arnold308c1012014-03-12 15:37:06 -070025 // Initializes the PolicyManager instance, assuming ownership on the provided
26 // |state|, which is assumed to be pre-initialized. Returns whether the
27 // initialization succeeded.
28 bool Init(State* state);
Alex Deymoc705cc82014-02-19 11:15:00 -080029
30 // PolicyRequest() evaluates the given policy with the provided arguments and
31 // returns the result. The |policy_method| is the pointer-to-method of the
32 // Policy class for the policy request to call. The PolicyManager will call
33 // this method on the right policy. The pointer |result| must not be NULL and
34 // the remaining |args| depend on the arguments required by the passed
35 // |policy_method|.
36 //
37 // When the policy request succeeds, the |result| is set and the method
Alex Deymoe636c3c2014-03-11 19:02:08 -070038 // returns EvalStatus::kSucceeded, otherwise, the |result| may not be set.
39 // Also, if the policy implementation should block, this method returns
40 // immediately with EvalStatus::kAskMeAgainLater. In case of failure
41 // EvalStatus::kFailed is returned and the |error| message is set, which must
42 // not be NULL.
Alex Deymoc705cc82014-02-19 11:15:00 -080043 //
44 // An example call to this method is:
45 // pm.PolicyRequest(&Policy::SomePolicyMethod, &bool_result, arg1, arg2);
Alex Deymoe75e0252014-04-08 14:00:11 -070046 template<typename R, typename... Args>
47 EvalStatus PolicyRequest(
48 EvalStatus (Policy::*policy_method)(EvaluationContext* ec,
49 State* state,
50 std::string* error,
51 R* result,
52 Args... args) const,
53 R* result, Args... args);
Alex Deymoc705cc82014-02-19 11:15:00 -080054
Alex Deymo7b948f02014-03-10 17:01:10 -070055 // Evaluates the given |policy_method| policy with the provided |args|
56 // arguments and calls the |callback| callback with the result when done.
57 //
58 // If the policy implementation should block, returning a
59 // EvalStatus::kAskMeAgainLater status the policy manager will re-evaluate the
Alex Deymo53556ec2014-03-17 10:05:57 -070060 // policy until another status is returned. If the policy implementation based
61 // its return value solely on const variables, the callback will be called
62 // with the EvalStatus::kAskMeAgainLater status.
Alex Deymoe75e0252014-04-08 14:00:11 -070063 template<typename R, typename... Args>
Alex Deymo7b948f02014-03-10 17:01:10 -070064 void AsyncPolicyRequest(
65 base::Callback<void(EvalStatus, const R& result)> callback,
Alex Deymoe75e0252014-04-08 14:00:11 -070066 EvalStatus (Policy::*policy_method)(EvaluationContext* ec,
67 State* state,
68 std::string* error,
69 R* result,
70 Args... args) const,
71 Args... args);
Alex Deymo7b948f02014-03-10 17:01:10 -070072
Alex Deymo94c06162014-03-21 20:34:46 -070073 protected:
74 // The PolicyManager receives ownership of the passed Policy instance.
75 void set_policy(const Policy* policy) {
76 policy_.reset(policy);
77 }
78
Alex Deymoc705cc82014-02-19 11:15:00 -080079 private:
80 friend class PmPolicyManagerTest;
81 FRIEND_TEST(PmPolicyManagerTest, PolicyRequestCallsPolicy);
82 FRIEND_TEST(PmPolicyManagerTest, PolicyRequestCallsDefaultOnError);
83 FRIEND_TEST(PmPolicyManagerTest, PolicyRequestDoesntBlock);
Alex Deymo7b948f02014-03-10 17:01:10 -070084 FRIEND_TEST(PmPolicyManagerTest, AsyncPolicyRequestDelaysEvaluation);
85
Alex Deymo7b948f02014-03-10 17:01:10 -070086 // EvaluatePolicy() evaluates the passed |policy_method| method on the current
87 // policy with the given |args| arguments. If the method fails, the default
88 // policy is used instead.
Alex Deymoe75e0252014-04-08 14:00:11 -070089 template<typename R, typename... Args>
90 EvalStatus EvaluatePolicy(
91 EvaluationContext* ec,
92 EvalStatus (Policy::*policy_method)(EvaluationContext* ec,
93 State* state,
94 std::string* error,
95 R* result,
96 Args... args) const,
97 R* result, Args... args);
Alex Deymo7b948f02014-03-10 17:01:10 -070098
99 // OnPolicyReadyToEvaluate() is called by the main loop when the evaluation
100 // of the given |policy_method| should be executed. If the evaluation finishes
101 // the |callback| callback is called passing the |result| and the |status|
102 // returned by the policy. If the evaluation returns an
103 // EvalStatus::kAskMeAgainLater state, the |callback| will NOT be called and
104 // the evaluation will be re-scheduled to be called later.
Alex Deymoe75e0252014-04-08 14:00:11 -0700105 template<typename R, typename... Args>
Alex Deymo7b948f02014-03-10 17:01:10 -0700106 void OnPolicyReadyToEvaluate(
107 scoped_refptr<EvaluationContext> ec,
108 base::Callback<void(EvalStatus status, const R& result)> callback,
Alex Deymoe75e0252014-04-08 14:00:11 -0700109 EvalStatus (Policy::*policy_method)(EvaluationContext* ec,
110 State* state,
111 std::string* error,
112 R* result,
113 Args... args) const,
114 Args... args);
Alex Deymoc705cc82014-02-19 11:15:00 -0800115
116 // The policy used by the PolicyManager. Note that since it is a const Policy,
117 // policy implementations are not allowed to persist state on this class.
118 scoped_ptr<const Policy> policy_;
119
120 // A safe default value to the current policy. This policy is used whenever
Alex Deymoe636c3c2014-03-11 19:02:08 -0700121 // a policy implementation fails with EvalStatus::kFailed.
Alex Deymoc705cc82014-02-19 11:15:00 -0800122 const DefaultPolicy default_policy_;
123
Alex Deymo2de23f52014-02-26 14:30:13 -0800124 // State Providers.
125 scoped_ptr<State> state_;
Alex Deymoc705cc82014-02-19 11:15:00 -0800126
Alex Deymo41a75a72014-04-15 15:36:22 -0700127 // Pointer to the mockable clock interface;
128 chromeos_update_engine::ClockInterface* clock_;
129
Alex Deymoc705cc82014-02-19 11:15:00 -0800130 DISALLOW_COPY_AND_ASSIGN(PolicyManager);
131};
132
133} // namespace chromeos_policy_manager
134
135// Include the implementation of the template methods.
136#include "update_engine/policy_manager/policy_manager-inl.h"
137
Gilad Arnold2cbb3852014-03-07 12:40:50 -0800138#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_H_