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

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: Minor nits. 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/scoped_cftyperef.h"
8
9 namespace {
10 NSString* const kWebURLsWithTitlesPboardType = @"WebURLsWithTitlesPboardType";
11 NSString* const kCorePasteboardFlavorType_url =
12 @"CorePasteboardFlavorType 0x75726C20"; // 'url ' url
13 NSString* const kCorePasteboardFlavorType_urln =
14 @"CorePasteboardFlavorType 0x75726C6E"; // 'urln' title
15
16 // It's much more convenient to return an NSString than a
17 // base::ScopedCFTypeRef<CFStringRef>, since the methods on NSPasteboardItem
18 // require an NSString*.
19 NSString* UTIFromPboardType(NSString* type) {
20 return [static_cast<NSString*>(UTTypeCreatePreferredIdentifierForTag(
Avi (use Gerrit) 2016/03/19 01:15:20 base::mac::CFToNSCast?
erikchen 2016/03/21 18:26:38 Done.
21 kUTTagClassNSPboardType, static_cast<CFStringRef>(type), kUTTypeData))
Avi (use Gerrit) 2016/03/19 01:15:20 base::mac::NSToCFCast? Also, why specify conforma
erikchen 2016/03/21 18:26:38 Done. According to SO, we shouldn't pass null. Th
22 autorelease];
Avi (use Gerrit) 2016/03/19 01:15:20 And technically speaking you should use base::mac:
erikchen 2016/03/21 18:26:38 I kept -autorelease.
23 }
24 } // namespace
25
26 namespace ui {
27
28 // static
29 base::scoped_nsobject<NSPasteboardItem> ClipboardUtil::PasteboardItemFromUrl(
30 NSString* urlString,
31 NSString* title) {
32 DCHECK(urlString);
33 if (!title)
34 title = urlString;
35
36 base::scoped_nsobject<NSPasteboardItem> item([[NSPasteboardItem alloc] init]);
37
38 NSURL* url = [NSURL URLWithString:urlString];
39 if ([url isFileURL] &&
40 [[NSFileManager defaultManager] fileExistsAtPath:[url path]]) {
41 [item setPropertyList:@[ [url path] ]
42 forType:UTIFromPboardType(NSFilenamesPboardType)];
43 }
44
45 // Set Safari's URL + title arrays Pboard type.
46 NSArray* urlsAndTitles = @[ @[ urlString ], @[ title ] ];
47 [item setPropertyList:urlsAndTitles
48 forType:UTIFromPboardType(kWebURLsWithTitlesPboardType)];
49
50 // Set NSURLPboardType.
51 NSURL *base = [url baseURL];
Avi (use Gerrit) 2016/03/19 01:15:20 space after *
erikchen 2016/03/21 18:26:38 Done.
52 if (base) {
53 [item setPropertyList:@[ [url relativeString], [base absoluteString] ]
54 forType:UTIFromPboardType(NSURLPboardType)];
55 } else if (url) {
56 [item setPropertyList:@[ [url absoluteString], @"" ]
57 forType:UTIFromPboardType(NSURLPboardType)];
58 }
Avi (use Gerrit) 2016/03/19 01:15:20 Can you provide documentation or a link about why
erikchen 2016/03/21 18:26:38 Done. The format of the property list come straig
59
60 [item setString:urlString forType:UTIFromPboardType(NSStringPboardType)];
61
62 const char* tempCString = [urlString UTF8String];
63 [item setData:[NSData dataWithBytes:tempCString length:strlen(tempCString)]
Avi (use Gerrit) 2016/03/19 01:15:20 Why not do [urlString dataUsingEncoding:NSUTF8Stri
erikchen 2016/03/21 18:26:38 I was copy-pasting from the old Mozilla code. I ch
64 forType:UTIFromPboardType(kCorePasteboardFlavorType_url)];
Avi (use Gerrit) 2016/03/19 01:15:20 indent one fewer to align colons
erikchen 2016/03/21 18:26:38 Done.
65
66 tempCString = [title UTF8String];
67 [item setData:[NSData dataWithBytes:tempCString length:strlen(tempCString)]
Avi (use Gerrit) 2016/03/19 01:15:20 [title dataUsingEncoding:NSUTF8StringEncoding]?
erikchen 2016/03/21 18:26:38 Done.
68 forType:UTIFromPboardType(kCorePasteboardFlavorType_urln)];
Avi (use Gerrit) 2016/03/19 01:15:20 indent one fewer, align on :
erikchen 2016/03/21 18:26:38 Done.
69 return item;
70 }
71
72 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698