1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.xwalk.core;
7 import android.content.SharedPreferences;
8 import android.webkit.ValueCallback;
10 import java.util.HashSet;
13 import org.chromium.base.ThreadUtils;
14 import org.chromium.net.GURLUtils;
17 * This class is used to manage permissions for the WebView's Geolocation JavaScript API.
19 * Callbacks are posted on the UI thread.
21 public final class XWalkGeolocationPermissions {
23 * Callback interface used by the browser to report a Geolocation permission
24 * state set by the user in response to a permissions prompt.
26 public interface Callback {
27 public void invoke(String origin, boolean allow, boolean remember);
30 private static final String PREF_PREFIX =
31 XWalkGeolocationPermissions.class.getCanonicalName() + "%";
32 private final SharedPreferences mSharedPreferences;
34 public XWalkGeolocationPermissions(SharedPreferences sharedPreferences) {
35 mSharedPreferences = sharedPreferences;
39 * Set one origin to be allowed.
41 public void allow(String origin) {
42 String key = getOriginKey(origin);
44 mSharedPreferences.edit().putBoolean(key, true).apply();
49 * Set one origin to be denied.
51 public void deny(String origin) {
52 String key = getOriginKey(origin);
54 mSharedPreferences.edit().putBoolean(key, false).apply();
59 * Clear the stored permission for a particular origin.
61 public void clear(String origin) {
62 String key = getOriginKey(origin);
64 mSharedPreferences.edit().remove(key).apply();
69 * Clear stored permissions for all origins.
71 public void clearAll() {
72 SharedPreferences.Editor editor = null;
73 for (String name : mSharedPreferences.getAll().keySet()) {
74 if (name.startsWith(PREF_PREFIX)) {
76 editor = mSharedPreferences.edit();
87 * Synchronous method to get if an origin is set to be allowed.
89 public boolean isOriginAllowed(String origin) {
90 return mSharedPreferences.getBoolean(getOriginKey(origin), false);
94 * Returns true if the origin is either set to allowed or denied.
96 public boolean hasOrigin(String origin) {
97 return mSharedPreferences.contains(getOriginKey(origin));
101 * Asynchronous method to get if an origin set to be allowed.
103 public void getAllowed(String origin, final ValueCallback<Boolean> callback) {
104 final boolean finalAllowed = isOriginAllowed(origin);
105 ThreadUtils.postOnUiThread(new Runnable() {
108 callback.onReceiveValue(finalAllowed);
114 * Async method to get the domains currently allowed or denied.
116 public void getOrigins(final ValueCallback<Set<String>> callback) {
117 final Set<String> origins = new HashSet<String>();
118 for (String name : mSharedPreferences.getAll().keySet()) {
119 if (name.startsWith(PREF_PREFIX)) {
120 origins.add(name.substring(PREF_PREFIX.length()));
123 ThreadUtils.postOnUiThread(new Runnable() {
126 callback.onReceiveValue(origins);
132 * Get the domain of an URL using the GURL library.
134 private String getOriginKey(String url) {
135 String origin = GURLUtils.getOrigin(url);
136 if (origin.isEmpty()) {
140 return PREF_PREFIX + origin;