blob: 99ca26b1e85c90befbf12a136544faec3034a95a [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 Deymo680d0222014-04-24 21:00:08 -070022 // Creates the PolicyManager instance, assuming ownership on the provided
23 // |state|.
24 PolicyManager(chromeos_update_engine::ClockInterface* clock,
25 State* state);
Alex Deymoc705cc82014-02-19 11:15:00 -080026
Alex Deymo680d0222014-04-24 21:00:08 -070027 virtual ~PolicyManager() {}
Alex Deymoc705cc82014-02-19 11:15:00 -080028
29 // PolicyRequest() evaluates the given policy with the provided arguments and
30 // returns the result. The |policy_method| is the pointer-to-method of the
31 // Policy class for the policy request to call. The PolicyManager will call
32 // this method on the right policy. The pointer |result| must not be NULL and
33 // the remaining |args| depend on the arguments required by the passed
34 // |policy_method|.
35 //
36 // When the policy request succeeds, the |result| is set and the method
Alex Deymoe636c3c2014-03-11 19:02:08 -070037 // returns EvalStatus::kSucceeded, otherwise, the |result| may not be set.
38 // Also, if the policy implementation should block, this method returns
39 // immediately with EvalStatus::kAskMeAgainLater. In case of failure
40 // EvalStatus::kFailed is returned and the |error| message is set, which must
41 // not be NULL.
Alex Deymoc705cc82014-02-19 11:15:00 -080042 //
43 // An example call to this method is:
44 // pm.PolicyRequest(&Policy::SomePolicyMethod, &bool_result, arg1, arg2);
Alex Deymoe75e0252014-04-08 14:00:11 -070045 template<typename R, typename... Args>
46 EvalStatus PolicyRequest(
47 EvalStatus (Policy::*policy_method)(EvaluationContext* ec,
48 State* state,
49 std::string* error,
50 R* result,
51 Args... args) const,
52 R* result, Args... args);
Alex Deymoc705cc82014-02-19 11:15:00 -080053
Alex Deymo7b948f02014-03-10 17:01:10 -070054 // Evaluates the given |policy_method| policy with the provided |args|
55 // arguments and calls the |callback| callback with the result when done.
56 //
57 // If the policy implementation should block, returning a
58 // EvalStatus::kAskMeAgainLater status the policy manager will re-evaluate the
Alex Deymo53556ec2014-03-17 10:05:57 -070059 // policy until another status is returned. If the policy implementation based
60 // its return value solely on const variables, the callback will be called
61 // with the EvalStatus::kAskMeAgainLater status.
Alex Deymoe75e0252014-04-08 14:00:11 -070062 template<typename R, typename... Args>
Alex Deymo7b948f02014-03-10 17:01:10 -070063 void AsyncPolicyRequest(
64 base::Callback<void(EvalStatus, const R& result)> callback,
Alex Deymoe75e0252014-04-08 14:00:11 -070065 EvalStatus (Policy::*policy_method)(EvaluationContext* ec,
66 State* state,
67 std::string* error,
68 R* result,
69 Args... args) const,
70 Args... args);
Alex Deymo7b948f02014-03-10 17:01:10 -070071
Alex Deymo94c06162014-03-21 20:34:46 -070072 protected:
73 // The PolicyManager receives ownership of the passed Policy instance.
74 void set_policy(const Policy* policy) {
75 policy_.reset(policy);
76 }
77
Alex Deymo680d0222014-04-24 21:00:08 -070078 // State getter used for testing.
79 State* state() { return state_.get(); }
80
Alex Deymoc705cc82014-02-19 11:15:00 -080081 private:
Alex Deymoc705cc82014-02-19 11:15:00 -080082 FRIEND_TEST(PmPolicyManagerTest, PolicyRequestCallsPolicy);
83 FRIEND_TEST(PmPolicyManagerTest, PolicyRequestCallsDefaultOnError);
84 FRIEND_TEST(PmPolicyManagerTest, PolicyRequestDoesntBlock);
Alex Deymo7b948f02014-03-10 17:01:10 -070085 FRIEND_TEST(PmPolicyManagerTest, AsyncPolicyRequestDelaysEvaluation);
86
Alex Deymo7b948f02014-03-10 17:01:10 -070087 // EvaluatePolicy() evaluates the passed |policy_method| method on the current
88 // policy with the given |args| arguments. If the method fails, the default
89 // policy is used instead.
Alex Deymoe75e0252014-04-08 14:00:11 -070090 template<typename R, typename... Args>
91 EvalStatus EvaluatePolicy(
92 EvaluationContext* ec,
93 EvalStatus (Policy::*policy_method)(EvaluationContext* ec,
94 State* state,
95 std::string* error,
96 R* result,
97 Args... args) const,
98 R* result, Args... args);
Alex Deymo7b948f02014-03-10 17:01:10 -070099
100 // OnPolicyReadyToEvaluate() is called by the main loop when the evaluation
101 // of the given |policy_method| should be executed. If the evaluation finishes
102 // the |callback| callback is called passing the |result| and the |status|
103 // returned by the policy. If the evaluation returns an
104 // EvalStatus::kAskMeAgainLater state, the |callback| will NOT be called and
105 // the evaluation will be re-scheduled to be called later.
Alex Deymoe75e0252014-04-08 14:00:11 -0700106 template<typename R, typename... Args>
Alex Deymo7b948f02014-03-10 17:01:10 -0700107 void OnPolicyReadyToEvaluate(
108 scoped_refptr<EvaluationContext> ec,
109 base::Callback<void(EvalStatus status, const R& result)> callback,
Alex Deymoe75e0252014-04-08 14:00:11 -0700110 EvalStatus (Policy::*policy_method)(EvaluationContext* ec,
111 State* state,
112 std::string* error,
113 R* result,
114 Args... args) const,
115 Args... args);
Alex Deymoc705cc82014-02-19 11:15:00 -0800116
117 // The policy used by the PolicyManager. Note that since it is a const Policy,
118 // policy implementations are not allowed to persist state on this class.
119 scoped_ptr<const Policy> policy_;
120
121 // A safe default value to the current policy. This policy is used whenever
Alex Deymoe636c3c2014-03-11 19:02:08 -0700122 // a policy implementation fails with EvalStatus::kFailed.
Alex Deymoc705cc82014-02-19 11:15:00 -0800123 const DefaultPolicy default_policy_;
124
Alex Deymo2de23f52014-02-26 14:30:13 -0800125 // State Providers.
126 scoped_ptr<State> state_;
Alex Deymoc705cc82014-02-19 11:15:00 -0800127
Alex Deymo41a75a72014-04-15 15:36:22 -0700128 // Pointer to the mockable clock interface;
129 chromeos_update_engine::ClockInterface* clock_;
130
Alex Deymoc705cc82014-02-19 11:15:00 -0800131 DISALLOW_COPY_AND_ASSIGN(PolicyManager);
132};
133
134} // namespace chromeos_policy_manager
135
136// Include the implementation of the template methods.
137#include "update_engine/policy_manager/policy_manager-inl.h"
138
Gilad Arnold2cbb3852014-03-07 12:40:50 -0800139#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_H_