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 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) | 22 static jboolean JNICALL JniIsUndefined(JNIEnv* env, jclass clazz, jlong ptr) |
61 { | 23 { |
62 TRY | 24 try |
63 { | 25 { |
64 return AdblockPlus::Android::JniGetJsValue(ptr)->IsUndefined() ? JNI_TRUE :
JNI_FALSE; | 26 return JniGetJsValue(ptr)->IsUndefined() ? JNI_TRUE : JNI_FALSE; |
65 } | 27 } |
66 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | 28 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
67 } | 29 } |
68 | 30 |
69 static jboolean JNICALL JniIsNull(JNIEnv* env, jclass clazz, jlong ptr) | 31 static jboolean JNICALL JniIsNull(JNIEnv* env, jclass clazz, jlong ptr) |
70 { | 32 { |
71 TRY | 33 try |
72 { | 34 { |
73 return AdblockPlus::Android::JniGetJsValue(ptr)->IsNull() ? JNI_TRUE : JNI_F
ALSE; | 35 return JniGetJsValue(ptr)->IsNull() ? JNI_TRUE : JNI_FALSE; |
74 } | 36 } |
75 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | 37 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
76 } | 38 } |
77 | 39 |
78 static jboolean JNICALL JniIsString(JNIEnv* env, jclass clazz, jlong ptr) | 40 static jboolean JNICALL JniIsString(JNIEnv* env, jclass clazz, jlong ptr) |
79 { | 41 { |
80 TRY | 42 try |
81 { | 43 { |
82 return AdblockPlus::Android::JniGetJsValue(ptr)->IsString() ? JNI_TRUE : JNI
_FALSE; | 44 return JniGetJsValue(ptr)->IsString() ? JNI_TRUE : JNI_FALSE; |
83 } | 45 } |
84 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | 46 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
85 } | 47 } |
86 | 48 |
87 static jboolean JNICALL JniIsBoolean(JNIEnv* env, jclass clazz, jlong ptr) | 49 static jboolean JNICALL JniIsBoolean(JNIEnv* env, jclass clazz, jlong ptr) |
88 { | 50 { |
89 TRY | 51 try |
90 { | 52 { |
91 return AdblockPlus::Android::JniGetJsValue(ptr)->IsBool() ? JNI_TRUE : JNI_F
ALSE; | 53 return JniGetJsValue(ptr)->IsBool() ? JNI_TRUE : JNI_FALSE; |
92 } | 54 } |
93 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | 55 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
94 } | 56 } |
95 | 57 |
96 static jboolean JNICALL JniIsObject(JNIEnv* env, jclass clazz, jlong ptr) | 58 static jboolean JNICALL JniIsObject(JNIEnv* env, jclass clazz, jlong ptr) |
97 { | 59 { |
98 TRY | 60 try |
99 { | 61 { |
100 return AdblockPlus::Android::JniGetJsValue(ptr)->IsObject() ? JNI_TRUE : JNI
_FALSE; | 62 return JniGetJsValue(ptr)->IsObject() ? JNI_TRUE : JNI_FALSE; |
101 } | 63 } |
102 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | 64 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
103 } | 65 } |
104 | 66 |
105 static jboolean JNICALL JniIsArray(JNIEnv* env, jclass clazz, jlong ptr) | 67 static jboolean JNICALL JniIsArray(JNIEnv* env, jclass clazz, jlong ptr) |
106 { | 68 { |
107 TRY | 69 try |
108 { | 70 { |
109 return AdblockPlus::Android::JniGetJsValue(ptr)->IsArray() ? JNI_TRUE : JNI_
FALSE; | 71 return JniGetJsValue(ptr)->IsArray() ? JNI_TRUE : JNI_FALSE; |
110 } | 72 } |
111 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | 73 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
112 } | 74 } |
113 | 75 |
114 static jboolean JNICALL JniIsFunction(JNIEnv* env, jclass clazz, jlong ptr) | 76 static jboolean JNICALL JniIsFunction(JNIEnv* env, jclass clazz, jlong ptr) |
115 { | 77 { |
116 TRY | 78 try |
117 { | 79 { |
118 return AdblockPlus::Android::JniGetJsValue(ptr)->IsFunction() ? JNI_TRUE : J
NI_FALSE; | 80 return JniGetJsValue(ptr)->IsFunction() ? JNI_TRUE : JNI_FALSE; |
119 } | 81 } |
120 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | 82 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
121 } | 83 } |
122 | 84 |
123 static jstring JNICALL JniAsString(JNIEnv* env, jclass clazz, jlong ptr) | 85 static jstring JNICALL JniAsString(JNIEnv* env, jclass clazz, jlong ptr) |
124 { | 86 { |
125 TRY | 87 try |
126 { | 88 { |
127 return env->NewStringUTF(AdblockPlus::Android::JniGetJsValue(ptr)->AsString(
).c_str()); | 89 return env->NewStringUTF(JniGetJsValue(ptr)->AsString().c_str()); |
128 } | 90 } |
129 CATCH_THROW_AND_RETURN(env, 0) | 91 CATCH_THROW_AND_RETURN(env, 0) |
130 } | 92 } |
131 | 93 |
132 static jlong JNICALL JniAsLong(JNIEnv* env, jclass clazz, jlong ptr) | 94 static jlong JNICALL JniAsLong(JNIEnv* env, jclass clazz, jlong ptr) |
133 { | 95 { |
134 TRY | 96 try |
135 { | 97 { |
136 return static_cast<jlong>(AdblockPlus::Android::JniGetJsValue(ptr)->AsInt())
; | 98 return static_cast<jlong>(JniGetJsValue(ptr)->AsInt()); |
137 } | 99 } |
138 CATCH_THROW_AND_RETURN(env, 0) | 100 CATCH_THROW_AND_RETURN(env, 0) |
139 } | 101 } |
140 | 102 |
141 static jboolean JNICALL JniAsBoolean(JNIEnv* env, jclass clazz, jlong ptr) | 103 static jboolean JNICALL JniAsBoolean(JNIEnv* env, jclass clazz, jlong ptr) |
142 { | 104 { |
143 TRY | 105 try |
144 { | 106 { |
145 return AdblockPlus::Android::JniGetJsValue(ptr)->AsBool() ? JNI_TRUE : JNI_F
ALSE; | 107 return JniGetJsValue(ptr)->AsBool() ? JNI_TRUE : JNI_FALSE; |
146 } | 108 } |
147 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | 109 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
148 } | 110 } |
149 | 111 |
150 static jboolean JNICALL JniIsValid(JNIEnv* env, jclass clazz, jlong ptr) | 112 static jboolean JNICALL JniIsValid(JNIEnv* env, jclass clazz, jlong ptr) |
151 { | 113 { |
152 return AdblockPlus::Android::JniGetJsValue(ptr) ? JNI_TRUE : JNI_FALSE; | 114 return JniGetJsValue(ptr) ? JNI_TRUE : JNI_FALSE; |
153 } | 115 } |
154 | 116 |
155 static jobject JNICALL JniAsList(JNIEnv* env, jclass clazz, jlong ptr) | 117 static jobject JNICALL JniAsList(JNIEnv* env, jclass clazz, jlong ptr) |
156 { | 118 { |
157 TRY | 119 try |
158 { | 120 { |
159 AdblockPlus::JsValueList list = AdblockPlus::Android::JniGetJsValue(ptr)->As
List(); | 121 AdblockPlus::JsValueList list = JniGetJsValue(ptr)->AsList(); |
160 | 122 |
161 return AdblockPlus::Android::JniJsValueList2ArrayList(env, list); | 123 return JniJsValueListToArrayList(env, list); |
162 } | 124 } |
163 CATCH_THROW_AND_RETURN(env, 0) | 125 CATCH_THROW_AND_RETURN(env, 0) |
164 } | 126 } |
165 | 127 |
166 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) | 128 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) |
167 { | 129 { |
168 delete AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsValuePtr>(ptr); | 130 delete JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr); |
169 } | 131 } |
170 | 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 |
171 //std::vector<std::string> GetOwnPropertyNames() const; | 164 //std::vector<std::string> GetOwnPropertyNames() const; |
172 //JsValuePtr GetProperty(const std::string& name) const; | 165 //JsValuePtr GetProperty(const std::string& name) const; |
173 //void SetProperty(const std::string& name, const std::string& val); | 166 //void SetProperty(const std::string& name, const std::string& val); |
174 //void SetProperty(const std::string& name, int64_t val); | 167 //void SetProperty(const std::string& name, int64_t val); |
175 //void SetProperty(const std::string& name, bool val); | 168 //void SetProperty(const std::string& name, bool val); |
176 //void SetProperty(const std::string& name, JsValuePtr value); | 169 //void SetProperty(const std::string& name, JsValuePtr value); |
177 //void SetProperty(const std::string& name, const char* val); | 170 //void SetProperty(const std::string& name, const char* val); |
178 //inline void SetProperty(const std::string& name, int val); | 171 //inline void SetProperty(const std::string& name, int val); |
179 //std::string GetClass() const; | 172 //std::string GetClass() const; |
180 //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; |
181 | 174 |
182 static JNINativeMethod methods[] = | 175 static JNINativeMethod methods[] = |
183 { | 176 { |
184 { (char*)"isUndefined", (char*)"(J)Z", (void*)JniIsUndefined }, | 177 { (char*)"isUndefined", (char*)"(J)Z", (void*)JniIsUndefined }, |
185 { (char*)"isNull", (char*)"(J)Z", (void*)JniIsNull }, | 178 { (char*)"isNull", (char*)"(J)Z", (void*)JniIsNull }, |
186 { (char*)"isString", (char*)"(J)Z", (void*)JniIsString }, | 179 { (char*)"isString", (char*)"(J)Z", (void*)JniIsString }, |
187 { (char*)"isBoolean", (char*)"(J)Z", (void*)JniIsBoolean }, | 180 { (char*)"isBoolean", (char*)"(J)Z", (void*)JniIsBoolean }, |
188 { (char*)"isObject", (char*)"(J)Z", (void*)JniIsObject }, | 181 { (char*)"isObject", (char*)"(J)Z", (void*)JniIsObject }, |
189 { (char*)"isArray", (char*)"(J)Z", (void*)JniIsArray }, | 182 { (char*)"isArray", (char*)"(J)Z", (void*)JniIsArray }, |
190 { (char*)"isFunction", (char*)"(J)Z", (void*)JniIsFunction }, | 183 { (char*)"isFunction", (char*)"(J)Z", (void*)JniIsFunction }, |
191 { (char*)"asString", (char*)"(J)Ljava/lang/String;", (void*)JniAsString }, | 184 { (char*)"asString", (char*)"(J)Ljava/lang/String;", (void*)JniAsString }, |
192 { (char*)"asLong", (char*)"(J)J", (void*)JniAsLong }, | 185 { (char*)"asLong", (char*)"(J)J", (void*)JniAsLong }, |
193 { (char*)"asBoolean", (char*)"(J)Z", (void*)JniAsBoolean }, | 186 { (char*)"asBoolean", (char*)"(J)Z", (void*)JniAsBoolean }, |
194 { (char*)"asList", (char*)"(J)Ljava/util/List;", (void*)JniAsList }, | 187 { (char*)"asList", (char*)"(J)Ljava/util/List;", (void*)JniAsList }, |
195 { (char*)"isValid", (char*)"(J)Z", (void*)JniIsValid }, | 188 { (char*)"isValid", (char*)"(J)Z", (void*)JniIsValid }, |
196 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }, }; | 189 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } |
| 190 }; |
197 | 191 |
198 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) |
199 { | 193 { |
200 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); | 194 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); |
201 } | 195 } |
LEFT | RIGHT |