From 19a1cfe1abd7ddede683b7ec744c0038c32fd2ff Mon Sep 17 00:00:00 2001 From: Manuel Martin Date: Tue, 7 Jul 2020 20:01:30 +0200 Subject: [PATCH] Telemetry crash fix (#3644) --- .../mozilla/vrbrowser/VRBrowserApplication.java | 15 ++++++++++++--- .../org/mozilla/vrbrowser/utils/SystemUtils.java | 14 ++++++++++++++ .../org/mozilla/vrbrowser/EnvironmentsTest.kt | 4 ++-- .../mozilla/vrbrowser/GleanMetricsServiceTest.kt | 4 ++-- .../java/org/mozilla/vrbrowser/TestApplication.kt | 5 +++++ 5 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 app/src/test/java/org/mozilla/vrbrowser/TestApplication.kt diff --git a/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserApplication.java b/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserApplication.java index 21519c3d0..51357302b 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserApplication.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/VRBrowserApplication.java @@ -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 { @@ -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) { @@ -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 diff --git a/app/src/common/shared/org/mozilla/vrbrowser/utils/SystemUtils.java b/app/src/common/shared/org/mozilla/vrbrowser/utils/SystemUtils.java index 959cc8a04..eb0cd5c4a 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/utils/SystemUtils.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/utils/SystemUtils.java @@ -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; @@ -113,4 +115,16 @@ public static void clearCrashFiles(@NonNull Context context, final ArrayList + processInfo.pid == pid && processInfo.processName.equals(context.getPackageName())); + } + } diff --git a/app/src/test/java/org/mozilla/vrbrowser/EnvironmentsTest.kt b/app/src/test/java/org/mozilla/vrbrowser/EnvironmentsTest.kt index 83730fb6e..d60cd8dc3 100644 --- a/app/src/test/java/org/mozilla/vrbrowser/EnvironmentsTest.kt +++ b/app/src/test/java/org/mozilla/vrbrowser/EnvironmentsTest.kt @@ -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 @@ -27,7 +27,7 @@ class EnvironmentsTest { @Before fun setup() { - val app = ApplicationProvider.getApplicationContext() + val app = ApplicationProvider.getApplicationContext() settingStore = SettingsStore.getInstance(app) context = ApplicationProvider.getApplicationContext() } diff --git a/app/src/test/java/org/mozilla/vrbrowser/GleanMetricsServiceTest.kt b/app/src/test/java/org/mozilla/vrbrowser/GleanMetricsServiceTest.kt index 338ce9a23..f49d49186 100644 --- a/app/src/test/java/org/mozilla/vrbrowser/GleanMetricsServiceTest.kt +++ b/app/src/test/java/org/mozilla/vrbrowser/GleanMetricsServiceTest.kt @@ -18,7 +18,7 @@ import org.robolectric.annotation.Config @RunWith(RobolectricTestRunner::class) -@Config(manifest = Config.NONE) +@Config(manifest = Config.NONE, application = TestApplication::class) class GleanMetricsServiceTest { @get:Rule @@ -26,7 +26,7 @@ class GleanMetricsServiceTest { @Before fun setup() { - val app = ApplicationProvider.getApplicationContext() + val app = ApplicationProvider.getApplicationContext() // 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() diff --git a/app/src/test/java/org/mozilla/vrbrowser/TestApplication.kt b/app/src/test/java/org/mozilla/vrbrowser/TestApplication.kt new file mode 100644 index 000000000..be6ff5d3c --- /dev/null +++ b/app/src/test/java/org/mozilla/vrbrowser/TestApplication.kt @@ -0,0 +1,5 @@ +package org.mozilla.vrbrowser + +import android.app.Application + +class TestApplication: Application() \ No newline at end of file