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