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

Unified 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/base/clipboard/clipboard_util_mac.mm
diff --git a/ui/base/clipboard/clipboard_util_mac.mm b/ui/base/clipboard/clipboard_util_mac.mm
new file mode 100644
index 0000000000000000000000000000000000000000..0d3f8f87fa64bfca4badd049625f02ed6d3962d3
--- /dev/null
+++ b/ui/base/clipboard/clipboard_util_mac.mm
@@ -0,0 +1,73 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/base/clipboard/clipboard_util_mac.h"
+
+#include "base/mac/foundation_util.h"
+#include "base/mac/scoped_cftyperef.h"
+
+namespace {
+NSString* const kWebURLsWithTitlesPboardType = @"WebURLsWithTitlesPboardType";
+NSString* const kCorePasteboardFlavorType_url =
+ @"CorePasteboardFlavorType 0x75726C20"; // 'url ' url
+NSString* const kCorePasteboardFlavorType_urln =
+ @"CorePasteboardFlavorType 0x75726C6E"; // 'urln' title
+
+// It's much more convenient to return an NSString than a
+// base::ScopedCFTypeRef<CFStringRef>, since the methods on NSPasteboardItem
+// require an NSString*.
+NSString* UTIFromPboardType(NSString* type) {
+ return [base::mac::CFToNSCast(UTTypeCreatePreferredIdentifierForTag(
+ kUTTagClassNSPboardType, base::mac::NSToCFCast(type), kUTTypeData))
+ autorelease];
+}
+} // namespace
+
+namespace ui {
+
+// static
+base::scoped_nsobject<NSPasteboardItem> ClipboardUtil::PasteboardItemFromUrl(
+ NSString* urlString,
+ NSString* title) {
+ DCHECK(urlString);
+ if (!title)
+ title = urlString;
+
+ base::scoped_nsobject<NSPasteboardItem> item([[NSPasteboardItem alloc] init]);
+
+ NSURL* url = [NSURL URLWithString:urlString];
+ if ([url isFileURL] &&
+ [[NSFileManager defaultManager] fileExistsAtPath:[url path]]) {
+ [item setPropertyList:@[ [url path] ]
+ forType:UTIFromPboardType(NSFilenamesPboardType)];
+ }
+
+ // Set Safari's URL + title arrays Pboard type.
+ NSArray* urlsAndTitles = @[ @[ urlString ], @[ title ] ];
+ [item setPropertyList:urlsAndTitles
+ forType:UTIFromPboardType(kWebURLsWithTitlesPboardType)];
+
+ // 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
+ // Webkit's function PlatformPasteboard::setStringForType.
+ // https://github.com/WebKit/webkit/blob/master/Source/WebCore/platform/mac/PlatformPasteboardMac.mm
+ NSURL* base = [url baseURL];
+ if (base) {
+ [item setPropertyList:@[ [url relativeString], [base absoluteString] ]
+ forType:UTIFromPboardType(NSURLPboardType)];
+ } else if (url) {
+ [item setPropertyList:@[ [url absoluteString], @"" ]
+ forType:UTIFromPboardType(NSURLPboardType)];
+ }
+
+ [item setString:urlString forType:UTIFromPboardType(NSStringPboardType)];
+
+ [item setData:[urlString dataUsingEncoding:NSUTF8StringEncoding]
+ forType:UTIFromPboardType(kCorePasteboardFlavorType_url)];
+
+ [item setData:[title dataUsingEncoding:NSUTF8StringEncoding]
+ forType:UTIFromPboardType(kCorePasteboardFlavorType_urln)];
+ return item;
+}
+
+} // namespace ui
« 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