OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_mac.h" | 5 #include "ui/base/clipboard/clipboard_mac.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <limits> | 10 #include <limits> |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 *fragment_end = static_cast<uint32_t>(markup->length()); | 291 *fragment_end = static_cast<uint32_t>(markup->length()); |
292 } | 292 } |
293 | 293 |
294 void ClipboardMac::ReadRTF(ClipboardType type, std::string* result) const { | 294 void ClipboardMac::ReadRTF(ClipboardType type, std::string* result) const { |
295 DCHECK(CalledOnValidThread()); | 295 DCHECK(CalledOnValidThread()); |
296 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); | 296 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); |
297 | 297 |
298 return ReadData(GetRtfFormatType(), result); | 298 return ReadData(GetRtfFormatType(), result); |
299 } | 299 } |
300 | 300 |
301 SkBitmap ClipboardMac::ReadImage(ClipboardType type) const { | 301 SkBitmap ClipboardMac::ReadImage(ClipboardType type, NSPasteboard* pb) const { |
302 DCHECK(CalledOnValidThread()); | 302 DCHECK(CalledOnValidThread()); |
303 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); | 303 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); |
304 | 304 |
305 // If the pasteboard's image data is not to its liking, the guts of NSImage | 305 // If the pasteboard's image data is not to its liking, the guts of NSImage |
306 // may throw, and that exception will leak. Prevent a crash in that case; | 306 // may throw, and that exception will leak. Prevent a crash in that case; |
307 // a blank image is better. | 307 // a blank image is better. |
308 base::scoped_nsobject<NSImage> image; | 308 base::scoped_nsobject<NSImage> image; |
309 NSPasteboard* pb = GetPasteboard(); | |
310 @try { | 309 @try { |
311 if ([[pb types] containsObject:NSFilenamesPboardType]) { | 310 if ([[pb types] containsObject:NSFilenamesPboardType]) { |
312 // -[NSImage initWithPasteboard:] gets confused with copies of a single | 311 // -[NSImage initWithPasteboard:] gets confused with copies of a single |
313 // file from the Finder, so extract the path ourselves. | 312 // file from the Finder, so extract the path ourselves. |
314 // http://crbug.com/553686 | 313 // http://crbug.com/553686 |
315 NSArray* paths = [pb propertyListForType:NSFilenamesPboardType]; | 314 NSArray* paths = [pb propertyListForType:NSFilenamesPboardType]; |
316 if ([paths count]) { | 315 if ([paths count]) { |
317 // If N number of files are selected from finder, choose the last one. | 316 // If N number of files are selected from finder, choose the last one. |
318 image.reset([[NSImage alloc] | 317 image.reset([[NSImage alloc] |
319 initWithContentsOfURL:[NSURL fileURLWithPath:[paths lastObject]]]); | 318 initWithContentsOfURL:[NSURL fileURLWithPath:[paths lastObject]]]); |
320 } | 319 } |
321 } else { | 320 } else { |
322 if (pb) | 321 if (pb) |
323 image.reset([[NSImage alloc] initWithPasteboard:pb]); | 322 image.reset([[NSImage alloc] initWithPasteboard:pb]); |
324 } | 323 } |
325 } @catch (id exception) { | 324 } @catch (id exception) { |
326 } | 325 } |
327 | 326 |
328 SkBitmap bitmap; | 327 SkBitmap bitmap; |
329 if (image.get()) { | 328 if (image.get()) { |
330 bitmap = skia::NSImageToSkBitmapWithColorSpace( | 329 bitmap = skia::NSImageToSkBitmapWithColorSpace( |
331 image.get(), /*is_opaque=*/ false, base::mac::GetSystemColorSpace()); | 330 image.get(), /*is_opaque=*/ false, base::mac::GetSystemColorSpace()); |
332 } | 331 } |
333 return bitmap; | 332 return bitmap; |
334 } | 333 } |
335 | 334 |
| 335 SkBitmap ClipboardMac::ReadImage(ClipboardType type) const { |
| 336 return ReadImage(type, GetPasteboard()); |
| 337 } |
| 338 |
336 void ClipboardMac::ReadCustomData(ClipboardType clipboard_type, | 339 void ClipboardMac::ReadCustomData(ClipboardType clipboard_type, |
337 const base::string16& type, | 340 const base::string16& type, |
338 base::string16* result) const { | 341 base::string16* result) const { |
339 DCHECK(CalledOnValidThread()); | 342 DCHECK(CalledOnValidThread()); |
340 DCHECK_EQ(clipboard_type, CLIPBOARD_TYPE_COPY_PASTE); | 343 DCHECK_EQ(clipboard_type, CLIPBOARD_TYPE_COPY_PASTE); |
341 | 344 |
342 NSPasteboard* pb = GetPasteboard(); | 345 NSPasteboard* pb = GetPasteboard(); |
343 if ([[pb types] containsObject:kWebCustomDataPboardType]) { | 346 if ([[pb types] containsObject:kWebCustomDataPboardType]) { |
344 NSData* data = [pb dataForType:kWebCustomDataPboardType]; | 347 NSData* data = [pb dataForType:kWebCustomDataPboardType]; |
345 if ([data length]) | 348 if ([data length]) |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 // Write an extra flavor that signifies WebKit was the last to modify the | 458 // Write an extra flavor that signifies WebKit was the last to modify the |
456 // pasteboard. This flavor has no data. | 459 // pasteboard. This flavor has no data. |
457 void ClipboardMac::WriteWebSmartPaste() { | 460 void ClipboardMac::WriteWebSmartPaste() { |
458 NSPasteboard* pb = GetPasteboard(); | 461 NSPasteboard* pb = GetPasteboard(); |
459 NSString* format = GetWebKitSmartPasteFormatType().ToNSString(); | 462 NSString* format = GetWebKitSmartPasteFormatType().ToNSString(); |
460 [pb addTypes:[NSArray arrayWithObject:format] owner:nil]; | 463 [pb addTypes:[NSArray arrayWithObject:format] owner:nil]; |
461 [pb setData:nil forType:format]; | 464 [pb setData:nil forType:format]; |
462 } | 465 } |
463 | 466 |
464 } // namespace ui | 467 } // namespace ui |
OLD | NEW |