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