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 using chromeos_update_engine::ErrorCode;
20 using chromeos_update_engine::InstallPlan;
21 
22 namespace {
23 
24 // A fixed minimum interval between consecutive allowed update checks. This
25 // needs to be long enough to prevent busywork and/or DDoS attacks on Omaha, but
26 // at the same time short enough to allow the machine to update itself
27 // reasonably soon.
28 const int kCheckIntervalInSeconds = 15 * 60;
29 
30 }  // namespace
31 
32 namespace chromeos_update_manager {
33 
34 DefaultPolicy::DefaultPolicy(chromeos_update_engine::ClockInterface* clock)
35     : clock_(clock), aux_state_(new DefaultPolicyState()) {}
36 
37 EvalStatus DefaultPolicy::UpdateCheckAllowed(EvaluationContext* ec,
38                                              State* state,
39                                              std::string* error,
40                                              UpdateCheckParams* result) const {
41   result->updates_enabled = true;
42   result->target_channel.clear();
43   result->target_version_prefix.clear();
44   result->rollback_allowed = false;
45   result->rollback_allowed_milestones = -1;  // No version rolls should happen.
46   result->interactive = false;
47 
48   // Ensure that the minimum interval is set. If there's no clock, this defaults
49   // to always allowing the update.
50   if (!aux_state_->IsLastCheckAllowedTimeSet() ||
51       ec->IsMonotonicTimeGreaterThan(
52           aux_state_->last_check_allowed_time() +
53           base::TimeDelta::FromSeconds(kCheckIntervalInSeconds))) {
54     if (clock_)
55       aux_state_->set_last_check_allowed_time(clock_->GetMonotonicTime());
56     return EvalStatus::kSucceeded;
57   }
58 
59   return EvalStatus::kAskMeAgainLater;
60 }
61 
62 EvalStatus DefaultPolicy::UpdateCanBeApplied(EvaluationContext* ec,
63                                              State* state,
64                                              std::string* error,
65                                              ErrorCode* result,
66                                              InstallPlan* install_plan) const {
67   *result = ErrorCode::kSuccess;
68   return EvalStatus::kSucceeded;
69 }
70 
71 EvalStatus DefaultPolicy::UpdateCanStart(EvaluationContext* ec,
72                                          State* state,
73                                          std::string* error,
74                                          UpdateDownloadParams* result,
75                                          const UpdateState update_state) const {
76   result->update_can_start = true;
77   result->cannot_start_reason = UpdateCannotStartReason::kUndefined;
78   result->download_url_idx = 0;
79   result->download_url_allowed = true;
80   result->download_url_num_errors = 0;
81   result->p2p_downloading_allowed = false;
82   result->p2p_sharing_allowed = false;
83   result->do_increment_failures = false;
84   result->backoff_expiry = base::Time();
85   result->scatter_wait_period = base::TimeDelta();
86   result->scatter_check_threshold = 0;
87   return EvalStatus::kSucceeded;
88 }
89 
90 EvalStatus DefaultPolicy::UpdateDownloadAllowed(EvaluationContext* ec,
91                                                 State* state,
92                                                 std::string* error,
93                                                 bool* result) const {
94   *result = true;
95   return EvalStatus::kSucceeded;
96 }
97 
98 EvalStatus DefaultPolicy::P2PEnabled(EvaluationContext* ec,
99                                      State* state,
100                                      std::string* error,
101                                      bool* result) const {
102   *result = false;
103   return EvalStatus::kSucceeded;
104 }
105 
106 EvalStatus DefaultPolicy::P2PEnabledChanged(EvaluationContext* ec,
107                                             State* state,
108                                             std::string* error,
109                                             bool* result,
110                                             bool prev_result) const {
111   // This policy will always prohibit P2P, so this is signaling to the caller
112   // that the decision is final (because the current value is the same as the
113   // previous one) and there's no need to issue another call.
114   *result = false;
115   return EvalStatus::kSucceeded;
116 }
117 
118 }  // namespace chromeos_update_manager
119