Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / base / dragdrop / os_exchange_data_provider_mac.mm
blob94e4bed9c32e82d7ea934bc8bfb878116d50d385
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 #include "url/gurl.h"
15 namespace ui {
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() {
33   NOTIMPLEMENTED();
36 bool OSExchangeDataProviderMac::DidOriginateFromRenderer() const {
37   NOTIMPLEMENTED();
38   return false;
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) {
61   NOTIMPLEMENTED();
64 void OSExchangeDataProviderMac::SetPickledData(
65     const OSExchangeData::CustomFormat& format,
66     const Pickle& data) {
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 {
72   DCHECK(data);
73   NSArray* items = [pasteboard_ readObjectsForClasses:@[ [NSString class] ]
74                                               options:@{ }];
75   if ([items count] == 0)
76     return false;
78   *data = base::SysNSStringToUTF16([items objectAtIndex:0]);
79   return true;
82 bool OSExchangeDataProviderMac::GetURLAndTitle(
83     OSExchangeData::FilenameToURLPolicy policy,
84     GURL* url,
85     base::string16* title) const {
86   DCHECK(url);
87   DCHECK(title);
88   NSArray* items = [pasteboard_ readObjectsForClasses:@[ [NSURL class] ]
89                                               options:@{ }];
90   if ([items count] == 0)
91     return false;
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])
102         return false;
103     }
104   }
106   *url = GURL([[ns_url absoluteString] UTF8String]);
107   *title = base::SysNSStringToUTF16(
108       [pasteboard_ stringForType:kCorePasteboardFlavorType_urln]);
109   return true;
112 bool OSExchangeDataProviderMac::GetFilename(base::FilePath* path) const {
113   NSArray* paths = [pasteboard_ propertyListForType:NSFilenamesPboardType];
114   if ([paths count] == 0)
115     return false;
117   *path = base::FilePath([[paths objectAtIndex:0] UTF8String]);
118   return true;
121 bool OSExchangeDataProviderMac::GetFilenames(
122     std::vector<FileInfo>* filenames) const {
123   NOTIMPLEMENTED();
124   return false;
127 bool OSExchangeDataProviderMac::GetPickledData(
128     const OSExchangeData::CustomFormat& format,
129     Pickle* data) const {
130   DCHECK(data);
131   NSData* ns_data = [pasteboard_ dataForType:format.ToNSString()];
132   if (!ns_data)
133     return false;
135   *data = Pickle(static_cast<const char*>([ns_data bytes]), [ns_data length]);
136   return true;
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 {
146   GURL url;
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:
163 // static
164 OSExchangeData::Provider* OSExchangeData::CreateProvider() {
165   return new OSExchangeDataProviderMac;
168 }  // namespace ui