blob: b48c31c4453c8b144219fde545a755216c6e91f2 [file] [log] [blame]
[email protected]2a172e42014-02-21 04:06:101// Copyright 2014 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#ifndef COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_
6#define COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_
7
[email protected]ccb49262014-03-26 04:10:178#include <map>
[email protected]2a172e42014-02-21 04:06:109#include <string>
10
11#include "base/basictypes.h"
[email protected]ccb49262014-03-26 04:10:1712#include "base/macros.h"
13#include "base/memory/scoped_ptr.h"
[email protected]2a172e42014-02-21 04:06:1014#include "base/timer/timer.h"
holte07637b012014-09-22 19:15:0215#include "components/metrics/daily_event.h"
[email protected]2a172e42014-02-21 04:06:1016
17class PrefRegistrySimple;
[email protected]ccb49262014-03-26 04:10:1718class PrefService;
19
20namespace net {
21class URLRequestContextGetter;
22}
[email protected]2a172e42014-02-21 04:06:1023
24namespace rappor {
25
[email protected]ccb49262014-03-26 04:10:1726class LogUploader;
27class RapporMetric;
28class RapporReports;
29struct RapporParameters;
30
[email protected]2a172e42014-02-21 04:06:1031// The type of data stored in a metric.
32enum RapporType {
[email protected]ccb49262014-03-26 04:10:1733 // For sampling the eTLD+1 of a URL.
[email protected]2a172e42014-02-21 04:06:1034 ETLD_PLUS_ONE_RAPPOR_TYPE = 0,
35 NUM_RAPPOR_TYPES
36};
37
38// This class provides an interface for recording samples for rappor metrics,
39// and periodically generates and uploads reports based on the collected data.
40class RapporService {
41 public:
holte07637b012014-09-22 19:15:0242 // Constructs a RapporService.
43 // Calling code is responsible for ensuring that the lifetime of
44 // |pref_service| is longer than the lifetime of RapporService.
45 explicit RapporService(PrefService* pref_service);
[email protected]2a172e42014-02-21 04:06:1046 virtual ~RapporService();
47
holte07637b012014-09-22 19:15:0248 // Add an observer for collecting daily metrics.
49 void AddDailyObserver(scoped_ptr<metrics::DailyEvent::Observer> observer);
50
[email protected]2a172e42014-02-21 04:06:1051 // Starts the periodic generation of reports and upload attempts.
holte07637b012014-09-22 19:15:0252 void Start(net::URLRequestContextGetter* context,
[email protected]f861eca2014-08-07 20:02:3853 bool metrics_enabled);
[email protected]2a172e42014-02-21 04:06:1054
55 // Records a sample of the rappor metric specified by |metric_name|.
56 // Creates and initializes the metric, if it doesn't yet exist.
57 void RecordSample(const std::string& metric_name,
58 RapporType type,
59 const std::string& sample);
60
holte07637b012014-09-22 19:15:0261 // Sets the cohort value. For use by tests only.
[email protected]2a172e42014-02-21 04:06:1062 void SetCohortForTesting(uint32_t cohort) { cohort_ = cohort; }
63
holte07637b012014-09-22 19:15:0264 // Sets the secret value. For use by tests only.
[email protected]2a172e42014-02-21 04:06:1065 void SetSecretForTesting(const std::string& secret) { secret_ = secret; }
66
67 // Registers the names of all of the preferences used by RapporService in the
68 // provided PrefRegistry. This should be called before calling Start().
69 static void RegisterPrefs(PrefRegistrySimple* registry);
70
71 protected:
holte07637b012014-09-22 19:15:0272 // Retrieves the cohort number this client was assigned to, generating it if
73 // doesn't already exist. The cohort should be persistent.
74 void LoadCohort();
75
76 // Retrieves the value for secret_ from preferences, generating it if doesn't
77 // already exist. The secret should be persistent, so that additional bits
78 // from the client do not get exposed over time.
79 void LoadSecret();
80
[email protected]ccb49262014-03-26 04:10:1781 // Logs all of the collected metrics to the reports proto message and clears
holte07637b012014-09-22 19:15:0282 // the internal map. Exposed for tests. Returns true if any metrics were
[email protected]ccb49262014-03-26 04:10:1783 // recorded.
[email protected]2a172e42014-02-21 04:06:1084 bool ExportMetrics(RapporReports* reports);
85
86 // Records a sample of the rappor metric specified by |parameters|.
87 // Creates and initializes the metric, if it doesn't yet exist.
88 // Exposed for tests.
89 void RecordSampleInternal(const std::string& metric_name,
90 const RapporParameters& parameters,
91 const std::string& sample);
92
93 private:
94 // Check if the service has been started successfully.
95 bool IsInitialized() const;
96
[email protected]2a172e42014-02-21 04:06:1097 // Called whenever the logging interval elapses to generate a new log of
98 // reports and pass it to the uploader.
99 void OnLogInterval();
100
101 // Finds a metric in the metrics_map_, creating it if it doesn't already
102 // exist.
103 RapporMetric* LookUpMetric(const std::string& metric_name,
104 const RapporParameters& parameters);
105
holte07637b012014-09-22 19:15:02106 // A weak pointer to the PrefService used to read and write preferences.
107 PrefService* pref_service_;
108
[email protected]2a172e42014-02-21 04:06:10109 // Client-side secret used to generate fake bits.
110 std::string secret_;
111
112 // The cohort this client is assigned to. -1 is uninitialized.
113 int32_t cohort_;
114
[email protected]ccb49262014-03-26 04:10:17115 // Timer which schedules calls to OnLogInterval().
[email protected]2a172e42014-02-21 04:06:10116 base::OneShotTimer<RapporService> log_rotation_timer_;
117
holte07637b012014-09-22 19:15:02118 // A daily event for collecting metrics once a day.
119 metrics::DailyEvent daily_event_;
120
[email protected]2a172e42014-02-21 04:06:10121 // A private LogUploader instance for sending reports to the server.
122 scoped_ptr<LogUploader> uploader_;
123
124 // We keep all registered metrics in a map, from name to metric.
125 // The map owns the metrics it contains.
126 std::map<std::string, RapporMetric*> metrics_map_;
127
128 DISALLOW_COPY_AND_ASSIGN(RapporService);
129};
130
131} // namespace rappor
132
133#endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_