Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: jni/JniJsEngine.cpp

Issue 6606493159784448: New JNI bindings (Closed)
Patch Set: Created March 14, 2014, 11:32 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « jni/JniFilterEngine.cpp ('k') | jni/JniJsValue.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "JniCallbacks.h"
21
22 static void TransformAppInfo(JNIEnv* env, jobject jAppInfo, AdblockPlus::AppInfo & appInfo)
23 {
24 jclass clazz = env->GetObjectClass(jAppInfo);
25
26 appInfo.application = AdblockPlus::Android::JniGetStringField(env, clazz, jApp Info, "application");
27 appInfo.applicationVersion = AdblockPlus::Android::JniGetStringField(env, claz z, jAppInfo, "applicationVersion");
28 appInfo.id = AdblockPlus::Android::JniGetStringField(env, clazz, jAppInfo, "id ");
29 appInfo.locale = AdblockPlus::Android::JniGetStringField(env, clazz, jAppInfo, "locale");
30 appInfo.name = AdblockPlus::Android::JniGetStringField(env, clazz, jAppInfo, " name");
31 appInfo.version = AdblockPlus::Android::JniGetStringField(env, clazz, jAppInfo , "version");
32
33 appInfo.developmentBuild = AdblockPlus::Android::JniGetBooleanField(env, clazz , jAppInfo, "developmentBuild");
34 }
35
36 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jobject jAppInfo)
37 {
38 AdblockPlus::AppInfo appInfo;
39
40 TransformAppInfo(env, jAppInfo, appInfo);
41
42 TRY
43 {
44 return AdblockPlus::Android::JniPtr2Long(new AdblockPlus::JsEnginePtr(Adbloc kPlus::JsEngine::New(appInfo)));
45 }
46 CATCH_THROW_AND_RETURN(env, 0)
47 }
48
49 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
50 {
51 delete AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsEnginePtr>(ptr);
52 }
53
54 static void JNICALL JniSetEventCallback(JNIEnv* env, jclass clazz, jlong ptr, js tring jEventName, jlong jCallbackPtr)
55 {
56 AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<Adbl ockPlus::JsEnginePtr>(ptr);
57
58 AdblockPlus::Android::JniEventCallback* callback = AdblockPlus::Android::JniLo ng2TypePtr<AdblockPlus::Android::JniEventCallback>(
59 jCallbackPtr);
60 std::string eventName = AdblockPlus::Android::JniJava2StdString(env, jEventNam e);
61 AdblockPlus::JsEngine::EventCallback eCallback = std::tr1::bind(&AdblockPlus:: Android::JniEventCallback::Callback, callback,
62 std::tr1::placeholders::_1);
63
64 TRY
65 {
66 engine->SetEventCallback(eventName, eCallback);
67 }
68 CATCH_AND_THROW(env)
69 }
70
71 static void JNICALL JniRemoveEventCallback(JNIEnv* env, jclass clazz, jlong ptr, jstring jEventName)
72 {
73 AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<Adbl ockPlus::JsEnginePtr>(ptr);
74
75 std::string eventName = AdblockPlus::Android::JniJava2StdString(env, jEventNam e);
76
77 TRY
78 {
79 engine->RemoveEventCallback(eventName);
80 }
81 CATCH_AND_THROW(env)
82 }
83
84 static jobject JNICALL JniEvaluate(JNIEnv* env, jclass clazz, jlong ptr, jstring jSource, jstring jFilename)
85 {
86 AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<Adbl ockPlus::JsEnginePtr>(ptr);
87
88 std::string source = AdblockPlus::Android::JniJava2StdString(env, jSource);
89 std::string filename = AdblockPlus::Android::JniJava2StdString(env, jFilename) ;
90
91 TRY
92 {
93 AdblockPlus::JsValuePtr jsValue = engine->Evaluate(source, filename);
94 return AdblockPlus::Android::NewJniJsValue(env, jsValue);
95 }
96 CATCH_THROW_AND_RETURN(env, 0)
97 }
98
99 static void JNICALL JniTriggerEvent(JNIEnv* env, jclass clazz, jlong ptr, jstrin g jEventName, jarray jJsPtrs)
100 {
101 AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<Adbl ockPlus::JsEnginePtr>(ptr);
102 std::string eventName = AdblockPlus::Android::JniJava2StdString(env, jEventNam e);
103 AdblockPlus::JsValueList args;
104
105 if (jJsPtrs)
106 {
107 jlong* ptrs = (jlong*)env->GetPrimitiveArrayCritical(jJsPtrs, 0);
108
109 jsize length = env->GetArrayLength(jJsPtrs);
110
111 for (jsize i = 0; i < length; i++)
112 {
113 args.push_back(AdblockPlus::Android::JniGetJsValuePtr(ptrs[i]));
114 }
115
116 env->ReleasePrimitiveArrayCritical(jJsPtrs, ptrs, JNI_ABORT);
117 }
118
119 TRY
120 {
121 engine->TriggerEvent(eventName, args);
122 }
123 CATCH_AND_THROW(env)
124 }
125
126 static void JNICALL JniSetDefaultFileSystem(JNIEnv* env, jclass clazz, jlong ptr , jstring jBasePath)
127 {
128 AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<Adbl ockPlus::JsEnginePtr>(ptr);
129
130 TRY
131 {
132 AdblockPlus::FileSystemPtr fileSystem(new AdblockPlus::DefaultFileSystem());
133
134 std::string basePath = AdblockPlus::Android::JniJava2StdString(env, jBasePat h);
135 reinterpret_cast<AdblockPlus::DefaultFileSystem*>(fileSystem.get())->SetBase Path(basePath);
136
137 engine->SetFileSystem(fileSystem);
138 }
139 CATCH_AND_THROW(env)
140 }
141
142 static void JNICALL JniSetDefaultWebRequest(JNIEnv* env, jclass clazz, jlong ptr )
143 {
144 AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<Adbl ockPlus::JsEnginePtr>(ptr);
145
146 TRY
147 {
148 AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebRequest());
149
150 engine->SetWebRequest(webRequest);
151 }
152 CATCH_AND_THROW(env)
153 }
154
155 static void JNICALL JniSetDefaultLogSystem(JNIEnv* env, jclass clazz, jlong ptr)
156 {
157 AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<Adbl ockPlus::JsEnginePtr>(ptr);
158
159 TRY
160 {
161 AdblockPlus::LogSystemPtr logSystem(new AdblockPlus::DefaultLogSystem());
162
163 engine->SetLogSystem(logSystem);
164 }
165 CATCH_AND_THROW(env)
166 }
167
168 static void JNICALL JniSetLogSystem(JNIEnv* env, jclass clazz, jlong ptr, jlong logSystemPtr)
169 {
170 AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<Adbl ockPlus::JsEnginePtr>(ptr);
171
172 TRY
173 {
174 AdblockPlus::LogSystemPtr logSystem(AdblockPlus::Android::JniLong2TypePtr<Ad blockPlus::Android::JniLogSystemCallback>(logSystemPtr));
175
176 engine->SetLogSystem(logSystem);
177 }
178 CATCH_AND_THROW(env)
179 }
180
181 static void JNICALL JniSetWebRequest(JNIEnv* env, jclass clazz, jlong ptr, jlong webRequestPtr)
182 {
183 AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<Adbl ockPlus::JsEnginePtr>(ptr);
184
185 TRY
186 {
187 AdblockPlus::WebRequestPtr& webRequest = *AdblockPlus::Android::JniLong2Type Ptr<AdblockPlus::WebRequestPtr>(webRequestPtr);
188
189 engine->SetWebRequest(webRequest);
190 }
191 CATCH_AND_THROW(env)
192 }
193
194 static jobject JNICALL JniNewLongValue(JNIEnv* env, jclass clazz, jlong ptr, jlo ng value)
195 {
196 AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<Adbl ockPlus::JsEnginePtr>(ptr);
197
198 TRY
199 {
200 AdblockPlus::JsValuePtr jsValue = engine->NewValue(static_cast<int64_t>(valu e));
201 return AdblockPlus::Android::NewJniJsValue(env, jsValue);
202 }
203 CATCH_THROW_AND_RETURN(env, 0)
204 }
205
206 static jobject JNICALL JniNewBooleanValue(JNIEnv* env, jclass clazz, jlong ptr, jboolean value)
207 {
208 AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<Adbl ockPlus::JsEnginePtr>(ptr);
209
210 TRY
211 {
212 AdblockPlus::JsValuePtr jsValue = engine->NewValue(value == JNI_TRUE ? true : false);
213 return AdblockPlus::Android::NewJniJsValue(env, jsValue);
214 }
215 CATCH_THROW_AND_RETURN(env, 0)
216 }
217
218 static jobject JNICALL JniNewStringValue(JNIEnv* env, jclass clazz, jlong ptr, j string value)
219 {
220 AdblockPlus::JsEnginePtr& engine = *AdblockPlus::Android::JniLong2TypePtr<Adbl ockPlus::JsEnginePtr>(ptr);
221
222 TRY
223 {
224 std::string strValue = AdblockPlus::Android::JniJava2StdString(env, value);
225 AdblockPlus::JsValuePtr jsValue = engine->NewValue(strValue);
226 return AdblockPlus::Android::NewJniJsValue(env, jsValue);
227 }
228 CATCH_THROW_AND_RETURN(env, 0)
229 }
230
231 //JsValuePtr NewObject();
232 //JsValuePtr NewCallback(v8::InvocationCallback callback);
233 //static JsEnginePtr FromArguments(const v8::Arguments& arguments);
234 //JsValueList ConvertArguments(const v8::Arguments& arguments);
235
236 static JNINativeMethod methods[] =
237 {
238 { (char*)"ctor", (char*)"(" TYP("AppInfo") ")J", (void*)JniCtor },
239 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor },
240
241 { (char*)"setEventCallback", (char*)"(JLjava/lang/String;J)V", (void*)JniSetEven tCallback },
242 { (char*)"removeEventCallback", (char*)"(JLjava/lang/String;)V", (void*)JniRemov eEventCallback },
243 { (char*)"triggerEvent", (char*)"(JLjava/lang/String;[J)V", (void*)JniTriggerEve nt },
244
245 { (char*)"evaluate", (char*)"(JLjava/lang/String;Ljava/lang/String;)" TYP("JsVal ue"), (void*)JniEvaluate },
246
247 { (char*)"setDefaultFileSystem", (char*)"(JLjava/lang/String;)V", (void*)JniSetD efaultFileSystem },
248 { (char*)"setLogSystem", (char*)"(JJ)V", (void*)JniSetLogSystem },
249 { (char*)"setDefaultLogSystem", (char*)"(J)V", (void*)JniSetDefaultLogSystem },
250 { (char*)"setWebRequest", (char*)"(JJ)V", (void*)JniSetWebRequest },
251 { (char*)"setDefaultWebRequest", (char*)"(J)V", (void*)JniSetDefaultWebRequest } ,
252
253 { (char*)"newValue", (char*)"(JJ)"TYP("JsValue"), (void*)JniNewLongValue },
254 { (char*)"newValue", (char*)"(JZ)"TYP("JsValue"), (void*)JniNewBooleanValue },
255 { (char*)"newValue", (char*)"(JLjava/lang/String;)"TYP("JsValue"), (void*)JniNew StringValue },
256
257 };
258
259 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_android_api_JsEngine_regi sterNatives(JNIEnv *env, jclass clazz)
260 {
261 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
262 }
OLDNEW
« no previous file with comments | « jni/JniFilterEngine.cpp ('k') | jni/JniJsValue.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld