CookieSyncManager.java revision a70d1d99de6cdadd68176cb849c02d56b8536021
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.webkit;
18
19import android.content.Context;
20import android.util.Log;
21
22
23/**
24 * @deprecated The WebView now automatically syncs cookies as necessary.
25 *             You no longer need to create or use the CookieSyncManager.
26 *             To manually force a sync you can use the CookieManager
27 *             method {@link CookieManager#flush} which is synchronous
28 *             replacement for {@link #sync}.
29 *             <p>
30 *
31 * The CookieSyncManager is used to synchronize the browser cookie store
32 * between RAM and permanent storage. To get the best performance, browser cookies are
33 * saved in RAM. A separate thread saves the cookies between, driven by a timer.
34 * <p>
35 *
36 * To use the CookieSyncManager, the host application has to call the following
37 * when the application starts:
38 * <p>
39 *
40 * <pre class="prettyprint">CookieSyncManager.createInstance(context)</pre><p>
41 *
42 * To set up for sync, the host application has to call<p>
43 * <pre class="prettyprint">CookieSyncManager.getInstance().startSync()</pre><p>
44 *
45 * in Activity.onResume(), and call
46 * <p>
47 *
48 * <pre class="prettyprint">
49 * CookieSyncManager.getInstance().stopSync()
50 * </pre><p>
51 *
52 * in Activity.onPause().<p>
53 *
54 * To get instant sync instead of waiting for the timer to trigger, the host can
55 * call
56 * <p>
57 * <pre class="prettyprint">CookieSyncManager.getInstance().sync()</pre><p>
58 *
59 * The sync interval is 5 minutes, so you will want to force syncs
60 * manually anyway, for instance in {@link
61 * WebViewClient#onPageFinished}. Note that even sync() happens
62 * asynchronously, so don't do it just as your activity is shutting
63 * down.
64 */
65@Deprecated
66public final class CookieSyncManager extends WebSyncManager {
67
68    private static CookieSyncManager sRef;
69    private static boolean sGetInstanceAllowed = false;
70
71    private CookieSyncManager() {
72        super(null, null);
73    }
74
75    /**
76     * Singleton access to a {@link CookieSyncManager}. An
77     * IllegalStateException will be thrown if
78     * {@link CookieSyncManager#createInstance(Context)} is not called before.
79     *
80     * @return CookieSyncManager
81     */
82    public static synchronized CookieSyncManager getInstance() {
83        checkInstanceIsAllowed();
84        if (sRef == null) {
85            sRef = new CookieSyncManager();
86        }
87        return sRef;
88    }
89
90    /**
91     * Create a singleton CookieSyncManager within a context
92     * @param context
93     * @return CookieSyncManager
94     */
95    public static synchronized CookieSyncManager createInstance(Context context) {
96        if (context == null) {
97            throw new IllegalArgumentException("Invalid context argument");
98        }
99        setGetInstanceIsAllowed();
100        return getInstance();
101    }
102
103    /**
104     * sync() forces sync manager to sync now
105     * @deprecated Use {@link CookieManager#flush} instead.
106     */
107    @Deprecated
108    public void sync() {
109        CookieManager.getInstance().flush();
110    }
111
112    /**
113     * @deprecated Use {@link CookieManager#flush} instead.
114     */
115    @Deprecated
116    protected void syncFromRamToFlash() {
117        CookieManager.getInstance().flush();
118    }
119
120    /**
121     * resetSync() resets sync manager's timer.
122     * @deprecated Calling resetSync is no longer necessary as the WebView automatically
123     *             syncs cookies.
124     */
125    @Deprecated
126    public void resetSync() {
127    }
128
129    /**
130     * startSync() requests sync manager to start sync.
131     * @deprecated Calling startSync is no longer necessary as the WebView automatically
132     *             syncs cookies.
133     */
134    @Deprecated
135    public void startSync() {
136    }
137
138    /**
139     * stopSync() requests sync manager to stop sync. remove any SYNC_MESSAGE in
140     * the queue to break the sync loop
141     * @deprecated Calling stopSync is no longer useful as the WebView
142     *             automatically syncs cookies.
143     */
144    @Deprecated
145    public void stopSync() {
146    }
147
148    static void setGetInstanceIsAllowed() {
149        sGetInstanceAllowed = true;
150    }
151
152    private static void checkInstanceIsAllowed() {
153        // Prior to Android KK, calling createInstance() or constructing a WebView is
154        // a hard pre-condition for calling getInstance(). We retain that contract to aid
155        // developers targeting a range of SDK levels.
156        if (!sGetInstanceAllowed) {
157            throw new IllegalStateException(
158                    "CookieSyncManager::createInstance() needs to be called "
159                            + "before CookieSyncManager::getInstance()");
160        }
161    }
162}
163