Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(311)

Unified Diff: base/android/java/src/org/chromium/base/ContextUtils.java

Issue 1917193003: Revert of 🍈 Unify application context usage. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/android/java/src/org/chromium/base/ContextUtils.java
diff --git a/base/android/java/src/org/chromium/base/ContextUtils.java b/base/android/java/src/org/chromium/base/ContextUtils.java
index 95cdc4015348dd0af75da09e14d0955f82f22edb..51adcff8f67375a4bf0507fedfeecf72daa07396 100644
--- a/base/android/java/src/org/chromium/base/ContextUtils.java
+++ b/base/android/java/src/org/chromium/base/ContextUtils.java
@@ -6,6 +6,7 @@
import android.content.Context;
+import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
/**
@@ -13,7 +14,6 @@
*/
@JNINamespace("base::android")
public class ContextUtils {
- private static final String TAG = "ContextUtils";
private static Context sApplicationContext;
/**
@@ -29,52 +29,34 @@
* may make is that it is a Context whose lifetime is the same as the lifetime of the process.
*/
public static Context getApplicationContext() {
+ assert sApplicationContext != null;
return sApplicationContext;
}
/**
- * Initializes the java application context.
+ * Initialize the Android application context.
*
- * This should be called exactly once early on during startup, before native is loaded and
- * before any other clients make use of the application context through this class.
- *
- * @param appContext The application context.
+ * Either this or the native equivalent base::android::InitApplicationContext must be called
+ * once during startup. JNI bindings must have been initialized, as the context is stored on
+ * both sides.
*/
public static void initApplicationContext(Context appContext) {
- // Conceding that occasionally in tests, native is loaded before the browser process is
- // started, in which case the browser process re-sets the application context.
- if (sApplicationContext != null && sApplicationContext != appContext) {
- throw new RuntimeException("Attempting to set multiple global application contexts.");
- }
+ assert appContext != null;
+ assert sApplicationContext == null || sApplicationContext == appContext;
+ initJavaSideApplicationContext(appContext);
+ nativeInitNativeSideApplicationContext(appContext);
+ }
+
+ /**
+ * JUnit Robolectric tests run without native code; allow them to set just the Java-side
+ * context. Do not use in configurations that actually run on Android!
+ */
+ public static void initApplicationContextForJUnitTests(Context appContext) {
initJavaSideApplicationContext(appContext);
}
- /**
- * Initialize the native Android application context to be the same as the java counter-part.
- */
- public static void initApplicationContextForNative() {
- if (sApplicationContext == null) {
- throw new RuntimeException("Cannot have native global application context be null.");
- }
- nativeInitNativeSideApplicationContext(sApplicationContext);
- }
-
- /**
- * Occasionally tests cannot ensure the application context doesn't change between tests (junit)
- * and sometimes specific tests has its own special needs, initApplicationContext should be used
- * as much as possible, but this method can be used to override it.
- *
- * @param appContext The new application context.
- */
- @VisibleForTesting
- public static void initApplicationContextForTests(Context appContext) {
- initJavaSideApplicationContext(appContext);
- }
-
+ @CalledByNative
private static void initJavaSideApplicationContext(Context appContext) {
- if (appContext == null) {
- throw new RuntimeException("Global application context cannot be set to null.");
- }
sApplicationContext = appContext;
}
« no previous file with comments | « base/android/context_utils.cc ('k') | base/android/java/src/org/chromium/base/library_loader/LibraryLoader.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698