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

Changeset 284763 in webkit


Ignore:
Timestamp:
Oct 24, 2021, 11:13:21 AM (5 years ago)
Author:
Darin Adler
Message:

[Cocoa] Adopt bridge_cast and makeVector in a few more places, including cases where adoptCF/NS was used incorrectly
https://bugs.webkit.org/show_bug.cgi?id=232200

Reviewed by Anders Carlsson.

Source/WebCore:

  • platform/cocoa/SharedBufferCocoa.mm:

(WebCore::SharedBuffer::create): Use bridge_cast.
(WebCore::SharedBuffer::append): Ditto.
(WebCore::SharedBuffer::createNSData const): Ditto. Here we are replacing
a leakRef/adoptNS pair, so this fixes an ARC incompatibility.
(WebCore::SharedBuffer::createCFData const): Ditto. Here we are replacing
a leakRef/adoptCF pair, so this fixes an ARC incompatibility.
(WebCore::SharedBuffer::createFromReadingFile): Tweaked coding style.

  • platform/graphics/cocoa/HEVCUtilitiesCocoa.mm:

(WebCore::parseStringArrayFromDictionaryToUInt16Vector): Use dynamic_cf_cast,
dynamic_objc_cast, and makeVector to reduce code without behavior changes.

  • platform/mac/PasteboardWriter.mm:

(WebCore::toUTI): Use bridge_cast.
(WebCore::toUTIUnlessAlreadyUTI): Use bridge_cast after doing adoptCF
rather than first casting and then doing an adoptNS, relying on it doing the
correct thing for a CF object. This fixes an ARC incompatibility.
(WebCore::createPasteboardWriter): Ditto.

Source/WebKit:

  • Shared/Cocoa/ArgumentCodersCocoa.mm:

(-[WKSecureCodingURLWrapper encodeWithCoder:]): Use bridge_cast.
(-[WKSecureCodingURLWrapper initWithCoder:]): Use bridge_cast
after calling adoptCF rather than first doing a bridging cast and
then doing adoptNS and relying on it correctly adopting a CF object.
This fixes an ARC incompatibility.
(IPC::encodeDataInternal): Use bridge_cast.
(IPC::decodeDataInternal): Use bridge_cast/WTFMove instead of
adoptNS/cast/leakRef. This fixes an ARC incompatibility
(IPC::encodeDateInternal): Use bridge_cast.
(IPC::decodeDateInternal): Use bridge_cast/WTFMove as above.
(IPC::encodeNumberInternal): Use bridge_cast.
(IPC::decodeNumberInternal): Use bridge_cast/WTFMove as above.
(IPC::decodeSecureCodingInternal): Use bridge_cast.
(IPC::encodeStringInternal): Use bridge_cast.
(IPC::decodeStringInternal): Use bridge_cast/WTFMove as above.
(IPC::encodeURLInternal): Use bridge_cast.
(IPC::decodeURLInternal): Use bridge_cast/WTFMove as above.

  • UIProcess/API/Cocoa/WKConnection.mm:

(didReceiveMessage): Use bridge_cast and remove unneeded use of
RetainPtr/get on the body.

  • UIProcess/Plugins/mac/PluginInfoStoreMac.mm:

(WebKit::PluginInfoStore::pluginPathsInDirectory): Use bridge_cast
and makeVectort.

Source/WTF:

  • wtf/cocoa/URLCocoa.mm:

(WTF::URL::URL): Use bridge_cast.
(WTF::URL::createCFURL const): Ditto, using the RetainPtr version to avoid
retain count churn.

Location:
trunk/Source
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r284753 r284763  
     12021-10-24  Darin Adler  <darin@apple.com>
     2
     3        [Cocoa] Adopt bridge_cast and makeVector in a few more places, including cases where adoptCF/NS was used incorrectly
     4        https://bugs.webkit.org/show_bug.cgi?id=232200
     5
     6        Reviewed by Anders Carlsson.
     7
     8        * wtf/cocoa/URLCocoa.mm:
     9        (WTF::URL::URL): Use bridge_cast.
     10        (WTF::URL::createCFURL const): Ditto, using the RetainPtr version to avoid
     11        retain count churn.
     12
    1132021-10-23  Alan Bujtas  <zalan@apple.com>
    214
  • trunk/Source/WTF/wtf/cocoa/URLCocoa.mm

    r278619 r284763  
    11/*
    2  * Copyright (C) 2004, 2008 Apple Inc. All rights reserved.
     2 * Copyright (C) 2004-2021 Apple Inc. All rights reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
     
    3030#import <wtf/cf/CFURLExtras.h>
    3131#import <wtf/cocoa/NSURLExtras.h>
     32#import <wtf/cocoa/TypeCastsCocoa.h>
    3233#import <wtf/text/CString.h>
    3334
     
    3839namespace WTF {
    3940
    40 URL::URL(NSURL *url)
     41URL::URL(NSURL *cocoaURL)
    4142{
    42     if (!url) {
     43    if (!cocoaURL) {
    4344        invalidate();
    4445        return;
     
    4647
    4748    // FIXME: Why is it OK to ignore base URL here?
    48     CString urlBytes;
    49     WTF::getURLBytes((__bridge CFURLRef)url, urlBytes);
    50     URLParser parser(urlBytes.data());
     49    CString bytes;
     50    WTF::getURLBytes(bridge_cast(cocoaURL), bytes);
     51    URLParser parser(bytes.data());
    5152    *this = parser.result();
    5253}
     
    6667    if (isEmpty()) {
    6768        // We use the toll-free bridge between NSURL and CFURL to create a CFURLRef supporting both empty and null values.
    68         return (__bridge CFURLRef)adoptNS([[NSURL alloc] initWithString:@""]).get();
     69        return bridge_cast(adoptNS([[NSURL alloc] initWithString:@""]));
    6970    }
    7071
  • trunk/Source/WebCore/ChangeLog

    r284761 r284763  
     12021-10-24  Darin Adler  <darin@apple.com>
     2
     3        [Cocoa] Adopt bridge_cast and makeVector in a few more places, including cases where adoptCF/NS was used incorrectly
     4        https://bugs.webkit.org/show_bug.cgi?id=232200
     5
     6        Reviewed by Anders Carlsson.
     7
     8        * platform/cocoa/SharedBufferCocoa.mm:
     9        (WebCore::SharedBuffer::create): Use bridge_cast.
     10        (WebCore::SharedBuffer::append): Ditto.
     11        (WebCore::SharedBuffer::createNSData const): Ditto. Here we are replacing
     12        a leakRef/adoptNS pair, so this fixes an ARC incompatibility.
     13        (WebCore::SharedBuffer::createCFData const): Ditto. Here we are replacing
     14        a leakRef/adoptCF pair, so this fixes an ARC incompatibility.
     15        (WebCore::SharedBuffer::createFromReadingFile): Tweaked coding style.
     16
     17        * platform/graphics/cocoa/HEVCUtilitiesCocoa.mm:
     18        (WebCore::parseStringArrayFromDictionaryToUInt16Vector): Use dynamic_cf_cast,
     19        dynamic_objc_cast, and makeVector to reduce code without behavior changes.
     20
     21        * platform/mac/PasteboardWriter.mm:
     22        (WebCore::toUTI): Use bridge_cast.
     23        (WebCore::toUTIUnlessAlreadyUTI): Use bridge_cast after doing adoptCF
     24        rather than first casting and then doing an adoptNS, relying on it doing the
     25        correct thing for a CF object. This fixes an ARC incompatibility.
     26        (WebCore::createPasteboardWriter): Ditto.
     27
    1282021-10-24  Alexey Shvayka  <shvaikalesh@gmail.com>
    229
  • trunk/Source/WebCore/platform/cocoa/SharedBufferCocoa.mm

    r267938 r284763  
    11/*
    2  * Copyright (C) 2006-2019 Apple Inc. All rights reserved.
     2 * Copyright (C) 2006-2021 Apple Inc. All rights reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
     
    3232#import <string.h>
    3333#import <wtf/MainThread.h>
     34#import <wtf/cocoa/TypeCastsCocoa.h>
    3435#import <wtf/cocoa/VectorCocoa.h>
    3536
     
    8586namespace WebCore {
    8687
    87 Ref<SharedBuffer> SharedBuffer::create(NSData *nsData)
     88Ref<SharedBuffer> SharedBuffer::create(NSData *data)
    8889{
    89     return adoptRef(*new SharedBuffer((__bridge CFDataRef)nsData));
     90    return adoptRef(*new SharedBuffer(bridge_cast(data)));
    9091}
    9192
    92 void SharedBuffer::append(NSData *nsData)
     93void SharedBuffer::append(NSData *data)
    9394{
    94     return append((__bridge CFDataRef)nsData);
     95    return append(bridge_cast(data));
    9596}
    9697
    9798RetainPtr<NSData> SharedBuffer::createNSData() const
    9899{
    99     return adoptNS((NSData *)createCFData().leakRef());
     100    return bridge_cast(createCFData());
    100101}
    101102
     
    106107        return adoptCF(CFDataCreate(nullptr, nullptr, 0));
    107108    ASSERT(m_segments.size() == 1);
    108     return adoptCF((__bridge CFDataRef)m_segments[0].segment->createNSData().leakRef());
     109    return bridge_cast(m_segments[0].segment->createNSData());
    109110}
    110111
    111112RefPtr<SharedBuffer> SharedBuffer::createFromReadingFile(const String& filePath)
    112113{
    113     NSData *resourceData = [NSData dataWithContentsOfFile:filePath];
    114     if (resourceData)
     114    if (auto resourceData = [NSData dataWithContentsOfFile:filePath])
    115115        return SharedBuffer::create(resourceData);
    116116    return nullptr;
  • trunk/Source/WebCore/platform/graphics/cocoa/HEVCUtilitiesCocoa.mm

    r281791 r284763  
    3333#import "MediaCapabilitiesInfo.h"
    3434#import <wtf/cf/TypeCastsCF.h>
     35#import <wtf/cocoa/TypeCastsCocoa.h>
     36#import <wtf/cocoa/VectorCocoa.h>
    3537#import <wtf/text/StringToIntegerConversion.h>
    3638
     
    137139static std::optional<Vector<uint16_t>> parseStringArrayFromDictionaryToUInt16Vector(CFDictionaryRef dictionary, const void* key)
    138140{
    139     auto value = CFDictionaryGetValue(dictionary, key);
    140     if (!value || CFGetTypeID(value) != CFArrayGetTypeID())
     141    auto array = dynamic_cf_cast<CFArrayRef>(CFDictionaryGetValue(dictionary, key));
     142    if (!array)
    141143        return std::nullopt;
    142     NSArray *array = (__bridge NSArray *)value;
    143     Vector<uint16_t> vector;
    144     vector.reserveInitialCapacity(array.count);
    145     for (id value in array) {
    146         if (![value isKindOfClass:NSString.class])
    147             return std::nullopt;
    148         auto numericValue = parseInteger<uint16_t>(String((NSString *)value));
    149         if (!numericValue)
    150             return std::nullopt;
    151         vector.uncheckedAppend(*numericValue);
    152     }
    153     return vector;
     144    return makeVector(bridge_cast(array), [] (id value) {
     145        return parseInteger<uint16_t>(String(dynamic_objc_cast<NSString>(value)));
     146    });
    154147}
    155148
  • trunk/Source/WebCore/platform/mac/PasteboardWriter.mm

    r269869 r284763  
    3434#import "SharedBuffer.h"
    3535#import <pal/spi/mac/NSPasteboardSPI.h>
     36#import <wtf/cocoa/TypeCastsCocoa.h>
    3637
    3738namespace WebCore {
     
    4041{
    4142ALLOW_DEPRECATED_DECLARATIONS_BEGIN
    42     return adoptNS((__bridge NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassNSPboardType, (__bridge CFStringRef)pasteboardType, nullptr));
     43    return bridge_cast(adoptCF(UTTypeCreatePreferredIdentifierForTag(kUTTagClassNSPboardType, bridge_cast(pasteboardType), nullptr)));
    4344ALLOW_DEPRECATED_DECLARATIONS_END
    4445}
     
    4748{
    4849ALLOW_DEPRECATED_DECLARATIONS_BEGIN
    49     if (UTTypeIsDeclared((__bridge CFStringRef)type) || UTTypeIsDynamic((__bridge CFStringRef)type)) {
     50    if (UTTypeIsDeclared(bridge_cast(type)) || UTTypeIsDynamic(bridge_cast(type))) {
    5051        // This is already a UTI.
    5152        return type;
     
    6364        [pasteboardItem setString:plainText->text forType:NSPasteboardTypeString];
    6465        if (plainText->canSmartCopyOrDelete) {
    65 ALLOW_DEPRECATED_DECLARATIONS_BEGIN
    66             auto smartPasteType = adoptNS((__bridge NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassNSPboardType, (__bridge CFStringRef)_NXSmartPaste, nullptr));
    67 ALLOW_DEPRECATED_DECLARATIONS_END
     66            ALLOW_DEPRECATED_DECLARATIONS_BEGIN
     67            auto smartPasteType = bridge_cast(adoptCF(UTTypeCreatePreferredIdentifierForTag(kUTTagClassNSPboardType, bridge_cast(_NXSmartPaste), nullptr)));
     68            ALLOW_DEPRECATED_DECLARATIONS_END
    6869            [pasteboardItem setData:[NSData data] forType:smartPasteType.get()];
    6970        }
     
    107108    if (auto& webContent = data.webContent()) {
    108109        if (webContent->canSmartCopyOrDelete) {
    109 ALLOW_DEPRECATED_DECLARATIONS_BEGIN
    110             auto smartPasteType = adoptNS((__bridge NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassNSPboardType, (__bridge CFStringRef)_NXSmartPaste, nullptr));
    111 ALLOW_DEPRECATED_DECLARATIONS_END
     110            ALLOW_DEPRECATED_DECLARATIONS_BEGIN
     111            auto smartPasteType = bridge_cast(adoptCF(UTTypeCreatePreferredIdentifierForTag(kUTTagClassNSPboardType, bridge_cast(_NXSmartPaste), nullptr)));
     112            ALLOW_DEPRECATED_DECLARATIONS_END
    112113            [pasteboardItem setData:[NSData data] forType:smartPasteType.get()];
    113114        }
    114115        if (webContent->dataInWebArchiveFormat) {
    115 ALLOW_DEPRECATED_DECLARATIONS_BEGIN
    116             auto webArchiveType = adoptNS((__bridge NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassNSPboardType, (__bridge CFStringRef)@"Apple Web Archive pasteboard type", nullptr));
    117 ALLOW_DEPRECATED_DECLARATIONS_END
     116            ALLOW_DEPRECATED_DECLARATIONS_BEGIN
     117            auto webArchiveType = bridge_cast(adoptCF(UTTypeCreatePreferredIdentifierForTag(kUTTagClassNSPboardType, CFSTR("Apple Web Archive pasteboard type"), nullptr)));
     118            ALLOW_DEPRECATED_DECLARATIONS_END
    118119            [pasteboardItem setData:webContent->dataInWebArchiveFormat->createNSData().get() forType:webArchiveType.get()];
    119120        }
  • trunk/Source/WebKit/ChangeLog

    r284750 r284763  
     12021-10-24  Darin Adler  <darin@apple.com>
     2
     3        [Cocoa] Adopt bridge_cast and makeVector in a few more places, including cases where adoptCF/NS was used incorrectly
     4        https://bugs.webkit.org/show_bug.cgi?id=232200
     5
     6        Reviewed by Anders Carlsson.
     7
     8        * Shared/Cocoa/ArgumentCodersCocoa.mm:
     9        (-[WKSecureCodingURLWrapper encodeWithCoder:]): Use bridge_cast.
     10        (-[WKSecureCodingURLWrapper initWithCoder:]): Use bridge_cast
     11        after calling adoptCF rather than first doing a bridging cast and
     12        then doing adoptNS and relying on it correctly adopting a CF object.
     13        This fixes an ARC incompatibility.
     14        (IPC::encodeDataInternal): Use bridge_cast.
     15        (IPC::decodeDataInternal): Use bridge_cast/WTFMove instead of
     16        adoptNS/cast/leakRef. This fixes an ARC incompatibility
     17        (IPC::encodeDateInternal): Use bridge_cast.
     18        (IPC::decodeDateInternal): Use bridge_cast/WTFMove as above.
     19        (IPC::encodeNumberInternal): Use bridge_cast.
     20        (IPC::decodeNumberInternal): Use bridge_cast/WTFMove as above.
     21        (IPC::decodeSecureCodingInternal): Use bridge_cast.
     22        (IPC::encodeStringInternal): Use bridge_cast.
     23        (IPC::decodeStringInternal): Use bridge_cast/WTFMove as above.
     24        (IPC::encodeURLInternal): Use bridge_cast.
     25        (IPC::decodeURLInternal): Use bridge_cast/WTFMove as above.
     26
     27        * UIProcess/API/Cocoa/WKConnection.mm:
     28        (didReceiveMessage): Use bridge_cast and remove unneeded use of
     29        RetainPtr/get on the body.
     30
     31        * UIProcess/Plugins/mac/PluginInfoStoreMac.mm:
     32        (WebKit::PluginInfoStore::pluginPathsInDirectory): Use bridge_cast
     33        and makeVectort.
     34
    1352021-10-23  Dean Jackson  <dino@apple.com>
    236
  • trunk/Source/WebKit/Shared/Cocoa/ArgumentCodersCocoa.mm

    r284630 r284763  
    106106
    107107    WTF::URLCharBuffer urlBytes;
    108     WTF::getURLBytes((__bridge CFURLRef)m_wrappedURL.get(), urlBytes);
     108    WTF::getURLBytes(bridge_cast(m_wrappedURL.get()), urlBytes);
    109109    [coder encodeBytes:urlBytes.data() length:urlBytes.size()];
    110110}
     
    125125    NSUInteger length;
    126126    if (auto bytes = (UInt8 *)[coder decodeBytesWithReturnedLength:&length]) {
    127         m_wrappedURL = adoptNS((__bridge NSURL*)CFURLCreateAbsoluteURLWithBytes(nullptr, bytes, length, kCFStringEncodingUTF8, (__bridge CFURLRef)baseURL.get(), true));
     127        m_wrappedURL = bridge_cast(adoptCF(CFURLCreateAbsoluteURLWithBytes(nullptr, bytes, length, kCFStringEncodingUTF8, bridge_cast(baseURL.get()), true)));
    128128        if (!m_wrappedURL)
    129129            LOG_ERROR("Failed to decode NSURL due to invalid encoding of length %d. Substituting a blank URL", length);
     
    280280static inline void encodeDataInternal(Encoder& encoder, NSData *data)
    281281{
    282     encoder << (__bridge CFDataRef)data;
     282    encoder << bridge_cast(data);
    283283}
    284284
     
    288288    if (!decoder.decode(data))
    289289        return std::nullopt;
    290     return { adoptNS((NSData *)data.leakRef()) };
     290    return { bridge_cast(WTFMove(data)) };
    291291}
    292292
     
    295295static inline void encodeDateInternal(Encoder& encoder, NSDate *date)
    296296{
    297     encoder << (__bridge CFDateRef)date;
     297    encoder << bridge_cast(date);
    298298}
    299299
     
    303303    if (!decoder.decode(date))
    304304        return std::nullopt;
    305     return { adoptNS((NSDate *)date.leakRef()) };
     305    return { bridge_cast(WTFMove(date)) };
    306306}
    307307
     
    388388static inline void encodeNumberInternal(Encoder& encoder, NSNumber *number)
    389389{
    390     encoder << (__bridge CFNumberRef)number;
     390    encoder << bridge_cast(number);
    391391}
    392392
     
    396396    if (!decoder.decode(number))
    397397        return std::nullopt;
    398     return { adoptNS((NSNumber *)number.leakRef()) };
     398    return { bridge_cast(WTFMove(number)) };
    399399}
    400400
     
    425425        return std::nullopt;
    426426
    427     auto unarchiver = adoptNS([[NSKeyedUnarchiver alloc] initForReadingFromData:(__bridge NSData *)data.get() error:nullptr]);
     427    auto unarchiver = adoptNS([[NSKeyedUnarchiver alloc] initForReadingFromData:bridge_cast(data.get()) error:nullptr]);
    428428    unarchiver.get().decodingFailurePolicy = NSDecodingFailurePolicyRaiseException;
    429429
     
    451451static inline void encodeStringInternal(Encoder& encoder, NSString *string)
    452452{
    453     encoder << (__bridge CFStringRef)string;
     453    encoder << bridge_cast(string);
    454454}
    455455
     
    459459    if (!decoder.decode(string))
    460460        return std::nullopt;
    461     return { adoptNS((NSString *)string.leakRef()) };
     461    return { bridge_cast(WTFMove(string)) };
    462462}
    463463
     
    466466static inline void encodeURLInternal(Encoder& encoder, NSURL *URL)
    467467{
    468     encoder << (__bridge CFURLRef)URL;
     468    encoder << bridge_cast(URL);
    469469}
    470470
     
    474474    if (!decoder.decode(URL))
    475475        return std::nullopt;
    476     return { adoptNS((NSURL *)URL.leakRef()) };
     476    return { bridge_cast(WTFMove(URL)) };
    477477}
    478478
  • trunk/Source/WebKit/UIProcess/API/Cocoa/WKConnection.mm

    r278780 r284763  
    3434#import <wtf/RetainPtr.h>
    3535#import <wtf/WeakObjCPtr.h>
     36#import <wtf/cocoa/TypeCastsCocoa.h>
    3637#import <wtf/text/WTFString.h>
    3738
     
    6061
    6162    if ([delegate respondsToSelector:@selector(connection:didReceiveMessageWithName:body:)]) {
    62         RetainPtr<CFStringRef> nsMessageName = adoptCF(WKStringCopyCFString(kCFAllocatorDefault, messageName));
    63         RetainPtr<id> nsMessageBody = static_cast<WebKit::ObjCObjectGraph*>(WebKit::toImpl(messageBody))->rootObject();
    64         [delegate connection:connection didReceiveMessageWithName:(__bridge NSString *)nsMessageName.get() body:nsMessageBody.get()];
     63        auto name = bridge_cast(adoptCF(WKStringCopyCFString(kCFAllocatorDefault, messageName)));
     64        id body = static_cast<WebKit::ObjCObjectGraph*>(WebKit::toImpl(messageBody))->rootObject();
     65        [delegate connection:connection didReceiveMessageWithName:name.get() body:body];
    6566    }
    6667}
  • trunk/Source/WebKit/UIProcess/Plugins/mac/PluginInfoStoreMac.mm

    r276880 r284763  
    3535#import <pwd.h>
    3636#import <wtf/RetainPtr.h>
     37#import <wtf/cocoa/TypeCastsCocoa.h>
     38#import <wtf/cocoa/VectorCocoa.h>
    3739#import <wtf/text/CString.h>
    3840
     
    5658Vector<String> PluginInfoStore::pluginPathsInDirectory(const String& directory)
    5759{
    58     Vector<String> pluginPaths;
    59 
    60     RetainPtr<CFStringRef> directoryCFString = directory.createCFString();
    61     NSArray *filenames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:(__bridge NSString *)directoryCFString.get() error:nil];
    62     for (NSString *filename in filenames)
    63         pluginPaths.append([(__bridge NSString *)directoryCFString.get() stringByAppendingPathComponent:filename]);
    64    
    65     return pluginPaths;
     60    auto directoryNSString = bridge_cast(directory.createCFString());
     61    return makeVector([[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryNSString.get() error:nil], [&] (id filename) -> std::optional<String> {
     62        return [directoryNSString stringByAppendingPathComponent:filename];
     63    });
    6664}
    6765
Note: See TracChangeset for help on using the changeset viewer.