blob: 0b04c07223c3d3154076c13e73f8b5ba6bc934f7 [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 Arnold48415f12014-06-27 07:10:58 -07005#ifndef UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_H_
6#define UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_H_
7
8#include <string>
Alex Deymoc705cc82014-02-19 11:15:00 -08009
Alex Deymo7b948f02014-03-10 17:01:10 -070010#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>
Gilad Arnoldb2271992014-06-19 12:35:24 -070013#include <base/time/time.h>
Alex Deymoc705cc82014-02-19 11:15:00 -080014
Alex Deymo41a75a72014-04-15 15:36:22 -070015#include "update_engine/clock_interface.h"
Alex Deymo63784a52014-05-28 10:46:14 -070016#include "update_engine/update_manager/default_policy.h"
17#include "update_engine/update_manager/policy.h"
18#include "update_engine/update_manager/state.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080019
Alex Deymo63784a52014-05-28 10:46:14 -070020namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -080021
Alex Deymo63784a52014-05-28 10:46:14 -070022// The main Update Manager singleton class.
23class UpdateManager {
Alex Deymoc705cc82014-02-19 11:15:00 -080024 public:
Alex Deymo63784a52014-05-28 10:46:14 -070025 // Creates the UpdateManager instance, assuming ownership on the provided
Alex Deymo680d0222014-04-24 21:00:08 -070026 // |state|.
Alex Deymo63784a52014-05-28 10:46:14 -070027 UpdateManager(chromeos_update_engine::ClockInterface* clock,
Gilad Arnoldb2271992014-06-19 12:35:24 -070028 base::TimeDelta evaluation_timeout, State* state);
Alex Deymoc705cc82014-02-19 11:15:00 -080029
Alex Deymo63784a52014-05-28 10:46:14 -070030 virtual ~UpdateManager() {}
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
Alex Deymo63784a52014-05-28 10:46:14 -070034 // Policy class for the policy request to call. The UpdateManager will call
Alex Deymoc705cc82014-02-19 11:15:00 -080035 // 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
Gilad Arnold897b5e52014-05-21 09:37:18 -070040 // returns EvalStatus::kSucceeded, otherwise, the |result| may not be set. A
41 // policy called with this method should not block (i.e. return
42 // EvalStatus::kAskMeAgainLater), which is considered a programming error. On
43 // failure, EvalStatus::kFailed is returned.
Alex Deymoc705cc82014-02-19 11:15:00 -080044 //
45 // An example call to this method is:
Alex Deymo63784a52014-05-28 10:46:14 -070046 // um.PolicyRequest(&Policy::SomePolicyMethod, &bool_result, arg1, arg2);
Gilad Arnold13a82432014-05-19 12:52:44 -070047 template<typename R, typename... ActualArgs, typename... ExpectedArgs>
Alex Deymoe75e0252014-04-08 14:00:11 -070048 EvalStatus PolicyRequest(
Gilad Arnold13a82432014-05-19 12:52:44 -070049 EvalStatus (Policy::*policy_method)(EvaluationContext*, State*,
50 std::string*, R*,
51 ExpectedArgs...) const,
52 R* result, ActualArgs...);
Alex Deymoc705cc82014-02-19 11:15:00 -080053
Alex Deymo7b948f02014-03-10 17:01:10 -070054 // Evaluates the given |policy_method| policy with the provided |args|
55 // arguments and calls the |callback| callback with the result when done.
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070056 // Evaluation is not allowed to exceed |request_timeout|.
Alex Deymo7b948f02014-03-10 17:01:10 -070057 //
58 // If the policy implementation should block, returning a
Alex Deymo63784a52014-05-28 10:46:14 -070059 // EvalStatus::kAskMeAgainLater status the Update Manager will re-evaluate the
Alex Deymo53556ec2014-03-17 10:05:57 -070060 // policy until another status is returned. If the policy implementation based
61 // its return value solely on const variables, the callback will be called
62 // with the EvalStatus::kAskMeAgainLater status.
Gilad Arnold13a82432014-05-19 12:52:44 -070063 template<typename R, typename... ActualArgs, typename... ExpectedArgs>
Alex Deymo7b948f02014-03-10 17:01:10 -070064 void AsyncPolicyRequest(
65 base::Callback<void(EvalStatus, const R& result)> callback,
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070066 base::TimeDelta request_timeout,
Gilad Arnold13a82432014-05-19 12:52:44 -070067 EvalStatus (Policy::*policy_method)(EvaluationContext*, State*,
68 std::string*, R*,
69 ExpectedArgs...) const,
70 ActualArgs... args);
Alex Deymo7b948f02014-03-10 17:01:10 -070071
Alex Deymo94c06162014-03-21 20:34:46 -070072 protected:
Alex Deymo63784a52014-05-28 10:46:14 -070073 // The UpdateManager receives ownership of the passed Policy instance.
Alex Deymo94c06162014-03-21 20:34:46 -070074 void set_policy(const Policy* policy) {
75 policy_.reset(policy);
76 }
77
Alex Deymo680d0222014-04-24 21:00:08 -070078 // State getter used for testing.
79 State* state() { return state_.get(); }
80
Alex Deymoc705cc82014-02-19 11:15:00 -080081 private:
Alex Deymo63784a52014-05-28 10:46:14 -070082 FRIEND_TEST(UmUpdateManagerTest, PolicyRequestCallsPolicy);
83 FRIEND_TEST(UmUpdateManagerTest, PolicyRequestCallsDefaultOnError);
Gilad Arnold897b5e52014-05-21 09:37:18 -070084 FRIEND_TEST(UmUpdateManagerTest, PolicyRequestDoesntBlockDeathTest);
Alex Deymo63784a52014-05-28 10:46:14 -070085 FRIEND_TEST(UmUpdateManagerTest, AsyncPolicyRequestDelaysEvaluation);
Gilad Arnoldf9f85d62014-06-19 18:07:01 -070086 FRIEND_TEST(UmUpdateManagerTest, AsyncPolicyRequestDoesNotTimeOut);
87 FRIEND_TEST(UmUpdateManagerTest, AsyncPolicyRequestTimesOut);
Alex Deymo7b948f02014-03-10 17:01:10 -070088
Alex Deymo7b948f02014-03-10 17:01:10 -070089 // EvaluatePolicy() evaluates the passed |policy_method| method on the current
90 // policy with the given |args| arguments. If the method fails, the default
91 // policy is used instead.
Alex Deymoe75e0252014-04-08 14:00:11 -070092 template<typename R, typename... Args>
93 EvalStatus EvaluatePolicy(
94 EvaluationContext* ec,
Gilad Arnold13a82432014-05-19 12:52:44 -070095 EvalStatus (Policy::*policy_method)(EvaluationContext*, State*,
96 std::string*, R*,
97 Args...) const,
Alex Deymoe75e0252014-04-08 14:00:11 -070098 R* result, Args... args);
Alex Deymo7b948f02014-03-10 17:01:10 -070099
100 // OnPolicyReadyToEvaluate() is called by the main loop when the evaluation
101 // of the given |policy_method| should be executed. If the evaluation finishes
102 // the |callback| callback is called passing the |result| and the |status|
103 // returned by the policy. If the evaluation returns an
104 // EvalStatus::kAskMeAgainLater state, the |callback| will NOT be called and
105 // the evaluation will be re-scheduled to be called later.
Alex Deymoe75e0252014-04-08 14:00:11 -0700106 template<typename R, typename... Args>
Alex Deymo7b948f02014-03-10 17:01:10 -0700107 void OnPolicyReadyToEvaluate(
108 scoped_refptr<EvaluationContext> ec,
109 base::Callback<void(EvalStatus status, const R& result)> callback,
Gilad Arnold13a82432014-05-19 12:52:44 -0700110 EvalStatus (Policy::*policy_method)(EvaluationContext*, State*,
111 std::string*, R*,
112 Args...) const,
Alex Deymoe75e0252014-04-08 14:00:11 -0700113 Args... args);
Alex Deymoc705cc82014-02-19 11:15:00 -0800114
Alex Deymo63784a52014-05-28 10:46:14 -0700115 // The policy used by the UpdateManager. Note that since it is a const Policy,
Alex Deymoc705cc82014-02-19 11:15:00 -0800116 // policy implementations are not allowed to persist state on this class.
117 scoped_ptr<const Policy> policy_;
118
119 // A safe default value to the current policy. This policy is used whenever
Alex Deymoe636c3c2014-03-11 19:02:08 -0700120 // a policy implementation fails with EvalStatus::kFailed.
Alex Deymoc705cc82014-02-19 11:15:00 -0800121 const DefaultPolicy default_policy_;
122
Alex Deymo2de23f52014-02-26 14:30:13 -0800123 // State Providers.
124 scoped_ptr<State> state_;
Alex Deymoc705cc82014-02-19 11:15:00 -0800125
Alex Deymo41a75a72014-04-15 15:36:22 -0700126 // Pointer to the mockable clock interface;
127 chromeos_update_engine::ClockInterface* clock_;
128
Gilad Arnoldb2271992014-06-19 12:35:24 -0700129 // Timeout for a policy evaluation.
130 const base::TimeDelta evaluation_timeout_;
131
Alex Deymo63784a52014-05-28 10:46:14 -0700132 DISALLOW_COPY_AND_ASSIGN(UpdateManager);
Alex Deymoc705cc82014-02-19 11:15:00 -0800133};
134
Alex Deymo63784a52014-05-28 10:46:14 -0700135} // namespace chromeos_update_manager
Alex Deymoc705cc82014-02-19 11:15:00 -0800136
137// Include the implementation of the template methods.
Alex Deymo63784a52014-05-28 10:46:14 -0700138#include "update_engine/update_manager/update_manager-inl.h"
Alex Deymoc705cc82014-02-19 11:15:00 -0800139
Gilad Arnold48415f12014-06-27 07:10:58 -0700140#endif // UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_H_