1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chromecast/crash/android/crash_handler.h"
11 #include "base/android/jni_android.h"
12 #include "base/android/jni_string.h"
13 #include "base/files/file_path.h"
14 #include "base/logging.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "breakpad/src/client/linux/handler/exception_handler.h"
17 #include "breakpad/src/client/linux/handler/minidump_descriptor.h"
18 #include "chromecast/common/version.h"
19 #include "chromecast/crash/android/cast_crash_reporter_client_android.h"
20 #include "components/crash/app/breakpad_linux.h"
21 #include "components/crash/app/crash_reporter_client.h"
22 #include "content/public/common/content_switches.h"
23 #include "jni/CastCrashHandler_jni.h"
27 chromecast::CrashHandler
* g_crash_handler
= NULL
;
29 // ExceptionHandler requires a HandlerCallback as a function pointer. This
30 // function exists to proxy into the global CrashHandler instance.
31 bool HandleCrash(const void* /* crash_context */,
32 size_t /* crash_context_size */,
33 void* /* context */) {
34 DCHECK(g_crash_handler
);
35 g_crash_handler
->UploadCrashDumps();
37 // Let the exception continue to propagate up to the system.
41 // Debug builds: always to crash-staging
42 // Release builds: only to crash-staging for local/invalid build numbers
43 bool UploadCrashToStaging() {
44 #if CAST_IS_DEBUG_BUILD
48 if (base::StringToInt(CAST_BUILD_INCREMENTAL
, &build_number
))
49 return build_number
== 0;
56 namespace chromecast
{
59 void CrashHandler::Initialize(const std::string
& process_type
,
60 const base::FilePath
& log_file_path
) {
61 DCHECK(!g_crash_handler
);
62 g_crash_handler
= new CrashHandler(log_file_path
);
63 g_crash_handler
->Initialize(process_type
);
67 bool CrashHandler::GetCrashDumpLocation(base::FilePath
* crash_dir
) {
68 DCHECK(g_crash_handler
);
69 return g_crash_handler
->crash_reporter_client_
->
70 GetCrashDumpLocation(crash_dir
);
74 bool CrashHandler::RegisterCastCrashJni(JNIEnv
* env
) {
75 return RegisterNativesImpl(env
);
78 CrashHandler::CrashHandler(const base::FilePath
& log_file_path
)
79 : log_file_path_(log_file_path
),
80 crash_reporter_client_(new CastCrashReporterClientAndroid
) {
81 if (!crash_reporter_client_
->GetCrashDumpLocation(&crash_dump_path_
)) {
82 LOG(ERROR
) << "Could not get crash dump location";
84 SetCrashReporterClient(crash_reporter_client_
.get());
87 CrashHandler::~CrashHandler() {
88 DCHECK(g_crash_handler
);
89 g_crash_handler
= NULL
;
92 void CrashHandler::Initialize(const std::string
& process_type
) {
93 if (process_type
.empty()) {
96 // ExceptionHandlers are called on crash in reverse order of
97 // instantiation. This ExceptionHandler will attempt to upload crashes
98 // and the log file written out by the main process.
100 // Dummy MinidumpDescriptor just to start up another ExceptionHandler.
101 google_breakpad::MinidumpDescriptor
dummy(crash_dump_path_
.value());
102 crash_uploader_
.reset(new google_breakpad::ExceptionHandler(
103 dummy
, NULL
, NULL
, NULL
, true, -1));
104 crash_uploader_
->set_crash_handler(&::HandleCrash
);
106 breakpad::InitCrashReporter(process_type
);
111 if (process_type
!= switches::kZygoteProcess
) {
112 breakpad::InitNonBrowserCrashReporterForAndroid(process_type
);
116 void CrashHandler::InitializeUploader() {
117 JNIEnv
* env
= base::android::AttachCurrentThread();
118 base::android::ScopedJavaLocalRef
<jstring
> crash_dump_path_java
=
119 base::android::ConvertUTF8ToJavaString(env
,
120 crash_dump_path_
.value());
121 Java_CastCrashHandler_initializeUploader(
122 env
, crash_dump_path_java
.obj(), UploadCrashToStaging());
125 bool CrashHandler::CanUploadCrashDump() {
126 DCHECK(crash_reporter_client_
);
127 return crash_reporter_client_
->GetCollectStatsConsent();
130 void CrashHandler::UploadCrashDumps() {
131 VLOG(1) << "Attempting to upload current process crash";
133 if (CanUploadCrashDump()) {
134 JNIEnv
* env
= base::android::AttachCurrentThread();
135 // Current log file location
136 base::android::ScopedJavaLocalRef
<jstring
> log_file_path_java
=
137 base::android::ConvertUTF8ToJavaString(env
, log_file_path_
.value());
138 Java_CastCrashHandler_uploadCrashDumps(env
, log_file_path_java
.obj());
140 VLOG(1) << "Removing crash dumps instead of uploading";
141 JNIEnv
* env
= base::android::AttachCurrentThread();
142 Java_CastCrashHandler_removeCrashDumps(env
);
146 } // namespace chromecast