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

Side by Side Diff: jni/Utils.cpp

Issue 6606493159784448: New JNI bindings (Closed)
Patch Set: Created March 14, 2014, 11:32 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « jni/Utils.h ('k') | src/com/github/rjeschke/neetutils/Objects.java » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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-2013 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 namespace AdblockPlus
43 {
44 namespace Android
45 {
46
47 std::string JniJava2StdString(JNIEnv* env, jstring str)
48 {
49 if (!str)
50 {
51 return std::string();
52 }
53
54 const char* cStr = env->GetStringUTFChars(str, 0);
55 std::string ret(cStr);
56 env->ReleaseStringUTFChars(str, cStr);
57
58 return ret;
59 }
60
61 jobject NewJniArrayList(JNIEnv* env)
62 {
63 jclass clazz = env->FindClass("java/util/ArrayList");
64 jmethodID ctor = env->GetMethodID(clazz, "<init>", "()V");
65 return env->NewObject(clazz, ctor);
66 }
67
68 void JniAddObjectToList(JNIEnv* env, jobject list, jobject value)
69 {
70 jmethodID add = env->GetMethodID(env->GetObjectClass(list), "add", "(Ljava/lan g/Object;)Z");
71 env->CallBooleanMethod(list, add, value);
72 }
73
74 void JniThrowException(JNIEnv* env, const std::string& message)
75 {
76 jclass clazz = env->FindClass(PKG("AdblockPlusException"));
77 env->ThrowNew(clazz, message.c_str());
78 }
79
80 void JniThrowException(JNIEnv* env, const std::exception& e)
81 {
82 JniThrowException(env, e.what());
83 }
84
85 void JniThrowException(JNIEnv* env)
86 {
87 JniThrowException(env, "Unknown exception from libAdblockPlus");
88 }
89
90 } // namespace Android
91 } // namespace AdblockPlus
OLDNEW
« no previous file with comments | « jni/Utils.h ('k') | src/com/github/rjeschke/neetutils/Objects.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld