Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: jni/Utils.cpp

Issue 29345540: Issue 4030 - Move JNI bindings into separate library project (Closed)
Patch Set: Changeset in adblockplusandroid repo Created July 22, 2016, 12:10 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 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
20 #include "Utils.h"
21
22 std::string JniJavaToStdString(JNIEnv* env, jstring str)
23 {
24 if (!str)
25 {
26 return std::string();
27 }
28
29 const char* cStr = env->GetStringUTFChars(str, 0);
30 std::string ret(cStr);
31 env->ReleaseStringUTFChars(str, cStr);
32
33 return ret;
34 }
35
36 jobject NewJniArrayList(JNIEnv* env)
37 {
38 JniLocalReference<jclass> clazz(env, env->FindClass("java/util/ArrayList"));
39 jmethodID ctor = env->GetMethodID(*clazz, "<init>", "()V");
40 return env->NewObject(*clazz, ctor);
41 }
42
43 void JniAddObjectToList(JNIEnv* env, jobject list, jobject value)
44 {
45 JniLocalReference<jclass> clazz(env, env->GetObjectClass(list));
46 jmethodID add = env->GetMethodID(*clazz, "add", "(Ljava/lang/Object;)Z");
47 env->CallBooleanMethod(list, add, value);
48 }
49
50 void JniThrowException(JNIEnv* env, const std::string& message)
51 {
52 JniLocalReference<jclass> clazz(env,
53 env->FindClass(PKG("AdblockPlusException")));
54 env->ThrowNew(*clazz, message.c_str());
55 }
56
57 void JniThrowException(JNIEnv* env, const std::exception& e)
58 {
59 JniThrowException(env, e.what());
60 }
61
62 void JniThrowException(JNIEnv* env)
63 {
64 JniThrowException(env, "Unknown exception from libadblockplus");
65 }
66
67 JNIEnvAcquire::JNIEnvAcquire(JavaVM* javaVM)
68 : javaVM(javaVM), jniEnv(0), attachmentStatus(0)
69 {
70 attachmentStatus = javaVM->GetEnv((void **)&jniEnv, ABP_JNI_VERSION);
71 if (attachmentStatus == JNI_EDETACHED)
72 {
73 if (javaVM->AttachCurrentThread(&jniEnv, 0))
74 {
75 // This one is FATAL, we can't recover from this (because without a JVM we 're dead), so
76 // throwing a runtime_exception in a ctor can be tolerated here IMHO
77 throw std::runtime_error("Failed to get JNI environment");
78 }
79 }
80 }
81
82 JNIEnvAcquire::~JNIEnvAcquire()
83 {
84 if (attachmentStatus == JNI_EDETACHED)
85 {
86 javaVM->DetachCurrentThread();
87 }
88 }
89
90 template<typename T>
91 static jobject NewJniObject(JNIEnv* env, const T& value, const char* javaClass)
92 {
93 if (!value.get())
94 {
95 return 0;
96 }
97
98 JniLocalReference<jclass> clazz(
99 env,
100 env->FindClass(javaClass));
101 jmethodID method = env->GetMethodID(*clazz, "<init>", "(J)V");
102
103 return env->NewObject(
104 *clazz,
105 method,
106 JniPtrToLong(new T(value)));
107 }
108
109 jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter)
110 {
111 return NewJniObject(env, filter, PKG("Filter"));
112 }
113
114 jobject NewJniSubscription(JNIEnv* env,
115 const AdblockPlus::SubscriptionPtr& subscription)
116 {
117 return NewJniObject(env, subscription, PKG("Subscription"));
118 }
119
120 jobject NewJniNotification(JNIEnv* env,
121 const AdblockPlus::NotificationPtr& notification)
122 {
123 return NewJniObject(env, notification, PKG("Notification"));
124 }
OLDNEW
« dependencies ('K') | « jni/Utils.h ('k') | project.properties » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld