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> |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 | 32 |
| 33 // 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 |
| 34 // actual data associated with this type. | 34 // actual data associated with this type. |
| 35 NSString* const kWebSmartPastePboardType = @"NeXT smart paste pasteboard type"; | 35 NSString* const kWebSmartPastePboardType = @"NeXT smart paste pasteboard type"; |
| 36 | 36 |
| 37 // Pepper custom data format type. | 37 // Pepper custom data format type. |
| 38 NSString* const kPepperCustomDataPboardType = | 38 NSString* const kPepperCustomDataPboardType = |
| 39 @"org.chromium.pepper-custom-data"; | 39 @"org.chromium.pepper-custom-data"; |
| 40 | 40 |
| 41 NSPasteboard* GetPasteboard() { | 41 NSPasteboard* GetPasteboard() { |
| 42 // The pasteboard should not be nil in a UI session, but this handy DCHECK | 42 // The pasteboard can always be nil, since there is a finite amount of storage |
| 43 // can help track down problems if someone tries using clipboard code outside | 43 // that must be shared between all pasteboards. |
| 44 // of a UI session. | |
| 45 NSPasteboard* pasteboard = [NSPasteboard generalPasteboard]; | 44 NSPasteboard* pasteboard = [NSPasteboard generalPasteboard]; |
| 46 DCHECK(pasteboard); | |
| 47 return pasteboard; | 45 return pasteboard; |
| 48 } | 46 } |
| 49 | 47 |
| 50 } // namespace | 48 } // namespace |
| 51 | 49 |
| 52 // Clipboard::FormatType implementation. | 50 // Clipboard::FormatType implementation. |
| 53 Clipboard::FormatType::FormatType() : data_(nil) { | 51 Clipboard::FormatType::FormatType() : data_(nil) { |
| 54 } | 52 } |
| 55 | 53 |
| 56 Clipboard::FormatType::FormatType(NSString* native_format) | 54 Clipboard::FormatType::FormatType(NSString* native_format) |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 220 std::vector<base::string16>* types, | 218 std::vector<base::string16>* types, |
| 221 bool* contains_filenames) const { | 219 bool* contains_filenames) const { |
| 222 DCHECK(CalledOnValidThread()); | 220 DCHECK(CalledOnValidThread()); |
| 223 types->clear(); | 221 types->clear(); |
| 224 if (IsFormatAvailable(Clipboard::GetPlainTextFormatType(), type)) | 222 if (IsFormatAvailable(Clipboard::GetPlainTextFormatType(), type)) |
| 225 types->push_back(base::UTF8ToUTF16(kMimeTypeText)); | 223 types->push_back(base::UTF8ToUTF16(kMimeTypeText)); |
| 226 if (IsFormatAvailable(Clipboard::GetHtmlFormatType(), type)) | 224 if (IsFormatAvailable(Clipboard::GetHtmlFormatType(), type)) |
| 227 types->push_back(base::UTF8ToUTF16(kMimeTypeHTML)); | 225 types->push_back(base::UTF8ToUTF16(kMimeTypeHTML)); |
| 228 if (IsFormatAvailable(Clipboard::GetRtfFormatType(), type)) | 226 if (IsFormatAvailable(Clipboard::GetRtfFormatType(), type)) |
| 229 types->push_back(base::UTF8ToUTF16(kMimeTypeRTF)); | 227 types->push_back(base::UTF8ToUTF16(kMimeTypeRTF)); |
| 230 if ([NSImage canInitWithPasteboard:GetPasteboard()]) | 228 |
| 229 NSPasteboard* pb = GetPasteboard(); | |
| 230 if (pb && [NSImage canInitWithPasteboard:pb]) | |
| 231 types->push_back(base::UTF8ToUTF16(kMimeTypePNG)); | 231 types->push_back(base::UTF8ToUTF16(kMimeTypePNG)); |
| 232 *contains_filenames = false; | 232 *contains_filenames = false; |
| 233 | 233 |
| 234 NSPasteboard* pb = GetPasteboard(); | |
| 235 if ([[pb types] containsObject:kWebCustomDataPboardType]) { | 234 if ([[pb types] containsObject:kWebCustomDataPboardType]) { |
| 236 NSData* data = [pb dataForType:kWebCustomDataPboardType]; | 235 NSData* data = [pb dataForType:kWebCustomDataPboardType]; |
| 237 if ([data length]) | 236 if ([data length]) |
| 238 ReadCustomDataTypes([data bytes], [data length], types); | 237 ReadCustomDataTypes([data bytes], [data length], types); |
| 239 } | 238 } |
| 240 } | 239 } |
| 241 | 240 |
| 242 void ClipboardMac::ReadText(ClipboardType type, base::string16* result) const { | 241 void ClipboardMac::ReadText(ClipboardType type, base::string16* result) const { |
| 243 DCHECK(CalledOnValidThread()); | 242 DCHECK(CalledOnValidThread()); |
| 244 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); | 243 DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 313 // -[NSImage initWithPasteboard:] gets confused with copies of a single | 312 // -[NSImage initWithPasteboard:] gets confused with copies of a single |
| 314 // file from the Finder, so extract the path ourselves. | 313 // file from the Finder, so extract the path ourselves. |
| 315 // http://crbug.com/553686 | 314 // http://crbug.com/553686 |
| 316 NSArray* paths = [pb propertyListForType:NSFilenamesPboardType]; | 315 NSArray* paths = [pb propertyListForType:NSFilenamesPboardType]; |
| 317 if ([paths count]) { | 316 if ([paths count]) { |
| 318 // If N number of files are selected from finder, choose the last one. | 317 // If N number of files are selected from finder, choose the last one. |
| 319 image.reset([[NSImage alloc] | 318 image.reset([[NSImage alloc] |
| 320 initWithContentsOfURL:[NSURL fileURLWithPath:[paths lastObject]]]); | 319 initWithContentsOfURL:[NSURL fileURLWithPath:[paths lastObject]]]); |
| 321 } | 320 } |
| 322 } else { | 321 } else { |
| 323 image.reset([[NSImage alloc] initWithPasteboard:pb]); | 322 if (pb) |
| 323 image.reset([[NSImage alloc] initWithPasteboard:pb]); | |
| 324 } | 324 } |
| 325 } @catch (id exception) { | 325 } @catch (id exception) { |
| 326 } | 326 } |
| 327 | 327 |
| 328 SkBitmap bitmap; | 328 SkBitmap bitmap; |
| 329 if (image.get()) { | 329 if (image.get()) { |
| 330 bitmap = skia::NSImageToSkBitmapWithColorSpace( | 330 bitmap = skia::NSImageToSkBitmapWithColorSpace( |
| 331 image.get(), /*is_opaque=*/ false, base::mac::GetSystemColorSpace()); | 331 image.get(), /*is_opaque=*/ false, base::mac::GetSystemColorSpace()); |
| 332 } | 332 } |
| 333 return bitmap; | 333 return bitmap; |
| 334 } | 334 } |
| 335 | 335 |
| 336 void ClipboardMac::ReadCustomData(ClipboardType clipboard_type, | 336 void ClipboardMac::ReadCustomData(ClipboardType clipboard_type, |
| 337 const base::string16& type, | 337 const base::string16& type, |
| 338 base::string16* result) const { | 338 base::string16* result) const { |
| 339 DCHECK(CalledOnValidThread()); | 339 DCHECK(CalledOnValidThread()); |
| 340 DCHECK_EQ(clipboard_type, CLIPBOARD_TYPE_COPY_PASTE); | 340 DCHECK_EQ(clipboard_type, CLIPBOARD_TYPE_COPY_PASTE); |
| 341 | 341 |
| 342 NSPasteboard* pb = GetPasteboard(); | 342 NSPasteboard* pb = GetPasteboard(); |
| 343 if ([[pb types] containsObject:kWebCustomDataPboardType]) { | 343 if ([[pb types] containsObject:kWebCustomDataPboardType]) { |
| 344 NSData* data = [pb dataForType:kWebCustomDataPboardType]; | 344 NSData* data = [pb dataForType:kWebCustomDataPboardType]; |
| 345 if ([data length]) | 345 if ([data length]) |
| 346 ReadCustomDataForType([data bytes], [data length], type, result); | 346 ReadCustomDataForType([data bytes], [data length], type, result); |
| 347 } | 347 } |
| 348 } | 348 } |
| 349 | 349 |
| 350 void ClipboardMac::ReadBookmark(base::string16* title, std::string* url) const { | 350 void ClipboardMac::ReadBookmark(base::string16* title, std::string* url) const { |
| 351 DCHECK(CalledOnValidThread()); | 351 DCHECK(CalledOnValidThread()); |
| 352 NSPasteboard* pb = GetPasteboard(); | 352 NSPasteboard* pb = GetPasteboard(); |
| 353 if (!pb) | |
|
dcheng
2016/08/10 14:28:34
How come this one needs a null check too?
erikchen
2016/08/10 16:35:58
Unnecessary, removed.
| |
| 354 return; | |
| 353 | 355 |
| 354 if (title) { | 356 if (title) { |
| 355 NSString* contents = ClipboardUtil::GetTitleFromPasteboardURL(pb); | 357 NSString* contents = ClipboardUtil::GetTitleFromPasteboardURL(pb); |
| 356 *title = base::SysNSStringToUTF16(contents); | 358 *title = base::SysNSStringToUTF16(contents); |
| 357 } | 359 } |
| 358 | 360 |
| 359 if (url) { | 361 if (url) { |
| 360 NSString* url_string = ClipboardUtil::GetURLFromPasteboardURL(pb); | 362 NSString* url_string = ClipboardUtil::GetURLFromPasteboardURL(pb); |
| 361 if (!url_string) | 363 if (!url_string) |
| 362 url->clear(); | 364 url->clear(); |
| (...skipping 92 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 | 457 // Write an extra flavor that signifies WebKit was the last to modify the |
| 456 // pasteboard. This flavor has no data. | 458 // pasteboard. This flavor has no data. |
| 457 void ClipboardMac::WriteWebSmartPaste() { | 459 void ClipboardMac::WriteWebSmartPaste() { |
| 458 NSPasteboard* pb = GetPasteboard(); | 460 NSPasteboard* pb = GetPasteboard(); |
| 459 NSString* format = GetWebKitSmartPasteFormatType().ToNSString(); | 461 NSString* format = GetWebKitSmartPasteFormatType().ToNSString(); |
| 460 [pb addTypes:[NSArray arrayWithObject:format] owner:nil]; | 462 [pb addTypes:[NSArray arrayWithObject:format] owner:nil]; |
| 461 [pb setData:nil forType:format]; | 463 [pb setData:nil forType:format]; |
| 462 } | 464 } |
| 463 | 465 |
| 464 } // namespace ui | 466 } // namespace ui |
| OLD | NEW |