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

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: Updated indenting 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;
28
29 // TODO(kristianm): Make this private once framework is updated to use getIn stance
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 AwGeolocationPermissions getInstance() {
39 synchronized (AwGeolocationPermissions.class) {
40 if (sInstance == null) {
41 throw new IllegalStateException("This should only be called afte r createInstance.");
42 }
43 }
44 return sInstance;
45 }
46
47 /**
48 * Create the static instance of this class
49 */
50 public static AwGeolocationPermissions createInstance(
51 SharedPreferences sharedPreferences) {
52 synchronized (AwGeolocationPermissions.class) {
53 if (sInstance != null) {
54 throw new IllegalStateException("This should only be called once .");
55 }
56 // sInstance set in the constructor
57 new AwGeolocationPermissions(sharedPreferences);
58 return sInstance;
59 }
60 }
61
62 /**
63 * Set one origin to be allowed.
64 */
31 public void allow(String origin) { 65 public void allow(String origin) {
32 String key = getOriginKey(origin); 66 String key = getOriginKey(origin);
33 if (key != null) { 67 if (key != null) {
34 mSharedPreferences.edit().putBoolean(key, true).apply(); 68 mSharedPreferences.edit().putBoolean(key, true).apply();
35 } 69 }
36 } 70 }
37 71
72 /**
73 * Set one origin to be denied.
74 */
38 public void deny(String origin) { 75 public void deny(String origin) {
39 String key = getOriginKey(origin); 76 String key = getOriginKey(origin);
40 if (key != null) { 77 if (key != null) {
41 mSharedPreferences.edit().putBoolean(key, false).apply(); 78 mSharedPreferences.edit().putBoolean(key, false).apply();
42 } 79 }
43 } 80 }
44 81
82 /**
83 * Clear the stored permission for a particular origin.
84 */
45 public void clear(String origin) { 85 public void clear(String origin) {
46 String key = getOriginKey(origin); 86 String key = getOriginKey(origin);
47 if (key != null) { 87 if (key != null) {
48 mSharedPreferences.edit().remove(key).apply(); 88 mSharedPreferences.edit().remove(key).apply();
49 } 89 }
50 } 90 }
51 91
92 /**
93 * Clear stored permissions for all origins.
94 */
52 public void clearAll() { 95 public void clearAll() {
53 SharedPreferences.Editor editor = null; 96 SharedPreferences.Editor editor = null;
54 for (String name : mSharedPreferences.getAll().keySet()) { 97 for (String name : mSharedPreferences.getAll().keySet()) {
55 if (name.startsWith(PREF_PREFIX)) { 98 if (name.startsWith(PREF_PREFIX)) {
56 if (editor == null) { 99 if (editor == null) {
57 editor = mSharedPreferences.edit(); 100 editor = mSharedPreferences.edit();
58 } 101 }
59 editor.remove(name); 102 editor.remove(name);
60 } 103 }
61 } 104 }
62 if (editor != null) { 105 if (editor != null) {
63 editor.apply(); 106 editor.apply();
64 } 107 }
65 } 108 }
66 109
110 /**
111 * Sync method to get if an origin is set to be allowed.
benm (inactive) 2013/02/08 13:25:19 nit: Synchronous
Kristian Monsen 2013/02/12 18:53:03 Done.
112 */
113 public boolean isOriginAllowed(String origin) {
114 try {
115 return mSharedPreferences.getBoolean(getOriginKey(origin), false);
116 } catch (ClassCastException e) {
117 return false;
benm (inactive) 2013/02/08 13:25:19 How could we ever get a pref with the same name bu
Kristian Monsen 2013/02/12 18:53:03 Agree, but I also don't think it is right to throw
118 }
119 }
120
121 /**
122 * Returns true if the origin is either set to allowed or denied.
123 */
124 public boolean hasOrigin(String origin) {
125 return mSharedPreferences.contains(getOriginKey(origin));
126 }
127
128 /**
129 * Async method to get if an origin set to be allowed.
benm (inactive) 2013/02/08 13:25:19 nit: Asynchronous
Kristian Monsen 2013/02/12 18:53:03 Done.
130 */
67 public void getAllowed(String origin, final ValueCallback<Boolean> callback) { 131 public void getAllowed(String origin, final ValueCallback<Boolean> callback) {
68 boolean allowed = false; 132 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() { 133 ThreadUtils.postOnUiThread(new Runnable() {
79 @Override 134 @Override
80 public void run() { 135 public void run() {
81 callback.onReceiveValue(finalAllowed); 136 callback.onReceiveValue(finalAllowed);
82 } 137 }
83 }); 138 });
84 } 139 }
85 140
141 /**
142 * Async method to get the domains currently allowed or denied.
143 */
86 public void getOrigins(final ValueCallback<Set<String>> callback) { 144 public void getOrigins(final ValueCallback<Set<String>> callback) {
87 final Set<String> origins = new HashSet<String>(); 145 final Set<String> origins = new HashSet<String>();
88 for (String name : mSharedPreferences.getAll().keySet()) { 146 for (String name : mSharedPreferences.getAll().keySet()) {
89 if (name.startsWith(PREF_PREFIX)) { 147 if (name.startsWith(PREF_PREFIX)) {
90 origins.add(name.substring(PREF_PREFIX.length())); 148 origins.add(name.substring(PREF_PREFIX.length()));
91 } 149 }
92 } 150 }
93 ThreadUtils.postOnUiThread(new Runnable() { 151 ThreadUtils.postOnUiThread(new Runnable() {
94 @Override 152 @Override
95 public void run() { 153 public void run() {
96 callback.onReceiveValue(origins); 154 callback.onReceiveValue(origins);
97 } 155 }
98 }); 156 });
99 } 157 }
100 158
159 /**
160 * Get the domain of an URL using the GURL library.
161 */
101 private String getOriginKey(String url) { 162 private String getOriginKey(String url) {
102 String origin = GURLUtils.getOrigin(url); 163 String origin = GURLUtils.getOrigin(url);
103 if (origin.isEmpty()) { 164 if (origin.isEmpty()) {
104 return null; 165 return null;
105 } 166 }
106 167
107 return PREF_PREFIX + origin; 168 return PREF_PREFIX + origin;
108 } 169 }
109 } 170 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698