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