base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / chromecast / crash / android / crash_handler.cc
bloba73e31352b7b24f46bb45647ee6b7f6b5b41e79e
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"
7 #include <jni.h>
8 #include <string>
10 #include "base/android/jni_android.h"
11 #include "base/android/jni_string.h"
12 #include "base/files/file_path.h"
13 #include "base/logging.h"
14 #include "breakpad/src/client/linux/handler/exception_handler.h"
15 #include "breakpad/src/client/linux/handler/minidump_descriptor.h"
16 #include "chromecast/common/version.h"
17 #include "chromecast/crash/android/cast_crash_reporter_client_android.h"
18 #include "components/crash/app/breakpad_linux.h"
19 #include "components/crash/app/crash_reporter_client.h"
20 #include "content/public/common/content_switches.h"
21 #include "jni/CastCrashHandler_jni.h"
23 namespace {
25 chromecast::CrashHandler* g_crash_handler = NULL;
27 // ExceptionHandler requires a HandlerCallback as a function pointer. This
28 // function exists to proxy into the global CrashHandler instance.
29 bool HandleCrash(const void* /* crash_context */,
30 size_t /* crash_context_size */,
31 void* /* context */) {
32 DCHECK(g_crash_handler);
33 if (g_crash_handler->CanUploadCrashDump()) {
34 g_crash_handler->AttemptUploadCrashDump();
35 } else {
36 g_crash_handler->RemoveCrashDumps();
39 // Let the exception continue to propagate up to the system.
40 return false;
43 } // namespace
45 namespace chromecast {
47 // static
48 void CrashHandler::Initialize(const std::string& process_type,
49 const base::FilePath& log_file_path) {
50 DCHECK(!g_crash_handler);
51 g_crash_handler = new CrashHandler(log_file_path);
52 g_crash_handler->Initialize(process_type);
55 // static
56 bool CrashHandler::GetCrashDumpLocation(base::FilePath* crash_dir) {
57 DCHECK(g_crash_handler);
58 return g_crash_handler->crash_reporter_client_->
59 GetCrashDumpLocation(crash_dir);
62 // static
63 bool CrashHandler::RegisterCastCrashJni(JNIEnv* env) {
64 return RegisterNativesImpl(env);
67 CrashHandler::CrashHandler(const base::FilePath& log_file_path)
68 : log_file_path_(log_file_path),
69 crash_reporter_client_(new CastCrashReporterClientAndroid) {
70 if (!crash_reporter_client_->GetCrashDumpLocation(&crash_dump_path_)) {
71 LOG(ERROR) << "Could not get crash dump location";
73 SetCrashReporterClient(crash_reporter_client_.get());
76 CrashHandler::~CrashHandler() {
77 DCHECK(g_crash_handler);
78 g_crash_handler = NULL;
81 void CrashHandler::Initialize(const std::string& process_type) {
82 if (process_type != switches::kZygoteProcess) {
83 if (process_type.empty()) {
84 // ExceptionHandlers are called on crash in reverse order of
85 // instantiation. This ExceptionHandler will attempt to upload crashes
86 // and the log file written out by the main process.
88 // Dummy MinidumpDescriptor just to start up another ExceptionHandler.
89 google_breakpad::MinidumpDescriptor dummy(crash_dump_path_.value());
90 crash_uploader_.reset(new google_breakpad::ExceptionHandler(
91 dummy, NULL, NULL, NULL, true, -1));
92 crash_uploader_->set_crash_handler(&::HandleCrash);
94 breakpad::InitCrashReporter(process_type);
95 } else {
96 breakpad::InitNonBrowserCrashReporterForAndroid(process_type);
100 UploadCrashDumpsAsync();
103 bool CrashHandler::CanUploadCrashDump() {
104 DCHECK(crash_reporter_client_);
105 return crash_reporter_client_->GetCollectStatsConsent();
108 void CrashHandler::AttemptUploadCrashDump() {
109 VLOG(1) << "Attempting to upload current process crash";
110 JNIEnv* env = base::android::AttachCurrentThread();
111 // Crash dump location
112 base::android::ScopedJavaLocalRef<jstring> crash_dump_path_java =
113 base::android::ConvertUTF8ToJavaString(env,
114 crash_dump_path_.value());
115 // Current log file location
116 base::android::ScopedJavaLocalRef<jstring> log_file_path_java =
117 base::android::ConvertUTF8ToJavaString(env, log_file_path_.value());
118 Java_CastCrashHandler_uploadCurrentProcessDumpSync(
119 env,
120 crash_dump_path_java.obj(),
121 log_file_path_java.obj(),
122 CAST_IS_DEBUG_BUILD);
125 void CrashHandler::UploadCrashDumpsAsync() {
126 JNIEnv* env = base::android::AttachCurrentThread();
127 base::android::ScopedJavaLocalRef<jstring> crash_dump_path_java =
128 base::android::ConvertUTF8ToJavaString(env,
129 crash_dump_path_.value());
130 Java_CastCrashHandler_uploadCrashDumpsAsync(env,
131 crash_dump_path_java.obj(),
132 CAST_IS_DEBUG_BUILD);
135 void CrashHandler::RemoveCrashDumps() {
136 VLOG(1) << "Removing crash dumps instead of uploading";
137 JNIEnv* env = base::android::AttachCurrentThread();
138 base::android::ScopedJavaLocalRef<jstring> crash_dump_path_java =
139 base::android::ConvertUTF8ToJavaString(env,
140 crash_dump_path_.value());
141 Java_CastCrashHandler_removeCrashDumpsSync(
142 env, crash_dump_path_java.obj(), CAST_IS_DEBUG_BUILD);
145 } // namespace chromecast