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 #import "third_party/mozilla/NSPasteboard+Utils.h"
17 OSExchangeDataProviderMac::OSExchangeDataProviderMac()
18 : pasteboard_([[NSPasteboard pasteboardWithUniqueName] retain]) {
21 OSExchangeDataProviderMac::OSExchangeDataProviderMac(NSPasteboard* pasteboard)
22 : pasteboard_([pasteboard retain]) {
25 OSExchangeDataProviderMac::~OSExchangeDataProviderMac() {
28 OSExchangeData::Provider* OSExchangeDataProviderMac::Clone() const {
29 return new OSExchangeDataProviderMac(pasteboard_);
32 void OSExchangeDataProviderMac::MarkOriginatedFromRenderer() {
36 bool OSExchangeDataProviderMac::DidOriginateFromRenderer() const {
41 void OSExchangeDataProviderMac::SetString(const base::string16& string) {
42 [pasteboard_ writeObjects:@[ base::SysUTF16ToNSString(string) ]];
45 void OSExchangeDataProviderMac::SetURL(const GURL& url,
46 const base::string16& title) {
47 NSURL* ns_url = [NSURL URLWithString:base::SysUTF8ToNSString(url.spec())];
48 [pasteboard_ writeObjects:@[ ns_url ]];
50 [pasteboard_ setString:base::SysUTF16ToNSString(title)
51 forType:kCorePasteboardFlavorType_urln];
54 void OSExchangeDataProviderMac::SetFilename(const base::FilePath& path) {
55 [pasteboard_ setPropertyList:@[ base::SysUTF8ToNSString(path.value()) ]
56 forType:NSFilenamesPboardType];
59 void OSExchangeDataProviderMac::SetFilenames(
60 const std::vector<FileInfo>& filenames) {
64 void OSExchangeDataProviderMac::SetPickledData(
65 const OSExchangeData::CustomFormat& format,
67 NSData* ns_data = [NSData dataWithBytes:data.data() length:data.size()];
68 [pasteboard_ setData:ns_data forType:format.ToNSString()];
71 bool OSExchangeDataProviderMac::GetString(base::string16* data) const {
73 NSArray* items = [pasteboard_ readObjectsForClasses:@[ [NSString class] ]
75 if ([items count] == 0)
78 *data = base::SysNSStringToUTF16([items objectAtIndex:0]);
82 bool OSExchangeDataProviderMac::GetURLAndTitle(
83 OSExchangeData::FilenameToURLPolicy policy,
85 base::string16* title) const {
88 NSArray* items = [pasteboard_ readObjectsForClasses:@[ [NSURL class] ]
90 if ([items count] == 0)
93 NSURL* ns_url = [items objectAtIndex:0];
95 if (policy == OSExchangeData::DO_NOT_CONVERT_FILENAMES) {
96 // If the URL matches a filename, assume that it came from SetFilename().
97 // Don't return it if we are not supposed to convert filename to URL.
98 NSArray* paths = [pasteboard_ propertyListForType:NSFilenamesPboardType];
99 NSString* url_path = [[ns_url path] stringByStandardizingPath];
100 for (NSString* path in paths) {
101 if ([[path stringByStandardizingPath] isEqualToString:url_path])
106 *url = GURL([[ns_url absoluteString] UTF8String]);
107 *title = base::SysNSStringToUTF16(
108 [pasteboard_ stringForType:kCorePasteboardFlavorType_urln]);
112 bool OSExchangeDataProviderMac::GetFilename(base::FilePath* path) const {
113 NSArray* paths = [pasteboard_ propertyListForType:NSFilenamesPboardType];
114 if ([paths count] == 0)
117 *path = base::FilePath([[paths objectAtIndex:0] UTF8String]);
121 bool OSExchangeDataProviderMac::GetFilenames(
122 std::vector<FileInfo>* filenames) const {
127 bool OSExchangeDataProviderMac::GetPickledData(
128 const OSExchangeData::CustomFormat& format,
129 Pickle* data) const {
131 NSData* ns_data = [pasteboard_ dataForType:format.ToNSString()];
135 *data = Pickle(static_cast<const char*>([ns_data bytes]), [ns_data length]);
139 bool OSExchangeDataProviderMac::HasString() const {
140 NSArray* classes = @[ [NSString class] ];
141 return [pasteboard_ canReadObjectForClasses:classes options:nil];
144 bool OSExchangeDataProviderMac::HasURL(
145 OSExchangeData::FilenameToURLPolicy policy) const {
147 base::string16 title;
148 return GetURLAndTitle(policy, &url, &title);
151 bool OSExchangeDataProviderMac::HasFile() const {
152 return [[pasteboard_ types] containsObject:NSFilenamesPboardType];
155 bool OSExchangeDataProviderMac::HasCustomFormat(
156 const OSExchangeData::CustomFormat& format) const {
157 return [[pasteboard_ types] containsObject:format.ToNSString()];
160 ///////////////////////////////////////////////////////////////////////////////
161 // OSExchangeData, public:
164 OSExchangeData::Provider* OSExchangeData::CreateProvider() {
165 return new OSExchangeDataProviderMac;