Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ui/base/clipboard/clipboard_util_mac.h" | |
| 6 | |
| 7 #include "base/mac/foundation_util.h" | |
| 8 #include "base/mac/scoped_cftyperef.h" | |
| 9 | |
| 10 namespace { | |
| 11 NSString* const kWebURLsWithTitlesPboardType = @"WebURLsWithTitlesPboardType"; | |
| 12 NSString* const kCorePasteboardFlavorType_url = | |
| 13 @"CorePasteboardFlavorType 0x75726C20"; // 'url ' url | |
| 14 NSString* const kCorePasteboardFlavorType_urln = | |
| 15 @"CorePasteboardFlavorType 0x75726C6E"; // 'urln' title | |
| 16 | |
| 17 // It's much more convenient to return an NSString than a | |
| 18 // base::ScopedCFTypeRef<CFStringRef>, since the methods on NSPasteboardItem | |
| 19 // require an NSString*. | |
| 20 NSString* UTIFromPboardType(NSString* type) { | |
| 21 return [base::mac::CFToNSCast(UTTypeCreatePreferredIdentifierForTag( | |
| 22 kUTTagClassNSPboardType, base::mac::NSToCFCast(type), kUTTypeData)) | |
| 23 autorelease]; | |
| 24 } | |
| 25 } // namespace | |
| 26 | |
| 27 namespace ui { | |
| 28 | |
| 29 // static | |
| 30 base::scoped_nsobject<NSPasteboardItem> ClipboardUtil::PasteboardItemFromUrl( | |
| 31 NSString* urlString, | |
| 32 NSString* title) { | |
| 33 DCHECK(urlString); | |
| 34 if (!title) | |
| 35 title = urlString; | |
| 36 | |
| 37 base::scoped_nsobject<NSPasteboardItem> item([[NSPasteboardItem alloc] init]); | |
| 38 | |
| 39 NSURL* url = [NSURL URLWithString:urlString]; | |
| 40 if ([url isFileURL] && | |
| 41 [[NSFileManager defaultManager] fileExistsAtPath:[url path]]) { | |
| 42 [item setPropertyList:@[ [url path] ] | |
| 43 forType:UTIFromPboardType(NSFilenamesPboardType)]; | |
| 44 } | |
| 45 | |
| 46 // Set Safari's URL + title arrays Pboard type. | |
| 47 NSArray* urlsAndTitles = @[ @[ urlString ], @[ title ] ]; | |
| 48 [item setPropertyList:urlsAndTitles | |
| 49 forType:UTIFromPboardType(kWebURLsWithTitlesPboardType)]; | |
| 50 | |
| 51 // Set NSURLPboardType. The format of the property list is divined from | |
|
Avi (use Gerrit)
2016/03/21 18:30:38
Haha sob.
The word "divine" as a source of docume
| |
| 52 // Webkit's function PlatformPasteboard::setStringForType. | |
| 53 // https://github.com/WebKit/webkit/blob/master/Source/WebCore/platform/mac/Pl atformPasteboardMac.mm | |
| 54 NSURL* base = [url baseURL]; | |
| 55 if (base) { | |
| 56 [item setPropertyList:@[ [url relativeString], [base absoluteString] ] | |
| 57 forType:UTIFromPboardType(NSURLPboardType)]; | |
| 58 } else if (url) { | |
| 59 [item setPropertyList:@[ [url absoluteString], @"" ] | |
| 60 forType:UTIFromPboardType(NSURLPboardType)]; | |
| 61 } | |
| 62 | |
| 63 [item setString:urlString forType:UTIFromPboardType(NSStringPboardType)]; | |
| 64 | |
| 65 [item setData:[urlString dataUsingEncoding:NSUTF8StringEncoding] | |
| 66 forType:UTIFromPboardType(kCorePasteboardFlavorType_url)]; | |
| 67 | |
| 68 [item setData:[title dataUsingEncoding:NSUTF8StringEncoding] | |
| 69 forType:UTIFromPboardType(kCorePasteboardFlavorType_urln)]; | |
| 70 return item; | |
| 71 } | |
| 72 | |
| 73 } // namespace ui | |
| OLD | NEW |