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_OMAHA_REQUEST_PARAMS_H_ 18 #define UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H_ 19 20 #include <stdint.h> 21 22 #include <string> 23 24 #include <base/macros.h> 25 #include <base/time/time.h> 26 #include <gtest/gtest_prod.h> // for FRIEND_TEST 27 28 #include "update_engine/common/platform_constants.h" 29 #include "update_engine/image_properties.h" 30 31 // This gathers local system information and prepares info used by the 32 // Omaha request action. 33 34 namespace chromeos_update_engine { 35 36 class SystemState; 37 38 // 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 // 42 // TODO(jaysri): chromium-os:39752 tracks the need to rename this class to 43 // reflect its lifetime more appropriately. 44 class OmahaRequestParams { 45 public: OmahaRequestParams(SystemState * system_state)46 explicit OmahaRequestParams(SystemState* system_state) 47 : system_state_(system_state), 48 os_platform_(constants::kOmahaPlatformName), 49 os_version_(kOsVersion), 50 delta_okay_(true), 51 interactive_(false), 52 wall_clock_based_wait_enabled_(false), 53 update_check_count_wait_enabled_(false), 54 min_update_checks_needed_(kDefaultMinUpdateChecks), 55 max_update_checks_allowed_(kDefaultMaxUpdateChecks) {} 56 OmahaRequestParams(SystemState * system_state,const std::string & in_os_platform,const std::string & in_os_version,const std::string & in_os_sp,const std::string & in_os_board,const std::string & in_app_id,const std::string & in_app_version,const std::string & in_app_lang,const std::string & in_target_channel,const std::string & in_hwid,const std::string & in_fw_version,const std::string & in_ec_version,bool in_delta_okay,bool in_interactive,const std::string & in_update_url,const std::string & in_target_version_prefix)57 OmahaRequestParams(SystemState* system_state, 58 const std::string& in_os_platform, 59 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, 65 const std::string& in_target_channel, 66 const std::string& in_hwid, 67 const std::string& in_fw_version, 68 const std::string& in_ec_version, 69 bool in_delta_okay, 70 bool in_interactive, 71 const std::string& in_update_url, 72 const std::string& in_target_version_prefix) 73 : system_state_(system_state), 74 os_platform_(in_os_platform), 75 os_version_(in_os_version), 76 os_sp_(in_os_sp), 77 app_lang_(in_app_lang), 78 hwid_(in_hwid), 79 fw_version_(in_fw_version), 80 ec_version_(in_ec_version), 81 delta_okay_(in_delta_okay), 82 interactive_(in_interactive), 83 update_url_(in_update_url), 84 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), 88 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 } 97 98 virtual ~OmahaRequestParams(); 99 100 // Setters and getters for the various properties. os_platform()101 inline std::string os_platform() const { return os_platform_; } os_version()102 inline std::string os_version() const { return os_version_; } os_sp()103 inline std::string os_sp() const { return os_sp_; } os_board()104 inline std::string os_board() const { return image_props_.board; } os_build_fingerprint()105 inline std::string os_build_fingerprint() const { 106 return image_props_.build_fingerprint; 107 } os_build_type()108 inline std::string os_build_type() const { return image_props_.build_type; } board_app_id()109 inline std::string board_app_id() const { return image_props_.product_id; } canary_app_id()110 inline std::string canary_app_id() const { 111 return image_props_.canary_product_id; 112 } system_app_id()113 inline std::string system_app_id() const { return image_props_.system_id; } set_system_app_id(const std::string & system_app_id)114 inline void set_system_app_id(const std::string& system_app_id) { 115 image_props_.system_id = system_app_id; 116 } set_app_id(const std::string & app_id)117 inline void set_app_id(const std::string& app_id) { 118 image_props_.product_id = app_id; 119 image_props_.canary_product_id = app_id; 120 } app_lang()121 inline std::string app_lang() const { return app_lang_; } hwid()122 inline std::string hwid() const { return hwid_; } fw_version()123 inline std::string fw_version() const { return fw_version_; } ec_version()124 inline std::string ec_version() const { return ec_version_; } 125 set_app_version(const std::string & version)126 inline void set_app_version(const std::string& version) { 127 image_props_.version = version; 128 } app_version()129 inline std::string app_version() const { return image_props_.version; } system_version()130 inline std::string system_version() const { 131 return image_props_.system_version; 132 } product_components()133 inline std::string product_components() const { 134 return image_props_.product_components; 135 } set_product_components(const std::string & product_components)136 inline void set_product_components(const std::string& product_components) { 137 image_props_.product_components = product_components; 138 } 139 current_channel()140 inline std::string current_channel() const { 141 return image_props_.current_channel; 142 } target_channel()143 inline std::string target_channel() const { 144 return mutable_image_props_.target_channel; 145 } download_channel()146 inline std::string download_channel() const { return download_channel_; } 147 148 // Can client accept a delta ? set_delta_okay(bool ok)149 inline void set_delta_okay(bool ok) { delta_okay_ = ok; } delta_okay()150 inline bool delta_okay() const { return delta_okay_; } 151 152 // True if this is a user-initiated update check. set_interactive(bool interactive)153 inline void set_interactive(bool interactive) { interactive_ = interactive; } interactive()154 inline bool interactive() const { return interactive_; } 155 set_update_url(const std::string & url)156 inline void set_update_url(const std::string& url) { update_url_ = url; } update_url()157 inline std::string update_url() const { return update_url_; } 158 set_target_version_prefix(const std::string & prefix)159 inline void set_target_version_prefix(const std::string& prefix) { 160 target_version_prefix_ = prefix; 161 } 162 target_version_prefix()163 inline std::string target_version_prefix() const { 164 return target_version_prefix_; 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 } 200 201 // Returns the app id corresponding to the current value of the 202 // download channel. 203 virtual std::string GetAppId() const; 204 205 // Suggested defaults 206 static const char kOsVersion[]; 207 static const char kIsPowerwashAllowedKey[]; 208 static const int64_t kDefaultMinUpdateChecks = 0; 209 static const int64_t kDefaultMaxUpdateChecks = 8; 210 211 // Initializes all the data in the object. Non-empty 212 // |in_app_version| or |in_update_url| prevents automatic detection 213 // of the parameter. Returns true on success, false otherwise. 214 bool Init(const std::string& in_app_version, 215 const std::string& in_update_url, 216 bool in_interactive); 217 218 // Permanently changes the release channel to |channel|. Performs a 219 // powerwash, if required and allowed. 220 // Returns true on success, false otherwise. Note: This call will fail if 221 // there's a channel change pending already. This is to serialize all the 222 // channel changes done by the user in order to avoid having to solve 223 // numerous edge cases around ensuring the powerwash happens as intended in 224 // all such cases. 225 virtual bool SetTargetChannel(const std::string& channel, 226 bool is_powerwash_allowed, 227 std::string* error_message); 228 229 // Updates the download channel for this particular attempt from the current 230 // value of target channel. This method takes a "snapshot" of the current 231 // value of target channel and uses it for all subsequent Omaha requests for 232 // this attempt (i.e. initial request as well as download progress/error 233 // event requests). The snapshot will be updated only when either this method 234 // or Init is called again. 235 virtual void UpdateDownloadChannel(); 236 237 // Returns whether we should powerwash for this update. 238 virtual bool ShouldPowerwash() const; 239 240 // Check if the provided update URL is official, meaning either the default 241 // autoupdate server or the autoupdate autotest server. 242 virtual bool IsUpdateUrlOfficial() const; 243 244 // For unit-tests. 245 void set_root(const std::string& root); set_current_channel(const std::string & channel)246 void set_current_channel(const std::string& channel) { 247 image_props_.current_channel = channel; 248 } set_target_channel(const std::string & channel)249 void set_target_channel(const std::string& channel) { 250 mutable_image_props_.target_channel = channel; 251 } 252 253 private: 254 FRIEND_TEST(OmahaRequestParamsTest, ChannelIndexTest); 255 FRIEND_TEST(OmahaRequestParamsTest, CollectECFWVersionsTest); 256 FRIEND_TEST(OmahaRequestParamsTest, IsValidChannelTest); 257 FRIEND_TEST(OmahaRequestParamsTest, SetIsPowerwashAllowedTest); 258 FRIEND_TEST(OmahaRequestParamsTest, SetTargetChannelInvalidTest); 259 FRIEND_TEST(OmahaRequestParamsTest, SetTargetChannelTest); 260 FRIEND_TEST(OmahaRequestParamsTest, ShouldPowerwashTest); 261 FRIEND_TEST(OmahaRequestParamsTest, ToMoreStableChannelFlagTest); 262 263 // Returns true if |channel| is a valid channel, otherwise write error to 264 // |error_message| if passed and return false. 265 bool IsValidChannel(const std::string& channel, 266 std::string* error_message) const; IsValidChannel(const std::string & channel)267 bool IsValidChannel(const std::string& channel) const { 268 return IsValidChannel(channel, nullptr); 269 } 270 271 // Returns the index of the given channel. 272 int GetChannelIndex(const std::string& channel) const; 273 274 // True if we're trying to update to a more stable channel. 275 // i.e. index(target_channel) > index(current_channel). 276 bool ToMoreStableChannel() const; 277 278 // Returns True if we should store the fw/ec versions based on our hwid_. 279 // Compares hwid to a set of whitelisted prefixes. 280 bool CollectECFWVersions() const; 281 282 // These are individual helper methods to initialize the said properties from 283 // the LSB value. 284 void SetTargetChannelFromLsbValue(); 285 void SetCurrentChannelFromLsbValue(); 286 void SetIsPowerwashAllowedFromLsbValue(); 287 288 // Initializes the required properties from the LSB value. 289 void InitFromLsbValue(); 290 291 // Gets the machine type (e.g. "i686"). 292 std::string GetMachineType() const; 293 294 // Global system context. 295 SystemState* system_state_; 296 297 // The system image properties. 298 ImageProperties image_props_; 299 MutableImageProperties mutable_image_props_; 300 301 // Basic properties of the OS and Application that go into the Omaha request. 302 std::string os_platform_; 303 std::string os_version_; 304 std::string os_sp_; 305 std::string app_lang_; 306 307 // There are three channel values we deal with: 308 // * The channel we got the image we are running from or "current channel" 309 // stored in |image_props_.current_channel|. 310 // 311 // * The release channel we are tracking, where we should get updates from, 312 // stored in |mutable_image_props_.target_channel|. This channel is 313 // normally the same as the current_channel, except when the user changes 314 // the channel. In that case it'll have the release channel the user 315 // switched to, regardless of whether we downloaded an update from that 316 // channel or not, or if we are in the middle of a download from a 317 // previously selected channel (as opposed to download channel 318 // which gets updated only at the start of next download). 319 // 320 // * The channel from which we're downloading the payload. This should 321 // normally be the same as target channel. But if the user made another 322 // channel change after we started the download, then they'd be different, 323 // in which case, we'd detect elsewhere that the target channel has been 324 // changed and cancel the current download attempt. 325 std::string download_channel_; 326 327 std::string hwid_; // Hardware Qualification ID of the client 328 std::string fw_version_; // Chrome OS Firmware Version. 329 std::string ec_version_; // Chrome OS EC Version. 330 bool delta_okay_; // If this client can accept a delta 331 bool interactive_; // Whether this is a user-initiated update check 332 333 // The URL to send the Omaha request to. 334 std::string update_url_; 335 336 // Prefix of the target OS version that the enterprise wants this device 337 // to be pinned to. It's empty otherwise. 338 std::string target_version_prefix_; 339 340 // True if scattering is enabled, in which case waiting_period_ specifies the 341 // amount of absolute time that we've to wait for before sending a request to 342 // Omaha. 343 bool wall_clock_based_wait_enabled_; 344 base::TimeDelta waiting_period_; 345 346 // True if scattering is enabled to denote the number of update checks 347 // we've to skip before we can send a request to Omaha. The min and max 348 // values establish the bounds for a random number to be chosen within that 349 // range to enable such a wait. 350 bool update_check_count_wait_enabled_; 351 int64_t min_update_checks_needed_; 352 int64_t max_update_checks_allowed_; 353 354 // When reading files, prepend root_ to the paths. Useful for testing. 355 std::string root_; 356 357 // TODO(jaysri): Uncomment this after fixing unit tests, as part of 358 // chromium-os:39752 359 // DISALLOW_COPY_AND_ASSIGN(OmahaRequestParams); 360 }; 361 362 } // namespace chromeos_update_engine 363 364 #endif // UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H_ 365