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 try { | |
122 return mSharedPreferences.getBoolean(getOriginKey(origin), false); | |
123 } catch (ClassCastException e) { | |
124 mSharedPreferences.edit().remove(getOriginKey(origin)).apply(); | |
benm (inactive)
2013/02/12 19:40:33
Sorry, I'm still not sure I like this. If the embe
Kristian Monsen
2013/02/12 23:48:35
Done
| |
125 return false; | |
126 } | |
127 } | |
128 | |
129 /** | |
130 * Returns true if the origin is either set to allowed or denied. | |
131 */ | |
132 public boolean hasOrigin(String origin) { | |
133 return mSharedPreferences.contains(getOriginKey(origin)); | |
134 } | |
135 | |
136 /** | |
137 * Asynchronous method to get if an origin set to be allowed. | |
138 */ | |
67 public void getAllowed(String origin, final ValueCallback<Boolean> callback) { | 139 public void getAllowed(String origin, final ValueCallback<Boolean> callback) { |
68 boolean allowed = false; | 140 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() { | 141 ThreadUtils.postOnUiThread(new Runnable() { |
79 @Override | 142 @Override |
80 public void run() { | 143 public void run() { |
81 callback.onReceiveValue(finalAllowed); | 144 callback.onReceiveValue(finalAllowed); |
82 } | 145 } |
83 }); | 146 }); |
84 } | 147 } |
85 | 148 |
149 /** | |
150 * Async method to get the domains currently allowed or denied. | |
151 */ | |
86 public void getOrigins(final ValueCallback<Set<String>> callback) { | 152 public void getOrigins(final ValueCallback<Set<String>> callback) { |
87 final Set<String> origins = new HashSet<String>(); | 153 final Set<String> origins = new HashSet<String>(); |
88 for (String name : mSharedPreferences.getAll().keySet()) { | 154 for (String name : mSharedPreferences.getAll().keySet()) { |
89 if (name.startsWith(PREF_PREFIX)) { | 155 if (name.startsWith(PREF_PREFIX)) { |
90 origins.add(name.substring(PREF_PREFIX.length())); | 156 origins.add(name.substring(PREF_PREFIX.length())); |
91 } | 157 } |
92 } | 158 } |
93 ThreadUtils.postOnUiThread(new Runnable() { | 159 ThreadUtils.postOnUiThread(new Runnable() { |
94 @Override | 160 @Override |
95 public void run() { | 161 public void run() { |
96 callback.onReceiveValue(origins); | 162 callback.onReceiveValue(origins); |
97 } | 163 } |
98 }); | 164 }); |
99 } | 165 } |
100 | 166 |
167 /** | |
168 * Get the domain of an URL using the GURL library. | |
169 */ | |
101 private String getOriginKey(String url) { | 170 private String getOriginKey(String url) { |
102 String origin = GURLUtils.getOrigin(url); | 171 String origin = GURLUtils.getOrigin(url); |
103 if (origin.isEmpty()) { | 172 if (origin.isEmpty()) { |
104 return null; | 173 return null; |
105 } | 174 } |
106 | 175 |
107 return PREF_PREFIX + origin; | 176 return PREF_PREFIX + origin; |
108 } | 177 } |
109 } | 178 } |
OLD | NEW |