Home | History | Annotate | Download | only in webkit
      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 
     17 package android.webkit;
     18 
     19 import android.content.Context;
     20 import android.os.Handler;
     21 import android.os.Looper;
     22 import android.os.Message;
     23 import android.os.Process;
     24 import android.util.Log;
     25 
     26 abstract class WebSyncManager implements Runnable {
     27     // message code for sync message
     28     private static final int SYNC_MESSAGE = 101;
     29     // time delay in millisec for a sync (now) message
     30     private static int SYNC_NOW_INTERVAL = 100; // 100 millisec
     31     // time delay in millisec for a sync (later) message
     32     private static int SYNC_LATER_INTERVAL = 5 * 60 * 1000; // 5 minutes
     33     // thread for syncing
     34     private Thread mSyncThread;
     35     // Name of thread
     36     private String mThreadName;
     37     // handler of the sync thread
     38     protected Handler mHandler;
     39     // database for the persistent storage
     40     // Note that this remains uninitialised as it is unused. We cannot remove
     41     // the member as it leaked into the public API via CookieSyncManager.
     42     // TODO: hide this member, ditto for mHandler.
     43     protected WebViewDatabase mDataBase;
     44     // Ref count for calls to start/stop sync
     45     private int mStartSyncRefCount;
     46     // log tag
     47     protected static final String LOGTAG = "websync";
     48 
     49     private class SyncHandler extends Handler {
     50         @Override
     51         public void handleMessage(Message msg) {
     52             if (msg.what == SYNC_MESSAGE) {
     53                 if (DebugFlags.WEB_SYNC_MANAGER) {
     54                     Log.v(LOGTAG, "*** WebSyncManager sync ***");
     55                 }
     56                 syncFromRamToFlash();
     57 
     58                 // send a delayed message to request sync later
     59                 Message newmsg = obtainMessage(SYNC_MESSAGE);
     60                 sendMessageDelayed(newmsg, SYNC_LATER_INTERVAL);
     61             }
     62         }
     63     }
     64 
     65     protected WebSyncManager(Context context, String name) {
     66         mThreadName = name;
     67         if (context != null) {
     68             mSyncThread = new Thread(this);
     69             mSyncThread.setName(mThreadName);
     70             mSyncThread.start();
     71         } else {
     72             throw new IllegalStateException(
     73                     "WebSyncManager can't be created without context");
     74         }
     75     }
     76 
     77     protected Object clone() throws CloneNotSupportedException {
     78         throw new CloneNotSupportedException("doesn't implement Cloneable");
     79     }
     80 
     81     public void run() {
     82         // prepare Looper for sync handler
     83         Looper.prepare();
     84         mHandler = new SyncHandler();
     85         onSyncInit();
     86         // lower the priority after onSyncInit() is done
     87        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
     88 
     89         Message msg = mHandler.obtainMessage(SYNC_MESSAGE);
     90         mHandler.sendMessageDelayed(msg, SYNC_LATER_INTERVAL);
     91 
     92         Looper.loop();
     93     }
     94 
     95     /**
     96      * sync() forces sync manager to sync now
     97      */
     98     public void sync() {
     99         if (DebugFlags.WEB_SYNC_MANAGER) {
    100             Log.v(LOGTAG, "*** WebSyncManager sync ***");
    101         }
    102         if (mHandler == null) {
    103             return;
    104         }
    105         mHandler.removeMessages(SYNC_MESSAGE);
    106         Message msg = mHandler.obtainMessage(SYNC_MESSAGE);
    107         mHandler.sendMessageDelayed(msg, SYNC_NOW_INTERVAL);
    108     }
    109 
    110     /**
    111      * resetSync() resets sync manager's timer
    112      */
    113     public void resetSync() {
    114         if (DebugFlags.WEB_SYNC_MANAGER) {
    115             Log.v(LOGTAG, "*** WebSyncManager resetSync ***");
    116         }
    117         if (mHandler == null) {
    118             return;
    119         }
    120         mHandler.removeMessages(SYNC_MESSAGE);
    121         Message msg = mHandler.obtainMessage(SYNC_MESSAGE);
    122         mHandler.sendMessageDelayed(msg, SYNC_LATER_INTERVAL);
    123     }
    124 
    125     /**
    126      * startSync() requests sync manager to start sync
    127      */
    128     public void startSync() {
    129         if (DebugFlags.WEB_SYNC_MANAGER) {
    130             Log.v(LOGTAG, "***  WebSyncManager startSync ***, Ref count:" +
    131                     mStartSyncRefCount);
    132         }
    133         if (mHandler == null) {
    134             return;
    135         }
    136         if (++mStartSyncRefCount == 1) {
    137             Message msg = mHandler.obtainMessage(SYNC_MESSAGE);
    138             mHandler.sendMessageDelayed(msg, SYNC_LATER_INTERVAL);
    139         }
    140     }
    141 
    142     /**
    143      * stopSync() requests sync manager to stop sync. remove any SYNC_MESSAGE in
    144      * the queue to break the sync loop
    145      */
    146     public void stopSync() {
    147         if (DebugFlags.WEB_SYNC_MANAGER) {
    148             Log.v(LOGTAG, "*** WebSyncManager stopSync ***, Ref count:" +
    149                     mStartSyncRefCount);
    150         }
    151         if (mHandler == null) {
    152             return;
    153         }
    154         if (--mStartSyncRefCount == 0) {
    155             mHandler.removeMessages(SYNC_MESSAGE);
    156         }
    157     }
    158 
    159     protected void onSyncInit() {
    160     }
    161 
    162     abstract void syncFromRamToFlash();
    163 }
    164