Move getDatabaseDirectory and getCacheDirectory to a new class

The reason for this is that the CookieSyncManager might not be
initialized right after a webview is created since the initialization
is done with a message.

If someone creates a webview and tries to access a cookie right
afterwards that would cause an unintended exception.

This requires a change in external/webkit

Bug 3172863

Change-Id: I4f4f162253e2af09f63ee582bfce9f75ccf4037b
diff --git a/core/java/android/webkit/CookieSyncManager.java b/core/java/android/webkit/CookieSyncManager.java
index 8b76a3b..071eb8c 100644
--- a/core/java/android/webkit/CookieSyncManager.java
+++ b/core/java/android/webkit/CookieSyncManager.java
@@ -65,11 +65,6 @@
     // time when last update happened
     private long mLastUpdate;
 
-    // Used by the Chromium HTTP stack. Everything else in this class is used only by the Android
-    // Java HTTP stack.
-    private static String sDatabaseDirectory;
-    private static String sCacheDirectory;
-
     private CookieSyncManager(Context context) {
         super(context, "CookieSyncManager");
     }
@@ -93,11 +88,10 @@
      */
     public static synchronized CookieSyncManager createInstance(
             Context context) {
+        JniUtil.setContext(context);
         Context appContext = context.getApplicationContext();
         if (sRef == null) {
             sRef = new CookieSyncManager(appContext);
-            sDatabaseDirectory = appContext.getDatabasePath("dummy").getParent();
-            sCacheDirectory = appContext.getCacheDir().getAbsolutePath();
         }
         return sRef;
     }
@@ -222,22 +216,4 @@
                             + "before CookieSyncManager::getInstance()");
         }
     }
-
-    /**
-     * Called by JNI. Gets the application's database directory, excluding the trailing slash.
-     * @return String The application's database directory
-     */
-    private static synchronized String getDatabaseDirectory() {
-        checkInstanceIsCreated();
-        return sDatabaseDirectory;
-    }
-
-    /**
-     * Called by JNI. Gets the application's cache directory, excluding the trailing slash.
-     * @return String The application's cache directory
-     */
-    private static synchronized String getCacheDirectory() {
-        checkInstanceIsCreated();
-        return sCacheDirectory;
-    }
 }
diff --git a/core/java/android/webkit/JniUtil.java b/core/java/android/webkit/JniUtil.java
new file mode 100644
index 0000000..ef44d3a
--- /dev/null
+++ b/core/java/android/webkit/JniUtil.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.webkit;
+
+import android.content.Context;
+
+class JniUtil {
+    // Used by the Chromium HTTP stack.
+    private static String sDatabaseDirectory;
+    private static String sCacheDirectory;
+
+    private static boolean initialized = false;
+
+    private static void checkIntialized() {
+        if (!initialized) {
+            throw new IllegalStateException("Call CookieSyncManager::createInstance() or create a webview before using this class");
+        }
+    }
+
+    protected static synchronized void setContext(Context context) {
+        if (initialized)
+            return;
+
+        Context appContext = context.getApplicationContext();
+        sDatabaseDirectory = appContext.getDatabasePath("dummy").getParent();
+        sCacheDirectory = appContext.getCacheDir().getAbsolutePath();
+        initialized = true;
+    }
+
+    /**
+     * Called by JNI. Gets the application's database directory, excluding the trailing slash.
+     * @return String The application's database directory
+     */
+    private static synchronized String getDatabaseDirectory() {
+        checkIntialized();
+        return sDatabaseDirectory;
+    }
+
+    /**
+     * Called by JNI. Gets the application's cache directory, excluding the trailing slash.
+     * @return String The application's cache directory
+     */
+    private static synchronized String getCacheDirectory() {
+        checkIntialized();
+        return sCacheDirectory;
+    }
+}
diff --git a/core/java/android/webkit/WebView.java b/core/java/android/webkit/WebView.java
index f987a49..8fbdfe8 100644
--- a/core/java/android/webkit/WebView.java
+++ b/core/java/android/webkit/WebView.java
@@ -893,6 +893,9 @@
             Map<String, Object> javascriptInterfaces, boolean privateBrowsing) {
         super(context, attrs, defStyle);
 
+        // Used by the chrome stack to find application paths
+        JniUtil.setContext(context);
+
         if (AccessibilityManager.getInstance(context).isEnabled()) {
             if (javascriptInterfaces == null) {
                 javascriptInterfaces = new HashMap<String, Object>();