blob: 89c2c186f9c095d6c2a9210ca2c4e8a94ee64b98 [file] [log] [blame]
sorin97bd0292016-11-14 19:46:531// Copyright 2016 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 "components/update_client/updater_state.h"
6
Sorin Jianudf2b9cc2018-02-05 19:11:037#include <windows.h>
8
sorin97bd0292016-11-14 19:46:539#include <string>
10#include <utility>
11
Hector Carmonac3565bc42019-02-01 23:31:2112#include "base/enterprise_util.h"
sorin97bd0292016-11-14 19:46:5313#include "base/strings/string16.h"
14#include "base/strings/string_number_conversions.h"
15#include "base/strings/utf_string_conversions.h"
sorin97bd0292016-11-14 19:46:5316#include "base/win/registry.h"
17#include "base/win/win_util.h"
18
19// TODO(sorin): implement this in terms of
20// chrome/installer/util/google_update_settings (crbug.com/615187).
21
22namespace update_client {
23
24namespace {
25
26// Google Update group policy settings.
27const wchar_t kGoogleUpdatePoliciesKey[] =
28 L"SOFTWARE\\Policies\\Google\\Update";
29const wchar_t kCheckPeriodOverrideMinutes[] = L"AutoUpdateCheckPeriodMinutes";
30const wchar_t kUpdatePolicyValue[] = L"UpdateDefault";
31const wchar_t kChromeUpdatePolicyOverride[] =
32 L"Update{8A69D345-D564-463C-AFF1-A69D9E530F96}";
33
34// Don't allow update periods longer than six weeks (Chrome release cadence).
35const int kCheckPeriodOverrideMinutesMax = 60 * 24 * 7 * 6;
36
37// Google Update registry settings.
38const wchar_t kRegPathGoogleUpdate[] = L"Software\\Google\\Update";
39const wchar_t kRegPathClientsGoogleUpdate[] =
40 L"Software\\Google\\Update\\Clients\\"
41 L"{430FD4D0-B729-4F61-AA34-91526481799D}";
42const wchar_t kRegValueGoogleUpdatePv[] = L"pv";
43const wchar_t kRegValueLastStartedAU[] = L"LastStartedAU";
44const wchar_t kRegValueLastChecked[] = L"LastChecked";
45
sorina0a5a622017-05-05 17:08:0246base::Time GetUpdaterTimeValue(bool is_machine, const wchar_t* value_name) {
47 const HKEY root_key = is_machine ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
48 base::win::RegKey update_key;
49
50 if (update_key.Open(root_key, kRegPathGoogleUpdate,
51 KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) {
52 DWORD value(0);
53 if (update_key.ReadValueDW(value_name, &value) == ERROR_SUCCESS) {
54 return base::Time::FromTimeT(value);
55 }
56 }
57
58 return base::Time();
59}
60
sorin97bd0292016-11-14 19:46:5361} // namespace
62
63std::string UpdaterState::GetUpdaterName() {
64 return std::string("Omaha");
65}
66
67base::Version UpdaterState::GetUpdaterVersion(bool is_machine) {
68 const HKEY root_key = is_machine ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
69 base::string16 version;
70 base::win::RegKey key;
71
72 if (key.Open(root_key, kRegPathClientsGoogleUpdate,
73 KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS &&
74 key.ReadValue(kRegValueGoogleUpdatePv, &version) == ERROR_SUCCESS) {
75 return base::Version(base::UTF16ToUTF8(version));
76 }
77
78 return base::Version();
79}
80
81base::Time UpdaterState::GetUpdaterLastStartedAU(bool is_machine) {
82 return GetUpdaterTimeValue(is_machine, kRegValueLastStartedAU);
83}
84
85base::Time UpdaterState::GetUpdaterLastChecked(bool is_machine) {
86 return GetUpdaterTimeValue(is_machine, kRegValueLastChecked);
87}
88
sorin97bd0292016-11-14 19:46:5389bool UpdaterState::IsAutoupdateCheckEnabled() {
90 // Check the auto-update check period override. If it is 0 or exceeds the
91 // maximum timeout, then for all intents and purposes auto updates are
92 // disabled.
93 base::win::RegKey policy_key;
94 DWORD value = 0;
95 if (policy_key.Open(HKEY_LOCAL_MACHINE, kGoogleUpdatePoliciesKey,
96 KEY_QUERY_VALUE) == ERROR_SUCCESS &&
97 policy_key.ReadValueDW(kCheckPeriodOverrideMinutes, &value) ==
98 ERROR_SUCCESS &&
99 (value == 0 || value > kCheckPeriodOverrideMinutesMax)) {
100 return false;
101 }
102
103 return true;
104}
105
106// Returns -1 if the policy is not found or the value was invalid. Otherwise,
107// returns a value in the [0, 3] range, representing the value of the
108// Chrome update group policy.
109int UpdaterState::GetUpdatePolicy() {
110 const int kMaxUpdatePolicyValue = 3;
111
112 base::win::RegKey policy_key;
113
114 if (policy_key.Open(HKEY_LOCAL_MACHINE, kGoogleUpdatePoliciesKey,
115 KEY_QUERY_VALUE) != ERROR_SUCCESS) {
116 return -1;
117 }
118
119 DWORD value = 0;
120 // First try to read the Chrome-specific override.
121 if (policy_key.ReadValueDW(kChromeUpdatePolicyOverride, &value) ==
122 ERROR_SUCCESS &&
123 value <= kMaxUpdatePolicyValue) {
124 return value;
125 }
126
127 // Try to read default override.
128 if (policy_key.ReadValueDW(kUpdatePolicyValue, &value) == ERROR_SUCCESS &&
129 value <= kMaxUpdatePolicyValue) {
130 return value;
131 }
132
133 return -1;
134}
135
sorin97bd0292016-11-14 19:46:53136} // namespace update_client