xref: /aosp_15_r20/external/cronet/components/metrics/test/test_metrics_service_client.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 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 #ifndef COMPONENTS_METRICS_TEST_TEST_METRICS_SERVICE_CLIENT_H_
6 #define COMPONENTS_METRICS_TEST_TEST_METRICS_SERVICE_CLIENT_H_
7 
8 #include <stdint.h>
9 
10 #include <set>
11 #include <string>
12 
13 #include "base/memory/raw_ptr.h"
14 #include "components/metrics/metrics_log_store.h"
15 #include "components/metrics/metrics_log_uploader.h"
16 #include "components/metrics/metrics_service_client.h"
17 #include "components/metrics/test/test_metrics_log_uploader.h"
18 
19 namespace variations {
20 class SyntheticTrialRegistry;
21 }
22 
23 namespace metrics {
24 
25 // A simple concrete implementation of the MetricsServiceClient interface, for
26 // use in tests.
27 class TestMetricsServiceClient : public MetricsServiceClient {
28  public:
29   static const char kBrandForTesting[];
30 
31   TestMetricsServiceClient();
32   TestMetricsServiceClient(const TestMetricsServiceClient&) = delete;
33   TestMetricsServiceClient& operator=(const TestMetricsServiceClient&) = delete;
34   ~TestMetricsServiceClient() override;
35 
36   // MetricsServiceClient:
37   variations::SyntheticTrialRegistry* GetSyntheticTrialRegistry() override;
38   metrics::MetricsService* GetMetricsService() override;
39   void SetMetricsClientId(const std::string& client_id) override;
40   bool ShouldUploadMetricsForUserId(uint64_t user_id) override;
41   int32_t GetProduct() override;
42   std::string GetApplicationLocale() override;
43   const network_time::NetworkTimeTracker* GetNetworkTimeTracker() override;
44   bool GetBrand(std::string* brand_code) override;
45   SystemProfileProto::Channel GetChannel() override;
46   bool IsExtendedStableChannel() override;
47   std::string GetVersionString() override;
48   void CollectFinalMetricsForLog(base::OnceClosure done_callback) override;
49   std::unique_ptr<MetricsLogUploader> CreateUploader(
50       const GURL& server_url,
51       const GURL& insecure_server_url,
52       base::StringPiece mime_type,
53       MetricsLogUploader::MetricServiceType service_type,
54       const MetricsLogUploader::UploadCallback& on_upload_complete) override;
55   base::TimeDelta GetStandardUploadInterval() override;
56   bool IsReportingPolicyManaged() override;
57   EnableMetricsDefault GetMetricsReportingDefaultState() override;
58   std::string GetAppPackageNameIfLoggable() override;
59   bool ShouldResetClientIdsOnClonedInstall() override;
60   MetricsLogStore::StorageLimits GetStorageLimits() const override;
61 
62   // Adds/removes |user_id| from the set of user ids that have metrics consent
63   // as true.
64   void AllowMetricUploadForUserId(uint64_t user_id);
65   void RemoveMetricUploadForUserId(uint64_t user_id);
66 
get_client_id()67   const std::string& get_client_id() const { return client_id_; }
68   // Returns a weak ref to the last created uploader.
uploader()69   TestMetricsLogUploader* uploader() { return uploader_.get(); }
set_version_string(const std::string & str)70   void set_version_string(const std::string& str) { version_string_ = str; }
set_product(int32_t product)71   void set_product(int32_t product) { product_ = product; }
set_reporting_is_managed(bool managed)72   void set_reporting_is_managed(bool managed) {
73     reporting_is_managed_ = managed;
74   }
set_is_extended_stable_channel(bool is_extended_stable_channel)75   void set_is_extended_stable_channel(bool is_extended_stable_channel) {
76     is_extended_stable_channel_ = is_extended_stable_channel;
77   }
set_enable_default(EnableMetricsDefault enable_default)78   void set_enable_default(EnableMetricsDefault enable_default) {
79     enable_default_ = enable_default;
80   }
set_should_reset_client_ids_on_cloned_install(bool state)81   void set_should_reset_client_ids_on_cloned_install(bool state) {
82     should_reset_client_ids_on_cloned_install_ = state;
83   }
set_max_ongoing_log_size_bytes(size_t bytes)84   void set_max_ongoing_log_size_bytes(size_t bytes) {
85     storage_limits_.ongoing_log_queue_limits.max_log_size_bytes = bytes;
86   }
set_min_ongoing_log_queue_count(size_t log_count)87   void set_min_ongoing_log_queue_count(size_t log_count) {
88     storage_limits_.ongoing_log_queue_limits.min_log_count = log_count;
89   }
set_min_ongoing_log_queue_size_bytes(size_t bytes)90   void set_min_ongoing_log_queue_size_bytes(size_t bytes) {
91     storage_limits_.ongoing_log_queue_limits.min_queue_size_bytes = bytes;
92   }
set_synthetic_trial_registry(variations::SyntheticTrialRegistry * registry)93   void set_synthetic_trial_registry(
94       variations::SyntheticTrialRegistry* registry) {
95     synthetic_trial_registry_ = registry;
96   }
97 
98  private:
99   std::string client_id_{"0a94430b-18e5-43c8-a657-580f7e855ce1"};
100   std::string version_string_{"5.0.322.0-64-devel"};
101   int32_t product_ = ChromeUserMetricsExtension::CHROME;
102   bool reporting_is_managed_ = false;
103   bool is_extended_stable_channel_ = false;
104   EnableMetricsDefault enable_default_ = EnableMetricsDefault::DEFAULT_UNKNOWN;
105   bool should_reset_client_ids_on_cloned_install_ = false;
106   MetricsLogStore::StorageLimits storage_limits_ =
107       MetricsServiceClient::GetStorageLimits();
108   std::set<uint64_t> allowed_user_ids_;
109 
110   raw_ptr<variations::SyntheticTrialRegistry> synthetic_trial_registry_ =
111       nullptr;
112 
113   // A weak ref to the last created TestMetricsLogUploader.
114   base::WeakPtr<TestMetricsLogUploader> uploader_ = nullptr;
115 };
116 
117 }  // namespace metrics
118 
119 #endif  // COMPONENTS_METRICS_TEST_TEST_METRICS_SERVICE_CLIENT_H_
120