OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 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 |
(...skipping 14 matching lines...) Expand all Loading... |
25 | 25 |
26 appInfo.application = JniGetStringField(env, clazz, jAppInfo, "application"); | 26 appInfo.application = JniGetStringField(env, clazz, jAppInfo, "application"); |
27 appInfo.applicationVersion = JniGetStringField(env, clazz, jAppInfo, "applicat
ionVersion"); | 27 appInfo.applicationVersion = JniGetStringField(env, clazz, jAppInfo, "applicat
ionVersion"); |
28 appInfo.locale = JniGetStringField(env, clazz, jAppInfo, "locale"); | 28 appInfo.locale = JniGetStringField(env, clazz, jAppInfo, "locale"); |
29 appInfo.name = JniGetStringField(env, clazz, jAppInfo, "name"); | 29 appInfo.name = JniGetStringField(env, clazz, jAppInfo, "name"); |
30 appInfo.version = JniGetStringField(env, clazz, jAppInfo, "version"); | 30 appInfo.version = JniGetStringField(env, clazz, jAppInfo, "version"); |
31 | 31 |
32 appInfo.developmentBuild = JniGetBooleanField(env, clazz, jAppInfo, "developme
ntBuild"); | 32 appInfo.developmentBuild = JniGetBooleanField(env, clazz, jAppInfo, "developme
ntBuild"); |
33 } | 33 } |
34 | 34 |
| 35 static AdblockPlus::JsEnginePtr& GetJsEnginePtrRef(jlong ptr) |
| 36 { |
| 37 return *JniLongToTypePtr<AdblockPlus::JsEnginePtr>(ptr); |
| 38 } |
| 39 |
35 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jobject jAppInfo) | 40 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jobject jAppInfo) |
36 { | 41 { |
37 AdblockPlus::AppInfo appInfo; | 42 AdblockPlus::AppInfo appInfo; |
38 | 43 |
39 TransformAppInfo(env, jAppInfo, appInfo); | 44 TransformAppInfo(env, jAppInfo, appInfo); |
40 | 45 |
41 try | 46 try |
42 { | 47 { |
43 return JniPtrToLong(new AdblockPlus::JsEnginePtr(AdblockPlus::JsEngine::New(
appInfo))); | 48 return JniPtrToLong(new AdblockPlus::JsEnginePtr(AdblockPlus::JsEngine::New(
appInfo))); |
44 } | 49 } |
45 CATCH_THROW_AND_RETURN(env, 0) | 50 CATCH_THROW_AND_RETURN(env, 0) |
46 } | 51 } |
47 | 52 |
48 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) | 53 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) |
49 { | 54 { |
50 delete JniLongToTypePtr<AdblockPlus::JsEnginePtr>(ptr); | 55 delete JniLongToTypePtr<AdblockPlus::JsEnginePtr>(ptr); |
51 } | 56 } |
52 | 57 |
53 static void JNICALL JniSetEventCallback(JNIEnv* env, jclass clazz, jlong ptr, js
tring jEventName, jlong jCallbackPtr) | 58 static void JNICALL JniSetEventCallback(JNIEnv* env, jclass clazz, jlong ptr, js
tring jEventName, jlong jCallbackPtr) |
54 { | 59 { |
55 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 60 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr); |
56 | 61 |
57 JniEventCallback* callback = JniLongToTypePtr<JniEventCallback>(jCallbackPtr); | 62 JniEventCallback* callback = JniLongToTypePtr<JniEventCallback>(jCallbackPtr); |
58 std::string eventName = JniJavaToStdString(env, jEventName); | 63 std::string eventName = JniJavaToStdString(env, jEventName); |
59 | 64 |
60 auto eCallback = [callback](AdblockPlus::JsValueList&& params) | 65 auto eCallback = [callback](AdblockPlus::JsValueList&& params) |
61 { | 66 { |
62 callback->Callback(std::move(params)); | 67 callback->Callback(std::move(params)); |
63 }; | 68 }; |
64 | 69 |
65 try | 70 try |
66 { | 71 { |
67 engine->SetEventCallback(eventName, eCallback); | 72 engine->SetEventCallback(eventName, eCallback); |
68 } | 73 } |
69 CATCH_AND_THROW(env) | 74 CATCH_AND_THROW(env) |
70 } | 75 } |
71 | 76 |
72 static void JNICALL JniRemoveEventCallback(JNIEnv* env, jclass clazz, jlong ptr,
jstring jEventName) | 77 static void JNICALL JniRemoveEventCallback(JNIEnv* env, jclass clazz, jlong ptr,
jstring jEventName) |
73 { | 78 { |
74 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 79 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr); |
75 | 80 |
76 std::string eventName = JniJavaToStdString(env, jEventName); | 81 std::string eventName = JniJavaToStdString(env, jEventName); |
77 | 82 |
78 try | 83 try |
79 { | 84 { |
80 engine->RemoveEventCallback(eventName); | 85 engine->RemoveEventCallback(eventName); |
81 } | 86 } |
82 CATCH_AND_THROW(env) | 87 CATCH_AND_THROW(env) |
83 } | 88 } |
84 | 89 |
85 static jobject JNICALL JniEvaluate(JNIEnv* env, jclass clazz, jlong ptr, jstring
jSource, jstring jFilename) | 90 static jobject JNICALL JniEvaluate(JNIEnv* env, jclass clazz, jlong ptr, jstring
jSource, jstring jFilename) |
86 { | 91 { |
87 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 92 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr); |
88 | 93 |
89 std::string source = JniJavaToStdString(env, jSource); | 94 std::string source = JniJavaToStdString(env, jSource); |
90 std::string filename = JniJavaToStdString(env, jFilename); | 95 std::string filename = JniJavaToStdString(env, jFilename); |
91 | 96 |
92 try | 97 try |
93 { | 98 { |
94 return NewJniJsValue(env, engine->Evaluate(source, filename)); | 99 return NewJniJsValue(env, engine->Evaluate(source, filename)); |
95 } | 100 } |
96 CATCH_THROW_AND_RETURN(env, 0) | 101 CATCH_THROW_AND_RETURN(env, 0) |
97 } | 102 } |
98 | 103 |
99 static void JNICALL JniTriggerEvent(JNIEnv* env, jclass clazz, jlong ptr, jstrin
g jEventName, jarray jJsPtrs) | 104 static void JNICALL JniTriggerEvent(JNIEnv* env, jclass clazz, jlong ptr, jstrin
g jEventName, jarray jJsPtrs) |
100 { | 105 { |
101 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 106 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr); |
102 std::string eventName = JniJavaToStdString(env, jEventName); | 107 std::string eventName = JniJavaToStdString(env, jEventName); |
103 AdblockPlus::JsValueList args; | 108 AdblockPlus::JsValueList args; |
104 | 109 |
105 if (jJsPtrs) | 110 if (jJsPtrs) |
106 { | 111 { |
107 jlong* ptrs = (jlong*)env->GetPrimitiveArrayCritical(jJsPtrs, 0); | 112 jlong* ptrs = (jlong*)env->GetPrimitiveArrayCritical(jJsPtrs, 0); |
108 | 113 |
109 jsize length = env->GetArrayLength(jJsPtrs); | 114 jsize length = env->GetArrayLength(jJsPtrs); |
110 | 115 |
111 for (jsize i = 0; i < length; i++) | 116 for (jsize i = 0; i < length; i++) |
112 { | 117 { |
113 args.push_back(JniGetJsValue(ptrs[i])); | 118 args.push_back(JniGetJsValue(ptrs[i])); |
114 } | 119 } |
115 | 120 |
116 env->ReleasePrimitiveArrayCritical(jJsPtrs, ptrs, JNI_ABORT); | 121 env->ReleasePrimitiveArrayCritical(jJsPtrs, ptrs, JNI_ABORT); |
117 } | 122 } |
118 | 123 |
119 try | 124 try |
120 { | 125 { |
121 engine->TriggerEvent(eventName, std::move(args)); | 126 engine->TriggerEvent(eventName, std::move(args)); |
122 } | 127 } |
123 CATCH_AND_THROW(env) | 128 CATCH_AND_THROW(env) |
124 } | 129 } |
125 | 130 |
126 static void JNICALL JniSetDefaultFileSystem(JNIEnv* env, jclass clazz, jlong ptr
, jstring jBasePath) | 131 static void JNICALL JniSetDefaultFileSystem(JNIEnv* env, jclass clazz, jlong ptr
, jstring jBasePath) |
127 { | 132 { |
128 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 133 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr); |
129 | 134 |
130 try | 135 try |
131 { | 136 { |
132 AdblockPlus::FileSystemPtr fileSystem(new AdblockPlus::DefaultFileSystem()); | 137 AdblockPlus::FileSystemPtr fileSystem(new AdblockPlus::DefaultFileSystem()); |
133 | 138 |
134 std::string basePath = JniJavaToStdString(env, jBasePath); | 139 std::string basePath = JniJavaToStdString(env, jBasePath); |
135 reinterpret_cast<AdblockPlus::DefaultFileSystem*>(fileSystem.get())->SetBase
Path(basePath); | 140 reinterpret_cast<AdblockPlus::DefaultFileSystem*>(fileSystem.get())->SetBase
Path(basePath); |
136 | 141 |
137 engine->SetFileSystem(fileSystem); | 142 engine->SetFileSystem(fileSystem); |
138 } | 143 } |
139 CATCH_AND_THROW(env) | 144 CATCH_AND_THROW(env) |
140 } | 145 } |
141 | 146 |
142 static void JNICALL JniSetDefaultLogSystem(JNIEnv* env, jclass clazz, jlong ptr) | 147 static void JNICALL JniSetDefaultLogSystem(JNIEnv* env, jclass clazz, jlong ptr) |
143 { | 148 { |
144 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 149 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr); |
145 | 150 |
146 try | 151 try |
147 { | 152 { |
148 AdblockPlus::LogSystemPtr logSystem(new AdblockPlus::DefaultLogSystem()); | 153 AdblockPlus::LogSystemPtr logSystem(new AdblockPlus::DefaultLogSystem()); |
149 | 154 |
150 engine->SetLogSystem(logSystem); | 155 engine->SetLogSystem(logSystem); |
151 } | 156 } |
152 CATCH_AND_THROW(env) | 157 CATCH_AND_THROW(env) |
153 } | 158 } |
154 | 159 |
155 static void JNICALL JniSetLogSystem(JNIEnv* env, jclass clazz, jlong ptr, jlong
logSystemPtr) | 160 static void JNICALL JniSetLogSystem(JNIEnv* env, jclass clazz, jlong ptr, jlong
logSystemPtr) |
156 { | 161 { |
157 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 162 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr); |
158 | 163 |
159 try | 164 try |
160 { | 165 { |
161 AdblockPlus::LogSystemPtr logSystem = *JniLongToTypePtr<AdblockPlus::LogSyst
emPtr>(logSystemPtr); | 166 AdblockPlus::LogSystemPtr logSystem = *JniLongToTypePtr<AdblockPlus::LogSyst
emPtr>(logSystemPtr); |
162 | 167 |
163 engine->SetLogSystem(logSystem); | 168 engine->SetLogSystem(logSystem); |
164 } | 169 } |
165 CATCH_AND_THROW(env) | 170 CATCH_AND_THROW(env) |
166 } | 171 } |
167 | 172 |
168 static void JNICALL JniSetWebRequest(JNIEnv* env, jclass clazz, jlong ptr, jlong
webRequestPtr) | 173 static void JNICALL JniSetWebRequest(JNIEnv* env, jclass clazz, jlong ptr, jlong
webRequestPtr) |
169 { | 174 { |
170 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 175 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr); |
171 | 176 |
172 try | 177 try |
173 { | 178 { |
174 auto& webRequest = *JniLongToTypePtr<AdblockPlus::WebRequestSharedPtr>(webRe
questPtr); | 179 auto& webRequest = *JniLongToTypePtr<AdblockPlus::WebRequestSharedPtr>(webRe
questPtr); |
175 | 180 |
176 engine->SetWebRequest(webRequest); | 181 engine->SetWebRequest(webRequest); |
177 } | 182 } |
178 CATCH_AND_THROW(env) | 183 CATCH_AND_THROW(env) |
179 } | 184 } |
180 | 185 |
181 static jobject JNICALL JniNewLongValue(JNIEnv* env, jclass clazz, jlong ptr, jlo
ng value) | 186 static jobject JNICALL JniNewLongValue(JNIEnv* env, jclass clazz, jlong ptr, jlo
ng value) |
182 { | 187 { |
183 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 188 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr); |
184 | 189 |
185 try | 190 try |
186 { | 191 { |
187 return NewJniJsValue(env, engine->NewValue(static_cast<int64_t>(value))); | 192 return NewJniJsValue(env, engine->NewValue(static_cast<int64_t>(value))); |
188 } | 193 } |
189 CATCH_THROW_AND_RETURN(env, 0) | 194 CATCH_THROW_AND_RETURN(env, 0) |
190 } | 195 } |
191 | 196 |
192 static jobject JNICALL JniNewBooleanValue(JNIEnv* env, jclass clazz, jlong ptr,
jboolean value) | 197 static jobject JNICALL JniNewBooleanValue(JNIEnv* env, jclass clazz, jlong ptr,
jboolean value) |
193 { | 198 { |
194 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 199 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr); |
195 | 200 |
196 try | 201 try |
197 { | 202 { |
198 return NewJniJsValue(env, engine->NewValue(value == JNI_TRUE ? true : false)
); | 203 return NewJniJsValue(env, engine->NewValue(value == JNI_TRUE ? true : false)
); |
199 } | 204 } |
200 CATCH_THROW_AND_RETURN(env, 0) | 205 CATCH_THROW_AND_RETURN(env, 0) |
201 } | 206 } |
202 | 207 |
203 static jobject JNICALL JniNewStringValue(JNIEnv* env, jclass clazz, jlong ptr, j
string value) | 208 static jobject JNICALL JniNewStringValue(JNIEnv* env, jclass clazz, jlong ptr, j
string value) |
204 { | 209 { |
205 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 210 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr); |
206 | 211 |
207 try | 212 try |
208 { | 213 { |
209 std::string strValue = JniJavaToStdString(env, value); | 214 std::string strValue = JniJavaToStdString(env, value); |
210 return NewJniJsValue(env, engine->NewValue(strValue)); | 215 return NewJniJsValue(env, engine->NewValue(strValue)); |
211 } | 216 } |
212 CATCH_THROW_AND_RETURN(env, 0) | 217 CATCH_THROW_AND_RETURN(env, 0) |
213 } | 218 } |
214 | 219 |
215 // TODO: List of functions that lack JNI bindings | 220 // TODO: List of functions that lack JNI bindings |
(...skipping 20 matching lines...) Expand all Loading... |
236 | 241 |
237 { (char*)"newValue", (char*)"(JJ)" TYP("JsValue"), (void*)JniNewLongValue }, | 242 { (char*)"newValue", (char*)"(JJ)" TYP("JsValue"), (void*)JniNewLongValue }, |
238 { (char*)"newValue", (char*)"(JZ)" TYP("JsValue"), (void*)JniNewBooleanValue }
, | 243 { (char*)"newValue", (char*)"(JZ)" TYP("JsValue"), (void*)JniNewBooleanValue }
, |
239 { (char*)"newValue", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)Jni
NewStringValue } | 244 { (char*)"newValue", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)Jni
NewStringValue } |
240 }; | 245 }; |
241 | 246 |
242 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsEngine_r
egisterNatives(JNIEnv *env, jclass clazz) | 247 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsEngine_r
egisterNatives(JNIEnv *env, jclass clazz) |
243 { | 248 { |
244 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); | 249 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); |
245 } | 250 } |
OLD | NEW |