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