• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   // Records a CDM utility process launch with name |metrics_name|.
112   void CdmUtilityProcessLaunched(const std::string& metrics_name);
113 
114   // Records a CDM utility process crash with name |metrics_name|.
115   void CdmUtilityProcessCrashed(const std::string& metrics_name, int exit_code);
116 
117   // Records that a CDM utility process process with name |metrics_name| failed
118   // to launch. The |launch_error_code| is a platform-specific error code. On
119   // Windows, a |last_error| is also supplied to help diagnose the launch
120   // failure.
121   void CdmUtilityProcessLaunchFailed(const std::string& metrics_name,
122                                      int launch_error_code
123 #if BUILDFLAG(IS_WIN)
124                                      ,
125                                      DWORD last_error
126 #endif
127   );
128 
129   // Logs the initiation of a page load.
130   void LogLoadStarted();
131 
132   // Records a renderer process crash.
133 #if BUILDFLAG(IS_IOS)
134   void LogRendererCrash();
135 #elif !BUILDFLAG(IS_ANDROID)
136   void LogRendererCrash(RendererHostedContentType hosted_content_type,
137                         base::TerminationStatus status,
138                         int exit_code);
139 #endif
140 
141   // Records that a new renderer process was successfully launched.
142   void LogRendererLaunched(bool was_extension_process);
143 
144   // Registers local state prefs used by this class.
145   static void RegisterPrefs(PrefRegistrySimple* registry);
146 
147   // Increments the RendererCrash pref.
148   void IncreaseRendererCrashCount();
149 
150   // Increments the GpuCrash pref.
151   // Note: This is currently only used on Android. If you want to call this on
152   // another platform, server-side processing code needs to be updated for that
153   // platform to use the new data. Server-side currently assumes Android-only.
154   void IncreaseGpuCrashCount();
155 
156   // Records a histogram for the input |stability_event_type|.
157   static void RecordStabilityEvent(StabilityEventType stability_event_type);
158 
159  private:
160   // Used for metrics. Keep in sync with the corresponding enums.xml definition.
161   // Do not repurpose previously used indexes.
162   enum class CoarseRendererType {
163     kRenderer = 1,
164     kExtension = 2,
165     kMaxValue = kExtension,
166   };
167 
168   // Increments an Integer pref value specified by |path|.
169   void IncrementPrefValue(const char* path);
170 
171   // Records metrics specific to these termination statuses:
172   // - TERMINATION_STATUS_PROCESS_CRASHED
173   // - TERMINATION_STATUS_ABNORMAL_TERMINATION
174   // - TERMINATION_STATUS_OOM
175   // Extracted to a helper method to allow sharing between desktop and iOS.
176   void LogRendererCrashImpl(CoarseRendererType coarse_renderer_type,
177                             int exit_code);
178 
179   raw_ptr<PrefService> local_state_;
180 };
181 
182 }  // namespace metrics
183 
184 #endif  // COMPONENTS_METRICS_STABILITY_METRICS_HELPER_H_
185