bug 595169 - Implement crash reporter client for Android r=mwu,ted ui-r=madhava a=blocking-fennec

This commit is contained in:
Brad Lassey 2010-10-15 23:18:07 -04:00
parent de88b0bc81
commit 98dc671439
7 changed files with 367 additions and 4 deletions

View File

@ -77,5 +77,15 @@
<action android:name="org.mozilla.gecko.restart@MOZ_APP_NAME@" />
</intent-filter>
</receiver>
#if MOZ_CRASHREPORTER
<activity android:name="CrashReporter"
android:label="@MOZ_APP_DISPLAYNAME@ Crash Reporter"
android:icon="@drawable/crash_reporter"
android:process="org.mozilla.@MOZ_APP_NAME@.crashReporter" >
<intent-filter>
<action android:name="org.mozilla.gecko.reportCrash" />
</intent-filter>
</activity>
#endif
</application>
</manifest>

View File

@ -0,0 +1,285 @@
/* -*- Mode: Java; tab-width: 20; indent-tabs-mode: nil; -*-
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Android code.
*
* The Initial Developer of the Original Code is Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Brad Lassey <blassey@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#filter substitution
package org.mozilla.@MOZ_APP_NAME@;
import android.app.*;
import android.content.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import org.mozilla.gecko.*;
import java.util.*;
import java.io.*;
import java.net.*;
import java.nio.channels.*;
public class CrashReporter extends Activity
{
static final String kMiniDumpPathKey = "upload_file_minidump";
static final String kPageURLKey = "URL";
ProgressDialog mProgressDialog;
File mPendingMinidumpFile;
File mPendingExtrasFile;
HashMap<String, String> mExtrasStringMap;
boolean moveFile(File inFile, File outFile)
{
Log.i("GeckoCrashReporter", "moving " + inFile + " to " + outFile);
if (inFile.renameTo(outFile))
return true;
try {
outFile.createNewFile();
Log.i("GeckoCrashReporter", "couldn't rename minidump file");
// so copy it instead
FileChannel inChannel = new FileInputStream(inFile).getChannel();
FileChannel outChannel = new FileOutputStream(outFile).getChannel();
long transferred = inChannel.transferTo(0, inChannel.size(), outChannel);
inChannel.close();
outChannel.close();
if (transferred > 0)
inFile.delete();
} catch (Exception e) {
Log.e("GeckoCrashReporter",
"exception while copying minidump file: ", e);
return false;
}
return true;
}
@Override
public void finish()
{
mProgressDialog.dismiss();
super.finish();
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.crash_reporter);
mProgressDialog = new ProgressDialog(CrashReporter.this);
mProgressDialog.setMessage(getString(R.string.sending_crash_report));
final Button restartButton = (Button) findViewById(R.id.restart);
final Button closeButton = (Button) findViewById(R.id.close);
String passedMinidumpPath = getIntent().getStringExtra("minidumpPath");
File passedMinidumpFile = new File(passedMinidumpPath);
File pendingDir =
new File("/data/data/org.mozilla.@MOZ_APP_NAME@/mozilla/Crash Reports/pending");
pendingDir.mkdirs();
mPendingMinidumpFile = new File(pendingDir, passedMinidumpFile.getName());
moveFile(passedMinidumpFile, mPendingMinidumpFile);
File extrasFile = new File(passedMinidumpPath.replaceAll(".dmp", ".extra"));
mPendingExtrasFile = new File(pendingDir, extrasFile.getName());
moveFile(extrasFile, mPendingExtrasFile);
mExtrasStringMap = new HashMap<String, String>();
readStringsFromFile(mPendingExtrasFile.getPath(), mExtrasStringMap);
}
public void onCloseClick(View v)
{
mProgressDialog.show();
new Thread(new Runnable() {
public void run() {
sendReport(mPendingMinidumpFile, mExtrasStringMap, mPendingExtrasFile);
}}).start();
}
public void onRestartClick(View v)
{
doRestart();
mProgressDialog.show();
new Thread(new Runnable() {
public void run() {
sendReport(mPendingMinidumpFile, mExtrasStringMap, mPendingExtrasFile);
}}).start();
}
boolean readStringsFromFile(String filePath, Map stringMap)
{
try {
BufferedReader reader = new BufferedReader(
new FileReader(filePath));
return readStringsFromReader(reader, stringMap);
} catch (Exception e) {
Log.e("GeckoCrashReporter", "exception while reading strings: ", e);
return false;
}
}
boolean readStringsFromReader(BufferedReader reader, Map stringMap)
throws java.io.IOException
{
String line;
while ((line = reader.readLine()) != null) {
int equalsPos = -1;
if ((equalsPos = line.indexOf('=')) != -1) {
String key = line.substring(0, equalsPos);
String val = unescape(line.substring(equalsPos + 1));
stringMap.put(key, val);
}
}
reader.close();
return true;
}
String generateBoundary()
{
// Generate some random numbers to fill out the boundary
int r0 = (int)((double)Integer.MAX_VALUE * Math.random());
int r1 = (int)((double)Integer.MAX_VALUE * Math.random());
return String.format("---------------------------%08X%08X", r0, r1);
}
void sendPart(OutputStream os, String boundary, String name, String data)
throws IOException
{
os.write(("--" + boundary + "\r\n" +
"Content-Disposition: form-data; name=\"" +
name + "\"\r\n\r\n" +
data + "\r\n").getBytes());
}
void sendFile(OutputStream os, String boundary, String name, File file)
throws IOException
{
os.write(("--" + boundary + "\r\n" +
"Content-Disposition: form-data; " +
"name=\"" + name + "\"; " +
"filename=\"" + file.getName() + "\"\r\n" +
"Content-Type: application/octet-stream\r\n" +
"\r\n").getBytes());
FileChannel fc =
new FileInputStream(file).getChannel();
fc.transferTo(0, fc.size(), Channels.newChannel(os));
fc.close();
}
void sendReport(File minidumpFile, Map<String, String> extras,
File extrasFile)
{
Log.i("GeckoCrashReport", "sendReport: " + minidumpFile.getPath());
final CheckBox sendReportCheckbox = (CheckBox) findViewById(R.id.send_report);
final CheckBox includeURLCheckbox = (CheckBox) findViewById(R.id.include_url);
if (!sendReportCheckbox.isChecked())
finish();
String spec = extras.get("ServerURL");
if (spec == null)
finish();
Log.i("GeckoCrashReport", "server url: " + spec);
try {
URL url = new URL(spec);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
String boundary = generateBoundary();
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
OutputStream os = conn.getOutputStream();
Iterator<String> keys = extras.keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
if (key.equals(kPageURLKey)) {
if (includeURLCheckbox.isChecked())
sendPart(os, boundary, key, extras.get(key));
} else if (!key.equals("ServerURL")){
sendPart(os, boundary, key, extras.get(key));
}
}
sendFile(os, boundary, kMiniDumpPathKey, minidumpFile);
os.write(("\r\n--" + boundary + "--\r\n").getBytes());
os.flush();
os.close();
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
HashMap<String, String> responseMap = new HashMap<String, String>();
readStringsFromReader(br, responseMap);
if (conn.getResponseCode() == conn.HTTP_OK) {
File submittedDir = new File(
"/data/data/org.mozilla.@MOZ_APP_NAME@/mozilla/Crash Reports/submitted");
submittedDir.mkdirs();
minidumpFile.delete();
extrasFile.delete();
String crashid = responseMap.get("CrashID");
File file = new File(submittedDir, crashid + ".txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write("Crash ID: ".getBytes());
fos.write(crashid.getBytes());
fos.close();
}
} catch (IOException e) {
Log.e("GeckoCrashReporter", "exception during send: ", e);
}
finish();
}
void doRestart()
{
try {
String action = "org.mozilla.gecko.restart@MOZ_APP_NAME@";
String amCmd = "/system/bin/am broadcast -a " + action +
" -n org.mozilla.@MOZ_APP_NAME@/org.mozilla.@MOZ_APP_NAME@.Restarter";
Log.i("GeckoCrashReporter", amCmd);
Runtime.getRuntime().exec(amCmd);
} catch (Exception e) {
Log.i("GeckoCrashReporter", e.toString());
}
}
public String unescape(String string)
{
return string.replaceAll("\\\\", "\\").replaceAll("\\n", "\n")
.replaceAll("\\t", "\t");
}
}

View File

@ -71,6 +71,7 @@ DEFINES += \
-DMOZ_APP_VERSION=$(MOZ_APP_VERSION) \
-DMOZ_CHILD_PROCESS_NAME=$(MOZ_CHILD_PROCESS_NAME) \
-DMOZ_MIN_CPU_VERSION=$(MIN_CPU_VERSION) \
-DMOZ_CRASHREPORTER=$(MOZ_CRASHREPORTER) \
$(NULL)
GARBAGE += \
@ -82,7 +83,7 @@ GARBAGE += \
R.java \
$(NULL)
GARBAGE_DIRS += classes
GARBAGE_DIRS += classes res
# Bug 567884 - Need a way to find appropriate icons during packaging
ifeq ($(MOZ_APP_NAME),fennec)
@ -95,7 +96,7 @@ endif
RES_LAYOUT = \
res/layout/notification_progress.xml \
res/layout/notification_progress_text.xml
res/layout/notification_progress_text.xml \
$(NULL)
JAVA_CLASSPATH = $(ANDROID_SDK)/android.jar
@ -103,6 +104,13 @@ JAVA_CLASSPATH = $(ANDROID_SDK)/android.jar
DEFAULT_BRANDPATH = $(DEPTH)/$(MOZ_BRANDING_DIRECTORY)/locales/en-US/brand.dtd
DEFAULT_STRINGSPATH = android_strings.dtd
ifdef MOZ_CRASHREPORTER
PROCESSEDJAVAFILES += CrashReporter.java
MOZ_ANDROID_DRAWABLES += embedding/android/resources/drawable/crash_reporter.png
RES_LAYOUT += res/layout/crash_reporter.xml
endif
include $(topsrcdir)/config/rules.mk
# Override the Java settings with some specific android settings
@ -127,13 +135,11 @@ res/drawable-hdpi/icon.png: $(MOZ_APP_ICON)
$(NSINSTALL) -D res/drawable-hdpi
cp $(ICON_PATH_HDPI) $@
ifdef MOZ_ANDROID_DRAWABLES
RES_DRAWABLE = $(addprefix res/drawable/,$(notdir $(MOZ_ANDROID_DRAWABLES)))
$(RES_DRAWABLE): $(addprefix $(topsrcdir)/,$(MOZ_ANDROID_DRAWABLES))
$(NSINSTALL) -D res/drawable
$(NSINSTALL) $^ res/drawable/
endif
$(RES_LAYOUT): $(subst res/,$(srcdir)/resources/,$(RES_LAYOUT))
$(NSINSTALL) -D res/layout

View File

@ -2,3 +2,10 @@
<!ENTITY incompatable_cpu_error "This device does not meet the minimum system requirements for &brandShortName;.">
<!ENTITY no_space_to_start_error "There is not enough space available for &brandShortName; to start.">
<!ENTITY error_loading_file "An error occurred when trying to load files required to run &brandShortName;">
<!ENTITY crash_message "&brandShortName; has crashed. Your tabs should be listed on the &brandShortName; Start page when you restart.">
<!ENTITY crash_help_message "Please help us fix this problem!">
<!ENTITY crash_send_report_message "Send Mozilla a crash report">
<!ENTITY crash_include_url "Include page address">
<!ENTITY crash_close_label "Close">
<!ENTITY crash_restart_label "Restart &brandShortName;">
<!ENTITY sending_crash_report "Sending crash report\u2026">

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10px" >
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10px"
android:textStyle="bold"
android:text="@string/crash_message"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10px"
android:text="@string/crash_help_message"/>
<CheckBox android:id="@+id/send_report"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/crash_send_report_message" />
<CheckBox android:id="@+id/include_url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/crash_include_url" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10px"
android:gravity="center_horizontal" >
<Button android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10px"
android:minWidth="120sp"
android:onClick="onCloseClick"
android:text="@string/crash_close_label" />
<Button android:id="@+id/restart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:minWidth="120sp"
android:onClick="onRestartClick"
android:text="@string/crash_restart_label" />
</LinearLayout>
</LinearLayout>

View File

@ -9,4 +9,11 @@
<string name="incompatable_cpu_error">&incompatable_cpu_error;</string>
<string name="no_space_to_start_error">&no_space_to_start_error;</string>
<string name="error_loading_file">&error_loading_file;</string>
<string name="crash_message">&crash_message;</string>
<string name="crash_help_message">&crash_help_message;</string>
<string name="crash_send_report_message">&crash_send_report_message;</string>
<string name="crash_include_url">&crash_include_url;</string>
<string name="crash_close_label">&crash_close_label;</string>
<string name="crash_restart_label">&crash_restart_label;</string>
<string name="sending_crash_report">&sending_crash_report;</string>
</resources>