Changeset 43850 in webkit


Ignore:
Timestamp:
May 18, 2009 9:17:26 PM (15 years ago)
Author:
eric@webkit.org
Message:

2009-05-17 Eric Seidel <eric@webkit.org>

Reviewed by Darin Adler.

ClipboardMac cleanup
https://bugs.webkit.org/show_bug.cgi?id=25847

I'm mostly just moving code, however there are 3 changes I made while moving, detailed below.

No functional changes, so no tests.

  • platform/mac/ClipboardMac.mm: (WebCore::ClipboardMac::clearData): (WebCore::absoluteURLsFromPasteboardFilenames): Broke out logic for filenames into its own function to make the caller more readable. (WebCore::absoluteURLsFromPasteboard): Broke out logic from getData into absoluteURLsFromPasteboard. This returns an NSArray so that we can use -[NSArray componentsJoinedByString] in the caller (which is cleaner than the manual "\n" addition before). This also access to the full list of file urls for future callers. (WebCore::ClipboardMac::getData): unsigned count = (type == "URL") ? 1 : [fileList count]; is now an explicit check for "URL", before it was a check for != "text/uri-list" which was much more confusing in my opinion. text/uri-list and URL are the only two types which map to NSURLPboardType in cocoaTypeFromMIMEType(). (WebCore::ClipboardMac::types): I removed an extra if (!types) check, right before [types count]. In Obj-C messaging nil will return 0 (size of a pointer), so it's safe to message nil here and expect it to return 0.
Location:
trunk/WebCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r43847 r43850  
     12009-05-17  Eric Seidel  <eric@webkit.org>
     2
     3        Reviewed by Darin Adler.
     4
     5        ClipboardMac cleanup
     6        https://bugs.webkit.org/show_bug.cgi?id=25847
     7
     8        I'm mostly just moving code, however there are 3 changes I made while moving, detailed below.
     9
     10        No functional changes, so no tests.
     11
     12        * platform/mac/ClipboardMac.mm:
     13        (WebCore::ClipboardMac::clearData):
     14        (WebCore::absoluteURLsFromPasteboardFilenames):
     15          Broke out logic for filenames into its own function to make the caller more readable.
     16        (WebCore::absoluteURLsFromPasteboard):
     17          Broke out logic from getData into absoluteURLsFromPasteboard. This returns an NSArray
     18          so that we can use -[NSArray componentsJoinedByString] in the caller (which is
     19          cleaner than the manual "\n" addition before).
     20          This also access to the full list of file urls for future callers.
     21        (WebCore::ClipboardMac::getData):
     22          unsigned count = (type == "URL") ? 1 : [fileList count]; is now an
     23          explicit check for "URL", before it was a check for != "text/uri-list" which
     24          was much more confusing in my opinion.  text/uri-list and URL are the only
     25          two types which map to NSURLPboardType in cocoaTypeFromMIMEType().
     26        (WebCore::ClipboardMac::types):
     27          I removed an extra if (!types) check, right before [types count].  In Obj-C
     28          messaging nil will return 0 (size of a pointer), so it's safe to message nil
     29          here and expect it to return 0.
     30
    1312009-05-18  David Levin  <levin@chromium.org>
    232
  • trunk/WebCore/platform/mac/ClipboardMac.mm

    r43802 r43850  
    122122
    123123    NSString *cocoaType = cocoaTypeFromMIMEType(type);
    124     if (cocoaType) {
     124    if (cocoaType)
    125125        [m_pasteboard.get() setString:@"" forType:cocoaType];
    126     }
    127126}
    128127
     
    135134
    136135    [m_pasteboard.get() declareTypes:[NSArray array] owner:nil];
     136}
     137
     138static NSArray *absoluteURLsFromPasteboardFilenames(NSPasteboard* pasteboard, bool onlyFirstURL)
     139{
     140    NSArray *fileList = [pasteboard propertyListForType:NSFilenamesPboardType];
     141
     142    // FIXME: Why does this code need to guard against bad values on the pasteboard?
     143    ASSERT(!fileList || [fileList isKindOfClass:[NSArray class]]);
     144    if (!fileList || ![fileList isKindOfClass:[NSArray class]] || ![fileList count])
     145        return nil;
     146
     147    NSUInteger count = onlyFirstURL ? 1 : [fileList count];
     148    NSMutableArray *urls = [NSMutableArray array];
     149    for (NSUInteger i = 0; i < count; i++) {
     150        NSString *string = [fileList objectAtIndex:i];
     151
     152        ASSERT([string isKindOfClass:[NSString class]]);  // Added to understand why this if code is here
     153        if (![string isKindOfClass:[NSString class]])
     154            return nil; // Non-string object in the list, bail out!  FIXME: When can this happen?
     155
     156        NSURL *url = [NSURL fileURLWithPath:string];
     157        [urls addObject:[url absoluteString]];
     158    }
     159    return urls;
     160}
     161
     162static NSArray *absoluteURLsFromPasteboard(NSPasteboard* pasteboard, bool onlyFirstURL)
     163{
     164    // NOTE: We must always check [availableTypes containsObject:] before accessing pasteboard data
     165    // or CoreFoundation will printf when there is not data of the corresponding type.
     166    NSArray *availableTypes = [pasteboard types];
     167
     168    // Try NSFilenamesPboardType because it contains a list
     169    if ([availableTypes containsObject:NSFilenamesPboardType]) {
     170        if (NSArray* absoluteURLs = absoluteURLsFromPasteboardFilenames(pasteboard, onlyFirstURL))
     171            return absoluteURLs;
     172    }
     173
     174    // Fallback to NSURLPboardType (which is a single URL)
     175    if ([availableTypes containsObject:NSURLPboardType]) {
     176        if (NSURL *url = [NSURL URLFromPasteboard:pasteboard])
     177            return [NSArray arrayWithObject:[url absoluteString]];
     178    }
     179
     180    // No file paths on the pasteboard, return nil
     181    return nil;
    137182}
    138183
     
    142187    if (policy() != ClipboardReadable)
    143188        return String();
    144    
     189
    145190    NSString *cocoaType = cocoaTypeFromMIMEType(type);
    146191    NSString *cocoaValue = nil;
    147     NSArray *availableTypes = [m_pasteboard.get() types];
    148 
    149     // Fetch the data in different ways for the different Cocoa types
    150 
     192
     193    // Grab the value off the pasteboard corresponding to the cocoaType
    151194    if ([cocoaType isEqualToString:NSURLPboardType]) {
    152         // When both URL and filenames are present, filenames is superior since it can contain a list.
    153         // must check this or we get a printf from CF when there's no data of this type
    154         if ([availableTypes containsObject:NSFilenamesPboardType]) {
    155             NSArray *fileList = [m_pasteboard.get() propertyListForType:NSFilenamesPboardType];
    156             if (fileList && [fileList isKindOfClass:[NSArray class]]) {
    157                 unsigned count = [fileList count];
    158                 if (count > 0) {
    159                     if (type != "text/uri-list")
    160                         count = 1;
    161                     NSMutableString *urls = [NSMutableString string];
    162                     unsigned i;
    163                     for (i = 0; i < count; i++) {
    164                         if (i > 0) {
    165                             [urls appendString:@"\n"];
    166                         }
    167                         NSString *string = [fileList objectAtIndex:i];
    168                         if (![string isKindOfClass:[NSString class]])
    169                             break;
    170                         NSURL *url = [NSURL fileURLWithPath:string];
    171                         [urls appendString:[url absoluteString]];
    172                     }
    173                     if (i == count)
    174                         cocoaValue = urls;
    175                 }
    176             }
    177         }
    178         if (!cocoaValue) {
    179             // must check this or we get a printf from CF when there's no data of this type
    180             if ([availableTypes containsObject:NSURLPboardType]) {
    181                 NSURL *url = [NSURL URLFromPasteboard:m_pasteboard.get()];
    182                 if (url) {
    183                     cocoaValue = [url absoluteString];
    184                 }
    185             }
    186         }
     195        // "URL" and "text/url-list" both map to NSURLPboardType in cocoaTypeFromMIMEType(), "URL" only wants the first URL
     196        bool onlyFirstURL = (type == "URL");
     197        NSArray *absoluteURLs = absoluteURLsFromPasteboard(m_pasteboard.get(), onlyFirstURL);
     198        cocoaValue = [absoluteURLs componentsJoinedByString:@"\n"];
    187199    } else if ([cocoaType isEqualToString:NSStringPboardType]) {
    188200        cocoaValue = [[m_pasteboard.get() stringForType:cocoaType] precomposedStringWithCanonicalMapping];
    189     } else if (cocoaType) {       
     201    } else if (cocoaType)
    190202        cocoaValue = [m_pasteboard.get() stringForType:cocoaType];
    191     }
    192203
    193204    // Enforce changeCount ourselves for security.  We check after reading instead of before to be
     
    247258
    248259    HashSet<String> result;
    249     if (types) {
    250         unsigned count = [types count];
    251         unsigned i;
    252         for (i = 0; i < count; i++) {
    253             NSString *pbType = [types objectAtIndex:i];
    254             if ([pbType isEqualToString:@"NeXT plain ascii pasteboard type"])
    255                 continue;   // skip this ancient type that gets auto-supplied by some system conversion
    256 
    257             String str = MIMETypeFromCocoaType(pbType);
    258             if (!result.contains(str))
    259                 result.add(str);
    260         }
     260    NSUInteger count = [types count];
     261    for (NSUInteger i = 0; i < count; i++) {
     262        NSString *pbType = [types objectAtIndex:i];
     263        if ([pbType isEqualToString:@"NeXT plain ascii pasteboard type"])
     264            continue;   // skip this ancient type that gets auto-supplied by some system conversion
     265
     266        String str = MIMETypeFromCocoaType(pbType);
     267        if (!result.contains(str))
     268            result.add(str);
    261269    }
    262270    return result;
Note: See TracChangeset for help on using the changeset viewer.