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. |
| 52 void 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 private: |
| 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 GetSecret(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 |