| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/base/clipboard/clipboard_util_mac.h" | 5 #include "ui/base/clipboard/clipboard_util_mac.h" |
| 6 | 6 |
| 7 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
| 8 #include "base/mac/scoped_cftyperef.h" | 8 #include "base/mac/scoped_cftyperef.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| 11 NSString* const kWebURLsWithTitlesPboardType = @"WebURLsWithTitlesPboardType"; | 11 NSString* const kWebURLsWithTitlesPboardType = @"WebURLsWithTitlesPboardType"; |
| 12 NSString* const kCorePasteboardFlavorType_url = | 12 NSString* const kPublicUrl = @"public.url"; |
| 13 @"CorePasteboardFlavorType 0x75726C20"; // 'url ' url | 13 NSString* const kPublicUrlName = @"public.url-name"; |
| 14 NSString* const kCorePasteboardFlavorType_urln = | |
| 15 @"CorePasteboardFlavorType 0x75726C6E"; // 'urln' title | |
| 16 | 14 |
| 17 // It's much more convenient to return an NSString than a | 15 // It's much more convenient to return an NSString than a |
| 18 // base::ScopedCFTypeRef<CFStringRef>, since the methods on NSPasteboardItem | 16 // base::ScopedCFTypeRef<CFStringRef>, since the methods on NSPasteboardItem |
| 19 // require an NSString*. | 17 // require an NSString*. |
| 20 NSString* UTIFromPboardType(NSString* type) { | 18 NSString* UTIFromPboardType(NSString* type) { |
| 21 return [base::mac::CFToNSCast(UTTypeCreatePreferredIdentifierForTag( | 19 return [base::mac::CFToNSCast(UTTypeCreatePreferredIdentifierForTag( |
| 22 kUTTagClassNSPboardType, base::mac::NSToCFCast(type), kUTTypeData)) | 20 kUTTagClassNSPboardType, base::mac::NSToCFCast(type), kUTTypeData)) |
| 23 autorelease]; | 21 autorelease]; |
| 24 } | 22 } |
| 25 } // namespace | 23 } // namespace |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 NSURL* base = [url baseURL]; | 59 NSURL* base = [url baseURL]; |
| 62 if (base) { | 60 if (base) { |
| 63 [item setPropertyList:@[ [url relativeString], [base absoluteString] ] | 61 [item setPropertyList:@[ [url relativeString], [base absoluteString] ] |
| 64 forType:UTIFromPboardType(NSURLPboardType)]; | 62 forType:UTIFromPboardType(NSURLPboardType)]; |
| 65 } else if (url) { | 63 } else if (url) { |
| 66 [item setPropertyList:@[ [url absoluteString], @"" ] | 64 [item setPropertyList:@[ [url absoluteString], @"" ] |
| 67 forType:UTIFromPboardType(NSURLPboardType)]; | 65 forType:UTIFromPboardType(NSURLPboardType)]; |
| 68 } | 66 } |
| 69 | 67 |
| 70 [item setString:urlString forType:NSPasteboardTypeString]; | 68 [item setString:urlString forType:NSPasteboardTypeString]; |
| 71 | 69 [item setString:urlString forType:kPublicUrl]; |
| 72 [item setData:[urlString dataUsingEncoding:NSUTF8StringEncoding] | 70 [item setString:title forType:kPublicUrlName]; |
| 73 forType:UTIFromPboardType(kCorePasteboardFlavorType_url)]; | |
| 74 | |
| 75 [item setData:[title dataUsingEncoding:NSUTF8StringEncoding] | |
| 76 forType:UTIFromPboardType(kCorePasteboardFlavorType_urln)]; | |
| 77 return item; | 71 return item; |
| 78 } | 72 } |
| 79 | 73 |
| 80 // static | 74 // static |
| 81 base::scoped_nsobject<NSPasteboardItem> ClipboardUtil::PasteboardItemFromUrls( | 75 base::scoped_nsobject<NSPasteboardItem> ClipboardUtil::PasteboardItemFromUrls( |
| 82 NSArray* urls, | 76 NSArray* urls, |
| 83 NSArray* titles) { | 77 NSArray* titles) { |
| 84 base::scoped_nsobject<NSPasteboardItem> item([[NSPasteboardItem alloc] init]); | 78 base::scoped_nsobject<NSPasteboardItem> item([[NSPasteboardItem alloc] init]); |
| 85 | 79 |
| 86 // Set Safari's URL + title arrays Pboard type. | 80 // Set Safari's URL + title arrays Pboard type. |
| 87 NSArray* urlsAndTitles = @[ urls, titles ]; | 81 NSArray* urlsAndTitles = @[ urls, titles ]; |
| 88 [item setPropertyList:urlsAndTitles | 82 [item setPropertyList:urlsAndTitles |
| 89 forType:UTIFromPboardType(kWebURLsWithTitlesPboardType)]; | 83 forType:UTIFromPboardType(kWebURLsWithTitlesPboardType)]; |
| 90 | 84 |
| 91 return item; | 85 return item; |
| 92 } | 86 } |
| 93 | 87 |
| 94 // static | 88 // static |
| 95 base::scoped_nsobject<NSPasteboardItem> ClipboardUtil::PasteboardItemFromString( | 89 base::scoped_nsobject<NSPasteboardItem> ClipboardUtil::PasteboardItemFromString( |
| 96 NSString* string) { | 90 NSString* string) { |
| 97 base::scoped_nsobject<NSPasteboardItem> item([[NSPasteboardItem alloc] init]); | 91 base::scoped_nsobject<NSPasteboardItem> item([[NSPasteboardItem alloc] init]); |
| 98 [item setString:string forType:NSPasteboardTypeString]; | 92 [item setString:string forType:NSPasteboardTypeString]; |
| 99 return item; | 93 return item; |
| 100 } | 94 } |
| 101 | 95 |
| 102 //static | 96 //static |
| 103 NSString* ClipboardUtil::GetTitleFromPasteboardURL(NSPasteboard* pboard) { | 97 NSString* ClipboardUtil::GetTitleFromPasteboardURL(NSPasteboard* pboard) { |
| 104 return | 98 return [pboard stringForType:kPublicUrlName]; |
| 105 [pboard stringForType:UTIFromPboardType(kCorePasteboardFlavorType_urln)]; | |
| 106 } | 99 } |
| 107 | 100 |
| 108 //static | 101 //static |
| 109 NSString* ClipboardUtil::GetURLFromPasteboardURL(NSPasteboard* pboard) { | 102 NSString* ClipboardUtil::GetURLFromPasteboardURL(NSPasteboard* pboard) { |
| 110 return | 103 return [pboard stringForType:kPublicUrl]; |
| 111 [pboard stringForType:UTIFromPboardType(kCorePasteboardFlavorType_url)]; | |
| 112 } | 104 } |
| 113 | 105 |
| 114 // static | 106 // static |
| 115 NSString* ClipboardUtil::UTIForPasteboardType(NSString* type) { | 107 NSString* ClipboardUtil::UTIForPasteboardType(NSString* type) { |
| 116 return UTIFromPboardType(type); | 108 return UTIFromPboardType(type); |
| 117 } | 109 } |
| 118 | 110 |
| 119 // static | 111 // static |
| 120 NSString* ClipboardUtil::UTIForWebURLsAndTitles() { | 112 NSString* ClipboardUtil::UTIForWebURLsAndTitles() { |
| 121 return UTIFromPboardType(kWebURLsWithTitlesPboardType); | 113 return UTIFromPboardType(kWebURLsWithTitlesPboardType); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 if (![obj isKindOfClass:[NSString class]]) | 162 if (![obj isKindOfClass:[NSString class]]) |
| 171 return false; | 163 return false; |
| 172 } | 164 } |
| 173 | 165 |
| 174 *urls = urlsArr; | 166 *urls = urlsArr; |
| 175 *titles = titlesArr; | 167 *titles = titlesArr; |
| 176 return true; | 168 return true; |
| 177 } | 169 } |
| 178 | 170 |
| 179 } // namespace ui | 171 } // namespace ui |
| OLD | NEW |