blob: 51cc24a923cab5d9fb918733a15b6446d237c50f [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
Gilad Arnold5ef9c482014-03-03 13:51:02 -080014#include "update_engine/clock_interface.h"
15#include "update_engine/dbus_wrapper_interface.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080016#include "update_engine/policy_manager/default_policy.h"
17#include "update_engine/policy_manager/policy.h"
Alex Deymo2de23f52014-02-26 14:30:13 -080018#include "update_engine/policy_manager/state.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080019
20namespace chromeos_policy_manager {
21
22// The main Policy Manager singleton class.
23class PolicyManager {
24 public:
25 PolicyManager() {}
26
27 // Initializes the PolicyManager instance. Returns whether the initialization
28 // succeeded.
Gilad Arnold5ef9c482014-03-03 13:51:02 -080029 bool Init(chromeos_update_engine::DBusWrapperInterface* dbus,
30 chromeos_update_engine::ClockInterface* clock);
Alex Deymoc705cc82014-02-19 11:15:00 -080031
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 Deymoe636c3c2014-03-11 19:02:08 -070040 // 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 Deymoc705cc82014-02-19 11:15:00 -080045 //
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 Deymo2de23f52014-02-26 14:30:13 -080049 EvalStatus PolicyRequest(T policy_method, R* result, Args... args);
Alex Deymoc705cc82014-02-19 11:15:00 -080050
Alex Deymo7b948f02014-03-10 17:01:10 -070051 // 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 Deymoc705cc82014-02-19 11:15:00 -080062 private:
63 friend class PmPolicyManagerTest;
64 FRIEND_TEST(PmPolicyManagerTest, PolicyRequestCallsPolicy);
65 FRIEND_TEST(PmPolicyManagerTest, PolicyRequestCallsDefaultOnError);
66 FRIEND_TEST(PmPolicyManagerTest, PolicyRequestDoesntBlock);
Alex Deymo7b948f02014-03-10 17:01:10 -070067 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 Deymoc705cc82014-02-19 11:15:00 -0800100
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 Deymoe636c3c2014-03-11 19:02:08 -0700106 // a policy implementation fails with EvalStatus::kFailed.
Alex Deymoc705cc82014-02-19 11:15:00 -0800107 const DefaultPolicy default_policy_;
108
Alex Deymo2de23f52014-02-26 14:30:13 -0800109 // State Providers.
110 scoped_ptr<State> state_;
Alex Deymoc705cc82014-02-19 11:15:00 -0800111
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 Arnold2cbb3852014-03-07 12:40:50 -0800120#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_POLICY_MANAGER_H_