aw_metrics_service_client.h 3.45 KB
Newer Older
1
// Copyright 2017 The Chromium Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

paulmiller's avatar
paulmiller committed
5 6
#ifndef ANDROID_WEBVIEW_BROWSER_AW_METRICS_SERVICE_CLIENT_H_
#define ANDROID_WEBVIEW_BROWSER_AW_METRICS_SERVICE_CLIENT_H_
7

8 9 10 11 12
#include <memory>
#include <string>

#include "base/lazy_instance.h"
#include "base/macros.h"
13
#include "components/metrics/enabled_state_provider.h"
14
#include "components/metrics/metrics_log_uploader.h"
15
#include "components/metrics/metrics_service_client.h"
paulmiller's avatar
paulmiller committed
16
#include "components/version_info/channel.h"
17 18 19

class PrefService;

20 21 22 23
namespace base {
class FilePath;
}

24 25 26 27
namespace net {
class URLRequestContextGetter;
}

28 29 30 31
namespace metrics {
class MetricsStateManager;
}

32 33
namespace android_webview {

34 35 36 37 38 39
// This singleton manages metrics for an app using any number of WebViews. It
// must always be used on the same thread. (Currently the UI thread is enforced,
// but it could be any thread.) This is to prevent enable/disable race
// conditions, and because MetricsService is single-threaded. Initialization is
// asynchronous; even after Initialize has returned, some methods may not be
// ready to use (see below).
40 41
class AwMetricsServiceClient : public metrics::MetricsServiceClient,
                               public metrics::EnabledStateProvider {
42 43
  friend struct base::LazyInstanceTraitsBase<AwMetricsServiceClient>;

44 45
 public:
  static AwMetricsServiceClient* GetInstance();
Kyle Milka's avatar
Kyle Milka committed
46

47
  // Retrieve the client ID or generate one if none exists.
48 49 50 51
  static void LoadOrCreateClientId();

  // Return the cached client id.
  static std::string GetClientId();
Kyle Milka's avatar
Kyle Milka committed
52

53
  void Initialize(PrefService* pref_service,
Kyle Milka's avatar
Kyle Milka committed
54
                  net::URLRequestContextGetter* request_context);
55 56 57

  // metrics::EnabledStateProvider implementation
  bool IsConsentGiven() override;
58
  bool IsReportingEnabled() override;
59 60 61 62

  // The below functions must not be called until initialization has
  // asynchronously finished.

63
  void SetHaveMetricsConsent(bool consent);
64

65 66 67 68 69 70 71 72 73 74 75
  // metrics::MetricsServiceClient implementation
  metrics::MetricsService* GetMetricsService() override;
  void SetMetricsClientId(const std::string& client_id) override;
  int32_t GetProduct() override;
  std::string GetApplicationLocale() override;
  bool GetBrand(std::string* brand_code) override;
  metrics::SystemProfileProto::Channel GetChannel() override;
  std::string GetVersionString() override;
  void CollectFinalMetricsForLog(const base::Closure& done_callback) override;
  std::unique_ptr<metrics::MetricsLogUploader> CreateUploader(
      base::StringPiece server_url,
76
      base::StringPiece insecure_server_url,
77 78 79 80 81 82 83
      base::StringPiece mime_type,
      metrics::MetricsLogUploader::MetricServiceType service_type,
      const metrics::MetricsLogUploader::UploadCallback& on_upload_complete)
      override;
  base::TimeDelta GetStandardUploadInterval() override;

 private:
84 85 86
  AwMetricsServiceClient();
  ~AwMetricsServiceClient() override;

87
  void InitializeWithClientId();
88 89 90

  std::unique_ptr<metrics::MetricsStateManager> metrics_state_manager_;
  std::unique_ptr<metrics::MetricsService> metrics_service_;
91 92
  PrefService* pref_service_;
  net::URLRequestContextGetter* request_context_;
paulmiller's avatar
paulmiller committed
93
  version_info::Channel channel_;
94 95
  bool consent_;    // = (user has consented) && !(app has opted out)
  bool in_sample_;  // Is this client enabled by sampling?
96

97 98 99 100 101
  DISALLOW_COPY_AND_ASSIGN(AwMetricsServiceClient);
};

}  // namespace android_webview

paulmiller's avatar
paulmiller committed
102
#endif  // ANDROID_WEBVIEW_BROWSER_AW_METRICS_SERVICE_CLIENT_H_