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 | |
10 #include "base/basictypes.h" | |
11 #include "base/lazy_instance.h" | |
Ilya Sherman
2014/02/13 01:39:03
nit: This doesn't seem like it's used.
Steven Holte
2014/02/13 05:11:12
Done.
| |
12 #include "base/memory/weak_ptr.h" | |
Ilya Sherman
2014/02/13 01:39:03
nit: This doesn't seem like it's used.
Steven Holte
2014/02/13 05:11:12
Done.
| |
13 #include "base/prefs/pref_service.h" | |
14 #include "base/time/time.h" | |
15 #include "base/timer/timer.h" | |
16 #include "components/rappor/log_uploader.h" | |
17 #include "components/rappor/proto/rappor_metric.pb.h" | |
18 #include "components/rappor/rappor_metric.h" | |
19 | |
20 class PrefRegistrySimple; | |
21 | |
22 namespace rappor { | |
23 | |
24 // The type of data stored in a metric. | |
25 enum RapporType { | |
26 ETLD_PLUS_ONE_RAPPOR_TYPE = 0, | |
27 NUM_RAPPOR_TYPES | |
28 }; | |
29 | |
30 // This class provides an interface for recording samples for rappor metrics, | |
31 // and periodically generates and uploads reports based on the collected data. | |
32 class RapporService { | |
33 public: | |
34 RapporService(); | |
35 virtual ~RapporService(); | |
36 | |
37 // Starts the periodic generation of reports and upload attempts. | |
38 void Start(PrefService* pref_service, net::URLRequestContextGetter* context); | |
39 | |
40 // Records a sample of the rappor metric specified by |metric_name|. | |
41 // Creates and initializes the metric, if it doesn't yet exist. | |
42 void RecordSample(const std::string& metric_name, | |
43 RapporType type, | |
44 const std::string& sample); | |
45 | |
46 // Sets the cohort value. For use by tests only. | |
47 void SetCohortForTesting(uint32_t cohort) { cohort_ = cohort; } | |
48 | |
49 // Sets the secret value. For use by tests only. | |
50 void SetSecretForTesting(std::string secret) { secret_ = secret; } | |
Ilya Sherman
2014/02/13 01:39:03
nit: Pass by const-reference.
Ilya Sherman
2014/02/13 01:39:03
Optional nit: I prefer to restrict visibility of f
Steven Holte
2014/02/13 05:11:12
Leaving it public.
Steven Holte
2014/02/13 05:11:12
Done.
| |
51 | |
52 // Registers the names of all of the preferences used by RapporService in the | |
53 // provided PrefRegistry. This should be called before calling Start(). | |
54 static void RegisterPrefs(PrefRegistrySimple* registry); | |
55 | |
56 protected: | |
57 // Logs all of the collected metrics to the reports proto message. Exposed | |
58 // for tests. Returns true if any metrics were recorded. | |
59 bool ExportMetrics(RapporReports* reports); | |
Ilya Sherman
2014/02/13 01:39:03
nit: Where is the "RapporReports" type declared?
Steven Holte
2014/02/13 05:11:12
It's the proto/rappor_metric.pb.h type.
| |
60 | |
61 // Records a sample of the rappor metric specified by |parameters|. | |
62 // Creates and initializes the metric, if it doesn't yet exist. | |
Ilya Sherman
2014/02/13 01:39:03
nit: Exposed for tests?
Steven Holte
2014/02/13 05:11:12
Done.
| |
63 void RecordSampleInternal(const std::string& metric_name, | |
64 const RapporParameters& parameters, | |
65 const std::string& sample); | |
66 | |
67 private: | |
68 // Check if the service has been started successfully. | |
69 bool IsInitialized() const; | |
70 | |
71 // Retrieves the cohort number this client was assigned to, generating it if | |
72 // doesn't already exist. The cohort should be persistent. | |
73 void LoadCohort(PrefService* pref_service); | |
74 | |
75 // Retrieves the value for secret_ from preferences, generating it if doesn't | |
76 // already exist. The secret should be persistent, so that additional bits | |
77 // from the client do not get exposed over time. | |
78 void LoadSecret(PrefService* pref_service); | |
79 | |
80 // Called whenever the logging interval elapses to generate a new log of | |
81 // reports and pass it to the uploader. | |
82 void OnLogInterval(); | |
83 | |
84 // Finds a metric in the metrics_map_, creating it if it doesn't already | |
85 // exist. | |
86 RapporMetric* LookupMetric(const std::string& metric_name, | |
Ilya Sherman
2014/02/13 01:39:03
nit: Lookup -> LookUp (noun vs. verb)
Steven Holte
2014/02/13 05:11:12
Done.
| |
87 const RapporParameters& parameters); | |
88 | |
89 // Client-side secret used to generate fake bits. | |
90 std::string secret_; | |
91 | |
92 // The cohort this client is assigned to. -1 is uninitialized. | |
93 int32_t cohort_; | |
94 | |
95 // Timer which schedules calls to OnLogInterval() | |
96 base::OneShotTimer<RapporService> log_rotation_timer_; | |
97 | |
98 // A private LogUploader instance for sending reports to the server. | |
99 scoped_ptr<LogUploader> uploader_; | |
Ilya Sherman
2014/02/13 01:39:03
nit: Can this be a direct class member, rather tha
Steven Holte
2014/02/13 05:11:12
No, it's constructor requires the request_context
| |
100 | |
101 // We keep all registered histograms in a map, from name to histogram. | |
Ilya Sherman
2014/02/13 01:39:03
nit: "histogram" -> "metric"?
Steven Holte
2014/02/13 05:11:12
Done.
| |
102 std::map<std::string, RapporMetric*> metrics_map_; | |
Ilya Sherman
2014/02/13 01:39:03
Please document that the map owns the metrics it c
Steven Holte
2014/02/13 05:11:12
Done.
| |
103 | |
104 // Lock protects access to above map. | |
105 base::Lock lock_; | |
Ilya Sherman
2014/02/13 01:39:03
IMPORTANT: Hmm, why does the map need a lock? Thi
Steven Holte
2014/02/13 05:11:12
I'm not sure what thread the Timer executes it's c
Ilya Sherman
2014/02/13 23:23:08
The Timer runs on whatever thread it's instantiate
| |
106 | |
107 DISALLOW_COPY_AND_ASSIGN(RapporService); | |
108 }; | |
109 | |
110 } // namespace rappor | |
111 | |
112 #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ | |
OLD | NEW |