OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.android_webview; | 5 package org.chromium.android_webview; |
6 | 6 |
7 import android.content.SharedPreferences; | 7 import android.content.SharedPreferences; |
8 import android.webkit.ValueCallback; | 8 import android.webkit.ValueCallback; |
9 | 9 |
10 import org.chromium.base.ThreadUtils; | 10 import org.chromium.base.ThreadUtils; |
11 import org.chromium.net.GURLUtils; | 11 import org.chromium.net.GURLUtils; |
12 | 12 |
13 import java.util.HashSet; | 13 import java.util.HashSet; |
14 import java.util.Set; | 14 import java.util.Set; |
15 | 15 |
16 /** | 16 /** |
17 * This class is used to manage permissions for the WebView's Geolocation JavaSc
ript API. | 17 * This class is used to manage permissions for the WebView's Geolocation JavaSc
ript API. |
18 * | 18 * |
19 * Callbacks are posted on the UI thread. | 19 * Callbacks are posted on the UI thread. |
20 */ | 20 */ |
21 public final class AwGeolocationPermissions { | 21 public final class AwGeolocationPermissions { |
22 | 22 |
23 private static final String PREF_PREFIX = | 23 private static final String PREF_PREFIX = |
24 AwGeolocationPermissions.class.getCanonicalName() + "%"; | 24 AwGeolocationPermissions.class.getCanonicalName() + "%"; |
25 private final SharedPreferences mSharedPreferences; | 25 private final SharedPreferences mSharedPreferences; |
26 | 26 |
| 27 private static AwGeolocationPermissions sInstance; |
| 28 |
| 29 // TODO(kristianm): Rewrite when AwBrowserContext has landed in |
| 30 // CL: https://codereview.chromium.org/12208099/ |
27 public AwGeolocationPermissions(SharedPreferences sharedPreferences) { | 31 public AwGeolocationPermissions(SharedPreferences sharedPreferences) { |
28 mSharedPreferences = sharedPreferences; | 32 mSharedPreferences = sharedPreferences; |
| 33 setInstance(this); |
29 } | 34 } |
30 | 35 |
| 36 private static void setInstance(AwGeolocationPermissions instance) { |
| 37 synchronized (AwGeolocationPermissions.class) { |
| 38 sInstance = instance; |
| 39 } |
| 40 } |
| 41 |
| 42 /** |
| 43 * Get the static instance after it has been created |
| 44 */ |
| 45 public static AwGeolocationPermissions getInstance() { |
| 46 synchronized (AwGeolocationPermissions.class) { |
| 47 if (sInstance == null) { |
| 48 throw new IllegalStateException("This should only be called afte
r createInstance."); |
| 49 } |
| 50 } |
| 51 return sInstance; |
| 52 } |
| 53 |
| 54 /** |
| 55 * Create the static instance of this class |
| 56 */ |
| 57 public static AwGeolocationPermissions createInstance( |
| 58 SharedPreferences sharedPreferences) { |
| 59 synchronized (AwGeolocationPermissions.class) { |
| 60 if (sInstance != null) { |
| 61 throw new IllegalStateException("This should only be called once
."); |
| 62 } |
| 63 // sInstance set in the constructor |
| 64 new AwGeolocationPermissions(sharedPreferences); |
| 65 return sInstance; |
| 66 } |
| 67 } |
| 68 |
| 69 /** |
| 70 * Set one origin to be allowed. |
| 71 */ |
31 public void allow(String origin) { | 72 public void allow(String origin) { |
32 String key = getOriginKey(origin); | 73 String key = getOriginKey(origin); |
33 if (key != null) { | 74 if (key != null) { |
34 mSharedPreferences.edit().putBoolean(key, true).apply(); | 75 mSharedPreferences.edit().putBoolean(key, true).apply(); |
35 } | 76 } |
36 } | 77 } |
37 | 78 |
| 79 /** |
| 80 * Set one origin to be denied. |
| 81 */ |
38 public void deny(String origin) { | 82 public void deny(String origin) { |
39 String key = getOriginKey(origin); | 83 String key = getOriginKey(origin); |
40 if (key != null) { | 84 if (key != null) { |
41 mSharedPreferences.edit().putBoolean(key, false).apply(); | 85 mSharedPreferences.edit().putBoolean(key, false).apply(); |
42 } | 86 } |
43 } | 87 } |
44 | 88 |
| 89 /** |
| 90 * Clear the stored permission for a particular origin. |
| 91 */ |
45 public void clear(String origin) { | 92 public void clear(String origin) { |
46 String key = getOriginKey(origin); | 93 String key = getOriginKey(origin); |
47 if (key != null) { | 94 if (key != null) { |
48 mSharedPreferences.edit().remove(key).apply(); | 95 mSharedPreferences.edit().remove(key).apply(); |
49 } | 96 } |
50 } | 97 } |
51 | 98 |
| 99 /** |
| 100 * Clear stored permissions for all origins. |
| 101 */ |
52 public void clearAll() { | 102 public void clearAll() { |
53 SharedPreferences.Editor editor = null; | 103 SharedPreferences.Editor editor = null; |
54 for (String name : mSharedPreferences.getAll().keySet()) { | 104 for (String name : mSharedPreferences.getAll().keySet()) { |
55 if (name.startsWith(PREF_PREFIX)) { | 105 if (name.startsWith(PREF_PREFIX)) { |
56 if (editor == null) { | 106 if (editor == null) { |
57 editor = mSharedPreferences.edit(); | 107 editor = mSharedPreferences.edit(); |
58 } | 108 } |
59 editor.remove(name); | 109 editor.remove(name); |
60 } | 110 } |
61 } | 111 } |
62 if (editor != null) { | 112 if (editor != null) { |
63 editor.apply(); | 113 editor.apply(); |
64 } | 114 } |
65 } | 115 } |
66 | 116 |
| 117 /** |
| 118 * Synchronous method to get if an origin is set to be allowed. |
| 119 */ |
| 120 public boolean isOriginAllowed(String origin) { |
| 121 return mSharedPreferences.getBoolean(getOriginKey(origin), false); |
| 122 } |
| 123 |
| 124 /** |
| 125 * Returns true if the origin is either set to allowed or denied. |
| 126 */ |
| 127 public boolean hasOrigin(String origin) { |
| 128 return mSharedPreferences.contains(getOriginKey(origin)); |
| 129 } |
| 130 |
| 131 /** |
| 132 * Asynchronous method to get if an origin set to be allowed. |
| 133 */ |
67 public void getAllowed(String origin, final ValueCallback<Boolean> callback)
{ | 134 public void getAllowed(String origin, final ValueCallback<Boolean> callback)
{ |
68 boolean allowed = false; | 135 final boolean finalAllowed = isOriginAllowed(origin); |
69 try { | |
70 String key = getOriginKey(origin); | |
71 if (key != null) { | |
72 allowed = mSharedPreferences.getBoolean(key, false); | |
73 } | |
74 } catch (ClassCastException e) { | |
75 // Want to return false in this case, do nothing here | |
76 } | |
77 final boolean finalAllowed = allowed; | |
78 ThreadUtils.postOnUiThread(new Runnable() { | 136 ThreadUtils.postOnUiThread(new Runnable() { |
79 @Override | 137 @Override |
80 public void run() { | 138 public void run() { |
81 callback.onReceiveValue(finalAllowed); | 139 callback.onReceiveValue(finalAllowed); |
82 } | 140 } |
83 }); | 141 }); |
84 } | 142 } |
85 | 143 |
| 144 /** |
| 145 * Async method to get the domains currently allowed or denied. |
| 146 */ |
86 public void getOrigins(final ValueCallback<Set<String>> callback) { | 147 public void getOrigins(final ValueCallback<Set<String>> callback) { |
87 final Set<String> origins = new HashSet<String>(); | 148 final Set<String> origins = new HashSet<String>(); |
88 for (String name : mSharedPreferences.getAll().keySet()) { | 149 for (String name : mSharedPreferences.getAll().keySet()) { |
89 if (name.startsWith(PREF_PREFIX)) { | 150 if (name.startsWith(PREF_PREFIX)) { |
90 origins.add(name.substring(PREF_PREFIX.length())); | 151 origins.add(name.substring(PREF_PREFIX.length())); |
91 } | 152 } |
92 } | 153 } |
93 ThreadUtils.postOnUiThread(new Runnable() { | 154 ThreadUtils.postOnUiThread(new Runnable() { |
94 @Override | 155 @Override |
95 public void run() { | 156 public void run() { |
96 callback.onReceiveValue(origins); | 157 callback.onReceiveValue(origins); |
97 } | 158 } |
98 }); | 159 }); |
99 } | 160 } |
100 | 161 |
| 162 /** |
| 163 * Get the domain of an URL using the GURL library. |
| 164 */ |
101 private String getOriginKey(String url) { | 165 private String getOriginKey(String url) { |
102 String origin = GURLUtils.getOrigin(url); | 166 String origin = GURLUtils.getOrigin(url); |
103 if (origin.isEmpty()) { | 167 if (origin.isEmpty()) { |
104 return null; | 168 return null; |
105 } | 169 } |
106 | 170 |
107 return PREF_PREFIX + origin; | 171 return PREF_PREFIX + origin; |
108 } | 172 } |
109 } | 173 } |
OLD | NEW |