CookieSyncManager.java revision f2bfe2bc05322a754c5a5d4e16ca182303ca72aa
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        JniUtil.setContext(context);
90        Context appContext = context.getApplicationContext();
91        if (sRef == null) {
92            sRef = new CookieSyncManager(appContext);
93        }
94        return sRef;
95    }
96
97    protected void syncFromRamToFlash() {
98        if (DebugFlags.COOKIE_SYNC_MANAGER) {
99            Log.v(LOGTAG, "CookieSyncManager::syncFromRamToFlash STARTS");
100        }
101
102        CookieManager manager = CookieManager.getInstance();
103
104        if (!manager.acceptCookie()) {
105            return;
106        }
107
108        manager.flushCookieStore();
109
110        if (DebugFlags.COOKIE_SYNC_MANAGER) {
111            Log.v(LOGTAG, "CookieSyncManager::syncFromRamToFlash DONE");
112        }
113    }
114
115    private static void checkInstanceIsCreated() {
116        if (sRef == null) {
117            throw new IllegalStateException(
118                    "CookieSyncManager::createInstance() needs to be called "
119                            + "before CookieSyncManager::getInstance()");
120        }
121    }
122}
123