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: Removed TODO from AppInfo. Created April 11, 2014, 1:28 p.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 = JniGetStringField(env, clazz, jAppInfo, "application");
27 appInfo.applicationVersion = JniGetStringField(env, clazz, jAppInfo, "applicat ionVersion");
28 appInfo.id = JniGetStringField(env, clazz, jAppInfo, "id");
29 appInfo.locale = JniGetStringField(env, clazz, jAppInfo, "locale");
30 appInfo.name = JniGetStringField(env, clazz, jAppInfo, "name");
31 appInfo.version = JniGetStringField(env, clazz, jAppInfo, "version");
32
33 appInfo.developmentBuild = JniGetBooleanField(env, clazz, jAppInfo, "developme ntBuild");
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 JniPtrToLong(new AdblockPlus::JsEnginePtr(AdblockPlus::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 JniLongToTypePtr<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 = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
57
58 JniEventCallback* callback = JniLongToTypePtr<JniEventCallback>(jCallbackPtr);
59 std::string eventName = JniJavaToStdString(env, jEventName);
60 AdblockPlus::JsEngine::EventCallback eCallback = std::tr1::bind(&JniEventCallb ack::Callback, callback, std::tr1::placeholders::_1);
61
62 try
63 {
64 engine->SetEventCallback(eventName, eCallback);
65 }
66 CATCH_AND_THROW(env)
67 }
68
69 static void JNICALL JniRemoveEventCallback(JNIEnv* env, jclass clazz, jlong ptr, jstring jEventName)
70 {
71 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
72
73 std::string eventName = JniJavaToStdString(env, jEventName);
74
75 try
76 {
77 engine->RemoveEventCallback(eventName);
78 }
79 CATCH_AND_THROW(env)
80 }
81
82 static jobject JNICALL JniEvaluate(JNIEnv* env, jclass clazz, jlong ptr, jstring jSource, jstring jFilename)
83 {
84 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
85
86 std::string source = JniJavaToStdString(env, jSource);
87 std::string filename = JniJavaToStdString(env, jFilename);
88
89 try
90 {
91 AdblockPlus::JsValuePtr jsValue = engine->Evaluate(source, filename);
92 return NewJniJsValue(env, jsValue);
93 }
94 CATCH_THROW_AND_RETURN(env, 0)
95 }
96
97 static void JNICALL JniTriggerEvent(JNIEnv* env, jclass clazz, jlong ptr, jstrin g jEventName, jarray jJsPtrs)
98 {
99 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
100 std::string eventName = JniJavaToStdString(env, jEventName);
101 AdblockPlus::JsValueList args;
102
103 if (jJsPtrs)
104 {
105 jlong* ptrs = (jlong*)env->GetPrimitiveArrayCritical(jJsPtrs, 0);
106
107 jsize length = env->GetArrayLength(jJsPtrs);
108
109 for (jsize i = 0; i < length; i++)
110 {
111 args.push_back(JniGetJsValuePtr(ptrs[i]));
112 }
113
114 env->ReleasePrimitiveArrayCritical(jJsPtrs, ptrs, JNI_ABORT);
115 }
116
117 try
118 {
119 engine->TriggerEvent(eventName, args);
120 }
121 CATCH_AND_THROW(env)
122 }
123
124 static void JNICALL JniSetDefaultFileSystem(JNIEnv* env, jclass clazz, jlong ptr , jstring jBasePath)
125 {
126 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
127
128 try
129 {
130 AdblockPlus::FileSystemPtr fileSystem(new AdblockPlus::DefaultFileSystem());
131
132 std::string basePath = JniJavaToStdString(env, jBasePath);
133 reinterpret_cast<AdblockPlus::DefaultFileSystem*>(fileSystem.get())->SetBase Path(basePath);
134
135 engine->SetFileSystem(fileSystem);
136 }
137 CATCH_AND_THROW(env)
138 }
139
140 static void JNICALL JniSetDefaultWebRequest(JNIEnv* env, jclass clazz, jlong ptr )
141 {
142 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
143
144 try
145 {
146 AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebRequest());
147
148 engine->SetWebRequest(webRequest);
149 }
150 CATCH_AND_THROW(env)
151 }
152
153 static void JNICALL JniSetDefaultLogSystem(JNIEnv* env, jclass clazz, jlong ptr)
154 {
155 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
156
157 try
158 {
159 AdblockPlus::LogSystemPtr logSystem(new AdblockPlus::DefaultLogSystem());
160
161 engine->SetLogSystem(logSystem);
162 }
163 CATCH_AND_THROW(env)
164 }
165
166 static void JNICALL JniSetLogSystem(JNIEnv* env, jclass clazz, jlong ptr, jlong logSystemPtr)
167 {
168 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
169
170 try
171 {
172 AdblockPlus::LogSystemPtr logSystem(JniLongToTypePtr<JniLogSystemCallback>(l ogSystemPtr));
173
174 engine->SetLogSystem(logSystem);
175 }
176 CATCH_AND_THROW(env)
177 }
178
179 static void JNICALL JniSetWebRequest(JNIEnv* env, jclass clazz, jlong ptr, jlong webRequestPtr)
180 {
181 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
182
183 try
184 {
185 AdblockPlus::WebRequestPtr& webRequest = *JniLongToTypePtr<AdblockPlus::WebR equestPtr>(webRequestPtr);
186
187 engine->SetWebRequest(webRequest);
188 }
189 CATCH_AND_THROW(env)
190 }
191
192 static jobject JNICALL JniNewLongValue(JNIEnv* env, jclass clazz, jlong ptr, jlo ng value)
193 {
194 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
195
196 try
197 {
198 AdblockPlus::JsValuePtr jsValue = engine->NewValue(static_cast<int64_t>(valu e));
199 return NewJniJsValue(env, jsValue);
200 }
201 CATCH_THROW_AND_RETURN(env, 0)
202 }
203
204 static jobject JNICALL JniNewBooleanValue(JNIEnv* env, jclass clazz, jlong ptr, jboolean value)
205 {
206 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
207
208 try
209 {
210 AdblockPlus::JsValuePtr jsValue = engine->NewValue(value == JNI_TRUE ? true : false);
211 return NewJniJsValue(env, jsValue);
212 }
213 CATCH_THROW_AND_RETURN(env, 0)
214 }
215
216 static jobject JNICALL JniNewStringValue(JNIEnv* env, jclass clazz, jlong ptr, j string value)
217 {
218 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
219
220 try
221 {
222 std::string strValue = JniJavaToStdString(env, value);
223 AdblockPlus::JsValuePtr jsValue = engine->NewValue(strValue);
224 return NewJniJsValue(env, jsValue);
225 }
226 CATCH_THROW_AND_RETURN(env, 0)
227 }
228
229 // TODO: List of functions that lack JNI bindings
230 //JsValuePtr NewObject();
231 //JsValuePtr NewCallback(v8::InvocationCallback callback);
232 //static JsEnginePtr FromArguments(const v8::Arguments& arguments);
233 //JsValueList ConvertArguments(const v8::Arguments& arguments);
234
235 static JNINativeMethod methods[] =
236 {
237 { (char*)"ctor", (char*)"(" TYP("AppInfo") ")J", (void*)JniCtor },
238 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor },
239
240 { (char*)"setEventCallback", (char*)"(JLjava/lang/String;J)V", (void*)JniSetEv entCallback },
241 { (char*)"removeEventCallback", (char*)"(JLjava/lang/String;)V", (void*)JniRem oveEventCallback },
242 { (char*)"triggerEvent", (char*)"(JLjava/lang/String;[J)V", (void*)JniTriggerE vent },
243
244 { (char*)"evaluate", (char*)"(JLjava/lang/String;Ljava/lang/String;)" TYP("JsV alue"), (void*)JniEvaluate },
245
246 { (char*)"setDefaultFileSystem", (char*)"(JLjava/lang/String;)V", (void*)JniSe tDefaultFileSystem },
247 { (char*)"setLogSystem", (char*)"(JJ)V", (void*)JniSetLogSystem },
248 { (char*)"setDefaultLogSystem", (char*)"(J)V", (void*)JniSetDefaultLogSystem } ,
249 { (char*)"setWebRequest", (char*)"(JJ)V", (void*)JniSetWebRequest },
250 { (char*)"setDefaultWebRequest", (char*)"(J)V", (void*)JniSetDefaultWebRequest },
251
252 { (char*)"newValue", (char*)"(JJ)"TYP("JsValue"), (void*)JniNewLongValue },
253 { (char*)"newValue", (char*)"(JZ)"TYP("JsValue"), (void*)JniNewBooleanValue },
254 { (char*)"newValue", (char*)"(JLjava/lang/String;)"TYP("JsValue"), (void*)JniN ewStringValue }
255 };
256
257 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsEngine_r egisterNatives(JNIEnv *env, jclass clazz)
258 {
259 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
260 }
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