Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f0f45d19 authored by Pavel Grafov's avatar Pavel Grafov Committed by Android (Google) Code Review
Browse files

Merge "Delay instantiation of PacProcessor until unlocked" into sc-v2-dev

parents 3aef7134 e5ab1670
Loading
Loading
Loading
Loading
+52 −5
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.os.Binder;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
import android.os.UserManager;
import android.util.Log;
import android.webkit.PacProcessor;

@@ -33,16 +34,44 @@ import java.net.URL;
public class PacService extends Service {
    private static final String TAG = "PacService";

    private Object mLock = new Object();
    private final Object mLock = new Object();

    // Webkit PacProcessor cannot be instantiated before the user is unlocked, so this field is
    // initialized lazily.
    @GuardedBy("mLock")
    private final PacProcessor mPacProcessor = PacProcessor.getInstance();
    private PacProcessor mPacProcessor;

    // Stores PAC script when setPacFile is called before mPacProcessor is available. In case the
    // script was already fed to the PacProcessor, it should be null.
    @GuardedBy("mLock")
    private String mPendingScript;

    private ProxyServiceStub mStub = new ProxyServiceStub();

    @Override
    public void onCreate() {
        super.onCreate();

        synchronized (mLock) {
            checkPacProcessorLocked();
        }
    }

    /**
     * Initializes PacProcessor if it hasn't been initialized yet and if the system user is
     * unlocked, e.g. after the user has entered their PIN after a reboot.
     * Returns whether PacProcessor is available.
     */
    private boolean checkPacProcessorLocked() {
        if (mPacProcessor != null) {
            return true;
        }
        UserManager um = getSystemService(UserManager.class);
        if (um.isUserUnlocked()) {
            mPacProcessor = PacProcessor.getInstance();
            return true;
        }
        return false;
    }

    @Override
@@ -74,7 +103,20 @@ public class PacService extends Service {
                }

                synchronized (mLock) {
                    if (checkPacProcessorLocked()) {
                        // Apply pending script in case it was set before processor was ready.
                        if (mPendingScript != null) {
                            if (!mPacProcessor.setProxyScript(mPendingScript)) {
                                Log.e(TAG, "Unable to parse proxy script.");
                            }
                            mPendingScript = null;
                        }
                        return mPacProcessor.findProxyForUrl(url);
                    } else {
                        Log.e(TAG, "PacProcessor isn't ready during early boot,"
                                + " request will be direct");
                        return null;
                    }
                }
            } catch (MalformedURLException e) {
                throw new IllegalArgumentException("Invalid URL was passed");
@@ -88,9 +130,14 @@ public class PacService extends Service {
                throw new SecurityException();
            }
            synchronized (mLock) {
                if (checkPacProcessorLocked()) {
                    if (!mPacProcessor.setProxyScript(script)) {
                        Log.e(TAG, "Unable to parse proxy script.");
                    }
                } else {
                    Log.d(TAG, "PAC processor isn't ready, saving script for later.");
                    mPendingScript = script;
                }
            }
        }
    }