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

Side by Side Diff: 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
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:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | 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-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 16 matching lines...) Expand all
27 27
28 appInfo.application = JniGetStringField(env, clazz, jAppInfo, "application"); 28 appInfo.application = JniGetStringField(env, clazz, jAppInfo, "application");
29 appInfo.applicationVersion = JniGetStringField(env, clazz, jAppInfo, "applicat ionVersion"); 29 appInfo.applicationVersion = JniGetStringField(env, clazz, jAppInfo, "applicat ionVersion");
30 appInfo.locale = JniGetStringField(env, clazz, jAppInfo, "locale"); 30 appInfo.locale = JniGetStringField(env, clazz, jAppInfo, "locale");
31 appInfo.name = JniGetStringField(env, clazz, jAppInfo, "name"); 31 appInfo.name = JniGetStringField(env, clazz, jAppInfo, "name");
32 appInfo.version = JniGetStringField(env, clazz, jAppInfo, "version"); 32 appInfo.version = JniGetStringField(env, clazz, jAppInfo, "version");
33 33
34 appInfo.developmentBuild = JniGetBooleanField(env, clazz, jAppInfo, "developme ntBuild"); 34 appInfo.developmentBuild = JniGetBooleanField(env, clazz, jAppInfo, "developme ntBuild");
35 } 35 }
36 36
37 static AdblockPlus::JsEnginePtr& GetJsEnginePtrRef(jlong ptr) 37 static AdblockPlus::JsEngine& GetJsEngineRef(jlong ptr)
38 { 38 {
39 return JniLongToTypePtr<JniJsEngine>(ptr)->jsEngine; 39 return *JniLongToTypePtr<JniJsEngine>(ptr)->jsEngine;
40 } 40 }
41 41
42 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jobject jAppInfo) 42 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jobject jAppInfo)
43 { 43 {
44 AdblockPlus::AppInfo appInfo; 44 AdblockPlus::AppInfo appInfo;
45 45
46 TransformAppInfo(env, jAppInfo, appInfo); 46 TransformAppInfo(env, jAppInfo, appInfo);
47 47
48 try 48 try
49 { 49 {
50 AdblockPlus::TimerPtr timer = AdblockPlus::CreateDefaultTimer(); 50 AdblockPlus::TimerPtr timer = AdblockPlus::CreateDefaultTimer();
51 JniJsEngine* jniJsEngine = new JniJsEngine(); 51 JniJsEngine* jniJsEngine = new JniJsEngine();
52 jniJsEngine->timer = timer.get(); 52 jniJsEngine->timer = timer.get();
53 jniJsEngine->jsEngine = AdblockPlus::JsEngine::New(appInfo, std::move(timer) ); 53 jniJsEngine->jsEngine = AdblockPlus::JsEngine::New(appInfo, std::move(timer) );
54 return JniPtrToLong(jniJsEngine); 54 return JniPtrToLong(jniJsEngine);
55 } 55 }
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::JsEnginePtr& engine = GetJsEnginePtrRef(ptr); 66 AdblockPlus::JsEngine& engine = GetJsEngineRef(ptr);
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 engine->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::JsEnginePtr& engine = GetJsEnginePtrRef(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 engine->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::JsEnginePtr& engine = GetJsEnginePtrRef(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::JsEnginePtr& engine = GetJsEnginePtrRef(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 engine->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::JsEnginePtr& engine = GetJsEnginePtrRef(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 engine->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::JsEnginePtr& engine = GetJsEnginePtrRef(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 engine->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::JsEnginePtr& engine = GetJsEnginePtrRef(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 engine->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::JsEnginePtr& engine = GetJsEnginePtrRef(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 engine->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::JsEnginePtr& engine = GetJsEnginePtrRef(ptr); 194 AdblockPlus::JsEngine& engine = GetJsEngineRef(ptr);
195 195
196 try 196 try
197 { 197 {
198 return NewJniJsValue(env, engine->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::JsEnginePtr& engine = GetJsEnginePtrRef(ptr); 205 AdblockPlus::JsEngine& engine = GetJsEngineRef(ptr);
206 206
207 try 207 try
208 { 208 {
209 return NewJniJsValue(env, engine->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::JsEnginePtr& engine = GetJsEnginePtrRef(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, engine->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 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld