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

Commit f1d09761 authored by Pavel Grafov's avatar Pavel Grafov Committed by Gerrit Code Review
Browse files

Merge "Delay instantiation of PacProcessor until unlocked"

parents ef5365a7 3f0400e2
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;
                }
            }
        }
    }