| LEFT | RIGHT |
| 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-2013 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 * |
| (...skipping 18 matching lines...) Expand all Loading... |
| 32 const char *s = pEnv->GetStringUTFChars(str, &iscopy); | 32 const char *s = pEnv->GetStringUTFChars(str, &iscopy); |
| 33 jsize len = pEnv->GetStringUTFLength(str); | 33 jsize len = pEnv->GetStringUTFLength(str); |
| 34 | 34 |
| 35 const std::string value(s, len); | 35 const std::string value(s, len); |
| 36 | 36 |
| 37 pEnv->ReleaseStringUTFChars(str, s); | 37 pEnv->ReleaseStringUTFChars(str, s); |
| 38 | 38 |
| 39 return value; | 39 return value; |
| 40 } | 40 } |
| 41 | 41 |
| 42 namespace AdblockPlus | 42 std::string JniJavaToStdString(JNIEnv* env, jstring str) |
| 43 { | |
| 44 namespace Android | |
| 45 { | |
| 46 | |
| 47 std::string JniJava2StdString(JNIEnv* env, jstring str) | |
| 48 { | 43 { |
| 49 if (!str) | 44 if (!str) |
| 50 { | 45 { |
| 51 return std::string(); | 46 return std::string(); |
| 52 } | 47 } |
| 53 | 48 |
| 54 const char* cStr = env->GetStringUTFChars(str, 0); | 49 const char* cStr = env->GetStringUTFChars(str, 0); |
| 55 std::string ret(cStr); | 50 std::string ret(cStr); |
| 56 env->ReleaseStringUTFChars(str, cStr); | 51 env->ReleaseStringUTFChars(str, cStr); |
| 57 | 52 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 77 env->ThrowNew(clazz, message.c_str()); | 72 env->ThrowNew(clazz, message.c_str()); |
| 78 } | 73 } |
| 79 | 74 |
| 80 void JniThrowException(JNIEnv* env, const std::exception& e) | 75 void JniThrowException(JNIEnv* env, const std::exception& e) |
| 81 { | 76 { |
| 82 JniThrowException(env, e.what()); | 77 JniThrowException(env, e.what()); |
| 83 } | 78 } |
| 84 | 79 |
| 85 void JniThrowException(JNIEnv* env) | 80 void JniThrowException(JNIEnv* env) |
| 86 { | 81 { |
| 87 JniThrowException(env, "Unknown exception from libAdblockPlus"); | 82 JniThrowException(env, "Unknown exception from libadblockplus"); |
| 88 } | 83 } |
| 89 | 84 |
| 90 } // namespace Android | 85 JNIEnvAcquire::JNIEnvAcquire(JavaVM* javaVM) |
| 91 } // namespace AdblockPlus | 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 |
| 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 } |
| LEFT | RIGHT |