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 implements LibpacInterface { 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 52 @Override startPacSupport()53 public synchronized boolean startPacSupport() { 54 if (createV8ParserNativeLocked()) { 55 Log.e(TAG, "Unable to Create v8 Proxy Parser."); 56 return false; 57 } 58 mIsActive = true; 59 return true; 60 } 61 62 @Override stopPacSupport()63 public synchronized boolean stopPacSupport() { 64 if (mIsActive) { 65 if (destroyV8ParserNativeLocked()) { 66 Log.e(TAG, "Unable to Destroy v8 Proxy Parser."); 67 return false; 68 } 69 mIsActive = false; 70 } 71 return true; 72 } 73 74 @Override setCurrentProxyScript(String script)75 public synchronized boolean setCurrentProxyScript(String script) { 76 if (setProxyScriptNativeLocked(script)) { 77 Log.e(TAG, "Unable to parse proxy script."); 78 return false; 79 } 80 return true; 81 } 82 83 @Override makeProxyRequest(String url, String host)84 public synchronized String makeProxyRequest(String url, String host) { 85 String ret = makeProxyRequestNativeLocked(url, host); 86 if ((ret == null) || (ret.length() == 0)) { 87 Log.e(TAG, "v8 Proxy request failed."); 88 ret = null; 89 } 90 return ret; 91 } 92 isActive()93 public synchronized boolean isActive() { 94 return mIsActive; 95 } 96 } 97