Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright 2007, 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 #define LOG_TAG "webcoreglue"
     27 
     28 #include "config.h"
     29 #include "IntRect.h"
     30 #include "WebCoreJni.h"
     31 #include "wtf/Vector.h"
     32 
     33 #include "NotImplemented.h"
     34 #include <JNIUtility.h>
     35 #include <jni.h>
     36 #include <utils/Log.h>
     37 
     38 namespace android {
     39 
     40 AutoJObject getRealObject(JNIEnv* env, jobject obj)
     41 {
     42     jobject real = env->NewLocalRef(obj);
     43     ALOG_ASSERT(real, "The real object has been deleted!");
     44     return AutoJObject(env, real);
     45 }
     46 
     47 /**
     48  * Helper method for checking java exceptions
     49  * @return true if an exception occurred.
     50  */
     51 bool checkException(JNIEnv* env)
     52 {
     53     if (env->ExceptionCheck() != 0)
     54     {
     55         ALOGE("*** Uncaught exception returned from Java call!\n");
     56         env->ExceptionDescribe();
     57         return true;
     58     }
     59     return false;
     60 }
     61 
     62 // This method is safe to call from the ui thread and the WebCore thread.
     63 WTF::String jstringToWtfString(JNIEnv* env, jstring str)
     64 {
     65     if (!str || !env)
     66         return WTF::String();
     67     const jchar* s = env->GetStringChars(str, NULL);
     68     if (!s)
     69         return WTF::String();
     70     WTF::String ret(s, env->GetStringLength(str));
     71     env->ReleaseStringChars(str, s);
     72     checkException(env);
     73     return ret;
     74 }
     75 
     76 jstring wtfStringToJstring(JNIEnv* env, const WTF::String& str, bool validOnZeroLength)
     77 {
     78     int length = str.length();
     79     return length || validOnZeroLength ? env->NewString(str.characters(), length) : 0;
     80 }
     81 
     82 string16 jstringToString16(JNIEnv* env, jstring jstr)
     83 {
     84     if (!jstr || !env)
     85         return string16();
     86 
     87     const char* s = env->GetStringUTFChars(jstr, 0);
     88     if (!s)
     89         return string16();
     90     string16 str = UTF8ToUTF16(s);
     91     env->ReleaseStringUTFChars(jstr, s);
     92     checkException(env);
     93     return str;
     94 }
     95 
     96 std::string jstringToStdString(JNIEnv* env, jstring jstr)
     97 {
     98     if (!jstr || !env)
     99         return std::string();
    100 
    101     const char* s = env->GetStringUTFChars(jstr, 0);
    102     if (!s)
    103         return std::string();
    104     std::string str(s);
    105     env->ReleaseStringUTFChars(jstr, s);
    106     checkException(env);
    107     return str;
    108 }
    109 
    110 jstring stdStringToJstring(JNIEnv* env, const std::string& str, bool validOnZeroLength)
    111 {
    112     return !str.empty() || validOnZeroLength ? env->NewStringUTF(str.c_str()) : 0;
    113 }
    114 
    115 jobject intRectToRect(JNIEnv* env, const WebCore::IntRect& rect)
    116 {
    117     jclass rectClass = env->FindClass("android/graphics/Rect");
    118     ALOG_ASSERT(rectClass, "Could not find android/graphics/Rect");
    119     jmethodID rectInit = env->GetMethodID(rectClass, "<init>", "(IIII)V");
    120     ALOG_ASSERT(rectInit, "Could not find init method on Rect");
    121     jobject jrect = env->NewObject(rectClass, rectInit, rect.x(), rect.y(),
    122             rect.maxX(), rect.maxY());
    123     env->DeleteLocalRef(rectClass);
    124     return jrect;
    125 }
    126 
    127 jobjectArray intRectVectorToRectArray(JNIEnv* env, Vector<WebCore::IntRect>& rects)
    128 {
    129     jclass rectClass = env->FindClass("android/graphics/Rect");
    130     ALOG_ASSERT(rectClass, "Could not find android/graphics/Rect");
    131     jmethodID rectInit = env->GetMethodID(rectClass, "<init>", "(IIII)V");
    132     ALOG_ASSERT(rectInit, "Could not find init method on Rect");
    133     jobjectArray array = env->NewObjectArray(rects.size(), rectClass, 0);
    134     ALOG_ASSERT(array, "Could not create a Rect array");
    135     for (size_t i = 0; i < rects.size(); i++) {
    136         jobject rect = env->NewObject(rectClass, rectInit,
    137                 rects[i].x(), rects[i].y(),
    138                 rects[i].maxX(), rects[i].maxY());
    139         if (rect) {
    140             env->SetObjectArrayElement(array, i, rect);
    141             env->DeleteLocalRef(rect);
    142         }
    143     }
    144     env->DeleteLocalRef(rectClass);
    145     return array;
    146 }
    147 
    148 }
    149