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