Home | History | Annotate | Download | only in update_engine
      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:
     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 
     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.
    101   inline std::string os_platform() const { return os_platform_; }
    102   inline std::string os_version() const { return os_version_; }
    103   inline std::string os_sp() const { return os_sp_; }
    104   inline std::string os_board() const { return image_props_.board; }
    105   inline std::string os_build_fingerprint() const {
    106     return image_props_.build_fingerprint;
    107   }
    108   inline std::string board_app_id() const { return image_props_.product_id; }
    109   inline std::string canary_app_id() const {
    110     return image_props_.canary_product_id;
    111   }
    112   inline void set_app_id(const std::string& app_id) {
    113     image_props_.product_id = app_id;
    114     image_props_.canary_product_id = app_id;
    115   }
    116   inline std::string app_lang() const { return app_lang_; }
    117   inline std::string hwid() const { return hwid_; }
    118   inline std::string fw_version() const { return fw_version_; }
    119   inline std::string ec_version() const { return ec_version_; }
    120 
    121   inline void set_app_version(const std::string& version) {
    122     image_props_.version = version;
    123   }
    124   inline std::string app_version() const { return image_props_.version; }
    125 
    126   inline std::string current_channel() const {
    127     return image_props_.current_channel;
    128   }
    129   inline std::string target_channel() const {
    130     return mutable_image_props_.target_channel;
    131   }
    132   inline std::string download_channel() const { return download_channel_; }
    133 
    134   // Can client accept a delta ?
    135   inline void set_delta_okay(bool ok) { delta_okay_ = ok; }
    136   inline bool delta_okay() const { return delta_okay_; }
    137 
    138   // True if this is a user-initiated update check.
    139   inline void set_interactive(bool interactive) { interactive_ = interactive; }
    140   inline bool interactive() const { return interactive_; }
    141 
    142   inline void set_update_url(const std::string& url) { update_url_ = url; }
    143   inline std::string update_url() const { return update_url_; }
    144 
    145   inline void set_target_version_prefix(const std::string& prefix) {
    146     target_version_prefix_ = prefix;
    147   }
    148 
    149   inline std::string target_version_prefix() const {
    150     return target_version_prefix_;
    151   }
    152 
    153   inline void set_wall_clock_based_wait_enabled(bool enabled) {
    154     wall_clock_based_wait_enabled_ = enabled;
    155   }
    156   inline bool wall_clock_based_wait_enabled() const {
    157     return wall_clock_based_wait_enabled_;
    158   }
    159 
    160   inline void set_waiting_period(base::TimeDelta period) {
    161     waiting_period_ = period;
    162   }
    163   base::TimeDelta waiting_period() const { return waiting_period_; }
    164 
    165   inline void set_update_check_count_wait_enabled(bool enabled) {
    166     update_check_count_wait_enabled_ = enabled;
    167   }
    168 
    169   inline bool update_check_count_wait_enabled() const {
    170     return update_check_count_wait_enabled_;
    171   }
    172 
    173   inline void set_min_update_checks_needed(int64_t min) {
    174     min_update_checks_needed_ = min;
    175   }
    176   inline int64_t min_update_checks_needed() const {
    177     return min_update_checks_needed_;
    178   }
    179 
    180   inline void set_max_update_checks_allowed(int64_t max) {
    181     max_update_checks_allowed_ = max;
    182   }
    183   inline int64_t max_update_checks_allowed() const {
    184     return max_update_checks_allowed_;
    185   }
    186 
    187   // True if we're trying to update to a more stable channel.
    188   // i.e. index(target_channel) > index(current_channel).
    189   virtual bool to_more_stable_channel() const;
    190 
    191   // Returns the app id corresponding to the current value of the
    192   // download channel.
    193   virtual std::string GetAppId() const;
    194 
    195   // Suggested defaults
    196   static const char kOsVersion[];
    197   static const char kIsPowerwashAllowedKey[];
    198   static const int64_t kDefaultMinUpdateChecks = 0;
    199   static const int64_t kDefaultMaxUpdateChecks = 8;
    200 
    201   // Initializes all the data in the object. Non-empty
    202   // |in_app_version| or |in_update_url| prevents automatic detection
    203   // of the parameter. Returns true on success, false otherwise.
    204   bool Init(const std::string& in_app_version,
    205             const std::string& in_update_url,
    206             bool in_interactive);
    207 
    208   // Permanently changes the release channel to |channel|. Performs a
    209   // powerwash, if required and allowed.
    210   // Returns true on success, false otherwise. Note: This call will fail if
    211   // there's a channel change pending already. This is to serialize all the
    212   // channel changes done by the user in order to avoid having to solve
    213   // numerous edge cases around ensuring the powerwash happens as intended in
    214   // all such cases.
    215   virtual bool SetTargetChannel(const std::string& channel,
    216                                 bool is_powerwash_allowed,
    217                                 std::string* error_message);
    218 
    219   // Updates the download channel for this particular attempt from the current
    220   // value of target channel.  This method takes a "snapshot" of the current
    221   // value of target channel and uses it for all subsequent Omaha requests for
    222   // this attempt (i.e. initial request as well as download progress/error
    223   // event requests). The snapshot will be updated only when either this method
    224   // or Init is called again.
    225   virtual void UpdateDownloadChannel();
    226 
    227   virtual bool is_powerwash_allowed() const {
    228     return mutable_image_props_.is_powerwash_allowed;
    229   }
    230 
    231   // Check if the provided update URL is official, meaning either the default
    232   // autoupdate server or the autoupdate autotest server.
    233   virtual bool IsUpdateUrlOfficial() const;
    234 
    235   // For unit-tests.
    236   void set_root(const std::string& root);
    237   void set_current_channel(const std::string& channel) {
    238     image_props_.current_channel = channel;
    239   }
    240   void set_target_channel(const std::string& channel) {
    241     mutable_image_props_.target_channel = channel;
    242   }
    243 
    244  private:
    245   FRIEND_TEST(OmahaRequestParamsTest, IsValidChannelTest);
    246   FRIEND_TEST(OmahaRequestParamsTest, ChannelIndexTest);
    247   FRIEND_TEST(OmahaRequestParamsTest, ToMoreStableChannelFlagTest);
    248   FRIEND_TEST(OmahaRequestParamsTest, CollectECFWVersionsTest);
    249 
    250   // Returns true if |channel| is a valid channel, false otherwise.
    251   bool IsValidChannel(const std::string& channel) const;
    252 
    253   // Returns the index of the given channel.
    254   int GetChannelIndex(const std::string& channel) const;
    255 
    256   // Returns True if we should store the fw/ec versions based on our hwid_.
    257   // Compares hwid to a set of whitelisted prefixes.
    258   bool CollectECFWVersions() const;
    259 
    260   // These are individual helper methods to initialize the said properties from
    261   // the LSB value.
    262   void SetTargetChannelFromLsbValue();
    263   void SetCurrentChannelFromLsbValue();
    264   void SetIsPowerwashAllowedFromLsbValue();
    265 
    266   // Initializes the required properties from the LSB value.
    267   void InitFromLsbValue();
    268 
    269   // Gets the machine type (e.g. "i686").
    270   std::string GetMachineType() const;
    271 
    272   // Global system context.
    273   SystemState* system_state_;
    274 
    275   // The system image properties.
    276   ImageProperties image_props_;
    277   MutableImageProperties mutable_image_props_;
    278 
    279   // Basic properties of the OS and Application that go into the Omaha request.
    280   std::string os_platform_;
    281   std::string os_version_;
    282   std::string os_sp_;
    283   std::string app_lang_;
    284 
    285   // There are three channel values we deal with:
    286   // * The channel we got the image we are running from or "current channel"
    287   //   stored in |image_props_.current_channel|.
    288   //
    289   // * The release channel we are tracking, where we should get updates from,
    290   //   stored in |mutable_image_props_.target_channel|. This channel is
    291   //   normally the same as the current_channel, except when the user changes
    292   //   the channel. In that case it'll have the release channel the user
    293   //   switched to, regardless of whether we downloaded an update from that
    294   //   channel or not, or if we are in the middle of a download from a
    295   //   previously selected channel  (as opposed to download channel
    296   //   which gets updated only at the start of next download).
    297   //
    298   // * The channel from which we're downloading the payload. This should
    299   //   normally be the same as target channel. But if the user made another
    300   //   channel change after we started the download, then they'd be different,
    301   //   in which case, we'd detect elsewhere that the target channel has been
    302   //   changed and cancel the current download attempt.
    303   std::string download_channel_;
    304 
    305   std::string hwid_;  // Hardware Qualification ID of the client
    306   std::string fw_version_;  // Chrome OS Firmware Version.
    307   std::string ec_version_;  // Chrome OS EC Version.
    308   bool delta_okay_;  // If this client can accept a delta
    309   bool interactive_;   // Whether this is a user-initiated update check
    310 
    311   // The URL to send the Omaha request to.
    312   std::string update_url_;
    313 
    314   // Prefix of the target OS version that the enterprise wants this device
    315   // to be pinned to. It's empty otherwise.
    316   std::string target_version_prefix_;
    317 
    318   // True if scattering is enabled, in which case waiting_period_ specifies the
    319   // amount of absolute time that we've to wait for before sending a request to
    320   // Omaha.
    321   bool wall_clock_based_wait_enabled_;
    322   base::TimeDelta waiting_period_;
    323 
    324   // True if scattering is enabled to denote the number of update checks
    325   // we've to skip before we can send a request to Omaha. The min and max
    326   // values establish the bounds for a random number to be chosen within that
    327   // range to enable such a wait.
    328   bool update_check_count_wait_enabled_;
    329   int64_t min_update_checks_needed_;
    330   int64_t max_update_checks_allowed_;
    331 
    332   // When reading files, prepend root_ to the paths. Useful for testing.
    333   std::string root_;
    334 
    335   // TODO(jaysri): Uncomment this after fixing unit tests, as part of
    336   // chromium-os:39752
    337   // DISALLOW_COPY_AND_ASSIGN(OmahaRequestParams);
    338 };
    339 
    340 }  // namespace chromeos_update_engine
    341 
    342 #endif  // UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H_
    343