blob: c3581e41568b597d3b054d81bf6265f4affdaff9 [file] [log] [blame]
sorin97bd0292016-11-14 19:46:531
2// Copyright (c) 2016 The Chromium Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5
6#include "components/update_client/updater_state.h"
7
8#include <utility>
9
Hector Carmonac3565bc42019-02-01 23:31:2110#include "base/enterprise_util.h"
sorin97bd0292016-11-14 19:46:5311#include "base/strings/string16.h"
12#include "base/strings/string_number_conversions.h"
13#include "base/strings/utf_string_conversions.h"
Nico Weber356b3042019-08-23 15:30:4114#include "build/branding_buildflags.h"
sorin96d71e0c2017-01-25 17:39:1115#include "build/build_config.h"
sorin97bd0292016-11-14 19:46:5316
17namespace update_client {
18
rogerta57aed382017-02-23 14:50:4519// The value of this constant does not reflect its name (i.e. "domainjoined"
20// vs something like "isenterprisemanaged") because it is used with omaha.
21// After discussion with omaha team it was decided to leave the value as is to
22// keep continuity with previous chrome versions.
23const char UpdaterState::kIsEnterpriseManaged[] = "domainjoined";
sorin97bd0292016-11-14 19:46:5324
25UpdaterState::UpdaterState(bool is_machine) : is_machine_(is_machine) {}
26
Sorin Jianu30881152020-03-16 14:31:1927UpdaterState::~UpdaterState() = default;
sorin97bd0292016-11-14 19:46:5328
29std::unique_ptr<UpdaterState::Attributes> UpdaterState::GetState(
30 bool is_machine) {
borisva97d5302017-05-10 17:11:5531#if defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS))
sorin97bd0292016-11-14 19:46:5332 UpdaterState updater_state(is_machine);
33 updater_state.ReadState();
Jinho Bangda4e4282018-01-03 13:21:2334 return std::make_unique<Attributes>(updater_state.BuildAttributes());
sorin97bd0292016-11-14 19:46:5335#else
36 return nullptr;
borisva97d5302017-05-10 17:11:5537#endif // OS_WIN or Mac
sorin97bd0292016-11-14 19:46:5338}
39
borisva97d5302017-05-10 17:11:5540#if defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS))
sorin97bd0292016-11-14 19:46:5341void UpdaterState::ReadState() {
Hector Carmonac3565bc42019-02-01 23:31:2142 is_enterprise_managed_ = base::IsMachineExternallyManaged();
sorin97bd0292016-11-14 19:46:5343
Nico Weber356b3042019-08-23 15:30:4144#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
sorin97bd0292016-11-14 19:46:5345 updater_name_ = GetUpdaterName();
46 updater_version_ = GetUpdaterVersion(is_machine_);
47 last_autoupdate_started_ = GetUpdaterLastStartedAU(is_machine_);
48 last_checked_ = GetUpdaterLastChecked(is_machine_);
49 is_autoupdate_check_enabled_ = IsAutoupdateCheckEnabled();
50 update_policy_ = GetUpdatePolicy();
Nico Weber356b3042019-08-23 15:30:4151#endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
sorin97bd0292016-11-14 19:46:5352}
borisva97d5302017-05-10 17:11:5553#endif // OS_WIN or Mac
sorin97bd0292016-11-14 19:46:5354
55UpdaterState::Attributes UpdaterState::BuildAttributes() const {
56 Attributes attributes;
57
Sorin Jianudf2b9cc2018-02-05 19:11:0358#if defined(OS_WIN)
59 // Only Windows implements this attribute in a meaningful way.
60 attributes["ismachine"] = is_machine_ ? "1" : "0";
61#endif // OS_WIN
rogerta57aed382017-02-23 14:50:4562 attributes[kIsEnterpriseManaged] = is_enterprise_managed_ ? "1" : "0";
sorin97bd0292016-11-14 19:46:5363
64 attributes["name"] = updater_name_;
65
66 if (updater_version_.IsValid())
67 attributes["version"] = updater_version_.GetString();
68
69 const base::Time now = base::Time::NowFromSystemTime();
70 if (!last_autoupdate_started_.is_null())
71 attributes["laststarted"] =
72 NormalizeTimeDelta(now - last_autoupdate_started_);
73 if (!last_checked_.is_null())
74 attributes["lastchecked"] = NormalizeTimeDelta(now - last_checked_);
75
76 attributes["autoupdatecheckenabled"] =
77 is_autoupdate_check_enabled_ ? "1" : "0";
78
79 DCHECK((update_policy_ >= 0 && update_policy_ <= 3) || update_policy_ == -1);
Raul Tambref88e5102019-02-06 10:54:0380 attributes["updatepolicy"] = base::NumberToString(update_policy_);
sorin97bd0292016-11-14 19:46:5381
82 return attributes;
83}
84
85std::string UpdaterState::NormalizeTimeDelta(const base::TimeDelta& delta) {
86 const base::TimeDelta two_weeks = base::TimeDelta::FromDays(14);
Joshua Pawlickicf62dcba2020-02-26 16:56:5787 const base::TimeDelta two_months = base::TimeDelta::FromDays(56);
sorin97bd0292016-11-14 19:46:5388
89 std::string val; // Contains the value to return in hours.
90 if (delta <= two_weeks) {
91 val = "0";
92 } else if (two_weeks < delta && delta <= two_months) {
Joshua Pawlickicf62dcba2020-02-26 16:56:5793 val = "336"; // 2 weeks in hours.
sorin97bd0292016-11-14 19:46:5394 } else {
95 val = "1344"; // 2*28 days in hours.
96 }
97
98 DCHECK(!val.empty());
99 return val;
100}
101
102} // namespace update_client