1/*
2 * Copyright 2009, The Android Open Source Project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *  * Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 *  * Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#include <JNIHelp.h>  // For jniRegisterNativeMethods
29#include "GeolocationPermissions.h"
30#include "WebCoreJni.h"  // For jstringToWtfString
31
32
33/**
34 * This file provides a set of functions to bridge between the Java and C++
35 * GeolocationPermissions classes. The java GeolocationPermissions object calls
36 * the functions provided here, which in turn call static methods on the C++
37 * GeolocationPermissions class.
38 */
39
40namespace android {
41
42static jobject getOrigins(JNIEnv* env, jobject obj)
43{
44    GeolocationPermissions::OriginSet origins = GeolocationPermissions::getOrigins();
45
46    jclass setClass = env->FindClass("java/util/HashSet");
47    jmethodID constructor = env->GetMethodID(setClass, "<init>", "()V");
48    jmethodID addMethod = env->GetMethodID(setClass, "add", "(Ljava/lang/Object;)Z");
49    jobject set = env->NewObject(setClass, constructor);
50    env->DeleteLocalRef(setClass);
51
52    GeolocationPermissions::OriginSet::const_iterator end = origins.end();
53    for (GeolocationPermissions::OriginSet::const_iterator iter = origins.begin(); iter != end; ++iter) {
54        jstring originString = wtfStringToJstring(env, *iter);
55        env->CallBooleanMethod(set, addMethod, originString);
56        env->DeleteLocalRef(originString);
57    }
58    return set;
59}
60
61static bool getAllowed(JNIEnv* env, jobject obj, jstring origin)
62{
63    WTF::String originString = jstringToWtfString(env, origin);
64    return GeolocationPermissions::getAllowed(originString);
65}
66
67static void clear(JNIEnv* env, jobject obj, jstring origin)
68{
69    WTF::String originString = jstringToWtfString(env, origin);
70    GeolocationPermissions::clear(originString);
71}
72
73static void allow(JNIEnv* env, jobject obj, jstring origin)
74{
75    WTF::String originString = jstringToWtfString(env, origin);
76    GeolocationPermissions::allow(originString);
77}
78
79static void clearAll(JNIEnv* env, jobject obj)
80{
81    GeolocationPermissions::clearAll();
82}
83
84/*
85 * JNI registration
86 */
87static JNINativeMethod gGeolocationPermissionsMethods[] = {
88    { "nativeGetOrigins", "()Ljava/util/Set;",
89        (void*) getOrigins },
90    { "nativeGetAllowed", "(Ljava/lang/String;)Z",
91        (void*) getAllowed },
92    { "nativeClear", "(Ljava/lang/String;)V",
93        (void*) clear },
94    { "nativeAllow", "(Ljava/lang/String;)V",
95        (void*) allow },
96    { "nativeClearAll", "()V",
97        (void*) clearAll }
98};
99
100int registerGeolocationPermissions(JNIEnv* env)
101{
102    const char* kGeolocationPermissionsClass = "android/webkit/GeolocationPermissionsClassic";
103#ifndef NDEBUG
104    jclass geolocationPermissions = env->FindClass(kGeolocationPermissionsClass);
105    ALOG_ASSERT(geolocationPermissions, "Unable to find class");
106    env->DeleteLocalRef(geolocationPermissions);
107#endif
108
109    return jniRegisterNativeMethods(env, kGeolocationPermissionsClass,
110            gGeolocationPermissionsMethods, NELEM(gGeolocationPermissionsMethods));
111}
112
113}
114