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 * The CookieSyncManager is used to synchronize the browser cookie store
25 * between RAM and permanent storage. To get the best performance, browser cookies are
26 * saved in RAM. A separate thread saves the cookies between, driven by a timer.
27 * <p>
28 *
29 * To use the CookieSyncManager, the host application has to call the following
30 * when the application starts:
31 * <p>
32 *
33 * <pre class="prettyprint">CookieSyncManager.createInstance(context)</pre><p>
34 *
35 * To set up for sync, the host application has to call<p>
36 * <pre class="prettyprint">CookieSyncManager.getInstance().startSync()</pre><p>
37 *
38 * in Activity.onResume(), and call
39 * <p>
40 *
41 * <pre class="prettyprint">
42 * CookieSyncManager.getInstance().stopSync()
43 * </pre><p>
44 *
45 * in Activity.onPause().<p>
46 *
47 * To get instant sync instead of waiting for the timer to trigger, the host can
48 * call
49 * <p>
50 * <pre class="prettyprint">CookieSyncManager.getInstance().sync()</pre><p>
51 *
52 * The sync interval is 5 minutes, so you will want to force syncs
53 * manually anyway, for instance in {@link
54 * WebViewClient#onPageFinished}. Note that even sync() happens
55 * asynchronously, so don't do it just as your activity is shutting
56 * down.
57 */
58public final class CookieSyncManager extends WebSyncManager {
59
60    private static CookieSyncManager sRef;
61
62    private CookieSyncManager(Context context) {
63        super(context, "CookieSyncManager");
64    }
65
66    /**
67     * Singleton access to a {@link CookieSyncManager}. An
68     * IllegalStateException will be thrown if
69     * {@link CookieSyncManager#createInstance(Context)} is not called before.
70     *
71     * @return CookieSyncManager
72     */
73    public static synchronized CookieSyncManager getInstance() {
74        checkInstanceIsCreated();
75        return sRef;
76    }
77
78    /**
79     * Create a singleton CookieSyncManager within a context
80     * @param context
81     * @return CookieSyncManager
82     */
83    public static synchronized CookieSyncManager createInstance(
84            Context context) {
85        if (context == null) {
86            throw new IllegalArgumentException("Invalid context argument");
87        }
88
89        if (sRef == null) {
90            sRef = new CookieSyncManager(context);
91        }
92        return sRef;
93    }
94
95    protected void syncFromRamToFlash() {
96        if (DebugFlags.COOKIE_SYNC_MANAGER) {
97            Log.v(LOGTAG, "CookieSyncManager::syncFromRamToFlash STARTS");
98        }
99
100        CookieManager manager = CookieManager.getInstance();
101
102        if (!manager.acceptCookie()) {
103            return;
104        }
105
106        manager.flushCookieStore();
107
108        if (DebugFlags.COOKIE_SYNC_MANAGER) {
109            Log.v(LOGTAG, "CookieSyncManager::syncFromRamToFlash DONE");
110        }
111    }
112
113    private static void checkInstanceIsCreated() {
114        if (sRef == null) {
115            throw new IllegalStateException(
116                    "CookieSyncManager::createInstance() needs to be called "
117                            + "before CookieSyncManager::getInstance()");
118        }
119    }
120}
121