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