Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright 2008, 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 #ifndef WebCoreJni_h
     27 #define WebCoreJni_h
     28 
     29 #include "ChromiumIncludes.h"
     30 #include "PlatformString.h"
     31 #include <jni.h>
     32 
     33 namespace android {
     34 
     35 // A helper class that automatically deletes the local reference to the jobject
     36 // returned from getRealObject.
     37 class AutoJObject {
     38 public:
     39     AutoJObject(const AutoJObject& other)
     40         : m_env(other.m_env)
     41         , m_obj(other.m_obj ? other.m_env->NewLocalRef(other.m_obj) : NULL) {}
     42     ~AutoJObject() {
     43         if (m_obj)
     44             m_env->DeleteLocalRef(m_obj);
     45     }
     46     jobject get() const {
     47         return m_obj;
     48     }
     49     // Releases the local reference to the caller. The caller *must* delete the
     50     // local reference when it is done with it.
     51     jobject release() {
     52         jobject obj = m_obj;
     53         m_obj = 0;
     54         return obj;
     55     }
     56     JNIEnv* env() const {
     57         return m_env;
     58     }
     59 private:
     60     AutoJObject(); // Not permitted.
     61     AutoJObject(JNIEnv* env, jobject obj)
     62         : m_env(env)
     63         , m_obj(obj) {}
     64     JNIEnv* m_env;
     65     jobject m_obj;
     66     friend AutoJObject getRealObject(JNIEnv*, jobject);
     67 };
     68 
     69 // Get the real object stored in the weak reference returned as an
     70 // AutoJObject.
     71 AutoJObject getRealObject(JNIEnv*, jobject);
     72 
     73 // Helper method for check java exceptions. Returns true if an exception
     74 // occurred and logs the exception.
     75 bool checkException(JNIEnv* env);
     76 
     77 // Create a WTF::String object from a jstring object.
     78 WTF::String jstringToWtfString(JNIEnv*, jstring);
     79 // Returns a local reference to a new jstring. If validOnZeroLength is true then
     80 // passing in an empty WTF String will result in an empty jstring. Otherwise
     81 // an empty WTF String returns 0.
     82 jstring wtfStringToJstring(JNIEnv*, const WTF::String&, bool validOnZeroLength = false);
     83 
     84 #if USE(CHROME_NETWORK_STACK)
     85 string16 jstringToString16(JNIEnv*, jstring);
     86 
     87 std::string jstringToStdString(JNIEnv*, jstring);
     88 // Returns a local reference to a new jstring. If validOnZeroLength is true then
     89 // passing in an empty std::string will result in an empty jstring. Otherwise
     90 // an empty std::string returns 0.
     91 jstring stdStringToJstring(JNIEnv*, const std::string&, bool validOnZeroLength = false);
     92 #endif
     93 
     94 }
     95 
     96 #endif
     97