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 <glib.h> |
| 9 | |
| 10 | #include <base/callback.h> |
| 11 | #include <base/memory/ref_counted.h> |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 12 | #include <base/memory/scoped_ptr.h> |
| 13 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 14 | #include "update_engine/clock_interface.h" |
| 15 | #include "update_engine/dbus_wrapper_interface.h" |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 16 | #include "update_engine/policy_manager/default_policy.h" |
| 17 | #include "update_engine/policy_manager/policy.h" |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 18 | #include "update_engine/policy_manager/state.h" |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 19 | |
| 20 | namespace chromeos_policy_manager { |
| 21 | |
| 22 | // The main Policy Manager singleton class. |
| 23 | class PolicyManager { |
| 24 | public: |
| 25 | PolicyManager() {} |
| 26 | |
| 27 | // Initializes the PolicyManager instance. Returns whether the initialization |
| 28 | // succeeded. |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 29 | bool Init(chromeos_update_engine::DBusWrapperInterface* dbus, |
| 30 | chromeos_update_engine::ClockInterface* clock); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 31 | |
| 32 | // PolicyRequest() evaluates the given policy with the provided arguments and |
| 33 | // returns the result. The |policy_method| is the pointer-to-method of the |
| 34 | // Policy class for the policy request to call. The PolicyManager will call |
| 35 | // this method on the right policy. The pointer |result| must not be NULL and |
| 36 | // the remaining |args| depend on the arguments required by the passed |
| 37 | // |policy_method|. |
| 38 | // |
| 39 | // When the policy request succeeds, the |result| is set and the method |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 40 | // returns EvalStatus::kSucceeded, otherwise, the |result| may not be set. |
| 41 | // Also, if the policy implementation should block, this method returns |
| 42 | // immediately with EvalStatus::kAskMeAgainLater. In case of failure |
| 43 | // EvalStatus::kFailed is returned and the |error| message is set, which must |
| 44 | // not be NULL. |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 45 | // |
| 46 | // An example call to this method is: |
| 47 | // pm.PolicyRequest(&Policy::SomePolicyMethod, &bool_result, arg1, arg2); |
| 48 | template<typename T, typename R, typename... Args> |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 49 | EvalStatus PolicyRequest(T policy_method, R* result, Args... args); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 50 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 51 | // Evaluates the given |policy_method| policy with the provided |args| |
| 52 | // arguments and calls the |callback| callback with the result when done. |
| 53 | // |
| 54 | // If the policy implementation should block, returning a |
| 55 | // EvalStatus::kAskMeAgainLater status the policy manager will re-evaluate the |
| 56 | // policy until another status is returned. |
| 57 | template<typename T, typename R, typename... Args> |
| 58 | void AsyncPolicyRequest( |
| 59 | base::Callback<void(EvalStatus, const R& result)> callback, |
| 60 | T policy_method, Args... args); |
| 61 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 62 | private: |
| 63 | friend class PmPolicyManagerTest; |
| 64 | FRIEND_TEST(PmPolicyManagerTest, PolicyRequestCallsPolicy); |
| 65 | FRIEND_TEST(PmPolicyManagerTest, PolicyRequestCallsDefaultOnError); |
| 66 | FRIEND_TEST(PmPolicyManagerTest, PolicyRequestDoesntBlock); |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 67 | FRIEND_TEST(PmPolicyManagerTest, AsyncPolicyRequestDelaysEvaluation); |
| 68 | |
| 69 | // Schedules the passed |callback| to run from the GLib's main loop after a |
| 70 | // timeout if it is given. |
| 71 | static void RunFromMainLoop(const base::Closure& callback); |
| 72 | static void RunFromMainLoopAfterTimeout(const base::Closure& callback, |
| 73 | base::TimeDelta timeout); |
| 74 | |
| 75 | // Called by the GLib's main loop when is time to call the callback scheduled |
| 76 | // with RunFromMainLopp() and similar functions. The pointer to the callback |
| 77 | // passed when scheduling it is passed to this functions as a gpointer on |
| 78 | // |user_data|. |
| 79 | static gboolean OnRanFromMainLoop(gpointer user_data); |
| 80 | |
| 81 | // EvaluatePolicy() evaluates the passed |policy_method| method on the current |
| 82 | // policy with the given |args| arguments. If the method fails, the default |
| 83 | // policy is used instead. |
| 84 | template<typename T, typename R, typename... Args> |
| 85 | EvalStatus EvaluatePolicy(EvaluationContext* ec, |
| 86 | T policy_method, R* result, |
| 87 | Args... args); |
| 88 | |
| 89 | // OnPolicyReadyToEvaluate() is called by the main loop when the evaluation |
| 90 | // of the given |policy_method| should be executed. If the evaluation finishes |
| 91 | // the |callback| callback is called passing the |result| and the |status| |
| 92 | // returned by the policy. If the evaluation returns an |
| 93 | // EvalStatus::kAskMeAgainLater state, the |callback| will NOT be called and |
| 94 | // the evaluation will be re-scheduled to be called later. |
| 95 | template<typename T, typename R, typename... Args> |
| 96 | void OnPolicyReadyToEvaluate( |
| 97 | scoped_refptr<EvaluationContext> ec, |
| 98 | base::Callback<void(EvalStatus status, const R& result)> callback, |
| 99 | T policy_method, Args... args); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 100 | |
| 101 | // The policy used by the PolicyManager. Note that since it is a const Policy, |
| 102 | // policy implementations are not allowed to persist state on this class. |
| 103 | scoped_ptr<const Policy> policy_; |
| 104 | |
| 105 | // 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] | 106 | // a policy implementation fails with EvalStatus::kFailed. |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 107 | const DefaultPolicy default_policy_; |
| 108 | |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 109 | // State Providers. |
| 110 | scoped_ptr<State> state_; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 111 | |
| 112 | DISALLOW_COPY_AND_ASSIGN(PolicyManager); |
| 113 | }; |
| 114 | |
| 115 | } // namespace chromeos_policy_manager |
| 116 | |
| 117 | // Include the implementation of the template methods. |
| 118 | #include "update_engine/policy_manager/policy_manager-inl.h" |
| 119 | |
Gilad Arnold | 2cbb385 | 2014-03-07 12:40:50 -0800 | [diff] [blame^] | 120 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_H_ |