Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / base / dragdrop / os_exchange_data_provider_mac.mm
blob096b8066209089cce5ff82ab57b3b8c6a7011bc3
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"
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   [pasteboard_ setDataForURL:base::SysUTF8ToNSString(url.spec())
49                        title:base::SysUTF16ToNSString(title)];
52 void OSExchangeDataProviderMac::SetFilename(const base::FilePath& path) {
53   NOTIMPLEMENTED();
56 void OSExchangeDataProviderMac::SetFilenames(
57     const std::vector<FileInfo>& filenames) {
58   NOTIMPLEMENTED();
61 void OSExchangeDataProviderMac::SetPickledData(
62     const OSExchangeData::CustomFormat& format,
63     const Pickle& data) {
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 {
69   DCHECK(data);
70   NSArray* items = [pasteboard_ readObjectsForClasses:@[ [NSString class] ]
71                                               options:@{ }];
72   if ([items count] == 0)
73     return false;
75   *data = base::SysNSStringToUTF16([items objectAtIndex:0]);
76   return true;
79 bool OSExchangeDataProviderMac::GetURLAndTitle(
80     OSExchangeData::FilenameToURLPolicy policy,
81     GURL* url,
82     base::string16* title) const {
83   return PopulateURLAndTitleFromPasteboard(
84       url, title, pasteboard_, policy == OSExchangeData::CONVERT_FILENAMES);
87 bool OSExchangeDataProviderMac::GetFilename(base::FilePath* path) const {
88   NOTIMPLEMENTED();
89   return false;
92 bool OSExchangeDataProviderMac::GetFilenames(
93     std::vector<FileInfo>* filenames) const {
94   NOTIMPLEMENTED();
95   return false;
98 bool OSExchangeDataProviderMac::GetPickledData(
99     const OSExchangeData::CustomFormat& format,
100     Pickle* data) const {
101   DCHECK(data);
102   NSData* ns_data = [pasteboard_ dataForType:format.ToNSString()];
103   if (!ns_data)
104     return false;
106   *data = Pickle(static_cast<const char*>([ns_data bytes]), [ns_data length]);
107   return true;
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 {
117   GURL url;
118   base::string16 title;
119   return GetURLAndTitle(policy, &url, &title);
122 bool OSExchangeDataProviderMac::HasFile() const {
123   NOTIMPLEMENTED();
124   return false;
127 bool OSExchangeDataProviderMac::HasCustomFormat(
128     const OSExchangeData::CustomFormat& format) const {
129   return [[pasteboard_ types] containsObject:format.ToNSString()];
132 ///////////////////////////////////////////////////////////////////////////////
133 // OSExchangeData, public:
135 // static
136 OSExchangeData::Provider* OSExchangeData::CreateProvider() {
137   return new OSExchangeDataProviderMac;
140 }  // namespace ui