| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-2013 Eyeo GmbH | |
| 4 * | |
| 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 | |
| 7 * published by the Free Software Foundation. | |
| 8 * | |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 * GNU General Public License for more details. | |
| 13 * | |
| 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/>. | |
| 16 */ | |
| 17 | |
| 18 #include <string> | |
| 19 #include <android/log.h> | |
| 20 #include <jni.h> | |
| 21 #include "debug.h" | |
| 22 #include "wrap.h" | |
| 23 #include "ops.h" | |
| 24 | |
| 25 const char* scriptDir; | |
| 26 const char* dataDir; | |
| 27 JavaVM* globalJvm; | |
| 28 jobject jniCallback; | |
| 29 | |
| 30 extern "C" | |
| 31 { | |
| 32 JNIEXPORT jlong JNICALL Java_org_adblockplus_android_JSEngine_nativeInitialize
(JNIEnv *pEnv, jobject, jobject pCallback); | |
| 33 JNIEXPORT void JNICALL Java_org_adblockplus_android_JSEngine_nativeRelease(JNI
Env *pEnv, jobject pObj, jlong pContext); | |
| 34 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativeExecute(
JNIEnv *pEnv, jobject pObj, jstring pScript, jlong pContext); | |
| 35 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativeGet(JNIE
nv *pEnv, jobject pObj, jstring pKey, jlong pContext); | |
| 36 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativePut(JNIE
nv *pEnv, jobject pObj, jstring pKey, jobject pValue, jlong pContext); | |
| 37 JNIEXPORT void JNICALL Java_org_adblockplus_android_JSEngine_nativeCallback(JN
IEnv *pEnv, jobject pObj, jlong pCallback, jobjectArray pParams, jlong pContext)
; | |
| 38 JNIEXPORT jlong JNICALL Java_org_adblockplus_android_JSEngine_nativeRunCallbac
ks(JNIEnv *pEnv, jobject pObj, jlong pContext); | |
| 39 }; | |
| 40 | |
| 41 typedef struct __ObjMethod | |
| 42 { | |
| 43 const char* name; | |
| 44 v8::InvocationCallback callback; | |
| 45 } ObjMethod; | |
| 46 | |
| 47 static ObjMethod methods[] = | |
| 48 { | |
| 49 { "load", loadImpl }, | |
| 50 { "print", printImpl }, | |
| 51 { "showToast", showToastImpl }, | |
| 52 { "canAutoupdate", canAutoupdateImpl }, | |
| 53 { "setStatus", setStatusImpl }, | |
| 54 { "fileExists", fileExistsImpl }, | |
| 55 { "fileLastModified", fileLastModifiedImpl }, | |
| 56 { "fileRemove", fileRemoveImpl }, | |
| 57 { "fileRename", fileRenameImpl }, | |
| 58 { "fileRead", fileReadImpl }, | |
| 59 { "fileWrite", fileWriteImpl }, | |
| 60 { "setTimeout", setTimeoutImpl }, | |
| 61 { "httpSend", httpSendImpl }, | |
| 62 { NULL, NULL }, }; | |
| 63 | |
| 64 void reportException(v8::TryCatch* try_catch) | |
| 65 { | |
| 66 D(D_WARN, "reportException()"); | |
| 67 v8::HandleScope handle_scope; | |
| 68 v8::String::Utf8Value exception(try_catch->Exception()); | |
| 69 v8::Handle < v8::Message > message = try_catch->Message(); | |
| 70 if (message.IsEmpty()) | |
| 71 { | |
| 72 // Exception context is unknown | |
| 73 __android_log_print(ANDROID_LOG_ERROR, "JS", "Uncaught exception: %s", *exce
ption); | |
| 74 } | |
| 75 else | |
| 76 { | |
| 77 v8::String::Utf8Value filename(message->GetScriptResourceName()); | |
| 78 int linenum = message->GetLineNumber(); | |
| 79 __android_log_print(ANDROID_LOG_ERROR, "JS", "Uncaught exception in %s:%i: %
s\n", *filename, linenum, *exception); | |
| 80 | |
| 81 v8::String::Utf8Value sourceLine(message->GetSourceLine()); | |
| 82 if (*sourceLine) | |
| 83 { | |
| 84 fprintf(stderr, "\n%s\n", *sourceLine); | |
| 85 | |
| 86 int startcol = message->GetStartColumn(); | |
| 87 int endcol = message->GetEndColumn(); | |
| 88 for (int i = 0; i < startcol; i++) | |
| 89 fprintf(stderr, " "); | |
| 90 for (int i = startcol; i < endcol; i++) | |
| 91 fprintf(stderr, "^"); | |
| 92 fprintf(stderr, "\n"); | |
| 93 } | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 const std::string getString(JNIEnv *pEnv, jstring str) | |
| 98 { | |
| 99 D(D_WARN, "getString()"); | |
| 100 jboolean iscopy; | |
| 101 | |
| 102 const char *s = pEnv->GetStringUTFChars(str, &iscopy); | |
| 103 jsize len = pEnv->GetStringUTFLength(str); | |
| 104 | |
| 105 const std::string value(s, len); | |
| 106 | |
| 107 pEnv->ReleaseStringUTFChars(str, s); | |
| 108 | |
| 109 return value; | |
| 110 } | |
| 111 | |
| 112 jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) | |
| 113 { | |
| 114 return JNI_VERSION_1_6; | |
| 115 } | |
| 116 | |
| 117 void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) | |
| 118 { | |
| 119 } | |
| 120 | |
| 121 JNIEXPORT jlong JNICALL Java_org_adblockplus_android_JSEngine_nativeInitialize | |
| 122 (JNIEnv *pEnv, jobject, jobject pCallback) | |
| 123 { | |
| 124 D(D_WARN, "nativeInitialize()"); | |
| 125 int status = pEnv->GetJavaVM(&globalJvm); | |
| 126 jniCallback = pEnv->NewGlobalRef(pCallback); | |
| 127 | |
| 128 v8::HandleScope handle_scope; | |
| 129 | |
| 130 v8::Handle < v8::ObjectTemplate > system = v8::ObjectTemplate::New(); | |
| 131 for (int i = 0; methods[i].name; i++) | |
| 132 system->Set(v8::String::New(methods[i].name), v8::FunctionTemplate::New(meth
ods[i].callback)); | |
| 133 | |
| 134 v8::Handle < v8::ObjectTemplate > global = v8::ObjectTemplate::New(); | |
| 135 global->Set(v8::String::New("Android"), system); | |
| 136 | |
| 137 return (jlong) *v8::Persistent<v8::Context>::New(v8::Context::New(NULL, global
)); | |
| 138 } | |
| 139 | |
| 140 JNIEXPORT void JNICALL Java_org_adblockplus_android_JSEngine_nativeRelease | |
| 141 (JNIEnv *pEnv, jobject, jlong pContext) | |
| 142 { | |
| 143 D(D_WARN, "nativeRelease()"); | |
| 144 ClearQueue(); | |
| 145 if (pContext) | |
| 146 { | |
| 147 v8::Persistent<v8::Context> context((v8::Context *) pContext); | |
| 148 context.Dispose(); | |
| 149 } | |
| 150 pEnv->DeleteGlobalRef(jniCallback); | |
| 151 jniCallback = NULL; | |
| 152 globalJvm = NULL; | |
| 153 } | |
| 154 | |
| 155 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativeExecute | |
| 156 (JNIEnv *pEnv, jobject pObj, jstring pScript, jlong pContext) | |
| 157 { | |
| 158 D(D_WARN, "nativeExecute()"); | |
| 159 v8::HandleScope handle_scope; | |
| 160 | |
| 161 v8::Persistent<v8::Context> context((v8::Context *) pContext); | |
| 162 v8::Context::Scope context_scope(context); | |
| 163 | |
| 164 const std::string script = getString(pEnv, pScript); | |
| 165 | |
| 166 v8::Handle<v8::String> source = v8::String::New(script.c_str(), script.size())
; | |
| 167 { | |
| 168 v8::TryCatch try_catch; | |
| 169 v8::Handle<v8::Script> compiledScript = v8::Script::Compile(source); | |
| 170 v8::Handle<v8::Value> result = compiledScript->Run(); | |
| 171 if (try_catch.HasCaught()) | |
| 172 { | |
| 173 reportException(&try_catch); | |
| 174 return NULL; | |
| 175 } | |
| 176 else | |
| 177 { | |
| 178 return wrapJSObject(pEnv, result); | |
| 179 } | |
| 180 } | |
| 181 } | |
| 182 | |
| 183 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativeGet | |
| 184 (JNIEnv *pEnv, jobject pObj, jstring pKey, jlong pContext) | |
| 185 { | |
| 186 D(D_WARN, "nativeGet()"); | |
| 187 v8::HandleScope handle_scope; | |
| 188 | |
| 189 v8::Persistent<v8::Context> context((v8::Context *) pContext); | |
| 190 v8::Context::Scope context_scope(context); | |
| 191 | |
| 192 v8::Persistent<v8::Object> obj(v8::Persistent<v8::Object>::New(context->Global
())); | |
| 193 const std::string key = getString(pEnv, pKey); | |
| 194 | |
| 195 { | |
| 196 v8::TryCatch try_catch; | |
| 197 v8::Handle<v8::Value> value = obj->Get(v8::String::New(key.c_str(), key.size
())); | |
| 198 if (try_catch.HasCaught()) | |
| 199 { | |
| 200 reportException(&try_catch); | |
| 201 return NULL; | |
| 202 } | |
| 203 else | |
| 204 { | |
| 205 return wrapJSObject(pEnv, value); | |
| 206 } | |
| 207 } | |
| 208 } | |
| 209 | |
| 210 JNIEXPORT jobject JNICALL Java_org_adblockplus_android_JSEngine_nativePut | |
| 211 (JNIEnv *pEnv, jobject pObj, jstring pKey, jobject pValue, jlong pContext) | |
| 212 { | |
| 213 D(D_WARN, "nativePut()"); | |
| 214 v8::HandleScope handle_scope; | |
| 215 | |
| 216 v8::Persistent<v8::Context> context((v8::Context *) pContext); | |
| 217 v8::Context::Scope context_scope(context); | |
| 218 | |
| 219 v8::Persistent<v8::Object> obj(v8::Persistent<v8::Object>::New(context->Global
())); | |
| 220 | |
| 221 const std::string key = getString(pEnv, pKey); | |
| 222 v8::Handle<v8::String> name = v8::String::New(key.c_str(), key.size()); | |
| 223 | |
| 224 // v8::Handle<v8::Value> value = obj->Get(name); | |
| 225 obj->Set(name, wrapJavaObject(pEnv, pValue)); | |
| 226 | |
| 227 return NULL; | |
| 228 // return env.HasCaught() ? NULL : env.Wrap(value); | |
| 229 } | |
| 230 | |
| 231 JNIEXPORT void JNICALL Java_org_adblockplus_android_JSEngine_nativeCallback | |
| 232 (JNIEnv *pEnv, jobject pObj, jlong pCallback, jobjectArray pParams, jlong pCon
text) | |
| 233 { | |
| 234 D(D_WARN, "nativeCallback()"); | |
| 235 v8::HandleScope handle_scope; | |
| 236 | |
| 237 v8::Persistent<v8::Context> context((v8::Context *) pContext); | |
| 238 v8::Context::Scope context_scope(context); | |
| 239 | |
| 240 v8::Persistent<v8::Function> callback((v8::Function *) pCallback); | |
| 241 | |
| 242 jsize pnum = pEnv->GetArrayLength(pParams); | |
| 243 v8::Handle<v8::Value> *args = new v8::Handle<v8::Value>[pnum]; | |
| 244 | |
| 245 for (int i = 0; i < pnum; i++) | |
| 246 { | |
| 247 jobject param = pEnv->GetObjectArrayElement(pParams, i); | |
| 248 args[i] = wrapJavaObject(pEnv, param); | |
| 249 pEnv->DeleteLocalRef(param); | |
| 250 } | |
| 251 | |
| 252 { | |
| 253 v8::TryCatch try_catch; | |
| 254 callback->Call(context->Global(), pnum, args); | |
| 255 callback.Dispose(); | |
| 256 delete [] args; | |
| 257 if (try_catch.HasCaught()) | |
| 258 reportException(&try_catch); | |
| 259 } | |
| 260 } | |
| 261 | |
| 262 JNIEXPORT jlong JNICALL Java_org_adblockplus_android_JSEngine_nativeRunCallbacks | |
| 263 (JNIEnv *pEnv, jobject pObj, jlong pContext) | |
| 264 { | |
| 265 D(D_WARN, "nativeRunCallbacks()"); | |
| 266 v8::HandleScope handle_scope; | |
| 267 | |
| 268 v8::Persistent<v8::Context> context((v8::Context *) pContext); | |
| 269 v8::Context::Scope context_scope(context); | |
| 270 | |
| 271 long r = 0; | |
| 272 while (r == 0) | |
| 273 { | |
| 274 v8::TryCatch try_catch; | |
| 275 r = RunNextCallback(context); | |
| 276 if (try_catch.HasCaught()) | |
| 277 reportException(&try_catch); | |
| 278 } | |
| 279 return (jlong) r; | |
| 280 } | |
| OLD | NEW |