diff options
author | Anna Malova <amalova@google.com> | 2020-01-22 14:17:14 +0000 |
---|---|---|
committer | Anna Malova <amalova@google.com> | 2020-02-11 12:54:26 +0000 |
commit | 29ed0cc421bcd5dce43d8d8bf2885bc6c2a52ef5 (patch) | |
tree | b2c7c03a209cd95e10656f94aac5ebfe01b47dd5 /packages/services | |
parent | 83235861a5e1ae30411acc8105291ded37412b21 (diff) |
Implement PacService using WebView's version of libpac.
Add config flag to choose between PacService implementations.
Added flag (config_useWebViewPacProcessor) is disabled
by default for now.
Bug: 148516710
Test: atest DeviceOwnerTest#testProxyPacProxyTest
Change-Id: I5d376a2a37f1bfb7ba5268c0088bf4417434b8f5
Diffstat (limited to 'packages/services')
4 files changed, 114 insertions, 13 deletions
diff --git a/packages/services/PacProcessor/src/com/android/pacprocessor/LibpacInterface.java b/packages/services/PacProcessor/src/com/android/pacprocessor/LibpacInterface.java new file mode 100644 index 000000000000..103ef7811ff0 --- /dev/null +++ b/packages/services/PacProcessor/src/com/android/pacprocessor/LibpacInterface.java @@ -0,0 +1,34 @@ +/* + * Copyright 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.pacprocessor; + +/** + * Common interface for both Android's and WebView's implementation of PAC processor. + * + * @hide + */ +interface LibpacInterface { + default boolean startPacSupport() { + return true; + } + + default boolean stopPacSupport() { + return true; + } + + boolean setCurrentProxyScript(String script); + String makeProxyRequest(String url, String host); +} diff --git a/packages/services/PacProcessor/src/com/android/pacprocessor/PacNative.java b/packages/services/PacProcessor/src/com/android/pacprocessor/PacNative.java index 1e8109cb393c..9c0cfc27bd14 100644 --- a/packages/services/PacProcessor/src/com/android/pacprocessor/PacNative.java +++ b/packages/services/PacProcessor/src/com/android/pacprocessor/PacNative.java @@ -20,7 +20,7 @@ import android.util.Log; /** * @hide */ -public class PacNative { +public class PacNative implements LibpacInterface { private static final String TAG = "PacProxy"; private static final PacNative sInstance = new PacNative(); @@ -49,34 +49,38 @@ public class PacNative { return sInstance; } + @Override public synchronized boolean startPacSupport() { if (createV8ParserNativeLocked()) { Log.e(TAG, "Unable to Create v8 Proxy Parser."); - return true; + return false; } mIsActive = true; - return false; + return true; } + @Override public synchronized boolean stopPacSupport() { if (mIsActive) { if (destroyV8ParserNativeLocked()) { Log.e(TAG, "Unable to Destroy v8 Proxy Parser."); - return true; + return false; } mIsActive = false; } - return false; + return true; } + @Override public synchronized boolean setCurrentProxyScript(String script) { if (setProxyScriptNativeLocked(script)) { Log.e(TAG, "Unable to parse proxy script."); - return true; + return false; } - return false; + return true; } + @Override public synchronized String makeProxyRequest(String url, String host) { String ret = makeProxyRequestNativeLocked(url, host); if ((ret == null) || (ret.length() == 0)) { diff --git a/packages/services/PacProcessor/src/com/android/pacprocessor/PacService.java b/packages/services/PacProcessor/src/com/android/pacprocessor/PacService.java index b006d6e1fa7b..7aea721617b9 100644 --- a/packages/services/PacProcessor/src/com/android/pacprocessor/PacService.java +++ b/packages/services/PacProcessor/src/com/android/pacprocessor/PacService.java @@ -17,6 +17,7 @@ package com.android.pacprocessor; import android.app.Service; import android.content.Intent; +import android.content.res.Resources; import android.os.Binder; import android.os.IBinder; import android.os.Process; @@ -30,19 +31,24 @@ import java.net.URL; public class PacService extends Service { private static final String TAG = "PacService"; + private static final boolean sUseWebViewPacProcessor = Resources.getSystem().getBoolean( + com.android.internal.R.bool.config_useWebViewPacProcessor); + + private final LibpacInterface mLibpac = sUseWebViewPacProcessor + ? PacWebView.getInstance() + : PacNative.getInstance(); - private PacNative mPacNative = PacNative.getInstance(); private ProxyServiceStub mStub = new ProxyServiceStub(); @Override public void onCreate() { super.onCreate(); - mPacNative.startPacSupport(); + mLibpac.startPacSupport(); } @Override public void onDestroy() { - mPacNative.stopPacSupport(); + mLibpac.stopPacSupport(); super.onDestroy(); } @@ -52,7 +58,6 @@ public class PacService extends Service { } private class ProxyServiceStub extends IProxyService.Stub { - @Override public String resolvePacFile(String host, String url) throws RemoteException { try { @@ -69,7 +74,7 @@ public class PacService extends Service { throw new IllegalArgumentException("Invalid host was passed"); } } - return mPacNative.makeProxyRequest(url, host); + return mLibpac.makeProxyRequest(url, host); } catch (MalformedURLException e) { throw new IllegalArgumentException("Invalid URL was passed"); } @@ -81,7 +86,7 @@ public class PacService extends Service { Log.e(TAG, "Only system user is allowed to call setPacFile"); throw new SecurityException(); } - mPacNative.setCurrentProxyScript(script); + mLibpac.setCurrentProxyScript(script); } @Override diff --git a/packages/services/PacProcessor/src/com/android/pacprocessor/PacWebView.java b/packages/services/PacProcessor/src/com/android/pacprocessor/PacWebView.java new file mode 100644 index 000000000000..89c466500fef --- /dev/null +++ b/packages/services/PacProcessor/src/com/android/pacprocessor/PacWebView.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.pacprocessor; + +import android.util.Log; +import android.webkit.PacProcessor; + +import com.android.internal.annotations.GuardedBy; + +/** + * @hide + */ +public class PacWebView implements LibpacInterface { + private static final String TAG = "PacWebView"; + + private static final PacWebView sInstance = new PacWebView(); + + private Object mLock = new Object(); + + @GuardedBy("mLock") + private PacProcessor mProcessor = PacProcessor.getInstance(); + + public static PacWebView getInstance() { + return sInstance; + } + + @Override + public boolean setCurrentProxyScript(String script) { + synchronized (mLock) { + if (!mProcessor.setProxyScript(script)) { + Log.e(TAG, "Unable to parse proxy script."); + return false; + } + return true; + } + } + + @Override + public String makeProxyRequest(String url, String host) { + synchronized (mLock) { + return mProcessor.findProxyForUrl(url); + } + } +} |