omaha_request_params.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. #ifndef UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H_
  17. #define UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H_
  18. #include <stdint.h>
  19. #include <string>
  20. #include <vector>
  21. #include <base/macros.h>
  22. #include <base/time/time.h>
  23. #include <gtest/gtest_prod.h> // for FRIEND_TEST
  24. #include "update_engine/common/platform_constants.h"
  25. #include "update_engine/image_properties.h"
  26. // This gathers local system information and prepares info used by the
  27. // Omaha request action.
  28. namespace chromeos_update_engine {
  29. class SystemState;
  30. // This class encapsulates the data Omaha gets for the request, along with
  31. // essential state needed for the processing of the request/response. The
  32. // strings in this struct should not be XML escaped.
  33. //
  34. // TODO(jaysri): chromium-os:39752 tracks the need to rename this class to
  35. // reflect its lifetime more appropriately.
  36. class OmahaRequestParams {
  37. public:
  38. explicit OmahaRequestParams(SystemState* system_state)
  39. : system_state_(system_state),
  40. os_platform_(constants::kOmahaPlatformName),
  41. os_version_(kOsVersion),
  42. delta_okay_(true),
  43. interactive_(false),
  44. rollback_allowed_(false),
  45. wall_clock_based_wait_enabled_(false),
  46. update_check_count_wait_enabled_(false),
  47. min_update_checks_needed_(kDefaultMinUpdateChecks),
  48. max_update_checks_allowed_(kDefaultMaxUpdateChecks),
  49. is_install_(false) {}
  50. virtual ~OmahaRequestParams();
  51. // Setters and getters for the various properties.
  52. inline std::string os_platform() const { return os_platform_; }
  53. inline std::string os_version() const { return os_version_; }
  54. inline std::string os_sp() const { return os_sp_; }
  55. inline std::string os_board() const { return image_props_.board; }
  56. inline std::string os_build_fingerprint() const {
  57. return image_props_.build_fingerprint;
  58. }
  59. inline std::string os_build_type() const { return image_props_.build_type; }
  60. inline std::string board_app_id() const { return image_props_.product_id; }
  61. inline std::string canary_app_id() const {
  62. return image_props_.canary_product_id;
  63. }
  64. inline std::string system_app_id() const { return image_props_.system_id; }
  65. inline void set_system_app_id(const std::string& system_app_id) {
  66. image_props_.system_id = system_app_id;
  67. }
  68. inline void set_app_id(const std::string& app_id) {
  69. image_props_.product_id = app_id;
  70. image_props_.canary_product_id = app_id;
  71. }
  72. inline std::string app_lang() const { return app_lang_; }
  73. inline std::string hwid() const { return hwid_; }
  74. inline std::string fw_version() const { return fw_version_; }
  75. inline std::string ec_version() const { return ec_version_; }
  76. inline void set_app_version(const std::string& version) {
  77. image_props_.version = version;
  78. }
  79. inline std::string app_version() const { return image_props_.version; }
  80. inline std::string system_version() const {
  81. return image_props_.system_version;
  82. }
  83. inline std::string product_components() const {
  84. return image_props_.product_components;
  85. }
  86. inline void set_product_components(const std::string& product_components) {
  87. image_props_.product_components = product_components;
  88. }
  89. inline std::string current_channel() const {
  90. return image_props_.current_channel;
  91. }
  92. inline std::string target_channel() const {
  93. return mutable_image_props_.target_channel;
  94. }
  95. inline std::string download_channel() const { return download_channel_; }
  96. // Can client accept a delta ?
  97. inline void set_delta_okay(bool ok) { delta_okay_ = ok; }
  98. inline bool delta_okay() const { return delta_okay_; }
  99. // True if this is a user-initiated update check.
  100. inline void set_interactive(bool interactive) { interactive_ = interactive; }
  101. inline bool interactive() const { return interactive_; }
  102. inline void set_update_url(const std::string& url) { update_url_ = url; }
  103. inline std::string update_url() const { return update_url_; }
  104. inline void set_target_version_prefix(const std::string& prefix) {
  105. target_version_prefix_ = prefix;
  106. }
  107. inline std::string target_version_prefix() const {
  108. return target_version_prefix_;
  109. }
  110. inline void set_rollback_allowed(bool rollback_allowed) {
  111. rollback_allowed_ = rollback_allowed;
  112. }
  113. inline bool rollback_allowed() const { return rollback_allowed_; }
  114. inline void set_wall_clock_based_wait_enabled(bool enabled) {
  115. wall_clock_based_wait_enabled_ = enabled;
  116. }
  117. inline bool wall_clock_based_wait_enabled() const {
  118. return wall_clock_based_wait_enabled_;
  119. }
  120. inline void set_waiting_period(base::TimeDelta period) {
  121. waiting_period_ = period;
  122. }
  123. base::TimeDelta waiting_period() const { return waiting_period_; }
  124. inline void set_update_check_count_wait_enabled(bool enabled) {
  125. update_check_count_wait_enabled_ = enabled;
  126. }
  127. inline bool update_check_count_wait_enabled() const {
  128. return update_check_count_wait_enabled_;
  129. }
  130. inline void set_min_update_checks_needed(int64_t min) {
  131. min_update_checks_needed_ = min;
  132. }
  133. inline int64_t min_update_checks_needed() const {
  134. return min_update_checks_needed_;
  135. }
  136. inline void set_max_update_checks_allowed(int64_t max) {
  137. max_update_checks_allowed_ = max;
  138. }
  139. inline int64_t max_update_checks_allowed() const {
  140. return max_update_checks_allowed_;
  141. }
  142. inline void set_dlc_module_ids(
  143. const std::vector<std::string>& dlc_module_ids) {
  144. dlc_module_ids_ = dlc_module_ids;
  145. }
  146. inline std::vector<std::string> dlc_module_ids() const {
  147. return dlc_module_ids_;
  148. }
  149. inline void set_is_install(bool is_install) { is_install_ = is_install; }
  150. inline bool is_install() const { return is_install_; }
  151. // Returns the app id corresponding to the current value of the
  152. // download channel.
  153. virtual std::string GetAppId() const;
  154. // Suggested defaults
  155. static const char kOsVersion[];
  156. static const int64_t kDefaultMinUpdateChecks = 0;
  157. static const int64_t kDefaultMaxUpdateChecks = 8;
  158. // Initializes all the data in the object. Non-empty
  159. // |in_app_version| or |in_update_url| prevents automatic detection
  160. // of the parameter. Returns true on success, false otherwise.
  161. bool Init(const std::string& in_app_version,
  162. const std::string& in_update_url,
  163. bool in_interactive);
  164. // Permanently changes the release channel to |channel|. Performs a
  165. // powerwash, if required and allowed.
  166. // Returns true on success, false otherwise. Note: This call will fail if
  167. // there's a channel change pending already. This is to serialize all the
  168. // channel changes done by the user in order to avoid having to solve
  169. // numerous edge cases around ensuring the powerwash happens as intended in
  170. // all such cases.
  171. virtual bool SetTargetChannel(const std::string& channel,
  172. bool is_powerwash_allowed,
  173. std::string* error_message);
  174. // Updates the download channel for this particular attempt from the current
  175. // value of target channel. This method takes a "snapshot" of the current
  176. // value of target channel and uses it for all subsequent Omaha requests for
  177. // this attempt (i.e. initial request as well as download progress/error
  178. // event requests). The snapshot will be updated only when either this method
  179. // or Init is called again.
  180. virtual void UpdateDownloadChannel();
  181. // Returns whether we should powerwash for this update.
  182. virtual bool ShouldPowerwash() const;
  183. // Check if the provided update URL is official, meaning either the default
  184. // autoupdate server or the autoupdate autotest server.
  185. virtual bool IsUpdateUrlOfficial() const;
  186. // For unit-tests.
  187. void set_root(const std::string& root);
  188. void set_current_channel(const std::string& channel) {
  189. image_props_.current_channel = channel;
  190. }
  191. void set_target_channel(const std::string& channel) {
  192. mutable_image_props_.target_channel = channel;
  193. }
  194. void set_os_sp(const std::string& os_sp) { os_sp_ = os_sp; }
  195. void set_os_board(const std::string& os_board) {
  196. image_props_.board = os_board;
  197. }
  198. void set_app_lang(const std::string& app_lang) { app_lang_ = app_lang; }
  199. void set_hwid(const std::string& hwid) { hwid_ = hwid; }
  200. void set_fw_version(const std::string& fw_version) {
  201. fw_version_ = fw_version;
  202. }
  203. void set_ec_version(const std::string& ec_version) {
  204. ec_version_ = ec_version;
  205. }
  206. void set_is_powerwash_allowed(bool powerwash_allowed) {
  207. mutable_image_props_.is_powerwash_allowed = powerwash_allowed;
  208. }
  209. private:
  210. FRIEND_TEST(OmahaRequestParamsTest, ChannelIndexTest);
  211. FRIEND_TEST(OmahaRequestParamsTest, CollectECFWVersionsTest);
  212. FRIEND_TEST(OmahaRequestParamsTest, IsValidChannelTest);
  213. FRIEND_TEST(OmahaRequestParamsTest, SetIsPowerwashAllowedTest);
  214. FRIEND_TEST(OmahaRequestParamsTest, SetTargetChannelInvalidTest);
  215. FRIEND_TEST(OmahaRequestParamsTest, SetTargetChannelTest);
  216. FRIEND_TEST(OmahaRequestParamsTest, ShouldPowerwashTest);
  217. FRIEND_TEST(OmahaRequestParamsTest, ToMoreStableChannelFlagTest);
  218. // Returns true if |channel| is a valid channel, otherwise write error to
  219. // |error_message| if passed and return false.
  220. bool IsValidChannel(const std::string& channel,
  221. std::string* error_message) const;
  222. bool IsValidChannel(const std::string& channel) const {
  223. return IsValidChannel(channel, nullptr);
  224. }
  225. // Returns the index of the given channel.
  226. int GetChannelIndex(const std::string& channel) const;
  227. // True if we're trying to update to a more stable channel.
  228. // i.e. index(target_channel) > index(current_channel).
  229. bool ToMoreStableChannel() const;
  230. // Returns True if we should store the fw/ec versions based on our hwid_.
  231. // Compares hwid to a set of whitelisted prefixes.
  232. bool CollectECFWVersions() const;
  233. // Gets the machine type (e.g. "i686").
  234. std::string GetMachineType() const;
  235. // Global system context.
  236. SystemState* system_state_;
  237. // The system image properties.
  238. ImageProperties image_props_;
  239. MutableImageProperties mutable_image_props_;
  240. // Basic properties of the OS and Application that go into the Omaha request.
  241. std::string os_platform_;
  242. std::string os_version_;
  243. std::string os_sp_;
  244. std::string app_lang_;
  245. // There are three channel values we deal with:
  246. // * The channel we got the image we are running from or "current channel"
  247. // stored in |image_props_.current_channel|.
  248. //
  249. // * The release channel we are tracking, where we should get updates from,
  250. // stored in |mutable_image_props_.target_channel|. This channel is
  251. // normally the same as the current_channel, except when the user changes
  252. // the channel. In that case it'll have the release channel the user
  253. // switched to, regardless of whether we downloaded an update from that
  254. // channel or not, or if we are in the middle of a download from a
  255. // previously selected channel (as opposed to download channel
  256. // which gets updated only at the start of next download).
  257. //
  258. // * The channel from which we're downloading the payload. This should
  259. // normally be the same as target channel. But if the user made another
  260. // channel change after we started the download, then they'd be different,
  261. // in which case, we'd detect elsewhere that the target channel has been
  262. // changed and cancel the current download attempt.
  263. std::string download_channel_;
  264. std::string hwid_; // Hardware Qualification ID of the client
  265. std::string fw_version_; // Chrome OS Firmware Version.
  266. std::string ec_version_; // Chrome OS EC Version.
  267. bool delta_okay_; // If this client can accept a delta
  268. bool interactive_; // Whether this is a user-initiated update check
  269. // The URL to send the Omaha request to.
  270. std::string update_url_;
  271. // Prefix of the target OS version that the enterprise wants this device
  272. // to be pinned to. It's empty otherwise.
  273. std::string target_version_prefix_;
  274. // Whether the client is accepting rollback images too.
  275. bool rollback_allowed_;
  276. // True if scattering or staging are enabled, in which case waiting_period_
  277. // specifies the amount of absolute time that we've to wait for before sending
  278. // a request to Omaha.
  279. bool wall_clock_based_wait_enabled_;
  280. base::TimeDelta waiting_period_;
  281. // True if scattering or staging are enabled to denote the number of update
  282. // checks we've to skip before we can send a request to Omaha. The min and max
  283. // values establish the bounds for a random number to be chosen within that
  284. // range to enable such a wait.
  285. bool update_check_count_wait_enabled_;
  286. int64_t min_update_checks_needed_;
  287. int64_t max_update_checks_allowed_;
  288. // When reading files, prepend root_ to the paths. Useful for testing.
  289. std::string root_;
  290. // A list of DLC module IDs to install.
  291. std::vector<std::string> dlc_module_ids_;
  292. // This variable defines whether the payload is being installed in the current
  293. // partition. At the moment, this is used for installing DLC modules on the
  294. // current active partition instead of the inactive partition.
  295. bool is_install_;
  296. DISALLOW_COPY_AND_ASSIGN(OmahaRequestParams);
  297. };
  298. } // namespace chromeos_update_engine
  299. #endif // UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H_