1 // Copyright 2014 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.
5 #include "ui/base/dragdrop/os_exchange_data_provider_mac.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/logging.h"
10 #include "base/pickle.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #import "third_party/mozilla/NSPasteboard+Utils.h"
18 OSExchangeDataProviderMac::OSExchangeDataProviderMac()
19 : pasteboard_([[NSPasteboard pasteboardWithUniqueName] retain]) {
22 OSExchangeDataProviderMac::OSExchangeDataProviderMac(NSPasteboard* pasteboard)
23 : pasteboard_([pasteboard retain]) {
26 OSExchangeDataProviderMac::~OSExchangeDataProviderMac() {
29 OSExchangeData::Provider* OSExchangeDataProviderMac::Clone() const {
30 return new OSExchangeDataProviderMac(pasteboard_);
33 void OSExchangeDataProviderMac::MarkOriginatedFromRenderer() {
37 bool OSExchangeDataProviderMac::DidOriginateFromRenderer() const {
42 void OSExchangeDataProviderMac::SetString(const base::string16& string) {
43 [pasteboard_ writeObjects:@[ base::SysUTF16ToNSString(string) ]];
46 void OSExchangeDataProviderMac::SetURL(const GURL& url,
47 const base::string16& title) {
48 NSURL* ns_url = [NSURL URLWithString:base::SysUTF8ToNSString(url.spec())];
49 [pasteboard_ writeObjects:@[ ns_url ]];
51 [pasteboard_ setString:base::SysUTF16ToNSString(title)
52 forType:kCorePasteboardFlavorType_urln];
55 void OSExchangeDataProviderMac::SetFilename(const base::FilePath& path) {
56 [pasteboard_ setPropertyList:@[ base::SysUTF8ToNSString(path.value()) ]
57 forType:NSFilenamesPboardType];
60 void OSExchangeDataProviderMac::SetFilenames(
61 const std::vector<FileInfo>& filenames) {
65 void OSExchangeDataProviderMac::SetPickledData(
66 const OSExchangeData::CustomFormat& format,
67 const base::Pickle& data) {
68 NSData* ns_data = [NSData dataWithBytes:data.data() length:data.size()];
69 [pasteboard_ setData:ns_data forType:format.ToNSString()];
72 bool OSExchangeDataProviderMac::GetString(base::string16* data) const {
74 NSArray* items = [pasteboard_ readObjectsForClasses:@[ [NSString class] ]
77 // There was no NSString, check for an NSURL.
78 if ([items count] == 0) {
82 GetURLAndTitle(OSExchangeData::DO_NOT_CONVERT_FILENAMES, &url, &title);
84 *data = base::UTF8ToUTF16(url.spec());
88 *data = base::SysNSStringToUTF16([items objectAtIndex:0]);
92 bool OSExchangeDataProviderMac::GetURLAndTitle(
93 OSExchangeData::FilenameToURLPolicy policy,
95 base::string16* title) const {
98 NSArray* items = [pasteboard_ readObjectsForClasses:@[ [NSURL class] ]
100 if ([items count] == 0)
103 NSURL* ns_url = [items objectAtIndex:0];
105 if (policy == OSExchangeData::DO_NOT_CONVERT_FILENAMES) {
106 // If the URL matches a filename, assume that it came from SetFilename().
107 // Don't return it if we are not supposed to convert filename to URL.
108 NSArray* paths = [pasteboard_ propertyListForType:NSFilenamesPboardType];
109 NSString* url_path = [[ns_url path] stringByStandardizingPath];
110 for (NSString* path in paths) {
111 if ([[path stringByStandardizingPath] isEqualToString:url_path])
116 *url = GURL([[ns_url absoluteString] UTF8String]);
117 *title = base::SysNSStringToUTF16(
118 [pasteboard_ stringForType:kCorePasteboardFlavorType_urln]);
122 bool OSExchangeDataProviderMac::GetFilename(base::FilePath* path) const {
123 NSArray* paths = [pasteboard_ propertyListForType:NSFilenamesPboardType];
124 if ([paths count] == 0)
127 *path = base::FilePath([[paths objectAtIndex:0] UTF8String]);
131 bool OSExchangeDataProviderMac::GetFilenames(
132 std::vector<FileInfo>* filenames) const {
137 bool OSExchangeDataProviderMac::GetPickledData(
138 const OSExchangeData::CustomFormat& format,
139 base::Pickle* data) const {
141 NSData* ns_data = [pasteboard_ dataForType:format.ToNSString()];
146 base::Pickle(static_cast<const char*>([ns_data bytes]), [ns_data length]);
150 bool OSExchangeDataProviderMac::HasString() const {
151 NSArray* classes = @[ [NSString class] ];
152 return [pasteboard_ canReadObjectForClasses:classes options:nil];
155 bool OSExchangeDataProviderMac::HasURL(
156 OSExchangeData::FilenameToURLPolicy policy) const {
158 base::string16 title;
159 return GetURLAndTitle(policy, &url, &title);
162 bool OSExchangeDataProviderMac::HasFile() const {
163 return [[pasteboard_ types] containsObject:NSFilenamesPboardType];
166 bool OSExchangeDataProviderMac::HasCustomFormat(
167 const OSExchangeData::CustomFormat& format) const {
168 return [[pasteboard_ types] containsObject:format.ToNSString()];
171 ///////////////////////////////////////////////////////////////////////////////
172 // OSExchangeData, public:
175 OSExchangeData::Provider* OSExchangeData::CreateProvider() {
176 return new OSExchangeDataProviderMac;