Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Telemetry crash fix (#3644)
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo authored and bluemarvin committed Jul 7, 2020
1 parent c28ddb2 commit 19a1cfe
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.mozilla.vrbrowser.utils.BitmapCache;
import org.mozilla.vrbrowser.utils.EnvironmentsManager;
import org.mozilla.vrbrowser.utils.LocaleUtils;
import org.mozilla.vrbrowser.utils.SystemUtils;

public class VRBrowserApplication extends Application implements AppServicesProvider {

Expand All @@ -42,6 +43,17 @@ public class VRBrowserApplication extends Application implements AppServicesProv
@Override
public void onCreate() {
super.onCreate();

if (!SystemUtils.isMainProcess(this)) {
// If this is not the main process then do not continue with the initialization here. Everything that
// follows only needs to be done in our app's main process and should not be done in other processes like
// a GeckoView child process or the crash handling process. Most importantly we never want to end up in a
// situation where we create a GeckoRuntime from the Gecko child process.
return;
}

TelemetryWrapper.init(this, EngineProvider.INSTANCE.getDefaultClient(this));
GleanMetricsService.init(this, EngineProvider.INSTANCE.getDefaultClient(this));
}

protected void onActivityCreate(@NonNull Context activityContext) {
Expand All @@ -53,9 +65,6 @@ protected void onActivityCreate(@NonNull Context activityContext) {
mAppExecutors = new AppExecutors();
mBitmapCache = new BitmapCache(this, mAppExecutors.diskIO(), mAppExecutors.mainThread());
mEnvironmentsManager = new EnvironmentsManager(activityContext);

TelemetryWrapper.init(this, EngineProvider.INSTANCE.getDefaultClient(this));
GleanMetricsService.init(this, EngineProvider.INSTANCE.getDefaultClient(this));
}

@Override
Expand Down
14 changes: 14 additions & 0 deletions app/src/common/shared/org/mozilla/vrbrowser/utils/SystemUtils.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.mozilla.vrbrowser.utils;

import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Process;
import android.util.Log;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -113,4 +115,16 @@ public static void clearCrashFiles(@NonNull Context context, final ArrayList<Str
}
}

public static boolean isMainProcess(@NonNull Context context) {
int pid = Process.myPid();

final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (activityManager == null) {
return false;
}

return activityManager.getRunningAppProcesses().stream().anyMatch(processInfo ->
processInfo.pid == pid && processInfo.processName.equals(context.getPackageName()));
}

}
4 changes: 2 additions & 2 deletions app/src/test/java/org/mozilla/vrbrowser/EnvironmentsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import java.io.File


@RunWith(RobolectricTestRunner::class)
@Config(manifest = Config.NONE)
@Config(manifest = Config.NONE, application = TestApplication::class)
class EnvironmentsTest {

@get:Rule
Expand All @@ -27,7 +27,7 @@ class EnvironmentsTest {

@Before
fun setup() {
val app = ApplicationProvider.getApplicationContext<VRBrowserApplication>()
val app = ApplicationProvider.getApplicationContext<TestApplication>()
settingStore = SettingsStore.getInstance(app)
context = ApplicationProvider.getApplicationContext()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import org.robolectric.annotation.Config


@RunWith(RobolectricTestRunner::class)
@Config(manifest = Config.NONE)
@Config(manifest = Config.NONE, application = TestApplication::class)
class GleanMetricsServiceTest {

@get:Rule
val gleanRule = GleanTestRule(ApplicationProvider.getApplicationContext())

@Before
fun setup() {
val app = ApplicationProvider.getApplicationContext<VRBrowserApplication>()
val app = ApplicationProvider.getApplicationContext<TestApplication>()
// We use the HttpURLConnectionClient for tests as the GeckoWebExecutor based client needs
// full GeckoRuntime initialization and it crashes in the test environment.
val client = HttpURLConnectionClient()
Expand Down
5 changes: 5 additions & 0 deletions app/src/test/java/org/mozilla/vrbrowser/TestApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.mozilla.vrbrowser

import android.app.Application

class TestApplication: Application()

0 comments on commit 19a1cfe

Please sign in to comment.