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

Commit a70d1d99 authored by Hector Dearman's avatar Hector Dearman
Browse files

Deprecate the Cookie Sync Manager

The CookieSyncManager is largely useless as the WebView
automatically syncs cookies every 30s (whether you like it
or not). The one method which needs to be saved is sync,
there is no other way to force a sync without this.
We move sync to the CookieManager and rename it to flush
for greater consistency.

Bug: 11060034
Change-Id: I8a14998020eea54f196fc6ed845b09ed69cfd447
parent c7400b0c
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -35245,6 +35245,7 @@ package android.webkit {
    method public synchronized boolean acceptCookie();
    method public synchronized boolean acceptThirdPartyCookies(android.webkit.WebView);
    method public static boolean allowFileSchemeCookies();
    method public void flush();
    method public java.lang.String getCookie(java.lang.String);
    method public static synchronized android.webkit.CookieManager getInstance();
    method public synchronized boolean hasCookies();
@@ -35260,10 +35261,10 @@ package android.webkit {
    method public void setCookie(java.lang.String, java.lang.String, android.webkit.ValueCallback<java.lang.Boolean>);
  }
  public final class CookieSyncManager extends android.webkit.WebSyncManager {
  public final deprecated class CookieSyncManager extends android.webkit.WebSyncManager {
    method public static synchronized android.webkit.CookieSyncManager createInstance(android.content.Context);
    method public static synchronized android.webkit.CookieSyncManager getInstance();
    method protected void syncFromRamToFlash();
    method protected deprecated void syncFromRamToFlash();
    field protected static final java.lang.String LOGTAG = "websync";
    field protected android.webkit.WebViewDatabase mDataBase;
    field protected android.os.Handler mHandler;
@@ -35616,7 +35617,7 @@ package android.webkit {
    method public abstract void updateQuota(long);
  }
   abstract class WebSyncManager implements java.lang.Runnable {
   abstract deprecated class WebSyncManager implements java.lang.Runnable {
    ctor protected WebSyncManager(android.content.Context, java.lang.String);
    method protected void onSyncInit();
    method public void resetSync();
+10 −2
Original line number Diff line number Diff line
@@ -251,13 +251,21 @@ public class CookieManager {
        throw new MustOverrideException();
    }

    /**
     * Ensures all cookies currently accessible through the getCookie API are
     * written to persistent storage.
     * This call will block the caller until it is done and may perform I/O.
     */
    public void flush() {
        flushCookieStore();
    }

    /**
     * Flushes all cookies managed by the Chrome HTTP stack to flash.
     *
     * @hide Package level api, called from CookieSyncManager
     */
    protected void flushCookieStore() {
        throw new MustOverrideException();
    }

    /**
+49 −16
Original line number Diff line number Diff line
@@ -21,6 +21,13 @@ import android.util.Log;


/**
 * @deprecated The WebView now automatically syncs cookies as necessary.
 *             You no longer need to create or use the CookieSyncManager.
 *             To manually force a sync you can use the CookieManager
 *             method {@link CookieManager#flush} which is synchronous
 *             replacement for {@link #sync}.
 *             <p>
 *
 * The CookieSyncManager is used to synchronize the browser cookie store
 * between RAM and permanent storage. To get the best performance, browser cookies are
 * saved in RAM. A separate thread saves the cookies between, driven by a timer.
@@ -55,14 +62,14 @@ import android.util.Log;
 * asynchronously, so don't do it just as your activity is shutting
 * down.
 */
@Deprecated
public final class CookieSyncManager extends WebSyncManager {

    private static CookieSyncManager sRef;

    private static boolean sGetInstanceAllowed = false;

    private CookieSyncManager() {
        super("CookieSyncManager");
        super(null, null);
    }

    /**
@@ -89,27 +96,53 @@ public final class CookieSyncManager extends WebSyncManager {
        if (context == null) {
            throw new IllegalArgumentException("Invalid context argument");
        }

        setGetInstanceIsAllowed();
        return getInstance();
    }

    protected void syncFromRamToFlash() {
        if (DebugFlags.COOKIE_SYNC_MANAGER) {
            Log.v(LOGTAG, "CookieSyncManager::syncFromRamToFlash STARTS");
    /**
     * sync() forces sync manager to sync now
     * @deprecated Use {@link CookieManager#flush} instead.
     */
    @Deprecated
    public void sync() {
        CookieManager.getInstance().flush();
    }

        CookieManager manager = CookieManager.getInstance();

        if (!manager.acceptCookie()) {
            return;
    /**
     * @deprecated Use {@link CookieManager#flush} instead.
     */
    @Deprecated
    protected void syncFromRamToFlash() {
        CookieManager.getInstance().flush();
    }

        manager.flushCookieStore();
    /**
     * resetSync() resets sync manager's timer.
     * @deprecated Calling resetSync is no longer necessary as the WebView automatically
     *             syncs cookies.
     */
    @Deprecated
    public void resetSync() {
    }

        if (DebugFlags.COOKIE_SYNC_MANAGER) {
            Log.v(LOGTAG, "CookieSyncManager::syncFromRamToFlash DONE");
    /**
     * startSync() requests sync manager to start sync.
     * @deprecated Calling startSync is no longer necessary as the WebView automatically
     *             syncs cookies.
     */
    @Deprecated
    public void startSync() {
    }

    /**
     * stopSync() requests sync manager to stop sync. remove any SYNC_MESSAGE in
     * the queue to break the sync loop
     * @deprecated Calling stopSync is no longer useful as the WebView
     *             automatically syncs cookies.
     */
    @Deprecated
    public void stopSync() {
    }

    static void setGetInstanceIsAllowed() {
+7 −93
Original line number Diff line number Diff line
@@ -23,52 +23,16 @@ import android.os.Message;
import android.os.Process;
import android.util.Log;

/*
 * @deprecated The WebSyncManager no longer does anything.
 */
@Deprecated
abstract class WebSyncManager implements Runnable {
    // message code for sync message
    private static final int SYNC_MESSAGE = 101;
    // time delay in millisec for a sync (now) message
    private static int SYNC_NOW_INTERVAL = 100; // 100 millisec
    // time delay in millisec for a sync (later) message
    private static int SYNC_LATER_INTERVAL = 5 * 60 * 1000; // 5 minutes
    // thread for syncing
    private Thread mSyncThread;
    // Name of thread
    private String mThreadName;
    // handler of the sync thread
    protected Handler mHandler;
    // database for the persistent storage. Always null.
    protected WebViewDatabase mDataBase;
    // Ref count for calls to start/stop sync
    private int mStartSyncRefCount;
    // log tag
    protected static final String LOGTAG = "websync";

    private class SyncHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == SYNC_MESSAGE) {
                if (DebugFlags.WEB_SYNC_MANAGER) {
                    Log.v(LOGTAG, "*** WebSyncManager sync ***");
                }
                syncFromRamToFlash();

                // send a delayed message to request sync later
                Message newmsg = obtainMessage(SYNC_MESSAGE);
                sendMessageDelayed(newmsg, SYNC_LATER_INTERVAL);
            }
        }
    }
    protected static final java.lang.String LOGTAG = "websync";
    protected android.webkit.WebViewDatabase mDataBase;
    protected android.os.Handler mHandler;

    protected WebSyncManager(Context context, String name) {
        this(name);
    }

    /** @hide */
    WebSyncManager(String name) {
        mThreadName = name;
        mSyncThread = new Thread(this);
        mSyncThread.setName(mThreadName);
        mSyncThread.start();
    }

    protected Object clone() throws CloneNotSupportedException {
@@ -76,64 +40,24 @@ abstract class WebSyncManager implements Runnable {
    }

    public void run() {
        // prepare Looper for sync handler
        Looper.prepare();
        mHandler = new SyncHandler();
        onSyncInit();
        // lower the priority after onSyncInit() is done
        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

        Message msg = mHandler.obtainMessage(SYNC_MESSAGE);
        mHandler.sendMessageDelayed(msg, SYNC_LATER_INTERVAL);

        Looper.loop();
    }

    /**
     * sync() forces sync manager to sync now
     */
    public void sync() {
        if (DebugFlags.WEB_SYNC_MANAGER) {
            Log.v(LOGTAG, "*** WebSyncManager sync ***");
        }
        if (mHandler == null) {
            return;
        }
        mHandler.removeMessages(SYNC_MESSAGE);
        Message msg = mHandler.obtainMessage(SYNC_MESSAGE);
        mHandler.sendMessageDelayed(msg, SYNC_NOW_INTERVAL);
    }

    /**
     * resetSync() resets sync manager's timer
     */
    public void resetSync() {
        if (DebugFlags.WEB_SYNC_MANAGER) {
            Log.v(LOGTAG, "*** WebSyncManager resetSync ***");
        }
        if (mHandler == null) {
            return;
        }
        mHandler.removeMessages(SYNC_MESSAGE);
        Message msg = mHandler.obtainMessage(SYNC_MESSAGE);
        mHandler.sendMessageDelayed(msg, SYNC_LATER_INTERVAL);
    }

    /**
     * startSync() requests sync manager to start sync
     */
    public void startSync() {
        if (DebugFlags.WEB_SYNC_MANAGER) {
            Log.v(LOGTAG, "***  WebSyncManager startSync ***, Ref count:" + 
                    mStartSyncRefCount);
        }
        if (mHandler == null) {
            return;
        }
        if (++mStartSyncRefCount == 1) {
            Message msg = mHandler.obtainMessage(SYNC_MESSAGE);
            mHandler.sendMessageDelayed(msg, SYNC_LATER_INTERVAL);
        }
    }

    /**
@@ -141,16 +65,6 @@ abstract class WebSyncManager implements Runnable {
     * the queue to break the sync loop
     */
    public void stopSync() {
        if (DebugFlags.WEB_SYNC_MANAGER) {
            Log.v(LOGTAG, "*** WebSyncManager stopSync ***, Ref count:" + 
                    mStartSyncRefCount);
        }
        if (mHandler == null) {
            return;
        }
        if (--mStartSyncRefCount == 0) {
            mHandler.removeMessages(SYNC_MESSAGE);
        }
    }

    protected void onSyncInit() {