Chromium Code Reviews| 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..9645cf1a0b060211bfb775632c59dd0635520cfb |
| --- /dev/null |
| +++ b/ui/base/clipboard/clipboard_util_mac.mm |
| @@ -0,0 +1,72 @@ |
| +// 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/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 [static_cast<NSString*>(UTTypeCreatePreferredIdentifierForTag( |
|
Avi (use Gerrit)
2016/03/19 01:15:20
base::mac::CFToNSCast?
erikchen
2016/03/21 18:26:38
Done.
|
| + 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
|
| + 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.
|
| +} |
| +} // 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. |
| + NSURL *base = [url baseURL]; |
|
Avi (use Gerrit)
2016/03/19 01:15:20
space after *
erikchen
2016/03/21 18:26:38
Done.
|
| + if (base) { |
| + [item setPropertyList:@[ [url relativeString], [base absoluteString] ] |
| + forType:UTIFromPboardType(NSURLPboardType)]; |
| + } else if (url) { |
| + [item setPropertyList:@[ [url absoluteString], @"" ] |
| + forType:UTIFromPboardType(NSURLPboardType)]; |
| + } |
|
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
|
| + |
| + [item setString:urlString forType:UTIFromPboardType(NSStringPboardType)]; |
| + |
| + const char* tempCString = [urlString UTF8String]; |
| + [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
|
| + 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.
|
| + |
| + tempCString = [title UTF8String]; |
| + [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.
|
| + 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.
|
| + return item; |
| +} |
| + |
| +} // namespace ui |