• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Chromium Embedded Framework Authors. Portions copyright
2 // 2016 The Chromium Authors. All rights reserved. Use of this source code is
3 // governed by a BSD-style license that can be found in the LICENSE file.
4 
5 #ifndef CEF_LIBCEF_COMMON_CRASH_REPORTER_CLIENT_H_
6 #define CEF_LIBCEF_COMMON_CRASH_REPORTER_CLIENT_H_
7 
8 #include <string>
9 #include <vector>
10 
11 // Include this first to avoid compiler errors.
12 #include "base/compiler_specific.h"
13 
14 #include "include/cef_version.h"
15 
16 #include "base/strings/string_piece_forward.h"
17 #include "base/synchronization/lock.h"
18 #include "build/build_config.h"
19 #include "components/crash/core/app/crash_reporter_client.h"
20 
21 // Global object that is instantiated in each process and configures crash
22 // reporting. On Windows this is created by the
23 // InitializeCrashReportingForProcess() method called from chrome_elf. On
24 // Linux and macOS this is created by crash_reporting::BasicStartupComplete().
25 class CefCrashReporterClient : public crash_reporter::CrashReporterClient {
26  public:
27   CefCrashReporterClient();
28 
29   CefCrashReporterClient(const CefCrashReporterClient&) = delete;
30   CefCrashReporterClient& operator=(const CefCrashReporterClient&) = delete;
31 
32   ~CefCrashReporterClient() override;
33 
34   // Reads the crash config file and returns true on success. Failure to read
35   // the crash config file will disable crash reporting. This method should be
36   // called immediately after the CefCrashReporterClient instance is created.
37   bool ReadCrashConfigFile();
38   bool HasCrashConfigFile() const;
39 
40 #if BUILDFLAG(IS_WIN)
41   // Called from chrome_elf (chrome_elf/crash/crash_helper.cc) to instantiate
42   // a process wide instance of CefCrashReporterClient and initialize crash
43   // reporting for the process. The instance is leaked.
44   // crash_reporting_win::InitializeCrashReportingForModule() will be called
45   // later from crash_reporting::PreSandboxStartup() to read global state into
46   // the module address space.
47   static void InitializeCrashReportingForProcess();
48 
49   bool GetAlternativeCrashDumpLocation(std::wstring* crash_dir) override;
50   void GetProductNameAndVersion(const std::wstring& exe_path,
51                                 std::wstring* product_name,
52                                 std::wstring* version,
53                                 std::wstring* special_build,
54                                 std::wstring* channel_name) override;
55   bool GetCrashDumpLocation(std::wstring* crash_dir) override;
56   bool GetCrashMetricsLocation(std::wstring* metrics_dir) override;
57 #elif BUILDFLAG(IS_POSIX)
58   void GetProductNameAndVersion(const char** product_name,
59                                 const char** version) override;
60   void GetProductNameAndVersion(std::string* product_name,
61                                 std::string* version,
62                                 std::string* channel) override;
63 #if !BUILDFLAG(IS_MAC)
64   base::FilePath GetReporterLogFilename() override;
65   bool EnableBreakpadForProcess(const std::string& process_type) override;
66 #endif
67   bool GetCrashDumpLocation(base::FilePath* crash_dir) override;
68 #endif  // BUILDFLAG(IS_POSIX)
69 
70   // All of these methods must return true to enable crash report upload.
71   bool GetCollectStatsConsent() override;
72   bool GetCollectStatsInSample() override;
73 #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
74   bool ReportingIsEnforcedByPolicy(bool* crashpad_enabled) override;
75 #endif
76 
77 #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC)
78   bool IsRunningUnattended() override;
79 #endif
80 
81   std::string GetUploadUrl() override;
82   void GetCrashOptionalArguments(std::vector<std::string>* arguments) override;
83 
84 #if BUILDFLAG(IS_WIN)
85   std::wstring GetCrashExternalHandler(const std::wstring& exe_dir) override;
86   bool HasCrashExternalHandler() const;
87 #endif
88 
89 #if BUILDFLAG(IS_MAC)
90   bool EnableBrowserCrashForwarding() override;
91 #endif
92 
93 #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC)
94   ParameterMap FilterParameters(const ParameterMap& parameters) override;
95 #endif
96 
97   // Set or clear a crash key value.
98   bool SetCrashKeyValue(const base::StringPiece& key,
99                         const base::StringPiece& value);
100 
101  private:
102   bool has_crash_config_file_ = false;
103 
104   enum KeySize { SMALL_SIZE, MEDIUM_SIZE, LARGE_SIZE };
105 
106   // Map of crash key name to (KeySize, index).
107   // Const access to |crash_keys_| is thread-safe after initialization.
108   using KeyMap = std::map<std::string, std::pair<KeySize, size_t>>;
109   KeyMap crash_keys_;
110 
111   // Modification of CrashKeyString values must be synchronized.
112   base::Lock crash_key_lock_;
113 
114   std::string server_url_;
115   bool rate_limit_ = true;
116   int max_uploads_ = 5;
117   int max_db_size_ = 20;
118   int max_db_age_ = 5;
119 
120   std::string product_name_ = "cef";
121   std::string product_version_ = CEF_VERSION;
122 
123 #if BUILDFLAG(IS_WIN)
124   std::string app_name_ = "CEF";
125   std::string external_handler_;
126 #endif
127 
128 #if BUILDFLAG(IS_MAC)
129   bool enable_browser_crash_forwarding_ = false;
130 #endif
131 };
132 
133 #endif  // CEF_LIBCEF_COMMON_CRASH_REPORTER_CLIENT_H_
134