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

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

Issue 29465639: Issue 5309 - Subscriptions update causes ANR (Closed) Base URL: github.com:abby-sergz/libadblockplus-android.git
Patch Set: removed unneeded passing of AdblockEngine instance to wrapper listener@anton Created June 16, 2017, 12:02 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 | « libadblockplus-android/jni/JniJsEngine.h ('k') | libadblockplus-android/jni/Utils.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <AdblockPlus.h> 18 #include <AdblockPlus.h>
19 #include "Utils.h" 19 #include "Utils.h"
20 #include "JniCallbacks.h" 20 #include "JniCallbacks.h"
21 #include "JniJsEngine.h"
21 22
22 static void TransformAppInfo(JNIEnv* env, jobject jAppInfo, AdblockPlus::AppInfo & appInfo) 23 static void TransformAppInfo(JNIEnv* env, jobject jAppInfo, AdblockPlus::AppInfo & appInfo)
23 { 24 {
24 jclass clazz = env->GetObjectClass(jAppInfo); 25 jclass clazz = env->GetObjectClass(jAppInfo);
25 26
26 appInfo.application = JniGetStringField(env, clazz, jAppInfo, "application"); 27 appInfo.application = JniGetStringField(env, clazz, jAppInfo, "application");
27 appInfo.applicationVersion = JniGetStringField(env, clazz, jAppInfo, "applicat ionVersion"); 28 appInfo.applicationVersion = JniGetStringField(env, clazz, jAppInfo, "applicat ionVersion");
28 appInfo.locale = JniGetStringField(env, clazz, jAppInfo, "locale"); 29 appInfo.locale = JniGetStringField(env, clazz, jAppInfo, "locale");
29 appInfo.name = JniGetStringField(env, clazz, jAppInfo, "name"); 30 appInfo.name = JniGetStringField(env, clazz, jAppInfo, "name");
30 appInfo.version = JniGetStringField(env, clazz, jAppInfo, "version"); 31 appInfo.version = JniGetStringField(env, clazz, jAppInfo, "version");
31 32
32 appInfo.developmentBuild = JniGetBooleanField(env, clazz, jAppInfo, "developme ntBuild"); 33 appInfo.developmentBuild = JniGetBooleanField(env, clazz, jAppInfo, "developme ntBuild");
33 } 34 }
34 35
36 static AdblockPlus::JsEnginePtr& GetJsEnginePtrRef(jlong ptr)
37 {
38 return JniLongToTypePtr<JniJsEngine>(ptr)->jsEngine;
39 }
40
35 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jobject jAppInfo) 41 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jobject jAppInfo)
36 { 42 {
37 AdblockPlus::AppInfo appInfo; 43 AdblockPlus::AppInfo appInfo;
38 44
39 TransformAppInfo(env, jAppInfo, appInfo); 45 TransformAppInfo(env, jAppInfo, appInfo);
40 46
41 try 47 try
42 { 48 {
43 return JniPtrToLong(new AdblockPlus::JsEnginePtr(AdblockPlus::JsEngine::New( appInfo))); 49 AdblockPlus::TimerPtr timer = AdblockPlus::CreateDefaultTimer();
50 JniJsEngine* jniJsEngine = new JniJsEngine();
51 jniJsEngine->timer = timer.get();
52 jniJsEngine->jsEngine = AdblockPlus::JsEngine::New(appInfo, std::move(timer) );
53 return JniPtrToLong(jniJsEngine);
44 } 54 }
45 CATCH_THROW_AND_RETURN(env, 0) 55 CATCH_THROW_AND_RETURN(env, 0)
46 } 56 }
47 57
48 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) 58 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
49 { 59 {
50 delete JniLongToTypePtr<AdblockPlus::JsEnginePtr>(ptr); 60 delete JniLongToTypePtr<JniJsEngine>(ptr);
51 } 61 }
52 62
53 static void JNICALL JniSetEventCallback(JNIEnv* env, jclass clazz, jlong ptr, js tring jEventName, jlong jCallbackPtr) 63 static void JNICALL JniSetEventCallback(JNIEnv* env, jclass clazz, jlong ptr, js tring jEventName, jlong jCallbackPtr)
54 { 64 {
55 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 65 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr);
56 66
57 JniEventCallback* callback = JniLongToTypePtr<JniEventCallback>(jCallbackPtr); 67 JniEventCallback* callback = JniLongToTypePtr<JniEventCallback>(jCallbackPtr);
58 std::string eventName = JniJavaToStdString(env, jEventName); 68 std::string eventName = JniJavaToStdString(env, jEventName);
59 69
60 auto eCallback = [callback](AdblockPlus::JsValueList&& params) 70 auto eCallback = [callback](AdblockPlus::JsValueList&& params)
61 { 71 {
62 callback->Callback(std::move(params)); 72 callback->Callback(std::move(params));
63 }; 73 };
64 74
65 try 75 try
66 { 76 {
67 engine->SetEventCallback(eventName, eCallback); 77 engine->SetEventCallback(eventName, eCallback);
68 } 78 }
69 CATCH_AND_THROW(env) 79 CATCH_AND_THROW(env)
70 } 80 }
71 81
72 static void JNICALL JniRemoveEventCallback(JNIEnv* env, jclass clazz, jlong ptr, jstring jEventName) 82 static void JNICALL JniRemoveEventCallback(JNIEnv* env, jclass clazz, jlong ptr, jstring jEventName)
73 { 83 {
74 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 84 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr);
75 85
76 std::string eventName = JniJavaToStdString(env, jEventName); 86 std::string eventName = JniJavaToStdString(env, jEventName);
77 87
78 try 88 try
79 { 89 {
80 engine->RemoveEventCallback(eventName); 90 engine->RemoveEventCallback(eventName);
81 } 91 }
82 CATCH_AND_THROW(env) 92 CATCH_AND_THROW(env)
83 } 93 }
84 94
85 static jobject JNICALL JniEvaluate(JNIEnv* env, jclass clazz, jlong ptr, jstring jSource, jstring jFilename) 95 static jobject JNICALL JniEvaluate(JNIEnv* env, jclass clazz, jlong ptr, jstring jSource, jstring jFilename)
86 { 96 {
87 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 97 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr);
88 98
89 std::string source = JniJavaToStdString(env, jSource); 99 std::string source = JniJavaToStdString(env, jSource);
90 std::string filename = JniJavaToStdString(env, jFilename); 100 std::string filename = JniJavaToStdString(env, jFilename);
91 101
92 try 102 try
93 { 103 {
94 return NewJniJsValue(env, engine->Evaluate(source, filename)); 104 return NewJniJsValue(env, engine->Evaluate(source, filename));
95 } 105 }
96 CATCH_THROW_AND_RETURN(env, 0) 106 CATCH_THROW_AND_RETURN(env, 0)
97 } 107 }
98 108
99 static void JNICALL JniTriggerEvent(JNIEnv* env, jclass clazz, jlong ptr, jstrin g jEventName, jarray jJsPtrs) 109 static void JNICALL JniTriggerEvent(JNIEnv* env, jclass clazz, jlong ptr, jstrin g jEventName, jarray jJsPtrs)
100 { 110 {
101 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 111 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr);
102 std::string eventName = JniJavaToStdString(env, jEventName); 112 std::string eventName = JniJavaToStdString(env, jEventName);
103 AdblockPlus::JsValueList args; 113 AdblockPlus::JsValueList args;
104 114
105 if (jJsPtrs) 115 if (jJsPtrs)
106 { 116 {
107 jlong* ptrs = (jlong*)env->GetPrimitiveArrayCritical(jJsPtrs, 0); 117 jlong* ptrs = (jlong*)env->GetPrimitiveArrayCritical(jJsPtrs, 0);
108 118
109 jsize length = env->GetArrayLength(jJsPtrs); 119 jsize length = env->GetArrayLength(jJsPtrs);
110 120
111 for (jsize i = 0; i < length; i++) 121 for (jsize i = 0; i < length; i++)
112 { 122 {
113 args.push_back(JniGetJsValue(ptrs[i])); 123 args.push_back(JniGetJsValue(ptrs[i]));
114 } 124 }
115 125
116 env->ReleasePrimitiveArrayCritical(jJsPtrs, ptrs, JNI_ABORT); 126 env->ReleasePrimitiveArrayCritical(jJsPtrs, ptrs, JNI_ABORT);
117 } 127 }
118 128
119 try 129 try
120 { 130 {
121 engine->TriggerEvent(eventName, std::move(args)); 131 engine->TriggerEvent(eventName, std::move(args));
122 } 132 }
123 CATCH_AND_THROW(env) 133 CATCH_AND_THROW(env)
124 } 134 }
125 135
126 static void JNICALL JniSetDefaultFileSystem(JNIEnv* env, jclass clazz, jlong ptr , jstring jBasePath) 136 static void JNICALL JniSetDefaultFileSystem(JNIEnv* env, jclass clazz, jlong ptr , jstring jBasePath)
127 { 137 {
128 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 138 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr);
129 139
130 try 140 try
131 { 141 {
132 AdblockPlus::FileSystemPtr fileSystem(new AdblockPlus::DefaultFileSystem()); 142 AdblockPlus::FileSystemPtr fileSystem(new AdblockPlus::DefaultFileSystem());
133 143
134 std::string basePath = JniJavaToStdString(env, jBasePath); 144 std::string basePath = JniJavaToStdString(env, jBasePath);
135 reinterpret_cast<AdblockPlus::DefaultFileSystem*>(fileSystem.get())->SetBase Path(basePath); 145 reinterpret_cast<AdblockPlus::DefaultFileSystem*>(fileSystem.get())->SetBase Path(basePath);
136 146
137 engine->SetFileSystem(fileSystem); 147 engine->SetFileSystem(fileSystem);
138 } 148 }
139 CATCH_AND_THROW(env) 149 CATCH_AND_THROW(env)
140 } 150 }
141 151
142 static void JNICALL JniSetDefaultLogSystem(JNIEnv* env, jclass clazz, jlong ptr) 152 static void JNICALL JniSetDefaultLogSystem(JNIEnv* env, jclass clazz, jlong ptr)
143 { 153 {
144 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 154 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr);
145 155
146 try 156 try
147 { 157 {
148 AdblockPlus::LogSystemPtr logSystem(new AdblockPlus::DefaultLogSystem()); 158 AdblockPlus::LogSystemPtr logSystem(new AdblockPlus::DefaultLogSystem());
149 159
150 engine->SetLogSystem(logSystem); 160 engine->SetLogSystem(logSystem);
151 } 161 }
152 CATCH_AND_THROW(env) 162 CATCH_AND_THROW(env)
153 } 163 }
154 164
155 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)
156 { 166 {
157 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 167 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr);
158 168
159 try 169 try
160 { 170 {
161 AdblockPlus::LogSystemPtr logSystem = *JniLongToTypePtr<AdblockPlus::LogSyst emPtr>(logSystemPtr); 171 AdblockPlus::LogSystemPtr logSystem = *JniLongToTypePtr<AdblockPlus::LogSyst emPtr>(logSystemPtr);
162 172
163 engine->SetLogSystem(logSystem); 173 engine->SetLogSystem(logSystem);
164 } 174 }
165 CATCH_AND_THROW(env) 175 CATCH_AND_THROW(env)
166 } 176 }
167 177
168 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)
169 { 179 {
170 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 180 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr);
171 181
172 try 182 try
173 { 183 {
174 auto& webRequest = *JniLongToTypePtr<AdblockPlus::WebRequestSharedPtr>(webRe questPtr); 184 auto& webRequest = *JniLongToTypePtr<AdblockPlus::WebRequestSharedPtr>(webRe questPtr);
175 185
176 engine->SetWebRequest(webRequest); 186 engine->SetWebRequest(webRequest);
177 } 187 }
178 CATCH_AND_THROW(env) 188 CATCH_AND_THROW(env)
179 } 189 }
180 190
181 static jobject JNICALL JniNewLongValue(JNIEnv* env, jclass clazz, jlong ptr, jlo ng value) 191 static jobject JNICALL JniNewLongValue(JNIEnv* env, jclass clazz, jlong ptr, jlo ng value)
182 { 192 {
183 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 193 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr);
184 194
185 try 195 try
186 { 196 {
187 return NewJniJsValue(env, engine->NewValue(static_cast<int64_t>(value))); 197 return NewJniJsValue(env, engine->NewValue(static_cast<int64_t>(value)));
188 } 198 }
189 CATCH_THROW_AND_RETURN(env, 0) 199 CATCH_THROW_AND_RETURN(env, 0)
190 } 200 }
191 201
192 static jobject JNICALL JniNewBooleanValue(JNIEnv* env, jclass clazz, jlong ptr, jboolean value) 202 static jobject JNICALL JniNewBooleanValue(JNIEnv* env, jclass clazz, jlong ptr, jboolean value)
193 { 203 {
194 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 204 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr);
195 205
196 try 206 try
197 { 207 {
198 return NewJniJsValue(env, engine->NewValue(value == JNI_TRUE ? true : false) ); 208 return NewJniJsValue(env, engine->NewValue(value == JNI_TRUE ? true : false) );
199 } 209 }
200 CATCH_THROW_AND_RETURN(env, 0) 210 CATCH_THROW_AND_RETURN(env, 0)
201 } 211 }
202 212
203 static jobject JNICALL JniNewStringValue(JNIEnv* env, jclass clazz, jlong ptr, j string value) 213 static jobject JNICALL JniNewStringValue(JNIEnv* env, jclass clazz, jlong ptr, j string value)
204 { 214 {
205 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); 215 AdblockPlus::JsEnginePtr& engine = GetJsEnginePtrRef(ptr);
206 216
207 try 217 try
208 { 218 {
209 std::string strValue = JniJavaToStdString(env, value); 219 std::string strValue = JniJavaToStdString(env, value);
210 return NewJniJsValue(env, engine->NewValue(strValue)); 220 return NewJniJsValue(env, engine->NewValue(strValue));
211 } 221 }
212 CATCH_THROW_AND_RETURN(env, 0) 222 CATCH_THROW_AND_RETURN(env, 0)
213 } 223 }
214 224
215 // TODO: List of functions that lack JNI bindings 225 // TODO: List of functions that lack JNI bindings
(...skipping 20 matching lines...) Expand all
236 246
237 { (char*)"newValue", (char*)"(JJ)" TYP("JsValue"), (void*)JniNewLongValue }, 247 { (char*)"newValue", (char*)"(JJ)" TYP("JsValue"), (void*)JniNewLongValue },
238 { (char*)"newValue", (char*)"(JZ)" TYP("JsValue"), (void*)JniNewBooleanValue } , 248 { (char*)"newValue", (char*)"(JZ)" TYP("JsValue"), (void*)JniNewBooleanValue } ,
239 { (char*)"newValue", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)Jni NewStringValue } 249 { (char*)"newValue", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)Jni NewStringValue }
240 }; 250 };
241 251
242 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsEngine_r egisterNatives(JNIEnv *env, jclass clazz) 252 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsEngine_r egisterNatives(JNIEnv *env, jclass clazz)
243 { 253 {
244 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); 254 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
245 } 255 }
OLDNEW
« no previous file with comments | « libadblockplus-android/jni/JniJsEngine.h ('k') | libadblockplus-android/jni/Utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld