google_update_settings.cc 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#include "chrome/installer/util/google_update_settings.h"
6
7#include <algorithm>
8#include <string>
9
10#include "base/command_line.h"
11#include "base/files/file_path.h"
12#include "base/logging.h"
13#include "base/metrics/histogram.h"
14#include "base/path_service.h"
15#include "base/strings/string_number_conversions.h"
16#include "base/strings/string_util.h"
17#include "base/strings/utf_string_conversions.h"
18#include "base/threading/thread_restrictions.h"
19#include "base/time/time.h"
20#include "base/win/registry.h"
21#include "base/win/win_util.h"
22#include "chrome/common/chrome_switches.h"
23#include "chrome/installer/util/browser_distribution.h"
24#include "chrome/installer/util/channel_info.h"
25#include "chrome/installer/util/google_update_constants.h"
26#include "chrome/installer/util/google_update_experiment_util.h"
27#include "chrome/installer/util/install_util.h"
28#include "chrome/installer/util/installation_state.h"
29#include "chrome/installer/util/product.h"
30
31using base::win::RegKey;
32using installer::InstallationState;
33
34namespace {
35
36const wchar_t kGoogleUpdatePoliciesKey[] =
37    L"SOFTWARE\\Policies\\Google\\Update";
38const wchar_t kGoogleUpdateUpdatePolicyValue[] = L"UpdateDefault";
39const wchar_t kGoogleUpdateUpdateOverrideValuePrefix[] = L"Update";
40const GoogleUpdateSettings::UpdatePolicy kGoogleUpdateDefaultUpdatePolicy =
41#if defined(GOOGLE_CHROME_BUILD)
42    GoogleUpdateSettings::AUTOMATIC_UPDATES;
43#else
44    GoogleUpdateSettings::UPDATES_DISABLED;
45#endif
46
47bool ReadGoogleUpdateStrKey(const wchar_t* const name, std::wstring* value) {
48  BrowserDistribution* dist = BrowserDistribution::GetDistribution();
49  std::wstring reg_path = dist->GetStateKey();
50  RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ);
51  if (key.ReadValue(name, value) != ERROR_SUCCESS) {
52    RegKey hklm_key(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_READ);
53    return (hklm_key.ReadValue(name, value) == ERROR_SUCCESS);
54  }
55  return true;
56}
57
58// Update a state registry key |name| to be |value| for the given browser
59// |dist|.  If this is a |system_install|, then update the value under
60// HKLM (istead of HKCU for user-installs) using a group of keys (one
61// for each OS user) and also include the method to |aggregate| these
62// values when reporting.
63bool WriteGoogleUpdateStrKeyInternal(BrowserDistribution* dist,
64                                     bool system_install,
65                                     const wchar_t* const name,
66                                     // presubmit: allow wstring
67                                     const std::wstring& value,
68                                     const wchar_t* const aggregate) {
69  DCHECK(dist);
70
71  if (system_install) {
72    DCHECK(aggregate);
73    // Machine installs require each OS user to write a unique key under a
74    // named key in HKLM as well as an "aggregation" function that describes
75    // how the values of multiple users are to be combined.
76    std::wstring uniquename;  // presubmit: allow wstring
77    if (!base::win::GetUserSidString(&uniquename)) {
78      NOTREACHED();
79      return false;
80    }
81
82    // presubmit: allow wstring
83    std::wstring reg_path(dist->GetStateMediumKey());
84    reg_path.append(L"\\");
85    reg_path.append(name);
86    RegKey key(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_SET_VALUE);
87    key.WriteValue(google_update::kRegAggregateMethod, aggregate);
88    return (key.WriteValue(uniquename.c_str(), value.c_str()) == ERROR_SUCCESS);
89  } else {
90    // User installs are easy: just write the values to HKCU tree.
91    RegKey key(HKEY_CURRENT_USER, dist->GetStateKey().c_str(), KEY_SET_VALUE);
92    return (key.WriteValue(name, value.c_str()) == ERROR_SUCCESS);
93  }
94}
95
96bool WriteGoogleUpdateStrKey(const wchar_t* const name,
97                             const std::wstring& value) {
98  BrowserDistribution* dist = BrowserDistribution::GetDistribution();
99  return WriteGoogleUpdateStrKeyInternal(dist, false, name, value, NULL);
100}
101
102bool WriteGoogleUpdateStrKeyMultiInstall(BrowserDistribution* dist,
103                                         const wchar_t* const name,
104                                         const std::wstring& value,
105                                         bool system_level) {
106  bool result = WriteGoogleUpdateStrKeyInternal(dist, false, name, value, NULL);
107  if (!InstallUtil::IsMultiInstall(dist, system_level))
108    return result;
109  // It is a multi-install distro. Must write the reg value again.
110  BrowserDistribution* multi_dist =
111      BrowserDistribution::GetSpecificDistribution(
112          BrowserDistribution::CHROME_BINARIES);
113  return
114      WriteGoogleUpdateStrKeyInternal(multi_dist, false, name, value, NULL) &&
115      result;
116}
117
118bool ClearGoogleUpdateStrKey(const wchar_t* const name) {
119  BrowserDistribution* dist = BrowserDistribution::GetDistribution();
120  std::wstring reg_path = dist->GetStateKey();
121  RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ | KEY_WRITE);
122  std::wstring value;
123  if (key.ReadValue(name, &value) != ERROR_SUCCESS)
124    return false;
125  return (key.WriteValue(name, L"") == ERROR_SUCCESS);
126}
127
128bool RemoveGoogleUpdateStrKey(const wchar_t* const name) {
129  BrowserDistribution* dist = BrowserDistribution::GetDistribution();
130  std::wstring reg_path = dist->GetStateKey();
131  RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ | KEY_WRITE);
132  if (!key.HasValue(name))
133    return true;
134  return (key.DeleteValue(name) == ERROR_SUCCESS);
135}
136
137bool GetChromeChannelInternal(bool system_install,
138                              bool add_multi_modifier,
139                              base::string16* channel) {
140  BrowserDistribution* dist = BrowserDistribution::GetDistribution();
141  if (dist->GetChromeChannel(channel)) {
142    return true;
143  }
144
145  HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
146  base::string16 reg_path = dist->GetStateKey();
147  RegKey key(root_key, reg_path.c_str(), KEY_READ);
148
149  installer::ChannelInfo channel_info;
150  if (!channel_info.Initialize(key)) {
151    channel->assign(installer::kChromeChannelUnknown);
152    return false;
153  }
154
155  if (!channel_info.GetChannelName(channel)) {
156    channel->assign(installer::kChromeChannelUnknown);
157  }
158
159  // Tag the channel name if this is a multi-install.
160  if (add_multi_modifier && channel_info.IsMultiInstall()) {
161    if (!channel->empty()) {
162      channel->append(1, L'-');
163    }
164    channel->append(1, L'm');
165  }
166
167  return true;
168}
169
170// Populates |update_policy| with the UpdatePolicy enum value corresponding to a
171// DWORD read from the registry and returns true if |value| is within range.
172// If |value| is out of range, returns false without modifying |update_policy|.
173bool GetUpdatePolicyFromDword(
174    const DWORD value,
175    GoogleUpdateSettings::UpdatePolicy* update_policy) {
176  switch (value) {
177    case GoogleUpdateSettings::UPDATES_DISABLED:
178    case GoogleUpdateSettings::AUTOMATIC_UPDATES:
179    case GoogleUpdateSettings::MANUAL_UPDATES_ONLY:
180    case GoogleUpdateSettings::AUTO_UPDATES_ONLY:
181      *update_policy = static_cast<GoogleUpdateSettings::UpdatePolicy>(value);
182      return true;
183    default:
184      LOG(WARNING) << "Unexpected update policy override value: " << value;
185  }
186  return false;
187}
188
189}  // namespace
190
191bool GoogleUpdateSettings::IsSystemInstall() {
192  bool system_install = false;
193  base::FilePath module_dir;
194  if (!PathService::Get(base::DIR_MODULE, &module_dir)) {
195    LOG(WARNING)
196        << "Failed to get directory of module; assuming per-user install.";
197  } else {
198    system_install = !InstallUtil::IsPerUserInstall(module_dir.value().c_str());
199  }
200  return system_install;
201}
202
203bool GoogleUpdateSettings::GetCollectStatsConsent() {
204  return GetCollectStatsConsentAtLevel(IsSystemInstall());
205}
206
207// Older versions of Chrome unconditionally read from HKCU\...\ClientState\...
208// and then HKLM\...\ClientState\....  This means that system-level Chrome
209// never checked ClientStateMedium (which has priority according to Google
210// Update) and gave preference to a value in HKCU (which was never checked by
211// Google Update).  From now on, Chrome follows Google Update's policy.
212bool GoogleUpdateSettings::GetCollectStatsConsentAtLevel(bool system_install) {
213  BrowserDistribution* dist = BrowserDistribution::GetDistribution();
214
215  // Consent applies to all products in a multi-install package.
216  if (InstallUtil::IsMultiInstall(dist, system_install)) {
217    dist = BrowserDistribution::GetSpecificDistribution(
218        BrowserDistribution::CHROME_BINARIES);
219  }
220
221  RegKey key;
222  DWORD value = 0;
223  bool have_value = false;
224
225  // For system-level installs, try ClientStateMedium first.
226  have_value =
227      system_install &&
228      key.Open(HKEY_LOCAL_MACHINE, dist->GetStateMediumKey().c_str(),
229               KEY_QUERY_VALUE) == ERROR_SUCCESS &&
230      key.ReadValueDW(google_update::kRegUsageStatsField,
231                      &value) == ERROR_SUCCESS;
232
233  // Otherwise, try ClientState.
234  if (!have_value) {
235    have_value =
236        key.Open(system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
237                 dist->GetStateKey().c_str(),
238                 KEY_QUERY_VALUE) == ERROR_SUCCESS &&
239        key.ReadValueDW(google_update::kRegUsageStatsField,
240                        &value) == ERROR_SUCCESS;
241  }
242
243  // Google Update specifically checks that the value is 1, so we do the same.
244  return have_value && value == 1;
245}
246
247bool GoogleUpdateSettings::SetCollectStatsConsent(bool consented) {
248  return SetCollectStatsConsentAtLevel(IsSystemInstall(), consented);
249}
250
251bool GoogleUpdateSettings::SetCollectStatsConsentAtLevel(bool system_install,
252                                                         bool consented) {
253  // Google Update writes and expects 1 for true, 0 for false.
254  DWORD value = consented ? 1 : 0;
255
256  BrowserDistribution* dist = BrowserDistribution::GetDistribution();
257
258  // Consent applies to all products in a multi-install package.
259  if (InstallUtil::IsMultiInstall(dist, system_install)) {
260    dist = BrowserDistribution::GetSpecificDistribution(
261        BrowserDistribution::CHROME_BINARIES);
262  }
263
264  // Write to ClientStateMedium for system-level; ClientState otherwise.
265  HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
266  std::wstring reg_path =
267      system_install ? dist->GetStateMediumKey() : dist->GetStateKey();
268  RegKey key;
269  LONG result = key.Create(root_key, reg_path.c_str(), KEY_SET_VALUE);
270  if (result != ERROR_SUCCESS) {
271    LOG(ERROR) << "Failed opening key " << reg_path << " to set "
272               << google_update::kRegUsageStatsField << "; result: " << result;
273  } else {
274    result = key.WriteValue(google_update::kRegUsageStatsField, value);
275    LOG_IF(ERROR, result != ERROR_SUCCESS) << "Failed setting "
276        << google_update::kRegUsageStatsField << " in key " << reg_path
277        << "; result: " << result;
278  }
279  return (result == ERROR_SUCCESS);
280}
281
282bool GoogleUpdateSettings::GetMetricsId(std::string* metrics_id) {
283  std::wstring metrics_id_w;
284  bool rv = ReadGoogleUpdateStrKey(google_update::kRegMetricsId, &metrics_id_w);
285  *metrics_id = base::WideToUTF8(metrics_id_w);
286  return rv;
287}
288
289bool GoogleUpdateSettings::SetMetricsId(const std::string& metrics_id) {
290  std::wstring metrics_id_w = base::UTF8ToWide(metrics_id);
291  return WriteGoogleUpdateStrKey(google_update::kRegMetricsId, metrics_id_w);
292}
293
294// EULA consent is only relevant for system-level installs.
295bool GoogleUpdateSettings::SetEULAConsent(
296    const InstallationState& machine_state,
297    BrowserDistribution* dist,
298    bool consented) {
299  DCHECK(dist);
300  const DWORD eula_accepted = consented ? 1 : 0;
301  std::wstring reg_path = dist->GetStateMediumKey();
302  bool succeeded = true;
303  RegKey key;
304
305  // Write the consent value into the product's ClientStateMedium key.
306  if (key.Create(HKEY_LOCAL_MACHINE, reg_path.c_str(),
307                 KEY_SET_VALUE) != ERROR_SUCCESS ||
308      key.WriteValue(google_update::kRegEULAAceptedField,
309                     eula_accepted) != ERROR_SUCCESS) {
310    succeeded = false;
311  }
312
313  // If this is a multi-install, also write it into the binaries' key.
314  // --mutli-install is not provided on the command-line, so deduce it from
315  // the product's state.
316  const installer::ProductState* product_state =
317      machine_state.GetProductState(true, dist->GetType());
318  if (product_state != NULL && product_state->is_multi_install()) {
319    dist = BrowserDistribution::GetSpecificDistribution(
320        BrowserDistribution::CHROME_BINARIES);
321    reg_path = dist->GetStateMediumKey();
322    if (key.Create(HKEY_LOCAL_MACHINE, reg_path.c_str(),
323                   KEY_SET_VALUE) != ERROR_SUCCESS ||
324        key.WriteValue(google_update::kRegEULAAceptedField,
325                       eula_accepted) != ERROR_SUCCESS) {
326        succeeded = false;
327    }
328  }
329
330  return succeeded;
331}
332
333int GoogleUpdateSettings::GetLastRunTime() {
334  std::wstring time_s;
335  if (!ReadGoogleUpdateStrKey(google_update::kRegLastRunTimeField, &time_s))
336    return -1;
337  int64 time_i;
338  if (!base::StringToInt64(time_s, &time_i))
339    return -1;
340  base::TimeDelta td =
341      base::Time::NowFromSystemTime() - base::Time::FromInternalValue(time_i);
342  return td.InDays();
343}
344
345bool GoogleUpdateSettings::SetLastRunTime() {
346  int64 time = base::Time::NowFromSystemTime().ToInternalValue();
347  return WriteGoogleUpdateStrKey(google_update::kRegLastRunTimeField,
348                                 base::Int64ToString16(time));
349}
350
351bool GoogleUpdateSettings::RemoveLastRunTime() {
352  return RemoveGoogleUpdateStrKey(google_update::kRegLastRunTimeField);
353}
354
355bool GoogleUpdateSettings::GetBrowser(std::wstring* browser) {
356  return ReadGoogleUpdateStrKey(google_update::kRegBrowserField, browser);
357}
358
359bool GoogleUpdateSettings::GetLanguage(std::wstring* language) {
360  return ReadGoogleUpdateStrKey(google_update::kRegLangField, language);
361}
362
363bool GoogleUpdateSettings::GetBrand(std::wstring* brand) {
364  return ReadGoogleUpdateStrKey(google_update::kRegRLZBrandField, brand);
365}
366
367bool GoogleUpdateSettings::GetReactivationBrand(std::wstring* brand) {
368  return ReadGoogleUpdateStrKey(google_update::kRegRLZReactivationBrandField,
369                                brand);
370}
371
372bool GoogleUpdateSettings::GetClient(std::wstring* client) {
373  return ReadGoogleUpdateStrKey(google_update::kRegClientField, client);
374}
375
376bool GoogleUpdateSettings::SetClient(const std::wstring& client) {
377  return WriteGoogleUpdateStrKey(google_update::kRegClientField, client);
378}
379
380bool GoogleUpdateSettings::GetReferral(std::wstring* referral) {
381  return ReadGoogleUpdateStrKey(google_update::kRegReferralField, referral);
382}
383
384bool GoogleUpdateSettings::ClearReferral() {
385  return ClearGoogleUpdateStrKey(google_update::kRegReferralField);
386}
387
388bool GoogleUpdateSettings::UpdateDidRunState(bool did_run,
389                                             bool system_level) {
390  BrowserDistribution* dist = BrowserDistribution::GetDistribution();
391  return UpdateDidRunStateForDistribution(dist, did_run, system_level);
392}
393
394bool GoogleUpdateSettings::UpdateDidRunStateForDistribution(
395    BrowserDistribution* dist,
396    bool did_run,
397    bool system_level) {
398  return WriteGoogleUpdateStrKeyMultiInstall(dist,
399                                             google_update::kRegDidRunField,
400                                             did_run ? L"1" : L"0",
401                                             system_level);
402}
403
404std::wstring GoogleUpdateSettings::GetChromeChannel(bool system_install) {
405  std::wstring channel;
406  GetChromeChannelInternal(system_install, false, &channel);
407  return channel;
408}
409
410bool GoogleUpdateSettings::GetChromeChannelAndModifiers(
411    bool system_install,
412    base::string16* channel) {
413  return GetChromeChannelInternal(system_install, true, channel);
414}
415
416void GoogleUpdateSettings::UpdateInstallStatus(bool system_install,
417    installer::ArchiveType archive_type, int install_return_code,
418    const std::wstring& product_guid) {
419  DCHECK(archive_type != installer::UNKNOWN_ARCHIVE_TYPE ||
420         install_return_code != 0);
421  HKEY reg_root = (system_install) ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
422
423  RegKey key;
424  installer::ChannelInfo channel_info;
425  std::wstring reg_key(google_update::kRegPathClientState);
426  reg_key.append(L"\\");
427  reg_key.append(product_guid);
428  LONG result = key.Open(reg_root, reg_key.c_str(),
429                         KEY_QUERY_VALUE | KEY_SET_VALUE);
430  if (result == ERROR_SUCCESS)
431    channel_info.Initialize(key);
432  else if (result != ERROR_FILE_NOT_FOUND)
433    LOG(ERROR) << "Failed to open " << reg_key << "; Error: " << result;
434
435  if (UpdateGoogleUpdateApKey(archive_type, install_return_code,
436                              &channel_info)) {
437    // We have a modified channel_info value to write.
438    // Create the app's ClientState key if it doesn't already exist.
439    if (!key.Valid()) {
440      result = key.Open(reg_root, google_update::kRegPathClientState,
441                        KEY_CREATE_SUB_KEY);
442      if (result == ERROR_SUCCESS)
443        result = key.CreateKey(product_guid.c_str(), KEY_SET_VALUE);
444
445      if (result != ERROR_SUCCESS) {
446        LOG(ERROR) << "Failed to create " << reg_key << "; Error: " << result;
447        return;
448      }
449    }
450    if (!channel_info.Write(&key)) {
451      LOG(ERROR) << "Failed to write to application's ClientState key "
452                 << google_update::kRegApField << " = " << channel_info.value();
453    }
454  }
455}
456
457bool GoogleUpdateSettings::UpdateGoogleUpdateApKey(
458    installer::ArchiveType archive_type, int install_return_code,
459    installer::ChannelInfo* value) {
460  DCHECK(archive_type != installer::UNKNOWN_ARCHIVE_TYPE ||
461         install_return_code != 0);
462  bool modified = false;
463
464  if (archive_type == installer::FULL_ARCHIVE_TYPE || !install_return_code) {
465    if (value->SetFullSuffix(false)) {
466      VLOG(1) << "Removed incremental installer failure key; "
467                 "switching to channel: "
468              << value->value();
469      modified = true;
470    }
471  } else if (archive_type == installer::INCREMENTAL_ARCHIVE_TYPE) {
472    if (value->SetFullSuffix(true)) {
473      VLOG(1) << "Incremental installer failed; switching to channel: "
474              << value->value();
475      modified = true;
476    } else {
477      VLOG(1) << "Incremental installer failure; already on channel: "
478              << value->value();
479    }
480  } else {
481    // It's okay if we don't know the archive type.  In this case, leave the
482    // "-full" suffix as we found it.
483    DCHECK_EQ(installer::UNKNOWN_ARCHIVE_TYPE, archive_type);
484  }
485
486  if (value->SetMultiFailSuffix(false)) {
487    VLOG(1) << "Removed multi-install failure key; switching to channel: "
488            << value->value();
489    modified = true;
490  }
491
492  return modified;
493}
494
495void GoogleUpdateSettings::UpdateProfileCounts(int profiles_active,
496                                               int profiles_signedin) {
497  BrowserDistribution* dist = BrowserDistribution::GetDistribution();
498  bool system_install = IsSystemInstall();
499  WriteGoogleUpdateStrKeyInternal(dist, system_install,
500                                  google_update::kRegProfilesActive,
501                                  base::Int64ToString16(profiles_active),
502                                  L"sum()");
503  WriteGoogleUpdateStrKeyInternal(dist, system_install,
504                                  google_update::kRegProfilesSignedIn,
505                                  base::Int64ToString16(profiles_signedin),
506                                  L"sum()");
507}
508
509int GoogleUpdateSettings::DuplicateGoogleUpdateSystemClientKey() {
510  BrowserDistribution* dist = BrowserDistribution::GetDistribution();
511  std::wstring reg_path = dist->GetStateKey();
512
513  // Minimum access needed is to be able to write to this key.
514  RegKey reg_key(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_SET_VALUE);
515  if (!reg_key.Valid())
516    return 0;
517
518  HANDLE target_handle = 0;
519  if (!DuplicateHandle(GetCurrentProcess(), reg_key.Handle(),
520                       GetCurrentProcess(), &target_handle, KEY_SET_VALUE,
521                       TRUE, DUPLICATE_SAME_ACCESS)) {
522    return 0;
523  }
524  return reinterpret_cast<int>(target_handle);
525}
526
527bool GoogleUpdateSettings::WriteGoogleUpdateSystemClientKey(
528    int handle, const std::wstring& key, const std::wstring& value) {
529  HKEY reg_key = reinterpret_cast<HKEY>(reinterpret_cast<void*>(handle));
530  DWORD size = static_cast<DWORD>(value.size()) * sizeof(wchar_t);
531  LSTATUS status = RegSetValueEx(reg_key, key.c_str(), 0, REG_SZ,
532      reinterpret_cast<const BYTE*>(value.c_str()), size);
533  return status == ERROR_SUCCESS;
534}
535
536GoogleUpdateSettings::UpdatePolicy GoogleUpdateSettings::GetAppUpdatePolicy(
537    const std::wstring& app_guid,
538    bool* is_overridden) {
539  bool found_override = false;
540  UpdatePolicy update_policy = kGoogleUpdateDefaultUpdatePolicy;
541
542#if defined(GOOGLE_CHROME_BUILD)
543  DCHECK(!app_guid.empty());
544  RegKey policy_key;
545
546  // Google Update Group Policy settings are always in HKLM.
547  if (policy_key.Open(HKEY_LOCAL_MACHINE, kGoogleUpdatePoliciesKey,
548                      KEY_QUERY_VALUE) == ERROR_SUCCESS) {
549    static const size_t kPrefixLen =
550        arraysize(kGoogleUpdateUpdateOverrideValuePrefix) - 1;
551    DWORD value;
552    std::wstring app_update_override;
553    app_update_override.reserve(kPrefixLen + app_guid.size());
554    app_update_override.append(kGoogleUpdateUpdateOverrideValuePrefix,
555                               kPrefixLen);
556    app_update_override.append(app_guid);
557    // First try to read and comprehend the app-specific override.
558    found_override = (policy_key.ReadValueDW(app_update_override.c_str(),
559                                             &value) == ERROR_SUCCESS &&
560                      GetUpdatePolicyFromDword(value, &update_policy));
561
562    // Failing that, try to read and comprehend the default override.
563    if (!found_override &&
564        policy_key.ReadValueDW(kGoogleUpdateUpdatePolicyValue,
565                               &value) == ERROR_SUCCESS) {
566      GetUpdatePolicyFromDword(value, &update_policy);
567    }
568  }
569#endif  // defined(GOOGLE_CHROME_BUILD)
570
571  if (is_overridden != NULL)
572    *is_overridden = found_override;
573
574  return update_policy;
575}
576
577void GoogleUpdateSettings::RecordChromeUpdatePolicyHistograms() {
578  const bool is_multi_install = InstallUtil::IsMultiInstall(
579      BrowserDistribution::GetDistribution(), IsSystemInstall());
580  const base::string16 app_guid =
581      BrowserDistribution::GetSpecificDistribution(
582          is_multi_install ? BrowserDistribution::CHROME_BINARIES :
583                             BrowserDistribution::CHROME_BROWSER)->GetAppGuid();
584
585  bool is_overridden = false;
586  const UpdatePolicy update_policy = GetAppUpdatePolicy(app_guid,
587                                                        &is_overridden);
588  UMA_HISTOGRAM_BOOLEAN("GoogleUpdate.UpdatePolicyIsOverridden", is_overridden);
589  UMA_HISTOGRAM_ENUMERATION("GoogleUpdate.EffectivePolicy", update_policy,
590                            UPDATE_POLICIES_COUNT);
591}
592
593base::string16 GoogleUpdateSettings::GetUninstallCommandLine(
594    bool system_install) {
595  const HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
596  base::string16 cmd_line;
597  RegKey update_key;
598
599  if (update_key.Open(root_key, google_update::kRegPathGoogleUpdate,
600                      KEY_QUERY_VALUE) == ERROR_SUCCESS) {
601    update_key.ReadValue(google_update::kRegUninstallCmdLine, &cmd_line);
602  }
603
604  return cmd_line;
605}
606
607Version GoogleUpdateSettings::GetGoogleUpdateVersion(bool system_install) {
608  const HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
609  base::string16 version;
610  RegKey key;
611
612  if (key.Open(root_key,
613               google_update::kRegPathGoogleUpdate,
614               KEY_QUERY_VALUE) == ERROR_SUCCESS &&
615      key.ReadValue(google_update::kRegGoogleUpdateVersion,
616                    &version) == ERROR_SUCCESS) {
617    return Version(base::UTF16ToUTF8(version));
618  }
619
620  return Version();
621}
622
623base::Time GoogleUpdateSettings::GetGoogleUpdateLastStartedAU(
624    bool system_install) {
625  const HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
626  RegKey update_key;
627
628  if (update_key.Open(root_key, google_update::kRegPathGoogleUpdate,
629                      KEY_QUERY_VALUE) == ERROR_SUCCESS) {
630    DWORD last_start;
631    if (update_key.ReadValueDW(google_update::kRegLastStartedAUField,
632                               &last_start) == ERROR_SUCCESS) {
633      return base::Time::FromTimeT(last_start);
634    }
635  }
636
637  return base::Time();
638}
639
640base::Time GoogleUpdateSettings::GetGoogleUpdateLastChecked(
641    bool system_install) {
642  const HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
643  RegKey update_key;
644
645  if (update_key.Open(root_key, google_update::kRegPathGoogleUpdate,
646                      KEY_QUERY_VALUE) == ERROR_SUCCESS) {
647    DWORD last_check;
648    if (update_key.ReadValueDW(google_update::kRegLastCheckedField,
649                               &last_check) == ERROR_SUCCESS) {
650      return base::Time::FromTimeT(last_check);
651    }
652  }
653
654  return base::Time();
655}
656
657bool GoogleUpdateSettings::GetUpdateDetailForApp(bool system_install,
658                                                 const wchar_t* app_guid,
659                                                 ProductData* data) {
660  DCHECK(app_guid);
661  DCHECK(data);
662
663  bool product_found = false;
664
665  const HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
666  base::string16 clientstate_reg_path(google_update::kRegPathClientState);
667  clientstate_reg_path.append(L"\\");
668  clientstate_reg_path.append(app_guid);
669
670  RegKey clientstate;
671  if (clientstate.Open(root_key, clientstate_reg_path.c_str(),
672                       KEY_QUERY_VALUE) == ERROR_SUCCESS) {
673    base::string16 version;
674    DWORD dword_value;
675    if ((clientstate.ReadValueDW(google_update::kRegLastCheckSuccessField,
676                                 &dword_value) == ERROR_SUCCESS) &&
677        (clientstate.ReadValue(google_update::kRegVersionField,
678                               &version) == ERROR_SUCCESS)) {
679      product_found = true;
680      data->version = base::UTF16ToASCII(version);
681      data->last_success = base::Time::FromTimeT(dword_value);
682      data->last_result = 0;
683      data->last_error_code = 0;
684      data->last_extra_code = 0;
685
686      if (clientstate.ReadValueDW(google_update::kRegLastInstallerResultField,
687                                  &dword_value) == ERROR_SUCCESS) {
688        // Google Update convention is that if an installer writes an result
689        // code that is invalid, it is clamped to an exit code result.
690        const DWORD kMaxValidInstallResult = 4;  // INSTALLER_RESULT_EXIT_CODE
691        data->last_result = std::min(dword_value, kMaxValidInstallResult);
692      }
693      if (clientstate.ReadValueDW(google_update::kRegLastInstallerErrorField,
694                                  &dword_value) == ERROR_SUCCESS) {
695        data->last_error_code = dword_value;
696      }
697      if (clientstate.ReadValueDW(google_update::kRegLastInstallerExtraField,
698                                  &dword_value) == ERROR_SUCCESS) {
699        data->last_extra_code = dword_value;
700      }
701    }
702  }
703
704  return product_found;
705}
706
707bool GoogleUpdateSettings::GetUpdateDetailForGoogleUpdate(bool system_install,
708                                                          ProductData* data) {
709  return GetUpdateDetailForApp(system_install,
710                               google_update::kGoogleUpdateUpgradeCode,
711                               data);
712}
713
714bool GoogleUpdateSettings::GetUpdateDetail(bool system_install,
715                                           ProductData* data) {
716  BrowserDistribution* dist = BrowserDistribution::GetDistribution();
717  return GetUpdateDetailForApp(system_install,
718                               dist->GetAppGuid().c_str(),
719                               data);
720}
721
722bool GoogleUpdateSettings::SetExperimentLabels(
723    bool system_install,
724    const base::string16& experiment_labels) {
725  HKEY reg_root = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
726
727  // Use the browser distribution and install level to write to the correct
728  // client state/app guid key.
729  bool success = false;
730  BrowserDistribution* dist = BrowserDistribution::GetDistribution();
731  if (dist->ShouldSetExperimentLabels()) {
732    base::string16 client_state_path(
733        system_install ? dist->GetStateMediumKey() : dist->GetStateKey());
734    RegKey client_state(
735        reg_root, client_state_path.c_str(), KEY_SET_VALUE);
736    if (experiment_labels.empty()) {
737      success = client_state.DeleteValue(google_update::kExperimentLabels)
738          == ERROR_SUCCESS;
739    } else {
740      success = client_state.WriteValue(google_update::kExperimentLabels,
741          experiment_labels.c_str()) == ERROR_SUCCESS;
742    }
743  }
744
745  return success;
746}
747
748bool GoogleUpdateSettings::ReadExperimentLabels(
749    bool system_install,
750    base::string16* experiment_labels) {
751  HKEY reg_root = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
752
753  // If this distribution does not set the experiment labels, don't bother
754  // reading.
755  BrowserDistribution* dist = BrowserDistribution::GetDistribution();
756  if (!dist->ShouldSetExperimentLabels())
757    return false;
758
759  base::string16 client_state_path(
760      system_install ? dist->GetStateMediumKey() : dist->GetStateKey());
761
762  RegKey client_state;
763  LONG result =
764      client_state.Open(reg_root, client_state_path.c_str(), KEY_QUERY_VALUE);
765  if (result == ERROR_SUCCESS) {
766    result = client_state.ReadValue(google_update::kExperimentLabels,
767                                    experiment_labels);
768  }
769
770  // If the key or value was not present, return the empty string.
771  if (result == ERROR_FILE_NOT_FOUND || result == ERROR_PATH_NOT_FOUND) {
772    experiment_labels->clear();
773    return true;
774  }
775
776  return result == ERROR_SUCCESS;
777}
778