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" |
| 14 | |
| 15 | namespace update_client { |
| 16 | |
| 17 | const char UpdaterState::kDomainJoined[] = "domainjoined"; |
| 18 | |
| 19 | UpdaterState::UpdaterState(bool is_machine) : is_machine_(is_machine) {} |
| 20 | |
| 21 | UpdaterState::~UpdaterState() {} |
| 22 | |
| 23 | std::unique_ptr<UpdaterState::Attributes> UpdaterState::GetState( |
| 24 | bool is_machine) { |
| 25 | #if defined(OS_WIN) |
| 26 | UpdaterState updater_state(is_machine); |
| 27 | updater_state.ReadState(); |
| 28 | return base::MakeUnique<Attributes>(updater_state.BuildAttributes()); |
| 29 | #else |
| 30 | return nullptr; |
| 31 | #endif // OS_WIN |
| 32 | } |
| 33 | |
| 34 | #if defined(OS_WIN) |
| 35 | void UpdaterState::ReadState() { |
| 36 | is_joined_to_domain_ = IsJoinedToDomain(); |
| 37 | |
| 38 | #if defined(GOOGLE_CHROME_BUILD) |
| 39 | updater_name_ = GetUpdaterName(); |
| 40 | updater_version_ = GetUpdaterVersion(is_machine_); |
| 41 | last_autoupdate_started_ = GetUpdaterLastStartedAU(is_machine_); |
| 42 | last_checked_ = GetUpdaterLastChecked(is_machine_); |
| 43 | is_autoupdate_check_enabled_ = IsAutoupdateCheckEnabled(); |
| 44 | update_policy_ = GetUpdatePolicy(); |
| 45 | #endif // GOOGLE_CHROME_BUILD |
| 46 | } |
| 47 | #endif // OS_WIN |
| 48 | |
| 49 | UpdaterState::Attributes UpdaterState::BuildAttributes() const { |
| 50 | Attributes attributes; |
| 51 | |
| 52 | attributes[kDomainJoined] = is_joined_to_domain_ ? "1" : "0"; |
| 53 | |
| 54 | attributes["name"] = updater_name_; |
| 55 | |
| 56 | if (updater_version_.IsValid()) |
| 57 | attributes["version"] = updater_version_.GetString(); |
| 58 | |
| 59 | const base::Time now = base::Time::NowFromSystemTime(); |
| 60 | if (!last_autoupdate_started_.is_null()) |
| 61 | attributes["laststarted"] = |
| 62 | NormalizeTimeDelta(now - last_autoupdate_started_); |
| 63 | if (!last_checked_.is_null()) |
| 64 | attributes["lastchecked"] = NormalizeTimeDelta(now - last_checked_); |
| 65 | |
| 66 | attributes["autoupdatecheckenabled"] = |
| 67 | is_autoupdate_check_enabled_ ? "1" : "0"; |
| 68 | |
| 69 | DCHECK((update_policy_ >= 0 && update_policy_ <= 3) || update_policy_ == -1); |
| 70 | attributes["updatepolicy"] = base::IntToString(update_policy_); |
| 71 | |
| 72 | return attributes; |
| 73 | } |
| 74 | |
| 75 | std::string UpdaterState::NormalizeTimeDelta(const base::TimeDelta& delta) { |
| 76 | const base::TimeDelta two_weeks = base::TimeDelta::FromDays(14); |
| 77 | const base::TimeDelta two_months = base::TimeDelta::FromDays(60); |
| 78 | |
| 79 | std::string val; // Contains the value to return in hours. |
| 80 | if (delta <= two_weeks) { |
| 81 | val = "0"; |
| 82 | } else if (two_weeks < delta && delta <= two_months) { |
| 83 | val = "408"; // 2 weeks in hours. |
| 84 | } else { |
| 85 | val = "1344"; // 2*28 days in hours. |
| 86 | } |
| 87 | |
| 88 | DCHECK(!val.empty()); |
| 89 | return val; |
| 90 | } |
| 91 | |
| 92 | } // namespace update_client |