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