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

Side by Side Diff: base/android/java/src/org/chromium/base/ContextUtils.java

Issue 1879013002: 🍈 Unify application context usage. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix caller/callee relationship in resource_provider. 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 unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.base; 5 package org.chromium.base;
6 6
7 import android.content.Context; 7 import android.content.Context;
8 8
9 import org.chromium.base.annotations.CalledByNative;
10 import org.chromium.base.annotations.JNINamespace; 9 import org.chromium.base.annotations.JNINamespace;
11 10
12 /** 11 /**
13 * This class provides Android Context utility methods. 12 * This class provides Android Context utility methods.
14 */ 13 */
15 @JNINamespace("base::android") 14 @JNINamespace("base::android")
16 public class ContextUtils { 15 public class ContextUtils {
16 private static final String TAG = "ContextUtils";
17 private static Context sApplicationContext; 17 private static Context sApplicationContext;
18 18
19 /** 19 /**
20 * Get the Android application context. 20 * Get the Android application context.
21 * 21 *
22 * Under normal circumstances there is only one application context in a pro cess, so it's safe 22 * Under normal circumstances there is only one application context in a pro cess, so it's safe
23 * to treat this as a global. In WebView it's possible for more than one app using WebView to be 23 * to treat this as a global. In WebView it's possible for more than one app using WebView to be
24 * running in a single process, but this mechanism is rarely used and this i s not the only 24 * running in a single process, but this mechanism is rarely used and this i s not the only
25 * problem in that scenario, so we don't currently forbid using it as a glob al. 25 * problem in that scenario, so we don't currently forbid using it as a glob al.
26 * 26 *
27 * Do not downcast the context returned by this method to Application (or an y subclass). It may 27 * Do not downcast the context returned by this method to Application (or an y subclass). It may
28 * not be an Application object; it may be wrapped in a ContextWrapper. The only assumption you 28 * not be an Application object; it may be wrapped in a ContextWrapper. The only assumption you
29 * may make is that it is a Context whose lifetime is the same as the lifeti me of the process. 29 * may make is that it is a Context whose lifetime is the same as the lifeti me of the process.
30 */ 30 */
31 public static Context getApplicationContext() { 31 public static Context getApplicationContext() {
32 assert sApplicationContext != null;
33 return sApplicationContext; 32 return sApplicationContext;
34 } 33 }
35 34
36 /** 35 /**
37 * Initialize the Android application context. 36 * Initializes the java application context.
38 * 37 *
39 * Either this or the native equivalent base::android::InitApplicationContex t must be called 38 * This should be called exactly once early on during startup, before native is loaded and
40 * once during startup. JNI bindings must have been initialized, as the cont ext is stored on 39 * before any other clients make use of the application context through this class.
41 * both sides. 40 *
41 * @param appContext The application context.
42 */ 42 */
43 public static void initApplicationContext(Context appContext) { 43 public static void initApplicationContext(Context appContext) {
44 assert appContext != null; 44 // Conceding that occasionally in tests, native is loaded before the bro wser process is
45 assert sApplicationContext == null || sApplicationContext == appContext; 45 // started, in which case the browser process re-sets the application co ntext.
46 if (sApplicationContext != null && sApplicationContext != appContext) {
47 throw new RuntimeException("Attempting to set multiple global applic ation contexts.");
48 }
46 initJavaSideApplicationContext(appContext); 49 initJavaSideApplicationContext(appContext);
47 nativeInitNativeSideApplicationContext(appContext);
48 } 50 }
49 51
50 /** 52 /**
51 * JUnit Robolectric tests run without native code; allow them to set just t he Java-side 53 * Initialize the native Android application context to be the same as the j ava counter-part.
52 * context. Do not use in configurations that actually run on Android!
53 */ 54 */
54 public static void initApplicationContextForJUnitTests(Context appContext) { 55 public static void initApplicationContextForNative() {
56 if (sApplicationContext == null) {
57 throw new RuntimeException("Cannot have native global application co ntext be null.");
58 }
59 nativeInitNativeSideApplicationContext(sApplicationContext);
60 }
61
62 /**
63 * Occasionally tests cannot ensure the application context doesn't change b etween tests (junit)
64 * and sometimes specific tests has its own special needs, initApplicationCo ntext should be used
65 * as much as possible, but this method can be used to override it.
66 *
67 * @param appContext The new application context.
68 */
69 @VisibleForTesting
70 public static void initApplicationContextForTests(Context appContext) {
55 initJavaSideApplicationContext(appContext); 71 initJavaSideApplicationContext(appContext);
56 } 72 }
57 73
58 @CalledByNative
59 private static void initJavaSideApplicationContext(Context appContext) { 74 private static void initJavaSideApplicationContext(Context appContext) {
75 if (appContext == null) {
76 throw new RuntimeException("Global application context cannot be set to null.");
77 }
60 sApplicationContext = appContext; 78 sApplicationContext = appContext;
61 } 79 }
62 80
63 private static native void nativeInitNativeSideApplicationContext(Context ap pContext); 81 private static native void nativeInitNativeSideApplicationContext(Context ap pContext);
64 } 82 }
OLDNEW
« 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