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 //
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
DefaultPolicy(chromeos_update_engine::ClockInterface * clock)31 DefaultPolicy::DefaultPolicy(chromeos_update_engine::ClockInterface* clock)
32 : clock_(clock), aux_state_(new DefaultPolicyState()) {}
33
UpdateCheckAllowed(EvaluationContext * ec,State * state,std::string * error,UpdateCheckParams * result) const34 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();
39 result->target_version_prefix.clear();
40 result->is_interactive = false;
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
UpdateCanStart(EvaluationContext * ec,State * state,std::string * error,UpdateDownloadParams * result,const UpdateState update_state) const56 EvalStatus DefaultPolicy::UpdateCanStart(
57 EvaluationContext* ec,
58 State* state,
59 std::string* error,
60 UpdateDownloadParams* result,
61 const UpdateState update_state) const {
62 result->update_can_start = true;
63 result->cannot_start_reason = UpdateCannotStartReason::kUndefined;
64 result->download_url_idx = 0;
65 result->download_url_allowed = true;
66 result->download_url_num_errors = 0;
67 result->p2p_downloading_allowed = false;
68 result->p2p_sharing_allowed = false;
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
UpdateDownloadAllowed(EvaluationContext * ec,State * state,std::string * error,bool * result) const76 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
P2PEnabled(EvaluationContext * ec,State * state,std::string * error,bool * result) const85 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
P2PEnabledChanged(EvaluationContext * ec,State * state,std::string * error,bool * result,bool prev_result) const94 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
107 } // namespace chromeos_update_manager
108