blob: a57370d4feeb16efe6702b05de7968cb11d05a9f [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 Deymo39910dc2015-11-09 17:04:30 -080028#include "update_engine/common/platform_constants.h"
Alex Deymo85616652015-10-15 18:48:31 -070029#include "update_engine/image_properties.h"
Alex Deymoac41a822015-09-15 20:52:53 -070030
Darin Petkova4a8a8c2010-07-15 22:21:12 -070031// This gathers local system information and prepares info used by the
32// Omaha request action.
33
34namespace chromeos_update_engine {
35
Jay Srinivasanae4697c2013-03-18 17:08:08 -070036class SystemState;
Jay Srinivasan0a708742012-03-20 11:26:12 -070037
Jay Srinivasanae4697c2013-03-18 17:08:08 -070038// This class encapsulates the data Omaha gets for the request, along with
39// essential state needed for the processing of the request/response. The
40// strings in this struct should not be XML escaped.
41//
Alex Vakulenkod2779df2014-06-16 13:19:00 -070042// TODO(jaysri): chromium-os:39752 tracks the need to rename this class to
Jay Srinivasanae4697c2013-03-18 17:08:08 -070043// reflect its lifetime more appropriately.
44class OmahaRequestParams {
45 public:
Alex Vakulenkod2779df2014-06-16 13:19:00 -070046 explicit OmahaRequestParams(SystemState* system_state)
Jay Srinivasanae4697c2013-03-18 17:08:08 -070047 : system_state_(system_state),
Alex Deymoac41a822015-09-15 20:52:53 -070048 os_platform_(constants::kOmahaPlatformName),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070049 os_version_(kOsVersion),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070050 delta_okay_(true),
51 interactive_(false),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070052 wall_clock_based_wait_enabled_(false),
53 update_check_count_wait_enabled_(false),
54 min_update_checks_needed_(kDefaultMinUpdateChecks),
Alex Deymo85616652015-10-15 18:48:31 -070055 max_update_checks_allowed_(kDefaultMaxUpdateChecks) {}
Jay Srinivasan0a708742012-03-20 11:26:12 -070056
Alex Deymo3be05c82015-10-23 11:29:11 -070057 virtual ~OmahaRequestParams();
Alex Deymoe8948702014-11-11 21:44:45 -080058
Jay Srinivasanae4697c2013-03-18 17:08:08 -070059 // Setters and getters for the various properties.
60 inline std::string os_platform() const { return os_platform_; }
61 inline std::string os_version() const { return os_version_; }
62 inline std::string os_sp() const { return os_sp_; }
Alex Deymo85616652015-10-15 18:48:31 -070063 inline std::string os_board() const { return image_props_.board; }
Alex Deymoebf6e122017-03-10 16:12:01 -080064 inline std::string os_build_fingerprint() const {
65 return image_props_.build_fingerprint;
66 }
Sen Jiang1d5d95f2017-05-19 11:33:10 -070067 inline std::string os_build_type() const { return image_props_.build_type; }
Alex Deymo85616652015-10-15 18:48:31 -070068 inline std::string board_app_id() const { return image_props_.product_id; }
69 inline std::string canary_app_id() const {
70 return image_props_.canary_product_id;
71 }
Sen Jiang94a4dec2017-03-28 18:23:35 -070072 inline std::string system_app_id() const { return image_props_.system_id; }
Aaron Wood7dcdedf2017-09-06 17:17:41 -070073 inline void set_system_app_id(const std::string& system_app_id) {
74 image_props_.system_id = system_app_id;
75 }
Sen Jiang297e5832016-03-17 14:45:51 -070076 inline void set_app_id(const std::string& app_id) {
77 image_props_.product_id = app_id;
78 image_props_.canary_product_id = app_id;
79 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -070080 inline std::string app_lang() const { return app_lang_; }
81 inline std::string hwid() const { return hwid_; }
Chris Sosac1972482013-04-30 22:31:10 -070082 inline std::string fw_version() const { return fw_version_; }
83 inline std::string ec_version() const { return ec_version_; }
Darin Petkova4a8a8c2010-07-15 22:21:12 -070084
Jay Srinivasanae4697c2013-03-18 17:08:08 -070085 inline void set_app_version(const std::string& version) {
Alex Deymo85616652015-10-15 18:48:31 -070086 image_props_.version = version;
Jay Srinivasanae4697c2013-03-18 17:08:08 -070087 }
Alex Deymo85616652015-10-15 18:48:31 -070088 inline std::string app_version() const { return image_props_.version; }
Sen Jiang94a4dec2017-03-28 18:23:35 -070089 inline std::string system_version() const {
90 return image_props_.system_version;
91 }
Sen Jiang684c9cd2017-10-17 16:26:45 -070092 inline std::string product_components() const {
93 return image_props_.product_components;
94 }
Sen Jiang8cd42342018-01-31 12:06:59 -080095 inline void set_product_components(const std::string& product_components) {
96 image_props_.product_components = product_components;
97 }
Darin Petkova4a8a8c2010-07-15 22:21:12 -070098
Alex Deymo85616652015-10-15 18:48:31 -070099 inline std::string current_channel() const {
100 return image_props_.current_channel;
101 }
102 inline std::string target_channel() const {
103 return mutable_image_props_.target_channel;
104 }
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700105 inline std::string download_channel() const { return download_channel_; }
Darin Petkov49d91322010-10-25 16:34:58 -0700106
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700107 // Can client accept a delta ?
108 inline void set_delta_okay(bool ok) { delta_okay_ = ok; }
109 inline bool delta_okay() const { return delta_okay_; }
Jay Srinivasan0a708742012-03-20 11:26:12 -0700110
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700111 // True if this is a user-initiated update check.
Chris Sosa968d0572013-08-23 14:46:02 -0700112 inline void set_interactive(bool interactive) { interactive_ = interactive; }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700113 inline bool interactive() const { return interactive_; }
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700114
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700115 inline void set_update_url(const std::string& url) { update_url_ = url; }
116 inline std::string update_url() const { return update_url_; }
117
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700118 inline void set_target_version_prefix(const std::string& prefix) {
119 target_version_prefix_ = prefix;
120 }
121
122 inline std::string target_version_prefix() const {
123 return target_version_prefix_;
124 }
125
126 inline void set_wall_clock_based_wait_enabled(bool enabled) {
127 wall_clock_based_wait_enabled_ = enabled;
128 }
129 inline bool wall_clock_based_wait_enabled() const {
130 return wall_clock_based_wait_enabled_;
131 }
132
133 inline void set_waiting_period(base::TimeDelta period) {
134 waiting_period_ = period;
135 }
136 base::TimeDelta waiting_period() const { return waiting_period_; }
137
138 inline void set_update_check_count_wait_enabled(bool enabled) {
139 update_check_count_wait_enabled_ = enabled;
140 }
141
142 inline bool update_check_count_wait_enabled() const {
143 return update_check_count_wait_enabled_;
144 }
145
Ben Chan9abb7632014-08-07 00:10:53 -0700146 inline void set_min_update_checks_needed(int64_t min) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700147 min_update_checks_needed_ = min;
148 }
Ben Chan9abb7632014-08-07 00:10:53 -0700149 inline int64_t min_update_checks_needed() const {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700150 return min_update_checks_needed_;
151 }
152
Ben Chan9abb7632014-08-07 00:10:53 -0700153 inline void set_max_update_checks_allowed(int64_t max) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700154 max_update_checks_allowed_ = max;
155 }
Ben Chan9abb7632014-08-07 00:10:53 -0700156 inline int64_t max_update_checks_allowed() const {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700157 return max_update_checks_allowed_;
158 }
159
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700160 // Returns the app id corresponding to the current value of the
161 // download channel.
Alex Deymo560ae1d2014-10-28 02:17:54 -0700162 virtual std::string GetAppId() const;
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700163
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700164 // Suggested defaults
David Pursell02c18642014-11-06 11:26:11 -0800165 static const char kOsVersion[];
Ben Chan9abb7632014-08-07 00:10:53 -0700166 static const int64_t kDefaultMinUpdateChecks = 0;
167 static const int64_t kDefaultMaxUpdateChecks = 8;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700168
Darin Petkov5a7f5652010-07-22 21:40:09 -0700169 // Initializes all the data in the object. Non-empty
170 // |in_app_version| or |in_update_url| prevents automatic detection
171 // of the parameter. Returns true on success, false otherwise.
172 bool Init(const std::string& in_app_version,
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200173 const std::string& in_update_url,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -0800174 bool in_interactive);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700175
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700176 // Permanently changes the release channel to |channel|. Performs a
177 // powerwash, if required and allowed.
178 // Returns true on success, false otherwise. Note: This call will fail if
179 // there's a channel change pending already. This is to serialize all the
180 // channel changes done by the user in order to avoid having to solve
181 // numerous edge cases around ensuring the powerwash happens as intended in
182 // all such cases.
Alex Deymo560ae1d2014-10-28 02:17:54 -0700183 virtual bool SetTargetChannel(const std::string& channel,
Alex Deymod942f9d2015-11-06 16:11:50 -0800184 bool is_powerwash_allowed,
185 std::string* error_message);
Darin Petkov49d91322010-10-25 16:34:58 -0700186
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700187 // Updates the download channel for this particular attempt from the current
188 // value of target channel. This method takes a "snapshot" of the current
189 // value of target channel and uses it for all subsequent Omaha requests for
190 // this attempt (i.e. initial request as well as download progress/error
191 // event requests). The snapshot will be updated only when either this method
192 // or Init is called again.
Alex Deymo560ae1d2014-10-28 02:17:54 -0700193 virtual void UpdateDownloadChannel();
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700194
Sen Jiang8500d3a2018-02-08 12:04:05 -0800195 // Returns whether we should powerwash for this update.
196 virtual bool ShouldPowerwash() const;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900197
David Pursell02c18642014-11-06 11:26:11 -0800198 // Check if the provided update URL is official, meaning either the default
199 // autoupdate server or the autoupdate autotest server.
200 virtual bool IsUpdateUrlOfficial() const;
201
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700202 // For unit-tests.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700203 void set_root(const std::string& root);
Chris Sosa28e479c2013-07-12 11:39:53 -0700204 void set_current_channel(const std::string& channel) {
Alex Deymo85616652015-10-15 18:48:31 -0700205 image_props_.current_channel = channel;
Chris Sosa28e479c2013-07-12 11:39:53 -0700206 }
Gilad Arnold76b2b482014-04-01 13:32:43 -0700207 void set_target_channel(const std::string& channel) {
Alex Deymo85616652015-10-15 18:48:31 -0700208 mutable_image_props_.target_channel = channel;
Gilad Arnold76b2b482014-04-01 13:32:43 -0700209 }
Marton Hunyady2abda312018-04-24 18:21:49 +0200210 void set_os_sp(const std::string& os_sp) { os_sp_ = os_sp; }
211 void set_os_board(const std::string& os_board) {
212 image_props_.board = os_board;
213 }
214 void set_app_lang(const std::string& app_lang) { app_lang_ = app_lang; }
215 void set_hwid(const std::string& hwid) { hwid_ = hwid; }
216 void set_fw_version(const std::string& fw_version) {
217 fw_version_ = fw_version;
218 }
219 void set_ec_version(const std::string& ec_version) {
220 ec_version_ = ec_version;
221 }
222 void set_is_powerwash_allowed(bool powerwash_allowed) {
223 mutable_image_props_.is_powerwash_allowed = powerwash_allowed;
224 }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700225
226 private:
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700227 FRIEND_TEST(OmahaRequestParamsTest, ChannelIndexTest);
Chris Sosac1972482013-04-30 22:31:10 -0700228 FRIEND_TEST(OmahaRequestParamsTest, CollectECFWVersionsTest);
Sen Jiang7f785f52018-01-24 13:31:56 -0800229 FRIEND_TEST(OmahaRequestParamsTest, IsValidChannelTest);
Sen Jiang8500d3a2018-02-08 12:04:05 -0800230 FRIEND_TEST(OmahaRequestParamsTest, SetIsPowerwashAllowedTest);
Sen Jiang7f785f52018-01-24 13:31:56 -0800231 FRIEND_TEST(OmahaRequestParamsTest, SetTargetChannelInvalidTest);
Sen Jiang8500d3a2018-02-08 12:04:05 -0800232 FRIEND_TEST(OmahaRequestParamsTest, SetTargetChannelTest);
233 FRIEND_TEST(OmahaRequestParamsTest, ShouldPowerwashTest);
Sen Jiang7f785f52018-01-24 13:31:56 -0800234 FRIEND_TEST(OmahaRequestParamsTest, ToMoreStableChannelFlagTest);
Darin Petkov49d91322010-10-25 16:34:58 -0700235
Sen Jiangce3f7cf2018-01-25 14:07:45 -0800236 // Returns true if |channel| is a valid channel, otherwise write error to
237 // |error_message| if passed and return false.
238 bool IsValidChannel(const std::string& channel,
239 std::string* error_message) const;
240 bool IsValidChannel(const std::string& channel) const {
241 return IsValidChannel(channel, nullptr);
242 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700243
244 // Returns the index of the given channel.
245 int GetChannelIndex(const std::string& channel) const;
246
Sen Jiang8500d3a2018-02-08 12:04:05 -0800247 // True if we're trying to update to a more stable channel.
248 // i.e. index(target_channel) > index(current_channel).
249 bool ToMoreStableChannel() const;
250
Chris Sosac1972482013-04-30 22:31:10 -0700251 // Returns True if we should store the fw/ec versions based on our hwid_.
252 // Compares hwid to a set of whitelisted prefixes.
253 bool CollectECFWVersions() const;
254
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700255 // Gets the machine type (e.g. "i686").
256 std::string GetMachineType() const;
257
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700258 // Global system context.
259 SystemState* system_state_;
260
Alex Deymo85616652015-10-15 18:48:31 -0700261 // The system image properties.
262 ImageProperties image_props_;
263 MutableImageProperties mutable_image_props_;
264
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700265 // Basic properties of the OS and Application that go into the Omaha request.
266 std::string os_platform_;
267 std::string os_version_;
268 std::string os_sp_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700269 std::string app_lang_;
270
Alex Deymo85616652015-10-15 18:48:31 -0700271 // There are three channel values we deal with:
272 // * The channel we got the image we are running from or "current channel"
273 // stored in |image_props_.current_channel|.
274 //
275 // * The release channel we are tracking, where we should get updates from,
276 // stored in |mutable_image_props_.target_channel|. This channel is
277 // normally the same as the current_channel, except when the user changes
278 // the channel. In that case it'll have the release channel the user
279 // switched to, regardless of whether we downloaded an update from that
280 // channel or not, or if we are in the middle of a download from a
281 // previously selected channel (as opposed to download channel
282 // which gets updated only at the start of next download).
283 //
284 // * The channel from which we're downloading the payload. This should
285 // normally be the same as target channel. But if the user made another
286 // channel change after we started the download, then they'd be different,
287 // in which case, we'd detect elsewhere that the target channel has been
288 // changed and cancel the current download attempt.
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700289 std::string download_channel_;
290
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700291 std::string hwid_; // Hardware Qualification ID of the client
Chris Sosac1972482013-04-30 22:31:10 -0700292 std::string fw_version_; // Chrome OS Firmware Version.
293 std::string ec_version_; // Chrome OS EC Version.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700294 bool delta_okay_; // If this client can accept a delta
295 bool interactive_; // Whether this is a user-initiated update check
296
297 // The URL to send the Omaha request to.
298 std::string update_url_;
299
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700300 // Prefix of the target OS version that the enterprise wants this device
301 // to be pinned to. It's empty otherwise.
302 std::string target_version_prefix_;
303
304 // True if scattering is enabled, in which case waiting_period_ specifies the
305 // amount of absolute time that we've to wait for before sending a request to
306 // Omaha.
307 bool wall_clock_based_wait_enabled_;
308 base::TimeDelta waiting_period_;
309
310 // True if scattering is enabled to denote the number of update checks
311 // we've to skip before we can send a request to Omaha. The min and max
312 // values establish the bounds for a random number to be chosen within that
313 // range to enable such a wait.
314 bool update_check_count_wait_enabled_;
Ben Chan9abb7632014-08-07 00:10:53 -0700315 int64_t min_update_checks_needed_;
316 int64_t max_update_checks_allowed_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700317
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700318 // When reading files, prepend root_ to the paths. Useful for testing.
319 std::string root_;
320
Marton Hunyady2abda312018-04-24 18:21:49 +0200321 DISALLOW_COPY_AND_ASSIGN(OmahaRequestParams);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700322};
323
324} // namespace chromeos_update_engine
325
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700326#endif // UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H_