blob: f8e9438ea186b3002d0c1cc64bb65484984c4568 [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
Jay Srinivasanae4697c2013-03-18 17:08:08 -070057 OmahaRequestParams(SystemState* system_state,
58 const std::string& in_os_platform,
Darin Petkova4a8a8c2010-07-15 22:21:12 -070059 const std::string& in_os_version,
60 const std::string& in_os_sp,
61 const std::string& in_os_board,
62 const std::string& in_app_id,
63 const std::string& in_app_version,
64 const std::string& in_app_lang,
Jay Srinivasanae4697c2013-03-18 17:08:08 -070065 const std::string& in_target_channel,
66 const std::string& in_hwid,
Chris Sosac1972482013-04-30 22:31:10 -070067 const std::string& in_fw_version,
68 const std::string& in_ec_version,
Jay Srinivasan0a708742012-03-20 11:26:12 -070069 bool in_delta_okay,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -080070 bool in_interactive,
Jay Srinivasan0a708742012-03-20 11:26:12 -070071 const std::string& in_update_url,
Gilad Arnold74b5f552014-10-07 08:17:16 -070072 const std::string& in_target_version_prefix)
Jay Srinivasanae4697c2013-03-18 17:08:08 -070073 : system_state_(system_state),
74 os_platform_(in_os_platform),
75 os_version_(in_os_version),
76 os_sp_(in_os_sp),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070077 app_lang_(in_app_lang),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070078 hwid_(in_hwid),
Chris Sosac1972482013-04-30 22:31:10 -070079 fw_version_(in_fw_version),
80 ec_version_(in_ec_version),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070081 delta_okay_(in_delta_okay),
82 interactive_(in_interactive),
83 update_url_(in_update_url),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070084 target_version_prefix_(in_target_version_prefix),
85 wall_clock_based_wait_enabled_(false),
86 update_check_count_wait_enabled_(false),
87 min_update_checks_needed_(kDefaultMinUpdateChecks),
Alex Deymo85616652015-10-15 18:48:31 -070088 max_update_checks_allowed_(kDefaultMaxUpdateChecks) {
89 image_props_.board = in_os_board;
90 image_props_.product_id = in_app_id;
91 image_props_.canary_product_id = in_app_id;
92 image_props_.version = in_app_version;
93 image_props_.current_channel = in_target_channel;
94 mutable_image_props_.target_channel = in_target_channel;
95 mutable_image_props_.is_powerwash_allowed = false;
96 }
Darin Petkova4a8a8c2010-07-15 22:21:12 -070097
Alex Deymo3be05c82015-10-23 11:29:11 -070098 virtual ~OmahaRequestParams();
Alex Deymoe8948702014-11-11 21:44:45 -080099
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700100 // Setters and getters for the various properties.
101 inline std::string os_platform() const { return os_platform_; }
102 inline std::string os_version() const { return os_version_; }
103 inline std::string os_sp() const { return os_sp_; }
Alex Deymo85616652015-10-15 18:48:31 -0700104 inline std::string os_board() const { return image_props_.board; }
Alex Deymoebf6e122017-03-10 16:12:01 -0800105 inline std::string os_build_fingerprint() const {
106 return image_props_.build_fingerprint;
107 }
Sen Jiang1d5d95f2017-05-19 11:33:10 -0700108 inline std::string os_build_type() const { return image_props_.build_type; }
Alex Deymo85616652015-10-15 18:48:31 -0700109 inline std::string board_app_id() const { return image_props_.product_id; }
110 inline std::string canary_app_id() const {
111 return image_props_.canary_product_id;
112 }
Sen Jiang94a4dec2017-03-28 18:23:35 -0700113 inline std::string system_app_id() const { return image_props_.system_id; }
Sen Jiang297e5832016-03-17 14:45:51 -0700114 inline void set_app_id(const std::string& app_id) {
115 image_props_.product_id = app_id;
116 image_props_.canary_product_id = app_id;
117 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700118 inline std::string app_lang() const { return app_lang_; }
119 inline std::string hwid() const { return hwid_; }
Chris Sosac1972482013-04-30 22:31:10 -0700120 inline std::string fw_version() const { return fw_version_; }
121 inline std::string ec_version() const { return ec_version_; }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700122
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700123 inline void set_app_version(const std::string& version) {
Alex Deymo85616652015-10-15 18:48:31 -0700124 image_props_.version = version;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700125 }
Alex Deymo85616652015-10-15 18:48:31 -0700126 inline std::string app_version() const { return image_props_.version; }
Sen Jiang94a4dec2017-03-28 18:23:35 -0700127 inline std::string system_version() const {
128 return image_props_.system_version;
129 }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700130
Alex Deymo85616652015-10-15 18:48:31 -0700131 inline std::string current_channel() const {
132 return image_props_.current_channel;
133 }
134 inline std::string target_channel() const {
135 return mutable_image_props_.target_channel;
136 }
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700137 inline std::string download_channel() const { return download_channel_; }
Darin Petkov49d91322010-10-25 16:34:58 -0700138
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700139 // Can client accept a delta ?
140 inline void set_delta_okay(bool ok) { delta_okay_ = ok; }
141 inline bool delta_okay() const { return delta_okay_; }
Jay Srinivasan0a708742012-03-20 11:26:12 -0700142
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700143 // True if this is a user-initiated update check.
Chris Sosa968d0572013-08-23 14:46:02 -0700144 inline void set_interactive(bool interactive) { interactive_ = interactive; }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700145 inline bool interactive() const { return interactive_; }
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700146
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700147 inline void set_update_url(const std::string& url) { update_url_ = url; }
148 inline std::string update_url() const { return update_url_; }
149
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700150 inline void set_target_version_prefix(const std::string& prefix) {
151 target_version_prefix_ = prefix;
152 }
153
154 inline std::string target_version_prefix() const {
155 return target_version_prefix_;
156 }
157
158 inline void set_wall_clock_based_wait_enabled(bool enabled) {
159 wall_clock_based_wait_enabled_ = enabled;
160 }
161 inline bool wall_clock_based_wait_enabled() const {
162 return wall_clock_based_wait_enabled_;
163 }
164
165 inline void set_waiting_period(base::TimeDelta period) {
166 waiting_period_ = period;
167 }
168 base::TimeDelta waiting_period() const { return waiting_period_; }
169
170 inline void set_update_check_count_wait_enabled(bool enabled) {
171 update_check_count_wait_enabled_ = enabled;
172 }
173
174 inline bool update_check_count_wait_enabled() const {
175 return update_check_count_wait_enabled_;
176 }
177
Ben Chan9abb7632014-08-07 00:10:53 -0700178 inline void set_min_update_checks_needed(int64_t min) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700179 min_update_checks_needed_ = min;
180 }
Ben Chan9abb7632014-08-07 00:10:53 -0700181 inline int64_t min_update_checks_needed() const {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700182 return min_update_checks_needed_;
183 }
184
Ben Chan9abb7632014-08-07 00:10:53 -0700185 inline void set_max_update_checks_allowed(int64_t max) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700186 max_update_checks_allowed_ = max;
187 }
Ben Chan9abb7632014-08-07 00:10:53 -0700188 inline int64_t max_update_checks_allowed() const {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700189 return max_update_checks_allowed_;
190 }
191
192 // True if we're trying to update to a more stable channel.
193 // i.e. index(target_channel) > index(current_channel).
Alex Deymo560ae1d2014-10-28 02:17:54 -0700194 virtual bool to_more_stable_channel() const;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700195
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700196 // Returns the app id corresponding to the current value of the
197 // download channel.
Alex Deymo560ae1d2014-10-28 02:17:54 -0700198 virtual std::string GetAppId() const;
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700199
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700200 // Suggested defaults
David Pursell02c18642014-11-06 11:26:11 -0800201 static const char kOsVersion[];
David Pursell02c18642014-11-06 11:26:11 -0800202 static const char kIsPowerwashAllowedKey[];
Ben Chan9abb7632014-08-07 00:10:53 -0700203 static const int64_t kDefaultMinUpdateChecks = 0;
204 static const int64_t kDefaultMaxUpdateChecks = 8;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700205
Darin Petkov5a7f5652010-07-22 21:40:09 -0700206 // Initializes all the data in the object. Non-empty
207 // |in_app_version| or |in_update_url| prevents automatic detection
208 // of the parameter. Returns true on success, false otherwise.
209 bool Init(const std::string& in_app_version,
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200210 const std::string& in_update_url,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -0800211 bool in_interactive);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700212
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700213 // Permanently changes the release channel to |channel|. Performs a
214 // powerwash, if required and allowed.
215 // Returns true on success, false otherwise. Note: This call will fail if
216 // there's a channel change pending already. This is to serialize all the
217 // channel changes done by the user in order to avoid having to solve
218 // numerous edge cases around ensuring the powerwash happens as intended in
219 // all such cases.
Alex Deymo560ae1d2014-10-28 02:17:54 -0700220 virtual bool SetTargetChannel(const std::string& channel,
Alex Deymod942f9d2015-11-06 16:11:50 -0800221 bool is_powerwash_allowed,
222 std::string* error_message);
Darin Petkov49d91322010-10-25 16:34:58 -0700223
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700224 // Updates the download channel for this particular attempt from the current
225 // value of target channel. This method takes a "snapshot" of the current
226 // value of target channel and uses it for all subsequent Omaha requests for
227 // this attempt (i.e. initial request as well as download progress/error
228 // event requests). The snapshot will be updated only when either this method
229 // or Init is called again.
Alex Deymo560ae1d2014-10-28 02:17:54 -0700230 virtual void UpdateDownloadChannel();
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700231
Alex Deymo85616652015-10-15 18:48:31 -0700232 virtual bool is_powerwash_allowed() const {
233 return mutable_image_props_.is_powerwash_allowed;
234 }
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900235
David Pursell02c18642014-11-06 11:26:11 -0800236 // Check if the provided update URL is official, meaning either the default
237 // autoupdate server or the autoupdate autotest server.
238 virtual bool IsUpdateUrlOfficial() const;
239
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700240 // For unit-tests.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700241 void set_root(const std::string& root);
Chris Sosa28e479c2013-07-12 11:39:53 -0700242 void set_current_channel(const std::string& channel) {
Alex Deymo85616652015-10-15 18:48:31 -0700243 image_props_.current_channel = channel;
Chris Sosa28e479c2013-07-12 11:39:53 -0700244 }
Gilad Arnold76b2b482014-04-01 13:32:43 -0700245 void set_target_channel(const std::string& channel) {
Alex Deymo85616652015-10-15 18:48:31 -0700246 mutable_image_props_.target_channel = channel;
Gilad Arnold76b2b482014-04-01 13:32:43 -0700247 }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700248
249 private:
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700250 FRIEND_TEST(OmahaRequestParamsTest, IsValidChannelTest);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700251 FRIEND_TEST(OmahaRequestParamsTest, ChannelIndexTest);
Sen Jiang297e5832016-03-17 14:45:51 -0700252 FRIEND_TEST(OmahaRequestParamsTest, ToMoreStableChannelFlagTest);
Chris Sosac1972482013-04-30 22:31:10 -0700253 FRIEND_TEST(OmahaRequestParamsTest, CollectECFWVersionsTest);
Darin Petkov49d91322010-10-25 16:34:58 -0700254
Alex Deymo85616652015-10-15 18:48:31 -0700255 // Returns true if |channel| is a valid channel, false otherwise.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700256 bool IsValidChannel(const std::string& channel) const;
257
258 // Returns the index of the given channel.
259 int GetChannelIndex(const std::string& channel) const;
260
Chris Sosac1972482013-04-30 22:31:10 -0700261 // Returns True if we should store the fw/ec versions based on our hwid_.
262 // Compares hwid to a set of whitelisted prefixes.
263 bool CollectECFWVersions() const;
264
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700265 // These are individual helper methods to initialize the said properties from
266 // the LSB value.
267 void SetTargetChannelFromLsbValue();
268 void SetCurrentChannelFromLsbValue();
269 void SetIsPowerwashAllowedFromLsbValue();
270
271 // Initializes the required properties from the LSB value.
272 void InitFromLsbValue();
Darin Petkov49d91322010-10-25 16:34:58 -0700273
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700274 // Gets the machine type (e.g. "i686").
275 std::string GetMachineType() const;
276
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700277 // Global system context.
278 SystemState* system_state_;
279
Alex Deymo85616652015-10-15 18:48:31 -0700280 // The system image properties.
281 ImageProperties image_props_;
282 MutableImageProperties mutable_image_props_;
283
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700284 // Basic properties of the OS and Application that go into the Omaha request.
285 std::string os_platform_;
286 std::string os_version_;
287 std::string os_sp_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700288 std::string app_lang_;
289
Alex Deymo85616652015-10-15 18:48:31 -0700290 // There are three channel values we deal with:
291 // * The channel we got the image we are running from or "current channel"
292 // stored in |image_props_.current_channel|.
293 //
294 // * The release channel we are tracking, where we should get updates from,
295 // stored in |mutable_image_props_.target_channel|. This channel is
296 // normally the same as the current_channel, except when the user changes
297 // the channel. In that case it'll have the release channel the user
298 // switched to, regardless of whether we downloaded an update from that
299 // channel or not, or if we are in the middle of a download from a
300 // previously selected channel (as opposed to download channel
301 // which gets updated only at the start of next download).
302 //
303 // * The channel from which we're downloading the payload. This should
304 // normally be the same as target channel. But if the user made another
305 // channel change after we started the download, then they'd be different,
306 // in which case, we'd detect elsewhere that the target channel has been
307 // changed and cancel the current download attempt.
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700308 std::string download_channel_;
309
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700310 std::string hwid_; // Hardware Qualification ID of the client
Chris Sosac1972482013-04-30 22:31:10 -0700311 std::string fw_version_; // Chrome OS Firmware Version.
312 std::string ec_version_; // Chrome OS EC Version.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700313 bool delta_okay_; // If this client can accept a delta
314 bool interactive_; // Whether this is a user-initiated update check
315
316 // The URL to send the Omaha request to.
317 std::string update_url_;
318
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700319 // Prefix of the target OS version that the enterprise wants this device
320 // to be pinned to. It's empty otherwise.
321 std::string target_version_prefix_;
322
323 // True if scattering is enabled, in which case waiting_period_ specifies the
324 // amount of absolute time that we've to wait for before sending a request to
325 // Omaha.
326 bool wall_clock_based_wait_enabled_;
327 base::TimeDelta waiting_period_;
328
329 // True if scattering is enabled to denote the number of update checks
330 // we've to skip before we can send a request to Omaha. The min and max
331 // values establish the bounds for a random number to be chosen within that
332 // range to enable such a wait.
333 bool update_check_count_wait_enabled_;
Ben Chan9abb7632014-08-07 00:10:53 -0700334 int64_t min_update_checks_needed_;
335 int64_t max_update_checks_allowed_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700336
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700337 // When reading files, prepend root_ to the paths. Useful for testing.
338 std::string root_;
339
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700340 // TODO(jaysri): Uncomment this after fixing unit tests, as part of
341 // chromium-os:39752
342 // DISALLOW_COPY_AND_ASSIGN(OmahaRequestParams);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700343};
344
345} // namespace chromeos_update_engine
346
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700347#endif // UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H_