blob: 5b89bf2c3b11ea7041ba1e10ca96f6757e287704 [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
12#include "update_engine/policy_manager/default_policy.h"
13#include "update_engine/policy_manager/policy.h"
Alex Deymo2de23f52014-02-26 14:30:13 -080014#include "update_engine/policy_manager/state.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080015
16namespace chromeos_policy_manager {
17
18// The main Policy Manager singleton class.
19class PolicyManager {
20 public:
21 PolicyManager() {}
22
Gilad Arnold308c1012014-03-12 15:37:06 -070023 // Initializes the PolicyManager instance, assuming ownership on the provided
24 // |state|, which is assumed to be pre-initialized. Returns whether the
25 // initialization succeeded.
26 bool Init(State* state);
Alex Deymoc705cc82014-02-19 11:15:00 -080027
28 // PolicyRequest() evaluates the given policy with the provided arguments and
29 // returns the result. The |policy_method| is the pointer-to-method of the
30 // Policy class for the policy request to call. The PolicyManager will call
31 // this method on the right policy. The pointer |result| must not be NULL and
32 // the remaining |args| depend on the arguments required by the passed
33 // |policy_method|.
34 //
35 // When the policy request succeeds, the |result| is set and the method
Alex Deymoe636c3c2014-03-11 19:02:08 -070036 // returns EvalStatus::kSucceeded, otherwise, the |result| may not be set.
37 // Also, if the policy implementation should block, this method returns
38 // immediately with EvalStatus::kAskMeAgainLater. In case of failure
39 // EvalStatus::kFailed is returned and the |error| message is set, which must
40 // not be NULL.
Alex Deymoc705cc82014-02-19 11:15:00 -080041 //
42 // An example call to this method is:
43 // pm.PolicyRequest(&Policy::SomePolicyMethod, &bool_result, arg1, arg2);
44 template<typename T, typename R, typename... Args>
Alex Deymo2de23f52014-02-26 14:30:13 -080045 EvalStatus PolicyRequest(T policy_method, R* result, Args... args);
Alex Deymoc705cc82014-02-19 11:15:00 -080046
Alex Deymo7b948f02014-03-10 17:01:10 -070047 // Evaluates the given |policy_method| policy with the provided |args|
48 // arguments and calls the |callback| callback with the result when done.
49 //
50 // If the policy implementation should block, returning a
51 // EvalStatus::kAskMeAgainLater status the policy manager will re-evaluate the
Alex Deymo53556ec2014-03-17 10:05:57 -070052 // policy until another status is returned. If the policy implementation based
53 // its return value solely on const variables, the callback will be called
54 // with the EvalStatus::kAskMeAgainLater status.
Alex Deymo7b948f02014-03-10 17:01:10 -070055 template<typename T, typename R, typename... Args>
56 void AsyncPolicyRequest(
57 base::Callback<void(EvalStatus, const R& result)> callback,
58 T policy_method, Args... args);
59
Alex Deymo94c06162014-03-21 20:34:46 -070060 protected:
61 // The PolicyManager receives ownership of the passed Policy instance.
62 void set_policy(const Policy* policy) {
63 policy_.reset(policy);
64 }
65
Alex Deymoc705cc82014-02-19 11:15:00 -080066 private:
67 friend class PmPolicyManagerTest;
68 FRIEND_TEST(PmPolicyManagerTest, PolicyRequestCallsPolicy);
69 FRIEND_TEST(PmPolicyManagerTest, PolicyRequestCallsDefaultOnError);
70 FRIEND_TEST(PmPolicyManagerTest, PolicyRequestDoesntBlock);
Alex Deymo7b948f02014-03-10 17:01:10 -070071 FRIEND_TEST(PmPolicyManagerTest, AsyncPolicyRequestDelaysEvaluation);
72
Alex Deymo7b948f02014-03-10 17:01:10 -070073 // EvaluatePolicy() evaluates the passed |policy_method| method on the current
74 // policy with the given |args| arguments. If the method fails, the default
75 // policy is used instead.
76 template<typename T, typename R, typename... Args>
77 EvalStatus EvaluatePolicy(EvaluationContext* ec,
78 T policy_method, R* result,
79 Args... args);
80
81 // OnPolicyReadyToEvaluate() is called by the main loop when the evaluation
82 // of the given |policy_method| should be executed. If the evaluation finishes
83 // the |callback| callback is called passing the |result| and the |status|
84 // returned by the policy. If the evaluation returns an
85 // EvalStatus::kAskMeAgainLater state, the |callback| will NOT be called and
86 // the evaluation will be re-scheduled to be called later.
87 template<typename T, typename R, typename... Args>
88 void OnPolicyReadyToEvaluate(
89 scoped_refptr<EvaluationContext> ec,
90 base::Callback<void(EvalStatus status, const R& result)> callback,
91 T policy_method, Args... args);
Alex Deymoc705cc82014-02-19 11:15:00 -080092
93 // The policy used by the PolicyManager. Note that since it is a const Policy,
94 // policy implementations are not allowed to persist state on this class.
95 scoped_ptr<const Policy> policy_;
96
97 // A safe default value to the current policy. This policy is used whenever
Alex Deymoe636c3c2014-03-11 19:02:08 -070098 // a policy implementation fails with EvalStatus::kFailed.
Alex Deymoc705cc82014-02-19 11:15:00 -080099 const DefaultPolicy default_policy_;
100
Alex Deymo2de23f52014-02-26 14:30:13 -0800101 // State Providers.
102 scoped_ptr<State> state_;
Alex Deymoc705cc82014-02-19 11:15:00 -0800103
104 DISALLOW_COPY_AND_ASSIGN(PolicyManager);
105};
106
107} // namespace chromeos_policy_manager
108
109// Include the implementation of the template methods.
110#include "update_engine/policy_manager/policy_manager-inl.h"
111
Gilad Arnold2cbb3852014-03-07 12:40:50 -0800112#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_H_