OLD | NEW |
---|---|
(Empty) | |
1 // 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 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/lazy_instance.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "base/prefs/pref_service.h" | |
15 #include "base/time/time.h" | |
16 #include "base/timer/timer.h" | |
17 #include "components/rappor/log_uploader.h" | |
18 #include "components/rappor/proto/rappor_metric.pb.h" | |
19 #include "components/rappor/rappor_metric.h" | |
20 #include "url/gurl.h" | |
21 | |
22 class PrefRegistrySimple; | |
23 | |
24 namespace rappor { | |
25 | |
26 // This class provides an interface for recording samples for rappor metrics, | |
27 // and periodically generates and uploads reports based on the collected data. | |
28 class RapporService { | |
29 public: | |
30 RapporService(); | |
31 virtual ~RapporService(); | |
32 | |
33 // Starts the periodic generation of reports and upload attempts. | |
34 void Start(PrefService* pref_service, net::URLRequestContextGetter* context); | |
35 | |
36 // Registers the names of all of the preferences used by RapporService in the | |
37 // provided PrefRegistry. This should be called before calling Start(). | |
38 static void RegisterPrefs(PrefRegistrySimple* registry); | |
39 | |
40 // Records a set of samples on the rappor metric specified by |parameters|. | |
41 // Creates and initializes the metric, if it doesn't yet exist. | |
42 void RecordSamples(const RapporParameters& parameters, | |
43 const std::vector<std::string>& samples); | |
44 | |
45 // Utility method for recording a URL. Breaks the URL up into parts and | |
46 // records samples for each of them. | |
47 void RecordUrl(const RapporParameters& parameters, const GURL& url); | |
48 | |
49 protected: | |
50 // Logs all of the collected metrics to the reports proto message. Exposed | |
51 // for tests. Returns true if any metrics were recorded. | |
52 bool LogMetrics(RapporReports* reports); | |
53 | |
54 // Records a set of samples on the rappor metric specified by |parameters|. | |
55 // Creates and initializes the metric, if it doesn't yet exist. | |
56 void RecordSamplesInternal(const RapporParameters& parameters, | |
57 const std::vector<std::string>& samples); | |
58 | |
59 // The cohort this client is assigned to. -1 is uninitialized. | |
60 int32_t cohort_; | |
61 private: | |
62 // Retrieves the cohort number this client was assigned to, generating it if | |
63 // doesn't already exist. The cohort should be persistent. | |
64 void LoadCohort(PrefService* pref_service); | |
65 | |
66 // Retrieves the value for secret_ from preferences, generating it if doesn't | |
67 // already exist. The secret should be persistent, so that additional bits | |
68 // from the client do not get exposed over time. | |
69 void LoadSecret(PrefService* pref_service); | |
70 | |
71 // Logs a single metric's output to the report message. | |
72 void LogMetric(const RapporMetric& metric, RapporReports::Report* report); | |
73 | |
74 // Called whenever the logging interval elapses to generate a new log of | |
75 // reports and pass it to the uploader. | |
76 void OnLogInterval(); | |
77 | |
78 // Finds a metric in the metrics_map_, creating it if it doesn't already | |
Alexei Svitkine (slow)
2014/01/24 22:09:03
Nit: When mentioning variable names in comments, e
| |
79 // exist. | |
80 RapporMetric* LookupMetric(const RapporParameters& parameters); | |
Alexei Svitkine (slow)
2014/01/24 22:09:03
Nit: LookUpMetric
| |
81 | |
82 // Client-side secret used to generate fake bits. | |
83 std::string secret_; | |
84 | |
85 // Timer which schedules calls to OnLogInterval() | |
86 base::OneShotTimer<RapporService> log_rotation_timer_; | |
87 | |
88 // A private LogUploader instance for sending reports to the server. | |
89 scoped_ptr<LogUploader> uploader_; | |
90 | |
91 // We keep all registered histograms in a map, from name to histogram. | |
92 std::map<std::string, RapporMetric*> metrics_map_; | |
93 | |
94 // Lock protects access to above map. | |
95 base::Lock lock_; | |
96 | |
97 DISALLOW_COPY_AND_ASSIGN(RapporService); | |
98 }; | |
99 | |
100 } // namespace rappor | |
101 | |
102 #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ | |
OLD | NEW |