blob: 33bb89cc2ffb4be8c27db3ea58c64162dab9917d [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 <glib.h>
9
10#include <base/callback.h>
11#include <base/memory/ref_counted.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080012#include <base/memory/scoped_ptr.h>
13
14#include "update_engine/policy_manager/default_policy.h"
15#include "update_engine/policy_manager/policy.h"
Alex Deymo2de23f52014-02-26 14:30:13 -080016#include "update_engine/policy_manager/state.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080017
18namespace chromeos_policy_manager {
19
20// The main Policy Manager singleton class.
21class PolicyManager {
22 public:
23 PolicyManager() {}
24
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);
46 template<typename T, typename R, typename... Args>
Alex Deymo2de23f52014-02-26 14:30:13 -080047 EvalStatus PolicyRequest(T policy_method, R* result, Args... args);
Alex Deymoc705cc82014-02-19 11:15:00 -080048
Alex Deymo7b948f02014-03-10 17:01:10 -070049 // Evaluates the given |policy_method| policy with the provided |args|
50 // arguments and calls the |callback| callback with the result when done.
51 //
52 // If the policy implementation should block, returning a
53 // EvalStatus::kAskMeAgainLater status the policy manager will re-evaluate the
54 // policy until another status is returned.
55 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 Deymoc705cc82014-02-19 11:15:00 -080060 private:
61 friend class PmPolicyManagerTest;
62 FRIEND_TEST(PmPolicyManagerTest, PolicyRequestCallsPolicy);
63 FRIEND_TEST(PmPolicyManagerTest, PolicyRequestCallsDefaultOnError);
64 FRIEND_TEST(PmPolicyManagerTest, PolicyRequestDoesntBlock);
Alex Deymo7b948f02014-03-10 17:01:10 -070065 FRIEND_TEST(PmPolicyManagerTest, AsyncPolicyRequestDelaysEvaluation);
66
67 // Schedules the passed |callback| to run from the GLib's main loop after a
68 // timeout if it is given.
69 static void RunFromMainLoop(const base::Closure& callback);
70 static void RunFromMainLoopAfterTimeout(const base::Closure& callback,
71 base::TimeDelta timeout);
72
73 // Called by the GLib's main loop when is time to call the callback scheduled
74 // with RunFromMainLopp() and similar functions. The pointer to the callback
75 // passed when scheduling it is passed to this functions as a gpointer on
76 // |user_data|.
77 static gboolean OnRanFromMainLoop(gpointer user_data);
78
79 // EvaluatePolicy() evaluates the passed |policy_method| method on the current
80 // policy with the given |args| arguments. If the method fails, the default
81 // policy is used instead.
82 template<typename T, typename R, typename... Args>
83 EvalStatus EvaluatePolicy(EvaluationContext* ec,
84 T policy_method, R* result,
85 Args... args);
86
87 // OnPolicyReadyToEvaluate() is called by the main loop when the evaluation
88 // of the given |policy_method| should be executed. If the evaluation finishes
89 // the |callback| callback is called passing the |result| and the |status|
90 // returned by the policy. If the evaluation returns an
91 // EvalStatus::kAskMeAgainLater state, the |callback| will NOT be called and
92 // the evaluation will be re-scheduled to be called later.
93 template<typename T, typename R, typename... Args>
94 void OnPolicyReadyToEvaluate(
95 scoped_refptr<EvaluationContext> ec,
96 base::Callback<void(EvalStatus status, const R& result)> callback,
97 T policy_method, Args... args);
Alex Deymoc705cc82014-02-19 11:15:00 -080098
99 // The policy used by the PolicyManager. Note that since it is a const Policy,
100 // policy implementations are not allowed to persist state on this class.
101 scoped_ptr<const Policy> policy_;
102
103 // A safe default value to the current policy. This policy is used whenever
Alex Deymoe636c3c2014-03-11 19:02:08 -0700104 // a policy implementation fails with EvalStatus::kFailed.
Alex Deymoc705cc82014-02-19 11:15:00 -0800105 const DefaultPolicy default_policy_;
106
Alex Deymo2de23f52014-02-26 14:30:13 -0800107 // State Providers.
108 scoped_ptr<State> state_;
Alex Deymoc705cc82014-02-19 11:15:00 -0800109
110 DISALLOW_COPY_AND_ASSIGN(PolicyManager);
111};
112
113} // namespace chromeos_policy_manager
114
115// Include the implementation of the template methods.
116#include "update_engine/policy_manager/policy_manager-inl.h"
117
Gilad Arnold2cbb3852014-03-07 12:40:50 -0800118#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_H_