1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
// 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"
#import "base/mac/mac_util.h"
#include "base/mac/scoped_cftyperef.h"
namespace ui {
namespace {
NSString* const kWebURLsWithTitlesPboardType = @"WebURLsWithTitlesPboardType";
NSString* const kPublicUrl = @"public.url";
NSString* const kPublicUrlName = @"public.url-name";
// 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
UniquePasteboard::UniquePasteboard()
: pasteboard_([[NSPasteboard pasteboardWithUniqueName] retain]) {}
UniquePasteboard::~UniquePasteboard() {
[pasteboard_ releaseGlobally];
if (base::mac::IsOS10_12()) {
// On 10.12, move ownership to the autorelease pool rather than possibly
// triggering -[NSPasteboard dealloc] here. This is a speculative workaround
// for https://crbug.com/877979 where a call to __CFPasteboardDeallocate
// from here is triggering "Semaphore object deallocated while in use".
pasteboard_.autorelease();
}
}
// 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
// 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:NSPasteboardTypeString];
[item setString:urlString forType:kPublicUrl];
[item setString:title forType:kPublicUrlName];
return item;
}
// static
base::scoped_nsobject<NSPasteboardItem> ClipboardUtil::PasteboardItemFromUrls(
NSArray* urls,
NSArray* titles) {
base::scoped_nsobject<NSPasteboardItem> item([[NSPasteboardItem alloc] init]);
// Set Safari's URL + title arrays Pboard type.
NSArray* urlsAndTitles = @[ urls, titles ];
[item setPropertyList:urlsAndTitles
forType:UTIFromPboardType(kWebURLsWithTitlesPboardType)];
return item;
}
// static
base::scoped_nsobject<NSPasteboardItem> ClipboardUtil::PasteboardItemFromString(
NSString* string) {
base::scoped_nsobject<NSPasteboardItem> item([[NSPasteboardItem alloc] init]);
[item setString:string forType:NSPasteboardTypeString];
return item;
}
//static
NSString* ClipboardUtil::GetTitleFromPasteboardURL(NSPasteboard* pboard) {
return [pboard stringForType:kPublicUrlName];
}
//static
NSString* ClipboardUtil::GetURLFromPasteboardURL(NSPasteboard* pboard) {
return [pboard stringForType:kPublicUrl];
}
// static
NSString* ClipboardUtil::UTIForPasteboardType(NSString* type) {
return UTIFromPboardType(type);
}
// static
NSString* ClipboardUtil::UTIForWebURLsAndTitles() {
return UTIFromPboardType(kWebURLsWithTitlesPboardType);
}
// static
void ClipboardUtil::AddDataToPasteboard(NSPasteboard* pboard,
NSPasteboardItem* item) {
NSSet* oldTypes = [NSSet setWithArray:[pboard types]];
NSMutableSet* newTypes = [NSMutableSet setWithArray:[item types]];
[newTypes minusSet:oldTypes];
[pboard addTypes:[newTypes allObjects] owner:nil];
for (NSString* type in newTypes) {
// Technically, the object associated with |type| might be an NSString or a
// property list. It doesn't matter though, since the type gets pulled from
// and shoved into an NSDictionary.
[pboard setData:[item dataForType:type] forType:type];
}
}
// static
bool ClipboardUtil::URLsAndTitlesFromPasteboard(NSPasteboard* pboard,
NSArray** urls,
NSArray** titles) {
NSArray* bookmarkPairs = base::mac::ObjCCast<NSArray>([pboard
propertyListForType:UTIFromPboardType(kWebURLsWithTitlesPboardType)]);
if (!bookmarkPairs)
return false;
if ([bookmarkPairs count] != 2)
return false;
NSArray* urlsArr =
base::mac::ObjCCast<NSArray>([bookmarkPairs objectAtIndex:0]);
NSArray* titlesArr =
base::mac::ObjCCast<NSArray>([bookmarkPairs objectAtIndex:1]);
if (!urlsArr || !titlesArr)
return false;
if ([urlsArr count] < 1)
return false;
if ([urlsArr count] != [titlesArr count])
return false;
for (id obj in urlsArr) {
if (![obj isKindOfClass:[NSString class]])
return false;
}
for (id obj in titlesArr) {
if (![obj isKindOfClass:[NSString class]])
return false;
}
*urls = urlsArr;
*titles = titlesArr;
return true;
}
} // namespace ui
|