Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(171)

Side by Side Diff: android_webview/java/src/org/chromium/android_webview/AwGeolocationPermissions.java

Issue 12211047: Implementing geolocation for the Android Webview (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Minor style updates Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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;
joth 2013/02/06 22:09:51 crumbs more statics. I'll make a patch to avoid th
Kristian Monsen 2013/02/08 00:07:44 OK.
28
29 // TODO(kristianm): Make this private once framework is updated to use getIn stance
boliu 2013/02/06 20:29:54 Sorry I don't understand this comment. What framew
Kristian Monsen 2013/02/08 00:07:44 Android -> framework/webview. That is where this i
27 public AwGeolocationPermissions(SharedPreferences sharedPreferences) { 30 public AwGeolocationPermissions(SharedPreferences sharedPreferences) {
28 mSharedPreferences = sharedPreferences; 31 mSharedPreferences = sharedPreferences;
32 sInstance = this;
29 } 33 }
30 34
35 /**
36 * Get the static instance after it has been created
37 */
38 public static synchronized AwGeolocationPermissions getInstance() {
boliu 2013/02/06 20:29:54 Can we use synchronized blocks instead of having t
Kristian Monsen 2013/02/08 00:07:44 Done.
39 if (sInstance == null) {
40 throw new IllegalStateException("This should only be called after cr eteInstance.");
41 }
42 return sInstance;
43 }
44
45 /**
46 * Create the static instance of this class
47 */
48 public static synchronized AwGeolocationPermissions createInstance(
boliu 2013/02/06 20:29:54 getInstance vs createInstance? I'm confused if thi
Kristian Monsen 2013/02/08 00:07:44 It needs a SharedPreference when creating the stat
49 SharedPreferences sharedPreferences) {
50 if (sInstance != null) {
51 throw new IllegalStateException("This should only be called once.");
52 }
53 sInstance = new AwGeolocationPermissions(sharedPreferences);
boliu 2013/02/06 20:29:54 sInstance is set here and in the constructor, dupl
Kristian Monsen 2013/02/08 00:07:44 Yes, done. I wanted it here, but had to be in the
54 return sInstance;
55 }
56
57 /**
58 * Set one origin to be allowed.
59 */
31 public void allow(String origin) { 60 public void allow(String origin) {
32 String key = getOriginKey(origin); 61 String key = getOriginKey(origin);
33 if (key != null) { 62 if (key != null) {
34 mSharedPreferences.edit().putBoolean(key, true).apply(); 63 mSharedPreferences.edit().putBoolean(key, true).apply();
35 } 64 }
36 } 65 }
37 66
67 /**
68 * Set one origin to be denied.
69 */
38 public void deny(String origin) { 70 public void deny(String origin) {
39 String key = getOriginKey(origin); 71 String key = getOriginKey(origin);
40 if (key != null) { 72 if (key != null) {
41 mSharedPreferences.edit().putBoolean(key, false).apply(); 73 mSharedPreferences.edit().putBoolean(key, false).apply();
42 } 74 }
43 } 75 }
44 76
77 /**
78 * Clear one origin from being allowed and denied.
79 */
45 public void clear(String origin) { 80 public void clear(String origin) {
46 String key = getOriginKey(origin); 81 String key = getOriginKey(origin);
47 if (key != null) { 82 if (key != null) {
48 mSharedPreferences.edit().remove(key).apply(); 83 mSharedPreferences.edit().remove(key).apply();
49 } 84 }
50 } 85 }
51 86
87 /**
88 * Clear all origins set to allowed or denied.
89 */
52 public void clearAll() { 90 public void clearAll() {
53 SharedPreferences.Editor editor = null; 91 SharedPreferences.Editor editor = null;
54 for (String name : mSharedPreferences.getAll().keySet()) { 92 for (String name : mSharedPreferences.getAll().keySet()) {
55 if (name.startsWith(PREF_PREFIX)) { 93 if (name.startsWith(PREF_PREFIX)) {
56 if (editor == null) { 94 if (editor == null) {
57 editor = mSharedPreferences.edit(); 95 editor = mSharedPreferences.edit();
58 } 96 }
59 editor.remove(name); 97 editor.remove(name);
60 } 98 }
61 } 99 }
62 if (editor != null) { 100 if (editor != null) {
63 editor.apply(); 101 editor.apply();
64 } 102 }
65 } 103 }
66 104
67 public void getAllowed(String origin, final ValueCallback<Boolean> callback) { 105 /**
106 * Sync method to get if an origin set to be allowed.
107 */
108 public boolean getAllowed(String origin) {
joth 2013/02/06 22:09:51 I think isOriginAllowed / isOriginDenied would be
Kristian Monsen 2013/02/08 00:07:44 Done.
68 boolean allowed = false; 109 boolean allowed = false;
69 try { 110 try {
70 String key = getOriginKey(origin); 111 String key = getOriginKey(origin);
71 if (key != null) { 112 if (key != null) {
72 allowed = mSharedPreferences.getBoolean(key, false); 113 allowed = mSharedPreferences.getBoolean(key, false);
73 } 114 }
74 } catch (ClassCastException e) { 115 } catch (ClassCastException e) {
75 // Want to return false in this case, do nothing here 116 // Want to return false in this case, do nothing here
76 } 117 }
77 final boolean finalAllowed = allowed; 118 return allowed;
joth 2013/02/06 22:09:51 AIUI passing key == null to getFoo() is fine so yo
Kristian Monsen 2013/02/08 00:07:44 Done.
119 }
120
121 /**
122 * Sync method to get if an origin set to be denied.
123 */
124 public boolean getDenied(String origin) {
125 boolean allowed = true;
126 try {
127 String key = getOriginKey(origin);
128 if (key != null) {
129 allowed = mSharedPreferences.getBoolean(key, true);
joth 2013/02/06 22:09:51 the 'true' here & above looks very odd. This come
Kristian Monsen 2013/02/08 00:07:44 Updated as it makes the only callsite clearer, but
joth 2013/02/08 02:15:01 agreed. we might as well invent an enum (int const
130 }
131 } catch (ClassCastException e) {
132 // Want to return false in this case, do nothing here
133 }
134 return !allowed;
135 }
136
137 /**
138 * ASync method to get if an origin set to be allowed.
139 */
140 public void getAllowed(String origin, final ValueCallback<Boolean> callback) {
141 final boolean finalAllowed = getAllowed(origin);
boliu 2013/02/06 20:29:54 given this is async, should this be more async tha
Kristian Monsen 2013/02/08 00:07:44 I felt this was better as the result will reflect
joth 2013/02/08 02:15:01 Seems reasonable. Otherwise, calling this sequence
78 ThreadUtils.postOnUiThread(new Runnable() { 142 ThreadUtils.postOnUiThread(new Runnable() {
79 @Override 143 @Override
80 public void run() { 144 public void run() {
81 callback.onReceiveValue(finalAllowed); 145 callback.onReceiveValue(finalAllowed);
82 } 146 }
83 }); 147 });
84 } 148 }
85 149
150 /**
151 * ASync method to get the domains currently allowed or denied.
152 */
86 public void getOrigins(final ValueCallback<Set<String>> callback) { 153 public void getOrigins(final ValueCallback<Set<String>> callback) {
87 final Set<String> origins = new HashSet<String>(); 154 final Set<String> origins = new HashSet<String>();
88 for (String name : mSharedPreferences.getAll().keySet()) { 155 for (String name : mSharedPreferences.getAll().keySet()) {
89 if (name.startsWith(PREF_PREFIX)) { 156 if (name.startsWith(PREF_PREFIX)) {
90 origins.add(name.substring(PREF_PREFIX.length())); 157 origins.add(name.substring(PREF_PREFIX.length()));
91 } 158 }
92 } 159 }
93 ThreadUtils.postOnUiThread(new Runnable() { 160 ThreadUtils.postOnUiThread(new Runnable() {
94 @Override 161 @Override
95 public void run() { 162 public void run() {
96 callback.onReceiveValue(origins); 163 callback.onReceiveValue(origins);
97 } 164 }
98 }); 165 });
99 } 166 }
100 167
168 /**
169 * Get the domain of an URL using the GURL library.
170 */
101 private String getOriginKey(String url) { 171 private String getOriginKey(String url) {
102 String origin = GURLUtils.getOrigin(url); 172 String origin = GURLUtils.getOrigin(url);
103 if (origin.isEmpty()) { 173 if (origin.isEmpty()) {
104 return null; 174 return null;
105 } 175 }
106 176
107 return PREF_PREFIX + origin; 177 return PREF_PREFIX + origin;
108 } 178 }
109 } 179 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698