blob: 099c07cef3e2f5f4297846736d59ae8c841b1f4d [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 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//
Darin Petkova4a8a8c2010-07-15 22:21:12 -070016
Gilad Arnoldcf175a02014-07-10 16:48:47 -070017#ifndef UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H_
18#define UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H_
Darin Petkova4a8a8c2010-07-15 22:21:12 -070019
Ben Chan9abb7632014-08-07 00:10:53 -070020#include <stdint.h>
21
Darin Petkova4a8a8c2010-07-15 22:21:12 -070022#include <string>
23
Ben Chan05735a12014-09-03 07:48:22 -070024#include <base/macros.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070025#include <base/time/time.h>
Darin Petkov49d91322010-10-25 16:34:58 -070026#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkova4a8a8c2010-07-15 22:21:12 -070027
Alex Deymoac41a822015-09-15 20:52:53 -070028#include "update_engine/platform_constants.h"
29
Darin Petkova4a8a8c2010-07-15 22:21:12 -070030// This gathers local system information and prepares info used by the
31// Omaha request action.
32
33namespace chromeos_update_engine {
34
Jay Srinivasanae4697c2013-03-18 17:08:08 -070035class SystemState;
Jay Srinivasan0a708742012-03-20 11:26:12 -070036
Jay Srinivasanae4697c2013-03-18 17:08:08 -070037// This class encapsulates the data Omaha gets for the request, along with
38// essential state needed for the processing of the request/response. The
39// strings in this struct should not be XML escaped.
40//
Alex Vakulenkod2779df2014-06-16 13:19:00 -070041// TODO(jaysri): chromium-os:39752 tracks the need to rename this class to
Jay Srinivasanae4697c2013-03-18 17:08:08 -070042// reflect its lifetime more appropriately.
43class OmahaRequestParams {
44 public:
Alex Vakulenkod2779df2014-06-16 13:19:00 -070045 explicit OmahaRequestParams(SystemState* system_state)
Jay Srinivasanae4697c2013-03-18 17:08:08 -070046 : system_state_(system_state),
Alex Deymoac41a822015-09-15 20:52:53 -070047 os_platform_(constants::kOmahaPlatformName),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070048 os_version_(kOsVersion),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070049 board_app_id_(kAppId),
Jay Srinivasandb0acdf2013-04-02 14:47:45 -070050 canary_app_id_(kAppId),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070051 delta_okay_(true),
52 interactive_(false),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070053 wall_clock_based_wait_enabled_(false),
54 update_check_count_wait_enabled_(false),
55 min_update_checks_needed_(kDefaultMinUpdateChecks),
56 max_update_checks_allowed_(kDefaultMaxUpdateChecks),
57 is_powerwash_allowed_(false),
58 force_lock_down_(false),
Gilad Arnold74b5f552014-10-07 08:17:16 -070059 forced_lock_down_(false) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -070060 InitFromLsbValue();
61 }
Jay Srinivasan0a708742012-03-20 11:26:12 -070062
Jay Srinivasanae4697c2013-03-18 17:08:08 -070063 OmahaRequestParams(SystemState* system_state,
64 const std::string& in_os_platform,
Darin Petkova4a8a8c2010-07-15 22:21:12 -070065 const std::string& in_os_version,
66 const std::string& in_os_sp,
67 const std::string& in_os_board,
68 const std::string& in_app_id,
69 const std::string& in_app_version,
70 const std::string& in_app_lang,
Jay Srinivasanae4697c2013-03-18 17:08:08 -070071 const std::string& in_target_channel,
72 const std::string& in_hwid,
Chris Sosac1972482013-04-30 22:31:10 -070073 const std::string& in_fw_version,
74 const std::string& in_ec_version,
Jay Srinivasan0a708742012-03-20 11:26:12 -070075 bool in_delta_okay,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -080076 bool in_interactive,
Jay Srinivasan0a708742012-03-20 11:26:12 -070077 const std::string& in_update_url,
Gilad Arnold74b5f552014-10-07 08:17:16 -070078 const std::string& in_target_version_prefix)
Jay Srinivasanae4697c2013-03-18 17:08:08 -070079 : system_state_(system_state),
80 os_platform_(in_os_platform),
81 os_version_(in_os_version),
82 os_sp_(in_os_sp),
83 os_board_(in_os_board),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070084 board_app_id_(in_app_id),
Jay Srinivasandb0acdf2013-04-02 14:47:45 -070085 canary_app_id_(in_app_id),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070086 app_version_(in_app_version),
87 app_lang_(in_app_lang),
88 current_channel_(in_target_channel),
89 target_channel_(in_target_channel),
90 hwid_(in_hwid),
Chris Sosac1972482013-04-30 22:31:10 -070091 fw_version_(in_fw_version),
92 ec_version_(in_ec_version),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070093 delta_okay_(in_delta_okay),
94 interactive_(in_interactive),
95 update_url_(in_update_url),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070096 target_version_prefix_(in_target_version_prefix),
97 wall_clock_based_wait_enabled_(false),
98 update_check_count_wait_enabled_(false),
99 min_update_checks_needed_(kDefaultMinUpdateChecks),
100 max_update_checks_allowed_(kDefaultMaxUpdateChecks),
101 is_powerwash_allowed_(false),
102 force_lock_down_(false),
Gilad Arnold74b5f552014-10-07 08:17:16 -0700103 forced_lock_down_(false) {}
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700104
Alex Deymoe8948702014-11-11 21:44:45 -0800105 virtual ~OmahaRequestParams() = default;
106
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700107 // Setters and getters for the various properties.
108 inline std::string os_platform() const { return os_platform_; }
109 inline std::string os_version() const { return os_version_; }
110 inline std::string os_sp() const { return os_sp_; }
111 inline std::string os_board() const { return os_board_; }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700112 inline std::string board_app_id() const { return board_app_id_; }
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700113 inline std::string canary_app_id() const { return canary_app_id_; }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700114 inline std::string app_lang() const { return app_lang_; }
115 inline std::string hwid() const { return hwid_; }
Chris Sosac1972482013-04-30 22:31:10 -0700116 inline std::string fw_version() const { return fw_version_; }
117 inline std::string ec_version() const { return ec_version_; }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700118
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700119 inline void set_app_version(const std::string& version) {
120 app_version_ = version;
121 }
122 inline std::string app_version() const { return app_version_; }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700123
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700124 inline std::string current_channel() const { return current_channel_; }
125 inline std::string target_channel() const { return target_channel_; }
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700126 inline std::string download_channel() const { return download_channel_; }
Darin Petkov49d91322010-10-25 16:34:58 -0700127
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700128 // Can client accept a delta ?
129 inline void set_delta_okay(bool ok) { delta_okay_ = ok; }
130 inline bool delta_okay() const { return delta_okay_; }
Jay Srinivasan0a708742012-03-20 11:26:12 -0700131
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700132 // True if this is a user-initiated update check.
Chris Sosa968d0572013-08-23 14:46:02 -0700133 inline void set_interactive(bool interactive) { interactive_ = interactive; }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700134 inline bool interactive() const { return interactive_; }
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700135
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700136 inline void set_update_url(const std::string& url) { update_url_ = url; }
137 inline std::string update_url() const { return update_url_; }
138
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700139 inline void set_target_version_prefix(const std::string& prefix) {
140 target_version_prefix_ = prefix;
141 }
142
143 inline std::string target_version_prefix() const {
144 return target_version_prefix_;
145 }
146
147 inline void set_wall_clock_based_wait_enabled(bool enabled) {
148 wall_clock_based_wait_enabled_ = enabled;
149 }
150 inline bool wall_clock_based_wait_enabled() const {
151 return wall_clock_based_wait_enabled_;
152 }
153
154 inline void set_waiting_period(base::TimeDelta period) {
155 waiting_period_ = period;
156 }
157 base::TimeDelta waiting_period() const { return waiting_period_; }
158
159 inline void set_update_check_count_wait_enabled(bool enabled) {
160 update_check_count_wait_enabled_ = enabled;
161 }
162
163 inline bool update_check_count_wait_enabled() const {
164 return update_check_count_wait_enabled_;
165 }
166
Ben Chan9abb7632014-08-07 00:10:53 -0700167 inline void set_min_update_checks_needed(int64_t min) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700168 min_update_checks_needed_ = min;
169 }
Ben Chan9abb7632014-08-07 00:10:53 -0700170 inline int64_t min_update_checks_needed() const {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700171 return min_update_checks_needed_;
172 }
173
Ben Chan9abb7632014-08-07 00:10:53 -0700174 inline void set_max_update_checks_allowed(int64_t max) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700175 max_update_checks_allowed_ = max;
176 }
Ben Chan9abb7632014-08-07 00:10:53 -0700177 inline int64_t max_update_checks_allowed() const {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700178 return max_update_checks_allowed_;
179 }
180
181 // True if we're trying to update to a more stable channel.
182 // i.e. index(target_channel) > index(current_channel).
Alex Deymo560ae1d2014-10-28 02:17:54 -0700183 virtual bool to_more_stable_channel() const;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700184
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700185 // Returns the app id corresponding to the current value of the
186 // download channel.
Alex Deymo560ae1d2014-10-28 02:17:54 -0700187 virtual std::string GetAppId() const;
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700188
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700189 // Suggested defaults
David Pursell02c18642014-11-06 11:26:11 -0800190 static const char kAppId[];
David Pursell02c18642014-11-06 11:26:11 -0800191 static const char kOsVersion[];
David Pursell02c18642014-11-06 11:26:11 -0800192 static const char kUpdateChannelKey[];
193 static const char kIsPowerwashAllowedKey[];
194 static const char kAutoUpdateServerKey[];
Ben Chan9abb7632014-08-07 00:10:53 -0700195 static const int64_t kDefaultMinUpdateChecks = 0;
196 static const int64_t kDefaultMaxUpdateChecks = 8;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700197
Darin Petkov5a7f5652010-07-22 21:40:09 -0700198 // Initializes all the data in the object. Non-empty
199 // |in_app_version| or |in_update_url| prevents automatic detection
200 // of the parameter. Returns true on success, false otherwise.
201 bool Init(const std::string& in_app_version,
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200202 const std::string& in_update_url,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -0800203 bool in_interactive);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700204
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700205 // Permanently changes the release channel to |channel|. Performs a
206 // powerwash, if required and allowed.
207 // Returns true on success, false otherwise. Note: This call will fail if
208 // there's a channel change pending already. This is to serialize all the
209 // channel changes done by the user in order to avoid having to solve
210 // numerous edge cases around ensuring the powerwash happens as intended in
211 // all such cases.
Alex Deymo560ae1d2014-10-28 02:17:54 -0700212 virtual bool SetTargetChannel(const std::string& channel,
213 bool is_powerwash_allowed);
Darin Petkov49d91322010-10-25 16:34:58 -0700214
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700215 // Updates the download channel for this particular attempt from the current
216 // value of target channel. This method takes a "snapshot" of the current
217 // value of target channel and uses it for all subsequent Omaha requests for
218 // this attempt (i.e. initial request as well as download progress/error
219 // event requests). The snapshot will be updated only when either this method
220 // or Init is called again.
Alex Deymo560ae1d2014-10-28 02:17:54 -0700221 virtual void UpdateDownloadChannel();
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700222
Alex Deymo560ae1d2014-10-28 02:17:54 -0700223 virtual bool is_powerwash_allowed() const { return is_powerwash_allowed_; }
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900224
David Pursell02c18642014-11-06 11:26:11 -0800225 // Check if the provided update URL is official, meaning either the default
226 // autoupdate server or the autoupdate autotest server.
227 virtual bool IsUpdateUrlOfficial() const;
228
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700229 // For unit-tests.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700230 void set_root(const std::string& root);
Chris Sosa28e479c2013-07-12 11:39:53 -0700231 void set_current_channel(const std::string& channel) {
232 current_channel_ = channel;
233 }
Gilad Arnold76b2b482014-04-01 13:32:43 -0700234 void set_target_channel(const std::string& channel) {
235 target_channel_ = channel;
236 }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700237
Darin Petkov10d02dd2011-01-10 14:57:39 -0800238 // Enforce security mode for testing purposes.
239 void SetLockDown(bool lock);
Darin Petkov49d91322010-10-25 16:34:58 -0700240
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700241 private:
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700242 FRIEND_TEST(OmahaRequestParamsTest, IsValidChannelTest);
243 FRIEND_TEST(OmahaRequestParamsTest, ShouldLockDownTest);
244 FRIEND_TEST(OmahaRequestParamsTest, ChannelIndexTest);
245 FRIEND_TEST(OmahaRequestParamsTest, LsbPreserveTest);
Chris Sosac1972482013-04-30 22:31:10 -0700246 FRIEND_TEST(OmahaRequestParamsTest, CollectECFWVersionsTest);
Darin Petkov49d91322010-10-25 16:34:58 -0700247
248 // Use a validator that is a non-static member of this class so that its
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700249 // inputs can be mocked in unit tests (e.g., build type for IsValidChannel).
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700250 typedef bool(
251 OmahaRequestParams::*ValueValidator)( // NOLINT(readability/casting)
Darin Petkov49d91322010-10-25 16:34:58 -0700252 const std::string&) const;
253
Darin Petkov10d02dd2011-01-10 14:57:39 -0800254 // Returns true if parameter values should be locked down for security
255 // reasons. If this is an official build running in normal boot mode, all
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700256 // values except the release channel are parsed only from the read-only rootfs
257 // partition and the channel values are restricted to a pre-approved set.
Darin Petkov10d02dd2011-01-10 14:57:39 -0800258 bool ShouldLockDown() const;
Darin Petkov49d91322010-10-25 16:34:58 -0700259
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700260 // Returns true if |channel| is a valid channel, false otherwise. This method
261 // restricts the channel value only if the image is official (see
Darin Petkov49d91322010-10-25 16:34:58 -0700262 // IsOfficialBuild).
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700263 bool IsValidChannel(const std::string& channel) const;
264
265 // Returns the index of the given channel.
266 int GetChannelIndex(const std::string& channel) const;
267
Chris Sosac1972482013-04-30 22:31:10 -0700268 // Returns True if we should store the fw/ec versions based on our hwid_.
269 // Compares hwid to a set of whitelisted prefixes.
270 bool CollectECFWVersions() const;
271
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700272 // These are individual helper methods to initialize the said properties from
273 // the LSB value.
274 void SetTargetChannelFromLsbValue();
275 void SetCurrentChannelFromLsbValue();
276 void SetIsPowerwashAllowedFromLsbValue();
277
278 // Initializes the required properties from the LSB value.
279 void InitFromLsbValue();
Darin Petkov49d91322010-10-25 16:34:58 -0700280
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700281 // Fetches the value for a given key from
Darin Petkova3df55b2010-11-15 13:33:55 -0800282 // /mnt/stateful_partition/etc/lsb-release if possible and |stateful_override|
283 // is true. Failing that, it looks for the key in /etc/lsb-release. If
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700284 // |validator| is non-null, uses it to validate and ignore invalid values.
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700285 std::string GetLsbValue(const std::string& key,
Darin Petkov49d91322010-10-25 16:34:58 -0700286 const std::string& default_value,
Darin Petkova3df55b2010-11-15 13:33:55 -0800287 ValueValidator validator,
288 bool stateful_override) const;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700289
290 // Gets the machine type (e.g. "i686").
291 std::string GetMachineType() const;
292
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700293 // Global system context.
294 SystemState* system_state_;
295
296 // Basic properties of the OS and Application that go into the Omaha request.
297 std::string os_platform_;
298 std::string os_version_;
299 std::string os_sp_;
300 std::string os_board_;
301
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700302 // The board app id identifies the app id for the board irrespective of the
303 // channel that we're on. The canary app id identifies the app id to be used
304 // iff we're in the canary-channel. These values could be different depending
305 // on how the release tools are implemented.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700306 std::string board_app_id_;
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700307 std::string canary_app_id_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700308
309 std::string app_version_;
310 std::string app_lang_;
311
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700312 // The three channel values we deal with.
313 // Current channel: is always the channel from /etc/lsb-release. It never
314 // changes. It's just read in during initialization.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700315 std::string current_channel_;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700316
317 // Target channel: It starts off with the value of current channel. But if
318 // the user changes the channel, then it'll have a different value. If the
319 // user changes multiple times, target channel will always contain the most
320 // recent change and is updated immediately to the user-selected value even
321 // if we're in the middle of a download (as opposed to download channel
322 // which gets updated only at the start of next download)
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700323 std::string target_channel_;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700324
325 // The channel from which we're downloading the payload. This should normally
326 // be the same as target channel. But if the user made another channel change
327 // we started the download, then they'd be different, in which case, we'd
328 // detect elsewhere that the target channel has been changed and cancel the
329 // current download attempt.
330 std::string download_channel_;
331
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700332 std::string hwid_; // Hardware Qualification ID of the client
Chris Sosac1972482013-04-30 22:31:10 -0700333 std::string fw_version_; // Chrome OS Firmware Version.
334 std::string ec_version_; // Chrome OS EC Version.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700335 bool delta_okay_; // If this client can accept a delta
336 bool interactive_; // Whether this is a user-initiated update check
337
338 // The URL to send the Omaha request to.
339 std::string update_url_;
340
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700341 // Prefix of the target OS version that the enterprise wants this device
342 // to be pinned to. It's empty otherwise.
343 std::string target_version_prefix_;
344
345 // True if scattering is enabled, in which case waiting_period_ specifies the
346 // amount of absolute time that we've to wait for before sending a request to
347 // Omaha.
348 bool wall_clock_based_wait_enabled_;
349 base::TimeDelta waiting_period_;
350
351 // True if scattering is enabled to denote the number of update checks
352 // we've to skip before we can send a request to Omaha. The min and max
353 // values establish the bounds for a random number to be chosen within that
354 // range to enable such a wait.
355 bool update_check_count_wait_enabled_;
Ben Chan9abb7632014-08-07 00:10:53 -0700356 int64_t min_update_checks_needed_;
357 int64_t max_update_checks_allowed_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700358
359 // True if we are allowed to do powerwash, if required, on a channel change.
360 bool is_powerwash_allowed_;
361
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700362 // When reading files, prepend root_ to the paths. Useful for testing.
363 std::string root_;
364
Darin Petkov10d02dd2011-01-10 14:57:39 -0800365 // Force security lock down for testing purposes.
366 bool force_lock_down_;
367 bool forced_lock_down_;
Darin Petkov49d91322010-10-25 16:34:58 -0700368
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700369 // TODO(jaysri): Uncomment this after fixing unit tests, as part of
370 // chromium-os:39752
371 // DISALLOW_COPY_AND_ASSIGN(OmahaRequestParams);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700372};
373
374} // namespace chromeos_update_engine
375
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700376#endif // UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H_