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_DEFAULT_POLICY_H_ |
| 18 | #define UPDATE_ENGINE_UPDATE_MANAGER_DEFAULT_POLICY_H_ |
| 19 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 20 | #include <memory> |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 21 | #include <string> |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 22 | |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 23 | #include <base/time/time.h> |
| 24 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame^] | 25 | #include "update_engine/common/clock_interface.h" |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 26 | #include "update_engine/update_manager/policy.h" |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 27 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 28 | namespace chromeos_update_manager { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 29 | |
Gilad Arnold | a23e408 | 2014-07-17 11:40:43 -0700 | [diff] [blame] | 30 | // Auxiliary state class for DefaultPolicy evaluations. |
| 31 | // |
| 32 | // IMPORTANT: The use of a state object in policies is generally forbidden, as |
| 33 | // it was a design decision to keep policy calls side-effect free. We make an |
| 34 | // exception here to ensure that DefaultPolicy indeed serves as a safe (and |
| 35 | // secure) fallback option. This practice should be avoided when imlpementing |
| 36 | // other policies. |
| 37 | class DefaultPolicyState { |
| 38 | public: |
| 39 | DefaultPolicyState() {} |
| 40 | |
| 41 | bool IsLastCheckAllowedTimeSet() const { |
| 42 | return last_check_allowed_time_ != base::Time::Max(); |
| 43 | } |
| 44 | |
| 45 | // Sets/returns the point time on the monotonic time scale when the latest |
| 46 | // check allowed was recorded. |
| 47 | void set_last_check_allowed_time(base::Time timestamp) { |
| 48 | last_check_allowed_time_ = timestamp; |
| 49 | } |
| 50 | base::Time last_check_allowed_time() const { |
| 51 | return last_check_allowed_time_; |
| 52 | } |
| 53 | |
| 54 | private: |
| 55 | base::Time last_check_allowed_time_ = base::Time::Max(); |
| 56 | }; |
| 57 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 58 | // The DefaultPolicy is a safe Policy implementation that doesn't fail. The |
| 59 | // values returned by this policy are safe default in case of failure of the |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 60 | // actual policy being used by the UpdateManager. |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 61 | class DefaultPolicy : public Policy { |
| 62 | public: |
Gilad Arnold | a23e408 | 2014-07-17 11:40:43 -0700 | [diff] [blame] | 63 | explicit DefaultPolicy(chromeos_update_engine::ClockInterface* clock); |
| 64 | DefaultPolicy() : DefaultPolicy(nullptr) {} |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 65 | ~DefaultPolicy() override {} |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 66 | |
| 67 | // Policy overrides. |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 68 | EvalStatus UpdateCheckAllowed( |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 69 | EvaluationContext* ec, State* state, std::string* error, |
Gilad Arnold | a23e408 | 2014-07-17 11:40:43 -0700 | [diff] [blame] | 70 | UpdateCheckParams* result) const override; |
Gilad Arnold | af2f6ae | 2014-04-28 14:14:52 -0700 | [diff] [blame] | 71 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 72 | EvalStatus UpdateCanStart( |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 73 | EvaluationContext* ec, State* state, std::string* error, |
Gilad Arnold | 42f253b | 2014-06-25 12:39:17 -0700 | [diff] [blame] | 74 | UpdateDownloadParams* result, |
Gilad Arnold | d78caf9 | 2014-09-24 09:28:14 -0700 | [diff] [blame] | 75 | UpdateState update_state) const override; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 76 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 77 | EvalStatus UpdateDownloadAllowed( |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 78 | EvaluationContext* ec, State* state, std::string* error, |
| 79 | bool* result) const override; |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 80 | |
Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 81 | EvalStatus P2PEnabled( |
| 82 | EvaluationContext* ec, State* state, std::string* error, |
| 83 | bool* result) const override; |
| 84 | |
| 85 | EvalStatus P2PEnabledChanged( |
| 86 | EvaluationContext* ec, State* state, std::string* error, |
| 87 | bool* result, bool prev_result) const override; |
| 88 | |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 89 | protected: |
| 90 | // Policy override. |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 91 | std::string PolicyName() const override { return "DefaultPolicy"; } |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 92 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 93 | private: |
Gilad Arnold | a23e408 | 2014-07-17 11:40:43 -0700 | [diff] [blame] | 94 | // A clock interface. |
| 95 | chromeos_update_engine::ClockInterface* clock_; |
| 96 | |
| 97 | // An auxiliary state object. |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 98 | std::unique_ptr<DefaultPolicyState> aux_state_; |
Gilad Arnold | a23e408 | 2014-07-17 11:40:43 -0700 | [diff] [blame] | 99 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 100 | DISALLOW_COPY_AND_ASSIGN(DefaultPolicy); |
| 101 | }; |
| 102 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 103 | } // namespace chromeos_update_manager |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 104 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 105 | #endif // UPDATE_ENGINE_UPDATE_MANAGER_DEFAULT_POLICY_H_ |