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