Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / base / dragdrop / os_exchange_data_provider_mac.mm
blob1178f1d3f631491fe07bffa72de43e13b345180d
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"
14 #include "url/gurl.h"
16 namespace ui {
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() {
34   NOTIMPLEMENTED();
37 bool OSExchangeDataProviderMac::DidOriginateFromRenderer() const {
38   NOTIMPLEMENTED();
39   return false;
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) {
62   NOTIMPLEMENTED();
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 {
73   DCHECK(data);
74   NSArray* items = [pasteboard_ readObjectsForClasses:@[ [NSString class] ]
75                                               options:@{ }];
77   // There was no NSString, check for an NSURL.
78   if ([items count] == 0) {
79     GURL url;
80     base::string16 title;
81     bool result =
82         GetURLAndTitle(OSExchangeData::DO_NOT_CONVERT_FILENAMES, &url, &title);
83     if (result)
84       *data = base::UTF8ToUTF16(url.spec());
85     return result;
86   }
88   *data = base::SysNSStringToUTF16([items objectAtIndex:0]);
89   return true;
92 bool OSExchangeDataProviderMac::GetURLAndTitle(
93     OSExchangeData::FilenameToURLPolicy policy,
94     GURL* url,
95     base::string16* title) const {
96   DCHECK(url);
97   DCHECK(title);
98   NSArray* items = [pasteboard_ readObjectsForClasses:@[ [NSURL class] ]
99                                               options:@{ }];
100   if ([items count] == 0)
101     return false;
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])
112         return false;
113     }
114   }
116   *url = GURL([[ns_url absoluteString] UTF8String]);
117   *title = base::SysNSStringToUTF16(
118       [pasteboard_ stringForType:kCorePasteboardFlavorType_urln]);
119   return true;
122 bool OSExchangeDataProviderMac::GetFilename(base::FilePath* path) const {
123   NSArray* paths = [pasteboard_ propertyListForType:NSFilenamesPboardType];
124   if ([paths count] == 0)
125     return false;
127   *path = base::FilePath([[paths objectAtIndex:0] UTF8String]);
128   return true;
131 bool OSExchangeDataProviderMac::GetFilenames(
132     std::vector<FileInfo>* filenames) const {
133   NOTIMPLEMENTED();
134   return false;
137 bool OSExchangeDataProviderMac::GetPickledData(
138     const OSExchangeData::CustomFormat& format,
139     base::Pickle* data) const {
140   DCHECK(data);
141   NSData* ns_data = [pasteboard_ dataForType:format.ToNSString()];
142   if (!ns_data)
143     return false;
145   *data =
146       base::Pickle(static_cast<const char*>([ns_data bytes]), [ns_data length]);
147   return true;
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 {
157   GURL url;
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:
174 // static
175 OSExchangeData::Provider* OSExchangeData::CreateProvider() {
176   return new OSExchangeDataProviderMac;
179 }  // namespace ui