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: Reuploaded full diff Created March 28, 2014, 3:56 p.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
Felix Dahlke 2014/03/28 17:28:41 PKG is fine, but I'd personally vote for "PACKAGE"
René Jeschke 2014/03/31 09:10:38 I prefer PKG because it's only 3 chars. As there a
Felix Dahlke 2014/03/31 10:43:23 OK, fine with having no prefix. Also fine with PKG
René Jeschke 2014/04/11 12:25:53 Done.
32 #define TYP(x) "L" PKG(x) ";" 32 #define TYP(x) "L" PKG(x) ";"
Felix Dahlke 2014/03/28 17:28:41 Correct me if I'm wrong, but it seems the proper n
René Jeschke 2014/03/31 09:10:38 That's not quite correct. The name of the class co
Felix Dahlke 2014/03/31 10:43:23 Na it's not, we should both be able to understand
René Jeschke 2014/04/11 12:25:53 Done.
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
(...skipping 18 matching lines...) Expand all
61 int attachmentStatus; 61 int attachmentStatus;
62 }; 62 };
63 63
64 template<typename T> 64 template<typename T>
65 class JniGlobalReference 65 class JniGlobalReference
66 { 66 {
67 public: 67 public:
68 JniGlobalReference(JNIEnv* env, T reference) 68 JniGlobalReference(JNIEnv* env, T reference)
69 { 69 {
70 env->GetJavaVM(&javaVM); 70 env->GetJavaVM(&javaVM);
71 reference = static_cast<T>(env->NewGlobalRef(static_cast<jobject>(reference) )); 71 this->reference = static_cast<T>(env->NewGlobalRef(static_cast<jobject>(refe rence)));
72 } 72 }
73 73
74 ~JniGlobalReference() 74 ~JniGlobalReference()
75 { 75 {
76 JNIEnvAcquire env(javaVM); 76 JNIEnvAcquire env(javaVM);
77
78 env->DeleteGlobalRef(static_cast<jobject>(reference)); 77 env->DeleteGlobalRef(static_cast<jobject>(reference));
79 } 78 }
80 79
81 JniGlobalReference(const JniGlobalReference& other); 80 JniGlobalReference(const JniGlobalReference& other);
82 JniGlobalReference& operator=(const JniGlobalReference& other); 81 JniGlobalReference& operator=(const JniGlobalReference& other);
83 82
84 T get() 83 T Get()
Felix Dahlke 2014/03/28 17:28:41 All functions should start with upper case letters
René Jeschke 2014/04/11 12:25:53 Done.
85 { 84 {
86 return reference; 85 return reference;
87 } 86 }
88 87
89 typedef std::tr1::shared_ptr<JniGlobalReference<T> > Ptr; 88 typedef std::tr1::shared_ptr<JniGlobalReference<T> > Ptr;
90 89
91 private: 90 private:
92 T reference; 91 T reference;
93 JavaVM* javaVM; 92 JavaVM* javaVM;
94 }; 93 };
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::FilterPtr(f ilter))); 145 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::FilterPtr(f ilter)));
147 } 146 }
148 147
149 inline jobject NewJniSubscription(JNIEnv* env, const AdblockPlus::SubscriptionPt r& subscription) 148 inline jobject NewJniSubscription(JNIEnv* env, const AdblockPlus::SubscriptionPt r& subscription)
150 { 149 {
151 jclass clazz = env->FindClass(PKG("Subscription")); 150 jclass clazz = env->FindClass(PKG("Subscription"));
152 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V"); 151 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V");
153 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::Subscriptio nPtr(subscription))); 152 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::Subscriptio nPtr(subscription)));
154 } 153 }
155 154
156 #define TRY try
Felix Dahlke 2014/03/28 17:28:41 I believe we can live without this one :D
René Jeschke 2014/03/31 09:10:38 It is just for symmetry reasons as: try { // ...
Felix Dahlke 2014/03/31 10:43:23 Using "TRY" suggests that some magic is going on h
René Jeschke 2014/04/11 12:25:53 Done.
157
158 #define CATCH_AND_THROW(jEnv) \ 155 #define CATCH_AND_THROW(jEnv) \
Felix Dahlke 2014/03/28 17:28:41 How about "CATCH_AND_RETHROW"?
René Jeschke 2014/03/31 09:10:38 Rethrow would imply that I would catch the C++ exc
Felix Dahlke 2014/03/31 10:43:23 Fair enough.
René Jeschke 2014/04/11 12:25:53 Done.
159 catch (const std::exception& except) \ 156 catch (const std::exception& except) \
160 { \ 157 { \
161 JniThrowException(jEnv, except); \ 158 JniThrowException(jEnv, except); \
162 } \ 159 } \
163 catch (...) \ 160 catch (...) \
164 { \ 161 { \
165 JniThrowException(jEnv); \ 162 JniThrowException(jEnv); \
166 } 163 }
167 164
168 #define CATCH_THROW_AND_RETURN(jEnv, retVal) \ 165 #define CATCH_THROW_AND_RETURN(jEnv, retVal) \
(...skipping 14 matching lines...) Expand all
183 T TrimString(T text) 180 T TrimString(T text)
184 { 181 {
185 // 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
186 T trimmed(text); 183 T trimmed(text);
187 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))));
188 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());
189 return trimmed; 186 return trimmed;
190 } 187 }
191 188
192 #endif 189 #endif
LEFTRIGHT

Powered by Google App Engine
This is Rietveld