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

Delta Between Two Patch Sets: jni/Utils.h

Issue 6606493159784448: New JNI bindings (Closed)
Left Patch Set: Style review fixes Created March 28, 2014, 11 a.m.
Right Patch Set: Removed TODO from AppInfo. Created April 11, 2014, 1:28 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « jni/JniWebRequest.cpp ('k') | jni/Utils.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
(...skipping 10 matching lines...) Expand all
21 #include <string> 21 #include <string>
22 #include <algorithm> 22 #include <algorithm>
23 #include <jni.h> 23 #include <jni.h>
24 #include <stdexcept> 24 #include <stdexcept>
25 25
26 #include <AdblockPlus.h> 26 #include <AdblockPlus.h>
27 #include <AdblockPlus/tr1_memory.h> 27 #include <AdblockPlus/tr1_memory.h>
28 28
29 #include "v8/v8stdint.h" 29 #include "v8/v8stdint.h"
30 30
31 #define PKG(x) "org/adblockplus/android/api/" x 31 #define PKG(x) "org/adblockplus/libadblockplus/" x
32 #define TYP(x) "L" PKG(x) ";" 32 #define TYP(x) "L" PKG(x) ";"
33 33
34 #define ABP_JNI_VERSION JNI_VERSION_1_6 34 #define ABP_JNI_VERSION JNI_VERSION_1_6
35 35
36 void JniThrowException(JNIEnv* env, const std::string& message); 36 void JniThrowException(JNIEnv* env, const std::string& message);
37 37
38 void JniThrowException(JNIEnv* env, const std::exception& e); 38 void JniThrowException(JNIEnv* env, const std::exception& e);
39 39
40 void JniThrowException(JNIEnv* env); 40 void JniThrowException(JNIEnv* env);
41 41
42 class JNIEnvAcquire 42 class JNIEnvAcquire
43 { 43 {
44 public: 44 public:
45 JNIEnvAcquire(JavaVM* javaVM) : 45 JNIEnvAcquire(JavaVM* javaVM);
Felix Dahlke 2014/03/28 11:27:18 Colon on next line s'il vous plaît.
46 javaVM(javaVM), jniEnv(0), attachmentStatus(0) 46 ~JNIEnvAcquire();
47 {
48 attachmentStatus = javaVM->GetEnv((void **)&jniEnv, ABP_JNI_VERSION);
Felix Dahlke 2014/03/28 11:27:18 Same as JniCallbacks.h, non-trivial non-template f
49 if (attachmentStatus == JNI_EDETACHED)
50 {
51 if (javaVM->AttachCurrentThread(&jniEnv, 0))
52 {
53 // This one is FATAL, we can't recover from this (because without a JVM we're dead), so
54 // throwing a runtime_exception in a ctor can be tolerated here IMHO
55 throw std::runtime_error("Failed to get JNI environment");
56 }
57 }
58 }
59 47
60 ~JNIEnvAcquire() 48 JNIEnv* operator*()
61 {
62 if (attachmentStatus == JNI_EDETACHED)
63 {
64 javaVM->DetachCurrentThread();
65 }
66 }
67
68 inline JNIEnv* operator*()
69 { 49 {
70 return jniEnv; 50 return jniEnv;
71 } 51 }
72 52
73 inline JNIEnv* operator->() 53 JNIEnv* operator->()
74 { 54 {
75 return jniEnv; 55 return jniEnv;
76 } 56 }
77 57
78 private: 58 private:
79 JavaVM* javaVM; 59 JavaVM* javaVM;
80 JNIEnv* jniEnv; 60 JNIEnv* jniEnv;
81 int attachmentStatus; 61 int attachmentStatus;
82 }; 62 };
83 63
84 template<typename T> 64 template<typename T>
85 class JniGlobalReference 65 class JniGlobalReference
86 { 66 {
87 public: 67 public:
88 JniGlobalReference(JNIEnv* env, T reference) 68 JniGlobalReference(JNIEnv* env, T reference)
89 { 69 {
90 env->GetJavaVM(&javaVM); 70 env->GetJavaVM(&javaVM);
91 reference = static_cast<T>(env->NewGlobalRef(static_cast<jobject>(reference) )); 71 this->reference = static_cast<T>(env->NewGlobalRef(static_cast<jobject>(refe rence)));
92 } 72 }
93 73
94 ~JniGlobalReference() 74 ~JniGlobalReference()
95 { 75 {
96 JNIEnvAcquire env(javaVM); 76 JNIEnvAcquire env(javaVM);
97
98 env->DeleteGlobalRef(static_cast<jobject>(reference)); 77 env->DeleteGlobalRef(static_cast<jobject>(reference));
99 } 78 }
100 79
101 JniGlobalReference(const JniGlobalReference& other); 80 JniGlobalReference(const JniGlobalReference& other);
102 JniGlobalReference& operator=(const JniGlobalReference& other); 81 JniGlobalReference& operator=(const JniGlobalReference& other);
103 82
104 inline T get() 83 T Get()
105 { 84 {
106 return reference; 85 return reference;
107 } 86 }
108 87
109 typedef std::tr1::shared_ptr<JniGlobalReference<T> > Ptr; 88 typedef std::tr1::shared_ptr<JniGlobalReference<T> > Ptr;
110 89
111 private: 90 private:
112 T reference; 91 T reference;
113 JavaVM* javaVM; 92 JavaVM* javaVM;
114 }; 93 };
(...skipping 13 matching lines...) Expand all
128 // dynamic_casts to shared_ptr<T: JsValue> ... also as the same inheritance is m irrored on the Java 107 // dynamic_casts to shared_ptr<T: JsValue> ... also as the same inheritance is m irrored on the Java
129 // side (and Java will throw a class cast exception on error) this shouldn't be an issue (TM) 108 // side (and Java will throw a class cast exception on error) this shouldn't be an issue (TM)
130 template<typename T> 109 template<typename T>
131 inline T* JniLongToTypePtr(jlong value) 110 inline T* JniLongToTypePtr(jlong value)
132 { 111 {
133 return reinterpret_cast<T*>((size_t)value); 112 return reinterpret_cast<T*>((size_t)value);
134 } 113 }
135 114
136 jobject NewJniArrayList(JNIEnv* env); 115 jobject NewJniArrayList(JNIEnv* env);
137 116
138 std::string JniJava2StdString(JNIEnv* env, jstring str); 117 std::string JniJavaToStdString(JNIEnv* env, jstring str);
139 118
140 void JniAddObjectToList(JNIEnv* env, jobject list, jobject value); 119 void JniAddObjectToList(JNIEnv* env, jobject list, jobject value);
141 120
142 inline std::string JniGetStringField(JNIEnv* env, jclass clazz, jobject jObj, co nst char* name) 121 inline std::string JniGetStringField(JNIEnv* env, jclass clazz, jobject jObj, co nst char* name)
143 { 122 {
144 return JniJava2StdString(env, reinterpret_cast<jstring>(env->GetObjectField(jO bj, env->GetFieldID(clazz, name, "Ljava/lang/String;")))); 123 return JniJavaToStdString(env, reinterpret_cast<jstring>(env->GetObjectField(j Obj, env->GetFieldID(clazz, name, "Ljava/lang/String;"))));
145 } 124 }
146 125
147 inline bool JniGetBooleanField(JNIEnv* env, jclass clazz, jobject jObj, const ch ar* name) 126 inline bool JniGetBooleanField(JNIEnv* env, jclass clazz, jobject jObj, const ch ar* name)
148 { 127 {
149 return env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "Z")) == JNI_TR UE; 128 return env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "Z")) == JNI_TR UE;
150 } 129 }
151 130
152 inline int32_t JniGetIntField(JNIEnv* env, jclass clazz, jobject jObj, const cha r* name) 131 inline int32_t JniGetIntField(JNIEnv* env, jclass clazz, jobject jObj, const cha r* name)
153 { 132 {
154 return (int32_t)env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "I")); 133 return (int32_t)env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "I"));
(...skipping 10 matching lines...) Expand all
165 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V"); 144 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V");
166 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::FilterPtr(f ilter))); 145 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::FilterPtr(f ilter)));
167 } 146 }
168 147
169 inline jobject NewJniSubscription(JNIEnv* env, const AdblockPlus::SubscriptionPt r& subscription) 148 inline jobject NewJniSubscription(JNIEnv* env, const AdblockPlus::SubscriptionPt r& subscription)
170 { 149 {
171 jclass clazz = env->FindClass(PKG("Subscription")); 150 jclass clazz = env->FindClass(PKG("Subscription"));
172 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V"); 151 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V");
173 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::Subscriptio nPtr(subscription))); 152 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::Subscriptio nPtr(subscription)));
174 } 153 }
175
176 #define TRY try
177 154
178 #define CATCH_AND_THROW(jEnv) \ 155 #define CATCH_AND_THROW(jEnv) \
179 catch (const std::exception& except) \ 156 catch (const std::exception& except) \
180 { \ 157 { \
181 JniThrowException(jEnv, except); \ 158 JniThrowException(jEnv, except); \
182 } \ 159 } \
183 catch (...) \ 160 catch (...) \
184 { \ 161 { \
185 JniThrowException(jEnv); \ 162 JniThrowException(jEnv); \
186 } 163 }
(...skipping 16 matching lines...) Expand all
203 T TrimString(T text) 180 T TrimString(T text)
204 { 181 {
205 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring 182 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring
206 T trimmed(text); 183 T trimmed(text);
207 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), st d::not1(std::ptr_fun<int, int>(std::isspace)))); 184 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), st d::not1(std::ptr_fun<int, int>(std::isspace))));
208 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(std::pt r_fun<int, int>(std::isspace))).base(), trimmed.end()); 185 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(std::pt r_fun<int, int>(std::isspace))).base(), trimmed.end());
209 return trimmed; 186 return trimmed;
210 } 187 }
211 188
212 #endif 189 #endif
LEFTRIGHT

Powered by Google App Engine
This is Rietveld