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

Unified Diff: android_webview/java/src/org/chromium/android_webview/AwGeolocationPermissions.java

Issue 11416347: Adding java implementation for Geolocation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Updated comment and copyright date Created 8 years 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/android/java/src/org/chromium/base/ThreadUtils.java » ('j') | net/android/gurl_utils.h » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: android_webview/java/src/org/chromium/android_webview/AwGeolocationPermissions.java
diff --git a/android_webview/java/src/org/chromium/android_webview/AwGeolocationPermissions.java b/android_webview/java/src/org/chromium/android_webview/AwGeolocationPermissions.java
new file mode 100644
index 0000000000000000000000000000000000000000..7b4b945e79309025c5381dc7ddcb6b8b8a5a86b1
--- /dev/null
+++ b/android_webview/java/src/org/chromium/android_webview/AwGeolocationPermissions.java
@@ -0,0 +1,101 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.android_webview;
+
+import android.content.SharedPreferences;
+import android.webkit.ValueCallback;
+
+import org.chromium.base.JNINamespace;
+import org.chromium.base.ThreadUtils;
+import org.chromium.net.GURLUtils;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * This class is used to manage permissions for the WebView's Geolocation JavaScript API.
+ *
+ * Callbacks are posted on the UI thread.
+ */
+public final class AwGeolocationPermissions {
+
+ private static final String PREF_PREFIX =
+ AwGeolocationPermissions.class.getCanonicalName() + "%";
+ private final SharedPreferences mSharedPreferences;
+
+ public AwGeolocationPermissions(SharedPreferences sharedPreferences) {
+ mSharedPreferences = sharedPreferences;
+ }
+
+ public void allow(String origin) {
+ String key = getOriginKey(origin);
+ if (key != null) {
+ mSharedPreferences.edit().putBoolean(key, true).apply();
+ }
+ }
+
+ public void deny(String origin) {
+ String key = getOriginKey(origin);
+ if (key != null) {
+ mSharedPreferences.edit().putBoolean(key, false).apply();
+ }
+ }
+
+ public void clear(String origin) {
+ String key = getOriginKey(origin);
+ if (key != null) {
+ mSharedPreferences.edit().remove(key).apply();
+ }
+ }
+
+ public void clearAll() {
+ for (String name : mSharedPreferences.getAll().keySet()) {
+ if (name.startsWith(PREF_PREFIX)) {
+ mSharedPreferences.edit().remove(name).apply();
+ }
+ }
+ }
+
+ public void getAllowed(String origin, final ValueCallback<Boolean> callback) {
+ boolean allowed = false;
+ try {
+ String key = getOriginKey(origin);
+ if (key != null) {
+ allowed = mSharedPreferences.getBoolean(key, false);
+ }
+ } catch (ClassCastException e) {
+ // Want to return false in this case, do nothing here
+ }
+ final boolean finalAllowed = allowed;
+ ThreadUtils.postOnUiThread(new Runnable() {
+ public void run() {
+ callback.onReceiveValue(finalAllowed);
+ }
+ });
+ }
+
+ public void getOrigins(final ValueCallback<Set<String>> callback) {
+ final Set<String> origins = new HashSet<String>();
+ for (String name : mSharedPreferences.getAll().keySet()) {
+ if (name.startsWith(PREF_PREFIX)) {
+ origins.add(name.substring(PREF_PREFIX.length()));
+ }
+ }
+ ThreadUtils.postOnUiThread(new Runnable() {
+ public void run() {
+ callback.onReceiveValue(origins);
+ }
+ });
+ }
+
+ private String getOriginKey(String url) {
+ String origin = GURLUtils.getOrigin(url);
+ if (origin.isEmpty()) {
+ return null;
+ }
+
+ return PREF_PREFIX + origin;
+ }
+}
« no previous file with comments | « no previous file | base/android/java/src/org/chromium/base/ThreadUtils.java » ('j') | net/android/gurl_utils.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698