Home | History | Annotate | Download | only in util
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_INSTALLER_UTIL_GOOGLE_UPDATE_SETTINGS_H_
      6 #define CHROME_INSTALLER_UTIL_GOOGLE_UPDATE_SETTINGS_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/strings/string16.h"
     12 #include "base/time/time.h"
     13 #include "base/version.h"
     14 #include "chrome/installer/util/util_constants.h"
     15 
     16 class BrowserDistribution;
     17 
     18 namespace installer {
     19 class ChannelInfo;
     20 class InstallationState;
     21 }
     22 
     23 // This class provides accessors to the Google Update 'ClientState' information
     24 // that recorded when the user downloads the chrome installer. It is
     25 // google_update.exe responsibility to write the initial values.
     26 class GoogleUpdateSettings {
     27  public:
     28   // Update policy constants defined by Google Update; do not change these.
     29   enum UpdatePolicy {
     30     UPDATES_DISABLED    = 0,
     31     AUTOMATIC_UPDATES   = 1,
     32     MANUAL_UPDATES_ONLY = 2,
     33     AUTO_UPDATES_ONLY   = 3,
     34   };
     35 
     36   // Defines product data that is tracked/used by Google Update.
     37   struct ProductData {
     38     // The currently installed version.
     39     std::string version;
     40     // The time that Google Update last updated this product.  (This means
     41     // either running an updater successfully, or doing an update check that
     42     // results in no update available.)
     43     base::Time last_success;
     44     // The result reported by the most recent run of an installer/updater.
     45     int last_result;
     46     // The error code, if any, reported by the most recent run of an
     47     // installer or updater.  This is typically platform independent.
     48     int last_error_code;
     49     // The extra error code, if any, reported by the most recent run of
     50     // an installer or updater.  This is typically an error code specific
     51     // to the platform -- i.e. on Windows, it will be a Win32 HRESULT.
     52     int last_extra_code;
     53   };
     54 
     55   // Returns true if this install is system-wide, false if it is per-user.
     56   static bool IsSystemInstall();
     57 
     58   // Returns whether the user has given consent to collect UMA data and send
     59   // crash dumps to Google. This information is collected by the web server
     60   // used to download the chrome installer.
     61   static bool GetCollectStatsConsent();
     62 
     63   // Sets the user consent to send UMA and crash dumps to Google. Returns
     64   // false if the setting could not be recorded.
     65   static bool SetCollectStatsConsent(bool consented);
     66 
     67 #if defined(OS_WIN)
     68   // Returns whether the user has given consent to collect UMA data and send
     69   // crash dumps to Google. This information is collected by the web server
     70   // used to download the chrome installer.
     71   static bool GetCollectStatsConsentAtLevel(bool system_install);
     72 
     73   // Sets the user consent to send UMA and crash dumps to Google. Returns
     74   // false if the setting could not be recorded.
     75   static bool SetCollectStatsConsentAtLevel(bool system_install,
     76                                             bool consented);
     77 #endif
     78 
     79   // Returns the metrics id set in the registry (that can be used in crash
     80   // reports). If none found, returns empty string.
     81   static bool GetMetricsId(std::wstring* metrics_id);
     82 
     83   // Sets the metrics id to be used in crash reports.
     84   static bool SetMetricsId(const std::wstring& metrics_id);
     85 
     86   // Sets the machine-wide EULA consented flag required on OEM installs.
     87   // Returns false if the setting could not be recorded.
     88   static bool SetEULAConsent(const installer::InstallationState& machine_state,
     89                              BrowserDistribution* dist,
     90                              bool consented);
     91 
     92   // Returns the last time chrome was run in days. It uses a recorded value
     93   // set by SetLastRunTime(). Returns -1 if the value was not found or if
     94   // the value is corrupted.
     95   static int GetLastRunTime();
     96 
     97   // Stores the time that this function was last called using an encoded
     98   // form of the system local time. Retrieve the time using GetLastRunTime().
     99   // Returns false if the value could not be stored.
    100   static bool SetLastRunTime();
    101 
    102   // Removes the storage used by SetLastRunTime() and SetLastRunTime(). Returns
    103   // false if the operation failed. Returns true if the storage was freed or
    104   // if it never existed in the first place.
    105   static bool RemoveLastRunTime();
    106 
    107   // Returns in |browser| the browser used to download chrome as recorded
    108   // Google Update. Returns false if the information is not available.
    109   static bool GetBrowser(std::wstring* browser);
    110 
    111   // Returns in |language| the language selected by the user when downloading
    112   // chrome. This information is collected by the web server used to download
    113   // the chrome installer. Returns false if the information is not available.
    114   static bool GetLanguage(std::wstring* language);
    115 
    116   // Returns in |brand| the RLZ brand code or distribution tag that has been
    117   // assigned to a partner. Returns false if the information is not available.
    118   //
    119   // NOTE: This function is Windows only.  If the code you are writing is not
    120   // specifically for Windows, prefer calling google_util::GetBrand().
    121   static bool GetBrand(std::wstring* brand);
    122 
    123   // Returns in |brand| the RLZ reactivation brand code or distribution tag
    124   // that has been assigned to a partner for reactivating a dormant chrome
    125   // install. Returns false if the information is not available.
    126   //
    127   // NOTE: This function is Windows only.  If the code you are writing is not
    128   // specifically for Windows, prefer calling
    129   // google_util::GetReactivationBrand().
    130   static bool GetReactivationBrand(std::wstring* brand);
    131 
    132   // Returns in |client| the google_update client field, which is currently
    133   // used to track experiments. Returns false if the entry does not exist.
    134   static bool GetClient(std::wstring* client);
    135 
    136   // Sets the google_update client field. Unlike GetClient() this is set only
    137   // for the current user. Returns false if the operation failed.
    138   static bool SetClient(const std::wstring& client);
    139 
    140   // Returns in 'client' the RLZ referral available for some distribution
    141   // partners. This value does not exist for most chrome or chromium installs.
    142   static bool GetReferral(std::wstring* referral);
    143 
    144   // Overwrites the current value of the referral with an empty string. Returns
    145   // true if this operation succeeded.
    146   static bool ClearReferral();
    147 
    148   // Set did_run "dr" in the client state value. This is used to measure
    149   // active users. Returns false if writting to the registry failed.
    150   static bool UpdateDidRunState(bool did_run, bool system_level);
    151 
    152   // Set did_run "dr" in the client state value for |dist|. This is used to
    153   // measure active users. Returns false if writting to the registry failed.
    154   static bool UpdateDidRunStateForDistribution(BrowserDistribution* dist,
    155                                                bool did_run,
    156                                                bool system_level);
    157 
    158   // Returns only the channel name: "" (stable), "dev", "beta", "canary", or
    159   // "unknown" if unknown. This value will not be modified by "-m" for a
    160   // multi-install. See kChromeChannel* in util_constants.h
    161   static std::wstring GetChromeChannel(bool system_install);
    162 
    163   // Return a human readable modifier for the version string, e.g.
    164   // the channel (dev, beta, stable). Returns true if this operation succeeded,
    165   // on success, channel contains one of "", "unknown", "dev" or "beta" (unless
    166   // it is a multi-install product, in which case it will return "m",
    167   // "unknown-m", "dev-m", or "beta-m").
    168   static bool GetChromeChannelAndModifiers(bool system_install,
    169                                            string16* channel);
    170 
    171   // This method changes the Google Update "ap" value to move the installation
    172   // on to or off of one of the recovery channels.
    173   // - If incremental installer fails we append a magic string ("-full"), if
    174   // it is not present already, so that Google Update server next time will send
    175   // full installer to update Chrome on the local machine
    176   // - If we are currently running full installer, we remove this magic
    177   // string (if it is present) regardless of whether installer failed or not.
    178   // There is no fall-back for full installer :)
    179   // - Unconditionally remove "-multifail" since we haven't crashed.
    180   // |state_key| should be obtained via InstallerState::state_key().
    181   static void UpdateInstallStatus(bool system_install,
    182                                   installer::ArchiveType archive_type,
    183                                   int install_return_code,
    184                                   const std::wstring& product_guid);
    185 
    186   // This method updates the value for Google Update "ap" key for Chrome
    187   // based on whether we are doing incremental install (or not) and whether
    188   // the install succeeded.
    189   // - If install worked, remove the magic string (if present).
    190   // - If incremental installer failed, append a magic string (if
    191   //   not present already).
    192   // - If full installer failed, still remove this magic
    193   //   string (if it is present already).
    194   //
    195   // archive_type: tells whether this is incremental install or not.
    196   // install_return_code: if 0, means installation was successful.
    197   // value: current value of Google Update "ap" key.
    198   // Returns true if |value| is modified.
    199   static bool UpdateGoogleUpdateApKey(installer::ArchiveType archive_type,
    200                                       int install_return_code,
    201                                       installer::ChannelInfo* value);
    202 
    203   // For system-level installs, we need to be able to communicate the results
    204   // of the Toast Experiments back to Google Update. The problem is just that
    205   // the experiment is run in the context of the user, which doesn't have
    206   // write access to the HKLM key that Google Update expects the results in.
    207   // However, when we are about to switch contexts from system to user, we can
    208   // duplicate the handle to the registry key and pass it (through handle
    209   // inheritance) to the newly created child process that is launched as the
    210   // user, allowing the child process to write to the key, with the
    211   // WriteGoogleUpdateSystemClientKey function below.
    212   static int DuplicateGoogleUpdateSystemClientKey();
    213 
    214   // Takes a |handle| to a registry key and writes |value| string into the
    215   // specified |key|. See DuplicateGoogleUpdateSystemClientKey for details.
    216   static bool WriteGoogleUpdateSystemClientKey(int handle,
    217                                                const std::wstring& key,
    218                                                const std::wstring& value);
    219 
    220   // Returns the effective update policy for |app_guid| as dictated by
    221   // Group Policy settings.  |is_overridden|, if non-NULL, is populated with
    222   // true if an app-specific policy override is in force, or false otherwise.
    223   static UpdatePolicy GetAppUpdatePolicy(const std::wstring& app_guid,
    224                                          bool* is_overridden);
    225 
    226   // Returns Google Update's uninstall command line, or an empty string if none
    227   // is found.
    228   static string16 GetUninstallCommandLine(bool system_install);
    229 
    230   // Returns the version of Google Update that is installed.
    231   static Version GetGoogleUpdateVersion(bool system_install);
    232 
    233   // Returns the time at which Google Update last started an automatic update
    234   // check, or the null time if this information isn't available.
    235   static base::Time GetGoogleUpdateLastStartedAU(bool system_install);
    236 
    237   // Returns the time at which Google Update last successfully contacted Google
    238   // servers and got a valid check response, or the null time if this
    239   // information isn't available.
    240   static base::Time GetGoogleUpdateLastChecked(bool system_install);
    241 
    242   // Returns detailed update data for a product being managed by Google Update.
    243   // Returns true if the |version| and |last_updated| fields in |data|
    244   // are modified.  The other items are considered optional.
    245   static bool GetUpdateDetailForApp(bool system_install,
    246                                     const wchar_t* app_guid,
    247                                     ProductData* data);
    248 
    249   // Returns product data for Google Update.  (Equivalent to calling
    250   // GetUpdateDetailForAppGuid with the app guid for Google Update itself.)
    251   static bool GetUpdateDetailForGoogleUpdate(bool system_install,
    252                                              ProductData* data);
    253 
    254   // Returns product data for the current product. (Equivalent to calling
    255   // GetUpdateDetailForApp with the app guid stored in BrowserDistribution.)
    256   static bool GetUpdateDetail(bool system_install, ProductData* data);
    257 
    258   // Sets |experiment_labels| as the Google Update experiment_labels value in
    259   // the ClientState key for this Chrome product, if appropriate. If
    260   // |experiment_labels| is empty, this will delete the value instead. This will
    261   // return true if the label was successfully set (or deleted), false otherwise
    262   // (even if the label does not need to be set for this particular distribution
    263   // type).
    264   static bool SetExperimentLabels(bool system_install,
    265                                   const string16& experiment_labels);
    266 
    267   // Reads the Google Update experiment_labels value in the ClientState key for
    268   // this Chrome product and writes it into |experiment_labels|. If the key or
    269   // value does not exist, |experiment_labels| will be set to the empty string.
    270   // If this distribution of Chrome does not set the experiment_labels value,
    271   // this will do nothing to |experiment_labels|. This will return true if the
    272   // label did not exist, or was successfully read.
    273   static bool ReadExperimentLabels(bool system_install,
    274                                    string16* experiment_labels);
    275 
    276  private:
    277   DISALLOW_IMPLICIT_CONSTRUCTORS(GoogleUpdateSettings);
    278 };
    279 
    280 #endif  // CHROME_INSTALLER_UTIL_GOOGLE_UPDATE_SETTINGS_H_
    281