forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding java implementation for Geolocation
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
Showing
7 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
android_webview/java/src/org/chromium/android_webview/AwGeolocationPermissions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters