| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 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> | |
| 19 | |
| 18 #include "Utils.h" | 20 #include "Utils.h" |
| 19 #include "Debug.h" | 21 #include "Debug.h" |
| 20 | 22 |
| 21 const std::string GetString(JNIEnv *pEnv, jstring str) | 23 const std::string GetString(JNIEnv *pEnv, jstring str) |
| 22 { | 24 { |
| 23 D(D_WARN, "getString()"); | 25 D(D_WARN, "getString()"); |
| 24 | 26 |
| 25 if (str == NULL) | 27 if (str == NULL) |
| 26 return std::string(); | 28 return std::string(); |
| 27 | 29 |
| 28 jboolean iscopy; | 30 jboolean iscopy; |
| 29 | 31 |
| 30 const char *s = pEnv->GetStringUTFChars(str, &iscopy); | 32 const char *s = pEnv->GetStringUTFChars(str, &iscopy); |
| 31 jsize len = pEnv->GetStringUTFLength(str); | 33 jsize len = pEnv->GetStringUTFLength(str); |
| 32 | 34 |
| 33 const std::string value(s, len); | 35 const std::string value(s, len); |
| 34 | 36 |
| 35 pEnv->ReleaseStringUTFChars(str, s); | 37 pEnv->ReleaseStringUTFChars(str, s); |
| 36 | 38 |
| 37 return value; | 39 return value; |
| 38 } | 40 } |
| 41 | |
| 42 std::string JniJavaToStdString(JNIEnv* env, jstring str) | |
| 43 { | |
| 44 if (!str) | |
| 45 { | |
| 46 return std::string(); | |
| 47 } | |
| 48 | |
| 49 const char* cStr = env->GetStringUTFChars(str, 0); | |
| 50 std::string ret(cStr); | |
| 51 env->ReleaseStringUTFChars(str, cStr); | |
| 52 | |
| 53 return ret; | |
| 54 } | |
| 55 | |
| 56 jobject NewJniArrayList(JNIEnv* env) | |
| 57 { | |
| 58 jclass clazz = env->FindClass("java/util/ArrayList"); | |
| 59 jmethodID ctor = env->GetMethodID(clazz, "<init>", "()V"); | |
| 60 return env->NewObject(clazz, ctor); | |
| 61 } | |
| 62 | |
| 63 void JniAddObjectToList(JNIEnv* env, jobject list, jobject value) | |
| 64 { | |
| 65 jmethodID add = env->GetMethodID(env->GetObjectClass(list), "add", "(Ljava/lan g/Object;)Z"); | |
| 66 env->CallBooleanMethod(list, add, value); | |
| 67 } | |
| 68 | |
| 69 void JniThrowException(JNIEnv* env, const std::string& message) | |
| 70 { | |
| 71 jclass clazz = env->FindClass(PKG("AdblockPlusException")); | |
|
Felix Dahlke
2014/03/28 17:28:41
We're only using AdblockPlusException for native e
René Jeschke
2014/03/31 09:10:38
I would also like to use the "AdblockPlusException
Felix Dahlke
2014/03/31 10:43:23
Alright.
René Jeschke
2014/04/11 12:25:53
Done.
| |
| 72 env->ThrowNew(clazz, message.c_str()); | |
| 73 } | |
| 74 | |
| 75 void JniThrowException(JNIEnv* env, const std::exception& e) | |
| 76 { | |
| 77 JniThrowException(env, e.what()); | |
| 78 } | |
| 79 | |
| 80 void JniThrowException(JNIEnv* env) | |
| 81 { | |
| 82 JniThrowException(env, "Unknown exception from libAdblockPlus"); | |
|
Felix Dahlke
2014/03/28 17:28:41
We usually call it "libadblockplus".
René Jeschke
2014/04/11 12:25:53
Done.
| |
| 83 } | |
| 84 | |
| 85 JNIEnvAcquire::JNIEnvAcquire(JavaVM* javaVM) | |
| 86 : javaVM(javaVM), jniEnv(0), attachmentStatus(0) | |
| 87 { | |
| 88 attachmentStatus = javaVM->GetEnv((void **)&jniEnv, ABP_JNI_VERSION); | |
| 89 if (attachmentStatus == JNI_EDETACHED) | |
| 90 { | |
| 91 if (javaVM->AttachCurrentThread(&jniEnv, 0)) | |
| 92 { | |
| 93 // This one is FATAL, we can't recover from this (because without a JVM we 're dead), so | |
| 94 // throwing a runtime_exception in a ctor can be tolerated here IMHO | |
|
Felix Dahlke
2014/03/28 17:28:41
Throwing a std::runtime_error from a constructor i
René Jeschke
2014/03/31 09:10:38
When using 'new' and exceptions from the ctor you
Felix Dahlke
2014/03/31 10:43:23
Are we talking about C++ constructors? I'm absolut
René Jeschke
2014/04/11 12:25:53
Done.
| |
| 95 throw std::runtime_error("Failed to get JNI environment"); | |
| 96 } | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 JNIEnvAcquire::~JNIEnvAcquire() | |
| 101 { | |
| 102 if (attachmentStatus == JNI_EDETACHED) | |
| 103 { | |
| 104 javaVM->DetachCurrentThread(); | |
| 105 } | |
| 106 } | |
| OLD | NEW |