blob: 5f48d79cd2bfb25432fe7e2df4dc613b0a810dda [file] [log] [blame]
holtea3b24112017-03-14 02:08:241// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This file defines a service that sends metrics logs to a server.
6
7#ifndef COMPONENTS_METRICS_REPORTING_SERVICE_H_
8#define COMPONENTS_METRICS_REPORTING_SERVICE_H_
9
10#include <stdint.h>
11
12#include <string>
13
14#include "base/macros.h"
15#include "base/time/time.h"
16#include "build/build_config.h"
17#include "components/metrics/data_use_tracker.h"
18#include "components/metrics/metrics_log_uploader.h"
Steven Holtef9d5ed62017-10-21 02:02:3019#include "third_party/metrics_proto/reporting_info.pb.h"
holtea3b24112017-03-14 02:08:2420
21class PrefService;
22class PrefRegistrySimple;
23
24namespace metrics {
25
26class LogStore;
27class MetricsUploadScheduler;
28class MetricsServiceClient;
29
30// ReportingService is an abstract class which uploads serialized logs from a
31// LogStore to a remote server. A concrete implementation of this class must
32// provide the specific LogStore and parameters for the MetricsLogUploader, and
33// can also implement hooks to record histograms based on certain events that
34// occur while attempting to upload logs.
35class ReportingService {
36 public:
37 // Creates a ReportingService with the given |client|, |local_state|, and
38 // |max_retransmit_size|. Does not take ownership of the parameters; instead
39 // it stores a weak pointer to each. Caller should ensure that the parameters
40 // are valid for the lifetime of this class.
41 ReportingService(MetricsServiceClient* client,
42 PrefService* local_state,
43 size_t max_retransmit_size);
44 virtual ~ReportingService();
45
46 // Completes setup tasks that can't be done at construction time.
47 // Loads persisted logs and creates the MetricsUploadScheduler.
48 void Initialize();
49
50 // Starts the metrics reporting system.
51 // Should be called when metrics enabled or new logs are created.
52 // When the service is already running, this is a safe no-op.
53 void Start();
54
55 // Shuts down the metrics system. Should be called at shutdown, or if metrics
56 // are turned off.
57 void Stop();
58
59 // Enable/disable transmission of accumulated logs and crash reports (dumps).
60 // Calling Start() automatically enables reporting, but sending is
61 // asyncronous so this can be called immediately after Start() to prevent
62 // any uploading.
63 void EnableReporting();
64 void DisableReporting();
65
66 // True iff reporting is currently enabled.
67 bool reporting_active() const;
68
69 // Updates data usage tracking prefs with the specified values.
70 void UpdateMetricsUsagePrefs(const std::string& service_name,
71 int message_size,
72 bool is_cellular);
73
74 // Registers local state prefs used by this class. This should only be called
75 // once.
76 static void RegisterPrefs(PrefRegistrySimple* registry);
77
78 protected:
79 MetricsServiceClient* client() const { return client_; };
80
81 private:
82 // Retrieves the log store backing this service.
83 virtual LogStore* log_store() = 0;
84
85 // Getters for MetricsLogUploader parameters.
86 virtual std::string GetUploadUrl() const = 0;
87 virtual base::StringPiece upload_mime_type() const = 0;
88 virtual MetricsLogUploader::MetricServiceType service_type() const = 0;
89
90 // Methods for recording data to histograms.
91 virtual void LogActualUploadInterval(base::TimeDelta interval) {}
92 virtual void LogCellularConstraint(bool upload_canceled) {}
holte035ec7fb2017-04-04 20:16:5993 virtual void LogResponseOrErrorCode(int response_code, int error_code) {}
holtea3b24112017-03-14 02:08:2494 virtual void LogSuccess(size_t log_size) {}
95 virtual void LogLargeRejection(size_t log_size) {}
96
97 // If recording is enabled, begins uploading the next completed log from
98 // the log manager, staging it if necessary.
99 void SendNextLog();
100
101 // Uploads the currently staged log (which must be non-null).
102 void SendStagedLog();
103
104 // Called after transmission completes (either successfully or with failure).
holte035ec7fb2017-04-04 20:16:59105 void OnLogUploadComplete(int response_code, int error_code);
holtea3b24112017-03-14 02:08:24106
107 // Used to interact with the embedder. Weak pointer; must outlive |this|
108 // instance.
109 MetricsServiceClient* const client_;
110
111 // Largest log size to attempt to retransmit.
112 size_t max_retransmit_size_;
113
114 // Indicate whether recording and reporting are currently happening.
115 // These should not be set directly, but by calling SetRecording and
116 // SetReporting.
117 bool reporting_active_;
118
119 // Instance of the helper class for uploading logs.
120 std::unique_ptr<MetricsLogUploader> log_uploader_;
121
122 // Whether there is a current log upload in progress.
123 bool log_upload_in_progress_;
124
125 // The scheduler for determining when uploads should happen.
126 std::unique_ptr<MetricsUploadScheduler> upload_scheduler_;
127
128 // Pointer used for obtaining data use pref updater callback on above layers.
129 std::unique_ptr<DataUseTracker> data_use_tracker_;
130
131 // The tick count of the last time log upload has been finished and null if no
132 // upload has been done yet.
133 base::TimeTicks last_upload_finish_time_;
134
Steven Holte72428db2017-10-13 19:47:22135 // Info on current reporting state to send along with reports.
136 ReportingInfo reporting_info_;
137
Steven Holte971b0592017-10-12 15:28:26138 SEQUENCE_CHECKER(sequence_checker_);
holtea3b24112017-03-14 02:08:24139
140 // Weak pointers factory used to post task on different threads. All weak
141 // pointers managed by this factory have the same lifetime as
142 // ReportingService.
143 base::WeakPtrFactory<ReportingService> self_ptr_factory_;
144
145 DISALLOW_COPY_AND_ASSIGN(ReportingService);
146};
147
148} // namespace metrics
149
150#endif // COMPONENTS_METRICS_REPORTING_SERVICE_H_