• 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 class SystemProfileProto;
48 
49 // Responsible for providing functionality common to different embedders'
50 // stability metrics providers.
51 class StabilityMetricsHelper {
52  public:
53   explicit StabilityMetricsHelper(PrefService* local_state);
54 
55   StabilityMetricsHelper(const StabilityMetricsHelper&) = delete;
56   StabilityMetricsHelper& operator=(const StabilityMetricsHelper&) = delete;
57 
58   ~StabilityMetricsHelper();
59 
60 #if BUILDFLAG(IS_ANDROID)
61   // A couple Local-State-pref-based stability counts are retained for Android
62   // WebView. Other platforms, including Android Chrome and WebLayer, should use
63   // Stability.Counts2 as the source of truth for these counts.
64 
65   // Provides stability metrics.
66   void ProvideStabilityMetrics(SystemProfileProto* system_profile_proto);
67 
68   // Clears the gathered stability metrics.
69   void ClearSavedStabilityMetrics();
70 #endif  // BUILDFLAG(IS_ANDROID)
71 
72   // Records a utility process launch with name |metrics_name|.
73   void BrowserUtilityProcessLaunched(const std::string& metrics_name);
74 
75   // Records a utility process crash with name |metrics_name|.
76   void BrowserUtilityProcessCrashed(const std::string& metrics_name,
77                                     int exit_code);
78 
79   // Records that a utility process with name |metrics_name| failed to launch.
80   // The |launch_error_code| is a platform-specific error code. On Windows, a
81   // |last_error| is also supplied to help diagnose the launch failure.
82   void BrowserUtilityProcessLaunchFailed(const std::string& metrics_name,
83                                          int launch_error_code
84 #if BUILDFLAG(IS_WIN)
85                                          ,
86                                          DWORD last_error
87 #endif
88   );
89 
90   // Logs the initiation of a page load.
91   void LogLoadStarted();
92 
93 #if !BUILDFLAG(IS_ANDROID)
94   // Records a renderer process crash.
95   void LogRendererCrash(bool was_extension_process,
96                         base::TerminationStatus status,
97                         int exit_code);
98 #endif  // !BUILDFLAG(IS_ANDROID)
99 
100   // Records that a new renderer process was successfully launched.
101   void LogRendererLaunched(bool was_extension_process);
102 
103   // Registers local state prefs used by this class.
104   static void RegisterPrefs(PrefRegistrySimple* registry);
105 
106   // Increments the RendererCrash pref.
107   void IncreaseRendererCrashCount();
108 
109   // Increments the GpuCrash pref.
110   // Note: This is currently only used on Android. If you want to call this on
111   // another platform, server-side processing code needs to be updated for that
112   // platform to use the new data. Server-side currently assumes Android-only.
113   void IncreaseGpuCrashCount();
114 
115   // Records a histogram for the input |stability_event_type|.
116   static void RecordStabilityEvent(StabilityEventType stability_event_type);
117 
118  private:
119   // Increments an Integer pref value specified by |path|.
120   void IncrementPrefValue(const char* path);
121 
122   // Records that a renderer launch failed.
123   void LogRendererLaunchFailed(bool was_extension_process);
124 
125   raw_ptr<PrefService> local_state_;
126 };
127 
128 }  // namespace metrics
129 
130 #endif  // COMPONENTS_METRICS_STABILITY_METRICS_HELPER_H_
131