Home | History | Annotate | Download | only in jni
      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 to_string
     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 
     40 namespace android {
     41 
     42 static 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 
     51     GeolocationPermissions::OriginSet::const_iterator end = origins.end();
     52     for (GeolocationPermissions::OriginSet::const_iterator iter = origins.begin(); iter != end; ++iter) {
     53         jstring originString = env->NewString(iter->characters(), iter->length());
     54         env->CallBooleanMethod(set, addMethod, originString);
     55         env->DeleteLocalRef(originString);
     56     }
     57     return set;
     58 }
     59 
     60 static bool getAllowed(JNIEnv* env, jobject obj, jstring origin)
     61 {
     62     WebCore::String originString = to_string(env, origin);
     63     return GeolocationPermissions::getAllowed(originString);
     64 }
     65 
     66 static void clear(JNIEnv* env, jobject obj, jstring origin)
     67 {
     68     WebCore::String originString = to_string(env, origin);
     69     GeolocationPermissions::clear(originString);
     70 }
     71 
     72 static void allow(JNIEnv* env, jobject obj, jstring origin)
     73 {
     74     WebCore::String originString = to_string(env, origin);
     75     GeolocationPermissions::allow(originString);
     76 }
     77 
     78 static void clearAll(JNIEnv* env, jobject obj)
     79 {
     80     GeolocationPermissions::clearAll();
     81 }
     82 
     83 /*
     84  * JNI registration
     85  */
     86 static JNINativeMethod gGeolocationPermissionsMethods[] = {
     87     { "nativeGetOrigins", "()Ljava/util/Set;",
     88         (void*) getOrigins },
     89     { "nativeGetAllowed", "(Ljava/lang/String;)Z",
     90         (void*) getAllowed },
     91     { "nativeClear", "(Ljava/lang/String;)V",
     92         (void*) clear },
     93     { "nativeAllow", "(Ljava/lang/String;)V",
     94         (void*) allow },
     95     { "nativeClearAll", "()V",
     96         (void*) clearAll }
     97 };
     98 
     99 int register_geolocation_permissions(JNIEnv* env)
    100 {
    101     const char* kGeolocationPermissionsClass = "android/webkit/GeolocationPermissions";
    102     jclass geolocationPermissions = env->FindClass(kGeolocationPermissionsClass);
    103     LOG_ASSERT(geolocationPermissions, "Unable to find class");
    104 
    105     return jniRegisterNativeMethods(env, kGeolocationPermissionsClass,
    106             gGeolocationPermissionsMethods, NELEM(gGeolocationPermissionsMethods));
    107 }
    108 
    109 }
    110