Seperate interface and implementation of 4 WebView classes

GeolocationPermissionsClassic
CookieManagerClassic
WebIconDatabaseClassic
WebStorageClassic

Also creats a WebViewFactory top level class - this remains hidden
for now, as it's currently only used implicitly by the other
public WebView classes to create the provider instances.

Bug: 5626244

Change-Id: Id0ca1c16d8058f31a86414bbc0e8a55db4b907ba
diff --git a/core/java/android/webkit/WebStorage.java b/core/java/android/webkit/WebStorage.java
index 041791b..c46d161 100644
--- a/core/java/android/webkit/WebStorage.java
+++ b/core/java/android/webkit/WebStorage.java
@@ -16,13 +16,7 @@
 
 package android.webkit;
 
-import android.os.Handler;
-import android.os.Message;
-
-import java.util.Collection;
-import java.util.HashMap;
 import java.util.Map;
-import java.util.Set;
 
 /**
  * This class is used to manage the JavaScript storage APIs provided by the
@@ -56,34 +50,6 @@
         public void updateQuota(long newQuota);
     };
 
-    // Global instance of a WebStorage
-    private static WebStorage sWebStorage;
-
-    // Message ids
-    static final int UPDATE = 0;
-    static final int SET_QUOTA_ORIGIN = 1;
-    static final int DELETE_ORIGIN = 2;
-    static final int DELETE_ALL = 3;
-    static final int GET_ORIGINS = 4;
-    static final int GET_USAGE_ORIGIN = 5;
-    static final int GET_QUOTA_ORIGIN = 6;
-
-    // Message ids on the UI thread
-    static final int RETURN_ORIGINS = 0;
-    static final int RETURN_USAGE_ORIGIN = 1;
-    static final int RETURN_QUOTA_ORIGIN = 2;
-
-    private static final String ORIGINS = "origins";
-    private static final String ORIGIN = "origin";
-    private static final String CALLBACK = "callback";
-    private static final String USAGE = "usage";
-    private static final String QUOTA = "quota";
-
-    private Map <String, Origin> mOrigins;
-
-    private Handler mHandler = null;
-    private Handler mUIHandler = null;
-
     /**
      * This class encapsulates information about the amount of storage
      * currently used by an origin for the JavaScript storage APIs.
@@ -94,18 +60,21 @@
         private long mQuota = 0;
         private long mUsage = 0;
 
-        private Origin(String origin, long quota, long usage) {
+        /** @hide */
+        protected Origin(String origin, long quota, long usage) {
             mOrigin = origin;
             mQuota = quota;
             mUsage = usage;
         }
 
-        private Origin(String origin, long quota) {
+        /** @hide */
+        protected Origin(String origin, long quota) {
             mOrigin = origin;
             mQuota = quota;
         }
 
-        private Origin(String origin) {
+        /** @hide */
+        protected Origin(String origin) {
             mOrigin = origin;
         }
 
@@ -142,114 +111,6 @@
         }
     }
 
-    /**
-     * Message handler, UI side
-     * @hide
-     */
-    public void createUIHandler() {
-        if (mUIHandler == null) {
-            mUIHandler = new Handler() {
-                @Override
-                public void handleMessage(Message msg) {
-                    switch (msg.what) {
-                        case RETURN_ORIGINS: {
-                            Map values = (Map) msg.obj;
-                            Map origins = (Map) values.get(ORIGINS);
-                            ValueCallback<Map> callback = (ValueCallback<Map>) values.get(CALLBACK);
-                            callback.onReceiveValue(origins);
-                            } break;
-
-                        case RETURN_USAGE_ORIGIN: {
-                            Map values = (Map) msg.obj;
-                            ValueCallback<Long> callback = (ValueCallback<Long>) values.get(CALLBACK);
-                            callback.onReceiveValue((Long)values.get(USAGE));
-                            } break;
-
-                        case RETURN_QUOTA_ORIGIN: {
-                            Map values = (Map) msg.obj;
-                            ValueCallback<Long> callback = (ValueCallback<Long>) values.get(CALLBACK);
-                            callback.onReceiveValue((Long)values.get(QUOTA));
-                            } break;
-                    }
-                }
-            };
-        }
-    }
-
-    /**
-     * Message handler, WebCore side
-     * @hide
-     */
-    public synchronized void createHandler() {
-        if (mHandler == null) {
-            mHandler = new Handler() {
-                @Override
-                public void handleMessage(Message msg) {
-                    switch (msg.what) {
-                        case SET_QUOTA_ORIGIN: {
-                            Origin website = (Origin) msg.obj;
-                            nativeSetQuotaForOrigin(website.getOrigin(),
-                                                    website.getQuota());
-                            } break;
-
-                        case DELETE_ORIGIN: {
-                            Origin website = (Origin) msg.obj;
-                            nativeDeleteOrigin(website.getOrigin());
-                            } break;
-
-                        case DELETE_ALL:
-                            nativeDeleteAllData();
-                            break;
-
-                        case GET_ORIGINS: {
-                            syncValues();
-                            ValueCallback callback = (ValueCallback) msg.obj;
-                            Map origins = new HashMap(mOrigins);
-                            Map values = new HashMap<String, Object>();
-                            values.put(CALLBACK, callback);
-                            values.put(ORIGINS, origins);
-                            postUIMessage(Message.obtain(null, RETURN_ORIGINS, values));
-                            } break;
-
-                        case GET_USAGE_ORIGIN: {
-                            syncValues();
-                            Map values = (Map) msg.obj;
-                            String origin = (String) values.get(ORIGIN);
-                            ValueCallback callback = (ValueCallback) values.get(CALLBACK);
-                            Origin website = mOrigins.get(origin);
-                            Map retValues = new HashMap<String, Object>();
-                            retValues.put(CALLBACK, callback);
-                            if (website != null) {
-                                long usage = website.getUsage();
-                                retValues.put(USAGE, new Long(usage));
-                            }
-                            postUIMessage(Message.obtain(null, RETURN_USAGE_ORIGIN, retValues));
-                            } break;
-
-                        case GET_QUOTA_ORIGIN: {
-                            syncValues();
-                            Map values = (Map) msg.obj;
-                            String origin = (String) values.get(ORIGIN);
-                            ValueCallback callback = (ValueCallback) values.get(CALLBACK);
-                            Origin website = mOrigins.get(origin);
-                            Map retValues = new HashMap<String, Object>();
-                            retValues.put(CALLBACK, callback);
-                            if (website != null) {
-                                long quota = website.getQuota();
-                                retValues.put(QUOTA, new Long(quota));
-                            }
-                            postUIMessage(Message.obtain(null, RETURN_QUOTA_ORIGIN, retValues));
-                            } break;
-
-                        case UPDATE:
-                            syncValues();
-                            break;
-                    }
-                }
-            };
-        }
-    }
-
     /*
      * When calling getOrigins(), getUsageForOrigin() and getQuotaForOrigin(),
      * we need to get the values from WebCore, but we cannot block while doing so
@@ -270,26 +131,7 @@
      * representation of the origin to a {@link WebStorage.Origin} object.
      */
     public void getOrigins(ValueCallback<Map> callback) {
-        if (callback != null) {
-            if (WebViewCore.THREAD_NAME.equals(Thread.currentThread().getName())) {
-                syncValues();
-                callback.onReceiveValue(mOrigins);
-            } else {
-                postMessage(Message.obtain(null, GET_ORIGINS, callback));
-            }
-        }
-    }
-
-    /**
-     * Returns a list of origins having a database
-     * should only be called from WebViewCore.
-     */
-    Collection<Origin> getOriginsSync() {
-        if (WebViewCore.THREAD_NAME.equals(Thread.currentThread().getName())) {
-            update();
-            return mOrigins.values();
-        }
-        return null;
+        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
     }
 
     /**
@@ -300,23 +142,7 @@
      * a {@link ValueCallback}.
      */
     public void getUsageForOrigin(String origin, ValueCallback<Long> callback) {
-        if (callback == null) {
-            return;
-        }
-        if (origin == null) {
-            callback.onReceiveValue(null);
-            return;
-        }
-        if (WebViewCore.THREAD_NAME.equals(Thread.currentThread().getName())) {
-            syncValues();
-            Origin website = mOrigins.get(origin);
-            callback.onReceiveValue(new Long(website.getUsage()));
-        } else {
-            HashMap values = new HashMap<String, Object>();
-            values.put(ORIGIN, origin);
-            values.put(CALLBACK, callback);
-            postMessage(Message.obtain(null, GET_USAGE_ORIGIN, values));
-        }
+        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
     }
 
     /**
@@ -327,23 +153,7 @@
      * enforced on a per-origin basis for the Application Cache API.
      */
     public void getQuotaForOrigin(String origin, ValueCallback<Long> callback) {
-        if (callback == null) {
-            return;
-        }
-        if (origin == null) {
-            callback.onReceiveValue(null);
-            return;
-        }
-        if (WebViewCore.THREAD_NAME.equals(Thread.currentThread().getName())) {
-            syncValues();
-            Origin website = mOrigins.get(origin);
-            callback.onReceiveValue(new Long(website.getUsage()));
-        } else {
-            HashMap values = new HashMap<String, Object>();
-            values.put(ORIGIN, origin);
-            values.put(CALLBACK, callback);
-            postMessage(Message.obtain(null, GET_QUOTA_ORIGIN, values));
-        }
+        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
     }
 
     /**
@@ -353,14 +163,7 @@
      * for the Application Cache API.
      */
     public void setQuotaForOrigin(String origin, long quota) {
-        if (origin != null) {
-            if (WebViewCore.THREAD_NAME.equals(Thread.currentThread().getName())) {
-                nativeSetQuotaForOrigin(origin, quota);
-            } else {
-                postMessage(Message.obtain(null, SET_QUOTA_ORIGIN,
-                    new Origin(origin, quota)));
-            }
-        }
+        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
     }
 
     /**
@@ -369,14 +172,7 @@
      * its string representation.
      */
     public void deleteOrigin(String origin) {
-        if (origin != null) {
-            if (WebViewCore.THREAD_NAME.equals(Thread.currentThread().getName())) {
-                nativeDeleteOrigin(origin);
-            } else {
-                postMessage(Message.obtain(null, DELETE_ORIGIN,
-                    new Origin(origin)));
-            }
-        }
+        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
     }
 
     /**
@@ -385,38 +181,7 @@
      * Storage APIs.
      */
     public void deleteAllData() {
-        if (WebViewCore.THREAD_NAME.equals(Thread.currentThread().getName())) {
-            nativeDeleteAllData();
-        } else {
-            postMessage(Message.obtain(null, DELETE_ALL));
-        }
-    }
-
-    /**
-     * Sets the maximum size of the ApplicationCache.
-     * This should only ever be called on the WebKit thread.
-     * @hide
-     */
-    public void setAppCacheMaximumSize(long size) {
-        nativeSetAppCacheMaximumSize(size);
-    }
-
-    /**
-     * Utility function to send a message to our handler
-     */
-    private synchronized void postMessage(Message msg) {
-        if (mHandler != null) {
-            mHandler.sendMessage(msg);
-        }
-    }
-
-    /**
-     * Utility function to send a message to the handler on the UI thread
-     */
-    private void postUIMessage(Message msg) {
-        if (mUIHandler != null) {
-            mUIHandler.sendMessage(msg);
-        }
+        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
     }
 
     /**
@@ -424,37 +189,7 @@
      * @return The singleton {@link WebStorage} instance.
      */
     public static WebStorage getInstance() {
-      if (sWebStorage == null) {
-          sWebStorage = new WebStorage();
-      }
-      return sWebStorage;
-    }
-
-    /**
-     * @hide
-     * Post a Sync request
-     */
-    public void update() {
-        if (WebViewCore.THREAD_NAME.equals(Thread.currentThread().getName())) {
-            syncValues();
-        } else {
-            postMessage(Message.obtain(null, UPDATE));
-        }
-    }
-
-    /**
-     * Run on the WebCore thread
-     * set the local values with the current ones
-     */
-    private void syncValues() {
-        Set<String> tmp = nativeGetOrigins();
-        mOrigins = new HashMap<String, Origin>();
-        for (String origin : tmp) {
-            Origin website = new Origin(origin,
-                                 nativeGetQuotaForOrigin(origin),
-                                 nativeGetUsageForOrigin(origin));
-            mOrigins.put(origin, website);
-        }
+      return WebViewFactory.getProvider().getWebStorage();
     }
 
     /**
@@ -466,13 +201,4 @@
      * @hide
      */
     public WebStorage() {}
-
-    // Native functions
-    private static native Set nativeGetOrigins();
-    private static native long nativeGetUsageForOrigin(String origin);
-    private static native long nativeGetQuotaForOrigin(String origin);
-    private static native void nativeSetQuotaForOrigin(String origin, long quota);
-    private static native void nativeDeleteOrigin(String origin);
-    private static native void nativeDeleteAllData();
-    private static native void nativeSetAppCacheMaximumSize(long size);
 }