Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 1 | // 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 Arnold | 2cbb385 | 2014-03-07 12:40:50 -0800 | [diff] [blame] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_H_ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_H_ |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 7 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 8 | #include <base/callback.h> |
| 9 | #include <base/memory/ref_counted.h> |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 10 | #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 Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 14 | #include "update_engine/policy_manager/state.h" |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 15 | |
| 16 | namespace chromeos_policy_manager { |
| 17 | |
| 18 | // The main Policy Manager singleton class. |
| 19 | class PolicyManager { |
| 20 | public: |
| 21 | PolicyManager() {} |
| 22 | |
Gilad Arnold | 308c101 | 2014-03-12 15:37:06 -0700 | [diff] [blame] | 23 | // 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 Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 27 | |
| 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 Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 36 | // 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 Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 41 | // |
| 42 | // An example call to this method is: |
| 43 | // pm.PolicyRequest(&Policy::SomePolicyMethod, &bool_result, arg1, arg2); |
Alex Deymo | e75e025 | 2014-04-08 14:00:11 -0700 | [diff] [blame^] | 44 | template<typename R, typename... Args> |
| 45 | EvalStatus PolicyRequest( |
| 46 | EvalStatus (Policy::*policy_method)(EvaluationContext* ec, |
| 47 | State* state, |
| 48 | std::string* error, |
| 49 | R* result, |
| 50 | Args... args) const, |
| 51 | R* result, Args... args); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 52 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 53 | // Evaluates the given |policy_method| policy with the provided |args| |
| 54 | // arguments and calls the |callback| callback with the result when done. |
| 55 | // |
| 56 | // If the policy implementation should block, returning a |
| 57 | // EvalStatus::kAskMeAgainLater status the policy manager will re-evaluate the |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 58 | // policy until another status is returned. If the policy implementation based |
| 59 | // its return value solely on const variables, the callback will be called |
| 60 | // with the EvalStatus::kAskMeAgainLater status. |
Alex Deymo | e75e025 | 2014-04-08 14:00:11 -0700 | [diff] [blame^] | 61 | template<typename R, typename... Args> |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 62 | void AsyncPolicyRequest( |
| 63 | base::Callback<void(EvalStatus, const R& result)> callback, |
Alex Deymo | e75e025 | 2014-04-08 14:00:11 -0700 | [diff] [blame^] | 64 | EvalStatus (Policy::*policy_method)(EvaluationContext* ec, |
| 65 | State* state, |
| 66 | std::string* error, |
| 67 | R* result, |
| 68 | Args... args) const, |
| 69 | Args... args); |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 70 | |
Alex Deymo | 94c0616 | 2014-03-21 20:34:46 -0700 | [diff] [blame] | 71 | protected: |
| 72 | // The PolicyManager receives ownership of the passed Policy instance. |
| 73 | void set_policy(const Policy* policy) { |
| 74 | policy_.reset(policy); |
| 75 | } |
| 76 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 77 | private: |
| 78 | friend class PmPolicyManagerTest; |
| 79 | FRIEND_TEST(PmPolicyManagerTest, PolicyRequestCallsPolicy); |
| 80 | FRIEND_TEST(PmPolicyManagerTest, PolicyRequestCallsDefaultOnError); |
| 81 | FRIEND_TEST(PmPolicyManagerTest, PolicyRequestDoesntBlock); |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 82 | FRIEND_TEST(PmPolicyManagerTest, AsyncPolicyRequestDelaysEvaluation); |
| 83 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 84 | // EvaluatePolicy() evaluates the passed |policy_method| method on the current |
| 85 | // policy with the given |args| arguments. If the method fails, the default |
| 86 | // policy is used instead. |
Alex Deymo | e75e025 | 2014-04-08 14:00:11 -0700 | [diff] [blame^] | 87 | template<typename R, typename... Args> |
| 88 | EvalStatus EvaluatePolicy( |
| 89 | EvaluationContext* ec, |
| 90 | EvalStatus (Policy::*policy_method)(EvaluationContext* ec, |
| 91 | State* state, |
| 92 | std::string* error, |
| 93 | R* result, |
| 94 | Args... args) const, |
| 95 | R* result, Args... args); |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 96 | |
| 97 | // OnPolicyReadyToEvaluate() is called by the main loop when the evaluation |
| 98 | // of the given |policy_method| should be executed. If the evaluation finishes |
| 99 | // the |callback| callback is called passing the |result| and the |status| |
| 100 | // returned by the policy. If the evaluation returns an |
| 101 | // EvalStatus::kAskMeAgainLater state, the |callback| will NOT be called and |
| 102 | // the evaluation will be re-scheduled to be called later. |
Alex Deymo | e75e025 | 2014-04-08 14:00:11 -0700 | [diff] [blame^] | 103 | template<typename R, typename... Args> |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 104 | void OnPolicyReadyToEvaluate( |
| 105 | scoped_refptr<EvaluationContext> ec, |
| 106 | base::Callback<void(EvalStatus status, const R& result)> callback, |
Alex Deymo | e75e025 | 2014-04-08 14:00:11 -0700 | [diff] [blame^] | 107 | EvalStatus (Policy::*policy_method)(EvaluationContext* ec, |
| 108 | State* state, |
| 109 | std::string* error, |
| 110 | R* result, |
| 111 | Args... args) const, |
| 112 | Args... args); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 113 | |
| 114 | // The policy used by the PolicyManager. Note that since it is a const Policy, |
| 115 | // policy implementations are not allowed to persist state on this class. |
| 116 | scoped_ptr<const Policy> policy_; |
| 117 | |
| 118 | // A safe default value to the current policy. This policy is used whenever |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 119 | // a policy implementation fails with EvalStatus::kFailed. |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 120 | const DefaultPolicy default_policy_; |
| 121 | |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 122 | // State Providers. |
| 123 | scoped_ptr<State> state_; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 124 | |
| 125 | DISALLOW_COPY_AND_ASSIGN(PolicyManager); |
| 126 | }; |
| 127 | |
| 128 | } // namespace chromeos_policy_manager |
| 129 | |
| 130 | // Include the implementation of the template methods. |
| 131 | #include "update_engine/policy_manager/policy_manager-inl.h" |
| 132 | |
Gilad Arnold | 2cbb385 | 2014-03-07 12:40:50 -0800 | [diff] [blame] | 133 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_H_ |