blob: 396395a6d6ac3cf59c02110b0a42bf5e6ac676d8 [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
7#include <string>
8#include <utility>
9
10#include "base/memory/ptr_util.h"
11#include "base/strings/string16.h"
12#include "base/strings/string_number_conversions.h"
13#include "base/strings/utf_string_conversions.h"
14
15#include "base/win/registry.h"
16#include "base/win/win_util.h"
17
18// TODO(sorin): implement this in terms of
19// chrome/installer/util/google_update_settings (crbug.com/615187).
20
21namespace update_client {
22
23namespace {
24
25// Google Update group policy settings.
26const wchar_t kGoogleUpdatePoliciesKey[] =
27 L"SOFTWARE\\Policies\\Google\\Update";
28const wchar_t kCheckPeriodOverrideMinutes[] = L"AutoUpdateCheckPeriodMinutes";
29const wchar_t kUpdatePolicyValue[] = L"UpdateDefault";
30const wchar_t kChromeUpdatePolicyOverride[] =
31 L"Update{8A69D345-D564-463C-AFF1-A69D9E530F96}";
32
33// Don't allow update periods longer than six weeks (Chrome release cadence).
34const int kCheckPeriodOverrideMinutesMax = 60 * 24 * 7 * 6;
35
36// Google Update registry settings.
37const wchar_t kRegPathGoogleUpdate[] = L"Software\\Google\\Update";
38const wchar_t kRegPathClientsGoogleUpdate[] =
39 L"Software\\Google\\Update\\Clients\\"
40 L"{430FD4D0-B729-4F61-AA34-91526481799D}";
41const wchar_t kRegValueGoogleUpdatePv[] = L"pv";
42const wchar_t kRegValueLastStartedAU[] = L"LastStartedAU";
43const wchar_t kRegValueLastChecked[] = L"LastChecked";
44
45} // namespace
46
47std::string UpdaterState::GetUpdaterName() {
48 return std::string("Omaha");
49}
50
51base::Version UpdaterState::GetUpdaterVersion(bool is_machine) {
52 const HKEY root_key = is_machine ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
53 base::string16 version;
54 base::win::RegKey key;
55
56 if (key.Open(root_key, kRegPathClientsGoogleUpdate,
57 KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS &&
58 key.ReadValue(kRegValueGoogleUpdatePv, &version) == ERROR_SUCCESS) {
59 return base::Version(base::UTF16ToUTF8(version));
60 }
61
62 return base::Version();
63}
64
65base::Time UpdaterState::GetUpdaterLastStartedAU(bool is_machine) {
66 return GetUpdaterTimeValue(is_machine, kRegValueLastStartedAU);
67}
68
69base::Time UpdaterState::GetUpdaterLastChecked(bool is_machine) {
70 return GetUpdaterTimeValue(is_machine, kRegValueLastChecked);
71}
72
73base::Time UpdaterState::GetUpdaterTimeValue(bool is_machine,
74 const wchar_t* value_name) {
75 const HKEY root_key = is_machine ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
76 base::win::RegKey update_key;
77
78 if (update_key.Open(root_key, kRegPathGoogleUpdate,
79 KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) {
80 DWORD value(0);
81 if (update_key.ReadValueDW(value_name, &value) == ERROR_SUCCESS) {
82 return base::Time::FromTimeT(value);
83 }
84 }
85
86 return base::Time();
87}
88
89bool 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
136bool UpdaterState::IsJoinedToDomain() {
137 return base::win::IsEnrolledToDomain();
138}
139
140} // namespace update_client