Changeset 43850 in webkit
- Timestamp:
- May 18, 2009 9:17:26 PM (15 years ago)
- Location:
- trunk/WebCore
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/WebCore/ChangeLog
r43847 r43850 1 2009-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 1 31 2009-05-18 David Levin <levin@chromium.org> 2 32 -
trunk/WebCore/platform/mac/ClipboardMac.mm
r43802 r43850 122 122 123 123 NSString *cocoaType = cocoaTypeFromMIMEType(type); 124 if (cocoaType) {124 if (cocoaType) 125 125 [m_pasteboard.get() setString:@"" forType:cocoaType]; 126 }127 126 } 128 127 … … 135 134 136 135 [m_pasteboard.get() declareTypes:[NSArray array] owner:nil]; 136 } 137 138 static 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 162 static 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; 137 182 } 138 183 … … 142 187 if (policy() != ClipboardReadable) 143 188 return String(); 144 189 145 190 NSString *cocoaType = cocoaTypeFromMIMEType(type); 146 191 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 151 194 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"]; 187 199 } else if ([cocoaType isEqualToString:NSStringPboardType]) { 188 200 cocoaValue = [[m_pasteboard.get() stringForType:cocoaType] precomposedStringWithCanonicalMapping]; 189 } else if (cocoaType) {201 } else if (cocoaType) 190 202 cocoaValue = [m_pasteboard.get() stringForType:cocoaType]; 191 }192 203 193 204 // Enforce changeCount ourselves for security. We check after reading instead of before to be … … 247 258 248 259 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); 261 269 } 262 270 return result;
Note: See TracChangeset
for help on using the changeset viewer.