blob: 8c96d7b704c9194f64ee97b622752ab3476f1da6 [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
Alex Deymo63784a52014-05-28 10:46:14 -07005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_H_
Alex Deymoc705cc82014-02-19 11:15:00 -08007
Alex Deymo7b948f02014-03-10 17:01:10 -07008#include <base/callback.h>
9#include <base/memory/ref_counted.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080010#include <base/memory/scoped_ptr.h>
11
Alex Deymo41a75a72014-04-15 15:36:22 -070012#include "update_engine/clock_interface.h"
Alex Deymo63784a52014-05-28 10:46:14 -070013#include "update_engine/update_manager/default_policy.h"
14#include "update_engine/update_manager/policy.h"
15#include "update_engine/update_manager/state.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080016
Alex Deymo63784a52014-05-28 10:46:14 -070017namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -080018
Alex Deymo63784a52014-05-28 10:46:14 -070019// The main Update Manager singleton class.
20class UpdateManager {
Alex Deymoc705cc82014-02-19 11:15:00 -080021 public:
Alex Deymo63784a52014-05-28 10:46:14 -070022 // Creates the UpdateManager instance, assuming ownership on the provided
Alex Deymo680d0222014-04-24 21:00:08 -070023 // |state|.
Alex Deymo63784a52014-05-28 10:46:14 -070024 UpdateManager(chromeos_update_engine::ClockInterface* clock,
Alex Deymo680d0222014-04-24 21:00:08 -070025 State* state);
Alex Deymoc705cc82014-02-19 11:15:00 -080026
Alex Deymo63784a52014-05-28 10:46:14 -070027 virtual ~UpdateManager() {}
Alex Deymoc705cc82014-02-19 11:15:00 -080028
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
Alex Deymo63784a52014-05-28 10:46:14 -070031 // Policy class for the policy request to call. The UpdateManager will call
Alex Deymoc705cc82014-02-19 11:15:00 -080032 // 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
Gilad Arnold897b5e52014-05-21 09:37:18 -070037 // returns EvalStatus::kSucceeded, otherwise, the |result| may not be set. A
38 // policy called with this method should not block (i.e. return
39 // EvalStatus::kAskMeAgainLater), which is considered a programming error. On
40 // failure, EvalStatus::kFailed is returned.
Alex Deymoc705cc82014-02-19 11:15:00 -080041 //
42 // An example call to this method is:
Alex Deymo63784a52014-05-28 10:46:14 -070043 // um.PolicyRequest(&Policy::SomePolicyMethod, &bool_result, arg1, arg2);
Gilad Arnold13a82432014-05-19 12:52:44 -070044 template<typename R, typename... ActualArgs, typename... ExpectedArgs>
Alex Deymoe75e0252014-04-08 14:00:11 -070045 EvalStatus PolicyRequest(
Gilad Arnold13a82432014-05-19 12:52:44 -070046 EvalStatus (Policy::*policy_method)(EvaluationContext*, State*,
47 std::string*, R*,
48 ExpectedArgs...) const,
49 R* result, ActualArgs...);
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
Alex Deymo63784a52014-05-28 10:46:14 -070055 // EvalStatus::kAskMeAgainLater status the Update Manager will re-evaluate the
Alex Deymo53556ec2014-03-17 10:05:57 -070056 // policy until another status is returned. If the policy implementation based
57 // its return value solely on const variables, the callback will be called
58 // with the EvalStatus::kAskMeAgainLater status.
Gilad Arnold13a82432014-05-19 12:52:44 -070059 template<typename R, typename... ActualArgs, typename... ExpectedArgs>
Alex Deymo7b948f02014-03-10 17:01:10 -070060 void AsyncPolicyRequest(
61 base::Callback<void(EvalStatus, const R& result)> callback,
Gilad Arnold13a82432014-05-19 12:52:44 -070062 EvalStatus (Policy::*policy_method)(EvaluationContext*, State*,
63 std::string*, R*,
64 ExpectedArgs...) const,
65 ActualArgs... args);
Alex Deymo7b948f02014-03-10 17:01:10 -070066
Alex Deymo94c06162014-03-21 20:34:46 -070067 protected:
Alex Deymo63784a52014-05-28 10:46:14 -070068 // The UpdateManager receives ownership of the passed Policy instance.
Alex Deymo94c06162014-03-21 20:34:46 -070069 void set_policy(const Policy* policy) {
70 policy_.reset(policy);
71 }
72
Alex Deymo680d0222014-04-24 21:00:08 -070073 // State getter used for testing.
74 State* state() { return state_.get(); }
75
Alex Deymoc705cc82014-02-19 11:15:00 -080076 private:
Alex Deymo63784a52014-05-28 10:46:14 -070077 FRIEND_TEST(UmUpdateManagerTest, PolicyRequestCallsPolicy);
78 FRIEND_TEST(UmUpdateManagerTest, PolicyRequestCallsDefaultOnError);
Gilad Arnold897b5e52014-05-21 09:37:18 -070079 FRIEND_TEST(UmUpdateManagerTest, PolicyRequestDoesntBlockDeathTest);
Alex Deymo63784a52014-05-28 10:46:14 -070080 FRIEND_TEST(UmUpdateManagerTest, AsyncPolicyRequestDelaysEvaluation);
Alex Deymo7b948f02014-03-10 17:01:10 -070081
Alex Deymo7b948f02014-03-10 17:01:10 -070082 // EvaluatePolicy() evaluates the passed |policy_method| method on the current
83 // policy with the given |args| arguments. If the method fails, the default
84 // policy is used instead.
Alex Deymoe75e0252014-04-08 14:00:11 -070085 template<typename R, typename... Args>
86 EvalStatus EvaluatePolicy(
87 EvaluationContext* ec,
Gilad Arnold13a82432014-05-19 12:52:44 -070088 EvalStatus (Policy::*policy_method)(EvaluationContext*, State*,
89 std::string*, R*,
90 Args...) const,
Alex Deymoe75e0252014-04-08 14:00:11 -070091 R* result, Args... args);
Alex Deymo7b948f02014-03-10 17:01:10 -070092
93 // OnPolicyReadyToEvaluate() is called by the main loop when the evaluation
94 // of the given |policy_method| should be executed. If the evaluation finishes
95 // the |callback| callback is called passing the |result| and the |status|
96 // returned by the policy. If the evaluation returns an
97 // EvalStatus::kAskMeAgainLater state, the |callback| will NOT be called and
98 // the evaluation will be re-scheduled to be called later.
Alex Deymoe75e0252014-04-08 14:00:11 -070099 template<typename R, typename... Args>
Alex Deymo7b948f02014-03-10 17:01:10 -0700100 void OnPolicyReadyToEvaluate(
101 scoped_refptr<EvaluationContext> ec,
102 base::Callback<void(EvalStatus status, const R& result)> callback,
Gilad Arnold13a82432014-05-19 12:52:44 -0700103 EvalStatus (Policy::*policy_method)(EvaluationContext*, State*,
104 std::string*, R*,
105 Args...) const,
Alex Deymoe75e0252014-04-08 14:00:11 -0700106 Args... args);
Alex Deymoc705cc82014-02-19 11:15:00 -0800107
Alex Deymo63784a52014-05-28 10:46:14 -0700108 // The policy used by the UpdateManager. Note that since it is a const Policy,
Alex Deymoc705cc82014-02-19 11:15:00 -0800109 // policy implementations are not allowed to persist state on this class.
110 scoped_ptr<const Policy> policy_;
111
112 // A safe default value to the current policy. This policy is used whenever
Alex Deymoe636c3c2014-03-11 19:02:08 -0700113 // a policy implementation fails with EvalStatus::kFailed.
Alex Deymoc705cc82014-02-19 11:15:00 -0800114 const DefaultPolicy default_policy_;
115
Alex Deymo2de23f52014-02-26 14:30:13 -0800116 // State Providers.
117 scoped_ptr<State> state_;
Alex Deymoc705cc82014-02-19 11:15:00 -0800118
Alex Deymo41a75a72014-04-15 15:36:22 -0700119 // Pointer to the mockable clock interface;
120 chromeos_update_engine::ClockInterface* clock_;
121
Alex Deymo63784a52014-05-28 10:46:14 -0700122 DISALLOW_COPY_AND_ASSIGN(UpdateManager);
Alex Deymoc705cc82014-02-19 11:15:00 -0800123};
124
Alex Deymo63784a52014-05-28 10:46:14 -0700125} // namespace chromeos_update_manager
Alex Deymoc705cc82014-02-19 11:15:00 -0800126
127// Include the implementation of the template methods.
Alex Deymo63784a52014-05-28 10:46:14 -0700128#include "update_engine/update_manager/update_manager-inl.h"
Alex Deymoc705cc82014-02-19 11:15:00 -0800129
Alex Deymo63784a52014-05-28 10:46:14 -0700130#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_H_