1 // Copyright 2018 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 #ifndef COMPONENTS_TAB_COUNT_METRICS_TAB_COUNT_METRICS_H_
6 #define COMPONENTS_TAB_COUNT_METRICS_TAB_COUNT_METRICS_H_
7 
8 #include <string>
9 
10 #include "base/component_export.h"
11 
12 // This namespace contains functions for creating histograms bucketed by number
13 // of tabs.
14 //
15 // All bucket parameters --- number of buckets, bucket sizes, bucket names ---
16 // are determined at compile time, and these methods are safe to call from any
17 // thread.
18 //
19 // A typical example of creating a histogram bucketed by tab count using
20 // STATIC_HISTOGRAM_POINTER_GROUP looks something like this:
21 //  const size_t live_tab_count = GetLiveTabCount();
22 //  const size_t bucket =
23 //      tab_count_metrics::BucketForTabCount(live_tab_count);
24 //  STATIC_HISTOGRAM_POINTER_GROUP(
25 //      tab_count_metrics::HistogramName(constant_histogram_prefix,
26 //                                       live_tabs_only, bucket),
27 //      static_cast<int>(bucket),
28 //      static_cast<int>(tab_count_metrics::kNumTabCountBuckets),
29 //      Add(sample),
30 //      base::Histogram::FactoryGet(
31 //          tab_count_metrics::HistogramName(constant_histogram_prefix,
32 //                                           live_tabs_only, bucket),
33 //          MINIMUM_SAMPLE, MAXIMUM_SAMPLE, BUCKET_COUNT,
34 //          base::HistogramBase::kUmaTargetedHistogramFlag));
35 //  }
36 namespace tab_count_metrics {
37 
38 // |kNumTabCountBuckets| is used in various constexpr arrays and as a bound
39 // on the histogram array when using STATIC_HISTOGRAM_POINTER_GROUP. This value
40 // must be equal to the length of the array of tab count bucket min values
41 // (|kTabCountBucketMins|) and the array of bucket names
42 // (|kTabCountBucketNames|) found in the corresponding .cc file.
43 constexpr size_t kNumTabCountBuckets = 8;
44 
45 // Returns the histogram name for |bucket|. The histogram name is the
46 // concatenation of |prefix| and the name corresponding to |bucket|, which is of
47 // the form |prefix| + ".ByTabCount."/".ByLiveTabCount." + <BucketRangeText>,
48 // where <BucketRangeText> is a string describing the bucket range, e.g. "1Tab",
49 // "3To4Tabs", etc. See |kTabCountBucketNames| for all of the bucket names.
50 // |bucket| must be in the interval [0, |kNumTabCountBuckets|).
51 COMPONENT_EXPORT(TAB_COUNT_METRICS)
52 std::string HistogramName(const std::string prefix,
53                           bool live_tabs_only,
54                           size_t bucket);
55 
56 // Return the bucket index for the |num_tabs|.
57 COMPONENT_EXPORT(TAB_COUNT_METRICS)
58 size_t BucketForTabCount(size_t num_tabs);
59 
60 // These are exposed for unit tests.
61 namespace internal {
62 
63 // Returns the number of tabs corresponding to the minimum value of |bucket|.
64 // |bucket| must be in the interval [0, |kNumTabCountBuckets|).
65 COMPONENT_EXPORT(TAB_COUNT_METRICS)
66 size_t BucketMin(size_t bucket);
67 
68 // Returns the number of tabs corresponding to the maximum value of |bucket|.
69 // |bucket| must be in the interval [0, |kNumTabCountBuckets|).
70 COMPONENT_EXPORT(TAB_COUNT_METRICS)
71 size_t BucketMax(size_t bucket);
72 
73 // Returns true if |num_tabs| falls within |bucket|.
74 // |bucket| must be in the interval [0, |kNumTabCountBuckets|).
75 COMPONENT_EXPORT(TAB_COUNT_METRICS)
76 bool IsInBucket(size_t num_tabs, size_t bucket);
77 
78 }  // namespace internal
79 
80 }  // namespace tab_count_metrics
81 
82 #endif  // COMPONENTS_TAB_COUNT_METRICS_TAB_COUNT_METRICS_H_
83