blob: 1c0ac0da7959622a9cfff40b2b79b571a2d2c524 [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
Darin Petkov49d91322010-10-25 16:34:58 -070012#include <base/basictypes.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),
42 update_disabled_(false),
43 wall_clock_based_wait_enabled_(false),
44 update_check_count_wait_enabled_(false),
45 min_update_checks_needed_(kDefaultMinUpdateChecks),
46 max_update_checks_allowed_(kDefaultMaxUpdateChecks),
47 is_powerwash_allowed_(false),
48 force_lock_down_(false),
David Zeuthen8f191b22013-08-06 12:27:50 -070049 forced_lock_down_(false),
50 use_p2p_for_downloading_(false),
51 use_p2p_for_sharing_(false) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -070052 InitFromLsbValue();
53 }
Jay Srinivasan0a708742012-03-20 11:26:12 -070054
Jay Srinivasanae4697c2013-03-18 17:08:08 -070055 OmahaRequestParams(SystemState* system_state,
56 const std::string& in_os_platform,
Darin Petkova4a8a8c2010-07-15 22:21:12 -070057 const std::string& in_os_version,
58 const std::string& in_os_sp,
59 const std::string& in_os_board,
60 const std::string& in_app_id,
61 const std::string& in_app_version,
62 const std::string& in_app_lang,
Jay Srinivasanae4697c2013-03-18 17:08:08 -070063 const std::string& in_target_channel,
64 const std::string& in_hwid,
Chris Sosac1972482013-04-30 22:31:10 -070065 const std::string& in_fw_version,
66 const std::string& in_ec_version,
Jay Srinivasan0a708742012-03-20 11:26:12 -070067 bool in_delta_okay,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -080068 bool in_interactive,
Jay Srinivasan0a708742012-03-20 11:26:12 -070069 const std::string& in_update_url,
70 bool in_update_disabled,
David Zeuthen8f191b22013-08-06 12:27:50 -070071 const std::string& in_target_version_prefix,
72 bool in_use_p2p_for_downloading,
73 bool in_use_p2p_for_sharing)
Jay Srinivasanae4697c2013-03-18 17:08:08 -070074 : system_state_(system_state),
75 os_platform_(in_os_platform),
76 os_version_(in_os_version),
77 os_sp_(in_os_sp),
78 os_board_(in_os_board),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070079 board_app_id_(in_app_id),
Jay Srinivasandb0acdf2013-04-02 14:47:45 -070080 canary_app_id_(in_app_id),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070081 app_version_(in_app_version),
82 app_lang_(in_app_lang),
83 current_channel_(in_target_channel),
84 target_channel_(in_target_channel),
85 hwid_(in_hwid),
Chris Sosac1972482013-04-30 22:31:10 -070086 fw_version_(in_fw_version),
87 ec_version_(in_ec_version),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070088 delta_okay_(in_delta_okay),
89 interactive_(in_interactive),
90 update_url_(in_update_url),
91 update_disabled_(in_update_disabled),
92 target_version_prefix_(in_target_version_prefix),
93 wall_clock_based_wait_enabled_(false),
94 update_check_count_wait_enabled_(false),
95 min_update_checks_needed_(kDefaultMinUpdateChecks),
96 max_update_checks_allowed_(kDefaultMaxUpdateChecks),
97 is_powerwash_allowed_(false),
98 force_lock_down_(false),
David Zeuthen8f191b22013-08-06 12:27:50 -070099 forced_lock_down_(false),
100 use_p2p_for_downloading_(in_use_p2p_for_downloading),
101 use_p2p_for_sharing_(in_use_p2p_for_sharing) {}
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700102
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700103 // Setters and getters for the various properties.
104 inline std::string os_platform() const { return os_platform_; }
105 inline std::string os_version() const { return os_version_; }
106 inline std::string os_sp() const { return os_sp_; }
107 inline std::string os_board() const { return os_board_; }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700108 inline std::string board_app_id() const { return board_app_id_; }
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700109 inline std::string canary_app_id() const { return canary_app_id_; }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700110 inline std::string app_lang() const { return app_lang_; }
111 inline std::string hwid() const { return hwid_; }
Chris Sosac1972482013-04-30 22:31:10 -0700112 inline std::string fw_version() const { return fw_version_; }
113 inline std::string ec_version() const { return ec_version_; }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700114
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700115 inline void set_app_version(const std::string& version) {
116 app_version_ = version;
117 }
118 inline std::string app_version() const { return app_version_; }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700119
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700120 inline std::string current_channel() const { return current_channel_; }
121 inline std::string target_channel() const { return target_channel_; }
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700122 inline std::string download_channel() const { return download_channel_; }
Darin Petkov49d91322010-10-25 16:34:58 -0700123
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700124 // Can client accept a delta ?
125 inline void set_delta_okay(bool ok) { delta_okay_ = ok; }
126 inline bool delta_okay() const { return delta_okay_; }
Jay Srinivasan0a708742012-03-20 11:26:12 -0700127
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700128 // True if this is a user-initiated update check.
Chris Sosa968d0572013-08-23 14:46:02 -0700129 inline void set_interactive(bool interactive) { interactive_ = interactive; }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700130 inline bool interactive() const { return interactive_; }
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700131
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700132 inline void set_update_url(const std::string& url) { update_url_ = url; }
133 inline std::string update_url() const { return update_url_; }
134
135 inline void set_update_disabled(bool disabled) {
136 update_disabled_ = disabled;
137 }
138 inline bool update_disabled() const { return update_disabled_; }
139
140 inline void set_target_version_prefix(const std::string& prefix) {
141 target_version_prefix_ = prefix;
142 }
143
144 inline std::string target_version_prefix() const {
145 return target_version_prefix_;
146 }
147
148 inline void set_wall_clock_based_wait_enabled(bool enabled) {
149 wall_clock_based_wait_enabled_ = enabled;
150 }
151 inline bool wall_clock_based_wait_enabled() const {
152 return wall_clock_based_wait_enabled_;
153 }
154
155 inline void set_waiting_period(base::TimeDelta period) {
156 waiting_period_ = period;
157 }
158 base::TimeDelta waiting_period() const { return waiting_period_; }
159
160 inline void set_update_check_count_wait_enabled(bool enabled) {
161 update_check_count_wait_enabled_ = enabled;
162 }
163
164 inline bool update_check_count_wait_enabled() const {
165 return update_check_count_wait_enabled_;
166 }
167
Ben Chan9abb7632014-08-07 00:10:53 -0700168 inline void set_min_update_checks_needed(int64_t min) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700169 min_update_checks_needed_ = min;
170 }
Ben Chan9abb7632014-08-07 00:10:53 -0700171 inline int64_t min_update_checks_needed() const {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700172 return min_update_checks_needed_;
173 }
174
Ben Chan9abb7632014-08-07 00:10:53 -0700175 inline void set_max_update_checks_allowed(int64_t max) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700176 max_update_checks_allowed_ = max;
177 }
Ben Chan9abb7632014-08-07 00:10:53 -0700178 inline int64_t max_update_checks_allowed() const {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700179 return max_update_checks_allowed_;
180 }
181
David Zeuthen8f191b22013-08-06 12:27:50 -0700182 inline void set_use_p2p_for_downloading(bool value) {
183 use_p2p_for_downloading_ = value;
184 }
185 inline bool use_p2p_for_downloading() const {
186 return use_p2p_for_downloading_;
187 }
188
189 inline void set_use_p2p_for_sharing(bool value) {
190 use_p2p_for_sharing_ = value;
191 }
192 inline bool use_p2p_for_sharing() const {
193 return use_p2p_for_sharing_;
194 }
195
196 inline void set_p2p_url(const std::string& value) {
197 p2p_url_ = value;
198 }
199 inline std::string p2p_url() const {
200 return p2p_url_;
201 }
202
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700203 // True if we're trying to update to a more stable channel.
204 // i.e. index(target_channel) > index(current_channel).
205 bool to_more_stable_channel() const;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700206
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700207 // Returns the app id corresponding to the current value of the
208 // download channel.
209 std::string GetAppId() const;
210
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700211 // Suggested defaults
212 static const char* const kAppId;
213 static const char* const kOsPlatform;
214 static const char* const kOsVersion;
215 static const char* const kUpdateUrl;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700216 static const char* const kUpdateChannelKey;
217 static const char* const kIsPowerwashAllowedKey;
Ben Chan9abb7632014-08-07 00:10:53 -0700218 static const int64_t kDefaultMinUpdateChecks = 0;
219 static const int64_t kDefaultMaxUpdateChecks = 8;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700220
Darin Petkov5a7f5652010-07-22 21:40:09 -0700221 // Initializes all the data in the object. Non-empty
222 // |in_app_version| or |in_update_url| prevents automatic detection
223 // of the parameter. Returns true on success, false otherwise.
224 bool Init(const std::string& in_app_version,
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200225 const std::string& in_update_url,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -0800226 bool in_interactive);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700227
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700228 // Permanently changes the release channel to |channel|. Performs a
229 // powerwash, if required and allowed.
230 // Returns true on success, false otherwise. Note: This call will fail if
231 // there's a channel change pending already. This is to serialize all the
232 // channel changes done by the user in order to avoid having to solve
233 // numerous edge cases around ensuring the powerwash happens as intended in
234 // all such cases.
235 bool SetTargetChannel(const std::string& channel, bool is_powerwash_allowed);
Darin Petkov49d91322010-10-25 16:34:58 -0700236
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700237 // Updates the download channel for this particular attempt from the current
238 // value of target channel. This method takes a "snapshot" of the current
239 // value of target channel and uses it for all subsequent Omaha requests for
240 // this attempt (i.e. initial request as well as download progress/error
241 // event requests). The snapshot will be updated only when either this method
242 // or Init is called again.
243 void UpdateDownloadChannel();
244
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700245 bool is_powerwash_allowed() const { return is_powerwash_allowed_; }
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900246
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700247 // For unit-tests.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700248 void set_root(const std::string& root);
Chris Sosa28e479c2013-07-12 11:39:53 -0700249 void set_current_channel(const std::string& channel) {
250 current_channel_ = channel;
251 }
Gilad Arnold76b2b482014-04-01 13:32:43 -0700252 void set_target_channel(const std::string& channel) {
253 target_channel_ = channel;
254 }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700255
Darin Petkov10d02dd2011-01-10 14:57:39 -0800256 // Enforce security mode for testing purposes.
257 void SetLockDown(bool lock);
Darin Petkov49d91322010-10-25 16:34:58 -0700258
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700259 private:
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700260 FRIEND_TEST(OmahaRequestParamsTest, IsValidChannelTest);
261 FRIEND_TEST(OmahaRequestParamsTest, ShouldLockDownTest);
262 FRIEND_TEST(OmahaRequestParamsTest, ChannelIndexTest);
263 FRIEND_TEST(OmahaRequestParamsTest, LsbPreserveTest);
Chris Sosac1972482013-04-30 22:31:10 -0700264 FRIEND_TEST(OmahaRequestParamsTest, CollectECFWVersionsTest);
Darin Petkov49d91322010-10-25 16:34:58 -0700265
266 // Use a validator that is a non-static member of this class so that its
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700267 // inputs can be mocked in unit tests (e.g., build type for IsValidChannel).
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700268 typedef bool(
269 OmahaRequestParams::*ValueValidator)( // NOLINT(readability/casting)
Darin Petkov49d91322010-10-25 16:34:58 -0700270 const std::string&) const;
271
Darin Petkov10d02dd2011-01-10 14:57:39 -0800272 // Returns true if parameter values should be locked down for security
273 // reasons. If this is an official build running in normal boot mode, all
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700274 // values except the release channel are parsed only from the read-only rootfs
275 // partition and the channel values are restricted to a pre-approved set.
Darin Petkov10d02dd2011-01-10 14:57:39 -0800276 bool ShouldLockDown() const;
Darin Petkov49d91322010-10-25 16:34:58 -0700277
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700278 // Returns true if |channel| is a valid channel, false otherwise. This method
279 // restricts the channel value only if the image is official (see
Darin Petkov49d91322010-10-25 16:34:58 -0700280 // IsOfficialBuild).
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700281 bool IsValidChannel(const std::string& channel) const;
282
283 // Returns the index of the given channel.
284 int GetChannelIndex(const std::string& channel) const;
285
Chris Sosac1972482013-04-30 22:31:10 -0700286 // Returns True if we should store the fw/ec versions based on our hwid_.
287 // Compares hwid to a set of whitelisted prefixes.
288 bool CollectECFWVersions() const;
289
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700290 // These are individual helper methods to initialize the said properties from
291 // the LSB value.
292 void SetTargetChannelFromLsbValue();
293 void SetCurrentChannelFromLsbValue();
294 void SetIsPowerwashAllowedFromLsbValue();
295
296 // Initializes the required properties from the LSB value.
297 void InitFromLsbValue();
Darin Petkov49d91322010-10-25 16:34:58 -0700298
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700299 // Fetches the value for a given key from
Darin Petkova3df55b2010-11-15 13:33:55 -0800300 // /mnt/stateful_partition/etc/lsb-release if possible and |stateful_override|
301 // is true. Failing that, it looks for the key in /etc/lsb-release. If
Alex Vakulenko072359c2014-07-18 11:41:07 -0700302 // |validator| is non-NULL, uses it to validate and ignore invalid values.
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700303 std::string GetLsbValue(const std::string& key,
Darin Petkov49d91322010-10-25 16:34:58 -0700304 const std::string& default_value,
Darin Petkova3df55b2010-11-15 13:33:55 -0800305 ValueValidator validator,
306 bool stateful_override) const;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700307
308 // Gets the machine type (e.g. "i686").
309 std::string GetMachineType() const;
310
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700311 // Global system context.
312 SystemState* system_state_;
313
314 // Basic properties of the OS and Application that go into the Omaha request.
315 std::string os_platform_;
316 std::string os_version_;
317 std::string os_sp_;
318 std::string os_board_;
319
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700320 // The board app id identifies the app id for the board irrespective of the
321 // channel that we're on. The canary app id identifies the app id to be used
322 // iff we're in the canary-channel. These values could be different depending
323 // on how the release tools are implemented.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700324 std::string board_app_id_;
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700325 std::string canary_app_id_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700326
327 std::string app_version_;
328 std::string app_lang_;
329
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700330 // The three channel values we deal with.
331 // Current channel: is always the channel from /etc/lsb-release. It never
332 // changes. It's just read in during initialization.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700333 std::string current_channel_;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700334
335 // Target channel: It starts off with the value of current channel. But if
336 // the user changes the channel, then it'll have a different value. If the
337 // user changes multiple times, target channel will always contain the most
338 // recent change and is updated immediately to the user-selected value even
339 // if we're in the middle of a download (as opposed to download channel
340 // which gets updated only at the start of next download)
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700341 std::string target_channel_;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700342
343 // The channel from which we're downloading the payload. This should normally
344 // be the same as target channel. But if the user made another channel change
345 // we started the download, then they'd be different, in which case, we'd
346 // detect elsewhere that the target channel has been changed and cancel the
347 // current download attempt.
348 std::string download_channel_;
349
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700350 std::string hwid_; // Hardware Qualification ID of the client
Chris Sosac1972482013-04-30 22:31:10 -0700351 std::string fw_version_; // Chrome OS Firmware Version.
352 std::string ec_version_; // Chrome OS EC Version.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700353 bool delta_okay_; // If this client can accept a delta
354 bool interactive_; // Whether this is a user-initiated update check
355
356 // The URL to send the Omaha request to.
357 std::string update_url_;
358
359 // True if we've been told to block updates per enterprise policy.
360 bool update_disabled_;
361
362 // Prefix of the target OS version that the enterprise wants this device
363 // to be pinned to. It's empty otherwise.
364 std::string target_version_prefix_;
365
366 // True if scattering is enabled, in which case waiting_period_ specifies the
367 // amount of absolute time that we've to wait for before sending a request to
368 // Omaha.
369 bool wall_clock_based_wait_enabled_;
370 base::TimeDelta waiting_period_;
371
372 // True if scattering is enabled to denote the number of update checks
373 // we've to skip before we can send a request to Omaha. The min and max
374 // values establish the bounds for a random number to be chosen within that
375 // range to enable such a wait.
376 bool update_check_count_wait_enabled_;
Ben Chan9abb7632014-08-07 00:10:53 -0700377 int64_t min_update_checks_needed_;
378 int64_t max_update_checks_allowed_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700379
380 // True if we are allowed to do powerwash, if required, on a channel change.
381 bool is_powerwash_allowed_;
382
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700383 // When reading files, prepend root_ to the paths. Useful for testing.
384 std::string root_;
385
Darin Petkov10d02dd2011-01-10 14:57:39 -0800386 // Force security lock down for testing purposes.
387 bool force_lock_down_;
388 bool forced_lock_down_;
Darin Petkov49d91322010-10-25 16:34:58 -0700389
David Zeuthen8f191b22013-08-06 12:27:50 -0700390 // True if we may use p2p to download. This is based on owner
391 // preferences and policy.
392 bool use_p2p_for_downloading_;
393
394 // True if we may use p2p to share. This is based on owner
395 // preferences and policy.
396 bool use_p2p_for_sharing_;
397
398 // An URL to a local peer serving the requested payload or "" if no
399 // such peer is available.
400 std::string p2p_url_;
401
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700402 // TODO(jaysri): Uncomment this after fixing unit tests, as part of
403 // chromium-os:39752
404 // DISALLOW_COPY_AND_ASSIGN(OmahaRequestParams);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700405};
406
407} // namespace chromeos_update_engine
408
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700409#endif // UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H_