1 // 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 // 16 17 #ifndef UPDATE_ENGINE_CROS_OMAHA_REQUEST_PARAMS_H_ 18 #define UPDATE_ENGINE_CROS_OMAHA_REQUEST_PARAMS_H_ 19 20 #include <stdint.h> 21 22 #include <map> 23 #include <string> 24 #include <vector> 25 26 #include <base/macros.h> 27 #include <base/time/time.h> 28 #include <gtest/gtest_prod.h> // for FRIEND_TEST 29 30 #include "update_engine/common/constants.h" 31 #include "update_engine/common/platform_constants.h" 32 #include "update_engine/cros/image_properties.h" 33 #include "update_engine/update_manager/policy.h" 34 35 // This gathers local system information and prepares info used by the 36 // Omaha request action. 37 38 namespace chromeos_update_engine { 39 40 // This class encapsulates the data Omaha gets for the request, along with 41 // essential state needed for the processing of the request/response. The 42 // strings in this struct should not be XML escaped. 43 // 44 // TODO(jaysri): chromium-os:39752 tracks the need to rename this class to 45 // reflect its lifetime more appropriately. 46 class OmahaRequestParams { 47 public: OmahaRequestParams()48 OmahaRequestParams() 49 : os_platform_(constants::kOmahaPlatformName), 50 os_version_(kOsVersion), 51 delta_okay_(true), 52 interactive_(false), 53 rollback_allowed_(false), 54 rollback_data_save_requested_(false), 55 wall_clock_based_wait_enabled_(false), 56 update_check_count_wait_enabled_(false), 57 min_update_checks_needed_(kDefaultMinUpdateChecks), 58 max_update_checks_allowed_(kDefaultMaxUpdateChecks), 59 is_install_(false) {} 60 61 virtual ~OmahaRequestParams(); 62 63 enum ActiveCountingType { 64 kDayBased = 0, 65 kDateBased, 66 }; 67 68 struct AppParams { 69 ActiveCountingType active_counting_type; 70 // |name| is only used for DLCs to store the DLC ID. 71 std::string name; 72 int64_t ping_active; 73 int64_t ping_date_last_active; 74 int64_t ping_date_last_rollcall; 75 bool send_ping; 76 // |updated| is only used for DLCs to decide sending DBus message to 77 // dlcservice on an install/update completion. 78 bool updated = true; 79 }; 80 81 // Setters and getters for the various properties. os_platform()82 inline std::string os_platform() const { return os_platform_; } os_version()83 inline std::string os_version() const { return os_version_; } os_sp()84 inline std::string os_sp() const { return os_sp_; } os_board()85 inline std::string os_board() const { return image_props_.board; } os_build_fingerprint()86 inline std::string os_build_fingerprint() const { 87 return image_props_.build_fingerprint; 88 } os_build_type()89 inline std::string os_build_type() const { return image_props_.build_type; } board_app_id()90 inline std::string board_app_id() const { return image_props_.product_id; } canary_app_id()91 inline std::string canary_app_id() const { 92 return image_props_.canary_product_id; 93 } set_app_id(const std::string & app_id)94 inline void set_app_id(const std::string& app_id) { 95 image_props_.product_id = app_id; 96 image_props_.canary_product_id = app_id; 97 } app_lang()98 inline std::string app_lang() const { return app_lang_; } hwid()99 inline std::string hwid() const { return hwid_; } device_requisition()100 inline std::string device_requisition() const { return device_requisition_; } 101 set_app_version(const std::string & version)102 inline void set_app_version(const std::string& version) { 103 image_props_.version = version; 104 } app_version()105 inline std::string app_version() const { return image_props_.version; } product_components()106 inline std::string product_components() const { 107 return image_props_.product_components; 108 } set_product_components(const std::string & product_components)109 inline void set_product_components(const std::string& product_components) { 110 image_props_.product_components = product_components; 111 } 112 current_channel()113 inline std::string current_channel() const { 114 return image_props_.current_channel; 115 } target_channel()116 inline std::string target_channel() const { 117 return mutable_image_props_.target_channel; 118 } download_channel()119 inline std::string download_channel() const { return download_channel_; } 120 121 // Can client accept a delta ? set_delta_okay(bool ok)122 inline void set_delta_okay(bool ok) { delta_okay_ = ok; } delta_okay()123 inline bool delta_okay() const { return delta_okay_; } 124 125 // True if this is a user-initiated update check. set_interactive(bool interactive)126 inline void set_interactive(bool interactive) { interactive_ = interactive; } interactive()127 inline bool interactive() const { return interactive_; } 128 set_update_url(const std::string & url)129 inline void set_update_url(const std::string& url) { update_url_ = url; } update_url()130 inline std::string update_url() const { return update_url_; } 131 set_target_version_prefix(const std::string & prefix)132 inline void set_target_version_prefix(const std::string& prefix) { 133 target_version_prefix_ = prefix; 134 } 135 target_version_prefix()136 inline std::string target_version_prefix() const { 137 return target_version_prefix_; 138 } 139 lts_tag()140 inline std::string lts_tag() const { return lts_tag_; } 141 set_lts_tag(const std::string & hint)142 inline void set_lts_tag(const std::string& hint) { lts_tag_ = hint; } 143 set_rollback_allowed(bool rollback_allowed)144 inline void set_rollback_allowed(bool rollback_allowed) { 145 rollback_allowed_ = rollback_allowed; 146 } 147 rollback_allowed()148 inline bool rollback_allowed() const { return rollback_allowed_; } 149 set_rollback_data_save_requested(bool rollback_data_save_requested)150 inline void set_rollback_data_save_requested( 151 bool rollback_data_save_requested) { 152 rollback_data_save_requested_ = rollback_data_save_requested; 153 } 154 rollback_data_save_requested()155 inline bool rollback_data_save_requested() const { 156 return rollback_data_save_requested_; 157 } 158 set_rollback_allowed_milestones(int rollback_allowed_milestones)159 inline void set_rollback_allowed_milestones(int rollback_allowed_milestones) { 160 rollback_allowed_milestones_ = rollback_allowed_milestones; 161 } 162 rollback_allowed_milestones()163 inline int rollback_allowed_milestones() const { 164 return rollback_allowed_milestones_; 165 } 166 set_wall_clock_based_wait_enabled(bool enabled)167 inline void set_wall_clock_based_wait_enabled(bool enabled) { 168 wall_clock_based_wait_enabled_ = enabled; 169 } wall_clock_based_wait_enabled()170 inline bool wall_clock_based_wait_enabled() const { 171 return wall_clock_based_wait_enabled_; 172 } 173 set_waiting_period(base::TimeDelta period)174 inline void set_waiting_period(base::TimeDelta period) { 175 waiting_period_ = period; 176 } waiting_period()177 base::TimeDelta waiting_period() const { return waiting_period_; } 178 set_update_check_count_wait_enabled(bool enabled)179 inline void set_update_check_count_wait_enabled(bool enabled) { 180 update_check_count_wait_enabled_ = enabled; 181 } 182 update_check_count_wait_enabled()183 inline bool update_check_count_wait_enabled() const { 184 return update_check_count_wait_enabled_; 185 } 186 set_min_update_checks_needed(int64_t min)187 inline void set_min_update_checks_needed(int64_t min) { 188 min_update_checks_needed_ = min; 189 } min_update_checks_needed()190 inline int64_t min_update_checks_needed() const { 191 return min_update_checks_needed_; 192 } 193 set_max_update_checks_allowed(int64_t max)194 inline void set_max_update_checks_allowed(int64_t max) { 195 max_update_checks_allowed_ = max; 196 } max_update_checks_allowed()197 inline int64_t max_update_checks_allowed() const { 198 return max_update_checks_allowed_; 199 } set_dlc_apps_params(const std::map<std::string,AppParams> & dlc_apps_params)200 inline void set_dlc_apps_params( 201 const std::map<std::string, AppParams>& dlc_apps_params) { 202 dlc_apps_params_ = dlc_apps_params; 203 } dlc_apps_params()204 inline const std::map<std::string, AppParams>& dlc_apps_params() const { 205 return dlc_apps_params_; 206 } set_is_install(bool is_install)207 inline void set_is_install(bool is_install) { is_install_ = is_install; } is_install()208 inline bool is_install() const { return is_install_; } 209 set_autoupdate_token(const std::string & token)210 inline void set_autoupdate_token(const std::string& token) { 211 autoupdate_token_ = token; 212 } autoupdate_token()213 inline const std::string& autoupdate_token() const { 214 return autoupdate_token_; 215 } 216 217 // Returns the App ID corresponding to the current value of the 218 // download channel. 219 virtual std::string GetAppId() const; 220 221 // Returns the DLC app ID. 222 virtual std::string GetDlcAppId(const std::string& dlc_id) const; 223 224 // Returns true if the App ID is a DLC App ID that is currently part of the 225 // request parameters. 226 virtual bool IsDlcAppId(const std::string& app_id) const; 227 228 // Returns the DLC App ID if the given App ID is a DLC that is currently part 229 // of the request parameters. 230 virtual bool GetDlcId(const std::string& app_id, std::string* dlc_id) const; 231 232 // If the App ID is a DLC App ID will set to no update. 233 void SetDlcNoUpdate(const std::string& app_id); 234 235 // Suggested defaults 236 static const char kOsVersion[]; 237 static const int64_t kDefaultMinUpdateChecks = 0; 238 static const int64_t kDefaultMaxUpdateChecks = 8; 239 240 // Initializes all the data in the object. Non-empty 241 // |in_app_version| or |in_update_url| prevents automatic detection 242 // of the parameter. Returns true on success, false otherwise. 243 bool Init(const std::string& in_app_version, 244 const std::string& in_update_url, 245 const chromeos_update_manager::UpdateCheckParams& params); 246 247 // Permanently changes the release channel to |channel|. Performs a 248 // powerwash, if required and allowed. 249 // Returns true on success, false otherwise. Note: This call will fail if 250 // there's a channel change pending already. This is to serialize all the 251 // channel changes done by the user in order to avoid having to solve 252 // numerous edge cases around ensuring the powerwash happens as intended in 253 // all such cases. 254 virtual bool SetTargetChannel(const std::string& channel, 255 bool is_powerwash_allowed, 256 std::string* error_message); 257 258 // Updates the download channel for this particular attempt from the current 259 // value of target channel. This method takes a "snapshot" of the current 260 // value of target channel and uses it for all subsequent Omaha requests for 261 // this attempt (i.e. initial request as well as download progress/error 262 // event requests). The snapshot will be updated only when either this method 263 // or Init is called again. 264 virtual void UpdateDownloadChannel(); 265 266 // Returns whether we should powerwash for this update. Note that this is 267 // just an indication, the final decision to powerwash or not is made in the 268 // response handler. 269 bool ShouldPowerwash() const; 270 271 // Check if the provided update URL is official, meaning either the default 272 // autoupdate server or the autoupdate autotest server. 273 virtual bool IsUpdateUrlOfficial() const; 274 275 // For unit-tests. 276 void set_root(const std::string& root); set_current_channel(const std::string & channel)277 void set_current_channel(const std::string& channel) { 278 image_props_.current_channel = channel; 279 } set_target_channel(const std::string & channel)280 void set_target_channel(const std::string& channel) { 281 mutable_image_props_.target_channel = channel; 282 } set_os_sp(const std::string & os_sp)283 void set_os_sp(const std::string& os_sp) { os_sp_ = os_sp; } set_os_board(const std::string & os_board)284 void set_os_board(const std::string& os_board) { 285 image_props_.board = os_board; 286 } set_app_lang(const std::string & app_lang)287 void set_app_lang(const std::string& app_lang) { app_lang_ = app_lang; } set_hwid(const std::string & hwid)288 void set_hwid(const std::string& hwid) { hwid_ = hwid; } set_is_powerwash_allowed(bool powerwash_allowed)289 void set_is_powerwash_allowed(bool powerwash_allowed) { 290 mutable_image_props_.is_powerwash_allowed = powerwash_allowed; 291 } is_powerwash_allowed()292 bool is_powerwash_allowed() { 293 return mutable_image_props_.is_powerwash_allowed; 294 } 295 set_device_requisition(const std::string & requisition)296 void set_device_requisition(const std::string& requisition) { 297 device_requisition_ = requisition; 298 } 299 300 private: 301 FRIEND_TEST(OmahaRequestParamsTest, ChannelIndexTest); 302 FRIEND_TEST(OmahaRequestParamsTest, IsValidChannelTest); 303 FRIEND_TEST(OmahaRequestParamsTest, SetIsPowerwashAllowedTest); 304 FRIEND_TEST(OmahaRequestParamsTest, SetTargetChannelInvalidTest); 305 FRIEND_TEST(OmahaRequestParamsTest, SetTargetChannelTest); 306 FRIEND_TEST(OmahaRequestParamsTest, ShouldPowerwashTest); 307 FRIEND_TEST(OmahaRequestParamsTest, ToMoreStableChannelFlagTest); 308 309 // Returns true if |channel| is a valid channel, otherwise write error to 310 // |error_message| if passed and return false. 311 bool IsValidChannel(const std::string& channel, 312 std::string* error_message) const; IsValidChannel(const std::string & channel)313 bool IsValidChannel(const std::string& channel) const { 314 return IsValidChannel(channel, nullptr); 315 } 316 317 // Returns the index of the given channel. 318 int GetChannelIndex(const std::string& channel) const; 319 320 // True if we're trying to update to a more stable channel. 321 // i.e. index(target_channel) > index(current_channel). 322 bool ToMoreStableChannel() const; 323 324 // Gets the machine type (e.g. "i686"). 325 std::string GetMachineType() const; 326 327 // The system image properties. 328 ImageProperties image_props_; 329 MutableImageProperties mutable_image_props_; 330 331 // Basic properties of the OS and Application that go into the Omaha request. 332 std::string os_platform_; 333 std::string os_version_; 334 std::string os_sp_; 335 std::string app_lang_; 336 337 // There are three channel values we deal with: 338 // * The channel we got the image we are running from or "current channel" 339 // stored in |image_props_.current_channel|. 340 // 341 // * The release channel we are tracking, where we should get updates from, 342 // stored in |mutable_image_props_.target_channel|. This channel is 343 // normally the same as the current_channel, except when the user changes 344 // the channel. In that case it'll have the release channel the user 345 // switched to, regardless of whether we downloaded an update from that 346 // channel or not, or if we are in the middle of a download from a 347 // previously selected channel (as opposed to download channel 348 // which gets updated only at the start of next download). 349 // 350 // * The channel from which we're downloading the payload. This should 351 // normally be the same as target channel. But if the user made another 352 // channel change after we started the download, then they'd be different, 353 // in which case, we'd detect elsewhere that the target channel has been 354 // changed and cancel the current download attempt. 355 std::string download_channel_; 356 357 // The value defining the parameters of the LTS (Long Term Support). 358 std::string lts_tag_; 359 360 std::string hwid_; // Hardware Qualification ID of the client 361 // TODO(b:133324571) tracks removal of this field once it is no longer 362 // needed in AU requests. Remove by October 1st 2019. 363 std::string device_requisition_; // Chrome OS Requisition type. 364 bool delta_okay_; // If this client can accept a delta 365 bool interactive_; // Whether this is a user-initiated update check 366 367 // The URL to send the Omaha request to. 368 std::string update_url_; 369 370 // Prefix of the target OS version that the enterprise wants this device 371 // to be pinned to. It's empty otherwise. 372 std::string target_version_prefix_; 373 374 // Whether the client is accepting rollback images too. 375 bool rollback_allowed_; 376 377 // Whether rollbacks should preserve some system state during powerwash. 378 bool rollback_data_save_requested_; 379 380 // How many milestones the client can rollback to. 381 int rollback_allowed_milestones_; 382 383 // True if scattering or staging are enabled, in which case waiting_period_ 384 // specifies the amount of absolute time that we've to wait for before sending 385 // a request to Omaha. 386 bool wall_clock_based_wait_enabled_; 387 base::TimeDelta waiting_period_; 388 389 // True if scattering or staging are enabled to denote the number of update 390 // checks we've to skip before we can send a request to Omaha. The min and max 391 // values establish the bounds for a random number to be chosen within that 392 // range to enable such a wait. 393 bool update_check_count_wait_enabled_; 394 int64_t min_update_checks_needed_; 395 int64_t max_update_checks_allowed_; 396 397 // When reading files, prepend root_ to the paths. Useful for testing. 398 std::string root_; 399 400 // A list of DLC modules to install. A mapping from DLC App ID to |AppParams|. 401 std::map<std::string, AppParams> dlc_apps_params_; 402 403 // This variable defines whether the payload is being installed in the current 404 // partition. At the moment, this is used for installing DLC modules on the 405 // current active partition instead of the inactive partition. 406 bool is_install_; 407 408 // Token used when making an update request for a specific build. 409 // For example: Token for a Quick Fix Build: 410 // https://cloud.google.com/docs/chrome-enterprise/policies/?policy=DeviceQuickFixBuildToken 411 std::string autoupdate_token_; 412 413 DISALLOW_COPY_AND_ASSIGN(OmahaRequestParams); 414 }; 415 416 } // namespace chromeos_update_engine 417 418 #endif // UPDATE_ENGINE_CROS_OMAHA_REQUEST_PARAMS_H_ 419