OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of the Adblock Plus, |
| 3 * Copyright (C) 2006-2012 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 "debug.h" |
| 20 #include "ops.h" |
| 21 #include "wrap.h" |
| 22 |
| 23 v8::Handle<v8::Value> loadImpl(const v8::Arguments& args) |
| 24 { |
| 25 D(D_WARN, "load()"); |
| 26 v8::HandleScope handle_scope; |
| 27 |
| 28 JNIEnv* jniEnv = NULL; |
| 29 if (globalJvm->AttachCurrentThread(&jniEnv, NULL) != 0) |
| 30 return v8::ThrowException(v8::String::New("Failed to get JNI environment")); |
| 31 |
| 32 if (args.Length() < 1) |
| 33 return v8::ThrowException(v8::String::New("File name expected")); |
| 34 |
| 35 v8::String::Utf8Value str(args[0]); |
| 36 if (!*str) |
| 37 return v8::ThrowException(v8::String::New("File name isn't a string")); |
| 38 |
| 39 jstring jstr = jniEnv->NewStringUTF(*str); |
| 40 |
| 41 jstring result; |
| 42 jclass cls = jniEnv->GetObjectClass(jniCallback); |
| 43 jmethodID mid = jniEnv->GetMethodID(cls, "readJSFile", "(Ljava/lang/String;)Lj
ava/lang/String;"); |
| 44 if (mid) |
| 45 result = (jstring) jniEnv->CallObjectMethod(jniCallback, mid, jstr); |
| 46 jniEnv->DeleteLocalRef(jstr); |
| 47 |
| 48 /* |
| 49 const char* src = jniEnv->GetStringUTFChars(result, 0); |
| 50 v8::Handle<v8::String> source = v8::String::New(src, jniEnv->GetStringLength(r
esult)); |
| 51 jniEnv->ReleaseStringUTFChars(result, src); |
| 52 */ |
| 53 v8::Handle<v8::String> source = v8::Handle<v8::String>::Cast(wrapJavaObject(jn
iEnv, result)); |
| 54 v8::Handle<v8::Script> script = v8::Script::Compile(source, args[0]); |
| 55 if (!script.IsEmpty()) |
| 56 script->Run(); |
| 57 |
| 58 return v8::Undefined(); |
| 59 } |
| 60 |
| 61 v8::Handle<v8::Value> setStatusImpl(const v8::Arguments& args) |
| 62 { |
| 63 D(D_WARN, "setStatus()"); |
| 64 v8::HandleScope handle_scope; |
| 65 |
| 66 JNIEnv* jniEnv = NULL; |
| 67 if (globalJvm->AttachCurrentThread(&jniEnv, NULL) != 0) |
| 68 return v8::ThrowException(v8::String::New("Failed to get JNI environment")); |
| 69 |
| 70 if (args.Length() < 2) |
| 71 return v8::ThrowException(v8::String::New("Not enough parameters")); |
| 72 |
| 73 if (!args[0]->IsString()) |
| 74 return v8::ThrowException(v8::String::New("Parameter 0 must be a string")); |
| 75 if (!args[1]->IsNumber()) |
| 76 return v8::ThrowException(v8::String::New("Parameter 1 must be a number")); |
| 77 |
| 78 v8::String::Utf8Value str(args[0]); |
| 79 if (!*str) |
| 80 return v8::ThrowException(v8::String::New("Parameter cannot be converted to
string")); |
| 81 jstring jstr = jniEnv->NewStringUTF(*str); |
| 82 |
| 83 jlong jnum = (v8::Handle<v8::Integer>::Cast(args[1]))->Value(); |
| 84 |
| 85 static jclass cls = jniEnv->GetObjectClass(jniCallback); |
| 86 static jmethodID mid = jniEnv->GetMethodID(cls, "setStatus", "(Ljava/lang/Stri
ng;J)V"); |
| 87 if (mid) |
| 88 jniEnv->CallVoidMethod(jniCallback, mid, jstr, jnum); |
| 89 jniEnv->DeleteLocalRef(jstr); |
| 90 |
| 91 return v8::Undefined(); |
| 92 } |
| 93 |
| 94 v8::Handle<v8::Value> canAutoupdateImpl(const v8::Arguments& args) |
| 95 { |
| 96 D(D_WARN, "canAutoupdate()"); |
| 97 v8::HandleScope handle_scope; |
| 98 |
| 99 JNIEnv* jniEnv = NULL; |
| 100 if (globalJvm->AttachCurrentThread(&jniEnv, NULL) != 0) |
| 101 return v8::ThrowException(v8::String::New("Failed to get JNI environment")); |
| 102 |
| 103 jboolean result; |
| 104 jclass cls = jniEnv->GetObjectClass(jniCallback); |
| 105 jmethodID mid = jniEnv->GetMethodID(cls, "canAutoupdate", "()Z"); |
| 106 if (mid) |
| 107 result = jniEnv->CallBooleanMethod(jniCallback, mid); |
| 108 return wrapJavaObject(jniEnv, NewBoolean(jniEnv, result)); |
| 109 } |
| 110 |
| 111 v8::Handle<v8::Value> showToastImpl(const v8::Arguments& args) |
| 112 { |
| 113 D(D_WARN, "showToast()"); |
| 114 v8::HandleScope handle_scope; |
| 115 |
| 116 JNIEnv* jniEnv = NULL; |
| 117 if (globalJvm->AttachCurrentThread(&jniEnv, NULL) != 0) |
| 118 return v8::ThrowException(v8::String::New("Failed to get JNI environment")); |
| 119 |
| 120 if (args.Length() < 1) |
| 121 return v8::ThrowException(v8::String::New("String expected")); |
| 122 |
| 123 v8::String::Utf8Value str(args[0]); |
| 124 if (!*str) |
| 125 return v8::ThrowException(v8::String::New("Parameter cannot be converted t
o string")); |
| 126 |
| 127 jstring jstr = jniEnv->NewStringUTF(*str); |
| 128 |
| 129 static jclass cls = jniEnv->GetObjectClass(jniCallback); |
| 130 static jmethodID mid = jniEnv->GetMethodID(cls, "showToast", "(Ljava/lang/Stri
ng;)V"); |
| 131 if (mid) |
| 132 jniEnv->CallVoidMethod(jniCallback, mid, jstr); |
| 133 jniEnv->DeleteLocalRef(jstr); |
| 134 |
| 135 return v8::Undefined(); |
| 136 } |
| 137 |
| 138 v8::Handle<v8::Value> printImpl(const v8::Arguments& args) |
| 139 { |
| 140 D(D_WARN, "print()"); |
| 141 bool first = true; |
| 142 std::string msg = ""; |
| 143 for (int i = 0; i < args.Length(); i++) |
| 144 { |
| 145 v8::HandleScope handle_scope; |
| 146 if (first) |
| 147 first = false; |
| 148 else |
| 149 msg += " "; |
| 150 v8::String::Utf8Value str(args[i]); |
| 151 if (!*str) |
| 152 return v8::ThrowException(v8::String::New("Parameter cannot be converted t
o string")); |
| 153 msg += *str; |
| 154 } |
| 155 |
| 156 return v8::Undefined(); |
| 157 } |
OLD | NEW |