OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 #include <AdblockPlus.h> |
| 19 #include "Utils.h" |
| 20 #include "JniJsValue.h" |
| 21 |
| 22 namespace AdblockPlus |
| 23 { |
| 24 namespace Android |
| 25 { |
| 26 |
| 27 jobject NewJniJsValue(JNIEnv* env, const AdblockPlus::JsValuePtr& jsValue) |
| 28 { |
| 29 jclass clazz = env->FindClass(PKG("JsValue")); |
| 30 jmethodID ctor = env->GetMethodID(clazz, "<init>", "(J)V"); |
| 31 jlong ptr = AdblockPlus::Android::JniPtr2Long(new AdblockPlus::JsValuePtr(jsVa
lue)); |
| 32 return env->NewObject(clazz, ctor, ptr); |
| 33 } |
| 34 |
| 35 AdblockPlus::JsValue* JniGetJsValue(jlong ptr) |
| 36 { |
| 37 return AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsValuePtr>(ptr)->ge
t(); |
| 38 } |
| 39 |
| 40 AdblockPlus::JsValuePtr& JniGetJsValuePtr(jlong ptr) |
| 41 { |
| 42 return *AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsValuePtr>(ptr); |
| 43 } |
| 44 |
| 45 jobject JniJsValueList2ArrayList(JNIEnv* env, AdblockPlus::JsValueList& list) |
| 46 { |
| 47 jobject arrayList = AdblockPlus::Android::NewJniArrayList(env); |
| 48 |
| 49 for (AdblockPlus::JsValueList::iterator it = list.begin(), end = list.end(); i
t != end; ++it) |
| 50 { |
| 51 AdblockPlus::Android::JniAddObjectToList(env, arrayList, AdblockPlus::Androi
d::NewJniJsValue(env, *it)); |
| 52 } |
| 53 |
| 54 return arrayList; |
| 55 } |
| 56 |
| 57 } // namespace Android |
| 58 } // namespace AdblockPlus |
| 59 |
| 60 static jboolean JNICALL JniIsUndefined(JNIEnv* env, jclass clazz, jlong ptr) |
| 61 { |
| 62 TRY |
| 63 { |
| 64 return AdblockPlus::Android::JniGetJsValue(ptr)->IsUndefined() ? JNI_TRUE :
JNI_FALSE; |
| 65 } |
| 66 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
| 67 } |
| 68 |
| 69 static jboolean JNICALL JniIsNull(JNIEnv* env, jclass clazz, jlong ptr) |
| 70 { |
| 71 TRY |
| 72 { |
| 73 return AdblockPlus::Android::JniGetJsValue(ptr)->IsNull() ? JNI_TRUE : JNI_F
ALSE; |
| 74 } |
| 75 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
| 76 } |
| 77 |
| 78 static jboolean JNICALL JniIsString(JNIEnv* env, jclass clazz, jlong ptr) |
| 79 { |
| 80 TRY |
| 81 { |
| 82 return AdblockPlus::Android::JniGetJsValue(ptr)->IsString() ? JNI_TRUE : JNI
_FALSE; |
| 83 } |
| 84 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
| 85 } |
| 86 |
| 87 static jboolean JNICALL JniIsBoolean(JNIEnv* env, jclass clazz, jlong ptr) |
| 88 { |
| 89 TRY |
| 90 { |
| 91 return AdblockPlus::Android::JniGetJsValue(ptr)->IsBool() ? JNI_TRUE : JNI_F
ALSE; |
| 92 } |
| 93 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
| 94 } |
| 95 |
| 96 static jboolean JNICALL JniIsObject(JNIEnv* env, jclass clazz, jlong ptr) |
| 97 { |
| 98 TRY |
| 99 { |
| 100 return AdblockPlus::Android::JniGetJsValue(ptr)->IsObject() ? JNI_TRUE : JNI
_FALSE; |
| 101 } |
| 102 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
| 103 } |
| 104 |
| 105 static jboolean JNICALL JniIsArray(JNIEnv* env, jclass clazz, jlong ptr) |
| 106 { |
| 107 TRY |
| 108 { |
| 109 return AdblockPlus::Android::JniGetJsValue(ptr)->IsArray() ? JNI_TRUE : JNI_
FALSE; |
| 110 } |
| 111 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
| 112 } |
| 113 |
| 114 static jboolean JNICALL JniIsFunction(JNIEnv* env, jclass clazz, jlong ptr) |
| 115 { |
| 116 TRY |
| 117 { |
| 118 return AdblockPlus::Android::JniGetJsValue(ptr)->IsFunction() ? JNI_TRUE : J
NI_FALSE; |
| 119 } |
| 120 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
| 121 } |
| 122 |
| 123 static jstring JNICALL JniAsString(JNIEnv* env, jclass clazz, jlong ptr) |
| 124 { |
| 125 TRY |
| 126 { |
| 127 return env->NewStringUTF(AdblockPlus::Android::JniGetJsValue(ptr)->AsString(
).c_str()); |
| 128 } |
| 129 CATCH_THROW_AND_RETURN(env, 0) |
| 130 } |
| 131 |
| 132 static jlong JNICALL JniAsLong(JNIEnv* env, jclass clazz, jlong ptr) |
| 133 { |
| 134 TRY |
| 135 { |
| 136 return static_cast<jlong>(AdblockPlus::Android::JniGetJsValue(ptr)->AsInt())
; |
| 137 } |
| 138 CATCH_THROW_AND_RETURN(env, 0) |
| 139 } |
| 140 |
| 141 static jboolean JNICALL JniAsBoolean(JNIEnv* env, jclass clazz, jlong ptr) |
| 142 { |
| 143 TRY |
| 144 { |
| 145 return AdblockPlus::Android::JniGetJsValue(ptr)->AsBool() ? JNI_TRUE : JNI_F
ALSE; |
| 146 } |
| 147 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
| 148 } |
| 149 |
| 150 static jboolean JNICALL JniIsValid(JNIEnv* env, jclass clazz, jlong ptr) |
| 151 { |
| 152 return AdblockPlus::Android::JniGetJsValue(ptr) ? JNI_TRUE : JNI_FALSE; |
| 153 } |
| 154 |
| 155 static jobject JNICALL JniAsList(JNIEnv* env, jclass clazz, jlong ptr) |
| 156 { |
| 157 TRY |
| 158 { |
| 159 AdblockPlus::JsValueList list = AdblockPlus::Android::JniGetJsValue(ptr)->As
List(); |
| 160 |
| 161 return AdblockPlus::Android::JniJsValueList2ArrayList(env, list); |
| 162 } |
| 163 CATCH_THROW_AND_RETURN(env, 0) |
| 164 } |
| 165 |
| 166 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) |
| 167 { |
| 168 delete AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsValuePtr>(ptr); |
| 169 } |
| 170 |
| 171 //std::vector<std::string> GetOwnPropertyNames() const; |
| 172 //JsValuePtr GetProperty(const std::string& name) const; |
| 173 //void SetProperty(const std::string& name, const std::string& val); |
| 174 //void SetProperty(const std::string& name, int64_t val); |
| 175 //void SetProperty(const std::string& name, bool val); |
| 176 //void SetProperty(const std::string& name, JsValuePtr value); |
| 177 //void SetProperty(const std::string& name, const char* val); |
| 178 //inline void SetProperty(const std::string& name, int val); |
| 179 //std::string GetClass() const; |
| 180 //JsValuePtr Call(const JsValueList& params = JsValueList(), AdblockPlus::JsValu
ePtr thisPtr = AdblockPlus::JsValuePtr()) const; |
| 181 |
| 182 static JNINativeMethod methods[] = |
| 183 { |
| 184 { (char*)"isUndefined", (char*)"(J)Z", (void*)JniIsUndefined }, |
| 185 { (char*)"isNull", (char*)"(J)Z", (void*)JniIsNull }, |
| 186 { (char*)"isString", (char*)"(J)Z", (void*)JniIsString }, |
| 187 { (char*)"isBoolean", (char*)"(J)Z", (void*)JniIsBoolean }, |
| 188 { (char*)"isObject", (char*)"(J)Z", (void*)JniIsObject }, |
| 189 { (char*)"isArray", (char*)"(J)Z", (void*)JniIsArray }, |
| 190 { (char*)"isFunction", (char*)"(J)Z", (void*)JniIsFunction }, |
| 191 { (char*)"asString", (char*)"(J)Ljava/lang/String;", (void*)JniAsString }, |
| 192 { (char*)"asLong", (char*)"(J)J", (void*)JniAsLong }, |
| 193 { (char*)"asBoolean", (char*)"(J)Z", (void*)JniAsBoolean }, |
| 194 { (char*)"asList", (char*)"(J)Ljava/util/List;", (void*)JniAsList }, |
| 195 { (char*)"isValid", (char*)"(J)Z", (void*)JniIsValid }, |
| 196 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }, }; |
| 197 |
| 198 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_android_api_JsValue_regis
terNatives(JNIEnv *env, jclass clazz) |
| 199 { |
| 200 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); |
| 201 } |
OLD | NEW |