blob: 19a1900fe8a0d4d034414149164628b2205dcb37 [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
5#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H__
7
8#include <string>
9
Darin Petkov49d91322010-10-25 16:34:58 -070010#include <base/basictypes.h>
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070011#include <base/time.h>
Darin Petkov49d91322010-10-25 16:34:58 -070012#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkova4a8a8c2010-07-15 22:21:12 -070013
14// This gathers local system information and prepares info used by the
15// Omaha request action.
16
17namespace chromeos_update_engine {
18
Jay Srinivasan55f50c22013-01-10 19:24:35 -080019// The default "official" Omaha update URL.
20extern const char* const kProductionOmahaUrl;
21
Jay Srinivasanae4697c2013-03-18 17:08:08 -070022class SystemState;
Jay Srinivasan0a708742012-03-20 11:26:12 -070023
Jay Srinivasanae4697c2013-03-18 17:08:08 -070024// This class encapsulates the data Omaha gets for the request, along with
25// essential state needed for the processing of the request/response. The
26// strings in this struct should not be XML escaped.
27//
28// TODO (jaysri): chromium-os:39752 tracks the need to rename this class to
29// reflect its lifetime more appropriately.
30class OmahaRequestParams {
31 public:
32 OmahaRequestParams(SystemState* system_state)
33 : system_state_(system_state),
34 os_platform_(kOsPlatform),
35 os_version_(kOsVersion),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070036 board_app_id_(kAppId),
Jay Srinivasandb0acdf2013-04-02 14:47:45 -070037 canary_app_id_(kAppId),
Jay Srinivasanae4697c2013-03-18 17:08:08 -070038 delta_okay_(true),
39 interactive_(false),
40 update_disabled_(false),
41 wall_clock_based_wait_enabled_(false),
42 update_check_count_wait_enabled_(false),
43 min_update_checks_needed_(kDefaultMinUpdateChecks),
44 max_update_checks_allowed_(kDefaultMaxUpdateChecks),
45 is_powerwash_allowed_(false),
46 force_lock_down_(false),
47 forced_lock_down_(false) {
48 InitFromLsbValue();
49 }
Jay Srinivasan0a708742012-03-20 11:26:12 -070050
Jay Srinivasanae4697c2013-03-18 17:08:08 -070051 OmahaRequestParams(SystemState* system_state,
52 const std::string& in_os_platform,
Darin Petkova4a8a8c2010-07-15 22:21:12 -070053 const std::string& in_os_version,
54 const std::string& in_os_sp,
55 const std::string& in_os_board,
56 const std::string& in_app_id,
57 const std::string& in_app_version,
58 const std::string& in_app_lang,
Jay Srinivasanae4697c2013-03-18 17:08:08 -070059 const std::string& in_target_channel,
60 const std::string& in_hwid,
Chris Sosac1972482013-04-30 22:31:10 -070061 const std::string& in_fw_version,
62 const std::string& in_ec_version,
Jay Srinivasan0a708742012-03-20 11:26:12 -070063 bool in_delta_okay,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -080064 bool in_interactive,
Jay Srinivasan0a708742012-03-20 11:26:12 -070065 const std::string& in_update_url,
66 bool in_update_disabled,
67 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),
85 update_disabled_(in_update_disabled),
86 target_version_prefix_(in_target_version_prefix),
87 wall_clock_based_wait_enabled_(false),
88 update_check_count_wait_enabled_(false),
89 min_update_checks_needed_(kDefaultMinUpdateChecks),
90 max_update_checks_allowed_(kDefaultMaxUpdateChecks),
91 is_powerwash_allowed_(false),
92 force_lock_down_(false),
93 forced_lock_down_(false) {}
Darin Petkova4a8a8c2010-07-15 22:21:12 -070094
Jay Srinivasanae4697c2013-03-18 17:08:08 -070095 // Setters and getters for the various properties.
96 inline std::string os_platform() const { return os_platform_; }
97 inline std::string os_version() const { return os_version_; }
98 inline std::string os_sp() const { return os_sp_; }
99 inline std::string os_board() const { return os_board_; }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700100 inline std::string board_app_id() const { return board_app_id_; }
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700101 inline std::string canary_app_id() const { return canary_app_id_; }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700102 inline std::string app_lang() const { return app_lang_; }
103 inline std::string hwid() const { return hwid_; }
Chris Sosac1972482013-04-30 22:31:10 -0700104 inline std::string fw_version() const { return fw_version_; }
105 inline std::string ec_version() const { return ec_version_; }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700106
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700107 inline void set_app_version(const std::string& version) {
108 app_version_ = version;
109 }
110 inline std::string app_version() const { return app_version_; }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700111
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700112 inline std::string current_channel() const { return current_channel_; }
113 inline std::string target_channel() const { return target_channel_; }
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700114 inline std::string download_channel() const { return download_channel_; }
Darin Petkov49d91322010-10-25 16:34:58 -0700115
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700116 // Can client accept a delta ?
117 inline void set_delta_okay(bool ok) { delta_okay_ = ok; }
118 inline bool delta_okay() const { return delta_okay_; }
Jay Srinivasan0a708742012-03-20 11:26:12 -0700119
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700120 // True if this is a user-initiated update check.
Chris Sosa968d0572013-08-23 14:46:02 -0700121 inline void set_interactive(bool interactive) { interactive_ = interactive; }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700122 inline bool interactive() const { return interactive_; }
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700123
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700124 inline void set_update_url(const std::string& url) { update_url_ = url; }
125 inline std::string update_url() const { return update_url_; }
126
127 inline void set_update_disabled(bool disabled) {
128 update_disabled_ = disabled;
129 }
130 inline bool update_disabled() const { return update_disabled_; }
131
132 inline void set_target_version_prefix(const std::string& prefix) {
133 target_version_prefix_ = prefix;
134 }
135
136 inline std::string target_version_prefix() const {
137 return target_version_prefix_;
138 }
139
140 inline void set_wall_clock_based_wait_enabled(bool enabled) {
141 wall_clock_based_wait_enabled_ = enabled;
142 }
143 inline bool wall_clock_based_wait_enabled() const {
144 return wall_clock_based_wait_enabled_;
145 }
146
147 inline void set_waiting_period(base::TimeDelta period) {
148 waiting_period_ = period;
149 }
150 base::TimeDelta waiting_period() const { return waiting_period_; }
151
152 inline void set_update_check_count_wait_enabled(bool enabled) {
153 update_check_count_wait_enabled_ = enabled;
154 }
155
156 inline bool update_check_count_wait_enabled() const {
157 return update_check_count_wait_enabled_;
158 }
159
160 inline void set_min_update_checks_needed(int64 min) {
161 min_update_checks_needed_ = min;
162 }
163 inline int64 min_update_checks_needed() const {
164 return min_update_checks_needed_;
165 }
166
167 inline void set_max_update_checks_allowed(int64 max) {
168 max_update_checks_allowed_ = max;
169 }
170 inline int64 max_update_checks_allowed() const {
171 return max_update_checks_allowed_;
172 }
173
174 // True if we're trying to update to a more stable channel.
175 // i.e. index(target_channel) > index(current_channel).
176 bool to_more_stable_channel() const;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700177
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700178 // Returns the app id corresponding to the current value of the
179 // download channel.
180 std::string GetAppId() const;
181
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700182 // Suggested defaults
183 static const char* const kAppId;
184 static const char* const kOsPlatform;
185 static const char* const kOsVersion;
186 static const char* const kUpdateUrl;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700187 static const char* const kUpdateChannelKey;
188 static const char* const kIsPowerwashAllowedKey;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700189 static const int64 kDefaultMinUpdateChecks = 0;
190 static const int64 kDefaultMaxUpdateChecks = 8;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700191
Darin Petkov5a7f5652010-07-22 21:40:09 -0700192 // Initializes all the data in the object. Non-empty
193 // |in_app_version| or |in_update_url| prevents automatic detection
194 // of the parameter. Returns true on success, false otherwise.
195 bool Init(const std::string& in_app_version,
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200196 const std::string& in_update_url,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -0800197 bool in_interactive);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700198
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700199 // Permanently changes the release channel to |channel|. Performs a
200 // powerwash, if required and allowed.
201 // Returns true on success, false otherwise. Note: This call will fail if
202 // there's a channel change pending already. This is to serialize all the
203 // channel changes done by the user in order to avoid having to solve
204 // numerous edge cases around ensuring the powerwash happens as intended in
205 // all such cases.
206 bool SetTargetChannel(const std::string& channel, bool is_powerwash_allowed);
Darin Petkov49d91322010-10-25 16:34:58 -0700207
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700208 // Updates the download channel for this particular attempt from the current
209 // value of target channel. This method takes a "snapshot" of the current
210 // value of target channel and uses it for all subsequent Omaha requests for
211 // this attempt (i.e. initial request as well as download progress/error
212 // event requests). The snapshot will be updated only when either this method
213 // or Init is called again.
214 void UpdateDownloadChannel();
215
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700216 bool is_powerwash_allowed() const { return is_powerwash_allowed_; }
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900217
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700218 // For unit-tests.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700219 void set_root(const std::string& root);
Chris Sosa28e479c2013-07-12 11:39:53 -0700220 void set_current_channel(const std::string& channel) {
221 current_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).
236 typedef bool(OmahaRequestParams::*ValueValidator)(
Darin Petkov49d91322010-10-25 16:34:58 -0700237 const std::string&) const;
238
Darin Petkov10d02dd2011-01-10 14:57:39 -0800239 // Returns true if parameter values should be locked down for security
240 // reasons. If this is an official build running in normal boot mode, all
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700241 // values except the release channel are parsed only from the read-only rootfs
242 // partition and the channel values are restricted to a pre-approved set.
Darin Petkov10d02dd2011-01-10 14:57:39 -0800243 bool ShouldLockDown() const;
Darin Petkov49d91322010-10-25 16:34:58 -0700244
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700245 // Returns true if |channel| is a valid channel, false otherwise. This method
246 // restricts the channel value only if the image is official (see
Darin Petkov49d91322010-10-25 16:34:58 -0700247 // IsOfficialBuild).
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700248 bool IsValidChannel(const std::string& channel) const;
249
250 // Returns the index of the given channel.
251 int GetChannelIndex(const std::string& channel) const;
252
Chris Sosac1972482013-04-30 22:31:10 -0700253 // Returns True if we should store the fw/ec versions based on our hwid_.
254 // Compares hwid to a set of whitelisted prefixes.
255 bool CollectECFWVersions() const;
256
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700257 // These are individual helper methods to initialize the said properties from
258 // the LSB value.
259 void SetTargetChannelFromLsbValue();
260 void SetCurrentChannelFromLsbValue();
261 void SetIsPowerwashAllowedFromLsbValue();
262
263 // Initializes the required properties from the LSB value.
264 void InitFromLsbValue();
Darin Petkov49d91322010-10-25 16:34:58 -0700265
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700266 // Fetches the value for a given key from
Darin Petkova3df55b2010-11-15 13:33:55 -0800267 // /mnt/stateful_partition/etc/lsb-release if possible and |stateful_override|
268 // is true. Failing that, it looks for the key in /etc/lsb-release. If
269 // |validator| is non-NULL, uses it to validate and ignore invalid valies.
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700270 std::string GetLsbValue(const std::string& key,
Darin Petkov49d91322010-10-25 16:34:58 -0700271 const std::string& default_value,
Darin Petkova3df55b2010-11-15 13:33:55 -0800272 ValueValidator validator,
273 bool stateful_override) const;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700274
275 // Gets the machine type (e.g. "i686").
276 std::string GetMachineType() const;
277
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700278 // Global system context.
279 SystemState* system_state_;
280
281 // Basic properties of the OS and Application that go into the Omaha request.
282 std::string os_platform_;
283 std::string os_version_;
284 std::string os_sp_;
285 std::string os_board_;
286
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700287 // The board app id identifies the app id for the board irrespective of the
288 // channel that we're on. The canary app id identifies the app id to be used
289 // iff we're in the canary-channel. These values could be different depending
290 // on how the release tools are implemented.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700291 std::string board_app_id_;
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700292 std::string canary_app_id_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700293
294 std::string app_version_;
295 std::string app_lang_;
296
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700297 // The three channel values we deal with.
298 // Current channel: is always the channel from /etc/lsb-release. It never
299 // changes. It's just read in during initialization.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700300 std::string current_channel_;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700301
302 // Target channel: It starts off with the value of current channel. But if
303 // the user changes the channel, then it'll have a different value. If the
304 // user changes multiple times, target channel will always contain the most
305 // recent change and is updated immediately to the user-selected value even
306 // if we're in the middle of a download (as opposed to download channel
307 // which gets updated only at the start of next download)
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700308 std::string target_channel_;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700309
310 // The channel from which we're downloading the payload. This should normally
311 // be the same as target channel. But if the user made another channel change
312 // we started the download, then they'd be different, in which case, we'd
313 // detect elsewhere that the target channel has been changed and cancel the
314 // current download attempt.
315 std::string download_channel_;
316
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700317 std::string hwid_; // Hardware Qualification ID of the client
Chris Sosac1972482013-04-30 22:31:10 -0700318 std::string fw_version_; // Chrome OS Firmware Version.
319 std::string ec_version_; // Chrome OS EC Version.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700320 bool delta_okay_; // If this client can accept a delta
321 bool interactive_; // Whether this is a user-initiated update check
322
323 // The URL to send the Omaha request to.
324 std::string update_url_;
325
326 // True if we've been told to block updates per enterprise policy.
327 bool update_disabled_;
328
329 // Prefix of the target OS version that the enterprise wants this device
330 // to be pinned to. It's empty otherwise.
331 std::string target_version_prefix_;
332
333 // True if scattering is enabled, in which case waiting_period_ specifies the
334 // amount of absolute time that we've to wait for before sending a request to
335 // Omaha.
336 bool wall_clock_based_wait_enabled_;
337 base::TimeDelta waiting_period_;
338
339 // True if scattering is enabled to denote the number of update checks
340 // we've to skip before we can send a request to Omaha. The min and max
341 // values establish the bounds for a random number to be chosen within that
342 // range to enable such a wait.
343 bool update_check_count_wait_enabled_;
344 int64 min_update_checks_needed_;
345 int64 max_update_checks_allowed_;
346
347 // True if we are allowed to do powerwash, if required, on a channel change.
348 bool is_powerwash_allowed_;
349
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700350 // When reading files, prepend root_ to the paths. Useful for testing.
351 std::string root_;
352
Darin Petkov10d02dd2011-01-10 14:57:39 -0800353 // Force security lock down for testing purposes.
354 bool force_lock_down_;
355 bool forced_lock_down_;
Darin Petkov49d91322010-10-25 16:34:58 -0700356
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700357 // TODO(jaysri): Uncomment this after fixing unit tests, as part of
358 // chromium-os:39752
359 // DISALLOW_COPY_AND_ASSIGN(OmahaRequestParams);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700360};
361
362} // namespace chromeos_update_engine
363
364#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H__