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"
13 #import "ui/base/dragdrop/cocoa_dnd_util.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 [pasteboard_ setDataForURL:base::SysUTF8ToNSString(url.spec())
49 title:base::SysUTF16ToNSString(title)];
52 void OSExchangeDataProviderMac::SetFilename(const base::FilePath& path) {
56 void OSExchangeDataProviderMac::SetFilenames(
57 const std::vector<FileInfo>& filenames) {
61 void OSExchangeDataProviderMac::SetPickledData(
62 const OSExchangeData::CustomFormat& format,
64 NSData* ns_data = [NSData dataWithBytes:data.data() length:data.size()];
65 [pasteboard_ setData:ns_data forType:format.ToNSString()];
68 bool OSExchangeDataProviderMac::GetString(base::string16* data) const {
70 NSArray* items = [pasteboard_ readObjectsForClasses:@[ [NSString class] ]
72 if ([items count] == 0)
75 *data = base::SysNSStringToUTF16([items objectAtIndex:0]);
79 bool OSExchangeDataProviderMac::GetURLAndTitle(
80 OSExchangeData::FilenameToURLPolicy policy,
82 base::string16* title) const {
83 return PopulateURLAndTitleFromPasteboard(
84 url, title, pasteboard_, policy == OSExchangeData::CONVERT_FILENAMES);
87 bool OSExchangeDataProviderMac::GetFilename(base::FilePath* path) const {
92 bool OSExchangeDataProviderMac::GetFilenames(
93 std::vector<FileInfo>* filenames) const {
98 bool OSExchangeDataProviderMac::GetPickledData(
99 const OSExchangeData::CustomFormat& format,
100 Pickle* data) const {
102 NSData* ns_data = [pasteboard_ dataForType:format.ToNSString()];
106 *data = Pickle(static_cast<const char*>([ns_data bytes]), [ns_data length]);
110 bool OSExchangeDataProviderMac::HasString() const {
111 NSArray* classes = @[ [NSString class] ];
112 return [pasteboard_ canReadObjectForClasses:classes options:nil];
115 bool OSExchangeDataProviderMac::HasURL(
116 OSExchangeData::FilenameToURLPolicy policy) const {
118 base::string16 title;
119 return GetURLAndTitle(policy, &url, &title);
122 bool OSExchangeDataProviderMac::HasFile() const {
127 bool OSExchangeDataProviderMac::HasCustomFormat(
128 const OSExchangeData::CustomFormat& format) const {
129 return [[pasteboard_ types] containsObject:format.ToNSString()];
132 ///////////////////////////////////////////////////////////////////////////////
133 // OSExchangeData, public:
136 OSExchangeData::Provider* OSExchangeData::CreateProvider() {
137 return new OSExchangeDataProviderMac;