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 "WebCoreJni.h"
     30 
     31 #include "NotImplemented.h"
     32 #include <JNIUtility.h>
     33 #include <jni.h>
     34 #include <utils/Log.h>
     35 
     36 namespace android {
     37 
     38 AutoJObject getRealObject(JNIEnv* env, jobject obj)
     39 {
     40     jobject real = env->NewLocalRef(obj);
     41     LOG_ASSERT(real, "The real object has been deleted!");
     42     return AutoJObject(env, real);
     43 }
     44 
     45 /**
     46  * Helper method for checking java exceptions
     47  * @return true if an exception occurred.
     48  */
     49 bool checkException(JNIEnv* env)
     50 {
     51     if (env->ExceptionCheck() != 0)
     52     {
     53         LOGE("*** Uncaught exception returned from Java call!\n");
     54         env->ExceptionDescribe();
     55         return true;
     56     }
     57     return false;
     58 }
     59 
     60 // This method is safe to call from the ui thread and the WebCore thread.
     61 WTF::String jstringToWtfString(JNIEnv* env, jstring str)
     62 {
     63     if (!str || !env)
     64         return WTF::String();
     65     const jchar* s = env->GetStringChars(str, NULL);
     66     if (!s)
     67         return WTF::String();
     68     WTF::String ret(s, env->GetStringLength(str));
     69     env->ReleaseStringChars(str, s);
     70     checkException(env);
     71     return ret;
     72 }
     73 
     74 jstring wtfStringToJstring(JNIEnv* env, const WTF::String& str, bool validOnZeroLength)
     75 {
     76     int length = str.length();
     77     return length || validOnZeroLength ? env->NewString(str.characters(), length) : 0;
     78 }
     79 
     80 
     81 #if USE(CHROME_NETWORK_STACK)
     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 #endif
    116 
    117 }
    118