[iOS] Subresources referenced in converted QuickLook documents sometimes fail to load
https://bugs.webkit.org/show_bug.cgi?id=135676

Reviewed by David Kilzer.

Source/WebCore:

* loader/DocumentLoader.h:
(WebCore::DocumentLoader::setQuickLookHandle):
(WebCore::DocumentLoader::quickLookHandle):

Source/WebKit2:

QuickLookHandle needs to stay alive in order for its NSURLProtocol to service subresource loads originating
from the converted HTML document. Some of these loads happen dynamically after the main resource finishes
loading, so we cannot tie the lifetime of the QuickLookHandle to that of the main resource's ResourceLoader.
Instead, give ownership of the QuickLookHandle to DocumentLoader.

* WebProcess/Network/WebResourceLoader.cpp:
(WebKit::WebResourceLoader::didReceiveResponseWithCertificateInfo): Stored the created QuickLookHandle in DocumentLoader.
(WebKit::WebResourceLoader::didReceiveData): Accessed DocumentLoader's QuickLookHandle.
(WebKit::WebResourceLoader::didFinishResourceLoad): Ditto.
(WebKit::WebResourceLoader::didFailResourceLoad): Ditto.
(WebKit::WebResourceLoader::didReceiveResource): Ditto.
* WebProcess/Network/WebResourceLoader.h: Removed m_quickLookHandle.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@172191 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebKit2/WebProcess/Network/WebResourceLoader.cpp b/Source/WebKit2/WebProcess/Network/WebResourceLoader.cpp
index 6eb4684..c106e81 100644
--- a/Source/WebKit2/WebProcess/Network/WebResourceLoader.cpp
+++ b/Source/WebKit2/WebProcess/Network/WebResourceLoader.cpp
@@ -125,8 +125,8 @@
     // Refrain from calling didReceiveResponse if QuickLook will convert this response, since the MIME type of the
     // converted resource isn't yet known. WebResourceLoaderQuickLookDelegate will later call didReceiveResponse upon
     // receiving the converted data.
-    m_quickLookHandle = QuickLookHandle::create(resourceLoader(), responseCopy.nsURLResponse());
-    if (!m_quickLookHandle)
+    m_coreLoader->documentLoader()->setQuickLookHandle(QuickLookHandle::create(resourceLoader(), responseCopy.nsURLResponse()));
+    if (!m_coreLoader->documentLoader()->quickLookHandle())
 #endif
         m_coreLoader->didReceiveResponse(responseCopy);
 
@@ -143,8 +143,8 @@
     LOG(Network, "(WebProcess) WebResourceLoader::didReceiveData of size %i for '%s'", (int)data.size(), m_coreLoader->url().string().utf8().data());
 
 #if USE(QUICK_LOOK)
-    if (m_quickLookHandle) {
-        if (m_quickLookHandle->didReceiveData(adoptCF(CFDataCreate(kCFAllocatorDefault, data.data(), data.size())).get()))
+    if (QuickLookHandle* quickLookHandle = m_coreLoader->documentLoader()->quickLookHandle()) {
+        if (quickLookHandle->didReceiveData(adoptCF(CFDataCreate(kCFAllocatorDefault, data.data(), data.size())).get()))
             return;
     }
 #endif
@@ -156,8 +156,10 @@
     LOG(Network, "(WebProcess) WebResourceLoader::didFinishResourceLoad for '%s'", m_coreLoader->url().string().utf8().data());
 
 #if USE(QUICK_LOOK)
-    if (m_quickLookHandle && m_quickLookHandle->didFinishLoading())
-        return;
+    if (QuickLookHandle* quickLookHandle = m_coreLoader->documentLoader()->quickLookHandle()) {
+        if (quickLookHandle->didFinishLoading())
+            return;
+    }
 #endif
     m_coreLoader->didFinishLoading(finishTime);
 }
@@ -167,8 +169,8 @@
     LOG(Network, "(WebProcess) WebResourceLoader::didFailResourceLoad for '%s'", m_coreLoader->url().string().utf8().data());
     
 #if USE(QUICK_LOOK)
-    if (m_quickLookHandle)
-        m_quickLookHandle->didFail();
+    if (QuickLookHandle* quickLookHandle = m_coreLoader->documentLoader()->quickLookHandle())
+        quickLookHandle->didFail();
 #endif
     if (m_coreLoader->documentLoader()->applicationCacheHost()->maybeLoadFallbackForError(m_coreLoader.get(), error))
         return;
@@ -181,15 +183,15 @@
     LOG(Network, "(WebProcess) WebResourceLoader::didReceiveResource for '%s'", m_coreLoader->url().string().utf8().data());
 
 #if USE(QUICK_LOOK)
-    if (m_quickLookHandle) {
+    if (QuickLookHandle* quickLookHandle = m_coreLoader->documentLoader()->quickLookHandle()) {
         RetainPtr<CFDataRef> cfBuffer = handle.tryWrapInCFData();
         if (cfBuffer) {
-            if (m_quickLookHandle->didReceiveData(cfBuffer.get())) {
-                m_quickLookHandle->didFinishLoading();
+            if (quickLookHandle->didReceiveData(cfBuffer.get())) {
+                quickLookHandle->didFinishLoading();
                 return;
             }
         } else
-            m_quickLookHandle->didFail();
+            quickLookHandle->didFail();
     }
 #endif