OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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_UKM_UKM_SERVICE_H_ |
| 6 #define COMPONENTS_UKM_UKM_SERVICE_H_ |
| 7 |
| 8 #include <stddef.h> |
| 9 #include <memory> |
| 10 |
| 11 #include "base/feature_list.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/threading/thread_checker.h" |
| 15 #include "build/build_config.h" |
| 16 #include "components/metrics/metrics_reporting_scheduler.h" |
| 17 #include "components/metrics/persisted_logs.h" |
| 18 |
| 19 class PrefRegistrySimple; |
| 20 class PrefService; |
| 21 |
| 22 namespace metrics { |
| 23 class MetricsLogUploader; |
| 24 class MetricsServiceClient; |
| 25 } |
| 26 |
| 27 namespace ukm { |
| 28 |
| 29 // This feature controls whether UkmService should be created. |
| 30 extern const base::Feature kUkmFeature; |
| 31 |
| 32 // The Url-Keyed Metrics (UKM) service is responsible for gathering and |
| 33 // uploading reports that contain fine grained performance metrics including |
| 34 // URLs for top-level navigations. |
| 35 class UkmService : public base::SupportsWeakPtr<UkmService> { |
| 36 public: |
| 37 // Constructs a UkmService. |
| 38 // Calling code is responsible for ensuring that the lifetime of |
| 39 // |pref_service| is longer than the lifetime of UkmService. |
| 40 UkmService(PrefService* pref_service, metrics::MetricsServiceClient* client); |
| 41 virtual ~UkmService(); |
| 42 |
| 43 // Initializes the UKM service. |
| 44 void Initialize(); |
| 45 |
| 46 // Enable/disable transmission of accumulated logs. Logs that have already |
| 47 // been created will remain persisted to disk. |
| 48 void EnableReporting(); |
| 49 void DisableReporting(); |
| 50 |
| 51 // Record any collected data into logs, and write to disk. |
| 52 void Flush(); |
| 53 |
| 54 // Delete any unsent local data. |
| 55 void Purge(); |
| 56 |
| 57 // Registers the names of all of the preferences used by UkmService in |
| 58 // the provided PrefRegistry. |
| 59 static void RegisterPrefs(PrefRegistrySimple* registry); |
| 60 |
| 61 private: |
| 62 // Start metrics client initialization. |
| 63 void StartInitTask(); |
| 64 |
| 65 // Called when initialization tasks are complete, to notify the scheduler |
| 66 // that it can begin calling RotateLog. |
| 67 void FinishedInitTask(); |
| 68 |
| 69 // Periodically called by scheduler_ to advance processing of logs. |
| 70 void RotateLog(); |
| 71 |
| 72 // Constructs a new Report from available data and stores it in |
| 73 // persisted_logs_. |
| 74 void BuildAndStoreLog(); |
| 75 |
| 76 // Start an upload of the next log from persisted_logs_. |
| 77 void StartScheduledUpload(); |
| 78 |
| 79 // Called by log_uploader_ when the an upload is completed. |
| 80 void OnLogUploadComplete(int response_code); |
| 81 |
| 82 // A weak pointer to the PrefService used to read and write preferences. |
| 83 PrefService* pref_service_; |
| 84 |
| 85 // The UKM client id stored in prefs. |
| 86 uint64_t client_id_; |
| 87 |
| 88 // Used to interact with the embedder. Weak pointer; must outlive |this| |
| 89 // instance. |
| 90 metrics::MetricsServiceClient* const client_; |
| 91 |
| 92 // Logs that have not yet been sent. |
| 93 metrics::PersistedLogs persisted_logs_; |
| 94 |
| 95 // The scheduler for determining when uploads should happen. |
| 96 std::unique_ptr<metrics::MetricsReportingScheduler> scheduler_; |
| 97 |
| 98 base::ThreadChecker thread_checker_; |
| 99 |
| 100 // Instance of the helper class for uploading logs. |
| 101 std::unique_ptr<metrics::MetricsLogUploader> log_uploader_; |
| 102 |
| 103 bool initialize_started_; |
| 104 bool initialize_complete_; |
| 105 bool log_upload_in_progress_; |
| 106 |
| 107 // Weak pointers factory used to post task on different threads. All weak |
| 108 // pointers managed by this factory have the same lifetime as UkmService. |
| 109 base::WeakPtrFactory<UkmService> self_ptr_factory_; |
| 110 |
| 111 DISALLOW_COPY_AND_ASSIGN(UkmService); |
| 112 }; |
| 113 |
| 114 } // namespace ukm |
| 115 |
| 116 #endif // COMPONENTS_UKM_UKM_SERVICE_H_ |
OLD | NEW |