<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[199644] trunk/Source</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/199644">199644</a></dd>
<dt>Author</dt> <dd>cdumez@apple.com</dd>
<dt>Date</dt> <dd>2016-04-17 11:58:56 -0700 (Sun, 17 Apr 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>[WK2][iOS] Do not dlopen() QuickLook in the NetworkProcess
https://bugs.webkit.org/show_bug.cgi?id=156639

Reviewed by Darin Adler.

Source/WebCore:

Do not unnecessarily dlopen() QuickLook in the NetworkProcess on iOS, as
we already dlopen() this library in the WebContent process. This patch
moves the resource response MIME type adjusting code for QuickLook from
adjustMIMETypeIfNecessary() to a new adjustMIMETypeForQuickLook() function.
adjustMIMETypeIfNecessary() is called in didReceiveResponse() in the Network
process side, for *every* resource response, even though QuickLook can only
be used to preview main resources. The new adjustMIMETypeForQuickLook()
function is called in the QuickLookHandle::createIfNecessary() factory
function, right before checking the MIME type to determine if we need to
use QuickLook, and after checking that the load is for a main resource.
In the WebKit2 case, the factory function is called from
WebResourceLoader::didReceiveResponse(), on the WebContent process side.

This patch speeds up the first page load during PLT by ~22%, because the
first load no longer triggers a dlopen() to QuickLook in the NetworkProcess.
The overall PLT score seems to be progressed by 0.9-1% as well. The change
should also be memory-positive as we no longer need to dlopen() the
QuickLook library in the NetworkProcess at all (and we would already dlopen()
it on the WebContent process side anyway). Sadly, PLUM benchmark does not
show the memory benefit because it does not measure the memory used by the
Network process.

* platform/network/cf/SynchronousResourceHandleCFURLConnectionDelegate.cpp:
(WebCore::SynchronousResourceHandleCFURLConnectionDelegate::didReceiveResponse):
Refactor the code a bit for clarity, so that we only
ResourceHandle::setQuickLookHandle() when QuickLookHandle::createIfNecessary()
returns a non-null pointer.

* platform/network/ios/QuickLook.h:
- Rename the factories from create() to createIfNecessary() given that they
  return nullptr when it is unnecessary to create such handle (i.e. this is not
  a main resource loader, or it is unecessary given the response's MIME type.
- Make shouldCreateForMIMEType() private now that this is always called inside
  the factory functions.

* platform/network/ios/QuickLook.mm:
(adjustMIMETypeForQuickLook):
Extracted code for adjusting the MIME type for QuickLook from the generic
adjustMIMETypeIfNecessary() in WebCoreURLResponseIOS.mm to its own function
here.

(WebCore::QuickLookHandle::createIfNecessary):
Call adjustMIMETypeForQuickLook() before checking the MIME type.

* platform/network/ios/WebCoreURLResponseIOS.mm:
(WebCore::adjustMIMETypeIfNecessary):
Extracted QuickLook-specific code to QuickLook.mm.

* platform/network/mac/WebCoreResourceHandleAsDelegate.mm:
(-[WebCoreResourceHandleAsDelegate connection:didReceiveResponse:]):
Refactor the code a bit for clarity, so that we only
ResourceHandle::setQuickLookHandle() when QuickLookHandle::createIfNecessary()
returns a non-null pointer.

Source/WebKit2:

* WebProcess/Network/WebResourceLoader.cpp:
(WebKit::WebResourceLoader::didReceiveResponse):
Move checks for main resource load and for MIME type inside of
QuickLookHandle::createIfNecessary(), for consistency with the
other QuickLookHandle factory functions.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreplatformnetworkcfSynchronousResourceHandleCFURLConnectionDelegatecpp">trunk/Source/WebCore/platform/network/cf/SynchronousResourceHandleCFURLConnectionDelegate.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformnetworkiosQuickLookh">trunk/Source/WebCore/platform/network/ios/QuickLook.h</a></li>
<li><a href="#trunkSourceWebCoreplatformnetworkiosQuickLookmm">trunk/Source/WebCore/platform/network/ios/QuickLook.mm</a></li>
<li><a href="#trunkSourceWebCoreplatformnetworkiosWebCoreURLResponseIOSmm">trunk/Source/WebCore/platform/network/ios/WebCoreURLResponseIOS.mm</a></li>
<li><a href="#trunkSourceWebCoreplatformnetworkmacWebCoreResourceHandleAsDelegatemm">trunk/Source/WebCore/platform/network/mac/WebCoreResourceHandleAsDelegate.mm</a></li>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2WebProcessNetworkWebResourceLoadercpp">trunk/Source/WebKit2/WebProcess/Network/WebResourceLoader.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (199643 => 199644)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2016-04-17 18:49:53 UTC (rev 199643)
+++ trunk/Source/WebCore/ChangeLog        2016-04-17 18:58:56 UTC (rev 199644)
</span><span class="lines">@@ -1,3 +1,64 @@
</span><ins>+2016-04-17  Chris Dumez  &lt;cdumez@apple.com&gt;
+
+        [WK2][iOS] Do not dlopen() QuickLook in the NetworkProcess
+        https://bugs.webkit.org/show_bug.cgi?id=156639
+
+        Reviewed by Darin Adler.
+
+        Do not unnecessarily dlopen() QuickLook in the NetworkProcess on iOS, as
+        we already dlopen() this library in the WebContent process. This patch
+        moves the resource response MIME type adjusting code for QuickLook from
+        adjustMIMETypeIfNecessary() to a new adjustMIMETypeForQuickLook() function.
+        adjustMIMETypeIfNecessary() is called in didReceiveResponse() in the Network
+        process side, for *every* resource response, even though QuickLook can only
+        be used to preview main resources. The new adjustMIMETypeForQuickLook()
+        function is called in the QuickLookHandle::createIfNecessary() factory
+        function, right before checking the MIME type to determine if we need to
+        use QuickLook, and after checking that the load is for a main resource.
+        In the WebKit2 case, the factory function is called from
+        WebResourceLoader::didReceiveResponse(), on the WebContent process side.
+
+        This patch speeds up the first page load during PLT by ~22%, because the
+        first load no longer triggers a dlopen() to QuickLook in the NetworkProcess.
+        The overall PLT score seems to be progressed by 0.9-1% as well. The change
+        should also be memory-positive as we no longer need to dlopen() the
+        QuickLook library in the NetworkProcess at all (and we would already dlopen()
+        it on the WebContent process side anyway). Sadly, PLUM benchmark does not
+        show the memory benefit because it does not measure the memory used by the
+        Network process.
+
+        * platform/network/cf/SynchronousResourceHandleCFURLConnectionDelegate.cpp:
+        (WebCore::SynchronousResourceHandleCFURLConnectionDelegate::didReceiveResponse):
+        Refactor the code a bit for clarity, so that we only
+        ResourceHandle::setQuickLookHandle() when QuickLookHandle::createIfNecessary()
+        returns a non-null pointer.
+
+        * platform/network/ios/QuickLook.h:
+        - Rename the factories from create() to createIfNecessary() given that they
+          return nullptr when it is unnecessary to create such handle (i.e. this is not
+          a main resource loader, or it is unecessary given the response's MIME type.
+        - Make shouldCreateForMIMEType() private now that this is always called inside
+          the factory functions.
+
+        * platform/network/ios/QuickLook.mm:
+        (adjustMIMETypeForQuickLook):
+        Extracted code for adjusting the MIME type for QuickLook from the generic
+        adjustMIMETypeIfNecessary() in WebCoreURLResponseIOS.mm to its own function
+        here.
+
+        (WebCore::QuickLookHandle::createIfNecessary):
+        Call adjustMIMETypeForQuickLook() before checking the MIME type.
+
+        * platform/network/ios/WebCoreURLResponseIOS.mm:
+        (WebCore::adjustMIMETypeIfNecessary):
+        Extracted QuickLook-specific code to QuickLook.mm.
+
+        * platform/network/mac/WebCoreResourceHandleAsDelegate.mm:
+        (-[WebCoreResourceHandleAsDelegate connection:didReceiveResponse:]):
+        Refactor the code a bit for clarity, so that we only
+        ResourceHandle::setQuickLookHandle() when QuickLookHandle::createIfNecessary()
+        returns a non-null pointer.
+
</ins><span class="cx"> 2016-04-17  Brady Eidson  &lt;beidson@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Clean up IDBBindingUtilities.
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformnetworkcfSynchronousResourceHandleCFURLConnectionDelegatecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/network/cf/SynchronousResourceHandleCFURLConnectionDelegate.cpp (199643 => 199644)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/network/cf/SynchronousResourceHandleCFURLConnectionDelegate.cpp        2016-04-17 18:49:53 UTC (rev 199643)
+++ trunk/Source/WebCore/platform/network/cf/SynchronousResourceHandleCFURLConnectionDelegate.cpp        2016-04-17 18:58:56 UTC (rev 199644)
</span><span class="lines">@@ -166,9 +166,10 @@
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx"> #if USE(QUICK_LOOK)
</span><del>-    m_handle-&gt;setQuickLookHandle(QuickLookHandle::create(m_handle, this, cfResponse));
-    if (m_handle-&gt;quickLookHandle())
-        cfResponse = m_handle-&gt;quickLookHandle()-&gt;cfResponse();
</del><ins>+    if (auto quickLookHandle = QuickLookHandle::createIfNecessary(*m_handle, this, cfResponse)) {
+        cfResponse = quickLookHandle-&gt;cfResponse();
+        m_handle-&gt;setQuickLookHandle(WTFMove(quickLookHandle));
+    }
</ins><span class="cx"> #endif
</span><span class="cx">     
</span><span class="cx">     ResourceResponse resourceResponse(cfResponse);
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformnetworkiosQuickLookh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/network/ios/QuickLook.h (199643 => 199644)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/network/ios/QuickLook.h        2016-04-17 18:49:53 UTC (rev 199643)
+++ trunk/Source/WebCore/platform/network/ios/QuickLook.h        2016-04-17 18:58:56 UTC (rev 199644)
</span><span class="lines">@@ -78,14 +78,13 @@
</span><span class="cx"> class QuickLookHandle {
</span><span class="cx">     WTF_MAKE_NONCOPYABLE(QuickLookHandle);
</span><span class="cx"> public:
</span><del>-    WEBCORE_EXPORT static bool shouldCreateForMIMEType(const String&amp;);
-
-    static std::unique_ptr&lt;QuickLookHandle&gt; create(ResourceHandle*, NSURLConnection *, NSURLResponse *, id delegate);
</del><ins>+    static std::unique_ptr&lt;QuickLookHandle&gt; createIfNecessary(ResourceHandle&amp;, NSURLConnection *, NSURLResponse *, id delegate);
</ins><span class="cx"> #if USE(CFNETWORK)
</span><del>-    static std::unique_ptr&lt;QuickLookHandle&gt; create(ResourceHandle*, SynchronousResourceHandleCFURLConnectionDelegate*, CFURLResponseRef);
</del><ins>+    static std::unique_ptr&lt;QuickLookHandle&gt; createIfNecessary(ResourceHandle&amp;, SynchronousResourceHandleCFURLConnectionDelegate*, CFURLResponseRef);
</ins><span class="cx"> #endif
</span><ins>+
</ins><span class="cx">     // FIXME: Use of ResourceLoader here is a platform violation.
</span><del>-    WEBCORE_EXPORT static std::unique_ptr&lt;QuickLookHandle&gt; create(ResourceLoader&amp;, const ResourceResponse&amp;);
</del><ins>+    WEBCORE_EXPORT static std::unique_ptr&lt;QuickLookHandle&gt; createIfNecessary(ResourceLoader&amp;, NSURLResponse *);
</ins><span class="cx"> 
</span><span class="cx">     WEBCORE_EXPORT ~QuickLookHandle();
</span><span class="cx"> 
</span><span class="lines">@@ -108,6 +107,7 @@
</span><span class="cx">     QLPreviewConverter *converter() const { return m_converter.get(); }
</span><span class="cx"> 
</span><span class="cx"> private:
</span><ins>+    static std::unique_ptr&lt;QuickLookHandle&gt; create(ResourceHandle&amp;, NSURLConnection *, NSURLResponse *, id delegate);
</ins><span class="cx">     QuickLookHandle(NSURL *, NSURLConnection *, NSURLResponse *, id delegate);
</span><span class="cx"> 
</span><span class="cx">     RetainPtr&lt;NSURL&gt; m_firstRequestURL;
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformnetworkiosQuickLookmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/network/ios/QuickLook.mm (199643 => 199644)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/network/ios/QuickLook.mm        2016-04-17 18:49:53 UTC (rev 199643)
+++ trunk/Source/WebCore/platform/network/ios/QuickLook.mm        2016-04-17 18:58:56 UTC (rev 199644)
</span><span class="lines">@@ -36,10 +36,14 @@
</span><span class="cx"> #import &quot;ResourceHandle.h&quot;
</span><span class="cx"> #import &quot;ResourceLoader.h&quot;
</span><span class="cx"> #import &quot;RuntimeApplicationChecks.h&quot;
</span><ins>+#import &quot;SubresourceLoader.h&quot;
</ins><span class="cx"> #import &quot;SynchronousResourceHandleCFURLConnectionDelegate.h&quot;
</span><ins>+#import &quot;UTIUtilities.h&quot;
</ins><span class="cx"> #import &quot;WebCoreResourceHandleAsDelegate.h&quot;
</span><ins>+#import &quot;WebCoreSystemInterface.h&quot;
</ins><span class="cx"> #import &quot;WebCoreURLResponseIOS.h&quot;
</span><span class="cx"> #import &lt;Foundation/Foundation.h&gt;
</span><ins>+#import &lt;MobileCoreServices/MobileCoreServices.h&gt;
</ins><span class="cx"> #import &lt;wtf/NeverDestroyed.h&gt;
</span><span class="cx"> #import &lt;wtf/StdLibExtras.h&gt;
</span><span class="cx"> #import &lt;wtf/Threading.h&gt;
</span><span class="lines">@@ -48,6 +52,14 @@
</span><span class="cx"> 
</span><span class="cx"> #import &quot;QuickLookSoftLink.h&quot;
</span><span class="cx"> 
</span><ins>+SOFT_LINK_FRAMEWORK(MobileCoreServices)
+
+SOFT_LINK(MobileCoreServices, UTTypeCreatePreferredIdentifierForTag, CFStringRef, (CFStringRef inTagClass, CFStringRef inTag, CFStringRef inConformingToUTI), (inTagClass, inTag, inConformingToUTI))
+
+SOFT_LINK_CONSTANT(MobileCoreServices, kUTTagClassFilenameExtension, CFStringRef)
+
+#define kUTTagClassFilenameExtension getkUTTagClassFilenameExtension()
+
</ins><span class="cx"> using namespace WebCore;
</span><span class="cx"> 
</span><span class="cx"> NSSet *WebCore::QLPreviewGetSupportedMIMETypesSet()
</span><span class="lines">@@ -96,6 +108,43 @@
</span><span class="cx">     return contentDictionary;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+// We must ensure that the MIME type is correct, so that QuickLook's web plugin is called when needed.
+// We filter the basic MIME types so that we don't do unnecessary work in standard browsing situations.
+static RetainPtr&lt;CFStringRef&gt; adjustMIMETypeForQuickLook(CFURLResponseRef cfResponse)
+{
+    RetainPtr&lt;CFStringRef&gt; mimeType = CFURLResponseGetMIMEType(cfResponse);
+    if (!shouldUseQuickLookForMIMEType(mimeType.get()))
+        return mimeType;
+
+    RetainPtr&lt;CFStringRef&gt; suggestedFilename = adoptCF(CFURLResponseCopySuggestedFilename(cfResponse));
+    RetainPtr&lt;CFStringRef&gt; quickLookMIMEType = adoptCF((CFStringRef)QLTypeCopyBestMimeTypeForFileNameAndMimeType((NSString *)suggestedFilename.get(), (NSString *)mimeType.get()));
+    if (!quickLookMIMEType) {
+        auto url = CFURLResponseGetURL(cfResponse);
+        if (![(NSURL *)url isFileURL])
+            return mimeType;
+        RetainPtr&lt;CFStringRef&gt; extension = adoptCF(CFURLCopyPathExtension(url));
+        if (!extension)
+            return mimeType;
+        RetainPtr&lt;CFStringRef&gt; uti = adoptCF(UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, extension.get(), nullptr));
+        quickLookMIMEType = mimeTypeFromUTITree(uti.get());
+        if (!quickLookMIMEType)
+            return mimeType;
+    }
+
+    if (!mimeType || CFStringCompare(mimeType.get(), quickLookMIMEType.get(), kCFCompareCaseInsensitive) != kCFCompareEqualTo) {
+        CFURLResponseSetMIMEType(cfResponse, quickLookMIMEType.get());
+        return quickLookMIMEType;
+    }
+
+    return mimeType;
+}
+
+static bool shouldCreateForResponse(CFURLResponseRef cfResponse)
+{
+    RetainPtr&lt;CFStringRef&gt; mimeType = adjustMIMETypeForQuickLook(cfResponse);
+    return [QLPreviewGetSupportedMIMETypesSet() containsObject:(NSString *)mimeType.get()];
+}
+
</ins><span class="cx"> void WebCore::addQLPreviewConverterWithFileForURL(NSURL *url, id converter, NSString *fileName)
</span><span class="cx"> {
</span><span class="cx">     ASSERT(url);
</span><span class="lines">@@ -396,29 +445,35 @@
</span><span class="cx">     LOG(Network, &quot;QuickLookHandle::QuickLookHandle() - previewFileName: %s&quot;, [m_converter previewFileName]);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-std::unique_ptr&lt;QuickLookHandle&gt; QuickLookHandle::create(ResourceHandle* handle, NSURLConnection *connection, NSURLResponse *nsResponse, id delegate)
</del><ins>+static bool shouldCreate(ResourceHandle&amp; handle, CFURLResponseRef response)
</ins><span class="cx"> {
</span><del>-    ASSERT_ARG(handle, handle);
-    if (handle-&gt;firstRequest().requester() != ResourceRequest::Requester::Main || ![QLPreviewGetSupportedMIMETypesSet() containsObject:[nsResponse MIMEType]])
-        return nullptr;
</del><ins>+    return handle.firstRequest().requester() == ResourceRequest::Requester::Main &amp;&amp; shouldCreateForResponse(response);
+}
</ins><span class="cx"> 
</span><del>-    std::unique_ptr&lt;QuickLookHandle&gt; quickLookHandle(new QuickLookHandle([handle-&gt;firstRequest().nsURLRequest(DoNotUpdateHTTPBody) URL], connection, nsResponse, delegate));
-    handle-&gt;client()-&gt;didCreateQuickLookHandle(*quickLookHandle);
</del><ins>+std::unique_ptr&lt;QuickLookHandle&gt; QuickLookHandle::create(ResourceHandle&amp; handle, NSURLConnection *connection, NSURLResponse *response, id delegate)
+{
+    std::unique_ptr&lt;QuickLookHandle&gt; quickLookHandle(new QuickLookHandle([handle.firstRequest().nsURLRequest(DoNotUpdateHTTPBody) URL], connection, response, delegate));
+    handle.client()-&gt;didCreateQuickLookHandle(*quickLookHandle);
</ins><span class="cx">     return quickLookHandle;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+std::unique_ptr&lt;QuickLookHandle&gt; QuickLookHandle::createIfNecessary(ResourceHandle&amp; handle, NSURLConnection *connection, NSURLResponse *response, id delegate)
+{
+    if (!shouldCreate(handle, response._CFURLResponse))
+        return nullptr;
+
+    return create(handle, connection, response, delegate);
+}
+
</ins><span class="cx"> #if USE(CFNETWORK)
</span><del>-std::unique_ptr&lt;QuickLookHandle&gt; QuickLookHandle::create(ResourceHandle* handle, SynchronousResourceHandleCFURLConnectionDelegate* connectionDelegate, CFURLResponseRef cfResponse)
</del><ins>+std::unique_ptr&lt;QuickLookHandle&gt; QuickLookHandle::createIfNecessary(ResourceHandle&amp; handle, SynchronousResourceHandleCFURLConnectionDelegate* connectionDelegate, CFURLResponseRef cfResponse)
</ins><span class="cx"> {
</span><del>-    ASSERT_ARG(handle, handle);
-    if (handle-&gt;firstRequest().requester() != ResourceRequest::Requester::Main || ![QLPreviewGetSupportedMIMETypesSet() containsObject:(NSString *)CFURLResponseGetMIMEType(cfResponse)])
</del><ins>+    if (!shouldCreate(handle, cfResponse))
</ins><span class="cx">         return nullptr;
</span><span class="cx"> 
</span><del>-    NSURLResponse *nsResponse = [NSURLResponse _responseWithCFURLResponse:cfResponse];
</del><ins>+    NSURLResponse *response = [NSURLResponse _responseWithCFURLResponse:cfResponse];
</ins><span class="cx">     WebQuickLookHandleAsDelegate *delegate = [[[WebQuickLookHandleAsDelegate alloc] initWithConnectionDelegate:connectionDelegate] autorelease];
</span><del>-    std::unique_ptr&lt;QuickLookHandle&gt; quickLookHandle(new QuickLookHandle([handle-&gt;firstRequest().nsURLRequest(DoNotUpdateHTTPBody) URL], nil, nsResponse, delegate));
-    handle-&gt;client()-&gt;didCreateQuickLookHandle(*quickLookHandle);
-    return quickLookHandle;
</del><ins>+    return create(handle, nil, response, delegate);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> CFURLResponseRef QuickLookHandle::cfResponse()
</span><span class="lines">@@ -427,17 +482,17 @@
</span><span class="cx"> }
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><del>-bool QuickLookHandle::shouldCreateForMIMEType(const String&amp; mimeType)
</del><ins>+std::unique_ptr&lt;QuickLookHandle&gt; QuickLookHandle::createIfNecessary(ResourceLoader&amp; loader, NSURLResponse *response)
</ins><span class="cx"> {
</span><del>-    return [QLPreviewGetSupportedMIMETypesSet() containsObject:mimeType];
-}
</del><ins>+    bool isMainResourceLoad = loader.documentLoader()-&gt;mainResourceLoader() == &amp;loader;
+    if (!isMainResourceLoad)
+        return nullptr;
</ins><span class="cx"> 
</span><del>-std::unique_ptr&lt;QuickLookHandle&gt; QuickLookHandle::create(ResourceLoader&amp; loader, const ResourceResponse&amp; response)
-{
-    ASSERT(shouldCreateForMIMEType(response.mimeType()));
</del><ins>+    if (!shouldCreateForResponse(response._CFURLResponse))
+        return nullptr;
</ins><span class="cx"> 
</span><span class="cx">     RetainPtr&lt;WebResourceLoaderQuickLookDelegate&gt; delegate = adoptNS([[WebResourceLoaderQuickLookDelegate alloc] initWithResourceLoader:&amp;loader]);
</span><del>-    std::unique_ptr&lt;QuickLookHandle&gt; quickLookHandle(new QuickLookHandle([loader.originalRequest().nsURLRequest(DoNotUpdateHTTPBody) URL], nil, response.nsURLResponse(), delegate.get()));
</del><ins>+    std::unique_ptr&lt;QuickLookHandle&gt; quickLookHandle(new QuickLookHandle([loader.originalRequest().nsURLRequest(DoNotUpdateHTTPBody) URL], nil, response, delegate.get()));
</ins><span class="cx">     [delegate setQuickLookHandle:quickLookHandle.get()];
</span><span class="cx">     loader.didCreateQuickLookHandle(*quickLookHandle);
</span><span class="cx">     return quickLookHandle;
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformnetworkiosWebCoreURLResponseIOSmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/network/ios/WebCoreURLResponseIOS.mm (199643 => 199644)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/network/ios/WebCoreURLResponseIOS.mm        2016-04-17 18:49:53 UTC (rev 199643)
+++ trunk/Source/WebCore/platform/network/ios/WebCoreURLResponseIOS.mm        2016-04-17 18:58:56 UTC (rev 199644)
</span><span class="lines">@@ -28,22 +28,7 @@
</span><span class="cx"> 
</span><span class="cx"> #import &quot;config.h&quot;
</span><span class="cx"> #import &quot;WebCoreURLResponseIOS.h&quot;
</span><del>-#import &quot;UTIUtilities.h&quot;
-#import &quot;WebCoreSystemInterface.h&quot;
</del><span class="cx"> 
</span><del>-#import &quot;QuickLook.h&quot;
-#import &quot;QuickLookSoftLink.h&quot;
-#import &quot;SoftLinking.h&quot;
-#import &lt;MobileCoreServices/MobileCoreServices.h&gt;
-
-SOFT_LINK_FRAMEWORK(MobileCoreServices)
-
-SOFT_LINK(MobileCoreServices, UTTypeCreatePreferredIdentifierForTag, CFStringRef, (CFStringRef inTagClass, CFStringRef inTag, CFStringRef inConformingToUTI), (inTagClass, inTag, inConformingToUTI))
-
-SOFT_LINK_CONSTANT(MobileCoreServices, kUTTagClassFilenameExtension, CFStringRef)
-
-#define kUTTagClassFilenameExtension getkUTTagClassFilenameExtension()
-
</del><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><span class="cx"> void adjustMIMETypeIfNecessary(CFURLResponseRef cfResponse)
</span><span class="lines">@@ -53,27 +38,6 @@
</span><span class="cx">     if (!updatedMIMEType)
</span><span class="cx">         updatedMIMEType = defaultMIMEType().createCFString();
</span><span class="cx"> 
</span><del>-#if USE(QUICK_LOOK)
-    // We must ensure that the MIME type is correct, so that QuickLook's web plugin is called when needed.
-    // We filter the basic MIME types so that we don't do unnecessary work in standard browsing situations.
-    if (shouldUseQuickLookForMIMEType((NSString *)updatedMIMEType.get())) {
-        RetainPtr&lt;CFStringRef&gt; suggestedFilename = adoptCF(CFURLResponseCopySuggestedFilename(cfResponse));
-        RetainPtr&lt;CFStringRef&gt; quickLookMIMEType = adoptCF((CFStringRef)QLTypeCopyBestMimeTypeForFileNameAndMimeType((NSString *)suggestedFilename.get(), (NSString *)mimeType.get()));
-        if (!quickLookMIMEType) {
-            auto url = CFURLResponseGetURL(cfResponse);
-            if ([(NSURL *)url isFileURL]) {
-                RetainPtr&lt;CFStringRef&gt; extension = adoptCF(CFURLCopyPathExtension(url));
-                if (extension) {
-                    RetainPtr&lt;CFStringRef&gt; uti = adoptCF(UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, extension.get(), nullptr));
-                    quickLookMIMEType = mimeTypeFromUTITree(uti.get());
-                }
-            }
-        }
-
-        if (quickLookMIMEType)
-            updatedMIMEType = quickLookMIMEType;
-    }
-#endif // USE(QUICK_LOOK)
</del><span class="cx">     if (!mimeType || CFStringCompare(mimeType.get(), updatedMIMEType.get(), kCFCompareCaseInsensitive) != kCFCompareEqualTo)
</span><span class="cx">         CFURLResponseSetMIMEType(cfResponse, updatedMIMEType.get());
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformnetworkmacWebCoreResourceHandleAsDelegatemm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/network/mac/WebCoreResourceHandleAsDelegate.mm (199643 => 199644)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/network/mac/WebCoreResourceHandleAsDelegate.mm        2016-04-17 18:49:53 UTC (rev 199643)
+++ trunk/Source/WebCore/platform/network/mac/WebCoreResourceHandleAsDelegate.mm        2016-04-17 18:58:56 UTC (rev 199644)
</span><span class="lines">@@ -162,9 +162,10 @@
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx"> #if USE(QUICK_LOOK)
</span><del>-    m_handle-&gt;setQuickLookHandle(QuickLookHandle::create(m_handle, connection, r, self));
-    if (m_handle-&gt;quickLookHandle())
-        r = m_handle-&gt;quickLookHandle()-&gt;nsResponse();
</del><ins>+    if (auto quickLookHandle = QuickLookHandle::createIfNecessary(*m_handle, connection, r, self)) {
+        r = quickLookHandle-&gt;nsResponse();
+        m_handle-&gt;setQuickLookHandle(WTFMove(quickLookHandle));
+    }
</ins><span class="cx"> #endif
</span><span class="cx">     
</span><span class="cx">     ResourceResponse resourceResponse(r);
</span></span></pre></div>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (199643 => 199644)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/ChangeLog        2016-04-17 18:49:53 UTC (rev 199643)
+++ trunk/Source/WebKit2/ChangeLog        2016-04-17 18:58:56 UTC (rev 199644)
</span><span class="lines">@@ -1,3 +1,16 @@
</span><ins>+2016-04-17  Chris Dumez  &lt;cdumez@apple.com&gt;
+
+        [WK2][iOS] Do not dlopen() QuickLook in the NetworkProcess
+        https://bugs.webkit.org/show_bug.cgi?id=156639
+
+        Reviewed by Darin Adler.
+
+        * WebProcess/Network/WebResourceLoader.cpp:
+        (WebKit::WebResourceLoader::didReceiveResponse):
+        Move checks for main resource load and for MIME type inside of
+        QuickLookHandle::createIfNecessary(), for consistency with the
+        other QuickLookHandle factory functions.
+
</ins><span class="cx"> 2016-04-16  Dan Bernstein  &lt;mitz@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [Mac] Web Content service with a restricted entitlement may load arbitrary dylibs
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessNetworkWebResourceLoadercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/Network/WebResourceLoader.cpp (199643 => 199644)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/Network/WebResourceLoader.cpp        2016-04-17 18:49:53 UTC (rev 199643)
+++ trunk/Source/WebKit2/WebProcess/Network/WebResourceLoader.cpp        2016-04-17 18:58:56 UTC (rev 199644)
</span><span class="lines">@@ -114,12 +114,12 @@
</span><span class="cx">     // Refrain from calling didReceiveResponse if QuickLook will convert this response, since the MIME type of the
</span><span class="cx">     // converted resource isn't yet known. WebResourceLoaderQuickLookDelegate will later call didReceiveResponse upon
</span><span class="cx">     // receiving the converted data.
</span><del>-    bool isMainLoad = m_coreLoader-&gt;documentLoader()-&gt;mainResourceLoader() == m_coreLoader;
-    if (isMainLoad &amp;&amp; QuickLookHandle::shouldCreateForMIMEType(response.mimeType())) {
-        m_coreLoader-&gt;documentLoader()-&gt;setQuickLookHandle(QuickLookHandle::create(*m_coreLoader, response));
</del><ins>+    if (auto quickLookHandle = QuickLookHandle::createIfNecessary(*m_coreLoader, response.nsURLResponse())) {
+        m_coreLoader-&gt;documentLoader()-&gt;setQuickLookHandle(WTFMove(quickLookHandle));
</ins><span class="cx">         shoudCallCoreLoaderDidReceiveResponse = false;
</span><span class="cx">     }
</span><span class="cx"> #endif
</span><ins>+
</ins><span class="cx">     if (shoudCallCoreLoaderDidReceiveResponse)
</span><span class="cx">         m_coreLoader-&gt;didReceiveResponse(response);
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>