7736b8de38cc1e858a08d81a69e3b117e746f154
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / java / src / org / xwalk / core / XWalkGeolocationPermissions.java
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.
4
5 package org.xwalk.core;
6
7 import android.content.SharedPreferences;
8 import android.webkit.ValueCallback;
9
10 import java.util.HashSet;
11 import java.util.Set;
12
13 import org.chromium.base.ThreadUtils;
14 import org.chromium.net.GURLUtils;
15
16 /**
17  * This class is used to manage permissions for the WebView's Geolocation JavaScript API.
18  *
19  * Callbacks are posted on the UI thread.
20  */
21 public final class XWalkGeolocationPermissions {
22     /**
23      * Callback interface used by the browser to report a Geolocation permission
24      * state set by the user in response to a permissions prompt.
25      */
26     public interface Callback {
27         public void invoke(String origin, boolean allow, boolean remember);
28     };
29
30     private static final String PREF_PREFIX =
31             XWalkGeolocationPermissions.class.getCanonicalName() + "%";
32     private final SharedPreferences mSharedPreferences;
33
34     public XWalkGeolocationPermissions(SharedPreferences sharedPreferences) {
35         mSharedPreferences = sharedPreferences;
36     }
37
38     /**
39      * Set one origin to be allowed.
40      */
41     public void allow(String origin) {
42         String key = getOriginKey(origin);
43         if (key != null) {
44             mSharedPreferences.edit().putBoolean(key, true).apply();
45         }
46     }
47
48     /**
49      * Set one origin to be denied.
50      */
51     public void deny(String origin) {
52         String key = getOriginKey(origin);
53         if (key != null) {
54             mSharedPreferences.edit().putBoolean(key, false).apply();
55         }
56     }
57
58     /**
59      * Clear the stored permission for a particular origin.
60      */
61     public void clear(String origin) {
62         String key = getOriginKey(origin);
63         if (key != null) {
64             mSharedPreferences.edit().remove(key).apply();
65         }
66     }
67
68     /**
69      * Clear stored permissions for all origins.
70      */
71     public void clearAll() {
72         SharedPreferences.Editor editor = null;
73         for (String name : mSharedPreferences.getAll().keySet()) {
74             if (name.startsWith(PREF_PREFIX)) {
75                 if (editor == null) {
76                     editor = mSharedPreferences.edit();
77                 }
78                 editor.remove(name);
79             }
80         }
81         if (editor != null) {
82             editor.apply();
83         }
84     }
85
86     /**
87      * Synchronous method to get if an origin is set to be allowed.
88      */
89     public boolean isOriginAllowed(String origin) {
90         return mSharedPreferences.getBoolean(getOriginKey(origin), false);
91     }
92
93     /**
94      * Returns true if the origin is either set to allowed or denied.
95      */
96     public boolean hasOrigin(String origin) {
97         return mSharedPreferences.contains(getOriginKey(origin));
98     }
99
100     /**
101      * Asynchronous method to get if an origin set to be allowed.
102      */
103     public void getAllowed(String origin, final ValueCallback<Boolean> callback) {
104         final boolean finalAllowed = isOriginAllowed(origin);
105         ThreadUtils.postOnUiThread(new Runnable() {
106             @Override
107             public void run() {
108                 callback.onReceiveValue(finalAllowed);
109             }
110         });
111     }
112
113     /**
114      * Async method to get the domains currently allowed or denied.
115      */
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()));
121             }
122         }
123         ThreadUtils.postOnUiThread(new Runnable() {
124             @Override
125             public void run() {
126                 callback.onReceiveValue(origins);
127             }
128         });
129     }
130
131     /**
132      * Get the domain of an URL using the GURL library.
133      */
134     private String getOriginKey(String url) {
135         String origin = GURLUtils.getOrigin(url);
136         if (origin.isEmpty()) {
137             return null;
138         }
139
140         return PREF_PREFIX + origin;
141     }
142 }