LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of the Adblock Plus, | 2 * This file is part of the Adblock Plus, |
3 * Copyright (C) 2006-2012 Eyeo GmbH | 3 * Copyright (C) 2006-2012 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 <string> | 18 #include <string> |
19 #include <android/log.h> | 19 #include <android/log.h> |
20 #include <jni.h> | 20 #include <jni.h> |
| 21 #include "debug.h" |
21 #include "wrap.h" | 22 #include "wrap.h" |
22 #include "ops.h" | 23 #include "ops.h" |
23 | 24 |
24 const char* scriptDir; | 25 const char* scriptDir; |
25 const char* dataDir; | 26 const char* dataDir; |
26 JNIEnv* jniEnv; | 27 JavaVM* globalJvm; |
27 jobject jniCallback; | 28 jobject jniCallback; |
28 | 29 |
29 extern "C" | 30 extern "C" |
30 { | 31 { |
31 JNIEXPORT jlong JNICALL Java_org_adblockplus_android_JSEngine_nativeInitialize
(JNIEnv *pEnv, jobject, jobject pCallback); | 32 JNIEXPORT jlong JNICALL Java_org_adblockplus_android_JSEngine_nativeInitialize
(JNIEnv *pEnv, jobject, jobject pCallback); |
32 JNIEXPORT void JNICALL Java_org_adblockplus_android_JSEngine_nativeRelease(JNI
Env *pEnv, jobject pObj, jlong pContext); | 33 JNIEXPORT void JNICALL Java_org_adblockplus_android_JSEngine_nativeRelease(JNI
Env *pEnv, jobject pObj, jlong pContext); |
33 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativeExecute(
JNIEnv *pEnv, jobject pObj, jstring pScript, jlong pContext); | 34 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativeExecute(
JNIEnv *pEnv, jobject pObj, jstring pScript, jlong pContext); |
34 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativeGet(JNIE
nv *pEnv, jobject pObj, jstring pKey, jlong pContext); | 35 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativeGet(JNIE
nv *pEnv, jobject pObj, jstring pKey, jlong pContext); |
35 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativePut(JNIE
nv *pEnv, jobject pObj, jstring pKey, jobject pValue, jlong pContext); | 36 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativePut(JNIE
nv *pEnv, jobject pObj, jstring pKey, jobject pValue, jlong pContext); |
36 JNIEXPORT void JNICALL Java_org_adblockplus_android_JSEngine_nativeCallback(JN
IEnv *pEnv, jobject pObj, jlong pCallback, jobjectArray pParams, jlong pContext)
; | 37 JNIEXPORT void JNICALL Java_org_adblockplus_android_JSEngine_nativeCallback(JN
IEnv *pEnv, jobject pObj, jlong pCallback, jobjectArray pParams, jlong pContext)
; |
(...skipping 18 matching lines...) Expand all Loading... |
55 { "fileRemove", fileRemoveImpl }, | 56 { "fileRemove", fileRemoveImpl }, |
56 { "fileRename", fileRenameImpl }, | 57 { "fileRename", fileRenameImpl }, |
57 { "fileRead", fileReadImpl }, | 58 { "fileRead", fileReadImpl }, |
58 { "fileWrite", fileWriteImpl }, | 59 { "fileWrite", fileWriteImpl }, |
59 { "setTimeout", setTimeoutImpl }, | 60 { "setTimeout", setTimeoutImpl }, |
60 { "httpSend", httpSendImpl }, | 61 { "httpSend", httpSendImpl }, |
61 { NULL, NULL }, }; | 62 { NULL, NULL }, }; |
62 | 63 |
63 void reportException(v8::TryCatch* try_catch) | 64 void reportException(v8::TryCatch* try_catch) |
64 { | 65 { |
| 66 D(D_WARN, "reportException()"); |
65 v8::HandleScope handle_scope; | 67 v8::HandleScope handle_scope; |
66 v8::String::Utf8Value exception(try_catch->Exception()); | 68 v8::String::Utf8Value exception(try_catch->Exception()); |
67 v8::Handle < v8::Message > message = try_catch->Message(); | 69 v8::Handle < v8::Message > message = try_catch->Message(); |
68 if (message.IsEmpty()) | 70 if (message.IsEmpty()) |
69 { | 71 { |
70 // Exception context is unknown | 72 // Exception context is unknown |
71 __android_log_print(ANDROID_LOG_ERROR, "JS", "Uncaught exception: %s", *exce
ption); | 73 __android_log_print(ANDROID_LOG_ERROR, "JS", "Uncaught exception: %s", *exce
ption); |
72 } | 74 } |
73 else | 75 else |
74 { | 76 { |
(...skipping 12 matching lines...) Expand all Loading... |
87 fprintf(stderr, " "); | 89 fprintf(stderr, " "); |
88 for (int i = startcol; i < endcol; i++) | 90 for (int i = startcol; i < endcol; i++) |
89 fprintf(stderr, "^"); | 91 fprintf(stderr, "^"); |
90 fprintf(stderr, "\n"); | 92 fprintf(stderr, "\n"); |
91 } | 93 } |
92 } | 94 } |
93 } | 95 } |
94 | 96 |
95 const std::string getString(JNIEnv *pEnv, jstring str) | 97 const std::string getString(JNIEnv *pEnv, jstring str) |
96 { | 98 { |
| 99 D(D_WARN, "getString()"); |
97 jboolean iscopy; | 100 jboolean iscopy; |
98 | 101 |
99 const char *s = pEnv->GetStringUTFChars(str, &iscopy); | 102 const char *s = pEnv->GetStringUTFChars(str, &iscopy); |
100 jsize len = pEnv->GetStringUTFLength(str); | 103 jsize len = pEnv->GetStringUTFLength(str); |
101 | 104 |
102 const std::string value(s, len); | 105 const std::string value(s, len); |
103 | 106 |
104 pEnv->ReleaseStringUTFChars(str, s); | 107 pEnv->ReleaseStringUTFChars(str, s); |
105 | 108 |
106 return value; | 109 return value; |
107 } | 110 } |
108 | 111 |
109 jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) | 112 jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) |
110 { | 113 { |
111 return JNI_VERSION_1_2; | 114 return JNI_VERSION_1_6; |
112 } | 115 } |
113 | 116 |
114 void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) | 117 void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) |
115 { | 118 { |
116 } | 119 } |
117 | 120 |
118 JNIEXPORT jlong JNICALL Java_org_adblockplus_android_JSEngine_nativeInitialize | 121 JNIEXPORT jlong JNICALL Java_org_adblockplus_android_JSEngine_nativeInitialize |
119 (JNIEnv *pEnv, jobject, jobject pCallback) | 122 (JNIEnv *pEnv, jobject, jobject pCallback) |
120 { | 123 { |
121 jniEnv = pEnv; | 124 D(D_WARN, "nativeInitialize()"); |
| 125 int status = pEnv->GetJavaVM(&globalJvm); |
122 jniCallback = pEnv->NewGlobalRef(pCallback); | 126 jniCallback = pEnv->NewGlobalRef(pCallback); |
123 | 127 |
124 v8::HandleScope handle_scope; | 128 v8::HandleScope handle_scope; |
125 | 129 |
126 v8::Handle < v8::ObjectTemplate > system = v8::ObjectTemplate::New(); | 130 v8::Handle < v8::ObjectTemplate > system = v8::ObjectTemplate::New(); |
127 for (int i = 0; methods[i].name; i++) | 131 for (int i = 0; methods[i].name; i++) |
128 system->Set(v8::String::New(methods[i].name), v8::FunctionTemplate::New(meth
ods[i].callback)); | 132 system->Set(v8::String::New(methods[i].name), v8::FunctionTemplate::New(meth
ods[i].callback)); |
129 | 133 |
130 v8::Handle < v8::ObjectTemplate > global = v8::ObjectTemplate::New(); | 134 v8::Handle < v8::ObjectTemplate > global = v8::ObjectTemplate::New(); |
131 global->Set(v8::String::New("Android"), system); | 135 global->Set(v8::String::New("Android"), system); |
132 | 136 |
133 return (jlong) *v8::Persistent<v8::Context>::New(v8::Context::New(NULL, global
)); | 137 return (jlong) *v8::Persistent<v8::Context>::New(v8::Context::New(NULL, global
)); |
134 } | 138 } |
135 | 139 |
136 JNIEXPORT void JNICALL Java_org_adblockplus_android_JSEngine_nativeRelease | 140 JNIEXPORT void JNICALL Java_org_adblockplus_android_JSEngine_nativeRelease |
137 (JNIEnv *pEnv, jobject, jlong pContext) | 141 (JNIEnv *pEnv, jobject, jlong pContext) |
138 { | 142 { |
| 143 D(D_WARN, "nativeRelease()"); |
139 ClearQueue(); | 144 ClearQueue(); |
140 if (pContext) | 145 if (pContext) |
141 { | 146 { |
142 v8::Persistent<v8::Context> context((v8::Context *) pContext); | 147 v8::Persistent<v8::Context> context((v8::Context *) pContext); |
143 context.Dispose(); | 148 context.Dispose(); |
144 } | 149 } |
| 150 pEnv->DeleteGlobalRef(jniCallback); |
145 jniCallback = NULL; | 151 jniCallback = NULL; |
146 jniEnv = NULL; | 152 globalJvm = NULL; |
147 } | 153 } |
148 | 154 |
149 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativeExecute | 155 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativeExecute |
150 (JNIEnv *pEnv, jobject pObj, jstring pScript, jlong pContext) | 156 (JNIEnv *pEnv, jobject pObj, jstring pScript, jlong pContext) |
151 { | 157 { |
| 158 D(D_WARN, "nativeExecute()"); |
152 v8::HandleScope handle_scope; | 159 v8::HandleScope handle_scope; |
153 | 160 |
154 v8::Persistent<v8::Context> context((v8::Context *) pContext); | 161 v8::Persistent<v8::Context> context((v8::Context *) pContext); |
155 v8::Context::Scope context_scope(context); | 162 v8::Context::Scope context_scope(context); |
156 | 163 |
157 const std::string script = getString(pEnv, pScript); | 164 const std::string script = getString(pEnv, pScript); |
158 | 165 |
159 v8::Handle<v8::String> source = v8::String::New(script.c_str(), script.size())
; | 166 v8::Handle<v8::String> source = v8::String::New(script.c_str(), script.size())
; |
160 v8::Handle<v8::Script> compiledScript = v8::Script::Compile(source); | 167 v8::Handle<v8::Script> compiledScript = v8::Script::Compile(source); |
161 { | 168 { |
162 v8::TryCatch try_catch; | 169 v8::TryCatch try_catch; |
163 v8::Handle<v8::Value> result = compiledScript->Run(); | 170 v8::Handle<v8::Value> result = compiledScript->Run(); |
164 if (try_catch.HasCaught()) | 171 if (try_catch.HasCaught()) |
165 { | 172 { |
166 reportException(&try_catch); | 173 reportException(&try_catch); |
167 return NULL; | 174 return NULL; |
168 } | 175 } |
169 else | 176 else |
170 { | 177 { |
171 return wrapJSObject(pEnv, result); | 178 return wrapJSObject(pEnv, result); |
172 } | 179 } |
173 } | 180 } |
174 } | 181 } |
175 | 182 |
176 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativeGet | 183 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativeGet |
177 (JNIEnv *pEnv, jobject pObj, jstring pKey, jlong pContext) | 184 (JNIEnv *pEnv, jobject pObj, jstring pKey, jlong pContext) |
178 { | 185 { |
| 186 D(D_WARN, "nativeGet()"); |
179 v8::HandleScope handle_scope; | 187 v8::HandleScope handle_scope; |
180 | 188 |
181 v8::Persistent<v8::Context> context((v8::Context *) pContext); | 189 v8::Persistent<v8::Context> context((v8::Context *) pContext); |
182 v8::Context::Scope context_scope(context); | 190 v8::Context::Scope context_scope(context); |
183 | 191 |
184 v8::Persistent<v8::Object> obj(v8::Persistent<v8::Object>::New(context->Global
())); | 192 v8::Persistent<v8::Object> obj(v8::Persistent<v8::Object>::New(context->Global
())); |
185 const std::string key = getString(pEnv, pKey); | 193 const std::string key = getString(pEnv, pKey); |
186 | 194 |
187 { | 195 { |
188 v8::TryCatch try_catch; | 196 v8::TryCatch try_catch; |
189 v8::Handle<v8::Value> value = obj->Get(v8::String::New(key.c_str(), key.size
())); | 197 v8::Handle<v8::Value> value = obj->Get(v8::String::New(key.c_str(), key.size
())); |
190 if (try_catch.HasCaught()) | 198 if (try_catch.HasCaught()) |
191 { | 199 { |
192 reportException(&try_catch); | 200 reportException(&try_catch); |
193 return NULL; | 201 return NULL; |
194 } | 202 } |
195 else | 203 else |
196 { | 204 { |
197 return wrapJSObject(pEnv, value); | 205 return wrapJSObject(pEnv, value); |
198 } | 206 } |
199 } | 207 } |
200 } | 208 } |
201 | 209 |
202 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativePut | 210 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativePut |
203 (JNIEnv *pEnv, jobject pObj, jstring pKey, jobject pValue, jlong pContext) | 211 (JNIEnv *pEnv, jobject pObj, jstring pKey, jobject pValue, jlong pContext) |
204 { | 212 { |
| 213 D(D_WARN, "nativePut()"); |
205 v8::HandleScope handle_scope; | 214 v8::HandleScope handle_scope; |
206 | 215 |
207 v8::Persistent<v8::Context> context((v8::Context *) pContext); | 216 v8::Persistent<v8::Context> context((v8::Context *) pContext); |
208 v8::Context::Scope context_scope(context); | 217 v8::Context::Scope context_scope(context); |
209 | 218 |
210 v8::Persistent<v8::Object> obj(v8::Persistent<v8::Object>::New(context->Global
())); | 219 v8::Persistent<v8::Object> obj(v8::Persistent<v8::Object>::New(context->Global
())); |
211 | 220 |
212 const std::string key = getString(pEnv, pKey); | 221 const std::string key = getString(pEnv, pKey); |
213 v8::Handle<v8::String> name = v8::String::New(key.c_str(), key.size()); | 222 v8::Handle<v8::String> name = v8::String::New(key.c_str(), key.size()); |
214 | 223 |
215 // v8::Handle<v8::Value> value = obj->Get(name); | 224 // v8::Handle<v8::Value> value = obj->Get(name); |
216 obj->Set(name, wrapJavaObject(pEnv, pValue)); | 225 obj->Set(name, wrapJavaObject(pEnv, pValue)); |
217 | 226 |
218 return NULL; | 227 return NULL; |
219 // return env.HasCaught() ? NULL : env.Wrap(value); | 228 // return env.HasCaught() ? NULL : env.Wrap(value); |
220 } | 229 } |
221 | 230 |
222 JNIEXPORT void JNICALL Java_org_adblockplus_android_JSEngine_nativeCallback | 231 JNIEXPORT void JNICALL Java_org_adblockplus_android_JSEngine_nativeCallback |
223 (JNIEnv *pEnv, jobject pObj, jlong pCallback, jobjectArray pParams, jlong pCon
text) | 232 (JNIEnv *pEnv, jobject pObj, jlong pCallback, jobjectArray pParams, jlong pCon
text) |
224 { | 233 { |
| 234 D(D_WARN, "nativeCallback()"); |
225 v8::HandleScope handle_scope; | 235 v8::HandleScope handle_scope; |
226 | 236 |
227 v8::Persistent<v8::Context> context((v8::Context *) pContext); | 237 v8::Persistent<v8::Context> context((v8::Context *) pContext); |
228 v8::Context::Scope context_scope(context); | 238 v8::Context::Scope context_scope(context); |
229 | 239 |
230 v8::Persistent<v8::Function> callback((v8::Function *) pCallback); | 240 v8::Persistent<v8::Function> callback((v8::Function *) pCallback); |
231 | 241 |
232 jsize pnum = pEnv->GetArrayLength(pParams); | 242 jsize pnum = pEnv->GetArrayLength(pParams); |
233 v8::Handle<v8::Value> *args = new v8::Handle<v8::Value>[pnum]; | 243 v8::Handle<v8::Value> *args = new v8::Handle<v8::Value>[pnum]; |
234 | 244 |
(...skipping 10 matching lines...) Expand all Loading... |
245 callback.Dispose(); | 255 callback.Dispose(); |
246 delete [] args; | 256 delete [] args; |
247 if (try_catch.HasCaught()) | 257 if (try_catch.HasCaught()) |
248 reportException(&try_catch); | 258 reportException(&try_catch); |
249 } | 259 } |
250 } | 260 } |
251 | 261 |
252 JNIEXPORT jlong JNICALL Java_org_adblockplus_android_JSEngine_nativeRunCallbacks | 262 JNIEXPORT jlong JNICALL Java_org_adblockplus_android_JSEngine_nativeRunCallbacks |
253 (JNIEnv *pEnv, jobject pObj, jlong pContext) | 263 (JNIEnv *pEnv, jobject pObj, jlong pContext) |
254 { | 264 { |
| 265 D(D_WARN, "nativeRunCallbacks()"); |
255 v8::HandleScope handle_scope; | 266 v8::HandleScope handle_scope; |
256 | 267 |
257 v8::Persistent<v8::Context> context((v8::Context *) pContext); | 268 v8::Persistent<v8::Context> context((v8::Context *) pContext); |
258 v8::Context::Scope context_scope(context); | 269 v8::Context::Scope context_scope(context); |
259 | 270 |
260 long r = 0; | 271 long r = 0; |
261 while (r == 0) | 272 while (r == 0) |
262 { | 273 { |
263 v8::TryCatch try_catch; | 274 v8::TryCatch try_catch; |
264 r = RunNextCallback(context); | 275 r = RunNextCallback(context); |
265 if (try_catch.HasCaught()) | 276 if (try_catch.HasCaught()) |
266 reportException(&try_catch); | 277 reportException(&try_catch); |
267 } | 278 } |
268 return (jlong) r; | 279 return (jlong) r; |
269 } | 280 } |
LEFT | RIGHT |