Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: ui/base/clipboard/clipboard_mac.mm

Issue 8803006: Implement Clipboard::ReadData and Clipboard::WriteData for Mac. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/clipboard/clipboard.h" 5 #include "ui/base/clipboard/clipboard.h"
6 6
7 #import <Cocoa/Cocoa.h> 7 #import <Cocoa/Cocoa.h>
8 8
9 #include "base/file_path.h" 9 #include "base/file_path.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 // For now, spit out the image as a TIFF. 146 // For now, spit out the image as a TIFF.
147 NSPasteboard* pb = GetPasteboard(); 147 NSPasteboard* pb = GetPasteboard();
148 [pb addTypes:[NSArray arrayWithObject:NSTIFFPboardType] owner:nil]; 148 [pb addTypes:[NSArray arrayWithObject:NSTIFFPboardType] owner:nil];
149 NSData *tiff_data = [image TIFFRepresentation]; 149 NSData *tiff_data = [image TIFFRepresentation];
150 LOG_IF(ERROR, tiff_data == NULL) << "Failed to allocate image for clipboard"; 150 LOG_IF(ERROR, tiff_data == NULL) << "Failed to allocate image for clipboard";
151 if (tiff_data) { 151 if (tiff_data) {
152 [pb setData:tiff_data forType:NSTIFFPboardType]; 152 [pb setData:tiff_data forType:NSTIFFPboardType];
153 } 153 }
154 } 154 }
155 155
156 void Clipboard::WriteData(const char* format_name, size_t format_len,
157 const char* data_data, size_t data_len) {
158 NSPasteboard* pb = GetPasteboard();
159 NSString* format = [[NSString alloc] initWithBytes:format_name
160 length:format_len
161 encoding:NSUTF8StringEncoding];
162 [pb addTypes:[NSArray arrayWithObject:format] owner:nil];
163 [pb setData:[NSData dataWithBytes:data_data length:data_len]
164 forType:format];
165 }
166
156 // Write an extra flavor that signifies WebKit was the last to modify the 167 // Write an extra flavor that signifies WebKit was the last to modify the
157 // pasteboard. This flavor has no data. 168 // pasteboard. This flavor has no data.
158 void Clipboard::WriteWebSmartPaste() { 169 void Clipboard::WriteWebSmartPaste() {
159 NSPasteboard* pb = GetPasteboard(); 170 NSPasteboard* pb = GetPasteboard();
160 NSString* format = base::SysUTF8ToNSString(GetWebKitSmartPasteFormatType()); 171 NSString* format = base::SysUTF8ToNSString(GetWebKitSmartPasteFormatType());
161 [pb addTypes:[NSArray arrayWithObject:format] owner:nil]; 172 [pb addTypes:[NSArray arrayWithObject:format] owner:nil];
162 [pb setData:nil forType:format]; 173 [pb setData:nil forType:format];
163 } 174 }
164 175
165 uint64 Clipboard::GetSequenceNumber(Buffer buffer) { 176 uint64 Clipboard::GetSequenceNumber(Buffer buffer) {
166 DCHECK_EQ(buffer, BUFFER_STANDARD); 177 DCHECK_EQ(buffer, BUFFER_STANDARD);
167 178
168 NSPasteboard* pb = GetPasteboard(); 179 NSPasteboard* pb = GetPasteboard();
169 return [pb changeCount]; 180 return [pb changeCount];
170 } 181 }
171 182
172 bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format, 183 bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format,
173 Clipboard::Buffer buffer) const { 184 Buffer buffer) const {
174 DCHECK_EQ(buffer, BUFFER_STANDARD); 185 DCHECK_EQ(buffer, BUFFER_STANDARD);
175 NSString* format_ns = base::SysUTF8ToNSString(format); 186 NSString* format_ns = base::SysUTF8ToNSString(format);
176 187
177 NSPasteboard* pb = GetPasteboard(); 188 NSPasteboard* pb = GetPasteboard();
178 NSArray* types = [pb types]; 189 NSArray* types = [pb types];
179 190
180 // Safari only places RTF on the pasteboard, never HTML. We can convert RTF 191 // Safari only places RTF on the pasteboard, never HTML. We can convert RTF
181 // to HTML, so the presence of either indicates success when looking for HTML. 192 // to HTML, so the presence of either indicates success when looking for HTML.
182 if ([format_ns isEqualToString:NSHTMLPboardType]) { 193 if ([format_ns isEqualToString:NSHTMLPboardType]) {
183 return [types containsObject:NSHTMLPboardType] || 194 return [types containsObject:NSHTMLPboardType] ||
184 [types containsObject:NSRTFPboardType]; 195 [types containsObject:NSRTFPboardType];
185 } 196 }
186 return [types containsObject:format_ns]; 197 return [types containsObject:format_ns];
187 } 198 }
188 199
200 bool Clipboard::IsFormatAvailableByString(const std::string& format,
201 Buffer buffer) const {
202 DCHECK_EQ(buffer, BUFFER_STANDARD);
203 NSString* format_ns = base::SysUTF8ToNSString(format);
204
205 NSPasteboard* pb = GetPasteboard();
206 NSArray* types = [pb types];
207
208 return [types containsObject:format_ns];
209 }
210
189 void Clipboard::ReadAvailableTypes(Clipboard::Buffer buffer, 211 void Clipboard::ReadAvailableTypes(Clipboard::Buffer buffer,
190 std::vector<string16>* types, 212 std::vector<string16>* types,
191 bool* contains_filenames) const { 213 bool* contains_filenames) const {
192 if (!types || !contains_filenames) { 214 if (!types || !contains_filenames) {
193 NOTREACHED(); 215 NOTREACHED();
194 return; 216 return;
195 } 217 }
196 218
197 types->clear(); 219 types->clear();
198 if (IsFormatAvailable(Clipboard::GetPlainTextFormatType(), buffer)) 220 if (IsFormatAvailable(Clipboard::GetPlainTextFormatType(), buffer))
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 357
336 NSPasteboard* pb = GetPasteboard(); 358 NSPasteboard* pb = GetPasteboard();
337 NSArray* fileList = [pb propertyListForType:NSFilenamesPboardType]; 359 NSArray* fileList = [pb propertyListForType:NSFilenamesPboardType];
338 360
339 for (unsigned int i = 0; i < [fileList count]; ++i) { 361 for (unsigned int i = 0; i < [fileList count]; ++i) {
340 std::string file = [[fileList objectAtIndex:i] UTF8String]; 362 std::string file = [[fileList objectAtIndex:i] UTF8String];
341 files->push_back(FilePath(file)); 363 files->push_back(FilePath(file));
342 } 364 }
343 } 365 }
344 366
367 void Clipboard::ReadData(const std::string& format, std::string* result) const {
368 NSPasteboard* pb = GetPasteboard();
369 NSData* data = [pb dataForType:base::SysUTF8ToNSString(format)];
370 if ([data length])
371 result->assign(reinterpret_cast<const char*>([data bytes]), [data length]);
tony 2011/12/05 18:56:09 Nit: I think you can static_cast here.
dcheng 2011/12/05 19:53:03 Done.
372 }
373
345 // static 374 // static
346 Clipboard::FormatType Clipboard::GetUrlFormatType() { 375 Clipboard::FormatType Clipboard::GetUrlFormatType() {
347 return base::SysNSStringToUTF8(NSURLPboardType); 376 return base::SysNSStringToUTF8(NSURLPboardType);
348 } 377 }
349 378
350 // static 379 // static
351 Clipboard::FormatType Clipboard::GetUrlWFormatType() { 380 Clipboard::FormatType Clipboard::GetUrlWFormatType() {
352 return base::SysNSStringToUTF8(NSURLPboardType); 381 return base::SysNSStringToUTF8(NSURLPboardType);
353 } 382 }
354 383
(...skipping 26 matching lines...) Expand all
381 Clipboard::FormatType Clipboard::GetBitmapFormatType() { 410 Clipboard::FormatType Clipboard::GetBitmapFormatType() {
382 return base::SysNSStringToUTF8(NSTIFFPboardType); 411 return base::SysNSStringToUTF8(NSTIFFPboardType);
383 } 412 }
384 413
385 // static 414 // static
386 Clipboard::FormatType Clipboard::GetWebKitSmartPasteFormatType() { 415 Clipboard::FormatType Clipboard::GetWebKitSmartPasteFormatType() {
387 return base::SysNSStringToUTF8(kWebSmartPastePboardType); 416 return base::SysNSStringToUTF8(kWebSmartPastePboardType);
388 } 417 }
389 418
390 } // namespace ui 419 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698