• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                            CdmServiceProcessObserverUtility);
73   FRIEND_TEST_ALL_PREFIXES(ContentStabilityMetricsProviderTest,
74                            CdmServiceProcessObserverUtilityLaunchFailed);
75 #if BUILDFLAG(IS_WIN)
76   FRIEND_TEST_ALL_PREFIXES(ContentStabilityMetricsProviderTest,
77                            MediaFoundationServiceProcessObserverUtility);
78 #endif  // BUILDFLAG(IS_WIN)
79   FRIEND_TEST_ALL_PREFIXES(ContentStabilityMetricsProviderTest,
80                            UnknownCdmServiceProcessObserverUtility);
81   FRIEND_TEST_ALL_PREFIXES(ContentStabilityMetricsProviderTest,
82                            RenderProcessObserver);
83   FRIEND_TEST_ALL_PREFIXES(ContentStabilityMetricsProviderTest,
84                            MetricsServicesWebContentObserver);
85   FRIEND_TEST_ALL_PREFIXES(ContentStabilityMetricsProviderTest,
86                            ExtensionsNotificationObserver);
87 
88   // content::RenderProcessHostCreationObserver:
89   void OnRenderProcessHostCreated(content::RenderProcessHost* host) override;
90   void OnRenderProcessHostCreationFailed(
91       content::RenderProcessHost* host,
92       const content::ChildProcessTerminationInfo& info) override;
93 
94   // content::RenderProcessHostObserver:
95   void RenderProcessExited(
96       content::RenderProcessHost* host,
97       const content::ChildProcessTerminationInfo& info) override;
98   void RenderProcessHostDestroyed(content::RenderProcessHost* host) override;
99 
100   // content::BrowserChildProcessObserver:
101   void BrowserChildProcessCrashed(
102       const content::ChildProcessData& data,
103       const content::ChildProcessTerminationInfo& info) override;
104   void BrowserChildProcessLaunchedAndConnected(
105       const content::ChildProcessData& data) override;
106   void BrowserChildProcessLaunchFailed(
107       const content::ChildProcessData& data,
108       const content::ChildProcessTerminationInfo& info) override;
109 
110 #if BUILDFLAG(IS_ANDROID)
111   // crash_reporter::CrashMetricsReporter::Observer:
112   void OnCrashDumpProcessed(
113       int rph_id,
114       const crash_reporter::CrashMetricsReporter::ReportedCrashTypeSet&
115           reported_counts) override;
116 
117   base::ScopedObservation<crash_reporter::CrashMetricsReporter,
118                           crash_reporter::CrashMetricsReporter::Observer>
119       scoped_observation_{this};
120 #endif  // BUILDFLAG(IS_ANDROID)
121 
122   StabilityMetricsHelper helper_;
123 
124   base::ScopedMultiSourceObservation<content::RenderProcessHost,
125                                      content::RenderProcessHostObserver>
126       host_observation_{this};
127 
128   std::unique_ptr<ExtensionsHelper> extensions_helper_;
129 };
130 
131 }  // namespace metrics
132 
133 #endif  // COMPONENTS_METRICS_CONTENT_CONTENT_STABILITY_METRICS_PROVIDER_H_
134