Changeset 224982 in webkit


Ignore:
Timestamp:
Nov 17, 2017 1:33:28 PM (6 years ago)
Author:
achristensen@apple.com
Message:

Use RunLoop and Mode from NetworkingContext if they are given
https://bugs.webkit.org/show_bug.cgi?id=179800
<rdar://problem/35519421>

Reviewed by Brady Eidson.

  • platform/network/mac/WebCoreResourceHandleAsOperationQueueDelegate.mm:

(callOnMainThreadOrSchedule):
(-[WebCoreResourceHandleAsOperationQueueDelegate connection:willSendRequest:redirectResponse:]):
(-[WebCoreResourceHandleAsOperationQueueDelegate connection:didReceiveAuthenticationChallenge:]):
(-[WebCoreResourceHandleAsOperationQueueDelegate connection:canAuthenticateAgainstProtectionSpace:]):
(-[WebCoreResourceHandleAsOperationQueueDelegate connection:didReceiveResponse:]):
(-[WebCoreResourceHandleAsOperationQueueDelegate connection:didReceiveData:lengthReceived:]):
(-[WebCoreResourceHandleAsOperationQueueDelegate connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:]):
(-[WebCoreResourceHandleAsOperationQueueDelegate connectionDidFinishLoading:]):
(-[WebCoreResourceHandleAsOperationQueueDelegate connection:didFailWithError:]):
(-[WebCoreResourceHandleAsOperationQueueDelegate connection:willCacheResponse:]):

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r224977 r224982  
     12017-11-17  Alex Christensen  <achristensen@webkit.org>
     2
     3        Use RunLoop and Mode from NetworkingContext if they are given
     4        https://bugs.webkit.org/show_bug.cgi?id=179800
     5        <rdar://problem/35519421>
     6
     7        Reviewed by Brady Eidson.
     8
     9        * platform/network/mac/WebCoreResourceHandleAsOperationQueueDelegate.mm:
     10        (callOnMainThreadOrSchedule):
     11        (-[WebCoreResourceHandleAsOperationQueueDelegate connection:willSendRequest:redirectResponse:]):
     12        (-[WebCoreResourceHandleAsOperationQueueDelegate connection:didReceiveAuthenticationChallenge:]):
     13        (-[WebCoreResourceHandleAsOperationQueueDelegate connection:canAuthenticateAgainstProtectionSpace:]):
     14        (-[WebCoreResourceHandleAsOperationQueueDelegate connection:didReceiveResponse:]):
     15        (-[WebCoreResourceHandleAsOperationQueueDelegate connection:didReceiveData:lengthReceived:]):
     16        (-[WebCoreResourceHandleAsOperationQueueDelegate connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:]):
     17        (-[WebCoreResourceHandleAsOperationQueueDelegate connectionDidFinishLoading:]):
     18        (-[WebCoreResourceHandleAsOperationQueueDelegate connection:didFailWithError:]):
     19        (-[WebCoreResourceHandleAsOperationQueueDelegate connection:willCacheResponse:]):
     20
    1212017-11-17  Simon Fraser  <simon.fraser@apple.com>
    222
  • trunk/Source/WebCore/platform/network/mac/WebCoreResourceHandleAsOperationQueueDelegate.mm

    r224969 r224982  
    3838#import "WebCoreURLResponse.h"
    3939#import <pal/spi/cf/CFNetworkSPI.h>
     40#import <wtf/BlockPtr.h>
    4041#import <wtf/MainThread.h>
    4142
    4243using namespace WebCore;
    4344
    44 static SchedulePairHashSet* scheduledRunLoopPairs(ResourceHandle* handle)
    45 {
    46     if (!handle)
    47         return nullptr;
    48     if (!handle->context())
    49         return nullptr;
    50     return handle->context()->scheduledRunLoopPairs();
     45static bool scheduledWithCustomRunLoopMode(SchedulePairHashSet* pairs)
     46{
     47    if (!pairs)
     48        return false;
     49    for (auto& pair : *pairs) {
     50        auto mode = pair->mode();
     51        if (mode != kCFRunLoopCommonModes && mode != kCFRunLoopDefaultMode)
     52            return true;
     53    }
     54    return false;
    5155}
    5256
    5357@implementation WebCoreResourceHandleAsOperationQueueDelegate
     58
     59- (void)callFunctionOnMainThread:(Function<void()>&&)function
     60{
     61    // Sync xhr uses the message queue.
     62    if (m_messageQueue)
     63        return m_messageQueue->append(std::make_unique<Function<void()>>(WTFMove(function)));
     64
     65    // This is the common case.
     66    SchedulePairHashSet* pairs = m_handle && m_handle->context() ? m_handle->context()->scheduledRunLoopPairs() : nullptr;
     67    if (!scheduledWithCustomRunLoopMode(pairs))
     68        return callOnMainThread(WTFMove(function));
     69   
     70    // If we have been scheduled in a custom run loop mode, schedule a block in that mode.
     71    auto block = BlockPtr<void()>::fromCallable([alreadyCalled = false, function = WTFMove(function)] () mutable {
     72        if (alreadyCalled)
     73            return;
     74        alreadyCalled = true;
     75        function();
     76        function = nullptr;
     77    });
     78    for (auto& pair : *pairs)
     79        CFRunLoopPerformBlock(pair->runLoop(), pair->mode(), block.get());
     80}
    5481
    5582- (id)initWithHandle:(ResourceHandle*)handle messageQueue:(MessageQueue<Function<void()>>*)messageQueue
     
    117144#endif
    118145
    119     auto work = [self = self, protectedSelf = RetainPtr<id>(self), newRequest = RetainPtr<NSURLRequest>(newRequest), redirectResponse = RetainPtr<NSURLResponse>(redirectResponse)] () mutable {
     146    auto work = [self = self, protectedSelf = retainPtr(self), newRequest = retainPtr(newRequest), redirectResponse = retainPtr(redirectResponse)] () mutable {
    120147        if (!m_handle) {
    121148            m_requestResult = nullptr;
     
    130157    };
    131158
    132     if (m_messageQueue)
    133         m_messageQueue->append(std::make_unique<Function<void()>>(WTFMove(work)));
    134     else
    135         callOnMainThread(WTFMove(work), scheduledRunLoopPairs(m_handle));
    136 
     159    [self callFunctionOnMainThread:WTFMove(work)];
    137160    dispatch_semaphore_wait(m_semaphore, DISPATCH_TIME_FOREVER);
    138161    return m_requestResult.get();
     
    146169    LOG(Network, "Handle %p delegate connection:%p didReceiveAuthenticationChallenge:%p", m_handle, connection, challenge);
    147170
    148     auto work = [self, protectedSelf = RetainPtr<id>(self), challenge = RetainPtr<NSURLAuthenticationChallenge>(challenge)] () mutable {
     171    auto work = [self, protectedSelf = retainPtr(self), challenge = retainPtr(challenge)] () mutable {
    149172        if (!m_handle) {
    150173            [[challenge sender] cancelAuthenticationChallenge:challenge.get()];
     
    154177    };
    155178
    156     if (m_messageQueue)
    157         m_messageQueue->append(std::make_unique<Function<void()>>(WTFMove(work)));
    158     else
    159         callOnMainThread(WTFMove(work), scheduledRunLoopPairs(m_handle));
     179    [self callFunctionOnMainThread:WTFMove(work)];
    160180}
    161181
     
    167187    LOG(Network, "Handle %p delegate connection:%p canAuthenticateAgainstProtectionSpace:%@://%@:%u realm:%@ method:%@ %@%@", m_handle, connection, [protectionSpace protocol], [protectionSpace host], [protectionSpace port], [protectionSpace realm], [protectionSpace authenticationMethod], [protectionSpace isProxy] ? @"proxy:" : @"", [protectionSpace isProxy] ? [protectionSpace proxyType] : @"");
    168188
    169     auto work = [self = self, protectedSelf = RetainPtr<id>(self), protectionSpace = RetainPtr<NSURLProtectionSpace>(protectionSpace)] () mutable {
     189    auto work = [self = self, protectedSelf = retainPtr(self), protectionSpace = retainPtr(protectionSpace)] () mutable {
    170190        if (!m_handle) {
    171191            m_boolResult = NO;
     
    176196    };
    177197
    178     if (m_messageQueue)
    179         m_messageQueue->append(std::make_unique<Function<void()>>(WTFMove(work)));
    180     else
    181         callOnMainThread(WTFMove(work), scheduledRunLoopPairs(m_handle));
    182 
     198    [self callFunctionOnMainThread:WTFMove(work)];
    183199    dispatch_semaphore_wait(m_semaphore, DISPATCH_TIME_FOREVER);
    184200    return m_boolResult;
     
    191207    LOG(Network, "Handle %p delegate connection:%p didReceiveResponse:%p (HTTP status %d, reported MIMEType '%s')", m_handle, connection, r, [r respondsToSelector:@selector(statusCode)] ? [(id)r statusCode] : 0, [[r MIMEType] UTF8String]);
    192208
    193     auto work = [self = self, protectedSelf = RetainPtr<id>(self), r = RetainPtr<NSURLResponse>(r), connection = RetainPtr<NSURLConnection>(connection)] () mutable {
     209    auto work = [self = self, protectedSelf = retainPtr(self), r = retainPtr(r), connection = retainPtr(connection)] () mutable {
    194210        RefPtr<ResourceHandle> protectedHandle(m_handle);
    195211        if (!m_handle || !m_handle->client()) {
     
    215231    };
    216232
    217     if (m_messageQueue)
    218         m_messageQueue->append(std::make_unique<Function<void()>>(WTFMove(work)));
    219     else
    220         callOnMainThread(WTFMove(work), scheduledRunLoopPairs(m_handle));
    221 
     233    [self callFunctionOnMainThread:WTFMove(work)];
    222234    dispatch_semaphore_wait(m_semaphore, DISPATCH_TIME_FOREVER);
    223235}
     
    231243    LOG(Network, "Handle %p delegate connection:%p didReceiveData:%p lengthReceived:%lld", m_handle, connection, data, lengthReceived);
    232244
    233     auto work = [self = self, protectedSelf = RetainPtr<id>(self), data = RetainPtr<NSData>(data)] () mutable {
     245    auto work = [self = self, protectedSelf = retainPtr(self), data = retainPtr(data)] () mutable {
    234246        if (!m_handle || !m_handle->client())
    235247            return;
     
    244256    };
    245257
    246     if (m_messageQueue)
    247         m_messageQueue->append(std::make_unique<Function<void()>>(WTFMove(work)));
    248     else
    249         callOnMainThread(WTFMove(work), scheduledRunLoopPairs(m_handle));
     258    [self callFunctionOnMainThread:WTFMove(work)];
    250259}
    251260
     
    258267    LOG(Network, "Handle %p delegate connection:%p didSendBodyData:%d totalBytesWritten:%d totalBytesExpectedToWrite:%d", m_handle, connection, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
    259268
    260     auto work = [self = self, protectedSelf = RetainPtr<id>(self), totalBytesWritten = totalBytesWritten, totalBytesExpectedToWrite = totalBytesExpectedToWrite] () mutable {
     269    auto work = [self = self, protectedSelf = retainPtr(self), totalBytesWritten = totalBytesWritten, totalBytesExpectedToWrite = totalBytesExpectedToWrite] () mutable {
    261270        if (!m_handle || !m_handle->client())
    262271            return;
     
    264273    };
    265274
    266     if (m_messageQueue)
    267         m_messageQueue->append(std::make_unique<Function<void()>>(WTFMove(work)));
    268     else
    269         callOnMainThread(WTFMove(work), scheduledRunLoopPairs(m_handle));
     275    [self callFunctionOnMainThread:WTFMove(work)];
    270276}
    271277
     
    277283    LOG(Network, "Handle %p delegate connectionDidFinishLoading:%p", m_handle, connection);
    278284
    279     auto work = [self = self, protectedSelf = RetainPtr<id>(self)] () mutable {
     285    auto work = [self = self, protectedSelf = retainPtr(self)] () mutable {
    280286        if (!m_handle || !m_handle->client())
    281287            return;
     
    288294    };
    289295
    290     if (m_messageQueue)
    291         m_messageQueue->append(std::make_unique<Function<void()>>(WTFMove(work)));
    292     else
    293         callOnMainThread(WTFMove(work), scheduledRunLoopPairs(m_handle));
     296    [self callFunctionOnMainThread:WTFMove(work)];
    294297}
    295298
     
    301304    LOG(Network, "Handle %p delegate connection:%p didFailWithError:%@", m_handle, connection, error);
    302305
    303     auto work = [self = self, protectedSelf = RetainPtr<id>(self), error = RetainPtr<NSError>(error)] () mutable {
     306    auto work = [self = self, protectedSelf = retainPtr(self), error = retainPtr(error)] () mutable {
    304307        if (!m_handle || !m_handle->client())
    305308            return;
     
    312315    };
    313316
    314     if (m_messageQueue)
    315         m_messageQueue->append(std::make_unique<Function<void()>>(WTFMove(work)));
    316     else
    317         callOnMainThread(WTFMove(work), scheduledRunLoopPairs(m_handle));
     317    [self callFunctionOnMainThread:WTFMove(work)];
    318318}
    319319
     
    326326    LOG(Network, "Handle %p delegate connection:%p willCacheResponse:%p", m_handle, connection, cachedResponse);
    327327
    328     auto work = [self = self, protectedSelf = RetainPtr<id>(self), cachedResponse = RetainPtr<NSCachedURLResponse>(cachedResponse)] () mutable {
     328    auto work = [self = self, protectedSelf = retainPtr(self), cachedResponse = retainPtr(cachedResponse)] () mutable {
    329329        if (!m_handle || !m_handle->client()) {
    330330            m_cachedResponseResult = nullptr;
     
    336336    };
    337337
    338     if (m_messageQueue)
    339         m_messageQueue->append(std::make_unique<Function<void()>>(WTFMove(work)));
    340     else
    341         callOnMainThread(WTFMove(work), scheduledRunLoopPairs(m_handle));
    342 
     338    [self callFunctionOnMainThread:WTFMove(work)];
    343339    dispatch_semaphore_wait(m_semaphore, DISPATCH_TIME_FOREVER);
    344340    return m_cachedResponseResult.get();
  • trunk/Tools/TestWebKitAPI/Tests/mac/WebViewScheduleInRunLoop.mm

    r224896 r224982  
    6464
    6565    while (loadsFinished < webViewCount)
    66         CFRunLoopRunInMode(CFSTR("TestRunLoopMode"), std::numeric_limits<double>::max(), true);
     66        CFRunLoopRunInMode(CFSTR("TestRunLoopMode"), .001, true);
    6767}
    6868
Note: See TracChangeset for help on using the changeset viewer.