app_package_name_logging_rule.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2021 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 "android_webview/common/metrics/app_package_name_logging_rule.h"
  5. #include "base/json/values_util.h"
  6. #include "base/time/time.h"
  7. #include "base/values.h"
  8. #include "base/version.h"
  9. #include "third_party/abseil-cpp/absl/types/optional.h"
  10. namespace android_webview {
  11. namespace {
  12. constexpr char kExpiryDateKey[] = "expiry-date";
  13. constexpr char kVersionKey[] = "allowlist-version";
  14. } // namespace
  15. AppPackageNameLoggingRule::AppPackageNameLoggingRule(
  16. const base::Version& version,
  17. const base::Time& expiry_date)
  18. : version_(version), expiry_date_(expiry_date) {
  19. DCHECK(version.IsValid());
  20. DCHECK(!expiry_date.is_null());
  21. }
  22. base::Version AppPackageNameLoggingRule::GetVersion() const {
  23. return version_;
  24. }
  25. base::Time AppPackageNameLoggingRule::GetExpiryDate() const {
  26. return expiry_date_;
  27. }
  28. bool AppPackageNameLoggingRule::IsAppPackageNameAllowed() const {
  29. return expiry_date_ >= base::Time::Now();
  30. }
  31. bool AppPackageNameLoggingRule::IsSameAs(
  32. const AppPackageNameLoggingRule& record) const {
  33. if (record.GetVersion() == GetVersion()) {
  34. DCHECK(record.GetExpiryDate() == GetExpiryDate());
  35. return true;
  36. }
  37. return false;
  38. }
  39. // static
  40. absl::optional<AppPackageNameLoggingRule>
  41. AppPackageNameLoggingRule::FromDictionary(const base::Value& dict) {
  42. const std::string* version_string = dict.FindStringKey(kVersionKey);
  43. if (!version_string) {
  44. return absl::optional<AppPackageNameLoggingRule>();
  45. }
  46. base::Version version(*version_string);
  47. if (!version.IsValid()) {
  48. return absl::optional<AppPackageNameLoggingRule>();
  49. }
  50. const base::Value* expiry_date_value = dict.FindKey(kExpiryDateKey);
  51. if (!expiry_date_value) {
  52. return AppPackageNameLoggingRule(version, base::Time::Min());
  53. }
  54. absl::optional<base::Time> expiry_date =
  55. base::ValueToTime(*expiry_date_value);
  56. if (!expiry_date.has_value()) {
  57. return AppPackageNameLoggingRule(version, base::Time::Min());
  58. }
  59. return AppPackageNameLoggingRule(version, expiry_date.value());
  60. }
  61. base::Value AppPackageNameLoggingRule::ToDictionary() {
  62. base::Value dict(base::Value::Type::DICTIONARY);
  63. dict.SetStringKey(kVersionKey, version_.GetString());
  64. if (!expiry_date_.is_min()) {
  65. dict.SetKey(kExpiryDateKey, base::TimeToValue(expiry_date_));
  66. }
  67. return dict;
  68. }
  69. } // namespace android_webview