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: optimizations by serge, using newer libadblockplus-binaries Created May 5, 2017, 11:33 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
60 auto eCallback = [callback](AdblockPlus::JsValueList&& params)
61 {
62 callback->Callback(std::move(params));
63 };
60 64
61 try 65 try
62 { 66 {
63 engine->SetEventCallback(eventName, eCallback); 67 engine->SetEventCallback(eventName, eCallback);
64 } 68 }
65 CATCH_AND_THROW(env) 69 CATCH_AND_THROW(env)
66 } 70 }
67 71
68 static void JNICALL JniRemoveEventCallback(JNIEnv* env, jclass clazz, jlong ptr, jstring jEventName) 72 static void JNICALL JniRemoveEventCallback(JNIEnv* env, jclass clazz, jlong ptr, jstring jEventName)
69 { 73 {
(...skipping 10 matching lines...) Expand all
80 84
81 static jobject JNICALL JniEvaluate(JNIEnv* env, jclass clazz, jlong ptr, jstring jSource, jstring jFilename) 85 static jobject JNICALL JniEvaluate(JNIEnv* env, jclass clazz, jlong ptr, jstring jSource, jstring jFilename)
82 { 86 {
83 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 87 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
84 88
85 std::string source = JniJavaToStdString(env, jSource); 89 std::string source = JniJavaToStdString(env, jSource);
86 std::string filename = JniJavaToStdString(env, jFilename); 90 std::string filename = JniJavaToStdString(env, jFilename);
87 91
88 try 92 try
89 { 93 {
90 AdblockPlus::JsValuePtr jsValue = engine->Evaluate(source, filename); 94 return NewJniJsValue(env, engine->Evaluate(source, filename));
91 return NewJniJsValue(env, jsValue);
92 } 95 }
93 CATCH_THROW_AND_RETURN(env, 0) 96 CATCH_THROW_AND_RETURN(env, 0)
94 } 97 }
95 98
96 static void JNICALL JniTriggerEvent(JNIEnv* env, jclass clazz, jlong ptr, jstrin g jEventName, jarray jJsPtrs) 99 static void JNICALL JniTriggerEvent(JNIEnv* env, jclass clazz, jlong ptr, jstrin g jEventName, jarray jJsPtrs)
97 { 100 {
98 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 101 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
99 std::string eventName = JniJavaToStdString(env, jEventName); 102 std::string eventName = JniJavaToStdString(env, jEventName);
100 AdblockPlus::JsValueList args; 103 AdblockPlus::JsValueList args;
101 104
102 if (jJsPtrs) 105 if (jJsPtrs)
103 { 106 {
104 jlong* ptrs = (jlong*)env->GetPrimitiveArrayCritical(jJsPtrs, 0); 107 jlong* ptrs = (jlong*)env->GetPrimitiveArrayCritical(jJsPtrs, 0);
105 108
106 jsize length = env->GetArrayLength(jJsPtrs); 109 jsize length = env->GetArrayLength(jJsPtrs);
107 110
108 for (jsize i = 0; i < length; i++) 111 for (jsize i = 0; i < length; i++)
109 { 112 {
110 args.push_back(JniGetJsValuePtr(ptrs[i])); 113 args.push_back(JniGetJsValue(ptrs[i]));
111 } 114 }
112 115
113 env->ReleasePrimitiveArrayCritical(jJsPtrs, ptrs, JNI_ABORT); 116 env->ReleasePrimitiveArrayCritical(jJsPtrs, ptrs, JNI_ABORT);
114 } 117 }
115 118
116 try 119 try
117 { 120 {
118 engine->TriggerEvent(eventName, args); 121 engine->TriggerEvent(eventName, std::move(args));
119 } 122 }
120 CATCH_AND_THROW(env) 123 CATCH_AND_THROW(env)
121 } 124 }
122 125
123 static void JNICALL JniSetDefaultFileSystem(JNIEnv* env, jclass clazz, jlong ptr , jstring jBasePath) 126 static void JNICALL JniSetDefaultFileSystem(JNIEnv* env, jclass clazz, jlong ptr , jstring jBasePath)
124 { 127 {
125 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 128 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
126 129
127 try 130 try
128 { 131 {
129 AdblockPlus::FileSystemPtr fileSystem(new AdblockPlus::DefaultFileSystem()); 132 AdblockPlus::FileSystemPtr fileSystem(new AdblockPlus::DefaultFileSystem());
130 133
131 std::string basePath = JniJavaToStdString(env, jBasePath); 134 std::string basePath = JniJavaToStdString(env, jBasePath);
132 reinterpret_cast<AdblockPlus::DefaultFileSystem*>(fileSystem.get())->SetBase Path(basePath); 135 reinterpret_cast<AdblockPlus::DefaultFileSystem*>(fileSystem.get())->SetBase Path(basePath);
133 136
134 engine->SetFileSystem(fileSystem); 137 engine->SetFileSystem(fileSystem);
135 } 138 }
136 CATCH_AND_THROW(env) 139 CATCH_AND_THROW(env)
137 } 140 }
138 141
139 static void JNICALL JniSetDefaultWebRequest(JNIEnv* env, jclass clazz, jlong ptr ) 142 static void JNICALL JniSetDefaultWebRequest(JNIEnv* env, jclass clazz, jlong ptr )
140 { 143 {
141 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 144 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
142 145
143 try 146 try
144 { 147 {
145 AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebRequest()); 148 AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebRequest());
146
147 engine->SetWebRequest(webRequest); 149 engine->SetWebRequest(webRequest);
148 } 150 }
149 CATCH_AND_THROW(env) 151 CATCH_AND_THROW(env)
150 } 152 }
151 153
152 static void JNICALL JniSetDefaultLogSystem(JNIEnv* env, jclass clazz, jlong ptr) 154 static void JNICALL JniSetDefaultLogSystem(JNIEnv* env, jclass clazz, jlong ptr)
153 { 155 {
154 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 156 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
155 157
156 try 158 try
(...skipping 30 matching lines...) Expand all
187 } 189 }
188 CATCH_AND_THROW(env) 190 CATCH_AND_THROW(env)
189 } 191 }
190 192
191 static jobject JNICALL JniNewLongValue(JNIEnv* env, jclass clazz, jlong ptr, jlo ng value) 193 static jobject JNICALL JniNewLongValue(JNIEnv* env, jclass clazz, jlong ptr, jlo ng value)
192 { 194 {
193 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 195 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
194 196
195 try 197 try
196 { 198 {
197 AdblockPlus::JsValuePtr jsValue = engine->NewValue(static_cast<int64_t>(valu e)); 199 return NewJniJsValue(env, engine->NewValue(static_cast<int64_t>(value)));
198 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 return NewJniJsValue(env, engine->NewValue(value == JNI_TRUE ? true : false) );
210 return NewJniJsValue(env, jsValue);
211 } 211 }
212 CATCH_THROW_AND_RETURN(env, 0) 212 CATCH_THROW_AND_RETURN(env, 0)
213 } 213 }
214 214
215 static jobject JNICALL JniNewStringValue(JNIEnv* env, jclass clazz, jlong ptr, j string value) 215 static jobject JNICALL JniNewStringValue(JNIEnv* env, jclass clazz, jlong ptr, j string value)
216 { 216 {
217 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 217 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr);
218 218
219 try 219 try
220 { 220 {
221 std::string strValue = JniJavaToStdString(env, value); 221 std::string strValue = JniJavaToStdString(env, value);
222 AdblockPlus::JsValuePtr jsValue = engine->NewValue(strValue); 222 return NewJniJsValue(env, engine->NewValue(strValue));
223 return NewJniJsValue(env, jsValue);
224 } 223 }
225 CATCH_THROW_AND_RETURN(env, 0) 224 CATCH_THROW_AND_RETURN(env, 0)
226 } 225 }
227 226
228 // TODO: List of functions that lack JNI bindings 227 // TODO: List of functions that lack JNI bindings
229 //JsValuePtr NewObject(); 228 //JsValuePtr NewObject();
230 //JsValuePtr NewCallback(v8::InvocationCallback callback); 229 //JsValuePtr NewCallback(v8::InvocationCallback callback);
231 //static JsEnginePtr FromArguments(const v8::Arguments& arguments); 230 //static JsEnginePtr FromArguments(const v8::Arguments& arguments);
232 //JsValueList ConvertArguments(const v8::Arguments& arguments); 231 //JsValueList ConvertArguments(const v8::Arguments& arguments);
233 232
(...skipping 16 matching lines...) Expand all
250 249
251 { (char*)"newValue", (char*)"(JJ)" TYP("JsValue"), (void*)JniNewLongValue }, 250 { (char*)"newValue", (char*)"(JJ)" TYP("JsValue"), (void*)JniNewLongValue },
252 { (char*)"newValue", (char*)"(JZ)" TYP("JsValue"), (void*)JniNewBooleanValue } , 251 { (char*)"newValue", (char*)"(JZ)" TYP("JsValue"), (void*)JniNewBooleanValue } ,
253 { (char*)"newValue", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)Jni NewStringValue } 252 { (char*)"newValue", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)Jni NewStringValue }
254 }; 253 };
255 254
256 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsEngine_r egisterNatives(JNIEnv *env, jclass clazz) 255 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsEngine_r egisterNatives(JNIEnv *env, jclass clazz)
257 { 256 {
258 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); 257 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
259 } 258 }
OLDNEW

Powered by Google App Engine
This is Rietveld