1 // Copyright 2019 The Chromium Authors 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_METRICS_STRUCTURED_RECORDER_H_ 6 #define COMPONENTS_METRICS_STRUCTURED_RECORDER_H_ 7 8 #include "base/callback_list.h" 9 #include "base/memory/scoped_refptr.h" 10 #include "base/no_destructor.h" 11 #include "base/observer_list.h" 12 #include "base/observer_list_types.h" 13 #include "base/task/sequenced_task_runner.h" 14 #include "components/metrics/structured/delegating_events_processor.h" 15 #include "components/metrics/structured/event.h" 16 #include "components/metrics/structured/events_processor_interface.h" 17 #include "components/metrics/structured/structured_metrics_client.h" 18 #include "components/metrics/structured/structured_metrics_validator.h" 19 #include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h" 20 21 namespace metrics::structured { 22 namespace { 23 24 using ::metrics::ChromeUserMetricsExtension; 25 26 } 27 28 // Recorder is a singleton to help communicate with the 29 // StructuredMetricsProvider. It serves three purposes: 30 // 1. Begin the initialization of the StructuredMetricsProvider (see class 31 // comment for more details). 32 // 2. Add an event for the StructuredMetricsProvider to record. 33 // 3. Retrieving information about project's key, specifically the day it was 34 // last rotated. 35 // 36 // The StructuredMetricsProvider is owned by the MetricsService, but it needs to 37 // be accessible to any part of the codebase, via an EventBase subclass, to 38 // record events. The StructuredMetricsProvider registers itself as an observer 39 // of this singleton when recording is enabled, and calls to Record (for 40 // recording) or ProfileAdded (for initialization) are then forwarded to it. 41 // 42 // Recorder is embedded within StructuredMetricsClient for Ash Chrome and should 43 // only be used in Ash Chrome. 44 // 45 // TODO(b/282031543): Remove this class and merge remaining logic into 46 // structured_metrics_recorder.h since the Record() is exposed via 47 // StructuredMetricsClient interface now. 48 class Recorder { 49 public: 50 class RecorderImpl : public base::CheckedObserver { 51 public: 52 // Called on a call to Record. 53 virtual void OnEventRecord(const Event& event) = 0; 54 // Called when SystemProfile has finished loading OnSystemProfileInitialized()55 virtual void OnSystemProfileInitialized() {} 56 }; 57 58 Recorder(const Recorder&) = delete; 59 Recorder& operator=(const Recorder&) = delete; 60 61 static Recorder* GetInstance(); 62 63 // This signals to StructuredMetricsProvider that the event should be 64 // recorded. 65 void RecordEvent(Event&& event); 66 67 // Notifies observers that system profile has been loaded. 68 void OnSystemProfileInitialized(); 69 70 void SetUiTaskRunner( 71 const scoped_refptr<base::SequencedTaskRunner> ui_task_runner); 72 73 void AddObserver(RecorderImpl* observer); 74 void RemoveObserver(RecorderImpl* observer); 75 76 // Adds |events_processor| to further add metadata to recorded events or 77 // listen to recorded events. 78 void AddEventsProcessor( 79 std::unique_ptr<EventsProcessorInterface> events_processor); 80 81 // Modifies |uma_proto| before the log is sent. 82 void OnProvideIndependentMetrics(ChromeUserMetricsExtension* uma_proto); 83 84 // Modifies |event| once after the proto has been built. 85 void OnEventRecorded(StructuredEventProto* event); 86 87 private: 88 friend class base::NoDestructor<Recorder>; 89 90 Recorder(); 91 ~Recorder(); 92 93 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; 94 95 base::ObserverList<RecorderImpl> observers_; 96 97 DelegatingEventsProcessor delegating_events_processor_; 98 }; 99 100 } // namespace metrics::structured 101 102 #endif // COMPONENTS_METRICS_STRUCTURED_RECORDER_H_ 103