Remove EWK_BRINGUPS for M120 #3
[platform/framework/web/chromium-efl.git] / chrome / app / chrome_crash_reporter_client_win.cc
1 // Copyright 2013 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 // TODO(ananta/scottmg)
6 // Add test coverage for Crashpad.
7 #include "chrome/app/chrome_crash_reporter_client_win.h"
8
9 #include <windows.h>
10
11 #include <assert.h>
12 #include <shellapi.h>
13
14 #include <iterator>
15 #include <memory>
16 #include <string>
17 #include <vector>
18
19 #include "base/command_line.h"
20 #include "base/debug/leak_annotations.h"
21 #include "base/files/file_path.h"
22 #include "base/format_macros.h"
23 #include "base/notreached.h"
24 #include "base/rand_util.h"
25 #include "base/strings/utf_string_conversions.h"
26 #include "base/win/current_module.h"
27 #include "chrome/chrome_elf/chrome_elf_constants.h"
28 #include "chrome/common/chrome_result_codes.h"
29 #include "chrome/install_static/install_util.h"
30 #include "chrome/install_static/user_data_dir.h"
31 #include "components/crash/core/app/crashpad.h"
32 #include "components/version_info/channel.h"
33
34 ChromeCrashReporterClient::ChromeCrashReporterClient() {}
35
36 ChromeCrashReporterClient::~ChromeCrashReporterClient() {}
37
38 #if !defined(NACL_WIN64)
39 // static
40 void ChromeCrashReporterClient::InitializeCrashReportingForProcess() {
41   static ChromeCrashReporterClient* instance = nullptr;
42   if (instance)
43     return;
44
45   instance = new ChromeCrashReporterClient();
46   ANNOTATE_LEAKING_OBJECT_PTR(instance);
47
48   std::wstring process_type = install_static::GetSwitchValueFromCommandLine(
49       ::GetCommandLine(), install_static::kProcessType);
50   // Don't set up Crashpad crash reporting in the Crashpad handler itself, nor
51   // in the fallback crash handler for the Crashpad handler process.
52   if (process_type != install_static::kCrashpadHandler &&
53       process_type != install_static::kFallbackHandler) {
54     crash_reporter::SetCrashReporterClient(instance);
55
56     std::wstring user_data_dir;
57     if (process_type.empty())
58       install_static::GetUserDataDirectory(&user_data_dir, nullptr);
59
60     // TODO(wfh): Add a DCHECK for success. See https://crbug.com/1329269.
61     std::ignore = crash_reporter::InitializeCrashpadWithEmbeddedHandler(
62         /*initial_client=*/process_type.empty(),
63         install_static::WideToUTF8(process_type),
64         install_static::WideToUTF8(user_data_dir), base::FilePath());
65   }
66 }
67 #endif  // !defined(NACL_WIN64)
68
69 bool ChromeCrashReporterClient::GetAlternativeCrashDumpLocation(
70     std::wstring* crash_dir) {
71   // By setting the BREAKPAD_DUMP_LOCATION environment variable, an alternate
72   // location to write breakpad crash dumps can be set.
73   *crash_dir = install_static::GetEnvironmentString(L"BREAKPAD_DUMP_LOCATION");
74   return !crash_dir->empty();
75 }
76
77 void ChromeCrashReporterClient::GetProductNameAndVersion(
78     const std::wstring& exe_path,
79     std::wstring* product_name,
80     std::wstring* version,
81     std::wstring* special_build,
82     std::wstring* channel_name) {
83   assert(product_name);
84   assert(version);
85   assert(special_build);
86   assert(channel_name);
87
88   install_static::GetExecutableVersionDetails(exe_path, product_name, version,
89                                               special_build, channel_name);
90 }
91
92 bool ChromeCrashReporterClient::ShouldShowRestartDialog(std::wstring* title,
93                                                         std::wstring* message,
94                                                         bool* is_rtl_locale) {
95   if (!install_static::HasEnvironmentVariable(install_static::kShowRestart) ||
96       !install_static::HasEnvironmentVariable(install_static::kRestartInfo)) {
97     return false;
98   }
99
100   std::wstring restart_info =
101       install_static::GetEnvironmentString(install_static::kRestartInfo);
102
103   // The CHROME_RESTART var contains the dialog strings separated by '|'.
104   // See ChromeBrowserMainPartsWin::PrepareRestartOnCrashEnviroment()
105   // for details.
106   std::vector<std::wstring> dlg_strings = install_static::TokenizeString(
107       restart_info, L'|', true);  // true = Trim whitespace.
108
109   if (dlg_strings.size() < 3)
110     return false;
111
112   *title = dlg_strings[0];
113   *message = dlg_strings[1];
114   *is_rtl_locale = dlg_strings[2] == install_static::kRtlLocale;
115   return true;
116 }
117
118 bool ChromeCrashReporterClient::AboutToRestart() {
119   if (!install_static::HasEnvironmentVariable(install_static::kRestartInfo))
120     return false;
121
122   install_static::SetEnvironmentString(install_static::kShowRestart, L"1");
123   return true;
124 }
125
126 bool ChromeCrashReporterClient::GetIsPerUserInstall() {
127   return !install_static::IsSystemInstall();
128 }
129
130 bool ChromeCrashReporterClient::GetShouldDumpLargerDumps() {
131   // Capture larger dumps for Google Chrome beta, dev, and canary channels, and
132   // Chromium builds. The Google Chrome stable channel uses smaller dumps.
133   return install_static::GetChromeChannel() != version_info::Channel::STABLE;
134 }
135
136 int ChromeCrashReporterClient::GetResultCodeRespawnFailed() {
137   return chrome::RESULT_CODE_RESPAWN_FAILED;
138 }
139
140 bool ChromeCrashReporterClient::ReportingIsEnforcedByPolicy(
141     bool* crashpad_enabled) {
142   // Determine whether configuration management allows loading the crash
143   // reporter.
144   // Since the configuration management infrastructure is not initialized at
145   // this point, we read the corresponding registry key directly. The return
146   // status indicates whether policy data was successfully read. If it is true,
147   // |breakpad_enabled| contains the value set by policy.
148   return install_static::ReportingIsEnforcedByPolicy(crashpad_enabled);
149 }
150
151 bool ChromeCrashReporterClient::GetCrashDumpLocation(std::wstring* crash_dir) {
152   // By setting the BREAKPAD_DUMP_LOCATION environment variable, an alternate
153   // location to write breakpad crash dumps can be set.
154   // If this environment variable exists, then for the time being,
155   // short-circuit how it's handled on Windows. Honoring this
156   // variable is required in order to symbolize stack traces in
157   // Telemetry based tests: http://crbug.com/561763.
158   if (GetAlternativeCrashDumpLocation(crash_dir))
159     return true;
160
161   *crash_dir = install_static::GetCrashDumpLocation();
162   return !crash_dir->empty();
163 }
164
165 bool ChromeCrashReporterClient::GetCrashMetricsLocation(
166     std::wstring* metrics_dir) {
167   if (!GetCollectStatsConsent())
168     return false;
169   install_static::GetUserDataDirectory(metrics_dir, nullptr);
170   return !metrics_dir->empty();
171 }
172
173 bool ChromeCrashReporterClient::IsRunningUnattended() {
174   return install_static::HasEnvironmentVariable(install_static::kHeadless);
175 }
176
177 bool ChromeCrashReporterClient::GetCollectStatsConsent() {
178   return install_static::GetCollectStatsConsent();
179 }
180
181 bool ChromeCrashReporterClient::GetCollectStatsInSample() {
182   return install_static::GetCollectStatsInSample();
183 }
184
185 bool ChromeCrashReporterClient::ShouldMonitorCrashHandlerExpensively() {
186   // The expensive mechanism dedicates a process to be crashpad_handler's own
187   // crashpad_handler. In Google Chrome, scale back on this in the more stable
188   // channels. There's a fallback crash handler that can catch crashes when this
189   // expensive mechanism isn't used, although the fallback crash handler has
190   // different characteristics so it's desirable to use the expensive mechanism
191   // at least some of the time.
192   double probability;
193   switch (install_static::GetChromeChannel()) {
194     case version_info::Channel::STABLE:
195       return false;
196
197     case version_info::Channel::BETA:
198       probability = 0.1;
199       break;
200
201     case version_info::Channel::DEV:
202       probability = 0.25;
203       break;
204
205     default:
206       probability = 0.5;
207       break;
208   }
209
210   return base::RandDouble() < probability;
211 }
212
213 bool ChromeCrashReporterClient::EnableBreakpadForProcess(
214     const std::string& process_type) {
215   // This is not used by Crashpad (at least on Windows).
216   NOTREACHED();
217   return true;
218 }
219
220 std::wstring ChromeCrashReporterClient::GetWerRuntimeExceptionModule() {
221   // We require that chrome_wer.dll is installed next to chrome_elf.dll. This
222   // approach means we don't need to check for the dll's existence on disk early
223   // in the process lifetime - we never load this dll ourselves - it is only
224   // loaded by WerFault.exe after certain crashes. We do not use base::FilePath
225   // and friends as chrome_elf will eventually not depend on //base.
226
227   wchar_t elf_file[MAX_PATH];
228   DWORD len = GetModuleFileName(CURRENT_MODULE(), elf_file, MAX_PATH);
229   // On error return an empty path to indicate than a module is not to be
230   // registered. This is harmless.
231   if (len == 0 || len == MAX_PATH)
232     return std::wstring();
233
234   wchar_t elf_dir[MAX_PATH];
235   wchar_t* file_start = nullptr;
236   DWORD dir_len = GetFullPathName(elf_file, MAX_PATH, elf_dir, &file_start);
237   if (dir_len == 0 || dir_len > len || !file_start)
238     return std::wstring();
239
240   // file_start points to the start of the filename in the elf_dir buffer.
241   return std::wstring(elf_dir, file_start).append(chrome::kWerDll);
242 }