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.
5 #include "components/proximity_auth/metrics.h"
7 #include "base/logging.h"
9 #include "base/metrics/histogram_macros.h"
10 #include "base/metrics/sparse_histogram.h"
11 #include "base/sys_byteorder.h"
13 namespace proximity_auth
{
18 // Converts the 4-byte prefix of an MD5 hash into a int32 value.
19 int32
DigestToInt32(const base::MD5Digest
& digest
) {
20 // First, copy to a uint32, since byte swapping and endianness conversions
21 // expect unsigned integers.
22 uint32 unsigned_value
;
23 DCHECK_GE(arraysize(digest
.a
), sizeof(unsigned_value
));
24 memcpy(&unsigned_value
, digest
.a
, sizeof(unsigned_value
));
25 unsigned_value
= base::ByteSwap(base::HostToNet32(unsigned_value
));
27 // Then copy the resulting bit pattern to an int32 to match the datatype that
30 memcpy(&value
, &unsigned_value
, sizeof(value
));
34 // Returns a hash of the given |name|, encoded as a 32-bit signed integer.
35 int32
HashDeviceModelName(const std::string
& name
) {
36 base::MD5Digest digest
;
37 base::MD5Sum(name
.c_str(), name
.size(), &digest
);
38 return DigestToInt32(digest
);
43 const char kUnknownDeviceModel
[] = "Unknown";
44 const int kUnknownProximityValue
= 127;
46 void RecordAuthProximityRollingRssi(int rolling_rssi
) {
47 if (rolling_rssi
!= kUnknownProximityValue
)
48 rolling_rssi
= std::min(50, std::max(-100, rolling_rssi
));
50 UMA_HISTOGRAM_SPARSE_SLOWLY("EasyUnlock.AuthProximity.RollingRssi",
54 void RecordAuthProximityTransmitPowerDelta(int transmit_power_delta
) {
55 if (transmit_power_delta
!= kUnknownProximityValue
)
56 transmit_power_delta
= std::min(50, std::max(-100, transmit_power_delta
));
58 UMA_HISTOGRAM_SPARSE_SLOWLY("EasyUnlock.AuthProximity.TransmitPowerDelta",
59 transmit_power_delta
);
62 void RecordAuthProximityTimeSinceLastZeroRssi(
63 base::TimeDelta time_since_last_zero_rssi
) {
64 UMA_HISTOGRAM_TIMES("EasyUnlock.AuthProximity.TimeSinceLastZeroRssi",
65 time_since_last_zero_rssi
);
68 void RecordAuthProximityRemoteDeviceModelHash(const std::string
& device_model
) {
69 UMA_HISTOGRAM_SPARSE_SLOWLY("EasyUnlock.AuthProximity.RemoteDeviceModelHash",
70 HashDeviceModelName(device_model
));
73 void RecordRemoteSecuritySettingsState(RemoteSecuritySettingsState state
) {
74 DCHECK(state
< RemoteSecuritySettingsState::COUNT
);
75 UMA_HISTOGRAM_ENUMERATION(
76 "EasyUnlock.RemoteLockScreenState", static_cast<int>(state
),
77 static_cast<int>(RemoteSecuritySettingsState::COUNT
));
80 } // namespace metrics
81 } // namespace proximity_auth