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

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: Reduce scope again. 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; 9 import org.chromium.base.annotations.CalledByNative;
10 import org.chromium.base.annotations.JNINamespace; 10 import org.chromium.base.annotations.JNINamespace;
(...skipping 16 matching lines...) Expand all
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; 32 assert sApplicationContext != null;
33 return sApplicationContext; 33 return sApplicationContext;
34 } 34 }
35 35
36 /** 36 /**
37 * Initialize the Android application context. 37 * Initializes the java application context.
38 *
39 * @param appContext The application context.
40 */
41 public static void setApplicationContext(Context appContext) {
42 // TODO(wnwen): Figure out how to fix this for JUnit tests. Currently th ey fail this assert
Torne 2016/04/13 15:38:38 Why weren't the tests having this problem before?
Peter Wen 2016/04/13 18:49:20 It's due to BaseChromiumApplication setting this u
43 // due to repeatedly calling onCreate in the base application and ke eping global state.
44 //assert sApplicationContext == null || sApplicationContext == appContex t;
45 initJavaSideApplicationContext(appContext);
46 }
47
48 /**
49 * Initialize the native Android application context to be the same as the j ava counter-part.
38 * 50 *
39 * Either this or the native equivalent base::android::InitApplicationContex t must be called 51 * Either this or the native equivalent base::android::InitApplicationContex t must be called
Torne 2016/04/13 15:38:38 Not sure the comment here makes sense any more; it
Peter Wen 2016/04/13 18:49:20 Updated comments.
40 * once during startup. JNI bindings must have been initialized, as the cont ext is stored on 52 * once during startup. JNI bindings must have been initialized, as the cont ext is stored on
41 * both sides. 53 * both sides.
42 */ 54 */
55 public static void initApplicationContextForNative() {
56 assert sApplicationContext != null;
57 nativeInitNativeSideApplicationContext(sApplicationContext);
58 }
59
60 // TODO(wnwen): Remove when all callers are migrated to new methods as neede d.
43 public static void initApplicationContext(Context appContext) { 61 public static void initApplicationContext(Context appContext) {
44 assert appContext != null; 62 setApplicationContext(appContext);
45 assert sApplicationContext == null || sApplicationContext == appContext; 63 initApplicationContextForNative();
46 initJavaSideApplicationContext(appContext);
47 nativeInitNativeSideApplicationContext(appContext);
48 } 64 }
49 65
50 /** 66 /**
51 * JUnit Robolectric tests run without native code; allow them to set just t he Java-side 67 * JUnit Robolectric tests run without native code; allow them to set just t he Java-side
52 * context. Do not use in configurations that actually run on Android! 68 * context. Do not use in configurations that actually run on Android!
69 *
70 * TODO(wnwen): These calls can now be safely removed.
53 */ 71 */
54 public static void initApplicationContextForJUnitTests(Context appContext) { 72 public static void initApplicationContextForJUnitTests(Context appContext) {
55 initJavaSideApplicationContext(appContext); 73 initJavaSideApplicationContext(appContext);
56 } 74 }
57 75
76
58 @CalledByNative 77 @CalledByNative
59 private static void initJavaSideApplicationContext(Context appContext) { 78 private static void initJavaSideApplicationContext(Context appContext) {
60 sApplicationContext = appContext; 79 sApplicationContext = appContext;
61 } 80 }
62 81
63 private static native void nativeInitNativeSideApplicationContext(Context ap pContext); 82 private static native void nativeInitNativeSideApplicationContext(Context ap pContext);
64 } 83 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698