Title: [163486] trunk/Source/WebCore
Revision
163486
Author
[email protected]
Date
2014-02-05 16:20:42 -0800 (Wed, 05 Feb 2014)

Log Message

Buffer incoming data in ContentFilter when using NEFilterSource
https://bugs.webkit.org/show_bug.cgi?id=127979

Reviewed by Sam Weinig.

WebFilterEvaluator buffers incoming data and returns it to us as
replacement data if the load is allowed. NEFilterSource doesn't do
this, so we need to do our own buffering.

* platform/ContentFilter.h: Forward-declared NSMutableData and added
m_originalData.
* platform/mac/ContentFilterMac.mm:
(WebCore::ContentFilter::ContentFilter): Constructed m_originalData
with an initial capacity (if we know the expected content size).
(WebCore::ContentFilter::addData): Buffered incoming data if we are
using NEFilterSource.
(WebCore::ContentFilter::finishedAddingData):
(WebCore::ContentFilter::getReplacementData): Returned m_originalData
if the load wasn't blocked.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (163485 => 163486)


--- trunk/Source/WebCore/ChangeLog	2014-02-06 00:13:04 UTC (rev 163485)
+++ trunk/Source/WebCore/ChangeLog	2014-02-06 00:20:42 UTC (rev 163486)
@@ -1,3 +1,25 @@
+2014-02-04  Andy Estes  <[email protected]>
+
+        Buffer incoming data in ContentFilter when using NEFilterSource
+        https://bugs.webkit.org/show_bug.cgi?id=127979
+
+        Reviewed by Sam Weinig.
+
+        WebFilterEvaluator buffers incoming data and returns it to us as
+        replacement data if the load is allowed. NEFilterSource doesn't do
+        this, so we need to do our own buffering.
+
+        * platform/ContentFilter.h: Forward-declared NSMutableData and added
+        m_originalData.
+        * platform/mac/ContentFilterMac.mm:
+        (WebCore::ContentFilter::ContentFilter): Constructed m_originalData
+        with an initial capacity (if we know the expected content size).
+        (WebCore::ContentFilter::addData): Buffered incoming data if we are
+        using NEFilterSource.
+        (WebCore::ContentFilter::finishedAddingData):
+        (WebCore::ContentFilter::getReplacementData): Returned m_originalData
+        if the load wasn't blocked.
+
 2014-02-05  Andreas Kling  <[email protected]>
 
         Remove ENABLE(DIRECTORY_UPLOAD).

Modified: trunk/Source/WebCore/platform/ContentFilter.h (163485 => 163486)


--- trunk/Source/WebCore/platform/ContentFilter.h	2014-02-06 00:13:04 UTC (rev 163485)
+++ trunk/Source/WebCore/platform/ContentFilter.h	2014-02-06 00:20:42 UTC (rev 163486)
@@ -37,6 +37,7 @@
 #endif
 
 #if PLATFORM(MAC)
+OBJC_CLASS NSMutableData;
 OBJC_CLASS WebFilterEvaluator;
 #endif
 
@@ -82,7 +83,7 @@
     std::atomic<long> m_neFilterSourceStatus;
     RetainPtr<NEFilterSource> m_neFilterSource;
     dispatch_queue_t m_neFilterSourceQueue;
-    dispatch_semaphore_t m_neFilterSourceSemaphore;
+    RetainPtr<NSMutableData> m_originalData;
 #endif
 };
 

Modified: trunk/Source/WebCore/platform/mac/ContentFilterMac.mm (163485 => 163486)


--- trunk/Source/WebCore/platform/mac/ContentFilterMac.mm	2014-02-06 00:13:04 UTC (rev 163485)
+++ trunk/Source/WebCore/platform/mac/ContentFilterMac.mm	2014-02-06 00:20:42 UTC (rev 163486)
@@ -102,6 +102,12 @@
     if ([getNEFilterSourceClass() filterRequired]) {
         m_neFilterSource = adoptNS([[getNEFilterSourceClass() alloc] initWithURL:[response.nsURLResponse() URL] direction:NEFilterSourceDirectionInbound socketIdentifier:0]);
         m_neFilterSourceQueue = dispatch_queue_create("com.apple.WebCore.NEFilterSourceQueue", DISPATCH_QUEUE_SERIAL);
+        
+        long long expectedContentSize = [response.nsURLResponse() expectedContentLength];
+        if (expectedContentSize < 0 || expectedContentSize > NSUIntegerMax)
+            m_originalData = adoptNS([[NSMutableData alloc] init]);
+        else
+            m_originalData = adoptNS([[NSMutableData alloc] initWithCapacity:(NSUInteger)expectedContentSize]);
     }
 #endif
 }
@@ -136,11 +142,25 @@
     if (!m_neFilterSource)
         return;
 
+    // FIXME: NEFilterSource doesn't buffer data like WebFilterEvaluator does,
+    // so we need to do it ourselves so getReplacementData() can return the
+    // original bytes back to the loader. We should find a way to remove this
+    // additional copy.
+    [m_originalData appendBytes:data length:length];
+
     ref();
-    [m_neFilterSource addData:[NSData dataWithBytesNoCopy:(void*)data length:length freeWhenDone:NO] withCompletionQueue:m_neFilterSourceQueue completionHandler:^(NEFilterSourceStatus status, NSData *) {
-       m_neFilterSourceStatus = status;
-       deref();
+    dispatch_semaphore_t neFilterSourceSemaphore = dispatch_semaphore_create(0);
+    [m_neFilterSource addData:[NSData dataWithBytes:(void*)data length:length] withCompletionQueue:m_neFilterSourceQueue completionHandler:^(NEFilterSourceStatus status, NSData *) {
+        m_neFilterSourceStatus = status;
+        deref();
+        dispatch_semaphore_signal(neFilterSourceSemaphore);
     }];
+
+    // FIXME: We have to block here since DocumentLoader expects to have a
+    // blocked/not blocked answer from the filter immediately after calling
+    // addData(). We should find a way to make this asynchronous.
+    dispatch_semaphore_wait(neFilterSourceSemaphore, DISPATCH_TIME_FOREVER);
+    dispatch_release(neFilterSourceSemaphore);
 #endif
 }
     
@@ -158,19 +178,18 @@
         return;
 
     ref();
-    m_neFilterSourceSemaphore = dispatch_semaphore_create(0);
+    dispatch_semaphore_t neFilterSourceSemaphore = dispatch_semaphore_create(0);
     [m_neFilterSource dataCompleteWithCompletionQueue:m_neFilterSourceQueue completionHandler:^(NEFilterSourceStatus status, NSData *) {
         m_neFilterSourceStatus = status;
         deref();
+        dispatch_semaphore_signal(neFilterSourceSemaphore);
+    }];
 
-        dispatch_semaphore_signal(m_neFilterSourceSemaphore);
-    }];
-    
     // FIXME: We have to block here since DocumentLoader expects to have a
     // blocked/not blocked answer from the filter immediately after calling
     // finishedAddingData(). We should find a way to make this asynchronous.
-    dispatch_semaphore_wait(m_neFilterSourceSemaphore, DISPATCH_TIME_FOREVER);
-    dispatch_release(m_neFilterSourceSemaphore);
+    dispatch_semaphore_wait(neFilterSourceSemaphore, DISPATCH_TIME_FOREVER);
+    dispatch_release(neFilterSourceSemaphore);
 #endif
 
     ASSERT(!needsMoreData());
@@ -196,11 +215,21 @@
 
 const char* ContentFilter::getReplacementData(int& length) const
 {
-    // FIXME: This will return a null pointer with length 0 when using
-    // NEFilterSource. We need to show a proper error page instead.
     ASSERT(!needsMoreData());
-    length = [m_replacementData length];
-    return static_cast<const char*>([m_replacementData bytes]);
+
+    if (didBlockData()) {
+        length = [m_replacementData length];
+        return static_cast<const char*>([m_replacementData bytes]);
+    }
+
+    NSData *originalData = m_replacementData.get();
+#if HAVE(NE_FILTER_SOURCE)
+    if (!originalData)
+        originalData = m_originalData.get();
+#endif
+
+    length = [originalData length];
+    return static_cast<const char*>([originalData bytes]);
 }
 
 } // namespace WebCore
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to