blob: 0657649ac270117123ebb047a14436efcd09e6f1 [file] [log] [blame]
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkova4a8a8c2010-07-15 22:21:12 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Gilad Arnoldcf175a02014-07-10 16:48:47 -07005#ifndef UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H_
6#define UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H_
Darin Petkova4a8a8c2010-07-15 22:21:12 -07007
Ben Chan9abb7632014-08-07 00:10:53 -07008#include <stdint.h>
9
Darin Petkova4a8a8c2010-07-15 22:21:12 -070010#include <string>
11
Ben Chan05735a12014-09-03 07:48:22 -070012#include <base/macros.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070013#include <base/time/time.h>
Darin Petkov49d91322010-10-25 16:34:58 -070014#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkova4a8a8c2010-07-15 22:21:12 -070015
16// This gathers local system information and prepares info used by the
17// Omaha request action.
18
19namespace chromeos_update_engine {
20
Jay Srinivasan55f50c22013-01-10 19:24:35 -080021// The default "official" Omaha update URL.
22extern const char* const kProductionOmahaUrl;
23
Jay Srinivasanae4697c2013-03-18 17:08:08 -070024class SystemState;
Jay Srinivasan0a708742012-03-20 11:26:12 -070025
Jay Srinivasanae4697c2013-03-18 17:08:08 -070026// This class encapsulates the data Omaha gets for the request, along with
27// essential state needed for the processing of the request/response. The
28// strings in this struct should not be XML escaped.
29//
Alex Vakulenkod2779df2014-06-16 13:19:00 -070030// TODO(jaysri): chromium-os:39752 tracks the need to rename this class to
Jay Srinivasanae4697c2013-03-18 17:08:08 -070031// reflect its lifetime more appropriately.
32class OmahaRequestParams {
33 public:
Alex Vakulenkod2779df2014-06-16 13:19:00 -070034 explicit OmahaRequestParams(SystemState* system_state)
Jay Srinivasanae4697c2013-03-18 17:08:08 -070035 : system_state_(system_state),
36 os_platform_(kOsPlatform),
37 os_version_(kOsVersion),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070038 board_app_id_(kAppId),
Jay Srinivasandb0acdf2013-04-02 14:47:45 -070039 canary_app_id_(kAppId),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070040 delta_okay_(true),
41 interactive_(false),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070042 wall_clock_based_wait_enabled_(false),
43 update_check_count_wait_enabled_(false),
44 min_update_checks_needed_(kDefaultMinUpdateChecks),
45 max_update_checks_allowed_(kDefaultMaxUpdateChecks),
46 is_powerwash_allowed_(false),
47 force_lock_down_(false),
Gilad Arnold74b5f552014-10-07 08:17:16 -070048 forced_lock_down_(false) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -070049 InitFromLsbValue();
50 }
Jay Srinivasan0a708742012-03-20 11:26:12 -070051
Jay Srinivasanae4697c2013-03-18 17:08:08 -070052 OmahaRequestParams(SystemState* system_state,
53 const std::string& in_os_platform,
Darin Petkova4a8a8c2010-07-15 22:21:12 -070054 const std::string& in_os_version,
55 const std::string& in_os_sp,
56 const std::string& in_os_board,
57 const std::string& in_app_id,
58 const std::string& in_app_version,
59 const std::string& in_app_lang,
Jay Srinivasanae4697c2013-03-18 17:08:08 -070060 const std::string& in_target_channel,
61 const std::string& in_hwid,
Chris Sosac1972482013-04-30 22:31:10 -070062 const std::string& in_fw_version,
63 const std::string& in_ec_version,
Jay Srinivasan0a708742012-03-20 11:26:12 -070064 bool in_delta_okay,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -080065 bool in_interactive,
Jay Srinivasan0a708742012-03-20 11:26:12 -070066 const std::string& in_update_url,
Gilad Arnold74b5f552014-10-07 08:17:16 -070067 const std::string& in_target_version_prefix)
Jay Srinivasanae4697c2013-03-18 17:08:08 -070068 : system_state_(system_state),
69 os_platform_(in_os_platform),
70 os_version_(in_os_version),
71 os_sp_(in_os_sp),
72 os_board_(in_os_board),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070073 board_app_id_(in_app_id),
Jay Srinivasandb0acdf2013-04-02 14:47:45 -070074 canary_app_id_(in_app_id),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070075 app_version_(in_app_version),
76 app_lang_(in_app_lang),
77 current_channel_(in_target_channel),
78 target_channel_(in_target_channel),
79 hwid_(in_hwid),
Chris Sosac1972482013-04-30 22:31:10 -070080 fw_version_(in_fw_version),
81 ec_version_(in_ec_version),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070082 delta_okay_(in_delta_okay),
83 interactive_(in_interactive),
84 update_url_(in_update_url),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070085 target_version_prefix_(in_target_version_prefix),
86 wall_clock_based_wait_enabled_(false),
87 update_check_count_wait_enabled_(false),
88 min_update_checks_needed_(kDefaultMinUpdateChecks),
89 max_update_checks_allowed_(kDefaultMaxUpdateChecks),
90 is_powerwash_allowed_(false),
91 force_lock_down_(false),
Gilad Arnold74b5f552014-10-07 08:17:16 -070092 forced_lock_down_(false) {}
Darin Petkova4a8a8c2010-07-15 22:21:12 -070093
Alex Deymoe8948702014-11-11 21:44:45 -080094 virtual ~OmahaRequestParams() = default;
95
Jay Srinivasanae4697c2013-03-18 17:08:08 -070096 // Setters and getters for the various properties.
97 inline std::string os_platform() const { return os_platform_; }
98 inline std::string os_version() const { return os_version_; }
99 inline std::string os_sp() const { return os_sp_; }
100 inline std::string os_board() const { return os_board_; }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700101 inline std::string board_app_id() const { return board_app_id_; }
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700102 inline std::string canary_app_id() const { return canary_app_id_; }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700103 inline std::string app_lang() const { return app_lang_; }
104 inline std::string hwid() const { return hwid_; }
Chris Sosac1972482013-04-30 22:31:10 -0700105 inline std::string fw_version() const { return fw_version_; }
106 inline std::string ec_version() const { return ec_version_; }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700107
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700108 inline void set_app_version(const std::string& version) {
109 app_version_ = version;
110 }
111 inline std::string app_version() const { return app_version_; }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700112
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700113 inline std::string current_channel() const { return current_channel_; }
114 inline std::string target_channel() const { return target_channel_; }
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700115 inline std::string download_channel() const { return download_channel_; }
Darin Petkov49d91322010-10-25 16:34:58 -0700116
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700117 // Can client accept a delta ?
118 inline void set_delta_okay(bool ok) { delta_okay_ = ok; }
119 inline bool delta_okay() const { return delta_okay_; }
Jay Srinivasan0a708742012-03-20 11:26:12 -0700120
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700121 // True if this is a user-initiated update check.
Chris Sosa968d0572013-08-23 14:46:02 -0700122 inline void set_interactive(bool interactive) { interactive_ = interactive; }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700123 inline bool interactive() const { return interactive_; }
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700124
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700125 inline void set_update_url(const std::string& url) { update_url_ = url; }
126 inline std::string update_url() const { return update_url_; }
127
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700128 inline void set_target_version_prefix(const std::string& prefix) {
129 target_version_prefix_ = prefix;
130 }
131
132 inline std::string target_version_prefix() const {
133 return target_version_prefix_;
134 }
135
136 inline void set_wall_clock_based_wait_enabled(bool enabled) {
137 wall_clock_based_wait_enabled_ = enabled;
138 }
139 inline bool wall_clock_based_wait_enabled() const {
140 return wall_clock_based_wait_enabled_;
141 }
142
143 inline void set_waiting_period(base::TimeDelta period) {
144 waiting_period_ = period;
145 }
146 base::TimeDelta waiting_period() const { return waiting_period_; }
147
148 inline void set_update_check_count_wait_enabled(bool enabled) {
149 update_check_count_wait_enabled_ = enabled;
150 }
151
152 inline bool update_check_count_wait_enabled() const {
153 return update_check_count_wait_enabled_;
154 }
155
Ben Chan9abb7632014-08-07 00:10:53 -0700156 inline void set_min_update_checks_needed(int64_t min) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700157 min_update_checks_needed_ = min;
158 }
Ben Chan9abb7632014-08-07 00:10:53 -0700159 inline int64_t min_update_checks_needed() const {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700160 return min_update_checks_needed_;
161 }
162
Ben Chan9abb7632014-08-07 00:10:53 -0700163 inline void set_max_update_checks_allowed(int64_t max) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700164 max_update_checks_allowed_ = max;
165 }
Ben Chan9abb7632014-08-07 00:10:53 -0700166 inline int64_t max_update_checks_allowed() const {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700167 return max_update_checks_allowed_;
168 }
169
170 // True if we're trying to update to a more stable channel.
171 // i.e. index(target_channel) > index(current_channel).
Alex Deymo560ae1d2014-10-28 02:17:54 -0700172 virtual bool to_more_stable_channel() const;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700173
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700174 // Returns the app id corresponding to the current value of the
175 // download channel.
Alex Deymo560ae1d2014-10-28 02:17:54 -0700176 virtual std::string GetAppId() const;
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700177
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700178 // Suggested defaults
179 static const char* const kAppId;
180 static const char* const kOsPlatform;
181 static const char* const kOsVersion;
182 static const char* const kUpdateUrl;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700183 static const char* const kUpdateChannelKey;
184 static const char* const kIsPowerwashAllowedKey;
Ben Chan9abb7632014-08-07 00:10:53 -0700185 static const int64_t kDefaultMinUpdateChecks = 0;
186 static const int64_t kDefaultMaxUpdateChecks = 8;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700187
Darin Petkov5a7f5652010-07-22 21:40:09 -0700188 // Initializes all the data in the object. Non-empty
189 // |in_app_version| or |in_update_url| prevents automatic detection
190 // of the parameter. Returns true on success, false otherwise.
191 bool Init(const std::string& in_app_version,
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200192 const std::string& in_update_url,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -0800193 bool in_interactive);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700194
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700195 // Permanently changes the release channel to |channel|. Performs a
196 // powerwash, if required and allowed.
197 // Returns true on success, false otherwise. Note: This call will fail if
198 // there's a channel change pending already. This is to serialize all the
199 // channel changes done by the user in order to avoid having to solve
200 // numerous edge cases around ensuring the powerwash happens as intended in
201 // all such cases.
Alex Deymo560ae1d2014-10-28 02:17:54 -0700202 virtual bool SetTargetChannel(const std::string& channel,
203 bool is_powerwash_allowed);
Darin Petkov49d91322010-10-25 16:34:58 -0700204
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700205 // Updates the download channel for this particular attempt from the current
206 // value of target channel. This method takes a "snapshot" of the current
207 // value of target channel and uses it for all subsequent Omaha requests for
208 // this attempt (i.e. initial request as well as download progress/error
209 // event requests). The snapshot will be updated only when either this method
210 // or Init is called again.
Alex Deymo560ae1d2014-10-28 02:17:54 -0700211 virtual void UpdateDownloadChannel();
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700212
Alex Deymo560ae1d2014-10-28 02:17:54 -0700213 virtual bool is_powerwash_allowed() const { return is_powerwash_allowed_; }
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900214
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700215 // For unit-tests.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700216 void set_root(const std::string& root);
Chris Sosa28e479c2013-07-12 11:39:53 -0700217 void set_current_channel(const std::string& channel) {
218 current_channel_ = channel;
219 }
Gilad Arnold76b2b482014-04-01 13:32:43 -0700220 void set_target_channel(const std::string& channel) {
221 target_channel_ = channel;
222 }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700223
Darin Petkov10d02dd2011-01-10 14:57:39 -0800224 // Enforce security mode for testing purposes.
225 void SetLockDown(bool lock);
Darin Petkov49d91322010-10-25 16:34:58 -0700226
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700227 private:
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700228 FRIEND_TEST(OmahaRequestParamsTest, IsValidChannelTest);
229 FRIEND_TEST(OmahaRequestParamsTest, ShouldLockDownTest);
230 FRIEND_TEST(OmahaRequestParamsTest, ChannelIndexTest);
231 FRIEND_TEST(OmahaRequestParamsTest, LsbPreserveTest);
Chris Sosac1972482013-04-30 22:31:10 -0700232 FRIEND_TEST(OmahaRequestParamsTest, CollectECFWVersionsTest);
Darin Petkov49d91322010-10-25 16:34:58 -0700233
234 // Use a validator that is a non-static member of this class so that its
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700235 // inputs can be mocked in unit tests (e.g., build type for IsValidChannel).
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700236 typedef bool(
237 OmahaRequestParams::*ValueValidator)( // NOLINT(readability/casting)
Darin Petkov49d91322010-10-25 16:34:58 -0700238 const std::string&) const;
239
Darin Petkov10d02dd2011-01-10 14:57:39 -0800240 // Returns true if parameter values should be locked down for security
241 // reasons. If this is an official build running in normal boot mode, all
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700242 // values except the release channel are parsed only from the read-only rootfs
243 // partition and the channel values are restricted to a pre-approved set.
Darin Petkov10d02dd2011-01-10 14:57:39 -0800244 bool ShouldLockDown() const;
Darin Petkov49d91322010-10-25 16:34:58 -0700245
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700246 // Returns true if |channel| is a valid channel, false otherwise. This method
247 // restricts the channel value only if the image is official (see
Darin Petkov49d91322010-10-25 16:34:58 -0700248 // IsOfficialBuild).
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700249 bool IsValidChannel(const std::string& channel) const;
250
251 // Returns the index of the given channel.
252 int GetChannelIndex(const std::string& channel) const;
253
Chris Sosac1972482013-04-30 22:31:10 -0700254 // Returns True if we should store the fw/ec versions based on our hwid_.
255 // Compares hwid to a set of whitelisted prefixes.
256 bool CollectECFWVersions() const;
257
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700258 // These are individual helper methods to initialize the said properties from
259 // the LSB value.
260 void SetTargetChannelFromLsbValue();
261 void SetCurrentChannelFromLsbValue();
262 void SetIsPowerwashAllowedFromLsbValue();
263
264 // Initializes the required properties from the LSB value.
265 void InitFromLsbValue();
Darin Petkov49d91322010-10-25 16:34:58 -0700266
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700267 // Fetches the value for a given key from
Darin Petkova3df55b2010-11-15 13:33:55 -0800268 // /mnt/stateful_partition/etc/lsb-release if possible and |stateful_override|
269 // is true. Failing that, it looks for the key in /etc/lsb-release. If
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700270 // |validator| is non-null, uses it to validate and ignore invalid values.
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700271 std::string GetLsbValue(const std::string& key,
Darin Petkov49d91322010-10-25 16:34:58 -0700272 const std::string& default_value,
Darin Petkova3df55b2010-11-15 13:33:55 -0800273 ValueValidator validator,
274 bool stateful_override) const;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700275
276 // Gets the machine type (e.g. "i686").
277 std::string GetMachineType() const;
278
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700279 // Global system context.
280 SystemState* system_state_;
281
282 // Basic properties of the OS and Application that go into the Omaha request.
283 std::string os_platform_;
284 std::string os_version_;
285 std::string os_sp_;
286 std::string os_board_;
287
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700288 // The board app id identifies the app id for the board irrespective of the
289 // channel that we're on. The canary app id identifies the app id to be used
290 // iff we're in the canary-channel. These values could be different depending
291 // on how the release tools are implemented.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700292 std::string board_app_id_;
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700293 std::string canary_app_id_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700294
295 std::string app_version_;
296 std::string app_lang_;
297
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700298 // The three channel values we deal with.
299 // Current channel: is always the channel from /etc/lsb-release. It never
300 // changes. It's just read in during initialization.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700301 std::string current_channel_;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700302
303 // Target channel: It starts off with the value of current channel. But if
304 // the user changes the channel, then it'll have a different value. If the
305 // user changes multiple times, target channel will always contain the most
306 // recent change and is updated immediately to the user-selected value even
307 // if we're in the middle of a download (as opposed to download channel
308 // which gets updated only at the start of next download)
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700309 std::string target_channel_;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700310
311 // The channel from which we're downloading the payload. This should normally
312 // be the same as target channel. But if the user made another channel change
313 // we started the download, then they'd be different, in which case, we'd
314 // detect elsewhere that the target channel has been changed and cancel the
315 // current download attempt.
316 std::string download_channel_;
317
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700318 std::string hwid_; // Hardware Qualification ID of the client
Chris Sosac1972482013-04-30 22:31:10 -0700319 std::string fw_version_; // Chrome OS Firmware Version.
320 std::string ec_version_; // Chrome OS EC Version.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700321 bool delta_okay_; // If this client can accept a delta
322 bool interactive_; // Whether this is a user-initiated update check
323
324 // The URL to send the Omaha request to.
325 std::string update_url_;
326
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700327 // Prefix of the target OS version that the enterprise wants this device
328 // to be pinned to. It's empty otherwise.
329 std::string target_version_prefix_;
330
331 // True if scattering is enabled, in which case waiting_period_ specifies the
332 // amount of absolute time that we've to wait for before sending a request to
333 // Omaha.
334 bool wall_clock_based_wait_enabled_;
335 base::TimeDelta waiting_period_;
336
337 // True if scattering is enabled to denote the number of update checks
338 // we've to skip before we can send a request to Omaha. The min and max
339 // values establish the bounds for a random number to be chosen within that
340 // range to enable such a wait.
341 bool update_check_count_wait_enabled_;
Ben Chan9abb7632014-08-07 00:10:53 -0700342 int64_t min_update_checks_needed_;
343 int64_t max_update_checks_allowed_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700344
345 // True if we are allowed to do powerwash, if required, on a channel change.
346 bool is_powerwash_allowed_;
347
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700348 // When reading files, prepend root_ to the paths. Useful for testing.
349 std::string root_;
350
Darin Petkov10d02dd2011-01-10 14:57:39 -0800351 // Force security lock down for testing purposes.
352 bool force_lock_down_;
353 bool forced_lock_down_;
Darin Petkov49d91322010-10-25 16:34:58 -0700354
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700355 // TODO(jaysri): Uncomment this after fixing unit tests, as part of
356 // chromium-os:39752
357 // DISALLOW_COPY_AND_ASSIGN(OmahaRequestParams);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700358};
359
360} // namespace chromeos_update_engine
361
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700362#endif // UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H_