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