1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_glean_GleanDenominator_h
8 #define mozilla_glean_GleanDenominator_h
9 
10 #include "mozilla/Maybe.h"
11 #include "mozilla/Result.h"
12 #include "nsIGleanMetrics.h"
13 #include "nsString.h"
14 
15 namespace mozilla::glean {
16 
17 namespace impl {
18 
19 class DenominatorMetric {
20  public:
DenominatorMetric(uint32_t aId)21   constexpr explicit DenominatorMetric(uint32_t aId) : mId(aId) {}
22 
23   /*
24    * Increases the counter by `amount`.
25    *
26    * @param aAmount The amount to increase by. Should be positive.
27    */
28   void Add(int32_t aAmount = 1) const;
29 
30   /**
31    * **Test-only API**
32    *
33    * Gets the currently stored value as an integer.
34    *
35    * This function will attempt to await the last parent-process task (if any)
36    * writing to the the metric's storage engine before returning a value.
37    * This function will not wait for data from child processes.
38    *
39    * This doesn't clear the stored value.
40    * Parent process only. Panics in child processes.
41    *
42    * @param aPingName The (optional) name of the ping to retrieve the metric
43    *        for. Defaults to the first value in `send_in_pings`.
44    *
45    * @return value of the stored metric, or Nothing() if there is no value.
46    */
47   Result<Maybe<int32_t>, nsCString> TestGetValue(
48       const nsACString& aPingName = nsCString()) const;
49 
50  private:
51   const uint32_t mId;
52 };
53 }  // namespace impl
54 
55 class GleanDenominator final : public nsIGleanDenominator {
56  public:
57   NS_DECL_ISUPPORTS
58   NS_DECL_NSIGLEANDENOMINATOR
59 
GleanDenominator(uint32_t id)60   explicit GleanDenominator(uint32_t id) : mDenominator(id){};
61 
62  private:
63   virtual ~GleanDenominator() = default;
64 
65   const impl::DenominatorMetric mDenominator;
66 };
67 
68 }  // namespace mozilla::glean
69 
70 #endif /* mozilla_glean_GleanDenominator_h */
71