Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 1 | // 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 Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 5 | #ifndef UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_H_ |
| 6 | #define UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_H_ |
| 7 | |
| 8 | #include <string> |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 9 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 10 | #include <base/callback.h> |
| 11 | #include <base/memory/ref_counted.h> |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 12 | #include <base/memory/scoped_ptr.h> |
Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 13 | #include <base/time/time.h> |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 14 | |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 15 | #include "update_engine/clock_interface.h" |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 16 | #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 Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 19 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 20 | namespace chromeos_update_manager { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 21 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 22 | // The main Update Manager singleton class. |
| 23 | class UpdateManager { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 24 | public: |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 25 | // Creates the UpdateManager instance, assuming ownership on the provided |
Alex Deymo | 680d022 | 2014-04-24 21:00:08 -0700 | [diff] [blame] | 26 | // |state|. |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 27 | UpdateManager(chromeos_update_engine::ClockInterface* clock, |
Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 28 | base::TimeDelta evaluation_timeout, State* state); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 29 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 30 | virtual ~UpdateManager() {} |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 31 | |
| 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 Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 34 | // Policy class for the policy request to call. The UpdateManager will call |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 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 |
Gilad Arnold | 897b5e5 | 2014-05-21 09:37:18 -0700 | [diff] [blame] | 40 | // 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 Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 44 | // |
| 45 | // An example call to this method is: |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 46 | // um.PolicyRequest(&Policy::SomePolicyMethod, &bool_result, arg1, arg2); |
Gilad Arnold | 13a8243 | 2014-05-19 12:52:44 -0700 | [diff] [blame] | 47 | template<typename R, typename... ActualArgs, typename... ExpectedArgs> |
Alex Deymo | e75e025 | 2014-04-08 14:00:11 -0700 | [diff] [blame] | 48 | EvalStatus PolicyRequest( |
Gilad Arnold | 13a8243 | 2014-05-19 12:52:44 -0700 | [diff] [blame] | 49 | EvalStatus (Policy::*policy_method)(EvaluationContext*, State*, |
| 50 | std::string*, R*, |
| 51 | ExpectedArgs...) const, |
| 52 | R* result, ActualArgs...); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 53 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 54 | // Evaluates the given |policy_method| policy with the provided |args| |
| 55 | // arguments and calls the |callback| callback with the result when done. |
| 56 | // |
| 57 | // If the policy implementation should block, returning a |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 58 | // EvalStatus::kAskMeAgainLater status the Update Manager will re-evaluate the |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 59 | // policy until another status is returned. If the policy implementation based |
| 60 | // its return value solely on const variables, the callback will be called |
| 61 | // with the EvalStatus::kAskMeAgainLater status. |
Gilad Arnold | 13a8243 | 2014-05-19 12:52:44 -0700 | [diff] [blame] | 62 | template<typename R, typename... ActualArgs, typename... ExpectedArgs> |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 63 | void AsyncPolicyRequest( |
| 64 | base::Callback<void(EvalStatus, const R& result)> callback, |
Gilad Arnold | 13a8243 | 2014-05-19 12:52:44 -0700 | [diff] [blame] | 65 | EvalStatus (Policy::*policy_method)(EvaluationContext*, State*, |
| 66 | std::string*, R*, |
| 67 | ExpectedArgs...) const, |
| 68 | ActualArgs... args); |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 69 | |
Alex Deymo | 94c0616 | 2014-03-21 20:34:46 -0700 | [diff] [blame] | 70 | protected: |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 71 | // The UpdateManager receives ownership of the passed Policy instance. |
Alex Deymo | 94c0616 | 2014-03-21 20:34:46 -0700 | [diff] [blame] | 72 | void set_policy(const Policy* policy) { |
| 73 | policy_.reset(policy); |
| 74 | } |
| 75 | |
Alex Deymo | 680d022 | 2014-04-24 21:00:08 -0700 | [diff] [blame] | 76 | // State getter used for testing. |
| 77 | State* state() { return state_.get(); } |
| 78 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 79 | private: |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 80 | FRIEND_TEST(UmUpdateManagerTest, PolicyRequestCallsPolicy); |
| 81 | FRIEND_TEST(UmUpdateManagerTest, PolicyRequestCallsDefaultOnError); |
Gilad Arnold | 897b5e5 | 2014-05-21 09:37:18 -0700 | [diff] [blame] | 82 | FRIEND_TEST(UmUpdateManagerTest, PolicyRequestDoesntBlockDeathTest); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 83 | FRIEND_TEST(UmUpdateManagerTest, AsyncPolicyRequestDelaysEvaluation); |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 84 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 85 | // EvaluatePolicy() evaluates the passed |policy_method| method on the current |
| 86 | // policy with the given |args| arguments. If the method fails, the default |
| 87 | // policy is used instead. |
Alex Deymo | e75e025 | 2014-04-08 14:00:11 -0700 | [diff] [blame] | 88 | template<typename R, typename... Args> |
| 89 | EvalStatus EvaluatePolicy( |
| 90 | EvaluationContext* ec, |
Gilad Arnold | 13a8243 | 2014-05-19 12:52:44 -0700 | [diff] [blame] | 91 | EvalStatus (Policy::*policy_method)(EvaluationContext*, State*, |
| 92 | std::string*, R*, |
| 93 | Args...) const, |
Alex Deymo | e75e025 | 2014-04-08 14:00:11 -0700 | [diff] [blame] | 94 | R* result, Args... args); |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 95 | |
| 96 | // OnPolicyReadyToEvaluate() is called by the main loop when the evaluation |
| 97 | // of the given |policy_method| should be executed. If the evaluation finishes |
| 98 | // the |callback| callback is called passing the |result| and the |status| |
| 99 | // returned by the policy. If the evaluation returns an |
| 100 | // EvalStatus::kAskMeAgainLater state, the |callback| will NOT be called and |
| 101 | // the evaluation will be re-scheduled to be called later. |
Alex Deymo | e75e025 | 2014-04-08 14:00:11 -0700 | [diff] [blame] | 102 | template<typename R, typename... Args> |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 103 | void OnPolicyReadyToEvaluate( |
| 104 | scoped_refptr<EvaluationContext> ec, |
| 105 | base::Callback<void(EvalStatus status, const R& result)> callback, |
Gilad Arnold | 13a8243 | 2014-05-19 12:52:44 -0700 | [diff] [blame] | 106 | EvalStatus (Policy::*policy_method)(EvaluationContext*, State*, |
| 107 | std::string*, R*, |
| 108 | Args...) const, |
Alex Deymo | e75e025 | 2014-04-08 14:00:11 -0700 | [diff] [blame] | 109 | Args... args); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 110 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 111 | // The policy used by the UpdateManager. Note that since it is a const Policy, |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 112 | // policy implementations are not allowed to persist state on this class. |
| 113 | scoped_ptr<const Policy> policy_; |
| 114 | |
| 115 | // A safe default value to the current policy. This policy is used whenever |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 116 | // a policy implementation fails with EvalStatus::kFailed. |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 117 | const DefaultPolicy default_policy_; |
| 118 | |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 119 | // State Providers. |
| 120 | scoped_ptr<State> state_; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 121 | |
Alex Deymo | 41a75a7 | 2014-04-15 15:36:22 -0700 | [diff] [blame] | 122 | // Pointer to the mockable clock interface; |
| 123 | chromeos_update_engine::ClockInterface* clock_; |
| 124 | |
Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 125 | // Timeout for a policy evaluation. |
| 126 | const base::TimeDelta evaluation_timeout_; |
| 127 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 128 | DISALLOW_COPY_AND_ASSIGN(UpdateManager); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 129 | }; |
| 130 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 131 | } // namespace chromeos_update_manager |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 132 | |
| 133 | // Include the implementation of the template methods. |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 134 | #include "update_engine/update_manager/update_manager-inl.h" |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 135 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 136 | #endif // UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_H_ |