910e62b5创建于 1月15日历史提交
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_INSTALLER_UTIL_GOOGLE_UPDATE_SETTINGS_H_
#define CHROME_INSTALLER_UTIL_GOOGLE_UPDATE_SETTINGS_H_

#include <stddef.h>

#include <memory>
#include <optional>
#include <string>
#include <string_view>

#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "base/version.h"
#include "build/build_config.h"
#include "chrome/installer/util/google_update_constants.h"
#include "chrome/installer/util/util_constants.h"
#include "components/metrics/client_info.h"

namespace installer {
class AdditionalParameters;
class InstallationState;
}  // namespace installer

// This class provides accessors to the Google Update group policies and
// 'ClientState' information. The group policies are set using specific
// administrative templates. The 'ClientState' information is recorded when the
// user downloads the Chrome installer. It is google_update.exe responsibility
// to write the initial values.
class GoogleUpdateSettings {
 public:
  // Update policy constants defined by Google Update; do not change these.
  enum UpdatePolicy {
    UPDATES_DISABLED = 0,
    AUTOMATIC_UPDATES = 1,
    MANUAL_UPDATES_ONLY = 2,
    AUTO_UPDATES_ONLY = 3,
    UPDATE_POLICIES_COUNT
  };

  static const wchar_t kPoliciesKey[];
  static const wchar_t kUpdatePolicyValue[];
  static const wchar_t kUpdateOverrideValuePrefix[];
  static const wchar_t kCheckPeriodOverrideMinutes[];
  static const wchar_t kDownloadPreferencePolicyValue[];
  static const int kCheckPeriodOverrideMinutesDefault;
  static const int kCheckPeriodOverrideMinutesMax;
  static const GoogleUpdateSettings::UpdatePolicy kDefaultUpdatePolicy;

  // Defines product data that is tracked/used by Google Update.
  struct ProductData {
    // The currently installed version.
    std::string version;
    // The time that Google Update last updated this product.  (This means
    // either running an updater successfully, or doing an update check that
    // results in no update available.)
    base::Time last_success;
    // The result reported by the most recent run of an installer/updater.
    int last_result;
    // The error code, if any, reported by the most recent run of an
    // installer or updater.  This is typically platform independent.
    int last_error_code;
    // The extra error code, if any, reported by the most recent run of
    // an installer or updater.  This is typically an error code specific
    // to the platform -- i.e. on Windows, it will be a Win32 HRESULT.
    int last_extra_code;
  };

  GoogleUpdateSettings() = delete;
  GoogleUpdateSettings(const GoogleUpdateSettings&) = delete;
  GoogleUpdateSettings& operator=(const GoogleUpdateSettings&) = delete;

  // Returns true if this install is system-wide, false if it is per-user.
  static bool IsSystemInstall();

  // Returns the SequencedTaskRunner to be used to sequence calls to
  // Get/SetCollectStatsConsent(). Tasks posted through this will run with
  // USER_VISIBLE priority and will block shutdown.
  // Note: There is not enforcement to ensure that all such calls go through
  // this SequencedTaskRunner but callers that don't are responsible to ensure
  // nothing else is racing with them (e.g. those calls can be called
  // synchronously on first run, startup, etc.).
  static base::SequencedTaskRunner* CollectStatsConsentTaskRunner();

#if BUILDFLAG(IS_POSIX)
  // Returns whether the user has given consent to collect UMA data and send
  // crash dumps to Google. This method reads the information from a custom
  // directory.
  static bool GetCollectStatsConsentFromDir(const base::FilePath& consent_dir);
#endif  // BUILDFLAG(IS_POSIX)

  // Returns whether the user has given consent to collect UMA data and send
  // crash dumps to Google. This information is collected by the web server
  // used to download the chrome installer.
  static bool GetCollectStatsConsent();

  // Sets the user consent to send UMA and crash dumps to Google. Returns
  // false if the setting could not be recorded.
  static bool SetCollectStatsConsent(bool consented);

#if BUILDFLAG(IS_WIN)
  // Returns the default (original) state of the "send usage stats" checkbox
  // shown to the user when they downloaded Chrome. The value is returned via
  // the out parameter |stats_consent_default|. This function returns true if
  // the default state is known and false otherwise. If false the out param
  // will not be set.
  [[nodiscard]] static bool GetCollectStatsConsentDefault(
      bool* stats_consent_default);
#endif

  // Returns a hash of the current update cohort ID string to which the
  // browser is assigned, if any. Discards any cohort data past the final ":".
  // If there is no ":", returns nullopt.
  static std::optional<uint32_t> GetHashedCohortId();

  // Returns the metrics client info backed up in the registry. nullptr
  // if-and-only-if the client_id couldn't be retrieved (failure to retrieve
  // other fields only makes them keep their default value). A non-null return
  // will NEVER contain an empty client_id field.
  static std::unique_ptr<metrics::ClientInfo> LoadMetricsClientInfo();

  // Stores a backup of the metrics client info in the registry. Storing a
  // |client_info| with an empty client id will effectively void the backup.
  static void StoreMetricsClientInfo(const metrics::ClientInfo& client_info);

  // Sets the machine-wide EULA consented flag required on OEM installs.
  // Returns false if the setting could not be recorded.
  static bool SetEulaConsent(const installer::InstallationState& machine_state,
                             bool consented);

  // Returns the last time chrome was run in days. It uses a recorded value
  // set by SetLastRunTime(). Returns -1 if the value was not found or if
  // the value is corrupted.
  static int GetLastRunTime();

  // Stores the time that this function was last called using an encoded
  // form of the system local time. Retrieve the time using GetLastRunTime().
  // Returns false if the value could not be stored.
  static bool SetLastRunTime();

  // Removes the storage used by SetLastRunTime() and SetLastRunTime(). Returns
  // false if the operation failed. Returns true if the storage was freed or
  // if it never existed in the first place.
  static bool RemoveLastRunTime();

  // Returns in |browser| the browser used to download chrome as recorded
  // Google Update. Returns false if the information is not available.
  static bool GetBrowser(std::wstring* browser);

  // Returns in |language| the language selected by the user when downloading
  // chrome. This information is collected by the web server used to download
  // the chrome installer. Returns false if the information is not available.
  static bool GetLanguage(std::wstring* language);

  // Returns in |brand| the RLZ brand code or distribution tag that has been
  // assigned to a partner. Returns false if the information is not available.
  //
  // NOTE: This function is Windows only.  If the code you are writing is not
  // specifically for Windows, prefer calling google_brand::GetBrand().
  static bool GetBrand(std::wstring* brand);

  // Returns in |brand| the RLZ reactivation brand code or distribution tag
  // that has been assigned to a partner for reactivating a dormant chrome
  // install. Returns false if the information is not available.
  //
  // NOTE: This function is Windows only.  If the code you are writing is not
  // specifically for Windows, prefer calling
  // google_brand::GetReactivationBrand().
  static bool GetReactivationBrand(std::wstring* brand);

  // Returns in 'client' the RLZ referral available for some distribution
  // partners. This value does not exist for most chrome or chromium installs.
  static bool GetReferral(std::wstring* referral);

  // Overwrites the current value of the referral with an empty string. Returns
  // true if this operation succeeded.
  static bool ClearReferral();

  // This method unconditionally clears legacy "-full" modifiers from the
  // Google Update "ap" key.
  static void UpdateInstallStatus();

  // Sets the InstallerProgress value in the registry so that Google Update can
  // provide informative user feedback. |path| is the full path to the app's
  // ClientState key. |progress| should be a number between 0 and 100,
  // inclusive.
  static void SetProgress(bool system_install,
                          const std::wstring& path,
                          int progress);

  // This method unconditionally clears legacy "-full" modifiers from |value|.
  // Returns true if |value| is modified.
  static bool UpdateGoogleUpdateApKey(installer::AdditionalParameters& value);

  // Returns the effective update policy for |app_guid| as dictated by
  // Group Policy settings.  |is_overridden|, if non-nullptr, is populated with
  // true if an app-specific policy override is in force, or false otherwise.
  static UpdatePolicy GetAppUpdatePolicy(std::wstring_view app_guid,
                                         bool* is_overridden);

  // Returns true if Chrome should be updated automatically by Google Update
  // based on current autoupdate settings. Note that for Chromium builds, this
  // returns false since Chromium is assumed not to autoupdate.
  static bool AreAutoupdatesEnabled();

  // Attempts to reenable auto-updates for Chrome by removing any group policy
  // settings that would block updates from occurring. This is a superset of the
  // things checked by GetAppUpdatePolicy() as GetAppUpdatePolicy() does not
  // check Omaha's AutoUpdateCheckPeriodMinutes setting which will be reset by
  // this method. Will need to be called from an elevated process since those
  // settings live in HKLM. Returns true if there is a reasonable belief that
  // updates are not disabled by policy when this method returns, false
  // otherwise. Note that for Chromium builds, this returns true since Chromium
  // is assumed not to autoupdate.
  static bool ReenableAutoupdates();

  // Returns a string if the corresponding Google Update group policy is set.
  // Returns an empty string if no policy or an invalid policy is set.
  // A valid policy for DownloadPreference is a string that matches the
  // following regex:  `[a-zA-z]{0-32}`. The actual values for this policy
  // are specific to Google Update and documented as part of the Google Update
  // protocol.
  static std::wstring GetDownloadPreference();

  // Returns Google Update's uninstall command line, or an empty string if none
  // is found.
  static std::wstring GetUninstallCommandLine(bool system_install);

  // Returns the version of Google Update that is installed.
  static base::Version GetGoogleUpdateVersion(bool system_install);

  // Returns the time at which Google Update last started an automatic update
  // check, or the null time if this information isn't available.
  static base::Time GetGoogleUpdateLastStartedAU(bool system_install);

  // Returns the time at which Google Update last successfully contacted Google
  // servers and got a valid check response, or the null time if this
  // information isn't available.
  static base::Time GetGoogleUpdateLastChecked(bool system_install);

  // Returns detailed update data for a product being managed by Google Update.
  // Returns true if the |version| and |last_updated| fields in |data|
  // are modified.  The other items are considered optional.
  static bool GetUpdateDetailForApp(bool system_install,
                                    const wchar_t* app_guid,
                                    ProductData* data);

  // Returns product data for Google Update.  (Equivalent to calling
  // GetUpdateDetailForAppGuid with the app guid for Google Update itself.)
  static bool GetUpdateDetailForGoogleUpdate(ProductData* data);

  // Returns product data for the current product. (Equivalent to calling
  // GetUpdateDetailForApp with the current install mode's app guid.)
  static bool GetUpdateDetail(ProductData* data);
};

#endif  // CHROME_INSTALLER_UTIL_GOOGLE_UPDATE_SETTINGS_H_