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..a77b8dcfbf5780ae9c3c45207768567ca4da077c 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,44 @@ public final class AwGeolocationPermissions { |
| AwGeolocationPermissions.class.getCanonicalName() + "%"; |
| private final SharedPreferences mSharedPreferences; |
| + private static AwGeolocationPermissions sInstance; |
| + |
| + // TODO(kristianm): Make this private once framework is updated to use getInstance |
| public AwGeolocationPermissions(SharedPreferences sharedPreferences) { |
| mSharedPreferences = sharedPreferences; |
| + sInstance = this; |
| + } |
| + |
| + /** |
| + * 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 +69,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 +79,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 +89,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 +107,29 @@ public final class AwGeolocationPermissions { |
| } |
| } |
| - public void getAllowed(String origin, final ValueCallback<Boolean> callback) { |
| - boolean allowed = false; |
| + /** |
| + * Sync method to get if an origin is set to be allowed. |
|
benm (inactive)
2013/02/08 13:25:19
nit: Synchronous
Kristian Monsen
2013/02/12 18:53:03
Done.
|
| + */ |
| + 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 |
| + return false; |
|
benm (inactive)
2013/02/08 13:25:19
How could we ever get a pref with the same name bu
Kristian Monsen
2013/02/12 18:53:03
Agree, but I also don't think it is right to throw
|
| } |
| - 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)); |
| + } |
| + |
| + /** |
| + * Async method to get if an origin set to be allowed. |
|
benm (inactive)
2013/02/08 13:25:19
nit: Asynchronous
Kristian Monsen
2013/02/12 18:53:03
Done.
|
| + */ |
| + public void getAllowed(String origin, final ValueCallback<Boolean> callback) { |
| + final boolean finalAllowed = isOriginAllowed(origin); |
| ThreadUtils.postOnUiThread(new Runnable() { |
| @Override |
| public void run() { |
| @@ -83,6 +138,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 +156,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()) { |