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

Commit 19a1cfe

Browse files
keianhzobluemarvin
authored andcommitted
Telemetry crash fix (#3644)
1 parent c28ddb2 commit 19a1cfe

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

app/src/common/shared/org/mozilla/vrbrowser/VRBrowserApplication.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.mozilla.vrbrowser.utils.BitmapCache;
2828
import org.mozilla.vrbrowser.utils.EnvironmentsManager;
2929
import org.mozilla.vrbrowser.utils.LocaleUtils;
30+
import org.mozilla.vrbrowser.utils.SystemUtils;
3031

3132
public class VRBrowserApplication extends Application implements AppServicesProvider {
3233

@@ -42,6 +43,17 @@ public class VRBrowserApplication extends Application implements AppServicesProv
4243
@Override
4344
public void onCreate() {
4445
super.onCreate();
46+
47+
if (!SystemUtils.isMainProcess(this)) {
48+
// If this is not the main process then do not continue with the initialization here. Everything that
49+
// follows only needs to be done in our app's main process and should not be done in other processes like
50+
// a GeckoView child process or the crash handling process. Most importantly we never want to end up in a
51+
// situation where we create a GeckoRuntime from the Gecko child process.
52+
return;
53+
}
54+
55+
TelemetryWrapper.init(this, EngineProvider.INSTANCE.getDefaultClient(this));
56+
GleanMetricsService.init(this, EngineProvider.INSTANCE.getDefaultClient(this));
4557
}
4658

4759
protected void onActivityCreate(@NonNull Context activityContext) {
@@ -53,9 +65,6 @@ protected void onActivityCreate(@NonNull Context activityContext) {
5365
mAppExecutors = new AppExecutors();
5466
mBitmapCache = new BitmapCache(this, mAppExecutors.diskIO(), mAppExecutors.mainThread());
5567
mEnvironmentsManager = new EnvironmentsManager(activityContext);
56-
57-
TelemetryWrapper.init(this, EngineProvider.INSTANCE.getDefaultClient(this));
58-
GleanMetricsService.init(this, EngineProvider.INSTANCE.getDefaultClient(this));
5968
}
6069

6170
@Override

app/src/common/shared/org/mozilla/vrbrowser/utils/SystemUtils.java

+14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.mozilla.vrbrowser.utils;
22

3+
import android.app.ActivityManager;
34
import android.app.AlarmManager;
45
import android.app.PendingIntent;
56
import android.content.Context;
67
import android.content.Intent;
8+
import android.os.Process;
79
import android.util.Log;
810

911
import androidx.annotation.NonNull;
@@ -113,4 +115,16 @@ public static void clearCrashFiles(@NonNull Context context, final ArrayList<Str
113115
}
114116
}
115117

118+
public static boolean isMainProcess(@NonNull Context context) {
119+
int pid = Process.myPid();
120+
121+
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
122+
if (activityManager == null) {
123+
return false;
124+
}
125+
126+
return activityManager.getRunningAppProcesses().stream().anyMatch(processInfo ->
127+
processInfo.pid == pid && processInfo.processName.equals(context.getPackageName()));
128+
}
129+
116130
}

app/src/test/java/org/mozilla/vrbrowser/EnvironmentsTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import java.io.File
1717

1818

1919
@RunWith(RobolectricTestRunner::class)
20-
@Config(manifest = Config.NONE)
20+
@Config(manifest = Config.NONE, application = TestApplication::class)
2121
class EnvironmentsTest {
2222

2323
@get:Rule
@@ -27,7 +27,7 @@ class EnvironmentsTest {
2727

2828
@Before
2929
fun setup() {
30-
val app = ApplicationProvider.getApplicationContext<VRBrowserApplication>()
30+
val app = ApplicationProvider.getApplicationContext<TestApplication>()
3131
settingStore = SettingsStore.getInstance(app)
3232
context = ApplicationProvider.getApplicationContext()
3333
}

app/src/test/java/org/mozilla/vrbrowser/GleanMetricsServiceTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ import org.robolectric.annotation.Config
1818

1919

2020
@RunWith(RobolectricTestRunner::class)
21-
@Config(manifest = Config.NONE)
21+
@Config(manifest = Config.NONE, application = TestApplication::class)
2222
class GleanMetricsServiceTest {
2323

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

2727
@Before
2828
fun setup() {
29-
val app = ApplicationProvider.getApplicationContext<VRBrowserApplication>()
29+
val app = ApplicationProvider.getApplicationContext<TestApplication>()
3030
// We use the HttpURLConnectionClient for tests as the GeckoWebExecutor based client needs
3131
// full GeckoRuntime initialization and it crashes in the test environment.
3232
val client = HttpURLConnectionClient()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.mozilla.vrbrowser
2+
3+
import android.app.Application
4+
5+
class TestApplication: Application()

0 commit comments

Comments
 (0)