blob: 3420b41db9947d0de9bda27d4fc912583058869f [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),
36 app_id_(kAppId),
37 board_app_id_(kAppId),
38 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,
Jay Srinivasan0a708742012-03-20 11:26:12 -070061 bool in_delta_okay,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -080062 bool in_interactive,
Jay Srinivasan0a708742012-03-20 11:26:12 -070063 const std::string& in_update_url,
64 bool in_update_disabled,
65 const std::string& in_target_version_prefix)
Jay Srinivasanae4697c2013-03-18 17:08:08 -070066 : system_state_(system_state),
67 os_platform_(in_os_platform),
68 os_version_(in_os_version),
69 os_sp_(in_os_sp),
70 os_board_(in_os_board),
71 app_id_(in_app_id),
72 board_app_id_(in_app_id),
73 app_version_(in_app_version),
74 app_lang_(in_app_lang),
75 current_channel_(in_target_channel),
76 target_channel_(in_target_channel),
77 hwid_(in_hwid),
78 delta_okay_(in_delta_okay),
79 interactive_(in_interactive),
80 update_url_(in_update_url),
81 update_disabled_(in_update_disabled),
82 target_version_prefix_(in_target_version_prefix),
83 wall_clock_based_wait_enabled_(false),
84 update_check_count_wait_enabled_(false),
85 min_update_checks_needed_(kDefaultMinUpdateChecks),
86 max_update_checks_allowed_(kDefaultMaxUpdateChecks),
87 is_powerwash_allowed_(false),
88 force_lock_down_(false),
89 forced_lock_down_(false) {}
Darin Petkova4a8a8c2010-07-15 22:21:12 -070090
Jay Srinivasanae4697c2013-03-18 17:08:08 -070091 // Setters and getters for the various properties.
92 inline std::string os_platform() const { return os_platform_; }
93 inline std::string os_version() const { return os_version_; }
94 inline std::string os_sp() const { return os_sp_; }
95 inline std::string os_board() const { return os_board_; }
96 inline std::string app_id() const { return app_id_; }
97 inline std::string board_app_id() const { return board_app_id_; }
98 inline std::string app_lang() const { return app_lang_; }
99 inline std::string hwid() const { return hwid_; }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700100
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700101 inline void set_app_version(const std::string& version) {
102 app_version_ = version;
103 }
104 inline std::string app_version() const { return app_version_; }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700105
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700106 inline std::string current_channel() const { return current_channel_; }
107 inline std::string target_channel() const { return target_channel_; }
Darin Petkov49d91322010-10-25 16:34:58 -0700108
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700109 // Can client accept a delta ?
110 inline void set_delta_okay(bool ok) { delta_okay_ = ok; }
111 inline bool delta_okay() const { return delta_okay_; }
Jay Srinivasan0a708742012-03-20 11:26:12 -0700112
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700113 // True if this is a user-initiated update check.
114 inline bool interactive() const { return interactive_; }
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700115
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700116 inline void set_update_url(const std::string& url) { update_url_ = url; }
117 inline std::string update_url() const { return update_url_; }
118
119 inline void set_update_disabled(bool disabled) {
120 update_disabled_ = disabled;
121 }
122 inline bool update_disabled() const { return update_disabled_; }
123
124 inline void set_target_version_prefix(const std::string& prefix) {
125 target_version_prefix_ = prefix;
126 }
127
128 inline std::string target_version_prefix() const {
129 return target_version_prefix_;
130 }
131
132 inline void set_wall_clock_based_wait_enabled(bool enabled) {
133 wall_clock_based_wait_enabled_ = enabled;
134 }
135 inline bool wall_clock_based_wait_enabled() const {
136 return wall_clock_based_wait_enabled_;
137 }
138
139 inline void set_waiting_period(base::TimeDelta period) {
140 waiting_period_ = period;
141 }
142 base::TimeDelta waiting_period() const { return waiting_period_; }
143
144 inline void set_update_check_count_wait_enabled(bool enabled) {
145 update_check_count_wait_enabled_ = enabled;
146 }
147
148 inline bool update_check_count_wait_enabled() const {
149 return update_check_count_wait_enabled_;
150 }
151
152 inline void set_min_update_checks_needed(int64 min) {
153 min_update_checks_needed_ = min;
154 }
155 inline int64 min_update_checks_needed() const {
156 return min_update_checks_needed_;
157 }
158
159 inline void set_max_update_checks_allowed(int64 max) {
160 max_update_checks_allowed_ = max;
161 }
162 inline int64 max_update_checks_allowed() const {
163 return max_update_checks_allowed_;
164 }
165
166 // True if we're trying to update to a more stable channel.
167 // i.e. index(target_channel) > index(current_channel).
168 bool to_more_stable_channel() const;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700169
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700170 // Suggested defaults
171 static const char* const kAppId;
172 static const char* const kOsPlatform;
173 static const char* const kOsVersion;
174 static const char* const kUpdateUrl;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700175 static const char* const kUpdateChannelKey;
176 static const char* const kIsPowerwashAllowedKey;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700177 static const int64 kDefaultMinUpdateChecks = 0;
178 static const int64 kDefaultMaxUpdateChecks = 8;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700179
Darin Petkov5a7f5652010-07-22 21:40:09 -0700180 // Initializes all the data in the object. Non-empty
181 // |in_app_version| or |in_update_url| prevents automatic detection
182 // of the parameter. Returns true on success, false otherwise.
183 bool Init(const std::string& in_app_version,
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200184 const std::string& in_update_url,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -0800185 bool in_interactive);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700186
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700187 // Permanently changes the release channel to |channel|. Performs a
188 // powerwash, if required and allowed.
189 // Returns true on success, false otherwise. Note: This call will fail if
190 // there's a channel change pending already. This is to serialize all the
191 // channel changes done by the user in order to avoid having to solve
192 // numerous edge cases around ensuring the powerwash happens as intended in
193 // all such cases.
194 bool SetTargetChannel(const std::string& channel, bool is_powerwash_allowed);
Darin Petkov49d91322010-10-25 16:34:58 -0700195
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700196 bool is_powerwash_allowed() const { return is_powerwash_allowed_; }
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900197
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700198 // For unit-tests.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700199 void set_root(const std::string& root);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700200
Darin Petkov10d02dd2011-01-10 14:57:39 -0800201 // Enforce security mode for testing purposes.
202 void SetLockDown(bool lock);
Darin Petkov49d91322010-10-25 16:34:58 -0700203
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700204 private:
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700205 FRIEND_TEST(OmahaRequestParamsTest, IsValidChannelTest);
206 FRIEND_TEST(OmahaRequestParamsTest, ShouldLockDownTest);
207 FRIEND_TEST(OmahaRequestParamsTest, ChannelIndexTest);
208 FRIEND_TEST(OmahaRequestParamsTest, LsbPreserveTest);
Darin Petkov49d91322010-10-25 16:34:58 -0700209
210 // Use a validator that is a non-static member of this class so that its
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700211 // inputs can be mocked in unit tests (e.g., build type for IsValidChannel).
212 typedef bool(OmahaRequestParams::*ValueValidator)(
Darin Petkov49d91322010-10-25 16:34:58 -0700213 const std::string&) const;
214
Darin Petkov10d02dd2011-01-10 14:57:39 -0800215 // Returns true if parameter values should be locked down for security
216 // reasons. If this is an official build running in normal boot mode, all
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700217 // values except the release channel are parsed only from the read-only rootfs
218 // partition and the channel values are restricted to a pre-approved set.
Darin Petkov10d02dd2011-01-10 14:57:39 -0800219 bool ShouldLockDown() const;
Darin Petkov49d91322010-10-25 16:34:58 -0700220
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700221 // Returns true if |channel| is a valid channel, false otherwise. This method
222 // restricts the channel value only if the image is official (see
Darin Petkov49d91322010-10-25 16:34:58 -0700223 // IsOfficialBuild).
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700224 bool IsValidChannel(const std::string& channel) const;
225
226 // Returns the index of the given channel.
227 int GetChannelIndex(const std::string& channel) const;
228
229 // These are individual helper methods to initialize the said properties from
230 // the LSB value.
231 void SetTargetChannelFromLsbValue();
232 void SetCurrentChannelFromLsbValue();
233 void SetIsPowerwashAllowedFromLsbValue();
234
235 // Initializes the required properties from the LSB value.
236 void InitFromLsbValue();
Darin Petkov49d91322010-10-25 16:34:58 -0700237
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700238 // Fetches the value for a given key from
Darin Petkova3df55b2010-11-15 13:33:55 -0800239 // /mnt/stateful_partition/etc/lsb-release if possible and |stateful_override|
240 // is true. Failing that, it looks for the key in /etc/lsb-release. If
241 // |validator| is non-NULL, uses it to validate and ignore invalid valies.
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700242 std::string GetLsbValue(const std::string& key,
Darin Petkov49d91322010-10-25 16:34:58 -0700243 const std::string& default_value,
Darin Petkova3df55b2010-11-15 13:33:55 -0800244 ValueValidator validator,
245 bool stateful_override) const;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700246
247 // Gets the machine type (e.g. "i686").
248 std::string GetMachineType() const;
249
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700250 // Global system context.
251 SystemState* system_state_;
252
253 // Basic properties of the OS and Application that go into the Omaha request.
254 std::string os_platform_;
255 std::string os_version_;
256 std::string os_sp_;
257 std::string os_board_;
258
259 // The app_id identifies the board except when we're on canary-channel.
260 // Whereas the board_app_id always identifies the board irrespective of the
261 // channel we are on. They are required the facilitate the switching from
262 // canary to a non-canary channel.
263 std::string app_id_;
264 std::string board_app_id_;
265
266 std::string app_version_;
267 std::string app_lang_;
268
269 // Current channel and target channel. Usually there are same, except when
270 // there's a pending channel change.
271 std::string current_channel_;
272 std::string target_channel_;
273 std::string hwid_; // Hardware Qualification ID of the client
274 bool delta_okay_; // If this client can accept a delta
275 bool interactive_; // Whether this is a user-initiated update check
276
277 // The URL to send the Omaha request to.
278 std::string update_url_;
279
280 // True if we've been told to block updates per enterprise policy.
281 bool update_disabled_;
282
283 // Prefix of the target OS version that the enterprise wants this device
284 // to be pinned to. It's empty otherwise.
285 std::string target_version_prefix_;
286
287 // True if scattering is enabled, in which case waiting_period_ specifies the
288 // amount of absolute time that we've to wait for before sending a request to
289 // Omaha.
290 bool wall_clock_based_wait_enabled_;
291 base::TimeDelta waiting_period_;
292
293 // True if scattering is enabled to denote the number of update checks
294 // we've to skip before we can send a request to Omaha. The min and max
295 // values establish the bounds for a random number to be chosen within that
296 // range to enable such a wait.
297 bool update_check_count_wait_enabled_;
298 int64 min_update_checks_needed_;
299 int64 max_update_checks_allowed_;
300
301 // True if we are allowed to do powerwash, if required, on a channel change.
302 bool is_powerwash_allowed_;
303
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700304 // When reading files, prepend root_ to the paths. Useful for testing.
305 std::string root_;
306
Darin Petkov10d02dd2011-01-10 14:57:39 -0800307 // Force security lock down for testing purposes.
308 bool force_lock_down_;
309 bool forced_lock_down_;
Darin Petkov49d91322010-10-25 16:34:58 -0700310
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700311 // TODO(jaysri): Uncomment this after fixing unit tests, as part of
312 // chromium-os:39752
313 // DISALLOW_COPY_AND_ASSIGN(OmahaRequestParams);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700314};
315
316} // namespace chromeos_update_engine
317
318#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H__