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 
5 #include "content/browser/background_sync/background_sync_metrics.h"
6 
7 #include "base/metrics/histogram_functions.h"
8 
9 namespace {
10 
EventResultToResultPattern(bool success,bool finished_in_foreground)11 content::BackgroundSyncMetrics::ResultPattern EventResultToResultPattern(
12     bool success,
13     bool finished_in_foreground) {
14   if (success) {
15     return finished_in_foreground ? content::BackgroundSyncMetrics::
16                                         RESULT_PATTERN_SUCCESS_FOREGROUND
17                                   : content::BackgroundSyncMetrics::
18                                         RESULT_PATTERN_SUCCESS_BACKGROUND;
19   }
20   return finished_in_foreground
21              ? content::BackgroundSyncMetrics::RESULT_PATTERN_FAILED_FOREGROUND
22              : content::BackgroundSyncMetrics::RESULT_PATTERN_FAILED_BACKGROUND;
23 }
24 
GetBackgroundSyncSuffix(blink::mojom::BackgroundSyncType sync_type)25 const std::string GetBackgroundSyncSuffix(
26     blink::mojom::BackgroundSyncType sync_type) {
27   if (sync_type == blink::mojom::BackgroundSyncType::ONE_SHOT)
28     return "OneShot";
29   else
30     return "Periodic";
31 }
32 
GetBackgroundSyncPrefix(blink::mojom::BackgroundSyncType sync_type)33 const std::string GetBackgroundSyncPrefix(
34     blink::mojom::BackgroundSyncType sync_type) {
35   if (sync_type == blink::mojom::BackgroundSyncType::ONE_SHOT)
36     return "";
37   else
38     return "Periodic";
39 }
40 
41 }  // namespace
42 
43 namespace content {
44 
45 // static
RecordEventStarted(blink::mojom::BackgroundSyncType sync_type,bool started_in_foreground)46 void BackgroundSyncMetrics::RecordEventStarted(
47     blink::mojom::BackgroundSyncType sync_type,
48     bool started_in_foreground) {
49   base::UmaHistogramBoolean("BackgroundSync.Event." +
50                                 GetBackgroundSyncSuffix(sync_type) +
51                                 "StartedInForeground",
52                             started_in_foreground);
53 }
54 
55 // static
RecordRegistrationComplete(bool event_succeeded,int num_attempts_required)56 void BackgroundSyncMetrics::RecordRegistrationComplete(
57     bool event_succeeded,
58     int num_attempts_required) {
59   base::UmaHistogramBoolean(
60       "BackgroundSync.Registration.OneShot.EventSucceededAtCompletion",
61       event_succeeded);
62 
63   if (!event_succeeded)
64     return;
65 
66   base::UmaHistogramExactLinear(
67       "BackgroundSync.Registration.OneShot.NumAttemptsForSuccessfulEvent",
68       num_attempts_required, 50);
69 }
70 
71 // static
RecordEventResult(blink::mojom::BackgroundSyncType sync_type,bool success,bool finished_in_foreground)72 void BackgroundSyncMetrics::RecordEventResult(
73     blink::mojom::BackgroundSyncType sync_type,
74     bool success,
75     bool finished_in_foreground) {
76   base::UmaHistogramEnumeration(
77       "BackgroundSync.Event." + GetBackgroundSyncSuffix(sync_type) +
78           "ResultPattern",
79       EventResultToResultPattern(success, finished_in_foreground),
80       static_cast<ResultPattern>(RESULT_PATTERN_MAX + 1));
81 }
82 
83 // static
RecordBatchSyncEventComplete(blink::mojom::BackgroundSyncType sync_type,const base::TimeDelta & time,bool from_wakeup_task,int number_of_batched_sync_events)84 void BackgroundSyncMetrics::RecordBatchSyncEventComplete(
85     blink::mojom::BackgroundSyncType sync_type,
86     const base::TimeDelta& time,
87     bool from_wakeup_task,
88     int number_of_batched_sync_events) {
89   // The total batch handling time should be under 5 minutes; we'll record up to
90   // 6 minutes, to be safe.
91   base::UmaHistogramCustomTimes(
92       GetBackgroundSyncPrefix(sync_type) + "BackgroundSync.Event.Time", time,
93       /* min= */ base::TimeDelta::FromMilliseconds(10),
94       /* max= */ base::TimeDelta::FromMinutes(6),
95       /* buckets= */ 50);
96   base::UmaHistogramCounts100(
97       GetBackgroundSyncPrefix(sync_type) + "BackgroundSync.Event.BatchSize",
98       number_of_batched_sync_events);
99 
100   base::UmaHistogramBoolean(GetBackgroundSyncPrefix(sync_type) +
101                                 "BackgroundSync.Event.FromWakeupTask",
102                             from_wakeup_task);
103 }
104 
105 // static
CountRegisterSuccess(blink::mojom::BackgroundSyncType sync_type,int64_t min_interval_ms,RegistrationCouldFire registration_could_fire,RegistrationIsDuplicate registration_is_duplicate)106 void BackgroundSyncMetrics::CountRegisterSuccess(
107     blink::mojom::BackgroundSyncType sync_type,
108     int64_t min_interval_ms,
109     RegistrationCouldFire registration_could_fire,
110     RegistrationIsDuplicate registration_is_duplicate) {
111   base::UmaHistogramEnumeration(
112       "BackgroundSync.Registration." + GetBackgroundSyncSuffix(sync_type),
113       BACKGROUND_SYNC_STATUS_OK,
114       static_cast<BackgroundSyncStatus>(BACKGROUND_SYNC_STATUS_MAX + 1));
115 
116   if (sync_type == blink::mojom::BackgroundSyncType::ONE_SHOT) {
117     base::UmaHistogramBoolean(
118         "BackgroundSync.Registration.OneShot.CouldFire",
119         registration_could_fire == REGISTRATION_COULD_FIRE);
120   } else {
121     DCHECK_GE(min_interval_ms, 0);
122     base::UmaHistogramCounts10M(
123         "BackgroundSync.Registration.Periodic.MinInterval",
124         min_interval_ms / 1000);
125   }
126 
127   base::UmaHistogramBoolean(
128       "BackgroundSync.Registration." + GetBackgroundSyncSuffix(sync_type) +
129           ".IsDuplicate",
130       registration_is_duplicate == REGISTRATION_IS_DUPLICATE);
131 }
132 
133 // static
CountRegisterFailure(blink::mojom::BackgroundSyncType sync_type,BackgroundSyncStatus result)134 void BackgroundSyncMetrics::CountRegisterFailure(
135     blink::mojom::BackgroundSyncType sync_type,
136     BackgroundSyncStatus result) {
137   base::UmaHistogramEnumeration(
138       std::string("BackgroundSync.Registration.") +
139           GetBackgroundSyncSuffix(sync_type),
140       result,
141       static_cast<BackgroundSyncStatus>(BACKGROUND_SYNC_STATUS_MAX + 1));
142 }
143 
144 // static
CountUnregisterPeriodicSync(BackgroundSyncStatus status)145 void BackgroundSyncMetrics::CountUnregisterPeriodicSync(
146     BackgroundSyncStatus status) {
147   base::UmaHistogramEnumeration(
148       "BackgroundSync.Unregistration.Periodic", status,
149       static_cast<BackgroundSyncStatus>(BACKGROUND_SYNC_STATUS_MAX + 1));
150 }
151 
152 // static
RecordEventsFiredFromWakeupTask(blink::mojom::BackgroundSyncType sync_type,bool fired_events)153 void BackgroundSyncMetrics::RecordEventsFiredFromWakeupTask(
154     blink::mojom::BackgroundSyncType sync_type,
155     bool fired_events) {
156   base::UmaHistogramBoolean("BackgroundSync.WakeupTaskFiredEvents." +
157                                 GetBackgroundSyncSuffix(sync_type),
158                             fired_events);
159 }
160 
161 }  // namespace content
162