private static CookieSyncManager sRef;
- private CookieSyncManager(Context context) {
- super(context, "CookieSyncManager");
+ private static boolean sGetInstanceAllowed = false;
+
+ private CookieSyncManager() {
+ super("CookieSyncManager");
}
/**
* @return CookieSyncManager
*/
public static synchronized CookieSyncManager getInstance() {
- checkInstanceIsCreated();
+ checkInstanceIsAllowed();
+ if (sRef == null) {
+ sRef = new CookieSyncManager();
+ }
return sRef;
}
* @param context
* @return CookieSyncManager
*/
- public static synchronized CookieSyncManager createInstance(
- Context context) {
+ public static synchronized CookieSyncManager createInstance(Context context) {
if (context == null) {
throw new IllegalArgumentException("Invalid context argument");
}
- if (sRef == null) {
- sRef = new CookieSyncManager(context);
- }
- return sRef;
+ setGetInstanceIsAllowed();
+ return getInstance();
}
protected void syncFromRamToFlash() {
}
}
- private static void checkInstanceIsCreated() {
- if (sRef == null) {
+ static void setGetInstanceIsAllowed() {
+ sGetInstanceAllowed = true;
+ }
+
+ private static void checkInstanceIsAllowed() {
+ // Prior to Android KK, calling createInstance() or constructing a WebView is
+ // a hard pre-condition for calling getInstance(). We retain that contract to aid
+ // developers targeting a range of SDK levels.
+ if (!sGetInstanceAllowed) {
throw new IllegalStateException(
"CookieSyncManager::createInstance() needs to be called "
+ "before CookieSyncManager::getInstance()");
private String mThreadName;
// handler of the sync thread
protected Handler mHandler;
- // database for the persistent storage
+ // database for the persistent storage. Always null.
protected WebViewDatabase mDataBase;
// Ref count for calls to start/stop sync
private int mStartSyncRefCount;
}
protected WebSyncManager(Context context, String name) {
+ this(name);
+ }
+
+ /** @hide */
+ WebSyncManager(String name) {
mThreadName = name;
- if (context != null) {
- mDataBase = WebViewDatabase.getInstance(context);
- mSyncThread = new Thread(this);
- mSyncThread.setName(mThreadName);
- mSyncThread.start();
- } else {
- throw new IllegalStateException(
- "WebSyncManager can't be created without context");
- }
+ mSyncThread = new Thread(this);
+ mSyncThread.setName(mThreadName);
+ mSyncThread.start();
}
protected Object clone() throws CloneNotSupportedException {
mHandler = new SyncHandler();
onSyncInit();
// lower the priority after onSyncInit() is done
- Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
+ Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
Message msg = mHandler.obtainMessage(SYNC_MESSAGE);
mHandler.sendMessageDelayed(msg, SYNC_LATER_INTERVAL);
ensureProviderCreated();
mProvider.init(javaScriptInterfaces, privateBrowsing);
- // Post condition of creating a webview is the CookieSyncManager instance exists.
- CookieSyncManager.createInstance(getContext());
+ // Post condition of creating a webview is the CookieSyncManager.getInstance() is allowed.
+ CookieSyncManager.setGetInstanceIsAllowed();
}
/**