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 | |
| 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" |
sorin | 96d71e0c | 2017-01-25 17:39:11 | [diff] [blame] | 14 | #include "build/build_config.h" |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 15 | |
| 16 | namespace update_client { |
| 17 | |
rogerta | 57aed38 | 2017-02-23 14:50:45 | [diff] [blame] | 18 | // The value of this constant does not reflect its name (i.e. "domainjoined" |
| 19 | // vs something like "isenterprisemanaged") because it is used with omaha. |
| 20 | // After discussion with omaha team it was decided to leave the value as is to |
| 21 | // keep continuity with previous chrome versions. |
| 22 | const char UpdaterState::kIsEnterpriseManaged[] = "domainjoined"; |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 23 | |
| 24 | UpdaterState::UpdaterState(bool is_machine) : is_machine_(is_machine) {} |
| 25 | |
| 26 | UpdaterState::~UpdaterState() {} |
| 27 | |
| 28 | std::unique_ptr<UpdaterState::Attributes> UpdaterState::GetState( |
| 29 | bool is_machine) { |
| 30 | #if defined(OS_WIN) |
| 31 | UpdaterState updater_state(is_machine); |
| 32 | updater_state.ReadState(); |
| 33 | return base::MakeUnique<Attributes>(updater_state.BuildAttributes()); |
| 34 | #else |
| 35 | return nullptr; |
| 36 | #endif // OS_WIN |
| 37 | } |
| 38 | |
| 39 | #if defined(OS_WIN) |
| 40 | void UpdaterState::ReadState() { |
rogerta | 57aed38 | 2017-02-23 14:50:45 | [diff] [blame] | 41 | is_enterprise_managed_ = IsEnterpriseManaged(); |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 42 | |
| 43 | #if defined(GOOGLE_CHROME_BUILD) |
| 44 | updater_name_ = GetUpdaterName(); |
| 45 | updater_version_ = GetUpdaterVersion(is_machine_); |
| 46 | last_autoupdate_started_ = GetUpdaterLastStartedAU(is_machine_); |
| 47 | last_checked_ = GetUpdaterLastChecked(is_machine_); |
| 48 | is_autoupdate_check_enabled_ = IsAutoupdateCheckEnabled(); |
| 49 | update_policy_ = GetUpdatePolicy(); |
| 50 | #endif // GOOGLE_CHROME_BUILD |
| 51 | } |
| 52 | #endif // OS_WIN |
| 53 | |
| 54 | UpdaterState::Attributes UpdaterState::BuildAttributes() const { |
| 55 | Attributes attributes; |
| 56 | |
rogerta | 57aed38 | 2017-02-23 14:50:45 | [diff] [blame] | 57 | attributes[kIsEnterpriseManaged] = is_enterprise_managed_ ? "1" : "0"; |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 58 | |
| 59 | attributes["name"] = updater_name_; |
| 60 | |
| 61 | if (updater_version_.IsValid()) |
| 62 | attributes["version"] = updater_version_.GetString(); |
| 63 | |
| 64 | const base::Time now = base::Time::NowFromSystemTime(); |
| 65 | if (!last_autoupdate_started_.is_null()) |
| 66 | attributes["laststarted"] = |
| 67 | NormalizeTimeDelta(now - last_autoupdate_started_); |
| 68 | if (!last_checked_.is_null()) |
| 69 | attributes["lastchecked"] = NormalizeTimeDelta(now - last_checked_); |
| 70 | |
| 71 | attributes["autoupdatecheckenabled"] = |
| 72 | is_autoupdate_check_enabled_ ? "1" : "0"; |
| 73 | |
| 74 | DCHECK((update_policy_ >= 0 && update_policy_ <= 3) || update_policy_ == -1); |
| 75 | attributes["updatepolicy"] = base::IntToString(update_policy_); |
| 76 | |
| 77 | return attributes; |
| 78 | } |
| 79 | |
| 80 | std::string UpdaterState::NormalizeTimeDelta(const base::TimeDelta& delta) { |
| 81 | const base::TimeDelta two_weeks = base::TimeDelta::FromDays(14); |
| 82 | const base::TimeDelta two_months = base::TimeDelta::FromDays(60); |
| 83 | |
| 84 | std::string val; // Contains the value to return in hours. |
| 85 | if (delta <= two_weeks) { |
| 86 | val = "0"; |
| 87 | } else if (two_weeks < delta && delta <= two_months) { |
| 88 | val = "408"; // 2 weeks in hours. |
| 89 | } else { |
| 90 | val = "1344"; // 2*28 days in hours. |
| 91 | } |
| 92 | |
| 93 | DCHECK(!val.empty()); |
| 94 | return val; |
| 95 | } |
| 96 | |
| 97 | } // namespace update_client |