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

Delta Between Two Patch Sets: libadblockplus-android/jni/JniJsEngine.cpp

Issue 29523679: Issue 5556 - use JsEngine& instead of JsEnginePtr because JsEnginePtr is not available anymore (Closed) Base URL: github.com:abby-sergz/libadblockplus-android.git
Left Patch Set: Created Aug. 22, 2017, 2:37 p.m.
Right Patch Set: revert jsEngine<>engine Created Aug. 23, 2017, 8:23 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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-present eyeo GmbH 3 * Copyright (C) 2006-present 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 CATCH_THROW_AND_RETURN(env, 0) 56 CATCH_THROW_AND_RETURN(env, 0)
57 } 57 }
58 58
59 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) 59 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
60 { 60 {
61 delete JniLongToTypePtr<JniJsEngine>(ptr); 61 delete JniLongToTypePtr<JniJsEngine>(ptr);
62 } 62 }
63 63
64 static void JNICALL JniSetEventCallback(JNIEnv* env, jclass clazz, jlong ptr, js tring jEventName, jlong jCallbackPtr) 64 static void JNICALL JniSetEventCallback(JNIEnv* env, jclass clazz, jlong ptr, js tring jEventName, jlong jCallbackPtr)
65 { 65 {
66 AdblockPlus::JsEngine& jsEngine = GetJsEngineRef(ptr); 66 AdblockPlus::JsEngine& engine = GetJsEngineRef(ptr);
anton 2017/08/23 05:46:47 in code review https://codereview.adblockplus.org/
sergei 2017/08/23 08:24:18 Done.
67 67
68 JniEventCallback* callback = JniLongToTypePtr<JniEventCallback>(jCallbackPtr); 68 JniEventCallback* callback = JniLongToTypePtr<JniEventCallback>(jCallbackPtr);
69 std::string eventName = JniJavaToStdString(env, jEventName); 69 std::string eventName = JniJavaToStdString(env, jEventName);
70 70
71 auto eCallback = [callback](AdblockPlus::JsValueList&& params) 71 auto eCallback = [callback](AdblockPlus::JsValueList&& params)
72 { 72 {
73 callback->Callback(std::move(params)); 73 callback->Callback(std::move(params));
74 }; 74 };
75 75
76 try 76 try
77 { 77 {
78 jsEngine.SetEventCallback(eventName, eCallback); 78 engine.SetEventCallback(eventName, eCallback);
79 } 79 }
80 CATCH_AND_THROW(env) 80 CATCH_AND_THROW(env)
81 } 81 }
82 82
83 static void JNICALL JniRemoveEventCallback(JNIEnv* env, jclass clazz, jlong ptr, jstring jEventName) 83 static void JNICALL JniRemoveEventCallback(JNIEnv* env, jclass clazz, jlong ptr, jstring jEventName)
84 { 84 {
85 AdblockPlus::JsEngine& jsEngine = GetJsEngineRef(ptr); 85 AdblockPlus::JsEngine& engine = GetJsEngineRef(ptr);
86 86
87 std::string eventName = JniJavaToStdString(env, jEventName); 87 std::string eventName = JniJavaToStdString(env, jEventName);
88 88
89 try 89 try
90 { 90 {
91 jsEngine.RemoveEventCallback(eventName); 91 engine.RemoveEventCallback(eventName);
92 } 92 }
93 CATCH_AND_THROW(env) 93 CATCH_AND_THROW(env)
94 } 94 }
95 95
96 static jobject JNICALL JniEvaluate(JNIEnv* env, jclass clazz, jlong ptr, jstring jSource, jstring jFilename) 96 static jobject JNICALL JniEvaluate(JNIEnv* env, jclass clazz, jlong ptr, jstring jSource, jstring jFilename)
97 { 97 {
98 AdblockPlus::JsEngine& engine = GetJsEngineRef(ptr); 98 AdblockPlus::JsEngine& engine = GetJsEngineRef(ptr);
99 99
100 std::string source = JniJavaToStdString(env, jSource); 100 std::string source = JniJavaToStdString(env, jSource);
101 std::string filename = JniJavaToStdString(env, jFilename); 101 std::string filename = JniJavaToStdString(env, jFilename);
102 102
103 try 103 try
104 { 104 {
105 return NewJniJsValue(env, engine.Evaluate(source, filename)); 105 return NewJniJsValue(env, engine.Evaluate(source, filename));
106 } 106 }
107 CATCH_THROW_AND_RETURN(env, 0) 107 CATCH_THROW_AND_RETURN(env, 0)
108 } 108 }
109 109
110 static void JNICALL JniTriggerEvent(JNIEnv* env, jclass clazz, jlong ptr, jstrin g jEventName, jarray jJsPtrs) 110 static void JNICALL JniTriggerEvent(JNIEnv* env, jclass clazz, jlong ptr, jstrin g jEventName, jarray jJsPtrs)
111 { 111 {
112 AdblockPlus::JsEngine& jsEngine = GetJsEngineRef(ptr); 112 AdblockPlus::JsEngine& engine = GetJsEngineRef(ptr);
113 std::string eventName = JniJavaToStdString(env, jEventName); 113 std::string eventName = JniJavaToStdString(env, jEventName);
114 AdblockPlus::JsValueList args; 114 AdblockPlus::JsValueList args;
115 115
116 if (jJsPtrs) 116 if (jJsPtrs)
117 { 117 {
118 jlong* ptrs = (jlong*)env->GetPrimitiveArrayCritical(jJsPtrs, 0); 118 jlong* ptrs = (jlong*)env->GetPrimitiveArrayCritical(jJsPtrs, 0);
119 119
120 jsize length = env->GetArrayLength(jJsPtrs); 120 jsize length = env->GetArrayLength(jJsPtrs);
121 121
122 for (jsize i = 0; i < length; i++) 122 for (jsize i = 0; i < length; i++)
123 { 123 {
124 args.push_back(JniGetJsValue(ptrs[i])); 124 args.push_back(JniGetJsValue(ptrs[i]));
125 } 125 }
126 126
127 env->ReleasePrimitiveArrayCritical(jJsPtrs, ptrs, JNI_ABORT); 127 env->ReleasePrimitiveArrayCritical(jJsPtrs, ptrs, JNI_ABORT);
128 } 128 }
129 129
130 try 130 try
131 { 131 {
132 jsEngine.TriggerEvent(eventName, std::move(args)); 132 engine.TriggerEvent(eventName, std::move(args));
133 } 133 }
134 CATCH_AND_THROW(env) 134 CATCH_AND_THROW(env)
135 } 135 }
136 136
137 static void JNICALL JniSetDefaultFileSystem(JNIEnv* env, jclass clazz, jlong ptr , jstring jBasePath) 137 static void JNICALL JniSetDefaultFileSystem(JNIEnv* env, jclass clazz, jlong ptr , jstring jBasePath)
138 { 138 {
139 AdblockPlus::JsEngine& jsEngine = GetJsEngineRef(ptr); 139 AdblockPlus::JsEngine& engine = GetJsEngineRef(ptr);
140 140
141 try 141 try
142 { 142 {
143 auto fileSystem = std::make_shared<AdblockPlus::DefaultFileSystemSync>(); 143 auto fileSystem = std::make_shared<AdblockPlus::DefaultFileSystemSync>();
144 144
145 std::string basePath = JniJavaToStdString(env, jBasePath); 145 std::string basePath = JniJavaToStdString(env, jBasePath);
146 fileSystem->SetBasePath(basePath); 146 fileSystem->SetBasePath(basePath);
147 147
148 jsEngine.SetFileSystem(fileSystem); 148 engine.SetFileSystem(fileSystem);
149 } 149 }
150 CATCH_AND_THROW(env) 150 CATCH_AND_THROW(env)
151 } 151 }
152 152
153 static void JNICALL JniSetDefaultLogSystem(JNIEnv* env, jclass clazz, jlong ptr) 153 static void JNICALL JniSetDefaultLogSystem(JNIEnv* env, jclass clazz, jlong ptr)
154 { 154 {
155 AdblockPlus::JsEngine& jsEngine = GetJsEngineRef(ptr); 155 AdblockPlus::JsEngine& engine = GetJsEngineRef(ptr);
156 156
157 try 157 try
158 { 158 {
159 AdblockPlus::LogSystemPtr logSystem(new AdblockPlus::DefaultLogSystem()); 159 AdblockPlus::LogSystemPtr logSystem(new AdblockPlus::DefaultLogSystem());
160 160
161 jsEngine.SetLogSystem(logSystem); 161 engine.SetLogSystem(logSystem);
162 } 162 }
163 CATCH_AND_THROW(env) 163 CATCH_AND_THROW(env)
164 } 164 }
165 165
166 static void JNICALL JniSetLogSystem(JNIEnv* env, jclass clazz, jlong ptr, jlong logSystemPtr) 166 static void JNICALL JniSetLogSystem(JNIEnv* env, jclass clazz, jlong ptr, jlong logSystemPtr)
167 { 167 {
168 AdblockPlus::JsEngine& jsEngine = GetJsEngineRef(ptr); 168 AdblockPlus::JsEngine& engine = GetJsEngineRef(ptr);
169 169
170 try 170 try
171 { 171 {
172 AdblockPlus::LogSystemPtr logSystem = *JniLongToTypePtr<AdblockPlus::LogSyst emPtr>(logSystemPtr); 172 AdblockPlus::LogSystemPtr logSystem = *JniLongToTypePtr<AdblockPlus::LogSyst emPtr>(logSystemPtr);
173 173
174 jsEngine.SetLogSystem(logSystem); 174 engine.SetLogSystem(logSystem);
175 } 175 }
176 CATCH_AND_THROW(env) 176 CATCH_AND_THROW(env)
177 } 177 }
178 178
179 static void JNICALL JniSetWebRequest(JNIEnv* env, jclass clazz, jlong ptr, jlong webRequestPtr) 179 static void JNICALL JniSetWebRequest(JNIEnv* env, jclass clazz, jlong ptr, jlong webRequestPtr)
180 { 180 {
181 AdblockPlus::JsEngine& jsEngine = GetJsEngineRef(ptr); 181 AdblockPlus::JsEngine& engine = GetJsEngineRef(ptr);
182 182
183 try 183 try
184 { 184 {
185 auto& webRequest = *JniLongToTypePtr<AdblockPlus::WebRequestSharedPtr>(webRe questPtr); 185 auto& webRequest = *JniLongToTypePtr<AdblockPlus::WebRequestSharedPtr>(webRe questPtr);
186 186
187 jsEngine.SetWebRequest(webRequest); 187 engine.SetWebRequest(webRequest);
188 } 188 }
189 CATCH_AND_THROW(env) 189 CATCH_AND_THROW(env)
190 } 190 }
191 191
192 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)
193 { 193 {
194 AdblockPlus::JsEngine& jsEngine = GetJsEngineRef(ptr); 194 AdblockPlus::JsEngine& engine = GetJsEngineRef(ptr);
195 195
196 try 196 try
197 { 197 {
198 return NewJniJsValue(env, jsEngine.NewValue(static_cast<int64_t>(value))); 198 return NewJniJsValue(env, engine.NewValue(static_cast<int64_t>(value)));
199 } 199 }
200 CATCH_THROW_AND_RETURN(env, 0) 200 CATCH_THROW_AND_RETURN(env, 0)
201 } 201 }
202 202
203 static jobject JNICALL JniNewBooleanValue(JNIEnv* env, jclass clazz, jlong ptr, jboolean value) 203 static jobject JNICALL JniNewBooleanValue(JNIEnv* env, jclass clazz, jlong ptr, jboolean value)
204 { 204 {
205 AdblockPlus::JsEngine& jsEngine = GetJsEngineRef(ptr); 205 AdblockPlus::JsEngine& engine = GetJsEngineRef(ptr);
206 206
207 try 207 try
208 { 208 {
209 return NewJniJsValue(env, jsEngine.NewValue(value == JNI_TRUE ? true : false )); 209 return NewJniJsValue(env, engine.NewValue(value == JNI_TRUE ? true : false)) ;
210 } 210 }
211 CATCH_THROW_AND_RETURN(env, 0) 211 CATCH_THROW_AND_RETURN(env, 0)
212 } 212 }
213 213
214 static jobject JNICALL JniNewStringValue(JNIEnv* env, jclass clazz, jlong ptr, j string value) 214 static jobject JNICALL JniNewStringValue(JNIEnv* env, jclass clazz, jlong ptr, j string value)
215 { 215 {
216 AdblockPlus::JsEngine& jsEngine = GetJsEngineRef(ptr); 216 AdblockPlus::JsEngine& engine = GetJsEngineRef(ptr);
217 217
218 try 218 try
219 { 219 {
220 std::string strValue = JniJavaToStdString(env, value); 220 std::string strValue = JniJavaToStdString(env, value);
221 return NewJniJsValue(env, jsEngine.NewValue(strValue)); 221 return NewJniJsValue(env, engine.NewValue(strValue));
222 } 222 }
223 CATCH_THROW_AND_RETURN(env, 0) 223 CATCH_THROW_AND_RETURN(env, 0)
224 } 224 }
225 225
226 // TODO: List of functions that lack JNI bindings 226 // TODO: List of functions that lack JNI bindings
227 //JsValuePtr NewObject(); 227 //JsValuePtr NewObject();
228 //JsValuePtr NewCallback(v8::InvocationCallback callback); 228 //JsValuePtr NewCallback(v8::InvocationCallback callback);
229 //static JsEnginePtr FromArguments(const v8::Arguments& arguments); 229 //static JsEnginePtr FromArguments(const v8::Arguments& arguments);
230 //JsValueList ConvertArguments(const v8::Arguments& arguments); 230 //JsValueList ConvertArguments(const v8::Arguments& arguments);
231 231
(...skipping 15 matching lines...) Expand all
247 247
248 { (char*)"newValue", (char*)"(JJ)" TYP("JsValue"), (void*)JniNewLongValue }, 248 { (char*)"newValue", (char*)"(JJ)" TYP("JsValue"), (void*)JniNewLongValue },
249 { (char*)"newValue", (char*)"(JZ)" TYP("JsValue"), (void*)JniNewBooleanValue } , 249 { (char*)"newValue", (char*)"(JZ)" TYP("JsValue"), (void*)JniNewBooleanValue } ,
250 { (char*)"newValue", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)Jni NewStringValue } 250 { (char*)"newValue", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)Jni NewStringValue }
251 }; 251 };
252 252
253 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsEngine_r egisterNatives(JNIEnv *env, jclass clazz) 253 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsEngine_r egisterNatives(JNIEnv *env, jclass clazz)
254 { 254 {
255 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); 255 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
256 } 256 }
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld