⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 236767 in webkit


Ignore:
Timestamp:
Oct 2, 2018, 3:34:09 PM (8 years ago)
Author:
achristensen@apple.com
Message:

Prepare WebCoreNSURLExtras for ARC
https://bugs.webkit.org/show_bug.cgi?id=190219

Reviewed by Tim Horton.

ARC doesn't like the explicit sending of -release.
Use RetainPtr instead.

  • platform/mac/WebCoreNSURLExtras.mm:

(WebCore::collectRangesThatNeedMapping):
(WebCore::collectRangesThatNeedEncoding):
(WebCore::collectRangesThatNeedDecoding):
(WebCore::applyHostNameFunctionToMailToURLString):
(WebCore::applyHostNameFunctionToURLString):
(WebCore::mapHostNames):
(WebCore::stringByTrimmingWhitespace):
(WebCore::URLWithUserTypedString):
(WebCore::userVisibleString):
(WebCore::rangeOfURLScheme):
(WebCore::looksLikeAbsoluteURL):
(WebCore::retain): Deleted.

Location:
trunk/Source/WebCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r236765 r236767  
     12018-10-02  Alex Christensen  <achristensen@webkit.org>
     2
     3        Prepare WebCoreNSURLExtras for ARC
     4        https://bugs.webkit.org/show_bug.cgi?id=190219
     5
     6        Reviewed by Tim Horton.
     7
     8        ARC doesn't like the explicit sending of -release.
     9        Use RetainPtr instead.
     10
     11        * platform/mac/WebCoreNSURLExtras.mm:
     12        (WebCore::collectRangesThatNeedMapping):
     13        (WebCore::collectRangesThatNeedEncoding):
     14        (WebCore::collectRangesThatNeedDecoding):
     15        (WebCore::applyHostNameFunctionToMailToURLString):
     16        (WebCore::applyHostNameFunctionToURLString):
     17        (WebCore::mapHostNames):
     18        (WebCore::stringByTrimmingWhitespace):
     19        (WebCore::URLWithUserTypedString):
     20        (WebCore::userVisibleString):
     21        (WebCore::rangeOfURLScheme):
     22        (WebCore::looksLikeAbsoluteURL):
     23        (WebCore::retain): Deleted.
     24
    1252018-10-02  Basuke Suzuki  <Basuke.Suzuki@sony.com>
    226
  • trunk/Source/WebCore/platform/mac/WebCoreNSURLExtras.mm

    r236703 r236767  
    4545#define URL_BYTES_BUFFER_LENGTH 2048
    4646
    47 typedef void (* StringRangeApplierFunction)(NSString *string, NSRange range, void *context);
     47typedef void (* StringRangeApplierFunction)(NSString *, NSRange, RetainPtr<NSMutableArray>&);
    4848
    4949static uint32_t IDNScriptWhiteList[(USCRIPT_CODE_LIMIT + 31) / 32];
     
    632632}
    633633
    634 static void collectRangesThatNeedMapping(NSString *string, NSRange range, void *context, BOOL encode)
     634static void collectRangesThatNeedMapping(NSString *string, NSRange range, RetainPtr<NSMutableArray>& array, BOOL encode)
    635635{
    636636    // Generally, we want to optimize for the case where there is one host name that does not need mapping.
     
    642642        return;
    643643   
    644     __strong NSMutableArray **array = (__strong NSMutableArray **)context;
    645     if (!*array)
    646         *array = [[NSMutableArray alloc] init];
    647    
     644    if (!array)
     645        array = adoptNS([NSMutableArray new]);
     646
    648647    if (!error)
    649         [*array addObject:[NSValue valueWithRange:range]];
    650 }
    651 
    652 static void collectRangesThatNeedEncoding(NSString *string, NSRange range, void *context)
    653 {
    654     return collectRangesThatNeedMapping(string, range, context, YES);
    655 }
    656 
    657 static void collectRangesThatNeedDecoding(NSString *string, NSRange range, void *context)
    658 {
    659     return collectRangesThatNeedMapping(string, range, context, NO);
    660 }
    661 
    662 static inline NSCharacterSet *retain(NSCharacterSet *charset)
    663 {
    664     CFRetain(charset);
    665     return charset;
    666 }
    667 
    668 static void applyHostNameFunctionToMailToURLString(NSString *string, StringRangeApplierFunction f, void *context)
     648        [array addObject:[NSValue valueWithRange:range]];
     649}
     650
     651static void collectRangesThatNeedEncoding(NSString *string, NSRange range, RetainPtr<NSMutableArray>& array)
     652{
     653    return collectRangesThatNeedMapping(string, range, array, YES);
     654}
     655
     656static void collectRangesThatNeedDecoding(NSString *string, NSRange range, RetainPtr<NSMutableArray>& array)
     657{
     658    return collectRangesThatNeedMapping(string, range, array, NO);
     659}
     660
     661static void applyHostNameFunctionToMailToURLString(NSString *string, StringRangeApplierFunction f, RetainPtr<NSMutableArray>& array)
    669662{
    670663    // In a mailto: URL, host names come after a '@' character and end with a '>' or ',' or '?' character.
     
    672665    // When we find a '?' character, we are past the part of the URL that contains host names.
    673666   
    674     static NSCharacterSet *hostNameOrStringStartCharacters = retain([NSCharacterSet characterSetWithCharactersInString:@"\"@?"]);
    675     static NSCharacterSet *hostNameEndCharacters = retain([NSCharacterSet characterSetWithCharactersInString:@">,?"]);
    676     static NSCharacterSet *quotedStringCharacters = retain([NSCharacterSet characterSetWithCharactersInString:@"\"\\"]);
     667    static NeverDestroyed<RetainPtr<NSCharacterSet>> hostNameOrStringStartCharacters = [NSCharacterSet characterSetWithCharactersInString:@"\"@?"];
     668    static NeverDestroyed<RetainPtr<NSCharacterSet>> hostNameEndCharacters = [NSCharacterSet characterSetWithCharactersInString:@">,?"];
     669    static NeverDestroyed<RetainPtr<NSCharacterSet>> quotedStringCharacters = [NSCharacterSet characterSetWithCharactersInString:@"\"\\"];
    677670   
    678671    unsigned stringLength = [string length];
     
    681674    while (1) {
    682675        // Find start of host name or of quoted string.
    683         NSRange hostNameOrStringStart = [string rangeOfCharacterFromSet:hostNameOrStringStartCharacters options:0 range:remaining];
     676        NSRange hostNameOrStringStart = [string rangeOfCharacterFromSet:hostNameOrStringStartCharacters.get().get() options:0 range:remaining];
    684677        if (hostNameOrStringStart.location == NSNotFound)
    685678            return;
     
    695688            // Find end of host name.
    696689            unsigned hostNameStart = remaining.location;
    697             NSRange hostNameEnd = [string rangeOfCharacterFromSet:hostNameEndCharacters options:0 range:remaining];
     690            NSRange hostNameEnd = [string rangeOfCharacterFromSet:hostNameEndCharacters.get().get() options:0 range:remaining];
    698691            BOOL done;
    699692            if (hostNameEnd.location == NSNotFound) {
     
    707700           
    708701            // Process host name range.
    709             f(string, NSMakeRange(hostNameStart, hostNameEnd.location - hostNameStart), context);
     702            f(string, NSMakeRange(hostNameStart, hostNameEnd.location - hostNameStart), array);
    710703           
    711704            if (done)
     
    715708            ASSERT(c == '"');
    716709            while (1) {
    717                 NSRange escapedCharacterOrStringEnd = [string rangeOfCharacterFromSet:quotedStringCharacters options:0 range:remaining];
     710                NSRange escapedCharacterOrStringEnd = [string rangeOfCharacterFromSet:quotedStringCharacters.get().get() options:0 range:remaining];
    718711                if (escapedCharacterOrStringEnd.location == NSNotFound)
    719712                    return;
     
    739732}
    740733
    741 static void applyHostNameFunctionToURLString(NSString *string, StringRangeApplierFunction f, void *context)
     734static void applyHostNameFunctionToURLString(NSString *string, StringRangeApplierFunction f, RetainPtr<NSMutableArray>& array)
    742735{
    743736    // Find hostnames. Too bad we can't use any real URL-parsing code to do this,
     
    748741   
    749742    if (protocolIs(string, "mailto")) {
    750         applyHostNameFunctionToMailToURLString(string, f, context);
     743        applyHostNameFunctionToMailToURLString(string, f, array);
    751744        return;
    752745    }
     
    761754   
    762755    // Check that all characters before the :// are valid scheme characters.
    763     static NSCharacterSet *nonSchemeCharacters = retain([[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-."] invertedSet]);
    764     if ([string rangeOfCharacterFromSet:nonSchemeCharacters options:0 range:NSMakeRange(0, separatorRange.location)].location != NSNotFound)
     756    static NeverDestroyed<RetainPtr<NSCharacterSet>> nonSchemeCharacters = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-."] invertedSet];
     757    if ([string rangeOfCharacterFromSet:nonSchemeCharacters.get().get() options:0 range:NSMakeRange(0, separatorRange.location)].location != NSNotFound)
    765758        return;
    766759   
    767760    unsigned stringLength = [string length];
    768761   
    769     static NSCharacterSet *hostTerminators = retain([NSCharacterSet characterSetWithCharactersInString:@":/?#"]);
     762    static NeverDestroyed<RetainPtr<NSCharacterSet>> hostTerminators = [NSCharacterSet characterSetWithCharactersInString:@":/?#"];
    770763   
    771764    // Start after the separator.
     
    773766   
    774767    // Find terminating character.
    775     NSRange hostNameTerminator = [string rangeOfCharacterFromSet:hostTerminators options:0 range:NSMakeRange(authorityStart, stringLength - authorityStart)];
     768    NSRange hostNameTerminator = [string rangeOfCharacterFromSet:hostTerminators.get().get() options:0 range:NSMakeRange(authorityStart, stringLength - authorityStart)];
    776769    unsigned hostNameEnd = hostNameTerminator.location == NSNotFound ? stringLength : hostNameTerminator.location;
    777770   
     
    780773    unsigned hostNameStart = userInfoTerminator.location == NSNotFound ? authorityStart : NSMaxRange(userInfoTerminator);
    781774   
    782     f(string, NSMakeRange(hostNameStart, hostNameEnd - hostNameStart), context);
    783 }
    784 
    785 static NSString *mapHostNames(NSString *string, BOOL encode)
     775    return f(string, NSMakeRange(hostNameStart, hostNameEnd - hostNameStart), array);
     776}
     777
     778static RetainPtr<NSString> mapHostNames(NSString *string, BOOL encode)
    786779{
    787780    // Generally, we want to optimize for the case where there is one host name that does not need mapping.
     
    791784   
    792785    // Make a list of ranges that actually need mapping.
    793     NSMutableArray *hostNameRanges = nil;
     786    RetainPtr<NSMutableArray> hostNameRanges;
    794787    StringRangeApplierFunction f = encode ? collectRangesThatNeedEncoding : collectRangesThatNeedDecoding;
    795     applyHostNameFunctionToURLString(string, f, &hostNameRanges);
     788    applyHostNameFunctionToURLString(string, f, hostNameRanges);
    796789    if (!hostNameRanges)
    797790        return string;
    798791
    799     if (![hostNameRanges count]) {
    800         [hostNameRanges release];
    801         return nil;
    802     }
     792    if (![hostNameRanges count])
     793        return nil;
    803794   
    804795    // Do the mapping.
    805     NSMutableString *mutableCopy = [string mutableCopy];
     796    auto mutableCopy = adoptNS([string mutableCopy]);
    806797    unsigned i = [hostNameRanges count];
    807798    while (i--) {
     
    810801        [mutableCopy replaceCharactersInRange:hostNameRange withString:mappedHostName];
    811802    }
    812     [hostNameRanges release];
    813     return [mutableCopy autorelease];
    814 }
    815 
    816 static NSString *stringByTrimmingWhitespace(NSString *string)
    817 {
    818     NSMutableString *trimmed = [[string mutableCopy] autorelease];
    819     CFStringTrimWhitespace((__bridge CFMutableStringRef)trimmed);
     803    return mutableCopy;
     804}
     805
     806static RetainPtr<NSString> stringByTrimmingWhitespace(NSString *string)
     807{
     808    auto trimmed = adoptNS([string mutableCopy]);
     809    CFStringTrimWhitespace((__bridge CFMutableStringRef)trimmed.get());
    820810    return trimmed;
    821811}
     
    913903        return nil;
    914904
    915     string = mapHostNames(stringByTrimmingWhitespace(string), YES);
    916     if (!string)
     905    auto mappedString = mapHostNames(stringByTrimmingWhitespace(string).get(), YES);
     906    if (!mappedString)
    917907        return nil;
    918908
    919909    // Let's check whether the URL is bogus.
    920     URL url { URL { nsURL }, string };
     910    URL url { URL { nsURL }, mappedString.get() };
    921911    if (!url.createCFURL())
    922912        return nil;
     
    924914    // FIXME: https://bugs.webkit.org/show_bug.cgi?id=186057
    925915    // We should be able to use url.createCFURL instead of using directly CFURL parsing routines.
    926     NSData *data = dataWithUserTypedString(string);
     916    NSData *data = dataWithUserTypedString(mappedString.get());
    927917    if (!data)
    928918        return [NSURL URLWithString:@""];
     
    11591149   
    11601150    // Check string to see if it can be converted to display using UTF-8 
    1161     NSString *result = [NSString stringWithUTF8String:after.data()];
     1151    RetainPtr<NSString> result = [NSString stringWithUTF8String:after.data()];
    11621152    if (!result) {
    11631153        // Could not convert to UTF-8.
     
    11861176    if (mayNeedHostNameDecoding) {
    11871177        // FIXME: Is it good to ignore the failure of mapHostNames and keep result intact?
    1188         NSString *mappedResult = mapHostNames(result, NO);
     1178        auto mappedResult = mapHostNames(result.get(), NO);
    11891179        if (mappedResult)
    11901180            result = mappedResult;
     
    11921182
    11931183    result = [result precomposedStringWithCanonicalMapping];
    1194     return CFBridgingRelease(createStringWithEscapedUnsafeCharacters((__bridge CFStringRef)result));
     1184    return CFBridgingRelease(createStringWithEscapedUnsafeCharacters((__bridge CFStringRef)result.get()));
    11951185}
    11961186
     
    12391229    if (colon.location != NSNotFound && colon.location > 0) {
    12401230        NSRange scheme = {0, colon.location};
    1241         static NSCharacterSet *InverseSchemeCharacterSet = nil;
    1242         if (!InverseSchemeCharacterSet) {
    1243             /*
    1244              This stuff is very expensive.  10-15 msec on a 2x1.2GHz.  If not cached it swamps
    1245              everything else when adding items to the autocomplete DB.  Makes me wonder if we
    1246              even need to enforce the character set here.
    1247             */
    1248             NSString *acceptableCharacters = @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+.-";
    1249             InverseSchemeCharacterSet = [[[NSCharacterSet characterSetWithCharactersInString:acceptableCharacters] invertedSet] retain];
    1250         }
    1251         NSRange illegals = [string rangeOfCharacterFromSet:InverseSchemeCharacterSet options:0 range:scheme];
     1231        /*
     1232         This stuff is very expensive.  10-15 msec on a 2x1.2GHz.  If not cached it swamps
     1233         everything else when adding items to the autocomplete DB.  Makes me wonder if we
     1234         even need to enforce the character set here.
     1235         */
     1236        NSString *acceptableCharacters = @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+.-";
     1237        static NeverDestroyed<RetainPtr<NSCharacterSet>> InverseSchemeCharacterSet([[NSCharacterSet characterSetWithCharactersInString:acceptableCharacters] invertedSet]);
     1238        NSRange illegals = [string rangeOfCharacterFromSet:InverseSchemeCharacterSet.get().get() options:0 range:scheme];
    12521239        if (illegals.location == NSNotFound)
    12531240            return scheme;
     
    12591246{
    12601247    // Trim whitespace because _web_URLWithString allows whitespace.
    1261     return rangeOfURLScheme(stringByTrimmingWhitespace(string)).location != NSNotFound;
     1248    return rangeOfURLScheme(stringByTrimmingWhitespace(string).get()).location != NSNotFound;
    12621249}
    12631250
Note: See TracChangeset for help on using the changeset viewer.