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

Side by Side Diff: libadblockplus-android/jni/JniJsValue.cpp

Issue 29422558: Issue 5167 - Update to use libadblockplus revision dca8df9af1a7 (Closed)
Patch Set: updated dependencies (..-binaries) Created May 10, 2017, 6:54 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 | « libadblockplus-android/jni/JniJsValue.h ('k') | libadblockplus-android/jni/JniNotification.cpp » ('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 <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 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 29 matching lines...) Expand all
40 40
41 jclass GetJsValueClass() 41 jclass GetJsValueClass()
42 { 42 {
43 return (jsValueClass != NULL ? jsValueClass->Get() : NULL); 43 return (jsValueClass != NULL ? jsValueClass->Get() : NULL);
44 } 44 }
45 45
46 static jboolean JNICALL JniIsUndefined(JNIEnv* env, jclass clazz, jlong ptr) 46 static jboolean JNICALL JniIsUndefined(JNIEnv* env, jclass clazz, jlong ptr)
47 { 47 {
48 try 48 try
49 { 49 {
50 return JniGetJsValue(ptr)->IsUndefined() ? JNI_TRUE : JNI_FALSE; 50 return JniGetJsValuePtr(ptr)->IsUndefined() ? JNI_TRUE : JNI_FALSE;
51 } 51 }
52 CATCH_THROW_AND_RETURN(env, JNI_FALSE) 52 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
53 } 53 }
54 54
55 static jboolean JNICALL JniIsNull(JNIEnv* env, jclass clazz, jlong ptr) 55 static jboolean JNICALL JniIsNull(JNIEnv* env, jclass clazz, jlong ptr)
56 { 56 {
57 try 57 try
58 { 58 {
59 return JniGetJsValue(ptr)->IsNull() ? JNI_TRUE : JNI_FALSE; 59 return JniGetJsValuePtr(ptr)->IsNull() ? JNI_TRUE : JNI_FALSE;
60 } 60 }
61 CATCH_THROW_AND_RETURN(env, JNI_FALSE) 61 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
62 } 62 }
63 63
64 static jboolean JNICALL JniIsNumber(JNIEnv* env, jclass clazz, jlong ptr) 64 static jboolean JNICALL JniIsNumber(JNIEnv* env, jclass clazz, jlong ptr)
65 { 65 {
66 try 66 try
67 { 67 {
68 return JniGetJsValue(ptr)->IsNumber() ? JNI_TRUE : JNI_FALSE; 68 return JniGetJsValuePtr(ptr)->IsNumber() ? JNI_TRUE : JNI_FALSE;
69 } 69 }
70 CATCH_THROW_AND_RETURN(env, JNI_FALSE) 70 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
71 } 71 }
72 72
73 static jboolean JNICALL JniIsString(JNIEnv* env, jclass clazz, jlong ptr) 73 static jboolean JNICALL JniIsString(JNIEnv* env, jclass clazz, jlong ptr)
74 { 74 {
75 try 75 try
76 { 76 {
77 return JniGetJsValue(ptr)->IsString() ? JNI_TRUE : JNI_FALSE; 77 return JniGetJsValuePtr(ptr)->IsString() ? JNI_TRUE : JNI_FALSE;
78 } 78 }
79 CATCH_THROW_AND_RETURN(env, JNI_FALSE) 79 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
80 } 80 }
81 81
82 static jboolean JNICALL JniIsBoolean(JNIEnv* env, jclass clazz, jlong ptr) 82 static jboolean JNICALL JniIsBoolean(JNIEnv* env, jclass clazz, jlong ptr)
83 { 83 {
84 try 84 try
85 { 85 {
86 return JniGetJsValue(ptr)->IsBool() ? JNI_TRUE : JNI_FALSE; 86 return JniGetJsValuePtr(ptr)->IsBool() ? JNI_TRUE : JNI_FALSE;
87 } 87 }
88 CATCH_THROW_AND_RETURN(env, JNI_FALSE) 88 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
89 } 89 }
90 90
91 static jboolean JNICALL JniIsObject(JNIEnv* env, jclass clazz, jlong ptr) 91 static jboolean JNICALL JniIsObject(JNIEnv* env, jclass clazz, jlong ptr)
92 { 92 {
93 try 93 try
94 { 94 {
95 return JniGetJsValue(ptr)->IsObject() ? JNI_TRUE : JNI_FALSE; 95 return JniGetJsValuePtr(ptr)->IsObject() ? JNI_TRUE : JNI_FALSE;
96 } 96 }
97 CATCH_THROW_AND_RETURN(env, JNI_FALSE) 97 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
98 } 98 }
99 99
100 static jboolean JNICALL JniIsArray(JNIEnv* env, jclass clazz, jlong ptr) 100 static jboolean JNICALL JniIsArray(JNIEnv* env, jclass clazz, jlong ptr)
101 { 101 {
102 try 102 try
103 { 103 {
104 return JniGetJsValue(ptr)->IsArray() ? JNI_TRUE : JNI_FALSE; 104 return JniGetJsValuePtr(ptr)->IsArray() ? JNI_TRUE : JNI_FALSE;
105 } 105 }
106 CATCH_THROW_AND_RETURN(env, JNI_FALSE) 106 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
107 } 107 }
108 108
109 static jboolean JNICALL JniIsFunction(JNIEnv* env, jclass clazz, jlong ptr) 109 static jboolean JNICALL JniIsFunction(JNIEnv* env, jclass clazz, jlong ptr)
110 { 110 {
111 try 111 try
112 { 112 {
113 return JniGetJsValue(ptr)->IsFunction() ? JNI_TRUE : JNI_FALSE; 113 return JniGetJsValuePtr(ptr)->IsFunction() ? JNI_TRUE : JNI_FALSE;
114 } 114 }
115 CATCH_THROW_AND_RETURN(env, JNI_FALSE) 115 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
116 } 116 }
117 117
118 static jstring JNICALL JniAsString(JNIEnv* env, jclass clazz, jlong ptr) 118 static jstring JNICALL JniAsString(JNIEnv* env, jclass clazz, jlong ptr)
119 { 119 {
120 try 120 try
121 { 121 {
122 return env->NewStringUTF(JniGetJsValue(ptr)->AsString().c_str()); 122 AdblockPlus::JsValue* jsValue = JniGetJsValuePtr(ptr);
123 return env->NewStringUTF(jsValue->AsString().c_str());
123 } 124 }
124 CATCH_THROW_AND_RETURN(env, 0) 125 CATCH_THROW_AND_RETURN(env, 0)
125 } 126 }
126 127
127 static jlong JNICALL JniAsLong(JNIEnv* env, jclass clazz, jlong ptr) 128 static jlong JNICALL JniAsLong(JNIEnv* env, jclass clazz, jlong ptr)
128 { 129 {
129 try 130 try
130 { 131 {
131 return static_cast<jlong>(JniGetJsValue(ptr)->AsInt()); 132 return static_cast<jlong>(JniGetJsValuePtr(ptr)->AsInt());
132 } 133 }
133 CATCH_THROW_AND_RETURN(env, 0) 134 CATCH_THROW_AND_RETURN(env, 0)
134 } 135 }
135 136
136 static jboolean JNICALL JniAsBoolean(JNIEnv* env, jclass clazz, jlong ptr) 137 static jboolean JNICALL JniAsBoolean(JNIEnv* env, jclass clazz, jlong ptr)
137 { 138 {
138 try 139 try
139 { 140 {
140 return JniGetJsValue(ptr)->AsBool() ? JNI_TRUE : JNI_FALSE; 141 return JniGetJsValuePtr(ptr)->AsBool() ? JNI_TRUE : JNI_FALSE;
141 } 142 }
142 CATCH_THROW_AND_RETURN(env, JNI_FALSE) 143 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
143 } 144 }
144 145
145 static jobject JNICALL JniAsList(JNIEnv* env, jclass clazz, jlong ptr) 146 static jobject JNICALL JniAsList(JNIEnv* env, jclass clazz, jlong ptr)
146 { 147 {
147 try 148 try
148 { 149 {
149 AdblockPlus::JsValueList list = JniGetJsValue(ptr)->AsList(); 150 return JniJsValueListToArrayList(env, JniGetJsValuePtr(ptr)->AsList());
150
151 return JniJsValueListToArrayList(env, list);
152 } 151 }
153 CATCH_THROW_AND_RETURN(env, 0) 152 CATCH_THROW_AND_RETURN(env, 0)
154 } 153 }
155 154
156 static jobject JNICALL JniGetProperty(JNIEnv* env, jclass clazz, jlong ptr, jstr ing name) 155 static jobject JNICALL JniGetProperty(JNIEnv* env, jclass clazz, jlong ptr, jstr ing name)
157 { 156 {
158 try 157 try
159 { 158 {
160 return NewJniJsValue(env, JniGetJsValue(ptr)->GetProperty(JniJavaToStdString (env, name))); 159 return NewJniJsValue(env, JniGetJsValuePtr(ptr)->GetProperty(JniJavaToStdStr ing(env, name)));
161 } 160 }
162 CATCH_THROW_AND_RETURN(env, 0) 161 CATCH_THROW_AND_RETURN(env, 0)
163 } 162 }
164 163
165 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) 164 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
166 { 165 {
167 delete JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr); 166 delete JniLongToTypePtr<AdblockPlus::JsValue>(ptr);
168 } 167 }
169 168
170 jobject NewJniJsValue(JNIEnv* env, const AdblockPlus::JsValuePtr& jsValue, jclas s jsValueClassArg) 169 jobject NewJniJsValue(JNIEnv* env, AdblockPlus::JsValue&& jsValue, jclass jsValu eClassArg)
171 { 170 {
172 if (!jsValue.get()) 171 return env->NewObject(jsValueClass->Get(), jsValueCtor, new AdblockPlus::JsVal ue(std::move(jsValue)));
173 {
174 return 0;
175 }
176
177 jlong ptr = JniPtrToLong(new AdblockPlus::JsValuePtr(jsValue));
178 return env->NewObject(jsValueClass->Get(), jsValueCtor, ptr);
179 } 172 }
180 173
181 AdblockPlus::JsValue* JniGetJsValue(jlong ptr) 174 AdblockPlus::JsValue* JniGetJsValuePtr(jlong ptr)
182 { 175 {
183 return JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr)->get(); 176 return JniLongToTypePtr<AdblockPlus::JsValue>(ptr);
184 } 177 }
185 178
186 AdblockPlus::JsValuePtr& JniGetJsValuePtr(jlong ptr) 179 AdblockPlus::JsValue& JniGetJsValue(jlong ptr)
187 { 180 {
188 return *JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr); 181 return *JniLongToTypePtr<AdblockPlus::JsValue>(ptr);
189 } 182 }
190 183
191 jobject JniJsValueListToArrayList(JNIEnv* env, AdblockPlus::JsValueList& list) 184 jobject JniJsValueListToArrayList(JNIEnv* env, AdblockPlus::JsValueList&& list)
192 { 185 {
193 jobject arrayList = NewJniArrayList(env); 186 jobject arrayList = NewJniArrayList(env);
194 187
195 for (AdblockPlus::JsValueList::iterator it = list.begin(), end = list.end(); i t != end; ++it) 188 for (AdblockPlus::JsValueList::iterator it = list.begin(), end = list.end(); i t != end; ++it)
196 { 189 {
197 JniAddObjectToList(env, arrayList, 190 JniAddObjectToList(env, arrayList, *JniLocalReference<jobject>(env, NewJniJs Value(env, std::move(*it))));
198 *JniLocalReference<jobject>(env, NewJniJsValue(env, *it)));
199 } 191 }
200 192
201 return arrayList; 193 return arrayList;
202 } 194 }
203 195
204 // TODO: List of functions that lack JNI bindings 196 // TODO: List of functions that lack JNI bindings
205 //std::vector<std::string> GetOwnPropertyNames() const; 197 //std::vector<std::string> GetOwnPropertyNames() const;
206 //void SetProperty(const std::string& name, const std::string& val); 198 //void SetProperty(const std::string& name, const std::string& val);
207 //void SetProperty(const std::string& name, int64_t val); 199 //void SetProperty(const std::string& name, int64_t val);
208 //void SetProperty(const std::string& name, bool val); 200 //void SetProperty(const std::string& name, bool val);
209 //void SetProperty(const std::string& name, JsValuePtr value); 201 //void SetProperty(const std::string& name, JsValue value);
210 //void SetProperty(const std::string& name, const char* val); 202 //void SetProperty(const std::string& name, const char* val);
211 //inline void SetProperty(const std::string& name, int val); 203 //inline void SetProperty(const std::string& name, int val);
212 //std::string GetClass() const; 204 //std::string GetClass() const;
213 //JsValuePtr Call(const JsValueList& params = JsValueList(), AdblockPlus::JsValu ePtr thisPtr = AdblockPlus::JsValuePtr()) const; 205 //JsValue Call(const JsValueList& params = JsValueList(), AdblockPlus::JsValue t hisPtr = AdblockPlus::JsValue()) const;
214 206
215 static JNINativeMethod methods[] = 207 static JNINativeMethod methods[] =
216 { 208 {
217 { (char*)"isUndefined", (char*)"(J)Z", (void*)JniIsUndefined }, 209 { (char*)"isUndefined", (char*)"(J)Z", (void*)JniIsUndefined },
218 { (char*)"isNull", (char*)"(J)Z", (void*)JniIsNull }, 210 { (char*)"isNull", (char*)"(J)Z", (void*)JniIsNull },
219 { (char*)"isNumber", (char*)"(J)Z", (void*)JniIsNumber }, 211 { (char*)"isNumber", (char*)"(J)Z", (void*)JniIsNumber },
220 { (char*)"isString", (char*)"(J)Z", (void*)JniIsString }, 212 { (char*)"isString", (char*)"(J)Z", (void*)JniIsString },
221 { (char*)"isBoolean", (char*)"(J)Z", (void*)JniIsBoolean }, 213 { (char*)"isBoolean", (char*)"(J)Z", (void*)JniIsBoolean },
222 { (char*)"isObject", (char*)"(J)Z", (void*)JniIsObject }, 214 { (char*)"isObject", (char*)"(J)Z", (void*)JniIsObject },
223 { (char*)"isArray", (char*)"(J)Z", (void*)JniIsArray }, 215 { (char*)"isArray", (char*)"(J)Z", (void*)JniIsArray },
224 { (char*)"isFunction", (char*)"(J)Z", (void*)JniIsFunction }, 216 { (char*)"isFunction", (char*)"(J)Z", (void*)JniIsFunction },
225 { (char*)"asString", (char*)"(J)Ljava/lang/String;", (void*)JniAsString }, 217 { (char*)"asString", (char*)"(J)Ljava/lang/String;", (void*)JniAsString },
226 { (char*)"asLong", (char*)"(J)J", (void*)JniAsLong }, 218 { (char*)"asLong", (char*)"(J)J", (void*)JniAsLong },
227 { (char*)"asBoolean", (char*)"(J)Z", (void*)JniAsBoolean }, 219 { (char*)"asBoolean", (char*)"(J)Z", (void*)JniAsBoolean },
228 { (char*)"asList", (char*)"(J)Ljava/util/List;", (void*)JniAsList }, 220 { (char*)"asList", (char*)"(J)Ljava/util/List;", (void*)JniAsList },
229 { (char*)"getProperty", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*) JniGetProperty }, 221 { (char*)"getProperty", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*) JniGetProperty },
230 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } 222 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }
231 }; 223 };
232 224
233 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsValue_re gisterNatives(JNIEnv *env, jclass clazz) 225 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsValue_re gisterNatives(JNIEnv *env, jclass clazz)
234 { 226 {
235 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); 227 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
236 } 228 }
OLDNEW
« no previous file with comments | « libadblockplus-android/jni/JniJsValue.h ('k') | libadblockplus-android/jni/JniNotification.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld