Fix CacheManager to correctly write CacheFiles to disk.

There are two problems with the CacheManager -
1. Need to null terminate the file path used for the cache file on
disk.
2. A race condition between starting the background task to write
the file and updating internal state variables.

These were causing a CTS test to fail.

Bug: 5619303
Change-Id: I61f06d50b7ef560ede2f1141fc51d92255d4efbd
diff --git a/Source/WebKit/android/WebCoreSupport/CacheResult.cpp b/Source/WebKit/android/WebCoreSupport/CacheResult.cpp
index 5309c66..6710e49 100644
--- a/Source/WebKit/android/WebCoreSupport/CacheResult.cpp
+++ b/Source/WebKit/android/WebCoreSupport/CacheResult.cpp
@@ -134,11 +134,11 @@
     if (!thread)
         return false;
 
-    CacheResult* me = const_cast<CacheResult*>(this);
-    thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(me, &CacheResult::writeToFileImpl));
-
     m_filePath = filePath.threadsafeCopy();
     m_isAsyncOperationInProgress = true;
+
+    thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(const_cast<CacheResult*>(this), &CacheResult::writeToFileImpl));
+
     while (m_isAsyncOperationInProgress)
         m_condition.wait(m_mutex);
 
@@ -213,10 +213,9 @@
     if (!thread)
         return 0;
 
-    CacheResult* me = const_cast<CacheResult*>(this);
-    thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(me, &CacheResult::responseHeadersImpl));
-
     m_isAsyncOperationInProgress = true;
+    thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(const_cast<CacheResult*>(this), &CacheResult::responseHeadersImpl));
+
     while (m_isAsyncOperationInProgress)
         m_condition.wait(m_mutex);
 
diff --git a/Source/WebKit/android/jni/CacheManager.cpp b/Source/WebKit/android/jni/CacheManager.cpp
index 144b62a..f600d00 100644
--- a/Source/WebKit/android/jni/CacheManager.cpp
+++ b/Source/WebKit/android/jni/CacheManager.cpp
@@ -90,6 +90,7 @@
     String urlWtfString = jstringToWtfString(env, url);
     Vector<char> encodedUrl;
     base64Encode(urlWtfString.utf8().data(), urlWtfString.length(), encodedUrl, false /*insertLFs*/);
+    encodedUrl.append('\0');
     String filePath = pathByAppendingComponent(getCacheFileBaseDir(env), encodedUrl.data());
     if (!result->writeToFile(filePath))
         return 0;