xref: /aosp_15_r20/external/cronet/components/metrics/content/content_stability_metrics_provider.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 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_CONTENT_CONTENT_STABILITY_METRICS_PROVIDER_H_
6 #define COMPONENTS_METRICS_CONTENT_CONTENT_STABILITY_METRICS_PROVIDER_H_
7 
8 #include <memory>
9 
10 #include "base/gtest_prod_util.h"
11 #include "base/scoped_multi_source_observation.h"
12 #include "base/scoped_observation.h"
13 #include "build/build_config.h"
14 #include "components/metrics/metrics_provider.h"
15 #include "components/metrics/stability_metrics_helper.h"
16 #include "content/public/browser/browser_child_process_observer.h"
17 #include "content/public/browser/render_process_host_creation_observer.h"
18 #include "content/public/browser/render_process_host_observer.h"
19 
20 #if BUILDFLAG(IS_ANDROID)
21 #include "components/crash/content/browser/crash_metrics_reporter_android.h"
22 #endif  // BUILDFLAG(IS_ANDROID)
23 
24 class PrefService;
25 
26 namespace metrics {
27 
28 class ExtensionsHelper;
29 
30 // ContentStabilityMetricsProvider gathers and logs Chrome-specific stability-
31 // related metrics.
32 class ContentStabilityMetricsProvider
33     : public MetricsProvider,
34       public content::BrowserChildProcessObserver,
35 #if BUILDFLAG(IS_ANDROID)
36       public crash_reporter::CrashMetricsReporter::Observer,
37 #endif
38       public content::RenderProcessHostCreationObserver,
39       public content::RenderProcessHostObserver {
40  public:
41   // |extensions_helper| is used to determine if a process corresponds to an
42   // extension and is optional. If an ExtensionsHelper is not supplied it is
43   // assumed the process does not correspond to an extension.
44   ContentStabilityMetricsProvider(
45       PrefService* local_state,
46       std::unique_ptr<ExtensionsHelper> extensions_helper);
47   ContentStabilityMetricsProvider(const ContentStabilityMetricsProvider&) =
48       delete;
49   ContentStabilityMetricsProvider& operator=(
50       const ContentStabilityMetricsProvider&) = delete;
51   ~ContentStabilityMetricsProvider() override;
52 
53   // MetricsProvider:
54   void OnRecordingEnabled() override;
55   void OnRecordingDisabled() override;
56   void OnPageLoadStarted() override;
57 #if BUILDFLAG(IS_ANDROID)
58   // A couple Local-State-pref-based stability counts are retained for Android
59   // WebView. Other platforms, including Android Chrome and WebLayer, should use
60   // Stability.Counts2 as the source of truth for these counts.
61   void ProvideStabilityMetrics(
62       SystemProfileProto* system_profile_proto) override;
63   void ClearSavedStabilityMetrics() override;
64 #endif  // BUILDFLAG(IS_ANDROID)
65 
66  private:
67   FRIEND_TEST_ALL_PREFIXES(ContentStabilityMetricsProviderTest,
68                            BrowserChildProcessObserverGpu);
69   FRIEND_TEST_ALL_PREFIXES(ContentStabilityMetricsProviderTest,
70                            BrowserChildProcessObserverUtility);
71   FRIEND_TEST_ALL_PREFIXES(ContentStabilityMetricsProviderTest,
72                            RenderProcessObserver);
73   FRIEND_TEST_ALL_PREFIXES(ContentStabilityMetricsProviderTest,
74                            MetricsServicesWebContentObserver);
75   FRIEND_TEST_ALL_PREFIXES(ContentStabilityMetricsProviderTest,
76                            ExtensionsNotificationObserver);
77 
78   // content::RenderProcessHostCreationObserver:
79   void OnRenderProcessHostCreated(content::RenderProcessHost* host) override;
80 
81   // content::RenderProcessHostObserver:
82   void RenderProcessExited(
83       content::RenderProcessHost* host,
84       const content::ChildProcessTerminationInfo& info) override;
85   void RenderProcessHostDestroyed(content::RenderProcessHost* host) override;
86 
87   // content::BrowserChildProcessObserver:
88   void BrowserChildProcessCrashed(
89       const content::ChildProcessData& data,
90       const content::ChildProcessTerminationInfo& info) override;
91   void BrowserChildProcessLaunchedAndConnected(
92       const content::ChildProcessData& data) override;
93   void BrowserChildProcessLaunchFailed(
94       const content::ChildProcessData& data,
95       const content::ChildProcessTerminationInfo& info) override;
96 
97 #if BUILDFLAG(IS_ANDROID)
98   // crash_reporter::CrashMetricsReporter::Observer:
99   void OnCrashDumpProcessed(
100       int rph_id,
101       const crash_reporter::CrashMetricsReporter::ReportedCrashTypeSet&
102           reported_counts) override;
103 
104   base::ScopedObservation<crash_reporter::CrashMetricsReporter,
105                           crash_reporter::CrashMetricsReporter::Observer>
106       scoped_observation_{this};
107 #endif  // BUILDFLAG(IS_ANDROID)
108 
109   StabilityMetricsHelper helper_;
110 
111   base::ScopedMultiSourceObservation<content::RenderProcessHost,
112                                      content::RenderProcessHostObserver>
113       host_observation_{this};
114 
115   std::unique_ptr<ExtensionsHelper> extensions_helper_;
116 };
117 
118 }  // namespace metrics
119 
120 #endif  // COMPONENTS_METRICS_CONTENT_CONTENT_STABILITY_METRICS_PROVIDER_H_
121