Skip to content

Commit

Permalink
Adding java implementation for Geolocation
Browse files Browse the repository at this point in the history
Review URL: https://chromiumcodereview.appspot.com/11416347

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@173328 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
kristianm@chromium.org committed Dec 15, 2012
1 parent 439a094 commit d72acb0
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
10 changes: 10 additions & 0 deletions base/android/java/src/org/chromium/base/ThreadUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ public static <T> FutureTask<T> postOnUiThread(FutureTask<T> task) {
return task;
}

/**
* Post the supplied Runnable to run on the main thread. The method will
* not block, even if called on the UI thread.
*
* @param task The Runnable to run
*/
public static void postOnUiThread(Runnable r) {
LazyHolder.sUiThreadHandler.post(r);
}

/**
* Asserts that the current thread is running on the main thread.
*/
Expand Down
24 changes: 24 additions & 0 deletions net/android/gurl_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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.

#include "net/android/gurl_utils.h"

#include "base/android/jni_string.h"
#include "googleurl/src/gurl.h"
#include "jni/GURLUtils_jni.h"

namespace net {

jstring GetOrigin(JNIEnv* env, jclass clazz, jstring url) {
GURL host(base::android::ConvertJavaStringToUTF16(env, url));

return base::android::ConvertUTF8ToJavaString(env,
host.GetOrigin().spec()).Release();
}

bool RegisterGURLUtils(JNIEnv* env) {
return RegisterNativesImpl(env);
}

} // net namespace
16 changes: 16 additions & 0 deletions net/android/gurl_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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.

#ifndef NET_ANDROID_GURL_UTILS_H_
#define NET_ANDROID_GURL_UTILS_H_

#include <jni.h>

namespace net {

bool RegisterGURLUtils(JNIEnv* env);

} // net namespace

#endif // NET_ANDROID_GURL_UTILS_H_
27 changes: 27 additions & 0 deletions net/android/java/src/org/chromium/net/GURLUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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.net;

import org.chromium.base.JNINamespace;

/**
* Class to access the GURL library from java.
*/
@JNINamespace("net")
public final class GURLUtils {

/**
* Get the origin of an url: Ex getOrigin("http://www.example.com:8080/index.html?bar=foo")
* would return "http://www.example.com:8080". It will return an empty string for an
* invalid url.
*
* @return The origin of the url
*/
public static String getOrigin(String url) {
return nativeGetOrigin(url);
}

private static native String nativeGetOrigin(String url);
}
2 changes: 2 additions & 0 deletions net/android/net_jni_registrar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "base/basictypes.h"
#include "base/android/jni_android.h"
#include "base/android/jni_registrar.h"
#include "net/android/gurl_utils.h"
#include "net/android/network_change_notifier_android.h"
#include "net/android/network_library.h"
#include "net/proxy/proxy_config_service_android.h"
Expand All @@ -16,6 +17,7 @@ namespace android {

static base::android::RegistrationMethod kNetRegisteredMethods[] = {
{ "AndroidNetworkLibrary", net::android::RegisterNetworkLibrary },
{ "GURLUtils", net::RegisterGURLUtils },
{ "NetworkChangeNotifierAndroid",
net::NetworkChangeNotifierAndroid::Register },
{ "ProxyConfigService", net::ProxyConfigServiceAndroid::Register },
Expand Down
3 changes: 3 additions & 0 deletions net/net.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
'net_resources',
],
'sources': [
'android/gurl_utils.cc',
'android/gurl_utils.h',
'android/net_jni_registrar.cc',
'android/net_jni_registrar.h',
'android/network_change_notifier_android.cc',
Expand Down Expand Up @@ -2265,6 +2267,7 @@
'type': 'none',
'sources': [
'android/java/src/org/chromium/net/AndroidNetworkLibrary.java',
'android/java/src/org/chromium/net/GURLUtils.java',
'android/java/src/org/chromium/net/NetworkChangeNotifier.java',
'android/java/src/org/chromium/net/ProxyChangeListener.java',
],
Expand Down

0 comments on commit d72acb0

Please sign in to comment.