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