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