blob: 3966ec5a8fac651fdc9d6d456b49236021410a08 [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
5#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_H
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_H
7
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
25 // Initializes the PolicyManager instance. Returns whether the initialization
26 // succeeded.
27 bool Init();
28
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);
45 template<typename T, typename R, typename... Args>
Alex Deymo2de23f52014-02-26 14:30:13 -080046 EvalStatus PolicyRequest(T policy_method, R* result, Args... args);
Alex Deymoc705cc82014-02-19 11:15:00 -080047
Alex Deymo7b948f02014-03-10 17:01:10 -070048 // Evaluates the given |policy_method| policy with the provided |args|
49 // arguments and calls the |callback| callback with the result when done.
50 //
51 // If the policy implementation should block, returning a
52 // EvalStatus::kAskMeAgainLater status the policy manager will re-evaluate the
53 // policy until another status is returned.
54 template<typename T, typename R, typename... Args>
55 void AsyncPolicyRequest(
56 base::Callback<void(EvalStatus, const R& result)> callback,
57 T policy_method, Args... args);
58
Alex Deymoc705cc82014-02-19 11:15:00 -080059 private:
60 friend class PmPolicyManagerTest;
61 FRIEND_TEST(PmPolicyManagerTest, PolicyRequestCallsPolicy);
62 FRIEND_TEST(PmPolicyManagerTest, PolicyRequestCallsDefaultOnError);
63 FRIEND_TEST(PmPolicyManagerTest, PolicyRequestDoesntBlock);
Alex Deymo7b948f02014-03-10 17:01:10 -070064 FRIEND_TEST(PmPolicyManagerTest, AsyncPolicyRequestDelaysEvaluation);
65
66 // Schedules the passed |callback| to run from the GLib's main loop after a
67 // timeout if it is given.
68 static void RunFromMainLoop(const base::Closure& callback);
69 static void RunFromMainLoopAfterTimeout(const base::Closure& callback,
70 base::TimeDelta timeout);
71
72 // Called by the GLib's main loop when is time to call the callback scheduled
73 // with RunFromMainLopp() and similar functions. The pointer to the callback
74 // passed when scheduling it is passed to this functions as a gpointer on
75 // |user_data|.
76 static gboolean OnRanFromMainLoop(gpointer user_data);
77
78 // EvaluatePolicy() evaluates the passed |policy_method| method on the current
79 // policy with the given |args| arguments. If the method fails, the default
80 // policy is used instead.
81 template<typename T, typename R, typename... Args>
82 EvalStatus EvaluatePolicy(EvaluationContext* ec,
83 T policy_method, R* result,
84 Args... args);
85
86 // OnPolicyReadyToEvaluate() is called by the main loop when the evaluation
87 // of the given |policy_method| should be executed. If the evaluation finishes
88 // the |callback| callback is called passing the |result| and the |status|
89 // returned by the policy. If the evaluation returns an
90 // EvalStatus::kAskMeAgainLater state, the |callback| will NOT be called and
91 // the evaluation will be re-scheduled to be called later.
92 template<typename T, typename R, typename... Args>
93 void OnPolicyReadyToEvaluate(
94 scoped_refptr<EvaluationContext> ec,
95 base::Callback<void(EvalStatus status, const R& result)> callback,
96 T policy_method, Args... args);
Alex Deymoc705cc82014-02-19 11:15:00 -080097
98 // The policy used by the PolicyManager. Note that since it is a const Policy,
99 // policy implementations are not allowed to persist state on this class.
100 scoped_ptr<const Policy> policy_;
101
102 // A safe default value to the current policy. This policy is used whenever
Alex Deymoe636c3c2014-03-11 19:02:08 -0700103 // a policy implementation fails with EvalStatus::kFailed.
Alex Deymoc705cc82014-02-19 11:15:00 -0800104 const DefaultPolicy default_policy_;
105
Alex Deymo2de23f52014-02-26 14:30:13 -0800106 // State Providers.
107 scoped_ptr<State> state_;
Alex Deymoc705cc82014-02-19 11:15:00 -0800108
109 DISALLOW_COPY_AND_ASSIGN(PolicyManager);
110};
111
112} // namespace chromeos_policy_manager
113
114// Include the implementation of the template methods.
115#include "update_engine/policy_manager/policy_manager-inl.h"
116
117#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_H