Merge changes from topic "libpac_C_api"
* changes: Replace C++ API by the C API provided by libpac Use std::u16string instead of android::String16 due to API change in libpac
This commit is contained in:
@@ -16,6 +16,9 @@
|
|||||||
|
|
||||||
#define LOG_TAG "PacProcessor"
|
#define LOG_TAG "PacProcessor"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include <utils/Log.h>
|
#include <utils/Log.h>
|
||||||
#include <utils/Mutex.h>
|
#include <utils/Mutex.h>
|
||||||
#include "android_runtime/AndroidRuntime.h"
|
#include "android_runtime/AndroidRuntime.h"
|
||||||
@@ -23,24 +26,24 @@
|
|||||||
#include "jni.h"
|
#include "jni.h"
|
||||||
#include <nativehelper/JNIHelp.h>
|
#include <nativehelper/JNIHelp.h>
|
||||||
|
|
||||||
#include "proxy_resolver_v8.h"
|
#include "proxy_resolver_v8_wrapper.h"
|
||||||
|
|
||||||
namespace android {
|
namespace android {
|
||||||
|
|
||||||
net::ProxyResolverV8* proxyResolver = NULL;
|
ProxyResolverV8Handle* proxyResolver = NULL;
|
||||||
bool pacSet = false;
|
bool pacSet = false;
|
||||||
|
|
||||||
String16 jstringToString16(JNIEnv* env, jstring jstr) {
|
std::u16string jstringToString16(JNIEnv* env, jstring jstr) {
|
||||||
const jchar* str = env->GetStringCritical(jstr, 0);
|
const jchar* str = env->GetStringCritical(jstr, 0);
|
||||||
String16 str16(reinterpret_cast<const char16_t*>(str),
|
std::u16string str16(reinterpret_cast<const char16_t*>(str),
|
||||||
env->GetStringLength(jstr));
|
env->GetStringLength(jstr));
|
||||||
env->ReleaseStringCritical(jstr, str);
|
env->ReleaseStringCritical(jstr, str);
|
||||||
return str16;
|
return str16;
|
||||||
}
|
}
|
||||||
|
|
||||||
jstring string16ToJstring(JNIEnv* env, String16 string) {
|
jstring string16ToJstring(JNIEnv* env, std::u16string string) {
|
||||||
const char16_t* str = string.string();
|
const char16_t* str = string.data();
|
||||||
size_t len = string.size();
|
size_t len = string.length();
|
||||||
|
|
||||||
return env->NewString(reinterpret_cast<const jchar*>(str), len);
|
return env->NewString(reinterpret_cast<const jchar*>(str), len);
|
||||||
}
|
}
|
||||||
@@ -48,7 +51,7 @@ jstring string16ToJstring(JNIEnv* env, String16 string) {
|
|||||||
static jboolean com_android_pacprocessor_PacNative_createV8ParserNativeLocked(JNIEnv* /* env */,
|
static jboolean com_android_pacprocessor_PacNative_createV8ParserNativeLocked(JNIEnv* /* env */,
|
||||||
jobject) {
|
jobject) {
|
||||||
if (proxyResolver == NULL) {
|
if (proxyResolver == NULL) {
|
||||||
proxyResolver = new net::ProxyResolverV8(net::ProxyResolverJSBindings::CreateDefault());
|
proxyResolver = ProxyResolverV8Handle_new();
|
||||||
pacSet = false;
|
pacSet = false;
|
||||||
return JNI_FALSE;
|
return JNI_FALSE;
|
||||||
}
|
}
|
||||||
@@ -58,7 +61,7 @@ static jboolean com_android_pacprocessor_PacNative_createV8ParserNativeLocked(JN
|
|||||||
static jboolean com_android_pacprocessor_PacNative_destroyV8ParserNativeLocked(JNIEnv* /* env */,
|
static jboolean com_android_pacprocessor_PacNative_destroyV8ParserNativeLocked(JNIEnv* /* env */,
|
||||||
jobject) {
|
jobject) {
|
||||||
if (proxyResolver != NULL) {
|
if (proxyResolver != NULL) {
|
||||||
delete proxyResolver;
|
ProxyResolverV8Handle_delete(proxyResolver);
|
||||||
proxyResolver = NULL;
|
proxyResolver = NULL;
|
||||||
return JNI_FALSE;
|
return JNI_FALSE;
|
||||||
}
|
}
|
||||||
@@ -67,14 +70,14 @@ static jboolean com_android_pacprocessor_PacNative_destroyV8ParserNativeLocked(J
|
|||||||
|
|
||||||
static jboolean com_android_pacprocessor_PacNative_setProxyScriptNativeLocked(JNIEnv* env, jobject,
|
static jboolean com_android_pacprocessor_PacNative_setProxyScriptNativeLocked(JNIEnv* env, jobject,
|
||||||
jstring script) {
|
jstring script) {
|
||||||
String16 script16 = jstringToString16(env, script);
|
std::u16string script16 = jstringToString16(env, script);
|
||||||
|
|
||||||
if (proxyResolver == NULL) {
|
if (proxyResolver == NULL) {
|
||||||
ALOGE("V8 Parser not started when setting PAC script");
|
ALOGE("V8 Parser not started when setting PAC script");
|
||||||
return JNI_TRUE;
|
return JNI_TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (proxyResolver->SetPacScript(script16) != OK) {
|
if (ProxyResolverV8Handle_SetPacScript(proxyResolver, script16.data()) != OK) {
|
||||||
ALOGE("Unable to set PAC script");
|
ALOGE("Unable to set PAC script");
|
||||||
return JNI_TRUE;
|
return JNI_TRUE;
|
||||||
}
|
}
|
||||||
@@ -85,9 +88,8 @@ static jboolean com_android_pacprocessor_PacNative_setProxyScriptNativeLocked(JN
|
|||||||
|
|
||||||
static jstring com_android_pacprocessor_PacNative_makeProxyRequestNativeLocked(JNIEnv* env, jobject,
|
static jstring com_android_pacprocessor_PacNative_makeProxyRequestNativeLocked(JNIEnv* env, jobject,
|
||||||
jstring url, jstring host) {
|
jstring url, jstring host) {
|
||||||
String16 url16 = jstringToString16(env, url);
|
std::u16string url16 = jstringToString16(env, url);
|
||||||
String16 host16 = jstringToString16(env, host);
|
std::u16string host16 = jstringToString16(env, host);
|
||||||
String16 ret;
|
|
||||||
|
|
||||||
if (proxyResolver == NULL) {
|
if (proxyResolver == NULL) {
|
||||||
ALOGE("V8 Parser not initialized when running PAC script");
|
ALOGE("V8 Parser not initialized when running PAC script");
|
||||||
@@ -99,12 +101,14 @@ static jstring com_android_pacprocessor_PacNative_makeProxyRequestNativeLocked(J
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (proxyResolver->GetProxyForURL(url16, host16, &ret) != OK) {
|
std::unique_ptr<char16_t, decltype(&free)> result = std::unique_ptr<char16_t, decltype(&free)>(
|
||||||
String8 ret8(ret);
|
ProxyResolverV8Handle_GetProxyForURL(proxyResolver, url16.data(), host16.data()), &free);
|
||||||
ALOGE("Error Running PAC: %s", ret8.string());
|
if (result.get() == NULL) {
|
||||||
|
ALOGE("Error Running PAC");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::u16string ret(result.get());
|
||||||
jstring jret = string16ToJstring(env, ret);
|
jstring jret = string16ToJstring(env, ret);
|
||||||
|
|
||||||
return jret;
|
return jret;
|
||||||
|
|||||||
Reference in New Issue
Block a user