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 |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d99d00eb1d2ed49259d4c0829a49d983b96f3a31 |
--- /dev/null |
+++ b/base/android/java/src/org/chromium/base/ContextUtils.java |
@@ -0,0 +1,46 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.base; |
+ |
+import android.content.Context; |
+ |
+import org.chromium.base.annotations.JNINamespace; |
+ |
+/** |
+ * This class provides Android Context utility methods. |
+ */ |
+@JNINamespace("base::android") |
+public class ContextUtils { |
+ private static Context sApplicationContext; |
+ |
+ /** |
+ * Get the Android application context. |
+ * |
+ * Under normal circumstances there is only one application context in a process, so it's safe |
+ * to treat this as a global. In WebView it's possible for more than one app using WebView to be |
+ * running in a single process, but this mechanism is rarely used and this is not the only |
+ * problem in that scenario, so we don't currently forbid using it as a global. |
+ * |
+ * Do not downcast the context returned by this method to Application (or any subclass). It may |
+ * not be an Application object, it may be wrapped in a ContextWrapper. The only assumption you |
+ * may make is that it is a Context whose lifetime is the same as the lifetime of the process. |
+ */ |
+ public static Context getApplicationContext() { |
+ return sApplicationContext; |
+ } |
+ |
+ /** |
+ * Initialize the Android application context. |
+ * |
+ * This must be called once during startup. It requires that JNI bindings have been initialized, |
+ * but should be called before general JNI functions are used. |
+ */ |
+ public static void initApplicationContext(Context appContext) { |
+ sApplicationContext = appContext; |
+ nativeInitApplicationContext(appContext); |
+ } |
+ |
+ private static native void nativeInitApplicationContext(Context appContext); |
+} |