factory_ping_embargo_check.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2018 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. #include "chromeos/system/factory_ping_embargo_check.h"
  5. #include <string>
  6. #include "base/logging.h"
  7. #include "base/metrics/histogram_macros.h"
  8. #include "chromeos/system/statistics_provider.h"
  9. namespace chromeos {
  10. namespace system {
  11. namespace {
  12. // These values are persisted to logs. Entries should not be renumbered and
  13. // numeric values should never be reused.
  14. // These values must match the corresponding enum defined in enums.xml.
  15. enum class EndDateValidityHistogramValue {
  16. kMalformed = 0,
  17. kInvalid = 1,
  18. kValid = 2,
  19. kMaxValue = kValid,
  20. };
  21. // This is in a helper function to help the compiler avoid generating duplicated
  22. // code.
  23. void RecordEndDateValidity(const char* uma_prefix,
  24. EndDateValidityHistogramValue value) {
  25. if (uma_prefix)
  26. UMA_HISTOGRAM_ENUMERATION(std::string(uma_prefix) + ".EndDateValidity",
  27. value);
  28. }
  29. } // namespace
  30. FactoryPingEmbargoState GetPingEmbargoState(
  31. StatisticsProvider* statistics_provider,
  32. const std::string& key_name,
  33. const char* uma_prefix) {
  34. std::string ping_embargo_end_date;
  35. if (!statistics_provider->GetMachineStatistic(key_name,
  36. &ping_embargo_end_date)) {
  37. return FactoryPingEmbargoState::kMissingOrMalformed;
  38. }
  39. base::Time parsed_time;
  40. if (!base::Time::FromUTCString(ping_embargo_end_date.c_str(), &parsed_time)) {
  41. LOG(ERROR) << key_name << " exists but cannot be parsed.";
  42. RecordEndDateValidity(uma_prefix,
  43. EndDateValidityHistogramValue::kMalformed);
  44. return FactoryPingEmbargoState::kMissingOrMalformed;
  45. }
  46. if (parsed_time - base::Time::Now() >= kEmbargoEndDateGarbageDateThreshold) {
  47. // If the date is more than this many days in the future, ignore it.
  48. // Because it indicates that the device is not connected to an NTP server
  49. // in the factory, and its internal clock could be off when the date is
  50. // written.
  51. RecordEndDateValidity(uma_prefix, EndDateValidityHistogramValue::kInvalid);
  52. return FactoryPingEmbargoState::kInvalid;
  53. }
  54. RecordEndDateValidity(uma_prefix, EndDateValidityHistogramValue::kValid);
  55. return base::Time::Now() > parsed_time ? FactoryPingEmbargoState::kPassed
  56. : FactoryPingEmbargoState::kNotPassed;
  57. }
  58. FactoryPingEmbargoState GetEnterpriseManagementPingEmbargoState(
  59. StatisticsProvider* statistics_provider) {
  60. std::string ping_embargo_end_date;
  61. if (statistics_provider->GetMachineStatistic(
  62. kEnterpriseManagementEmbargoEndDateKey, &ping_embargo_end_date))
  63. return GetPingEmbargoState(statistics_provider,
  64. kEnterpriseManagementEmbargoEndDateKey,
  65. "FactoryPingEmbargo");
  66. // Default to the RLZ ping embargo if no value for an enterprise management
  67. // embargo.
  68. return GetRlzPingEmbargoState(statistics_provider);
  69. }
  70. FactoryPingEmbargoState GetRlzPingEmbargoState(
  71. StatisticsProvider* statistics_provider) {
  72. return GetPingEmbargoState(statistics_provider, kRlzEmbargoEndDateKey,
  73. /*uma_prefix=*/nullptr);
  74. }
  75. } // namespace system
  76. } // namespace chromeos