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