OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ | 5 #ifndef COMPONENTS_RAPPOR_RAPPOR_SERVICE_IMPL_H_ |
6 #define COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ | 6 #define COMPONENTS_RAPPOR_RAPPOR_SERVICE_IMPL_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
11 #include <memory> | 11 #include <memory> |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/weak_ptr.h" | |
17 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
18 #include "base/timer/timer.h" | 17 #include "base/timer/timer.h" |
19 #include "components/metrics/daily_event.h" | 18 #include "components/metrics/daily_event.h" |
20 #include "components/rappor/rappor_parameters.h" | 19 #include "components/rappor/public/rappor_parameters.h" |
21 #include "components/rappor/sample.h" | 20 #include "components/rappor/public/rappor_service.h" |
| 21 #include "components/rappor/public/sample.h" |
22 #include "components/rappor/sampler.h" | 22 #include "components/rappor/sampler.h" |
23 | 23 |
24 class PrefRegistrySimple; | 24 class PrefRegistrySimple; |
25 class PrefService; | 25 class PrefService; |
26 | 26 |
27 namespace net { | 27 namespace net { |
28 class URLRequestContextGetter; | 28 class URLRequestContextGetter; |
29 } | 29 } |
30 | 30 |
31 namespace rappor { | 31 namespace rappor { |
32 | 32 |
33 class LogUploaderInterface; | 33 class LogUploaderInterface; |
34 class RapporMetric; | 34 class RapporMetric; |
35 class RapporReports; | 35 class RapporReports; |
36 | 36 |
37 // This class provides an interface for recording samples for rappor metrics, | 37 // This class provides an interface for recording samples for rappor metrics, |
38 // and periodically generates and uploads reports based on the collected data. | 38 // and periodically generates and uploads reports based on the collected data. |
39 class RapporService : public base::SupportsWeakPtr<RapporService> { | 39 class RapporServiceImpl : public RapporService { |
40 public: | 40 public: |
41 // Constructs a RapporService. | 41 // Constructs a RapporServiceImpl. |
42 // Calling code is responsible for ensuring that the lifetime of | 42 // Calling code is responsible for ensuring that the lifetime of |
43 // |pref_service| is longer than the lifetime of RapporService. | 43 // |pref_service| is longer than the lifetime of RapporServiceImpl. |
44 // |is_incognito_callback| will be called to test if incognito mode is active. | 44 // |is_incognito_callback| will be called to test if incognito mode is active. |
45 RapporService(PrefService* pref_service, | 45 RapporServiceImpl(PrefService* pref_service, |
46 const base::Callback<bool(void)> is_incognito_callback); | 46 const base::Callback<bool(void)> is_incognito_callback); |
47 virtual ~RapporService(); | 47 virtual ~RapporServiceImpl(); |
48 | 48 |
49 // Add an observer for collecting daily metrics. | 49 // Add an observer for collecting daily metrics. |
50 void AddDailyObserver( | 50 void AddDailyObserver( |
51 std::unique_ptr<metrics::DailyEvent::Observer> observer); | 51 std::unique_ptr<metrics::DailyEvent::Observer> observer); |
52 | 52 |
53 // Initializes the rappor service, including loading the cohort and secret | 53 // Initializes the rappor service, including loading the cohort and secret |
54 // preferences from disk. | 54 // preferences from disk. |
55 void Initialize(net::URLRequestContextGetter* context); | 55 void Initialize(net::URLRequestContextGetter* context); |
56 | 56 |
57 // Updates the settings for metric recording and uploading. | 57 // Updates the settings for metric recording and uploading. |
58 // The RapporService must be initialized before this method is called. | 58 // The RapporServiceImpl must be initialized before this method is called. |
59 // |recording_groups| should be set of flags, e.g. | 59 // |recording_groups| should be set of flags, e.g. |
60 // UMA_RECORDING_GROUP | SAFEBROWSING_RECORDING_GROUP | 60 // UMA_RECORDING_GROUP | SAFEBROWSING_RECORDING_GROUP |
61 // If it contains any enabled groups, periodic reports will be | 61 // If it contains any enabled groups, periodic reports will be |
62 // generated and queued for upload. | 62 // generated and queued for upload. |
63 // If |may_upload| is true, reports will be uploaded from the queue. | 63 // If |may_upload| is true, reports will be uploaded from the queue. |
64 void Update(int recording_groups, bool may_upload); | 64 void Update(int recording_groups, bool may_upload); |
65 | 65 |
66 // Constructs a Sample object for the caller to record fields in. | 66 // Constructs a Sample object for the caller to record fields in. |
67 virtual std::unique_ptr<Sample> CreateSample(RapporType); | 67 std::unique_ptr<Sample> CreateSample(RapporType) override; |
68 | 68 |
69 // Records a Sample of rappor metric specified by |metric_name|. | 69 // Records a Sample of rappor metric specified by |metric_name|. |
70 // | 70 // |
71 // TODO(holte): Rename RecordSample to RecordString and then rename this | |
72 // to RecordSample. | |
73 // | |
74 // example: | 71 // example: |
75 // std::unique_ptr<Sample> sample = | 72 // std::unique_ptr<Sample> sample = |
76 // rappor_service->CreateSample(MY_METRIC_TYPE); | 73 // rappor_service->CreateSample(MY_METRIC_TYPE); |
77 // sample->SetStringField("Field1", "some string"); | 74 // sample->SetStringField("Field1", "some string"); |
78 // sample->SetFlagsValue("Field2", SOME|FLAGS); | 75 // sample->SetFlagsValue("Field2", SOME|FLAGS); |
79 // rappor_service->RecordSample("MyMetric", std::move(sample)); | 76 // rappor_service->RecordSample("MyMetric", std::move(sample)); |
80 // | 77 // |
81 // This will result in a report setting two metrics "MyMetric.Field1" and | 78 // This will result in a report setting two metrics "MyMetric.Field1" and |
82 // "MyMetric.Field2", and they will both be generated from the same sample, | 79 // "MyMetric.Field2", and they will both be generated from the same sample, |
83 // to allow for correlations to be computed. | 80 // to allow for correlations to be computed. |
84 virtual void RecordSampleObj(const std::string& metric_name, | 81 void RecordSample(const std::string& metric_name, |
85 std::unique_ptr<Sample> sample); | 82 std::unique_ptr<Sample> sample) override; |
86 | 83 |
87 // Records a sample of the rappor metric specified by |metric_name|. | 84 // Records a sample of the rappor metric specified by |metric_name|. |
88 // Creates and initializes the metric, if it doesn't yet exist. | 85 // Creates and initializes the metric, if it doesn't yet exist. |
89 virtual void RecordSample(const std::string& metric_name, | 86 void RecordSampleString(const std::string& metric_name, |
90 RapporType type, | 87 RapporType type, |
91 const std::string& sample); | 88 const std::string& sample) override; |
92 | 89 |
93 // Registers the names of all of the preferences used by RapporService in the | 90 // Registers the names of all of the preferences used by RapporServiceImpl in |
94 // provided PrefRegistry. This should be called before calling Start(). | 91 // the provided PrefRegistry. This should be called before calling Start(). |
95 static void RegisterPrefs(PrefRegistrySimple* registry); | 92 static void RegisterPrefs(PrefRegistrySimple* registry); |
96 | 93 |
97 protected: | 94 protected: |
98 // Initializes the state of the RapporService. | 95 // Initializes the state of the RapporServiceImpl. |
99 void InitializeInternal(std::unique_ptr<LogUploaderInterface> uploader, | 96 void InitializeInternal(std::unique_ptr<LogUploaderInterface> uploader, |
100 int32_t cohort, | 97 int32_t cohort, |
101 const std::string& secret); | 98 const std::string& secret); |
102 | 99 |
103 // Cancels the next call to OnLogInterval. | 100 // Cancels the next call to OnLogInterval. |
104 virtual void CancelNextLogRotation(); | 101 virtual void CancelNextLogRotation(); |
105 | 102 |
106 // Schedules the next call to OnLogInterval. | 103 // Schedules the next call to OnLogInterval. |
107 virtual void ScheduleNextLogRotation(base::TimeDelta interval); | 104 virtual void ScheduleNextLogRotation(base::TimeDelta interval); |
108 | 105 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 // UMA_RECORDING_GROUP | SAFEBROWSING_RECORDING_GROUP | 158 // UMA_RECORDING_GROUP | SAFEBROWSING_RECORDING_GROUP |
162 int recording_groups_; | 159 int recording_groups_; |
163 | 160 |
164 // We keep all registered metrics in a map, from name to metric. | 161 // We keep all registered metrics in a map, from name to metric. |
165 std::map<std::string, std::unique_ptr<RapporMetric>> metrics_map_; | 162 std::map<std::string, std::unique_ptr<RapporMetric>> metrics_map_; |
166 | 163 |
167 internal::Sampler sampler_; | 164 internal::Sampler sampler_; |
168 | 165 |
169 base::ThreadChecker thread_checker_; | 166 base::ThreadChecker thread_checker_; |
170 | 167 |
171 DISALLOW_COPY_AND_ASSIGN(RapporService); | 168 DISALLOW_COPY_AND_ASSIGN(RapporServiceImpl); |
172 }; | 169 }; |
173 | 170 |
174 } // namespace rappor | 171 } // namespace rappor |
175 | 172 |
176 #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ | 173 #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_IMPL_H_ |
OLD | NEW |