Skip to content

Commit 4fa5111

Browse files
committed
Add boolean param to handle showing errors in webview
1 parent b0f09a4 commit 4fa5111

File tree

4 files changed

+175
-148
lines changed

4 files changed

+175
-148
lines changed

OpacityCore/src/main/cpp/OpacityCore.cpp

Lines changed: 144 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -13,139 +13,139 @@ JavaVM *java_vm;
1313
jobject java_object;
1414

1515
void DeferThreadDetach(JNIEnv *env) {
16-
static pthread_key_t thread_key;
17-
18-
// Set up a Thread Specific Data key, and a callback that
19-
// will be executed when a thread is destroyed.
20-
// This is only done once, across all threads, and the value
21-
// associated with the key for any given thread will initially
22-
// be NULL.
23-
static auto run_once = [] {
24-
const auto err = pthread_key_create(&thread_key, [](void *ts_env) {
25-
if (ts_env) {
26-
java_vm->DetachCurrentThread();
27-
}
28-
});
29-
if (err) {
30-
// Failed to create TSD key. Throw an exception if you want to.
31-
}
32-
return 0;
33-
}();
34-
35-
// For the callback to actually be executed when a thread exits
36-
// we need to associate a non-NULL value with the key on that thread.
37-
// We can use the JNIEnv* as that value.
38-
const auto ts_env = pthread_getspecific(thread_key);
39-
if (!ts_env) {
40-
if (pthread_setspecific(thread_key, env)) {
41-
// Failed to set thread-specific value for key. Throw an exception if you
42-
// want to.
16+
static pthread_key_t thread_key;
17+
18+
// Set up a Thread Specific Data key, and a callback that
19+
// will be executed when a thread is destroyed.
20+
// This is only done once, across all threads, and the value
21+
// associated with the key for any given thread will initially
22+
// be NULL.
23+
static auto run_once = [] {
24+
const auto err = pthread_key_create(&thread_key, [](void *ts_env) {
25+
if (ts_env) {
26+
java_vm->DetachCurrentThread();
27+
}
28+
});
29+
if (err) {
30+
// Failed to create TSD key. Throw an exception if you want to.
31+
}
32+
return 0;
33+
}();
34+
35+
// For the callback to actually be executed when a thread exits
36+
// we need to associate a non-NULL value with the key on that thread.
37+
// We can use the JNIEnv* as that value.
38+
const auto ts_env = pthread_getspecific(thread_key);
39+
if (!ts_env) {
40+
if (pthread_setspecific(thread_key, env)) {
41+
// Failed to set thread-specific value for key. Throw an exception if you
42+
// want to.
43+
}
4344
}
44-
}
4545
}
4646

4747
JNIEnv *GetJniEnv() {
48-
JNIEnv *env = nullptr;
49-
// We still call GetEnv first to detect if the thread already
50-
// is attached. This is done to avoid setting up a DetachCurrentThread
51-
// call on a Java thread.
52-
53-
// g_vm is a global.
54-
auto get_env_result = java_vm->GetEnv((void **)&env, JNI_VERSION_1_6);
55-
if (get_env_result == JNI_EDETACHED) {
56-
if (java_vm->AttachCurrentThread(&env, nullptr) == JNI_OK) {
57-
DeferThreadDetach(env);
58-
} else {
59-
// Failed to attach thread. Throw an exception if you want to.
48+
JNIEnv *env = nullptr;
49+
// We still call GetEnv first to detect if the thread already
50+
// is attached. This is done to avoid setting up a DetachCurrentThread
51+
// call on a Java thread.
52+
53+
// g_vm is a global.
54+
auto get_env_result = java_vm->GetEnv((void **) &env, JNI_VERSION_1_6);
55+
if (get_env_result == JNI_EDETACHED) {
56+
if (java_vm->AttachCurrentThread(&env, nullptr) == JNI_OK) {
57+
DeferThreadDetach(env);
58+
} else {
59+
// Failed to attach thread. Throw an exception if you want to.
60+
}
61+
} else if (get_env_result == JNI_EVERSION) {
62+
// Unsupported JNI version. Throw an exception if you want to.
6063
}
61-
} else if (get_env_result == JNI_EVERSION) {
62-
// Unsupported JNI version. Throw an exception if you want to.
63-
}
64-
return env;
64+
return env;
6565
}
6666

6767
extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
68-
java_vm = jvm;
69-
return JNI_VERSION_1_6;
68+
java_vm = jvm;
69+
return JNI_VERSION_1_6;
7070
}
7171

7272
jstring string2jstring(JNIEnv *env, const char *str) {
73-
return (*env).NewStringUTF(str);
73+
return (*env).NewStringUTF(str);
7474
}
7575

7676
extern "C" void secure_set(const char *key, const char *value) {
77-
JNIEnv *env = GetJniEnv();
78-
// Get the Kotlin class
79-
jclass jOpacityCore = env->GetObjectClass(java_object);
77+
JNIEnv *env = GetJniEnv();
78+
// Get the Kotlin class
79+
jclass jOpacityCore = env->GetObjectClass(java_object);
8080

81-
// Get the method ID for the method you want to call
82-
jmethodID set_method = env->GetMethodID(
83-
jOpacityCore, "securelySet", "(Ljava/lang/String;Ljava/lang/String;)V");
81+
// Get the method ID for the method you want to call
82+
jmethodID set_method = env->GetMethodID(
83+
jOpacityCore, "securelySet", "(Ljava/lang/String;Ljava/lang/String;)V");
8484

85-
env->CallVoidMethod(java_object, set_method, string2jstring(env, key),
86-
string2jstring(env, value));
85+
env->CallVoidMethod(java_object, set_method, string2jstring(env, key),
86+
string2jstring(env, value));
8787
}
8888

8989
extern "C" const char *secure_get(const char *key) {
90-
JNIEnv *env = GetJniEnv();
91-
// Get the Kotlin class
92-
jclass jOpacityCore = env->GetObjectClass(java_object);
90+
JNIEnv *env = GetJniEnv();
91+
// Get the Kotlin class
92+
jclass jOpacityCore = env->GetObjectClass(java_object);
9393

94-
// Get the method ID for the method you want to call
95-
jmethodID set_method = env->GetMethodID(
96-
jOpacityCore, "securelyGet", "(Ljava/lang/String;)Ljava/lang/String;");
94+
// Get the method ID for the method you want to call
95+
jmethodID set_method = env->GetMethodID(
96+
jOpacityCore, "securelyGet", "(Ljava/lang/String;)Ljava/lang/String;");
9797

98-
auto res = (jstring)env->CallObjectMethod(java_object, set_method,
99-
string2jstring(env, key));
98+
auto res = (jstring) env->CallObjectMethod(java_object, set_method,
99+
string2jstring(env, key));
100100

101-
if (res == nullptr) {
102-
return nullptr;
103-
}
101+
if (res == nullptr) {
102+
return nullptr;
103+
}
104104

105-
const char *val_str = env->GetStringUTFChars(res, nullptr);
106-
return val_str;
105+
const char *val_str = env->GetStringUTFChars(res, nullptr);
106+
return val_str;
107107
}
108108

109109
extern "C" void android_prepare_request(const char *url) {
110-
JNIEnv *env = GetJniEnv();
111-
// Get the Kotlin class
112-
jclass jOpacityCore = env->GetObjectClass(java_object);
110+
JNIEnv *env = GetJniEnv();
111+
// Get the Kotlin class
112+
jclass jOpacityCore = env->GetObjectClass(java_object);
113113

114-
// Get the method ID for the method you want to call
115-
jmethodID openBrowserMethod = env->GetMethodID(
116-
jOpacityCore, "prepareInAppBrowser", "(Ljava/lang/String;)V");
114+
// Get the method ID for the method you want to call
115+
jmethodID openBrowserMethod = env->GetMethodID(
116+
jOpacityCore, "prepareInAppBrowser", "(Ljava/lang/String;)V");
117117

118-
// Call the method with the necessary parameters
119-
jstring jurl = env->NewStringUTF(url);
120-
env->CallVoidMethod(java_object, openBrowserMethod, jurl);
118+
// Call the method with the necessary parameters
119+
jstring jurl = env->NewStringUTF(url);
120+
env->CallVoidMethod(java_object, openBrowserMethod, jurl);
121121
}
122122

123123
extern "C" void android_set_request_header(const char *key, const char *value) {
124-
JNIEnv *env = GetJniEnv();
125-
// Get the Kotlin class
126-
jclass jOpacityCore = env->GetObjectClass(java_object);
127-
128-
// Get the method ID for the method you want to call
129-
jmethodID method =
130-
env->GetMethodID(jOpacityCore, "setBrowserHeader",
131-
"(Ljava/lang/String;Ljava/lang/String;)V");
132-
133-
// Call the method with the necessary parameters
134-
jstring jkey = env->NewStringUTF(key);
135-
jstring jvalue = env->NewStringUTF(value);
136-
env->CallVoidMethod(java_object, method, jkey, jvalue);
124+
JNIEnv *env = GetJniEnv();
125+
// Get the Kotlin class
126+
jclass jOpacityCore = env->GetObjectClass(java_object);
127+
128+
// Get the method ID for the method you want to call
129+
jmethodID method =
130+
env->GetMethodID(jOpacityCore, "setBrowserHeader",
131+
"(Ljava/lang/String;Ljava/lang/String;)V");
132+
133+
// Call the method with the necessary parameters
134+
jstring jkey = env->NewStringUTF(key);
135+
jstring jvalue = env->NewStringUTF(value);
136+
env->CallVoidMethod(java_object, method, jkey, jvalue);
137137
}
138138

139139
extern "C" void android_present_webview() {
140-
JNIEnv *env = GetJniEnv();
141-
// Get the Kotlin class
142-
jclass jOpacityCore = env->GetObjectClass(java_object);
140+
JNIEnv *env = GetJniEnv();
141+
// Get the Kotlin class
142+
jclass jOpacityCore = env->GetObjectClass(java_object);
143143

144-
// Get the method ID for the method you want to call
145-
jmethodID method = env->GetMethodID(jOpacityCore, "presentBrowser", "()V");
144+
// Get the method ID for the method you want to call
145+
jmethodID method = env->GetMethodID(jOpacityCore, "presentBrowser", "()V");
146146

147-
// Call the method with the necessary parameters
148-
env->CallVoidMethod(java_object, method);
147+
// Call the method with the necessary parameters
148+
env->CallVoidMethod(java_object, method);
149149
}
150150

151151
extern "C" const char *get_ip_address() {
@@ -158,7 +158,7 @@ extern "C" const char *get_ip_address() {
158158
if (!ifa->ifa_addr) continue;
159159

160160
if (ifa->ifa_addr->sa_family == AF_INET) { // Check for IPv4
161-
tmpAddrPtr = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
161+
tmpAddrPtr = &((struct sockaddr_in *) ifa->ifa_addr)->sin_addr;
162162
char addressBuffer[INET_ADDRSTRLEN];
163163
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
164164
std::string interfaceName(ifa->ifa_name);
@@ -185,27 +185,28 @@ extern "C" const char *get_ip_address() {
185185
}
186186

187187
extern "C" void android_close_webview() {
188-
JNIEnv *env = GetJniEnv();
189-
// Get the Kotlin class
190-
jclass jOpacityCore = env->GetObjectClass(java_object);
188+
JNIEnv *env = GetJniEnv();
189+
// Get the Kotlin class
190+
jclass jOpacityCore = env->GetObjectClass(java_object);
191191

192-
// Get the method ID for the method you want to call
193-
jmethodID method = env->GetMethodID(jOpacityCore, "closeBrowser", "()V");
192+
// Get the method ID for the method you want to call
193+
jmethodID method = env->GetMethodID(jOpacityCore, "closeBrowser", "()V");
194194

195-
// Call the method with the necessary parameters
196-
env->CallVoidMethod(java_object, method);
195+
// Call the method with the necessary parameters
196+
env->CallVoidMethod(java_object, method);
197197
}
198198

199199
extern "C" JNIEXPORT jint JNICALL
200200
Java_com_opacitylabs_opacitycore_OpacityCore_init(JNIEnv *env, jobject thiz,
201201
jstring api_key,
202202
jboolean dry_run,
203-
jint environment_enum) {
204-
java_object = env->NewGlobalRef(thiz);
205-
const char *api_key_str = env->GetStringUTFChars(api_key, nullptr);
206-
int result = opacity_core::init(api_key_str, dry_run,
207-
static_cast<int>(environment_enum));
208-
return result;
203+
jint environment_enum,
204+
jboolean show_errors_in_webview) {
205+
java_object = env->NewGlobalRef(thiz);
206+
const char *api_key_str = env->GetStringUTFChars(api_key, nullptr);
207+
int result = opacity_core::init(api_key_str, dry_run,
208+
static_cast<int>(environment_enum), show_errors_in_webview);
209+
return result;
209210
}
210211

211212
// extern "C" JNIEXPORT void JNICALL
@@ -220,46 +221,46 @@ Java_com_opacitylabs_opacitycore_OpacityCore_init(JNIEnv *env, jobject thiz,
220221
//
221222
extern "C" JNIEXPORT void JNICALL
222223
Java_com_opacitylabs_opacitycore_OpacityCore_emitWebviewEvent(
223-
JNIEnv *env, jobject thiz, jstring event_json) {
224-
const char *json = env->GetStringUTFChars(event_json, nullptr);
225-
opacity_core::emit_webview_event(json);
224+
JNIEnv *env, jobject thiz, jstring event_json) {
225+
const char *json = env->GetStringUTFChars(event_json, nullptr);
226+
opacity_core::emit_webview_event(json);
226227
}
227228

228229
jobject createOpacityResponse(JNIEnv *env, int status, char *res, char *err) {
229-
jclass opacityResponseClass =
230-
env->FindClass("com/opacitylabs/opacitycore/OpacityResponse");
231-
232-
jmethodID constructor =
233-
env->GetMethodID(opacityResponseClass, "<init>",
234-
"(ILjava/lang/String;Ljava/lang/String;)V");
235-
236-
jobject opacityResponse;
237-
jstring jres, jerr;
238-
if (status == opacity_core::OPACITY_OK) {
239-
jres = env->NewStringUTF(res);
240-
jerr = env->NewStringUTF(nullptr);
241-
opacity_core::free_string(res);
242-
} else {
243-
jres = env->NewStringUTF(nullptr);
244-
jerr = env->NewStringUTF(err);
245-
opacity_core::free_string(err);
246-
}
247-
248-
opacityResponse =
249-
env->NewObject(opacityResponseClass, constructor, status, jres, jerr);
250-
251-
return opacityResponse;
230+
jclass opacityResponseClass =
231+
env->FindClass("com/opacitylabs/opacitycore/OpacityResponse");
232+
233+
jmethodID constructor =
234+
env->GetMethodID(opacityResponseClass, "<init>",
235+
"(ILjava/lang/String;Ljava/lang/String;)V");
236+
237+
jobject opacityResponse;
238+
jstring jres, jerr;
239+
if (status == opacity_core::OPACITY_OK) {
240+
jres = env->NewStringUTF(res);
241+
jerr = env->NewStringUTF(nullptr);
242+
opacity_core::free_string(res);
243+
} else {
244+
jres = env->NewStringUTF(nullptr);
245+
jerr = env->NewStringUTF(err);
246+
opacity_core::free_string(err);
247+
}
248+
249+
opacityResponse =
250+
env->NewObject(opacityResponseClass, constructor, status, jres, jerr);
251+
252+
return opacityResponse;
252253
}
253254

254255
extern "C" JNIEXPORT jobject JNICALL
255256
Java_com_opacitylabs_opacitycore_OpacityCore_getNative(JNIEnv *env,
256257
jobject thiz,
257258
jstring name,
258259
jstring params) {
259-
char *res, *err;
260-
const char *name_str = env->GetStringUTFChars(name, nullptr);
261-
const char *params_str =
262-
params != nullptr ? env->GetStringUTFChars(params, nullptr) : nullptr;
263-
int status = opacity_core::get(name_str, params_str, &res, &err);
264-
return createOpacityResponse(env, status, res, err);
260+
char *res, *err;
261+
const char *name_str = env->GetStringUTFChars(name, nullptr);
262+
const char *params_str =
263+
params != nullptr ? env->GetStringUTFChars(params, nullptr) : nullptr;
264+
int status = opacity_core::get(name_str, params_str, &res, &err);
265+
return createOpacityResponse(env, status, res, err);
265266
}

OpacityCore/src/main/jni/include/opacity.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ extern const int32_t OPACITY_ENVIRONMENT_STAGING;
3232

3333
extern const int32_t OPACITY_ENVIRONMENT_PRODUCTION;
3434

35-
int32_t init(const char *api_key_str, bool dry_run, int32_t backend_environment);
35+
int32_t init(const char *api_key_str,
36+
bool dry_run,
37+
int32_t backend_environment,
38+
bool show_errors_in_webview);
3639

3740
int32_t get(const char *name, const char *params, char **res_ptr, char **err_ptr);
3841

0 commit comments

Comments
 (0)