blob: 1d1db2ec2bffa754f5029e9bf488d1952ae54714 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2014 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Alex Deymoc705cc82014-02-19 11:15:00 -080016
Gilad Arnold48415f12014-06-27 07:10:58 -070017#ifndef UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_H_
18#define UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_H_
19
Ben Chan02f7c1d2014-10-18 15:18:02 -070020#include <memory>
Gilad Arnold83ffdda2014-08-08 13:30:31 -070021#include <set>
Gilad Arnold48415f12014-06-27 07:10:58 -070022#include <string>
Alex Deymoc705cc82014-02-19 11:15:00 -080023
Alex Deymo7b948f02014-03-10 17:01:10 -070024#include <base/callback.h>
25#include <base/memory/ref_counted.h>
Gilad Arnoldb2271992014-06-19 12:35:24 -070026#include <base/time/time.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080027
Alex Deymo41a75a72014-04-15 15:36:22 -070028#include "update_engine/clock_interface.h"
Alex Deymo63784a52014-05-28 10:46:14 -070029#include "update_engine/update_manager/default_policy.h"
Gilad Arnold83ffdda2014-08-08 13:30:31 -070030#include "update_engine/update_manager/evaluation_context.h"
Alex Deymo63784a52014-05-28 10:46:14 -070031#include "update_engine/update_manager/policy.h"
32#include "update_engine/update_manager/state.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080033
Alex Deymo63784a52014-05-28 10:46:14 -070034namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -080035
Gilad Arnold83ffdda2014-08-08 13:30:31 -070036// Comparator for scoped_refptr objects.
37template<typename T>
38struct ScopedRefPtrLess {
39 bool operator()(const scoped_refptr<T>& first,
40 const scoped_refptr<T>& second) const {
41 return first.get() < second.get();
42 }
43};
44
Alex Deymo63784a52014-05-28 10:46:14 -070045// The main Update Manager singleton class.
46class UpdateManager {
Alex Deymoc705cc82014-02-19 11:15:00 -080047 public:
Alex Deymo63784a52014-05-28 10:46:14 -070048 // Creates the UpdateManager instance, assuming ownership on the provided
Alex Deymo680d0222014-04-24 21:00:08 -070049 // |state|.
Alex Deymo63784a52014-05-28 10:46:14 -070050 UpdateManager(chromeos_update_engine::ClockInterface* clock,
Gilad Arnoldfd45a732014-08-07 15:53:46 -070051 base::TimeDelta evaluation_timeout,
52 base::TimeDelta expiration_timeout, State* state);
Alex Deymoc705cc82014-02-19 11:15:00 -080053
Gilad Arnold83ffdda2014-08-08 13:30:31 -070054 virtual ~UpdateManager();
Alex Deymoc705cc82014-02-19 11:15:00 -080055
56 // PolicyRequest() evaluates the given policy with the provided arguments and
57 // returns the result. The |policy_method| is the pointer-to-method of the
Alex Deymo63784a52014-05-28 10:46:14 -070058 // Policy class for the policy request to call. The UpdateManager will call
Alex Vakulenko88b591f2014-08-28 16:48:57 -070059 // this method on the right policy. The pointer |result| must not be null
60 // and the remaining |args| depend on the arguments required by the passed
Alex Deymoc705cc82014-02-19 11:15:00 -080061 // |policy_method|.
62 //
63 // When the policy request succeeds, the |result| is set and the method
Gilad Arnold897b5e52014-05-21 09:37:18 -070064 // returns EvalStatus::kSucceeded, otherwise, the |result| may not be set. A
65 // policy called with this method should not block (i.e. return
66 // EvalStatus::kAskMeAgainLater), which is considered a programming error. On
67 // failure, EvalStatus::kFailed is returned.
Alex Deymoc705cc82014-02-19 11:15:00 -080068 //
69 // An example call to this method is:
Alex Deymo63784a52014-05-28 10:46:14 -070070 // um.PolicyRequest(&Policy::SomePolicyMethod, &bool_result, arg1, arg2);
Gilad Arnold13a82432014-05-19 12:52:44 -070071 template<typename R, typename... ActualArgs, typename... ExpectedArgs>
Alex Deymoe75e0252014-04-08 14:00:11 -070072 EvalStatus PolicyRequest(
Gilad Arnold13a82432014-05-19 12:52:44 -070073 EvalStatus (Policy::*policy_method)(EvaluationContext*, State*,
74 std::string*, R*,
75 ExpectedArgs...) const,
76 R* result, ActualArgs...);
Alex Deymoc705cc82014-02-19 11:15:00 -080077
Alex Deymo7b948f02014-03-10 17:01:10 -070078 // Evaluates the given |policy_method| policy with the provided |args|
79 // arguments and calls the |callback| callback with the result when done.
80 //
81 // If the policy implementation should block, returning a
Alex Deymo63784a52014-05-28 10:46:14 -070082 // EvalStatus::kAskMeAgainLater status the Update Manager will re-evaluate the
Alex Deymo53556ec2014-03-17 10:05:57 -070083 // policy until another status is returned. If the policy implementation based
84 // its return value solely on const variables, the callback will be called
Gilad Arnoldfd45a732014-08-07 15:53:46 -070085 // with the EvalStatus::kAskMeAgainLater status (which indicates an error).
Gilad Arnold13a82432014-05-19 12:52:44 -070086 template<typename R, typename... ActualArgs, typename... ExpectedArgs>
Alex Deymo7b948f02014-03-10 17:01:10 -070087 void AsyncPolicyRequest(
88 base::Callback<void(EvalStatus, const R& result)> callback,
Gilad Arnold13a82432014-05-19 12:52:44 -070089 EvalStatus (Policy::*policy_method)(EvaluationContext*, State*,
90 std::string*, R*,
91 ExpectedArgs...) const,
92 ActualArgs... args);
Alex Deymo7b948f02014-03-10 17:01:10 -070093
Alex Deymo94c06162014-03-21 20:34:46 -070094 protected:
Alex Deymo63784a52014-05-28 10:46:14 -070095 // The UpdateManager receives ownership of the passed Policy instance.
Alex Deymo94c06162014-03-21 20:34:46 -070096 void set_policy(const Policy* policy) {
97 policy_.reset(policy);
98 }
99
Alex Deymo680d0222014-04-24 21:00:08 -0700100 // State getter used for testing.
101 State* state() { return state_.get(); }
102
Alex Deymoc705cc82014-02-19 11:15:00 -0800103 private:
Alex Deymo63784a52014-05-28 10:46:14 -0700104 FRIEND_TEST(UmUpdateManagerTest, PolicyRequestCallsPolicy);
105 FRIEND_TEST(UmUpdateManagerTest, PolicyRequestCallsDefaultOnError);
Gilad Arnold897b5e52014-05-21 09:37:18 -0700106 FRIEND_TEST(UmUpdateManagerTest, PolicyRequestDoesntBlockDeathTest);
Alex Deymo63784a52014-05-28 10:46:14 -0700107 FRIEND_TEST(UmUpdateManagerTest, AsyncPolicyRequestDelaysEvaluation);
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700108 FRIEND_TEST(UmUpdateManagerTest, AsyncPolicyRequestTimeoutDoesNotFire);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -0700109 FRIEND_TEST(UmUpdateManagerTest, AsyncPolicyRequestTimesOut);
Alex Deymo7b948f02014-03-10 17:01:10 -0700110
Alex Deymo7b948f02014-03-10 17:01:10 -0700111 // EvaluatePolicy() evaluates the passed |policy_method| method on the current
112 // policy with the given |args| arguments. If the method fails, the default
113 // policy is used instead.
Alex Deymoe75e0252014-04-08 14:00:11 -0700114 template<typename R, typename... Args>
115 EvalStatus EvaluatePolicy(
116 EvaluationContext* ec,
Gilad Arnold13a82432014-05-19 12:52:44 -0700117 EvalStatus (Policy::*policy_method)(EvaluationContext*, State*,
118 std::string*, R*,
119 Args...) const,
Alex Deymoe75e0252014-04-08 14:00:11 -0700120 R* result, Args... args);
Alex Deymo7b948f02014-03-10 17:01:10 -0700121
122 // OnPolicyReadyToEvaluate() is called by the main loop when the evaluation
123 // of the given |policy_method| should be executed. If the evaluation finishes
124 // the |callback| callback is called passing the |result| and the |status|
125 // returned by the policy. If the evaluation returns an
126 // EvalStatus::kAskMeAgainLater state, the |callback| will NOT be called and
127 // the evaluation will be re-scheduled to be called later.
Alex Deymoe75e0252014-04-08 14:00:11 -0700128 template<typename R, typename... Args>
Alex Deymo7b948f02014-03-10 17:01:10 -0700129 void OnPolicyReadyToEvaluate(
130 scoped_refptr<EvaluationContext> ec,
131 base::Callback<void(EvalStatus status, const R& result)> callback,
Gilad Arnold13a82432014-05-19 12:52:44 -0700132 EvalStatus (Policy::*policy_method)(EvaluationContext*, State*,
133 std::string*, R*,
134 Args...) const,
Alex Deymoe75e0252014-04-08 14:00:11 -0700135 Args... args);
Alex Deymoc705cc82014-02-19 11:15:00 -0800136
Gilad Arnold83ffdda2014-08-08 13:30:31 -0700137 // Unregisters (removes from repo) a previously created EvaluationContext.
138 void UnregisterEvalContext(EvaluationContext* ec);
139
Alex Deymo63784a52014-05-28 10:46:14 -0700140 // The policy used by the UpdateManager. Note that since it is a const Policy,
Alex Deymoc705cc82014-02-19 11:15:00 -0800141 // policy implementations are not allowed to persist state on this class.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700142 std::unique_ptr<const Policy> policy_;
Alex Deymoc705cc82014-02-19 11:15:00 -0800143
144 // A safe default value to the current policy. This policy is used whenever
Alex Deymoe636c3c2014-03-11 19:02:08 -0700145 // a policy implementation fails with EvalStatus::kFailed.
Alex Deymoc705cc82014-02-19 11:15:00 -0800146 const DefaultPolicy default_policy_;
147
Alex Deymo2de23f52014-02-26 14:30:13 -0800148 // State Providers.
Ben Chan02f7c1d2014-10-18 15:18:02 -0700149 std::unique_ptr<State> state_;
Alex Deymoc705cc82014-02-19 11:15:00 -0800150
Alex Deymo41a75a72014-04-15 15:36:22 -0700151 // Pointer to the mockable clock interface;
152 chromeos_update_engine::ClockInterface* clock_;
153
Gilad Arnoldb2271992014-06-19 12:35:24 -0700154 // Timeout for a policy evaluation.
155 const base::TimeDelta evaluation_timeout_;
156
Gilad Arnoldfd45a732014-08-07 15:53:46 -0700157 // Timeout for expiration of the evaluation context, used for async requests.
158 const base::TimeDelta expiration_timeout_;
159
Gilad Arnold83ffdda2014-08-08 13:30:31 -0700160 // Repository of previously created EvaluationContext objects. These are being
161 // unregistered (and the reference released) when the context is being
162 // destructed; alternatively, when the UpdateManager instance is destroyed, it
163 // will remove all pending events associated with all outstanding contexts
164 // (which should, in turn, trigger their destruction).
165 std::set<scoped_refptr<EvaluationContext>,
166 ScopedRefPtrLess<EvaluationContext>> ec_repo_;
167
168 base::WeakPtrFactory<UpdateManager> weak_ptr_factory_;
169
Alex Deymo63784a52014-05-28 10:46:14 -0700170 DISALLOW_COPY_AND_ASSIGN(UpdateManager);
Alex Deymoc705cc82014-02-19 11:15:00 -0800171};
172
Alex Deymo63784a52014-05-28 10:46:14 -0700173} // namespace chromeos_update_manager
Alex Deymoc705cc82014-02-19 11:15:00 -0800174
175// Include the implementation of the template methods.
Alex Deymo63784a52014-05-28 10:46:14 -0700176#include "update_engine/update_manager/update_manager-inl.h"
Alex Deymoc705cc82014-02-19 11:15:00 -0800177
Gilad Arnold48415f12014-06-27 07:10:58 -0700178#endif // UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_H_