metrics.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2015 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 "ash/components/proximity_auth/metrics.h"
  5. #include <stdint.h>
  6. #include <algorithm>
  7. #include "base/check_op.h"
  8. #include "base/cxx17_backports.h"
  9. #include "base/hash/md5.h"
  10. #include "base/metrics/histogram_functions.h"
  11. #include "base/metrics/histogram_macros.h"
  12. #include "base/sys_byteorder.h"
  13. namespace proximity_auth {
  14. namespace metrics {
  15. namespace {
  16. // Converts the 4-byte prefix of an MD5 hash into a int32_t value.
  17. int32_t DigestToInt32(const base::MD5Digest& digest) {
  18. // First, copy to a uint32_t, since byte swapping and endianness conversions
  19. // expect unsigned integers.
  20. uint32_t unsigned_value;
  21. DCHECK_GE(std::size(digest.a), sizeof(unsigned_value));
  22. memcpy(&unsigned_value, digest.a, sizeof(unsigned_value));
  23. unsigned_value = base::ByteSwap(base::HostToNet32(unsigned_value));
  24. // Then copy the resulting bit pattern to an int32_t to match the datatype
  25. // that histograms expect.
  26. int32_t value;
  27. memcpy(&value, &unsigned_value, sizeof(value));
  28. return value;
  29. }
  30. // Returns a hash of the given |name|, encoded as a 32-bit signed integer.
  31. int32_t HashDeviceModelName(const std::string& name) {
  32. base::MD5Digest digest;
  33. base::MD5Sum(name.c_str(), name.size(), &digest);
  34. return DigestToInt32(digest);
  35. }
  36. } // namespace
  37. const char kUnknownDeviceModel[] = "Unknown";
  38. const int kUnknownProximityValue = 127;
  39. void RecordAuthProximityRollingRssi(int rolling_rssi) {
  40. if (rolling_rssi != kUnknownProximityValue)
  41. rolling_rssi = base::clamp(rolling_rssi, -100, 50);
  42. base::UmaHistogramSparse("EasyUnlock.AuthProximity.RollingRssi",
  43. rolling_rssi);
  44. }
  45. void RecordAuthProximityRemoteDeviceModelHash(const std::string& device_model) {
  46. base::UmaHistogramSparse("EasyUnlock.AuthProximity.RemoteDeviceModelHash",
  47. HashDeviceModelName(device_model));
  48. }
  49. void RecordRemoteSecuritySettingsState(RemoteSecuritySettingsState state) {
  50. DCHECK(state < RemoteSecuritySettingsState::COUNT);
  51. UMA_HISTOGRAM_ENUMERATION(
  52. "EasyUnlock.RemoteLockScreenState", static_cast<int>(state),
  53. static_cast<int>(RemoteSecuritySettingsState::COUNT));
  54. }
  55. } // namespace metrics
  56. } // namespace proximity_auth