Index: android_webview/java/src/org/chromium/android_webview/AwGeolocationPermissions.java |
diff --git a/android_webview/java/src/org/chromium/android_webview/AwGeolocationPermissions.java b/android_webview/java/src/org/chromium/android_webview/AwGeolocationPermissions.java |
index 133f5970eb5a12a778701aba59b3f329e39941e0..229bf20ccd02673c1474b6ef63098484bc45eb4b 100644 |
--- a/android_webview/java/src/org/chromium/android_webview/AwGeolocationPermissions.java |
+++ b/android_webview/java/src/org/chromium/android_webview/AwGeolocationPermissions.java |
@@ -24,10 +24,51 @@ public final class AwGeolocationPermissions { |
AwGeolocationPermissions.class.getCanonicalName() + "%"; |
private final SharedPreferences mSharedPreferences; |
+ private static AwGeolocationPermissions sInstance; |
+ |
+ // TODO(kristianm): Rewrite when AwBrowserContext has landed in |
+ // CL: https://codereview.chromium.org/12208099/ |
public AwGeolocationPermissions(SharedPreferences sharedPreferences) { |
mSharedPreferences = sharedPreferences; |
+ setInstance(this); |
+ } |
+ |
+ private static void setInstance(AwGeolocationPermissions instance) { |
+ synchronized (AwGeolocationPermissions.class) { |
+ sInstance = instance; |
+ } |
+ } |
+ |
+ /** |
+ * Get the static instance after it has been created |
+ */ |
+ public static AwGeolocationPermissions getInstance() { |
+ synchronized (AwGeolocationPermissions.class) { |
+ if (sInstance == null) { |
+ throw new IllegalStateException("This should only be called after createInstance."); |
+ } |
+ } |
+ return sInstance; |
+ } |
+ |
+ /** |
+ * Create the static instance of this class |
+ */ |
+ public static AwGeolocationPermissions createInstance( |
+ SharedPreferences sharedPreferences) { |
+ synchronized (AwGeolocationPermissions.class) { |
+ if (sInstance != null) { |
+ throw new IllegalStateException("This should only be called once."); |
+ } |
+ // sInstance set in the constructor |
+ new AwGeolocationPermissions(sharedPreferences); |
+ return sInstance; |
+ } |
} |
+ /** |
+ * Set one origin to be allowed. |
+ */ |
public void allow(String origin) { |
String key = getOriginKey(origin); |
if (key != null) { |
@@ -35,6 +76,9 @@ public final class AwGeolocationPermissions { |
} |
} |
+ /** |
+ * Set one origin to be denied. |
+ */ |
public void deny(String origin) { |
String key = getOriginKey(origin); |
if (key != null) { |
@@ -42,6 +86,9 @@ public final class AwGeolocationPermissions { |
} |
} |
+ /** |
+ * Clear the stored permission for a particular origin. |
+ */ |
public void clear(String origin) { |
String key = getOriginKey(origin); |
if (key != null) { |
@@ -49,6 +96,9 @@ public final class AwGeolocationPermissions { |
} |
} |
+ /** |
+ * Clear stored permissions for all origins. |
+ */ |
public void clearAll() { |
SharedPreferences.Editor editor = null; |
for (String name : mSharedPreferences.getAll().keySet()) { |
@@ -64,17 +114,25 @@ public final class AwGeolocationPermissions { |
} |
} |
+ /** |
+ * Synchronous method to get if an origin is set to be allowed. |
+ */ |
+ public boolean isOriginAllowed(String origin) { |
+ return mSharedPreferences.getBoolean(getOriginKey(origin), false); |
+ } |
+ |
+ /** |
+ * Returns true if the origin is either set to allowed or denied. |
+ */ |
+ public boolean hasOrigin(String origin) { |
+ return mSharedPreferences.contains(getOriginKey(origin)); |
+ } |
+ |
+ /** |
+ * Asynchronous method to get if an origin set to be allowed. |
+ */ |
public void getAllowed(String origin, final ValueCallback<Boolean> callback) { |
- boolean allowed = false; |
- try { |
- String key = getOriginKey(origin); |
- if (key != null) { |
- allowed = mSharedPreferences.getBoolean(key, false); |
- } |
- } catch (ClassCastException e) { |
- // Want to return false in this case, do nothing here |
- } |
- final boolean finalAllowed = allowed; |
+ final boolean finalAllowed = isOriginAllowed(origin); |
ThreadUtils.postOnUiThread(new Runnable() { |
@Override |
public void run() { |
@@ -83,6 +141,9 @@ public final class AwGeolocationPermissions { |
}); |
} |
+ /** |
+ * Async method to get the domains currently allowed or denied. |
+ */ |
public void getOrigins(final ValueCallback<Set<String>> callback) { |
final Set<String> origins = new HashSet<String>(); |
for (String name : mSharedPreferences.getAll().keySet()) { |
@@ -98,6 +159,9 @@ public final class AwGeolocationPermissions { |
}); |
} |
+ /** |
+ * Get the domain of an URL using the GURL library. |
+ */ |
private String getOriginKey(String url) { |
String origin = GURLUtils.getOrigin(url); |
if (origin.isEmpty()) { |