Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(247)

Side by Side Diff: ui/base/clipboard/clipboard_util_mac.mm

Issue 1809243004: Add new NSPasteboard utility function and tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments from avi. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
OLDNEW
« no previous file with comments | « ui/base/clipboard/clipboard_util_mac.h ('k') | ui/base/clipboard/clipboard_util_mac_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698