FileDocCategorySizeDatePackage
CookieSyncManager.javaAPI DocAndroid 1.5 API6237Wed May 06 22:41:56 BST 2009android.webkit

CookieSyncManager

public final class CookieSyncManager extends WebSyncManager
The class CookieSyncManager is used to synchronize the browser cookies between RAM and FLASH. To get the best performance, browser cookie is saved in RAM. We use a separate thread to sync the cookies between RAM and FLASH on a timer base.

To use the CookieSyncManager, the host application has to call the following when the application starts.

CookieSyncManager.createInstance(context)

To set up for sync, the host application has to call

CookieSyncManager.getInstance().startSync()

in its Activity.onResume(), and call

CookieSyncManager.getInstance().stopSync()

in its Activity.onStop().

To get instant sync instead of waiting for the timer to trigger, the host can call

CookieSyncManager.getInstance().sync()

Fields Summary
private static CookieSyncManager
sRef
private long
mLastUpdate
Constructors Summary
private CookieSyncManager(android.content.Context context)

        super(context, "CookieSyncManager");
    
Methods Summary
voidclearAllCookies()
Package level api, called from CookieManager Clear all cookies in the database

        // null mDataBase implies that the host application doesn't support
        // persistent cookie.
        if (mDataBase == null) {
            return;
        }

        mDataBase.clearCookies();
    
voidclearExpiredCookies(long now)
Package level api, called from CookieManager Clear all expired cookies in the database

        // null mDataBase implies that the host application doesn't support
        // persistent cookie.
        if (mDataBase == null) {
            return;
        }

        mDataBase.clearExpiredCookies(now);
    
voidclearSessionCookies()
Package level api, called from CookieManager Clear all session cookies in the database

        // null mDataBase implies that the host application doesn't support
        // persistent cookie.
        if (mDataBase == null) {
            return;
        }

        mDataBase.clearSessionCookies();
    
public static synchronized android.webkit.CookieSyncManagercreateInstance(android.content.Context context)
Create a singleton CookieSyncManager within a context

param
context
return
CookieSyncManager

        if (sRef == null) {
            sRef = new CookieSyncManager(context);
        }
        return sRef;
    
java.util.ArrayListgetCookiesForDomain(java.lang.String domain)
Package level api, called from CookieManager Get all the cookies which matches a given base domain.

param
domain
return
A list of Cookie

        // null mDataBase implies that the host application doesn't support
        // persistent cookie. No sync needed.
        if (mDataBase == null) {
            return new ArrayList<Cookie>();
        }

        return mDataBase.getCookiesForDomain(domain);
    
public static synchronized android.webkit.CookieSyncManagergetInstance()
Singleton access to a {@link CookieSyncManager}. An IllegalStateException will be thrown if {@link CookieSyncManager#createInstance(Context)} is not called before.

return
CookieSyncManager

        if (sRef == null) {
            throw new IllegalStateException(
                    "CookieSyncManager::createInstance() needs to be called "
                            + "before CookieSyncManager::getInstance()");
        }
        return sRef;
    
booleanhasCookies()
Returns true if there are any saved cookies.

        // null mDataBase implies that the host application doesn't support
        // persistent cookie.
        if (mDataBase == null) {
            return false;
        }

        return mDataBase.hasCookies();
    
private voidsyncFromRamToFlash(java.util.ArrayList list)

        Iterator<Cookie> iter = list.iterator();
        while (iter.hasNext()) {
            Cookie cookie = iter.next();
            if (cookie.mode != Cookie.MODE_NORMAL) {
                if (cookie.mode != Cookie.MODE_NEW) {
                    mDataBase.deleteCookies(cookie.domain, cookie.path,
                            cookie.name);
                }
                if (cookie.mode != Cookie.MODE_DELETED) {
                    mDataBase.addCookie(cookie);
                    CookieManager.getInstance().syncedACookie(cookie);
                } else {
                    CookieManager.getInstance().deleteACookie(cookie);
                }
            }
        }
    
protected voidsyncFromRamToFlash()

        if (Config.LOGV) {
            Log.v(LOGTAG, "CookieSyncManager::syncFromRamToFlash STARTS");
        }

        if (!CookieManager.getInstance().acceptCookie()) {
            return;
        }

        ArrayList<Cookie> cookieList = CookieManager.getInstance()
                .getUpdatedCookiesSince(mLastUpdate);
        mLastUpdate = System.currentTimeMillis();
        syncFromRamToFlash(cookieList);

        ArrayList<Cookie> lruList =
                CookieManager.getInstance().deleteLRUDomain();
        syncFromRamToFlash(lruList);

        if (Config.LOGV) {
            Log.v(LOGTAG, "CookieSyncManager::syncFromRamToFlash DONE");
        }