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 | // |
Gilad Arnold | a23e408 | 2014-07-17 11:40:43 -0700 | [diff] [blame] | 16 | |
| 17 | #include "update_engine/update_manager/default_policy.h" |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | // A fixed minimum interval between consecutive allowed update checks. This |
| 22 | // needs to be long enough to prevent busywork and/or DDoS attacks on Omaha, but |
| 23 | // at the same time short enough to allow the machine to update itself |
| 24 | // reasonably soon. |
| 25 | const int kCheckIntervalInSeconds = 15 * 60; |
| 26 | |
| 27 | } // namespace |
| 28 | |
| 29 | namespace chromeos_update_manager { |
| 30 | |
| 31 | DefaultPolicy::DefaultPolicy(chromeos_update_engine::ClockInterface* clock) |
| 32 | : clock_(clock), aux_state_(new DefaultPolicyState()) {} |
| 33 | |
| 34 | EvalStatus DefaultPolicy::UpdateCheckAllowed( |
| 35 | EvaluationContext* ec, State* state, std::string* error, |
| 36 | UpdateCheckParams* result) const { |
| 37 | result->updates_enabled = true; |
| 38 | result->target_channel.clear(); |
Gilad Arnold | d4b3032 | 2014-07-21 15:35:27 -0700 | [diff] [blame] | 39 | result->target_version_prefix.clear(); |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 40 | result->is_interactive = false; |
Gilad Arnold | a23e408 | 2014-07-17 11:40:43 -0700 | [diff] [blame] | 41 | |
| 42 | // Ensure that the minimum interval is set. If there's no clock, this defaults |
| 43 | // to always allowing the update. |
| 44 | if (!aux_state_->IsLastCheckAllowedTimeSet() || |
| 45 | ec->IsMonotonicTimeGreaterThan( |
| 46 | aux_state_->last_check_allowed_time() + |
| 47 | base::TimeDelta::FromSeconds(kCheckIntervalInSeconds))) { |
| 48 | if (clock_) |
| 49 | aux_state_->set_last_check_allowed_time(clock_->GetMonotonicTime()); |
| 50 | return EvalStatus::kSucceeded; |
| 51 | } |
| 52 | |
| 53 | return EvalStatus::kAskMeAgainLater; |
| 54 | } |
| 55 | |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 56 | EvalStatus DefaultPolicy::UpdateCanStart( |
| 57 | EvaluationContext* ec, |
| 58 | State* state, |
| 59 | std::string* error, |
| 60 | UpdateDownloadParams* result, |
Gilad Arnold | d78caf9 | 2014-09-24 09:28:14 -0700 | [diff] [blame] | 61 | const UpdateState update_state) const { |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 62 | result->update_can_start = true; |
| 63 | result->cannot_start_reason = UpdateCannotStartReason::kUndefined; |
| 64 | result->download_url_idx = 0; |
Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 65 | result->download_url_allowed = true; |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 66 | result->download_url_num_errors = 0; |
Gilad Arnold | b2f9919 | 2014-10-07 13:01:52 -0700 | [diff] [blame] | 67 | result->p2p_downloading_allowed = false; |
| 68 | result->p2p_sharing_allowed = false; |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 69 | result->do_increment_failures = false; |
| 70 | result->backoff_expiry = base::Time(); |
| 71 | result->scatter_wait_period = base::TimeDelta(); |
| 72 | result->scatter_check_threshold = 0; |
| 73 | return EvalStatus::kSucceeded; |
| 74 | } |
| 75 | |
| 76 | EvalStatus DefaultPolicy::UpdateDownloadAllowed( |
| 77 | EvaluationContext* ec, |
| 78 | State* state, |
| 79 | std::string* error, |
| 80 | bool* result) const { |
| 81 | *result = true; |
| 82 | return EvalStatus::kSucceeded; |
| 83 | } |
| 84 | |
Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 85 | EvalStatus DefaultPolicy::P2PEnabled( |
| 86 | EvaluationContext* ec, |
| 87 | State* state, |
| 88 | std::string* error, |
| 89 | bool* result) const { |
| 90 | *result = false; |
| 91 | return EvalStatus::kSucceeded; |
| 92 | } |
| 93 | |
| 94 | EvalStatus DefaultPolicy::P2PEnabledChanged( |
| 95 | EvaluationContext* ec, |
| 96 | State* state, |
| 97 | std::string* error, |
| 98 | bool* result, |
| 99 | bool prev_result) const { |
| 100 | // This policy will always prohibit P2P, so this is signaling to the caller |
| 101 | // that the decision is final (because the current value is the same as the |
| 102 | // previous one) and there's no need to issue another call. |
| 103 | *result = false; |
| 104 | return EvalStatus::kSucceeded; |
| 105 | } |
| 106 | |
Gilad Arnold | a23e408 | 2014-07-17 11:40:43 -0700 | [diff] [blame] | 107 | } // namespace chromeos_update_manager |