xref: /aosp_15_r20/external/cronet/components/metrics/stability_metrics_helper.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2015 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_STABILITY_METRICS_HELPER_H_
6 #define COMPONENTS_METRICS_STABILITY_METRICS_HELPER_H_
7 
8 #include <string>
9 
10 #include "base/memory/raw_ptr.h"
11 #include "base/process/kill.h"
12 #include "build/build_config.h"
13 
14 #if BUILDFLAG(IS_WIN)
15 #include "base/win/windows_types.h"
16 #endif
17 
18 class PrefRegistrySimple;
19 class PrefService;
20 
21 namespace metrics {
22 
23 // The values here correspond to values in the Stability message in
24 // system_profile.proto.
25 // This must stay 1-1 with the StabilityEventType enum in enums.xml.
26 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.components.metrics
27 enum class StabilityEventType {
28   kPageLoad = 2,
29   kRendererCrash = 3,
30   // kRendererHang = 4,  // Removed due to disuse and correctness issues.
31   kExtensionCrash = 5,
32   // kChildProcessCrash = 6,  // Removed due to disuse and alternative metrics.
33   kLaunch = 15,
34   kBrowserCrash = 16,
35   // kIncompleteShutdown = 17,  // Removed due to disuse and correctness issues.
36   // kPluginCrash = 22,  // Removed due to plugin deprecation.
37   kRendererFailedLaunch = 24,
38   kExtensionRendererFailedLaunch = 25,
39   kRendererLaunch = 26,
40   kExtensionRendererLaunch = 27,
41   kGpuCrash = 31,
42   kUtilityCrash = 32,
43   kUtilityLaunch = 33,
44   kMaxValue = kUtilityLaunch,
45 };
46 
47 // Types of content hosted by a renderer process.
48 //
49 // Used for metrics. Keep in sync with the "RendererHostedContentType" histogram
50 // enum. Do not repurpose previously used indexes.
51 enum class RendererHostedContentType {
52   // Hosting an extension
53   kExtension = 0,
54   // Hosting an active foreground main frame
55   kForegroundMainFrame = 1,
56   // Hosting an active foreground subframe (but no active foreground main frame)
57   kForegroundSubframe = 2,
58   // Hosting an active background frame (but no active foreground frame)
59   kBackgroundFrame = 3,
60   // Hosting an inactive frame (but no active frame)
61   // Examples of inactive frames: pending commit, prerendering, in BFCache...
62   kInactiveFrame = 4,
63   // Not hosting any frame or extension
64   kNoFrameOrExtension = 5,
65   kMaxValue = kNoFrameOrExtension,
66 };
67 
68 class SystemProfileProto;
69 
70 // Responsible for providing functionality common to different embedders'
71 // stability metrics providers.
72 class StabilityMetricsHelper {
73  public:
74   explicit StabilityMetricsHelper(PrefService* local_state);
75 
76   StabilityMetricsHelper(const StabilityMetricsHelper&) = delete;
77   StabilityMetricsHelper& operator=(const StabilityMetricsHelper&) = delete;
78 
79   ~StabilityMetricsHelper();
80 
81 #if BUILDFLAG(IS_ANDROID)
82   // A couple Local-State-pref-based stability counts are retained for Android
83   // WebView. Other platforms, including Android Chrome and WebLayer, should use
84   // Stability.Counts2 as the source of truth for these counts.
85 
86   // Provides stability metrics.
87   void ProvideStabilityMetrics(SystemProfileProto* system_profile_proto);
88 
89   // Clears the gathered stability metrics.
90   void ClearSavedStabilityMetrics();
91 #endif  // BUILDFLAG(IS_ANDROID)
92 
93   // Records a utility process launch with name |metrics_name|.
94   void BrowserUtilityProcessLaunched(const std::string& metrics_name);
95 
96   // Records a utility process crash with name |metrics_name|.
97   void BrowserUtilityProcessCrashed(const std::string& metrics_name,
98                                     int exit_code);
99 
100   // Records that a utility process with name |metrics_name| failed to launch.
101   // The |launch_error_code| is a platform-specific error code. On Windows, a
102   // |last_error| is also supplied to help diagnose the launch failure.
103   void BrowserUtilityProcessLaunchFailed(const std::string& metrics_name,
104                                          int launch_error_code
105 #if BUILDFLAG(IS_WIN)
106                                          ,
107                                          DWORD last_error
108 #endif
109   );
110 
111   // Logs the initiation of a page load.
112   void LogLoadStarted();
113 
114   // Records a renderer process crash.
115 #if BUILDFLAG(IS_IOS)
116   void LogRendererCrash();
117 #elif !BUILDFLAG(IS_ANDROID)
118   void LogRendererCrash(RendererHostedContentType hosted_content_type,
119                         base::TerminationStatus status,
120                         int exit_code);
121 #endif
122 
123   // Records that a new renderer process was successfully launched.
124   void LogRendererLaunched(bool was_extension_process);
125 
126   // Registers local state prefs used by this class.
127   static void RegisterPrefs(PrefRegistrySimple* registry);
128 
129   // Increments the RendererCrash pref.
130   void IncreaseRendererCrashCount();
131 
132   // Increments the GpuCrash pref.
133   // Note: This is currently only used on Android. If you want to call this on
134   // another platform, server-side processing code needs to be updated for that
135   // platform to use the new data. Server-side currently assumes Android-only.
136   void IncreaseGpuCrashCount();
137 
138   // Records a histogram for the input |stability_event_type|.
139   static void RecordStabilityEvent(StabilityEventType stability_event_type);
140 
141  private:
142   // Used for metrics. Keep in sync with the corresponding enums.xml definition.
143   // Do not repurpose previously used indexes.
144   enum class CoarseRendererType {
145     kRenderer = 1,
146     kExtension = 2,
147     kMaxValue = kExtension,
148   };
149 
150   // Increments an Integer pref value specified by |path|.
151   void IncrementPrefValue(const char* path);
152 
153   // Records metrics specific to these termination statuses:
154   // - TERMINATION_STATUS_PROCESS_CRASHED
155   // - TERMINATION_STATUS_ABNORMAL_TERMINATION
156   // - TERMINATION_STATUS_OOM
157   // Extracted to a helper method to allow sharing between desktop and iOS.
158   void LogRendererCrashImpl(CoarseRendererType coarse_renderer_type,
159                             int exit_code);
160 
161   raw_ptr<PrefService> local_state_;
162 };
163 
164 }  // namespace metrics
165 
166 #endif  // COMPONENTS_METRICS_STABILITY_METRICS_HELPER_H_
167