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 package com.android.pacprocessor;
17 
18 import android.util.Log;
19 
20 /**
21  * @hide
22  */
23 public class PacNative {
24     private static final String TAG = "PacProxy";
25 
26     private static final PacNative sInstance = new PacNative();
27 
28     private String mCurrentPac;
29 
30     private boolean mIsActive;
31 
32     // Only make native calls from inside synchronized blocks.
createV8ParserNativeLocked()33     private native boolean createV8ParserNativeLocked();
destroyV8ParserNativeLocked()34     private native boolean destroyV8ParserNativeLocked();
35 
setProxyScriptNativeLocked(String script)36     private native boolean setProxyScriptNativeLocked(String script);
37 
makeProxyRequestNativeLocked(String url, String host)38     private native String makeProxyRequestNativeLocked(String url, String host);
39 
40     static {
41         System.loadLibrary("jni_pacprocessor");
42     }
43 
PacNative()44     private PacNative() {
45 
46     }
47 
getInstance()48     public static PacNative getInstance() {
49         return sInstance;
50     }
51 
startPacSupport()52     public synchronized boolean startPacSupport() {
53         if (createV8ParserNativeLocked()) {
54             Log.e(TAG, "Unable to Create v8 Proxy Parser.");
55             return true;
56         }
57         mIsActive = true;
58         return false;
59     }
60 
stopPacSupport()61     public synchronized boolean stopPacSupport() {
62         if (mIsActive) {
63             if (destroyV8ParserNativeLocked()) {
64                 Log.e(TAG, "Unable to Destroy v8 Proxy Parser.");
65                 return true;
66             }
67             mIsActive = false;
68         }
69         return false;
70     }
71 
setCurrentProxyScript(String script)72     public synchronized boolean setCurrentProxyScript(String script) {
73         if (setProxyScriptNativeLocked(script)) {
74             Log.e(TAG, "Unable to parse proxy script.");
75             return true;
76         }
77         return false;
78     }
79 
makeProxyRequest(String url, String host)80     public synchronized String makeProxyRequest(String url, String host) {
81         String ret = makeProxyRequestNativeLocked(url, host);
82         if ((ret == null) || (ret.length() == 0)) {
83             Log.e(TAG, "v8 Proxy request failed.");
84             ret = null;
85         }
86         return ret;
87     }
88 
isActive()89     public synchronized boolean isActive() {
90         return mIsActive;
91     }
92 }
93