From 64c3f6d6ba02841d86c68c39b523ab1dc22f07cb Mon Sep 17 00:00:00 2001 From: Victor Chang Date: Fri, 4 Jan 2019 11:43:25 +0000 Subject: [PATCH 1/2] Use std::u16string instead of android::String16 due to API change in libpac Bug: 121269980 Test: m droid Change-Id: I17a91114f1a97b2ccd3049680aace1719eb7ae8a --- .../com_android_pacprocessor_PacNative.cpp | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/packages/services/PacProcessor/jni/com_android_pacprocessor_PacNative.cpp b/packages/services/PacProcessor/jni/com_android_pacprocessor_PacNative.cpp index a40cf1c06d075..d43ef5df0ba83 100644 --- a/packages/services/PacProcessor/jni/com_android_pacprocessor_PacNative.cpp +++ b/packages/services/PacProcessor/jni/com_android_pacprocessor_PacNative.cpp @@ -16,6 +16,8 @@ #define LOG_TAG "PacProcessor" +#include + #include #include #include "android_runtime/AndroidRuntime.h" @@ -30,17 +32,17 @@ namespace android { net::ProxyResolverV8* proxyResolver = NULL; bool pacSet = false; -String16 jstringToString16(JNIEnv* env, jstring jstr) { +std::u16string jstringToString16(JNIEnv* env, jstring jstr) { const jchar* str = env->GetStringCritical(jstr, 0); - String16 str16(reinterpret_cast(str), + std::u16string str16(reinterpret_cast(str), env->GetStringLength(jstr)); env->ReleaseStringCritical(jstr, str); return str16; } -jstring string16ToJstring(JNIEnv* env, String16 string) { - const char16_t* str = string.string(); - size_t len = string.size(); +jstring string16ToJstring(JNIEnv* env, std::u16string string) { + const char16_t* str = string.data(); + size_t len = string.length(); return env->NewString(reinterpret_cast(str), len); } @@ -67,7 +69,7 @@ static jboolean com_android_pacprocessor_PacNative_destroyV8ParserNativeLocked(J static jboolean com_android_pacprocessor_PacNative_setProxyScriptNativeLocked(JNIEnv* env, jobject, jstring script) { - String16 script16 = jstringToString16(env, script); + std::u16string script16 = jstringToString16(env, script); if (proxyResolver == NULL) { ALOGE("V8 Parser not started when setting PAC script"); @@ -85,9 +87,9 @@ static jboolean com_android_pacprocessor_PacNative_setProxyScriptNativeLocked(JN static jstring com_android_pacprocessor_PacNative_makeProxyRequestNativeLocked(JNIEnv* env, jobject, jstring url, jstring host) { - String16 url16 = jstringToString16(env, url); - String16 host16 = jstringToString16(env, host); - String16 ret; + std::u16string url16 = jstringToString16(env, url); + std::u16string host16 = jstringToString16(env, host); + std::u16string ret; if (proxyResolver == NULL) { ALOGE("V8 Parser not initialized when running PAC script"); @@ -100,7 +102,7 @@ static jstring com_android_pacprocessor_PacNative_makeProxyRequestNativeLocked(J } if (proxyResolver->GetProxyForURL(url16, host16, &ret) != OK) { - String8 ret8(ret); + String8 ret8(ret.data()); ALOGE("Error Running PAC: %s", ret8.string()); return NULL; } From 361f4eb8c0f47fe843f5302d74f505202fc0916b Mon Sep 17 00:00:00 2001 From: Victor Chang Date: Fri, 4 Jan 2019 19:21:18 +0000 Subject: [PATCH 2/2] Replace C++ API by the C API provided by libpac - libpac will be moved into the Runtime APEX module. Use the new stable C API interface provided by libpac - The change also removes the following debug log when error occurs. ALOGE("Error Running PAC: %s", ret8.string()); When ProxyServerV8::GetProxyForURL != OK, ret8 may not contain the error message, but the non-ASCII proxy names. Bug: 121269980 Test: m droid Change-Id: I0ea0ad7489a23cbc0476dcd66d320f80499f8be1 --- .../com_android_pacprocessor_PacNative.cpp | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/services/PacProcessor/jni/com_android_pacprocessor_PacNative.cpp b/packages/services/PacProcessor/jni/com_android_pacprocessor_PacNative.cpp index d43ef5df0ba83..d969c69cfaf16 100644 --- a/packages/services/PacProcessor/jni/com_android_pacprocessor_PacNative.cpp +++ b/packages/services/PacProcessor/jni/com_android_pacprocessor_PacNative.cpp @@ -16,6 +16,7 @@ #define LOG_TAG "PacProcessor" +#include #include #include @@ -25,11 +26,11 @@ #include "jni.h" #include -#include "proxy_resolver_v8.h" +#include "proxy_resolver_v8_wrapper.h" namespace android { -net::ProxyResolverV8* proxyResolver = NULL; +ProxyResolverV8Handle* proxyResolver = NULL; bool pacSet = false; std::u16string jstringToString16(JNIEnv* env, jstring jstr) { @@ -50,7 +51,7 @@ jstring string16ToJstring(JNIEnv* env, std::u16string string) { static jboolean com_android_pacprocessor_PacNative_createV8ParserNativeLocked(JNIEnv* /* env */, jobject) { if (proxyResolver == NULL) { - proxyResolver = new net::ProxyResolverV8(net::ProxyResolverJSBindings::CreateDefault()); + proxyResolver = ProxyResolverV8Handle_new(); pacSet = false; return JNI_FALSE; } @@ -60,7 +61,7 @@ static jboolean com_android_pacprocessor_PacNative_createV8ParserNativeLocked(JN static jboolean com_android_pacprocessor_PacNative_destroyV8ParserNativeLocked(JNIEnv* /* env */, jobject) { if (proxyResolver != NULL) { - delete proxyResolver; + ProxyResolverV8Handle_delete(proxyResolver); proxyResolver = NULL; return JNI_FALSE; } @@ -76,7 +77,7 @@ static jboolean com_android_pacprocessor_PacNative_setProxyScriptNativeLocked(JN return JNI_TRUE; } - if (proxyResolver->SetPacScript(script16) != OK) { + if (ProxyResolverV8Handle_SetPacScript(proxyResolver, script16.data()) != OK) { ALOGE("Unable to set PAC script"); return JNI_TRUE; } @@ -89,7 +90,6 @@ static jstring com_android_pacprocessor_PacNative_makeProxyRequestNativeLocked(J jstring url, jstring host) { std::u16string url16 = jstringToString16(env, url); std::u16string host16 = jstringToString16(env, host); - std::u16string ret; if (proxyResolver == NULL) { ALOGE("V8 Parser not initialized when running PAC script"); @@ -101,12 +101,14 @@ static jstring com_android_pacprocessor_PacNative_makeProxyRequestNativeLocked(J return NULL; } - if (proxyResolver->GetProxyForURL(url16, host16, &ret) != OK) { - String8 ret8(ret.data()); - ALOGE("Error Running PAC: %s", ret8.string()); + std::unique_ptr result = std::unique_ptr( + ProxyResolverV8Handle_GetProxyForURL(proxyResolver, url16.data(), host16.data()), &free); + if (result.get() == NULL) { + ALOGE("Error Running PAC"); return NULL; } + std::u16string ret(result.get()); jstring jret = string16ToJstring(env, ret); return jret;