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

Side by Side Diff: libadblockplus-android/jni/JniJsEngine.cpp

Issue 29422558: Issue 5167 - Update to use libadblockplus revision dca8df9af1a7 (Closed)
Patch Set: removed unneeded intermediate variable Created April 26, 2017, 6:57 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
OLDNEW
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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 { 49 {
50 delete JniLongToTypePtr<AdblockPlus::JsEnginePtr>(ptr); 50 delete JniLongToTypePtr<AdblockPlus::JsEnginePtr>(ptr);
51 } 51 }
52 52
53 static void JNICALL JniSetEventCallback(JNIEnv* env, jclass clazz, jlong ptr, js tring jEventName, jlong jCallbackPtr) 53 static void JNICALL JniSetEventCallback(JNIEnv* env, jclass clazz, jlong ptr, js tring jEventName, jlong jCallbackPtr)
54 { 54 {
55 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 55 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
56 56
57 JniEventCallback* callback = JniLongToTypePtr<JniEventCallback>(jCallbackPtr); 57 JniEventCallback* callback = JniLongToTypePtr<JniEventCallback>(jCallbackPtr);
58 std::string eventName = JniJavaToStdString(env, jEventName); 58 std::string eventName = JniJavaToStdString(env, jEventName);
59 AdblockPlus::JsEngine::EventCallback eCallback = std::bind(&JniEventCallback:: Callback, callback, std::placeholders::_1); 59 const AdblockPlus::JsEngine::EventCallback eCallback =
sergei 2017/04/26 09:20:59 the `const` is not required here.
anton 2017/04/26 10:22:59 Acknowledged.
60 std::bind(&JniEventCallback::Callback, callback, std::placeholders::_1);
60 61
61 try 62 try
62 { 63 {
63 engine->SetEventCallback(eventName, eCallback); 64 engine->SetEventCallback(eventName, eCallback);
64 } 65 }
65 CATCH_AND_THROW(env) 66 CATCH_AND_THROW(env)
66 } 67 }
67 68
68 static void JNICALL JniRemoveEventCallback(JNIEnv* env, jclass clazz, jlong ptr, jstring jEventName) 69 static void JNICALL JniRemoveEventCallback(JNIEnv* env, jclass clazz, jlong ptr, jstring jEventName)
69 { 70 {
(...skipping 10 matching lines...) Expand all
80 81
81 static jobject JNICALL JniEvaluate(JNIEnv* env, jclass clazz, jlong ptr, jstring jSource, jstring jFilename) 82 static jobject JNICALL JniEvaluate(JNIEnv* env, jclass clazz, jlong ptr, jstring jSource, jstring jFilename)
82 { 83 {
83 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 84 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
84 85
85 std::string source = JniJavaToStdString(env, jSource); 86 std::string source = JniJavaToStdString(env, jSource);
86 std::string filename = JniJavaToStdString(env, jFilename); 87 std::string filename = JniJavaToStdString(env, jFilename);
87 88
88 try 89 try
89 { 90 {
90 AdblockPlus::JsValuePtr jsValue = engine->Evaluate(source, filename); 91 AdblockPlus::JsValue jsValue = engine->Evaluate(source, filename);
91 return NewJniJsValue(env, jsValue); 92 return NewJniJsValue(env, jsValue);
sergei 2017/04/26 09:20:59 to avoid call of a copy-ctr one has to either retu
anton 2017/04/26 10:22:58 Acknowledged.
92 } 93 }
93 CATCH_THROW_AND_RETURN(env, 0) 94 CATCH_THROW_AND_RETURN(env, 0)
94 } 95 }
95 96
96 static void JNICALL JniTriggerEvent(JNIEnv* env, jclass clazz, jlong ptr, jstrin g jEventName, jarray jJsPtrs) 97 static void JNICALL JniTriggerEvent(JNIEnv* env, jclass clazz, jlong ptr, jstrin g jEventName, jarray jJsPtrs)
sergei 2017/04/26 09:20:59 I wonder we need it for. TriggerEvent is called on
anton 2017/04/26 10:22:59 this was written from the very beginning and i don
sergei 2017/04/26 14:12:56 I totally agree that it should not be in this code
anton 2017/04/27 06:21:00 feel free to add a ticket to libadblockplus-androi
97 { 98 {
98 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 99 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
99 std::string eventName = JniJavaToStdString(env, jEventName); 100 std::string eventName = JniJavaToStdString(env, jEventName);
100 AdblockPlus::JsValueList args; 101 AdblockPlus::JsValueList args;
101 102
102 if (jJsPtrs) 103 if (jJsPtrs)
103 { 104 {
104 jlong* ptrs = (jlong*)env->GetPrimitiveArrayCritical(jJsPtrs, 0); 105 jlong* ptrs = (jlong*)env->GetPrimitiveArrayCritical(jJsPtrs, 0);
105 106
106 jsize length = env->GetArrayLength(jJsPtrs); 107 jsize length = env->GetArrayLength(jJsPtrs);
107 108
108 for (jsize i = 0; i < length; i++) 109 for (jsize i = 0; i < length; i++)
109 { 110 {
110 args.push_back(JniGetJsValuePtr(ptrs[i])); 111 args.push_back(JniGetJsValue(ptrs[i]));
111 } 112 }
112 113
113 env->ReleasePrimitiveArrayCritical(jJsPtrs, ptrs, JNI_ABORT); 114 env->ReleasePrimitiveArrayCritical(jJsPtrs, ptrs, JNI_ABORT);
114 } 115 }
115 116
116 try 117 try
117 { 118 {
118 engine->TriggerEvent(eventName, args); 119 engine->TriggerEvent(eventName, args);
119 } 120 }
120 CATCH_AND_THROW(env) 121 CATCH_AND_THROW(env)
121 } 122 }
122 123
123 static void JNICALL JniSetDefaultFileSystem(JNIEnv* env, jclass clazz, jlong ptr , jstring jBasePath) 124 static void JNICALL JniSetDefaultFileSystem(JNIEnv* env, jclass clazz, jlong ptr , jstring jBasePath)
124 { 125 {
125 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 126 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
126 127
127 try 128 try
128 { 129 {
129 AdblockPlus::FileSystemPtr fileSystem(new AdblockPlus::DefaultFileSystem()); 130 const AdblockPlus::FileSystemPtr fileSystem(new AdblockPlus::DefaultFileSyst em());
sergei 2017/04/26 09:20:59 const is not required here.
anton 2017/04/26 10:22:59 Acknowledged.
130 131
131 std::string basePath = JniJavaToStdString(env, jBasePath); 132 std::string basePath = JniJavaToStdString(env, jBasePath);
132 reinterpret_cast<AdblockPlus::DefaultFileSystem*>(fileSystem.get())->SetBase Path(basePath); 133 reinterpret_cast<AdblockPlus::DefaultFileSystem*>(fileSystem.get())->SetBase Path(basePath);
133 134
134 engine->SetFileSystem(fileSystem); 135 engine->SetFileSystem(fileSystem);
135 } 136 }
136 CATCH_AND_THROW(env) 137 CATCH_AND_THROW(env)
137 } 138 }
138 139
139 static void JNICALL JniSetDefaultWebRequest(JNIEnv* env, jclass clazz, jlong ptr ) 140 static void JNICALL JniSetDefaultWebRequest(JNIEnv* env, jclass clazz, jlong ptr )
140 { 141 {
141 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 142 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
142 143
143 try 144 try
144 { 145 {
145 AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebRequest()); 146 const AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebReque st());
sergei 2017/04/26 09:20:59 const is not required here.
anton 2017/04/26 10:22:59 Acknowledged.
146
147 engine->SetWebRequest(webRequest); 147 engine->SetWebRequest(webRequest);
148 } 148 }
149 CATCH_AND_THROW(env) 149 CATCH_AND_THROW(env)
150 } 150 }
151 151
152 static void JNICALL JniSetDefaultLogSystem(JNIEnv* env, jclass clazz, jlong ptr) 152 static void JNICALL JniSetDefaultLogSystem(JNIEnv* env, jclass clazz, jlong ptr)
153 { 153 {
154 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 154 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
155 155
156 try 156 try
157 { 157 {
158 AdblockPlus::LogSystemPtr logSystem(new AdblockPlus::DefaultLogSystem()); 158 const AdblockPlus::LogSystemPtr logSystem(new AdblockPlus::DefaultLogSystem( ));
sergei 2017/04/26 09:20:59 const is not required here.
159 159
160 engine->SetLogSystem(logSystem); 160 engine->SetLogSystem(logSystem);
161 } 161 }
162 CATCH_AND_THROW(env) 162 CATCH_AND_THROW(env)
163 } 163 }
164 164
165 static void JNICALL JniSetLogSystem(JNIEnv* env, jclass clazz, jlong ptr, jlong logSystemPtr) 165 static void JNICALL JniSetLogSystem(JNIEnv* env, jclass clazz, jlong ptr, jlong logSystemPtr)
166 { 166 {
167 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 167 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
168 168
169 try 169 try
170 { 170 {
171 AdblockPlus::LogSystemPtr logSystem = *JniLongToTypePtr<AdblockPlus::LogSyst emPtr>(logSystemPtr); 171 const AdblockPlus::LogSystemPtr logSystem = *JniLongToTypePtr<AdblockPlus::L ogSystemPtr>(logSystemPtr);
sergei 2017/04/26 09:20:59 const is not required here.
anton 2017/04/26 10:22:59 Acknowledged.
172 172
173 engine->SetLogSystem(logSystem); 173 engine->SetLogSystem(logSystem);
174 } 174 }
175 CATCH_AND_THROW(env) 175 CATCH_AND_THROW(env)
176 } 176 }
177 177
178 static void JNICALL JniSetWebRequest(JNIEnv* env, jclass clazz, jlong ptr, jlong webRequestPtr) 178 static void JNICALL JniSetWebRequest(JNIEnv* env, jclass clazz, jlong ptr, jlong webRequestPtr)
179 { 179 {
180 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 180 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
181 181
182 try 182 try
183 { 183 {
184 AdblockPlus::WebRequestPtr& webRequest = *JniLongToTypePtr<AdblockPlus::WebR equestPtr>(webRequestPtr); 184 const AdblockPlus::WebRequestPtr& webRequest =
sergei 2017/04/26 09:20:59 const is not required here.
anton 2017/04/26 10:22:59 Acknowledged.
185 *JniLongToTypePtr<AdblockPlus::WebRequestPtr>(webRequestPtr);
185 186
186 engine->SetWebRequest(webRequest); 187 engine->SetWebRequest(webRequest);
187 } 188 }
188 CATCH_AND_THROW(env) 189 CATCH_AND_THROW(env)
189 } 190 }
190 191
191 static jobject JNICALL JniNewLongValue(JNIEnv* env, jclass clazz, jlong ptr, jlo ng value) 192 static jobject JNICALL JniNewLongValue(JNIEnv* env, jclass clazz, jlong ptr, jlo ng value)
192 { 193 {
193 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 194 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
194 195
195 try 196 try
196 { 197 {
197 AdblockPlus::JsValuePtr jsValue = engine->NewValue(static_cast<int64_t>(valu e)); 198 AdblockPlus::JsValue jsValue = engine->NewValue(static_cast<int64_t>(value)) ;
sergei 2017/04/26 09:20:59 here and blow, see comment above to avoid copy-ctr
anton 2017/04/26 10:22:59 Acknowledged.
198 return NewJniJsValue(env, jsValue); 199 return NewJniJsValue(env, jsValue);
199 } 200 }
200 CATCH_THROW_AND_RETURN(env, 0) 201 CATCH_THROW_AND_RETURN(env, 0)
201 } 202 }
202 203
203 static jobject JNICALL JniNewBooleanValue(JNIEnv* env, jclass clazz, jlong ptr, jboolean value) 204 static jobject JNICALL JniNewBooleanValue(JNIEnv* env, jclass clazz, jlong ptr, jboolean value)
204 { 205 {
205 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 206 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
206 207
207 try 208 try
208 { 209 {
209 AdblockPlus::JsValuePtr jsValue = engine->NewValue(value == JNI_TRUE ? true : false); 210 AdblockPlus::JsValue jsValue = engine->NewValue(value == JNI_TRUE ? true : f alse);
210 return NewJniJsValue(env, jsValue); 211 return NewJniJsValue(env, jsValue);
211 } 212 }
212 CATCH_THROW_AND_RETURN(env, 0) 213 CATCH_THROW_AND_RETURN(env, 0)
213 } 214 }
214 215
215 static jobject JNICALL JniNewStringValue(JNIEnv* env, jclass clazz, jlong ptr, j string value) 216 static jobject JNICALL JniNewStringValue(JNIEnv* env, jclass clazz, jlong ptr, j string value)
216 { 217 {
217 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 218 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
218 219
219 try 220 try
220 { 221 {
221 std::string strValue = JniJavaToStdString(env, value); 222 std::string strValue = JniJavaToStdString(env, value);
222 AdblockPlus::JsValuePtr jsValue = engine->NewValue(strValue); 223 AdblockPlus::JsValue jsValue = engine->NewValue(strValue);
223 return NewJniJsValue(env, jsValue); 224 return NewJniJsValue(env, jsValue);
224 } 225 }
225 CATCH_THROW_AND_RETURN(env, 0) 226 CATCH_THROW_AND_RETURN(env, 0)
226 } 227 }
227 228
228 // TODO: List of functions that lack JNI bindings 229 // TODO: List of functions that lack JNI bindings
229 //JsValuePtr NewObject(); 230 //JsValue NewObject();
230 //JsValuePtr NewCallback(v8::InvocationCallback callback); 231 //JsValue NewCallback(v8::InvocationCallback callback);
231 //static JsEnginePtr FromArguments(const v8::Arguments& arguments); 232 //static JsEnginePtr FromArguments(const v8::Arguments& arguments);
232 //JsValueList ConvertArguments(const v8::Arguments& arguments); 233 //JsValueList ConvertArguments(const v8::Arguments& arguments);
233 234
234 static JNINativeMethod methods[] = 235 static JNINativeMethod methods[] =
235 { 236 {
236 { (char*)"ctor", (char*)"(" TYP("AppInfo") ")J", (void*)JniCtor }, 237 { (char*)"ctor", (char*)"(" TYP("AppInfo") ")J", (void*)JniCtor },
237 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }, 238 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor },
238 239
239 { (char*)"setEventCallback", (char*)"(JLjava/lang/String;J)V", (void*)JniSetEv entCallback }, 240 { (char*)"setEventCallback", (char*)"(JLjava/lang/String;J)V", (void*)JniSetEv entCallback },
240 { (char*)"removeEventCallback", (char*)"(JLjava/lang/String;)V", (void*)JniRem oveEventCallback }, 241 { (char*)"removeEventCallback", (char*)"(JLjava/lang/String;)V", (void*)JniRem oveEventCallback },
241 { (char*)"triggerEvent", (char*)"(JLjava/lang/String;[J)V", (void*)JniTriggerE vent }, 242 { (char*)"triggerEvent", (char*)"(JLjava/lang/String;[J)V", (void*)JniTriggerE vent },
242 243
243 { (char*)"evaluate", (char*)"(JLjava/lang/String;Ljava/lang/String;)" TYP("JsV alue"), (void*)JniEvaluate }, 244 { (char*)"evaluate", (char*)"(JLjava/lang/String;Ljava/lang/String;)" TYP("JsV alue"), (void*)JniEvaluate },
244 245
245 { (char*)"setDefaultFileSystem", (char*)"(JLjava/lang/String;)V", (void*)JniSe tDefaultFileSystem }, 246 { (char*)"setDefaultFileSystem", (char*)"(JLjava/lang/String;)V", (void*)JniSe tDefaultFileSystem },
246 { (char*)"setLogSystem", (char*)"(JJ)V", (void*)JniSetLogSystem }, 247 { (char*)"setLogSystem", (char*)"(JJ)V", (void*)JniSetLogSystem },
247 { (char*)"setDefaultLogSystem", (char*)"(J)V", (void*)JniSetDefaultLogSystem } , 248 { (char*)"setDefaultLogSystem", (char*)"(J)V", (void*)JniSetDefaultLogSystem } ,
248 { (char*)"setWebRequest", (char*)"(JJ)V", (void*)JniSetWebRequest }, 249 { (char*)"setWebRequest", (char*)"(JJ)V", (void*)JniSetWebRequest },
249 { (char*)"setDefaultWebRequest", (char*)"(J)V", (void*)JniSetDefaultWebRequest }, 250 { (char*)"setDefaultWebRequest", (char*)"(J)V", (void*)JniSetDefaultWebRequest },
250 251
251 { (char*)"newValue", (char*)"(JJ)" TYP("JsValue"), (void*)JniNewLongValue }, 252 { (char*)"newValue", (char*)"(JJ)" TYP("JsValue"), (void*)JniNewLongValue },
252 { (char*)"newValue", (char*)"(JZ)" TYP("JsValue"), (void*)JniNewBooleanValue } , 253 { (char*)"newValue", (char*)"(JZ)" TYP("JsValue"), (void*)JniNewBooleanValue } ,
253 { (char*)"newValue", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)Jni NewStringValue } 254 { (char*)"newValue", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)Jni NewStringValue }
254 }; 255 };
255 256
256 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsEngine_r egisterNatives(JNIEnv *env, jclass clazz) 257 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsEngine_r egisterNatives(JNIEnv *env, jclass clazz)
257 { 258 {
258 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); 259 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
259 } 260 }
OLDNEW

Powered by Google App Engine
This is Rietveld