| // Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef POWER_MANAGER_COMMON_METRICS_SENDER_H_ |
| #define POWER_MANAGER_COMMON_METRICS_SENDER_H_ |
| |
| #include <memory> |
| #include <string> |
| |
| #include <base/compiler_specific.h> |
| #include <base/macros.h> |
| |
| class MetricsLibraryInterface; |
| |
| namespace power_manager { |
| |
| // Stubbable interface for sending metrics. |
| class MetricsSenderInterface { |
| public: |
| // Returns the currently-registered singleton. The return value may be NULL |
| // (e.g. during testing). |
| static MetricsSenderInterface* GetInstance(); |
| |
| // Registers |instance| as the current singleton. Another instance must not |
| // already be registered. Ownership of |instance| remains with the caller. |
| static void SetInstance(MetricsSenderInterface* instance); |
| |
| virtual ~MetricsSenderInterface() {} |
| |
| // See MetricsLibrary::SendToUMA in metrics/metrics_library.h for a |
| // description of the arguments in the below methods. |
| |
| // Sends a regular (exponential) histogram sample. |
| // |
| // There are various constraints on values (see base/metrics/histogram.h in |
| // Chrome), including: |
| // |
| // * 1 <= |min| < |max| < base::HistogramBase::kSampleType_MAX |
| // * |num_buckets| < base::Histogram::kBucketCount_MAX |
| // * |num_buckets| <= |max| - |min| + 2 |
| // |
| // Violating these constraints may result in Chrome silently discarding the |
| // simple rather than reporting. |
| virtual bool SendMetric(const std::string& name, |
| int sample, |
| int min, |
| int max, |
| int num_buckets) = 0; |
| |
| // Sends an enumeration (linear) histogram sample. |
| virtual bool SendEnumMetric(const std::string& name, int sample, int max) = 0; |
| }; |
| |
| // MetricsSenderInterface implementation that wraps the metrics library and |
| // actually forwards metrics to Chrome. |
| class MetricsSender : public MetricsSenderInterface { |
| public: |
| // The c'tor and d'tor call SetInstance() to register and unregister this |
| // instance. |
| explicit MetricsSender(std::unique_ptr<MetricsLibraryInterface> metrics_lib); |
| MetricsSender(const MetricsSender&) = delete; |
| MetricsSender& operator=(const MetricsSender&) = delete; |
| |
| ~MetricsSender() override; |
| |
| // MetricsSenderInterface implementation: |
| bool SendMetric(const std::string& name, |
| int sample, |
| int min, |
| int max, |
| int num_buckets) override; |
| bool SendEnumMetric(const std::string& name, int sample, int max) override; |
| |
| private: |
| std::unique_ptr<MetricsLibraryInterface> metrics_lib_; |
| }; |
| |
| // Convenience wrapper for calling SendMetric() on the currently-registered |
| // MetricsSenderInterface singleton. Returns true without doing anything if no |
| // singleton is currently registered (e.g. for testing). |
| bool SendMetric( |
| const std::string& name, int sample, int min, int max, int num_buckets); |
| |
| // Convenience wrapper for calling SendEnumMetric() on the currently-registered |
| // MetricsSenderInterface singleton. Returns true without doing anything if no |
| // singleton is currently registered (e.g. for testing). |
| bool SendEnumMetric(const std::string& name, int sample, int max); |
| |
| } // namespace power_manager |
| |
| #endif // POWER_MANAGER_COMMON_METRICS_SENDER_H_ |