Chromium Code Reviews| 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..b66a9bb4a068331dfe8f2384af471fdfe4a6362a 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,30 @@ public final class AwGeolocationPermissions { |
| } |
| } |
| - public void getAllowed(String origin, final ValueCallback<Boolean> callback) { |
| - boolean allowed = false; |
| + /** |
| + * Synchronous method to get if an origin is set to be allowed. |
| + */ |
| + public boolean isOriginAllowed(String origin) { |
| try { |
| - String key = getOriginKey(origin); |
| - if (key != null) { |
| - allowed = mSharedPreferences.getBoolean(key, false); |
| - } |
| + return mSharedPreferences.getBoolean(getOriginKey(origin), false); |
| } catch (ClassCastException e) { |
| - // Want to return false in this case, do nothing here |
| + mSharedPreferences.edit().remove(getOriginKey(origin)).apply(); |
|
benm (inactive)
2013/02/12 19:40:33
Sorry, I'm still not sure I like this. If the embe
Kristian Monsen
2013/02/12 23:48:35
Done
|
| + return false; |
| } |
| - final boolean finalAllowed = allowed; |
| + } |
| + |
| + /** |
| + * 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) { |
| + final boolean finalAllowed = isOriginAllowed(origin); |
| ThreadUtils.postOnUiThread(new Runnable() { |
| @Override |
| public void run() { |
| @@ -83,6 +146,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 +164,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()) { |