| 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_H_ |
| 6 #define COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ | 6 #define COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <memory> |
| 11 #include <string> | 12 #include <string> |
| 12 | 13 |
| 13 #include "base/callback.h" | 14 #include "base/callback.h" |
| 14 #include "base/macros.h" | 15 #include "base/macros.h" |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 17 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
| 18 #include "base/timer/timer.h" | 18 #include "base/timer/timer.h" |
| 19 #include "components/metrics/daily_event.h" | 19 #include "components/metrics/daily_event.h" |
| 20 #include "components/rappor/rappor_parameters.h" | 20 #include "components/rappor/rappor_parameters.h" |
| 21 #include "components/rappor/sample.h" | 21 #include "components/rappor/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; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 40 public: | 40 public: |
| 41 // Constructs a RapporService. | 41 // Constructs a RapporService. |
| 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 RapporService. |
| 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 RapporService(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 ~RapporService(); |
| 48 | 48 |
| 49 // Add an observer for collecting daily metrics. | 49 // Add an observer for collecting daily metrics. |
| 50 void AddDailyObserver(scoped_ptr<metrics::DailyEvent::Observer> observer); | 50 void AddDailyObserver( |
| 51 std::unique_ptr<metrics::DailyEvent::Observer> observer); |
| 51 | 52 |
| 52 // Initializes the rappor service, including loading the cohort and secret | 53 // Initializes the rappor service, including loading the cohort and secret |
| 53 // preferences from disk. | 54 // preferences from disk. |
| 54 void Initialize(net::URLRequestContextGetter* context); | 55 void Initialize(net::URLRequestContextGetter* context); |
| 55 | 56 |
| 56 // Updates the settings for metric recording and uploading. | 57 // Updates the settings for metric recording and uploading. |
| 57 // The RapporService must be initialized before this method is called. | 58 // The RapporService must be initialized before this method is called. |
| 58 // |recording_groups| should be set of flags, e.g. | 59 // |recording_groups| should be set of flags, e.g. |
| 59 // UMA_RECORDING_GROUP | SAFEBROWSING_RECORDING_GROUP | 60 // UMA_RECORDING_GROUP | SAFEBROWSING_RECORDING_GROUP |
| 60 // If it contains any enabled groups, periodic reports will be | 61 // If it contains any enabled groups, periodic reports will be |
| 61 // generated and queued for upload. | 62 // generated and queued for upload. |
| 62 // 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. |
| 63 void Update(int recording_groups, bool may_upload); | 64 void Update(int recording_groups, bool may_upload); |
| 64 | 65 |
| 65 // Constructs a Sample object for the caller to record fields in. | 66 // Constructs a Sample object for the caller to record fields in. |
| 66 virtual scoped_ptr<Sample> CreateSample(RapporType); | 67 virtual std::unique_ptr<Sample> CreateSample(RapporType); |
| 67 | 68 |
| 68 // Records a Sample of rappor metric specified by |metric_name|. | 69 // Records a Sample of rappor metric specified by |metric_name|. |
| 69 // | 70 // |
| 70 // TODO(holte): Rename RecordSample to RecordString and then rename this | 71 // TODO(holte): Rename RecordSample to RecordString and then rename this |
| 71 // to RecordSample. | 72 // to RecordSample. |
| 72 // | 73 // |
| 73 // example: | 74 // example: |
| 74 // scoped_ptr<Sample> sample = rappor_service->CreateSample(MY_METRIC_TYPE); | 75 // std::unique_ptr<Sample> sample = |
| 76 // rappor_service->CreateSample(MY_METRIC_TYPE); |
| 75 // sample->SetStringField("Field1", "some string"); | 77 // sample->SetStringField("Field1", "some string"); |
| 76 // sample->SetFlagsValue("Field2", SOME|FLAGS); | 78 // sample->SetFlagsValue("Field2", SOME|FLAGS); |
| 77 // rappor_service->RecordSample("MyMetric", std::move(sample)); | 79 // rappor_service->RecordSample("MyMetric", std::move(sample)); |
| 78 // | 80 // |
| 79 // This will result in a report setting two metrics "MyMetric.Field1" and | 81 // This will result in a report setting two metrics "MyMetric.Field1" and |
| 80 // "MyMetric.Field2", and they will both be generated from the same sample, | 82 // "MyMetric.Field2", and they will both be generated from the same sample, |
| 81 // to allow for correllations to be computed. | 83 // to allow for correllations to be computed. |
| 82 virtual void RecordSampleObj(const std::string& metric_name, | 84 virtual void RecordSampleObj(const std::string& metric_name, |
| 83 scoped_ptr<Sample> sample); | 85 std::unique_ptr<Sample> sample); |
| 84 | 86 |
| 85 // Records a sample of the rappor metric specified by |metric_name|. | 87 // Records a sample of the rappor metric specified by |metric_name|. |
| 86 // Creates and initializes the metric, if it doesn't yet exist. | 88 // Creates and initializes the metric, if it doesn't yet exist. |
| 87 virtual void RecordSample(const std::string& metric_name, | 89 virtual void RecordSample(const std::string& metric_name, |
| 88 RapporType type, | 90 RapporType type, |
| 89 const std::string& sample); | 91 const std::string& sample); |
| 90 | 92 |
| 91 // Registers the names of all of the preferences used by RapporService in the | 93 // Registers the names of all of the preferences used by RapporService in the |
| 92 // provided PrefRegistry. This should be called before calling Start(). | 94 // provided PrefRegistry. This should be called before calling Start(). |
| 93 static void RegisterPrefs(PrefRegistrySimple* registry); | 95 static void RegisterPrefs(PrefRegistrySimple* registry); |
| 94 | 96 |
| 95 protected: | 97 protected: |
| 96 // Initializes the state of the RapporService. | 98 // Initializes the state of the RapporService. |
| 97 void InitializeInternal(scoped_ptr<LogUploaderInterface> uploader, | 99 void InitializeInternal(std::unique_ptr<LogUploaderInterface> uploader, |
| 98 int32_t cohort, | 100 int32_t cohort, |
| 99 const std::string& secret); | 101 const std::string& secret); |
| 100 | 102 |
| 101 // Cancels the next call to OnLogInterval. | 103 // Cancels the next call to OnLogInterval. |
| 102 virtual void CancelNextLogRotation(); | 104 virtual void CancelNextLogRotation(); |
| 103 | 105 |
| 104 // Schedules the next call to OnLogInterval. | 106 // Schedules the next call to OnLogInterval. |
| 105 virtual void ScheduleNextLogRotation(base::TimeDelta interval); | 107 virtual void ScheduleNextLogRotation(base::TimeDelta interval); |
| 106 | 108 |
| 107 // Logs all of the collected metrics to the reports proto message and clears | 109 // Logs all of the collected metrics to the reports proto message and clears |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 // The cohort this client is assigned to. -1 is uninitialized. | 148 // The cohort this client is assigned to. -1 is uninitialized. |
| 147 int32_t cohort_; | 149 int32_t cohort_; |
| 148 | 150 |
| 149 // Timer which schedules calls to OnLogInterval(). | 151 // Timer which schedules calls to OnLogInterval(). |
| 150 base::OneShotTimer log_rotation_timer_; | 152 base::OneShotTimer log_rotation_timer_; |
| 151 | 153 |
| 152 // A daily event for collecting metrics once a day. | 154 // A daily event for collecting metrics once a day. |
| 153 metrics::DailyEvent daily_event_; | 155 metrics::DailyEvent daily_event_; |
| 154 | 156 |
| 155 // A private LogUploader instance for sending reports to the server. | 157 // A private LogUploader instance for sending reports to the server. |
| 156 scoped_ptr<LogUploaderInterface> uploader_; | 158 std::unique_ptr<LogUploaderInterface> uploader_; |
| 157 | 159 |
| 158 // The set of recording groups that metrics are being recorded, e.g. | 160 // The set of recording groups that metrics are being recorded, e.g. |
| 159 // UMA_RECORDING_GROUP | SAFEBROWSING_RECORDING_GROUP | 161 // UMA_RECORDING_GROUP | SAFEBROWSING_RECORDING_GROUP |
| 160 int recording_groups_; | 162 int recording_groups_; |
| 161 | 163 |
| 162 // We keep all registered metrics in a map, from name to metric. | 164 // We keep all registered metrics in a map, from name to metric. |
| 163 // The map owns the metrics it contains. | 165 // The map owns the metrics it contains. |
| 164 std::map<std::string, RapporMetric*> metrics_map_; | 166 std::map<std::string, RapporMetric*> metrics_map_; |
| 165 | 167 |
| 166 internal::Sampler sampler_; | 168 internal::Sampler sampler_; |
| 167 | 169 |
| 168 base::ThreadChecker thread_checker_; | 170 base::ThreadChecker thread_checker_; |
| 169 | 171 |
| 170 DISALLOW_COPY_AND_ASSIGN(RapporService); | 172 DISALLOW_COPY_AND_ASSIGN(RapporService); |
| 171 }; | 173 }; |
| 172 | 174 |
| 173 } // namespace rappor | 175 } // namespace rappor |
| 174 | 176 |
| 175 #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ | 177 #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ |
| OLD | NEW |