sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 1 | |
| 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 Carmona | c3565bc4 | 2019-02-01 23:31:21 | [diff] [blame] | 10 | #include "base/enterprise_util.h" |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 11 | #include "base/strings/string16.h" |
| 12 | #include "base/strings/string_number_conversions.h" |
| 13 | #include "base/strings/utf_string_conversions.h" |
Nico Weber | 356b304 | 2019-08-23 15:30:41 | [diff] [blame] | 14 | #include "build/branding_buildflags.h" |
sorin | 96d71e0c | 2017-01-25 17:39:11 | [diff] [blame] | 15 | #include "build/build_config.h" |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 16 | |
| 17 | namespace update_client { |
| 18 | |
rogerta | 57aed38 | 2017-02-23 14:50:45 | [diff] [blame] | 19 | // 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. |
| 23 | const char UpdaterState::kIsEnterpriseManaged[] = "domainjoined"; |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 24 | |
| 25 | UpdaterState::UpdaterState(bool is_machine) : is_machine_(is_machine) {} |
| 26 | |
Sorin Jianu | 3088115 | 2020-03-16 14:31:19 | [diff] [blame] | 27 | UpdaterState::~UpdaterState() = default; |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 28 | |
| 29 | std::unique_ptr<UpdaterState::Attributes> UpdaterState::GetState( |
| 30 | bool is_machine) { |
borisv | a97d530 | 2017-05-10 17:11:55 | [diff] [blame] | 31 | #if defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS)) |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 32 | UpdaterState updater_state(is_machine); |
| 33 | updater_state.ReadState(); |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 34 | return std::make_unique<Attributes>(updater_state.BuildAttributes()); |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 35 | #else |
| 36 | return nullptr; |
borisv | a97d530 | 2017-05-10 17:11:55 | [diff] [blame] | 37 | #endif // OS_WIN or Mac |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 38 | } |
| 39 | |
borisv | a97d530 | 2017-05-10 17:11:55 | [diff] [blame] | 40 | #if defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS)) |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 41 | void UpdaterState::ReadState() { |
Hector Carmona | c3565bc4 | 2019-02-01 23:31:21 | [diff] [blame] | 42 | is_enterprise_managed_ = base::IsMachineExternallyManaged(); |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 43 | |
Nico Weber | 356b304 | 2019-08-23 15:30:41 | [diff] [blame] | 44 | #if BUILDFLAG(GOOGLE_CHROME_BRANDING) |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 45 | 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 Weber | 356b304 | 2019-08-23 15:30:41 | [diff] [blame] | 51 | #endif // BUILDFLAG(GOOGLE_CHROME_BRANDING) |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 52 | } |
borisv | a97d530 | 2017-05-10 17:11:55 | [diff] [blame] | 53 | #endif // OS_WIN or Mac |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 54 | |
| 55 | UpdaterState::Attributes UpdaterState::BuildAttributes() const { |
| 56 | Attributes attributes; |
| 57 | |
Sorin Jianu | df2b9cc | 2018-02-05 19:11:03 | [diff] [blame] | 58 | #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 |
rogerta | 57aed38 | 2017-02-23 14:50:45 | [diff] [blame] | 62 | attributes[kIsEnterpriseManaged] = is_enterprise_managed_ ? "1" : "0"; |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 63 | |
| 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 Tambre | f88e510 | 2019-02-06 10:54:03 | [diff] [blame] | 80 | attributes["updatepolicy"] = base::NumberToString(update_policy_); |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 81 | |
| 82 | return attributes; |
| 83 | } |
| 84 | |
| 85 | std::string UpdaterState::NormalizeTimeDelta(const base::TimeDelta& delta) { |
| 86 | const base::TimeDelta two_weeks = base::TimeDelta::FromDays(14); |
Joshua Pawlicki | cf62dcba | 2020-02-26 16:56:57 | [diff] [blame] | 87 | const base::TimeDelta two_months = base::TimeDelta::FromDays(56); |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 88 | |
| 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 Pawlicki | cf62dcba | 2020-02-26 16:56:57 | [diff] [blame] | 93 | val = "336"; // 2 weeks in hours. |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 94 | } else { |
| 95 | val = "1344"; // 2*28 days in hours. |
| 96 | } |
| 97 | |
| 98 | DCHECK(!val.empty()); |
| 99 | return val; |
| 100 | } |
| 101 | |
| 102 | } // namespace update_client |