OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/base/dragdrop/os_exchange_data_provider_mac.h" | 5 #include "ui/base/dragdrop/os_exchange_data_provider_mac.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/pickle.h" | 10 #include "base/pickle.h" |
11 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
12 #import "third_party/mozilla/NSPasteboard+Utils.h" | 12 #import "third_party/mozilla/NSPasteboard+Utils.h" |
13 #import "ui/base/dragdrop/cocoa_dnd_util.h" | |
14 #include "url/gurl.h" | 13 #include "url/gurl.h" |
15 | 14 |
16 namespace ui { | 15 namespace ui { |
17 | 16 |
18 OSExchangeDataProviderMac::OSExchangeDataProviderMac() | 17 OSExchangeDataProviderMac::OSExchangeDataProviderMac() |
19 : pasteboard_([[NSPasteboard pasteboardWithUniqueName] retain]) { | 18 : pasteboard_([[NSPasteboard pasteboardWithUniqueName] retain]) { |
20 } | 19 } |
21 | 20 |
22 OSExchangeDataProviderMac::OSExchangeDataProviderMac(NSPasteboard* pasteboard) | 21 OSExchangeDataProviderMac::OSExchangeDataProviderMac(NSPasteboard* pasteboard) |
23 : pasteboard_([pasteboard retain]) { | 22 : pasteboard_([pasteboard retain]) { |
(...skipping 14 matching lines...) Expand all Loading... |
38 NOTIMPLEMENTED(); | 37 NOTIMPLEMENTED(); |
39 return false; | 38 return false; |
40 } | 39 } |
41 | 40 |
42 void OSExchangeDataProviderMac::SetString(const base::string16& string) { | 41 void OSExchangeDataProviderMac::SetString(const base::string16& string) { |
43 [pasteboard_ writeObjects:@[ base::SysUTF16ToNSString(string) ]]; | 42 [pasteboard_ writeObjects:@[ base::SysUTF16ToNSString(string) ]]; |
44 } | 43 } |
45 | 44 |
46 void OSExchangeDataProviderMac::SetURL(const GURL& url, | 45 void OSExchangeDataProviderMac::SetURL(const GURL& url, |
47 const base::string16& title) { | 46 const base::string16& title) { |
48 [pasteboard_ setDataForURL:base::SysUTF8ToNSString(url.spec()) | 47 NSURL* ns_url = [NSURL URLWithString:base::SysUTF8ToNSString(url.spec())]; |
49 title:base::SysUTF16ToNSString(title)]; | 48 [pasteboard_ writeObjects:@[ ns_url ]]; |
| 49 |
| 50 [pasteboard_ setString:base::SysUTF16ToNSString(title) |
| 51 forType:kCorePasteboardFlavorType_urln]; |
50 } | 52 } |
51 | 53 |
52 void OSExchangeDataProviderMac::SetFilename(const base::FilePath& path) { | 54 void OSExchangeDataProviderMac::SetFilename(const base::FilePath& path) { |
53 NOTIMPLEMENTED(); | 55 [pasteboard_ setPropertyList:@[ base::SysUTF8ToNSString(path.value()) ] |
| 56 forType:NSFilenamesPboardType]; |
54 } | 57 } |
55 | 58 |
56 void OSExchangeDataProviderMac::SetFilenames( | 59 void OSExchangeDataProviderMac::SetFilenames( |
57 const std::vector<FileInfo>& filenames) { | 60 const std::vector<FileInfo>& filenames) { |
58 NOTIMPLEMENTED(); | 61 NOTIMPLEMENTED(); |
59 } | 62 } |
60 | 63 |
61 void OSExchangeDataProviderMac::SetPickledData( | 64 void OSExchangeDataProviderMac::SetPickledData( |
62 const OSExchangeData::CustomFormat& format, | 65 const OSExchangeData::CustomFormat& format, |
63 const Pickle& data) { | 66 const Pickle& data) { |
64 NSData* ns_data = [NSData dataWithBytes:data.data() length:data.size()]; | 67 NSData* ns_data = [NSData dataWithBytes:data.data() length:data.size()]; |
65 [pasteboard_ setData:ns_data forType:format.ToNSString()]; | 68 [pasteboard_ setData:ns_data forType:format.ToNSString()]; |
66 } | 69 } |
67 | 70 |
68 bool OSExchangeDataProviderMac::GetString(base::string16* data) const { | 71 bool OSExchangeDataProviderMac::GetString(base::string16* data) const { |
69 DCHECK(data); | 72 DCHECK(data); |
70 NSArray* items = [pasteboard_ readObjectsForClasses:@[ [NSString class] ] | 73 NSArray* items = [pasteboard_ readObjectsForClasses:@[ [NSString class] ] |
71 options:@{ }]; | 74 options:@{ }]; |
72 if ([items count] == 0) | 75 if ([items count] == 0) |
73 return false; | 76 return false; |
74 | 77 |
75 *data = base::SysNSStringToUTF16([items objectAtIndex:0]); | 78 *data = base::SysNSStringToUTF16([items objectAtIndex:0]); |
76 return true; | 79 return true; |
77 } | 80 } |
78 | 81 |
79 bool OSExchangeDataProviderMac::GetURLAndTitle( | 82 bool OSExchangeDataProviderMac::GetURLAndTitle( |
80 OSExchangeData::FilenameToURLPolicy policy, | 83 OSExchangeData::FilenameToURLPolicy policy, |
81 GURL* url, | 84 GURL* url, |
82 base::string16* title) const { | 85 base::string16* title) const { |
83 return PopulateURLAndTitleFromPasteboard( | 86 DCHECK(url); |
84 url, title, pasteboard_, policy == OSExchangeData::CONVERT_FILENAMES); | 87 DCHECK(title); |
| 88 NSArray* items = [pasteboard_ readObjectsForClasses:@[ [NSURL class] ] |
| 89 options:@{ }]; |
| 90 if ([items count] == 0) |
| 91 return false; |
| 92 |
| 93 NSURL* ns_url = [items objectAtIndex:0]; |
| 94 |
| 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 } |
| 105 |
| 106 *url = GURL([[ns_url absoluteString] UTF8String]); |
| 107 *title = base::SysNSStringToUTF16( |
| 108 [pasteboard_ stringForType:kCorePasteboardFlavorType_urln]); |
| 109 return true; |
85 } | 110 } |
86 | 111 |
87 bool OSExchangeDataProviderMac::GetFilename(base::FilePath* path) const { | 112 bool OSExchangeDataProviderMac::GetFilename(base::FilePath* path) const { |
88 NOTIMPLEMENTED(); | 113 NSArray* paths = [pasteboard_ propertyListForType:NSFilenamesPboardType]; |
89 return false; | 114 if ([paths count] == 0) |
| 115 return false; |
| 116 |
| 117 *path = base::FilePath([[paths objectAtIndex:0] UTF8String]); |
| 118 return true; |
90 } | 119 } |
91 | 120 |
92 bool OSExchangeDataProviderMac::GetFilenames( | 121 bool OSExchangeDataProviderMac::GetFilenames( |
93 std::vector<FileInfo>* filenames) const { | 122 std::vector<FileInfo>* filenames) const { |
94 NOTIMPLEMENTED(); | 123 NOTIMPLEMENTED(); |
95 return false; | 124 return false; |
96 } | 125 } |
97 | 126 |
98 bool OSExchangeDataProviderMac::GetPickledData( | 127 bool OSExchangeDataProviderMac::GetPickledData( |
99 const OSExchangeData::CustomFormat& format, | 128 const OSExchangeData::CustomFormat& format, |
(...skipping 13 matching lines...) Expand all Loading... |
113 } | 142 } |
114 | 143 |
115 bool OSExchangeDataProviderMac::HasURL( | 144 bool OSExchangeDataProviderMac::HasURL( |
116 OSExchangeData::FilenameToURLPolicy policy) const { | 145 OSExchangeData::FilenameToURLPolicy policy) const { |
117 GURL url; | 146 GURL url; |
118 base::string16 title; | 147 base::string16 title; |
119 return GetURLAndTitle(policy, &url, &title); | 148 return GetURLAndTitle(policy, &url, &title); |
120 } | 149 } |
121 | 150 |
122 bool OSExchangeDataProviderMac::HasFile() const { | 151 bool OSExchangeDataProviderMac::HasFile() const { |
123 NOTIMPLEMENTED(); | 152 return [[pasteboard_ types] containsObject:NSFilenamesPboardType]; |
124 return false; | |
125 } | 153 } |
126 | 154 |
127 bool OSExchangeDataProviderMac::HasCustomFormat( | 155 bool OSExchangeDataProviderMac::HasCustomFormat( |
128 const OSExchangeData::CustomFormat& format) const { | 156 const OSExchangeData::CustomFormat& format) const { |
129 return [[pasteboard_ types] containsObject:format.ToNSString()]; | 157 return [[pasteboard_ types] containsObject:format.ToNSString()]; |
130 } | 158 } |
131 | 159 |
132 /////////////////////////////////////////////////////////////////////////////// | 160 /////////////////////////////////////////////////////////////////////////////// |
133 // OSExchangeData, public: | 161 // OSExchangeData, public: |
134 | 162 |
135 // static | 163 // static |
136 OSExchangeData::Provider* OSExchangeData::CreateProvider() { | 164 OSExchangeData::Provider* OSExchangeData::CreateProvider() { |
137 return new OSExchangeDataProviderMac; | 165 return new OSExchangeDataProviderMac; |
138 } | 166 } |
139 | 167 |
140 } // namespace ui | 168 } // namespace ui |
OLD | NEW |