| 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 { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 | 71 |
| 72 [item setData:[urlString dataUsingEncoding:NSUTF8StringEncoding] | 72 [item setData:[urlString dataUsingEncoding:NSUTF8StringEncoding] |
| 73 forType:UTIFromPboardType(kCorePasteboardFlavorType_url)]; | 73 forType:UTIFromPboardType(kCorePasteboardFlavorType_url)]; |
| 74 | 74 |
| 75 [item setData:[title dataUsingEncoding:NSUTF8StringEncoding] | 75 [item setData:[title dataUsingEncoding:NSUTF8StringEncoding] |
| 76 forType:UTIFromPboardType(kCorePasteboardFlavorType_urln)]; | 76 forType:UTIFromPboardType(kCorePasteboardFlavorType_urln)]; |
| 77 return item; | 77 return item; |
| 78 } | 78 } |
| 79 | 79 |
| 80 // static | 80 // static |
| 81 base::scoped_nsobject<NSPasteboardItem> ClipboardUtil::PasteboardItemFromUrls( |
| 82 NSArray* urls, |
| 83 NSArray* titles) { |
| 84 base::scoped_nsobject<NSPasteboardItem> item([[NSPasteboardItem alloc] init]); |
| 85 |
| 86 // Set Safari's URL + title arrays Pboard type. |
| 87 NSArray* urlsAndTitles = @[ urls, titles ]; |
| 88 [item setPropertyList:urlsAndTitles |
| 89 forType:UTIFromPboardType(kWebURLsWithTitlesPboardType)]; |
| 90 |
| 91 return item; |
| 92 } |
| 93 |
| 94 // static |
| 81 base::scoped_nsobject<NSPasteboardItem> ClipboardUtil::PasteboardItemFromString( | 95 base::scoped_nsobject<NSPasteboardItem> ClipboardUtil::PasteboardItemFromString( |
| 82 NSString* string) { | 96 NSString* string) { |
| 83 base::scoped_nsobject<NSPasteboardItem> item([[NSPasteboardItem alloc] init]); | 97 base::scoped_nsobject<NSPasteboardItem> item([[NSPasteboardItem alloc] init]); |
| 84 [item setString:string forType:UTIFromPboardType(NSStringPboardType)]; | 98 [item setString:string forType:UTIFromPboardType(NSStringPboardType)]; |
| 85 return item; | 99 return item; |
| 86 } | 100 } |
| 87 | 101 |
| 88 //static | 102 //static |
| 89 NSString* ClipboardUtil::GetTitleFromPasteboardURL(NSPasteboard* pboard) { | 103 NSString* ClipboardUtil::GetTitleFromPasteboardURL(NSPasteboard* pboard) { |
| 90 return | 104 return |
| 91 [pboard stringForType:UTIFromPboardType(kCorePasteboardFlavorType_urln)]; | 105 [pboard stringForType:UTIFromPboardType(kCorePasteboardFlavorType_urln)]; |
| 92 } | 106 } |
| 93 | 107 |
| 94 //static | 108 //static |
| 95 NSString* ClipboardUtil::GetURLFromPasteboardURL(NSPasteboard* pboard) { | 109 NSString* ClipboardUtil::GetURLFromPasteboardURL(NSPasteboard* pboard) { |
| 96 return | 110 return |
| 97 [pboard stringForType:UTIFromPboardType(kCorePasteboardFlavorType_url)]; | 111 [pboard stringForType:UTIFromPboardType(kCorePasteboardFlavorType_url)]; |
| 98 } | 112 } |
| 99 | 113 |
| 114 // static |
| 115 NSString* ClipboardUtil::UTIForPasteboardType(NSString* type) { |
| 116 return UTIFromPboardType(type); |
| 117 } |
| 118 |
| 119 // static |
| 120 NSString* ClipboardUtil::UTIForWebURLsAndTitles() { |
| 121 return UTIFromPboardType(kWebURLsWithTitlesPboardType); |
| 122 } |
| 123 |
| 124 // static |
| 125 void ClipboardUtil::AddDataToPasteboard(NSPasteboard* pboard, |
| 126 NSPasteboardItem* item) { |
| 127 NSSet* oldTypes = [NSSet setWithArray:[pboard types]]; |
| 128 NSMutableSet* newTypes = [NSMutableSet setWithArray:[item types]]; |
| 129 [newTypes minusSet:oldTypes]; |
| 130 |
| 131 [pboard addTypes:[newTypes allObjects] owner:nil]; |
| 132 for (NSString* type in newTypes) { |
| 133 // Technically, the object associated with |type| might be an NSString or a |
| 134 // property list. It doesn't matter though, since the type gets pulled from |
| 135 // and shoved into an NSDictionary. |
| 136 [pboard setData:[item dataForType:type] forType:type]; |
| 137 } |
| 138 } |
| 139 |
| 140 // static |
| 141 bool ClipboardUtil::URLsAndTitlesFromPasteboard(NSPasteboard* pboard, |
| 142 NSArray** urls, |
| 143 NSArray** titles) { |
| 144 NSArray* bookmarkPairs = base::mac::ObjCCast<NSArray>([pboard |
| 145 propertyListForType:UTIFromPboardType(kWebURLsWithTitlesPboardType)]); |
| 146 if (!bookmarkPairs) |
| 147 return false; |
| 148 |
| 149 if ([bookmarkPairs count] != 2) |
| 150 return false; |
| 151 |
| 152 NSArray* urlsArr = |
| 153 base::mac::ObjCCast<NSArray>([bookmarkPairs objectAtIndex:0]); |
| 154 NSArray* titlesArr = |
| 155 base::mac::ObjCCast<NSArray>([bookmarkPairs objectAtIndex:1]); |
| 156 |
| 157 if (!urlsArr || !titlesArr) |
| 158 return false; |
| 159 if ([urlsArr count] < 1) |
| 160 return false; |
| 161 if ([urlsArr count] != [titlesArr count]) |
| 162 return false; |
| 163 |
| 164 for (id obj in urlsArr) { |
| 165 if (![obj isKindOfClass:[NSString class]]) |
| 166 return false; |
| 167 } |
| 168 |
| 169 for (id obj in titlesArr) { |
| 170 if (![obj isKindOfClass:[NSString class]]) |
| 171 return false; |
| 172 } |
| 173 |
| 174 *urls = urlsArr; |
| 175 *titles = titlesArr; |
| 176 return true; |
| 177 } |
| 178 |
| 100 } // namespace ui | 179 } // namespace ui |
| OLD | NEW |