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