| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 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 Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 16 |  | 
| Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_H_ | 
|  | 18 | #define UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_H_ | 
|  | 19 |  | 
| Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 20 | #include <memory> | 
| Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 21 | #include <set> | 
| Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 22 | #include <string> | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 23 |  | 
| Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 24 | #include <base/callback.h> | 
| Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 25 | #include <base/time/time.h> | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 26 |  | 
| Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 27 | #include "update_engine/common/system_state.h" | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 28 | #include "update_engine/update_manager/default_policy.h" | 
| Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 29 | #include "update_engine/update_manager/evaluation_context.h" | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 30 | #include "update_engine/update_manager/policy.h" | 
|  | 31 | #include "update_engine/update_manager/state.h" | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 32 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 33 | namespace chromeos_update_manager { | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 34 |  | 
| Jae Hoon Kim | 504c3cb | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 35 | // Please do not move this class into a new file for simplicity. | 
|  | 36 | // This pure virtual class is purely created for purpose of testing. The reason | 
|  | 37 | // was that |UpdateManager|'s member functions are templatized, which does not | 
|  | 38 | // play nicely when testing (mocking + faking). Whenever a specialized member of | 
|  | 39 | // |UpdateManager| must be tested, please add a specialized template member | 
|  | 40 | // function within this class for testing. | 
|  | 41 | class SpecializedPolicyRequestInterface { | 
|  | 42 | public: | 
|  | 43 | virtual ~SpecializedPolicyRequestInterface() = default; | 
|  | 44 |  | 
|  | 45 | virtual void AsyncPolicyRequestUpdateCheckAllowed( | 
|  | 46 | base::Callback<void(EvalStatus, const UpdateCheckParams& result)> | 
|  | 47 | callback, | 
|  | 48 | EvalStatus (Policy::*policy_method)(EvaluationContext*, | 
|  | 49 | State*, | 
|  | 50 | std::string*, | 
|  | 51 | UpdateCheckParams*) const) = 0; | 
|  | 52 | }; | 
|  | 53 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 54 | // The main Update Manager singleton class. | 
| Jae Hoon Kim | 504c3cb | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 55 | class UpdateManager : public SpecializedPolicyRequestInterface { | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 56 | public: | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 57 | // Creates the UpdateManager instance, assuming ownership on the provided | 
| Alex Deymo | 680d022 | 2014-04-24 21:00:08 -0700 | [diff] [blame] | 58 | // |state|. | 
| Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 59 | UpdateManager(base::TimeDelta evaluation_timeout, | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 60 | base::TimeDelta expiration_timeout, | 
|  | 61 | State* state); | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 62 |  | 
| Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 63 | virtual ~UpdateManager(); | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 64 |  | 
|  | 65 | // PolicyRequest() evaluates the given policy with the provided arguments and | 
|  | 66 | // 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] | 67 | // Policy class for the policy request to call. The UpdateManager will call | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 68 | // this method on the right policy. The pointer |result| must not be null | 
|  | 69 | // and the remaining |args| depend on the arguments required by the passed | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 70 | // |policy_method|. | 
|  | 71 | // | 
|  | 72 | // When the policy request succeeds, the |result| is set and the method | 
| Gilad Arnold | 897b5e5 | 2014-05-21 09:37:18 -0700 | [diff] [blame] | 73 | // returns EvalStatus::kSucceeded, otherwise, the |result| may not be set.  A | 
|  | 74 | // policy called with this method should not block (i.e. return | 
|  | 75 | // EvalStatus::kAskMeAgainLater), which is considered a programming error. On | 
|  | 76 | // failure, EvalStatus::kFailed is returned. | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 77 | // | 
|  | 78 | // An example call to this method is: | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 79 | //   um.PolicyRequest(&Policy::SomePolicyMethod, &bool_result, arg1, arg2); | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 80 | template <typename R, typename... ActualArgs, typename... ExpectedArgs> | 
| Alex Deymo | e75e025 | 2014-04-08 14:00:11 -0700 | [diff] [blame] | 81 | EvalStatus PolicyRequest( | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 82 | EvalStatus (Policy::*policy_method)( | 
|  | 83 | EvaluationContext*, State*, std::string*, R*, ExpectedArgs...) const, | 
|  | 84 | R* result, | 
|  | 85 | ActualArgs...); | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 86 |  | 
| Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 87 | // Evaluates the given |policy_method| policy with the provided |args| | 
|  | 88 | // arguments and calls the |callback| callback with the result when done. | 
|  | 89 | // | 
|  | 90 | // If the policy implementation should block, returning a | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 91 | // EvalStatus::kAskMeAgainLater status the Update Manager will re-evaluate the | 
| Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 92 | // policy until another status is returned. If the policy implementation based | 
|  | 93 | // its return value solely on const variables, the callback will be called | 
| Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 94 | // with the EvalStatus::kAskMeAgainLater status (which indicates an error). | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 95 | template <typename R, typename... ActualArgs, typename... ExpectedArgs> | 
| Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 96 | void AsyncPolicyRequest( | 
|  | 97 | base::Callback<void(EvalStatus, const R& result)> callback, | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 98 | EvalStatus (Policy::*policy_method)( | 
|  | 99 | EvaluationContext*, State*, std::string*, R*, ExpectedArgs...) const, | 
| Gilad Arnold | 13a8243 | 2014-05-19 12:52:44 -0700 | [diff] [blame] | 100 | ActualArgs... args); | 
| Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 101 |  | 
| Jae Hoon Kim | 504c3cb | 2019-07-02 11:17:24 -0700 | [diff] [blame] | 102 | void AsyncPolicyRequestUpdateCheckAllowed( | 
|  | 103 | base::Callback<void(EvalStatus, const UpdateCheckParams& result)> | 
|  | 104 | callback, | 
|  | 105 | EvalStatus (Policy::*policy_method)(EvaluationContext*, | 
|  | 106 | State*, | 
|  | 107 | std::string*, | 
|  | 108 | UpdateCheckParams*) const) override; | 
|  | 109 |  | 
| Alex Deymo | 94c0616 | 2014-03-21 20:34:46 -0700 | [diff] [blame] | 110 | protected: | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 111 | // The UpdateManager receives ownership of the passed Policy instance. | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 112 | void set_policy(const Policy* policy) { policy_.reset(policy); } | 
| Alex Deymo | 94c0616 | 2014-03-21 20:34:46 -0700 | [diff] [blame] | 113 |  | 
| Alex Deymo | 680d022 | 2014-04-24 21:00:08 -0700 | [diff] [blame] | 114 | // State getter used for testing. | 
|  | 115 | State* state() { return state_.get(); } | 
|  | 116 |  | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 117 | private: | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 118 | FRIEND_TEST(UmUpdateManagerTest, PolicyRequestCallsPolicy); | 
|  | 119 | FRIEND_TEST(UmUpdateManagerTest, PolicyRequestCallsDefaultOnError); | 
| Gilad Arnold | 897b5e5 | 2014-05-21 09:37:18 -0700 | [diff] [blame] | 120 | FRIEND_TEST(UmUpdateManagerTest, PolicyRequestDoesntBlockDeathTest); | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 121 | FRIEND_TEST(UmUpdateManagerTest, AsyncPolicyRequestDelaysEvaluation); | 
| Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 122 | FRIEND_TEST(UmUpdateManagerTest, AsyncPolicyRequestTimeoutDoesNotFire); | 
| Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 123 | FRIEND_TEST(UmUpdateManagerTest, AsyncPolicyRequestTimesOut); | 
| Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 124 |  | 
| Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 125 | // EvaluatePolicy() evaluates the passed |policy_method| method on the current | 
|  | 126 | // policy with the given |args| arguments. If the method fails, the default | 
|  | 127 | // policy is used instead. | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 128 | template <typename R, typename... Args> | 
| Alex Deymo | e75e025 | 2014-04-08 14:00:11 -0700 | [diff] [blame] | 129 | EvalStatus EvaluatePolicy( | 
|  | 130 | EvaluationContext* ec, | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 131 | EvalStatus (Policy::*policy_method)( | 
|  | 132 | EvaluationContext*, State*, std::string*, R*, Args...) const, | 
|  | 133 | R* result, | 
|  | 134 | Args... args); | 
| Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 135 |  | 
|  | 136 | // OnPolicyReadyToEvaluate() is called by the main loop when the evaluation | 
|  | 137 | // of the given |policy_method| should be executed. If the evaluation finishes | 
|  | 138 | // the |callback| callback is called passing the |result| and the |status| | 
|  | 139 | // returned by the policy. If the evaluation returns an | 
|  | 140 | // EvalStatus::kAskMeAgainLater state, the |callback| will NOT be called and | 
|  | 141 | // the evaluation will be re-scheduled to be called later. | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 142 | template <typename R, typename... Args> | 
| Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 143 | void OnPolicyReadyToEvaluate( | 
| Amin Hassani | a2c8b92 | 2019-08-14 19:41:03 -0700 | [diff] [blame] | 144 | std::shared_ptr<EvaluationContext> ec, | 
| Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 145 | base::Callback<void(EvalStatus status, const R& result)> callback, | 
| Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 146 | EvalStatus (Policy::*policy_method)( | 
|  | 147 | EvaluationContext*, State*, std::string*, R*, Args...) const, | 
| Alex Deymo | e75e025 | 2014-04-08 14:00:11 -0700 | [diff] [blame] | 148 | Args... args); | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 149 |  | 
| Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 150 | // Unregisters (removes from repo) a previously created EvaluationContext. | 
|  | 151 | void UnregisterEvalContext(EvaluationContext* ec); | 
|  | 152 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 153 | // 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] | 154 | // policy implementations are not allowed to persist state on this class. | 
| Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 155 | std::unique_ptr<const Policy> policy_; | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 156 |  | 
|  | 157 | // 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] | 158 | // a policy implementation fails with EvalStatus::kFailed. | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 159 | const DefaultPolicy default_policy_; | 
|  | 160 |  | 
| Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 161 | // State Providers. | 
| Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 162 | std::unique_ptr<State> state_; | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 163 |  | 
| Gilad Arnold | b227199 | 2014-06-19 12:35:24 -0700 | [diff] [blame] | 164 | // Timeout for a policy evaluation. | 
|  | 165 | const base::TimeDelta evaluation_timeout_; | 
|  | 166 |  | 
| Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 167 | // Timeout for expiration of the evaluation context, used for async requests. | 
|  | 168 | const base::TimeDelta expiration_timeout_; | 
|  | 169 |  | 
| Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 170 | // Repository of previously created EvaluationContext objects. These are being | 
|  | 171 | // unregistered (and the reference released) when the context is being | 
|  | 172 | // destructed; alternatively, when the UpdateManager instance is destroyed, it | 
|  | 173 | // will remove all pending events associated with all outstanding contexts | 
|  | 174 | // (which should, in turn, trigger their destruction). | 
| Amin Hassani | a2c8b92 | 2019-08-14 19:41:03 -0700 | [diff] [blame] | 175 | std::set<std::shared_ptr<EvaluationContext>> ec_repo_; | 
| Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 176 |  | 
|  | 177 | base::WeakPtrFactory<UpdateManager> weak_ptr_factory_; | 
|  | 178 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 179 | DISALLOW_COPY_AND_ASSIGN(UpdateManager); | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 180 | }; | 
|  | 181 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 182 | }  // namespace chromeos_update_manager | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 183 |  | 
|  | 184 | // Include the implementation of the template methods. | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 185 | #include "update_engine/update_manager/update_manager-inl.h" | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 186 |  | 
| Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 187 | #endif  // UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_H_ |