• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/metrics/structured/neutrino_logging.h"
6 
7 #include "base/time/time.h"
8 #include "components/metrics/structured/recorder.h"
9 #include "components/metrics/structured/structured_events.h"
10 
11 namespace {
12 
13 // Return the integer floor of the log base 2 of the time since |timestamp|,
14 // measured in days.
Log2TimeSince(int64_t timestamp)15 int Log2TimeSince(int64_t timestamp) {
16   return floor(
17       log2((base::Time::Now() - base::Time::FromTimeT(timestamp)).InSecondsF() /
18            base::Days(1).InSecondsF()));
19 }
20 
21 }  // namespace
22 
23 namespace metrics {
24 namespace structured {
25 
NeutrinoDevicesLog(NeutrinoDevicesLocation location)26 void NeutrinoDevicesLog(NeutrinoDevicesLocation location) {
27   NeutrinoDevicesLogWithClientId("", location);
28 }
29 
NeutrinoDevicesLogWithClientId(const std::string & client_id,NeutrinoDevicesLocation location)30 void NeutrinoDevicesLogWithClientId(const std::string& client_id,
31                                     NeutrinoDevicesLocation location) {
32   events::v2::neutrino_devices::CodePoint code_point;
33   if (!client_id.empty())
34     code_point.SetClientId(client_id);
35   code_point.SetLocation(static_cast<int64_t>(location)).Record();
36 }
37 
NeutrinoDevicesLogPolicy(const std::string & client_id,bool is_managed,NeutrinoDevicesLocation location)38 void NeutrinoDevicesLogPolicy(const std::string& client_id,
39                               bool is_managed,
40                               NeutrinoDevicesLocation location) {
41   events::v2::neutrino_devices::Enrollment enrollment;
42   if (!client_id.empty())
43     enrollment.SetClientId(client_id);
44   enrollment.SetIsManagedPolicy(is_managed)
45       .SetLocation(static_cast<int64_t>(location))
46       .Record();
47 }
48 
NeutrinoDevicesLogClientIdCleared(const std::string & client_id,int64_t install_date_timestamp,int64_t metrics_reporting_enabled_timestamp)49 void NeutrinoDevicesLogClientIdCleared(
50     const std::string& client_id,
51     int64_t install_date_timestamp,
52     int64_t metrics_reporting_enabled_timestamp) {
53   events::v2::neutrino_devices::ClientIdCleared client_id_cleared;
54   if (!client_id.empty())
55     client_id_cleared.SetInitialClientId(client_id);
56   client_id_cleared
57       .SetLog2TimeSinceInstallation(Log2TimeSince(install_date_timestamp))
58       .SetLog2TimeSinceMetricsEnabled(
59           Log2TimeSince(metrics_reporting_enabled_timestamp))
60       .Record();
61 }
62 
NeutrinoDevicesLogClientIdChanged(const std::string & client_id,const std::string & initial_client_id,int64_t install_date_timestamp,int64_t metrics_reporting_enabled_timestamp,NeutrinoDevicesLocation location)63 void NeutrinoDevicesLogClientIdChanged(
64     const std::string& client_id,
65     const std::string& initial_client_id,
66     int64_t install_date_timestamp,
67     int64_t metrics_reporting_enabled_timestamp,
68     NeutrinoDevicesLocation location) {
69   events::v2::neutrino_devices::ClientIdChanged client_id_changed;
70 
71   if (!initial_client_id.empty())
72     client_id_changed.SetInitialClientId(initial_client_id);
73   if (!client_id.empty())
74     client_id_changed.SetFinalClientId(client_id);
75 
76   const absl::optional<int> last_key_rotation =
77       Recorder::GetInstance()->LastKeyRotation(client_id_changed);
78 
79   if (last_key_rotation.has_value()) {
80     int days_since_rotation =
81         (base::Time::Now() - base::Time::UnixEpoch()).InDays() -
82         last_key_rotation.value();
83     client_id_changed.SetDaysSinceKeyRotation(days_since_rotation);
84   }
85 
86   client_id_changed
87       .SetLog2TimeSinceInstallation(Log2TimeSince(install_date_timestamp))
88       .SetLog2TimeSinceMetricsEnabled(
89           Log2TimeSince(metrics_reporting_enabled_timestamp))
90       .SetLocation(static_cast<int64_t>(location))
91       .Record();
92 }
93 
94 }  // namespace structured
95 }  // namespace metrics
96