Chromium Code Reviews| 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> |
| 11 | 11 |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/mac/mac_util.h" | 14 #include "base/mac/mac_util.h" |
| 15 #include "base/mac/scoped_cftyperef.h" | 15 #include "base/mac/scoped_cftyperef.h" |
| 16 #include "base/mac/scoped_nsobject.h" | 16 #include "base/mac/scoped_nsobject.h" |
| 17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 18 #include "base/strings/sys_string_conversions.h" | 18 #include "base/strings/sys_string_conversions.h" |
| 19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 20 #include "skia/ext/skia_utils_mac.h" | 20 #include "skia/ext/skia_utils_mac.h" |
| 21 #import "third_party/mozilla/NSPasteboard+Utils.h" | 21 #import "third_party/mozilla/NSPasteboard+Utils.h" |
| 22 #include "third_party/skia/include/core/SkBitmap.h" | 22 #include "third_party/skia/include/core/SkBitmap.h" |
| 23 #include "ui/base/clipboard/clipboard_util_mac.h" | |
| 23 #include "ui/base/clipboard/custom_data_helper.h" | 24 #include "ui/base/clipboard/custom_data_helper.h" |
| 24 #include "ui/gfx/canvas.h" | 25 #include "ui/gfx/canvas.h" |
| 25 #include "ui/gfx/geometry/size.h" | 26 #include "ui/gfx/geometry/size.h" |
| 26 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 27 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
| 27 | 28 |
| 28 namespace ui { | 29 namespace ui { |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 // Would be nice if this were in UTCoreTypes.h, but it isn't | |
| 33 NSString* const kUTTypeURLName = @"public.url-name"; | |
| 34 | |
| 35 // Tells us if WebKit was the last to write to the pasteboard. There's no | 33 // Tells us if WebKit was the last to write to the pasteboard. There's no |
| 36 // actual data associated with this type. | 34 // actual data associated with this type. |
| 37 NSString* const kWebSmartPastePboardType = @"NeXT smart paste pasteboard type"; | 35 NSString* const kWebSmartPastePboardType = @"NeXT smart paste pasteboard type"; |
| 38 | 36 |
| 39 // Pepper custom data format type. | 37 // Pepper custom data format type. |
| 40 NSString* const kPepperCustomDataPboardType = | 38 NSString* const kPepperCustomDataPboardType = |
| 41 @"org.chromium.pepper-custom-data"; | 39 @"org.chromium.pepper-custom-data"; |
| 42 | 40 |
| 43 NSPasteboard* GetPasteboard() { | 41 NSPasteboard* GetPasteboard() { |
| 44 // The pasteboard should not be nil in a UI session, but this handy DCHECK | 42 // The pasteboard should not be nil in a UI session, but this handy DCHECK |
| (...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 347 if ([data length]) | 345 if ([data length]) |
| 348 ReadCustomDataForType([data bytes], [data length], type, result); | 346 ReadCustomDataForType([data bytes], [data length], type, result); |
| 349 } | 347 } |
| 350 } | 348 } |
| 351 | 349 |
| 352 void ClipboardMac::ReadBookmark(base::string16* title, std::string* url) const { | 350 void ClipboardMac::ReadBookmark(base::string16* title, std::string* url) const { |
| 353 DCHECK(CalledOnValidThread()); | 351 DCHECK(CalledOnValidThread()); |
| 354 NSPasteboard* pb = GetPasteboard(); | 352 NSPasteboard* pb = GetPasteboard(); |
| 355 | 353 |
| 356 if (title) { | 354 if (title) { |
| 357 NSString* contents = [pb stringForType:kUTTypeURLName]; | 355 NSString* contents = ClipboardUtil::GetTitleFromPasteboardURL(pb); |
| 358 *title = base::SysNSStringToUTF16(contents); | 356 *title = base::SysNSStringToUTF16(contents); |
| 359 } | 357 } |
| 360 | 358 |
| 361 if (url) { | 359 if (url) { |
| 362 NSString* url_string = [[NSURL URLFromPasteboard:pb] absoluteString]; | 360 NSString* url_string = ClipboardUtil::GetURLFromPasteboardURL(pb); |
| 363 if (!url_string) | 361 if (!url_string) |
| 364 url->clear(); | 362 url->clear(); |
| 365 else | 363 else |
| 366 url->assign([url_string UTF8String]); | 364 url->assign([url_string UTF8String]); |
| 367 } | 365 } |
| 368 } | 366 } |
| 369 | 367 |
| 370 void ClipboardMac::ReadData(const FormatType& format, | 368 void ClipboardMac::ReadData(const FormatType& format, |
| 371 std::string* result) const { | 369 std::string* result) const { |
| 372 DCHECK(CalledOnValidThread()); | 370 DCHECK(CalledOnValidThread()); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 418 | 416 |
| 419 void ClipboardMac::WriteBookmark(const char* title_data, | 417 void ClipboardMac::WriteBookmark(const char* title_data, |
| 420 size_t title_len, | 418 size_t title_len, |
| 421 const char* url_data, | 419 const char* url_data, |
| 422 size_t url_len) { | 420 size_t url_len) { |
| 423 std::string title_str(title_data, title_len); | 421 std::string title_str(title_data, title_len); |
| 424 NSString* title = base::SysUTF8ToNSString(title_str); | 422 NSString* title = base::SysUTF8ToNSString(title_str); |
| 425 std::string url_str(url_data, url_len); | 423 std::string url_str(url_data, url_len); |
| 426 NSString* url = base::SysUTF8ToNSString(url_str); | 424 NSString* url = base::SysUTF8ToNSString(url_str); |
| 427 | 425 |
| 428 // TODO(playmobil): In the Windows version of this function, an HTML | 426 base::scoped_nsobject<NSPasteboardItem> item( |
| 429 // representation of the bookmark is also added to the clipboard, to support | 427 ClipboardUtil::PasteboardItemFromUrl(url, title)); |
| 430 // drag and drop of web shortcuts. I don't think we need to do this on the | 428 NSPasteboard* pb = GetPasteboard(); |
| 431 // Mac, but we should double check later on. | |
| 432 NSURL* nsurl = [NSURL URLWithString:url]; | |
| 433 | 429 |
| 434 NSPasteboard* pb = GetPasteboard(); | 430 NSSet* oldTypes = [NSSet setWithArray:[pb types]]; |
| 435 // passing UTIs into the pasteboard methods is valid >= 10.5 | 431 NSMutableSet* newTypes = [NSMutableSet setWithArray:[item types]]; |
| 436 [pb addTypes:[NSArray arrayWithObjects:NSURLPboardType, kUTTypeURLName, nil] | 432 [newTypes minusSet:oldTypes]; |
| 437 owner:nil]; | 433 NSArray* newTypesArray = [newTypes allObjects]; |
| 438 [nsurl writeToPasteboard:pb]; | 434 |
| 439 [pb setString:title forType:kUTTypeURLName]; | 435 [pb addTypes:newTypesArray owner:nil]; |
| 436 for (NSString* type in newTypesArray) { | |
|
Avi (use Gerrit)
2016/04/01 22:31:25
[pb addTypes:[newTypes allObjects] owner:nil];
for
erikchen
2016/04/01 22:41:08
Done.
| |
| 437 // Technically, the object associated with |type| might be an NSString or a | |
| 438 // property list. It doesn't matter though, since the type gets pulled from | |
| 439 // and shoved into an NSDictionary. | |
| 440 [pb setData:[item dataForType:type] forType:type]; | |
| 441 } | |
| 440 } | 442 } |
| 441 | 443 |
| 442 void ClipboardMac::WriteBitmap(const SkBitmap& bitmap) { | 444 void ClipboardMac::WriteBitmap(const SkBitmap& bitmap) { |
| 443 NSImage* image = skia::SkBitmapToNSImageWithColorSpace( | 445 NSImage* image = skia::SkBitmapToNSImageWithColorSpace( |
| 444 bitmap, base::mac::GetSystemColorSpace()); | 446 bitmap, base::mac::GetSystemColorSpace()); |
| 445 // An API to ask the NSImage to write itself to the clipboard comes in 10.6 :( | 447 // An API to ask the NSImage to write itself to the clipboard comes in 10.6 :( |
| 446 // For now, spit out the image as a TIFF. | 448 // For now, spit out the image as a TIFF. |
| 447 NSPasteboard* pb = GetPasteboard(); | 449 NSPasteboard* pb = GetPasteboard(); |
| 448 [pb addTypes:[NSArray arrayWithObject:NSTIFFPboardType] owner:nil]; | 450 [pb addTypes:[NSArray arrayWithObject:NSTIFFPboardType] owner:nil]; |
| 449 NSData* tiff_data = [image TIFFRepresentation]; | 451 NSData* tiff_data = [image TIFFRepresentation]; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 465 // Write an extra flavor that signifies WebKit was the last to modify the | 467 // Write an extra flavor that signifies WebKit was the last to modify the |
| 466 // pasteboard. This flavor has no data. | 468 // pasteboard. This flavor has no data. |
| 467 void ClipboardMac::WriteWebSmartPaste() { | 469 void ClipboardMac::WriteWebSmartPaste() { |
| 468 NSPasteboard* pb = GetPasteboard(); | 470 NSPasteboard* pb = GetPasteboard(); |
| 469 NSString* format = GetWebKitSmartPasteFormatType().ToNSString(); | 471 NSString* format = GetWebKitSmartPasteFormatType().ToNSString(); |
| 470 [pb addTypes:[NSArray arrayWithObject:format] owner:nil]; | 472 [pb addTypes:[NSArray arrayWithObject:format] owner:nil]; |
| 471 [pb setData:nil forType:format]; | 473 [pb setData:nil forType:format]; |
| 472 } | 474 } |
| 473 | 475 |
| 474 } // namespace ui | 476 } // namespace ui |
| OLD | NEW |