lin
2025-07-31 065ea569db06206874bbfa18eb25ff6121aec09b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// Copyright (C) 2017 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
 
#include "update_engine/update_manager/android_things_policy.h"
 
#include <string>
#include <vector>
 
#include <base/logging.h>
#include <base/time/time.h>
 
#include "update_engine/update_manager/api_restricted_downloads_policy_impl.h"
#include "update_engine/update_manager/enough_slots_ab_updates_policy_impl.h"
#include "update_engine/update_manager/interactive_update_policy_impl.h"
#include "update_engine/update_manager/official_build_check_policy_impl.h"
 
using base::Time;
using chromeos_update_engine::ErrorCode;
using std::string;
using std::vector;
 
namespace chromeos_update_manager {
 
const NextUpdateCheckPolicyConstants
    AndroidThingsPolicy::kNextUpdateCheckPolicyConstants = {
        .timeout_initial_interval = 7 * 60,
        .timeout_periodic_interval = 5 * 60 * 60,
        .timeout_max_backoff_interval = 26 * 60 * 60,
        .timeout_regular_fuzz = 10 * 60,
        .attempt_backoff_max_interval_in_days = 16,
        .attempt_backoff_fuzz_in_hours = 12,
};
 
EvalStatus AndroidThingsPolicy::UpdateCheckAllowed(
    EvaluationContext* ec,
    State* state,
    string* error,
    UpdateCheckParams* result) const {
  // Set the default return values.
  result->updates_enabled = true;
  result->target_channel.clear();
  result->target_version_prefix.clear();
  result->rollback_allowed = false;
  result->rollback_allowed_milestones = -1;
  result->interactive = false;
 
  // Build a list of policies to consult.  Note that each policy may modify the
  // result structure, even if it signals kContinue.
  EnoughSlotsAbUpdatesPolicyImpl enough_slots_ab_updates_policy;
  OnlyUpdateOfficialBuildsPolicyImpl only_update_official_builds_policy;
  InteractiveUpdatePolicyImpl interactive_update_policy;
  NextUpdateCheckTimePolicyImpl next_update_check_time_policy(
      kNextUpdateCheckPolicyConstants);
 
  vector<Policy const*> policies_to_consult = {
      // Do not perform any updates if there are not enough slots to do
      // A/B updates
      &enough_slots_ab_updates_policy,
 
      // Check to see if an interactive update was requested.
      &interactive_update_policy,
 
      // Unofficial builds should not perform periodic update checks.
      &only_update_official_builds_policy,
 
      // Ensure that periodic update checks are timed properly.
      &next_update_check_time_policy,
  };
 
  // Now that the list of policy implementations, and the order to consult them,
  // as been setup, do that.  If none of the policies make a definitive
  // decisions about whether or not to check for updates, then allow the update
  // check to happen.
  EvalStatus status = ConsultPolicies(policies_to_consult,
                                      &Policy::UpdateCheckAllowed,
                                      ec,
                                      state,
                                      error,
                                      result);
  if (status != EvalStatus::kContinue) {
    return status;
  } else {
    // It is time to check for an update.
    LOG(INFO) << "Allowing update check.";
    return EvalStatus::kSucceeded;
  }
}
 
// Uses the |UpdateRestrictions| to determine if the download and apply can
// occur at this time.
EvalStatus AndroidThingsPolicy::UpdateCanBeApplied(
    EvaluationContext* ec,
    State* state,
    string* error,
    ErrorCode* result,
    chromeos_update_engine::InstallPlan* install_plan) const {
  // Build a list of policies to consult.  Note that each policy may modify the
  // result structure, even if it signals kContinue.
  ApiRestrictedDownloadsPolicyImpl api_restricted_downloads_policy;
 
  vector<Policy const*> policies_to_consult = {
      // Do not apply the update if all updates are restricted by the API.
      &api_restricted_downloads_policy,
  };
 
  // Now that the list of policy implementations, and the order to consult them,
  // as been setup, do that.  If none of the policies make a definitive
  // decisions about whether or not to check for updates, then allow the update
  // check to happen.
  EvalStatus status = ConsultPolicies(policies_to_consult,
                                      &Policy::UpdateCanBeApplied,
                                      ec,
                                      state,
                                      error,
                                      result,
                                      install_plan);
  if (EvalStatus::kContinue != status) {
    return status;
  } else {
    // The update can proceed.
    LOG(INFO) << "Allowing update to be applied.";
    *result = ErrorCode::kSuccess;
    return EvalStatus::kSucceeded;
  }
}
 
// Always returns |EvalStatus::kSucceeded|
EvalStatus AndroidThingsPolicy::UpdateCanStart(EvaluationContext* ec,
                                               State* state,
                                               string* error,
                                               UpdateDownloadParams* result,
                                               UpdateState update_state) const {
  // Update is good to go.
  result->update_can_start = true;
  return EvalStatus::kSucceeded;
}
 
// Always returns |EvalStatus::kSucceeded|
EvalStatus AndroidThingsPolicy::UpdateDownloadAllowed(EvaluationContext* ec,
                                                      State* state,
                                                      string* error,
                                                      bool* result) const {
  // By default, we allow updates.
  *result = true;
  return EvalStatus::kSucceeded;
}
 
// P2P is always disabled.  Returns |result|==|false| and
// |EvalStatus::kSucceeded|
EvalStatus AndroidThingsPolicy::P2PEnabled(EvaluationContext* ec,
                                           State* state,
                                           string* error,
                                           bool* result) const {
  *result = false;
  return EvalStatus::kSucceeded;
}
 
// This will return immediately with |EvalStatus::kSucceeded| and set
// |result|==|false|
EvalStatus AndroidThingsPolicy::P2PEnabledChanged(EvaluationContext* ec,
                                                  State* state,
                                                  string* error,
                                                  bool* result,
                                                  bool prev_result) const {
  *result = false;
  return EvalStatus::kSucceeded;
}
 
}  // namespace chromeos_update_manager