1 /*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "PacProcessor"
18
19 #include <stdlib.h>
20 #include <string>
21
22 #include <utils/Log.h>
23 #include <utils/Mutex.h>
24 #include "android_runtime/AndroidRuntime.h"
25
26 #include "jni.h"
27 #include <nativehelper/JNIHelp.h>
28
29 #include "proxy_resolver_v8_wrapper.h"
30
31 namespace android {
32
33 ProxyResolverV8Handle* proxyResolver = NULL;
34 bool pacSet = false;
35
jstringToString16(JNIEnv * env,jstring jstr)36 std::u16string jstringToString16(JNIEnv* env, jstring jstr) {
37 const jchar* str = env->GetStringCritical(jstr, 0);
38 std::u16string str16(reinterpret_cast<const char16_t*>(str),
39 env->GetStringLength(jstr));
40 env->ReleaseStringCritical(jstr, str);
41 return str16;
42 }
43
string16ToJstring(JNIEnv * env,std::u16string string)44 jstring string16ToJstring(JNIEnv* env, std::u16string string) {
45 const char16_t* str = string.data();
46 size_t len = string.length();
47
48 return env->NewString(reinterpret_cast<const jchar*>(str), len);
49 }
50
com_android_pacprocessor_PacNative_createV8ParserNativeLocked(JNIEnv *,jobject)51 static jboolean com_android_pacprocessor_PacNative_createV8ParserNativeLocked(JNIEnv* /* env */,
52 jobject) {
53 if (proxyResolver == NULL) {
54 proxyResolver = ProxyResolverV8Handle_new();
55 pacSet = false;
56 return JNI_FALSE;
57 }
58 return JNI_TRUE;
59 }
60
com_android_pacprocessor_PacNative_destroyV8ParserNativeLocked(JNIEnv *,jobject)61 static jboolean com_android_pacprocessor_PacNative_destroyV8ParserNativeLocked(JNIEnv* /* env */,
62 jobject) {
63 if (proxyResolver != NULL) {
64 ProxyResolverV8Handle_delete(proxyResolver);
65 proxyResolver = NULL;
66 return JNI_FALSE;
67 }
68 return JNI_TRUE;
69 }
70
com_android_pacprocessor_PacNative_setProxyScriptNativeLocked(JNIEnv * env,jobject,jstring script)71 static jboolean com_android_pacprocessor_PacNative_setProxyScriptNativeLocked(JNIEnv* env, jobject,
72 jstring script) {
73 std::u16string script16 = jstringToString16(env, script);
74
75 if (proxyResolver == NULL) {
76 ALOGE("V8 Parser not started when setting PAC script");
77 return JNI_TRUE;
78 }
79
80 if (ProxyResolverV8Handle_SetPacScript(proxyResolver, script16.data()) != OK) {
81 ALOGE("Unable to set PAC script");
82 return JNI_TRUE;
83 }
84 pacSet = true;
85
86 return JNI_FALSE;
87 }
88
com_android_pacprocessor_PacNative_makeProxyRequestNativeLocked(JNIEnv * env,jobject,jstring url,jstring host)89 static jstring com_android_pacprocessor_PacNative_makeProxyRequestNativeLocked(JNIEnv* env, jobject,
90 jstring url, jstring host) {
91 std::u16string url16 = jstringToString16(env, url);
92 std::u16string host16 = jstringToString16(env, host);
93
94 if (proxyResolver == NULL) {
95 ALOGE("V8 Parser not initialized when running PAC script");
96 return NULL;
97 }
98
99 if (!pacSet) {
100 ALOGW("Attempting to run PAC with no script set");
101 return NULL;
102 }
103
104 std::unique_ptr<char16_t, decltype(&free)> result = std::unique_ptr<char16_t, decltype(&free)>(
105 ProxyResolverV8Handle_GetProxyForURL(proxyResolver, url16.data(), host16.data()), &free);
106 if (result.get() == NULL) {
107 ALOGE("Error Running PAC");
108 return NULL;
109 }
110
111 std::u16string ret(result.get());
112 jstring jret = string16ToJstring(env, ret);
113
114 return jret;
115 }
116
117 static const JNINativeMethod gMethods[] = {
118 { "createV8ParserNativeLocked", "()Z",
119 (void*)com_android_pacprocessor_PacNative_createV8ParserNativeLocked},
120 { "destroyV8ParserNativeLocked", "()Z",
121 (void*)com_android_pacprocessor_PacNative_destroyV8ParserNativeLocked},
122 { "setProxyScriptNativeLocked", "(Ljava/lang/String;)Z",
123 (void*)com_android_pacprocessor_PacNative_setProxyScriptNativeLocked},
124 { "makeProxyRequestNativeLocked", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
125 (void*)com_android_pacprocessor_PacNative_makeProxyRequestNativeLocked},
126 };
127
register_com_android_pacprocessor_PacNative(JNIEnv * env)128 int register_com_android_pacprocessor_PacNative(JNIEnv* env) {
129 return jniRegisterNativeMethods(env, "com/android/pacprocessor/PacNative",
130 gMethods, NELEM(gMethods));
131 }
132
133 } /* namespace android */
134