| OLD | NEW |
| 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/basictypes.h" |
| 9 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/mac/mac_util.h" | 12 #include "base/mac/mac_util.h" |
| 12 #include "base/mac/scoped_cftyperef.h" | 13 #include "base/mac/scoped_cftyperef.h" |
| 13 #include "base/memory/scoped_nsobject.h" | 14 #include "base/memory/scoped_nsobject.h" |
| 15 #include "base/stl_util.h" |
| 14 #include "base/sys_string_conversions.h" | 16 #include "base/sys_string_conversions.h" |
| 15 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
| 16 #import "third_party/mozilla/NSPasteboard+Utils.h" | 18 #import "third_party/mozilla/NSPasteboard+Utils.h" |
| 17 #include "third_party/skia/include/core/SkBitmap.h" | 19 #include "third_party/skia/include/core/SkBitmap.h" |
| 18 #include "ui/base/clipboard/custom_data_helper.h" | 20 #include "ui/base/clipboard/custom_data_helper.h" |
| 19 #include "ui/gfx/canvas_skia.h" | 21 #include "ui/gfx/canvas_skia.h" |
| 20 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 22 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
| 21 #include "ui/gfx/size.h" | 23 #include "ui/gfx/size.h" |
| 22 | 24 |
| 23 namespace ui { | 25 namespace ui { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 38 // The pasteboard should not be nil in a UI session, but this handy DCHECK | 40 // The pasteboard should not be nil in a UI session, but this handy DCHECK |
| 39 // can help track down problems if someone tries using clipboard code outside | 41 // can help track down problems if someone tries using clipboard code outside |
| 40 // of a UI session. | 42 // of a UI session. |
| 41 NSPasteboard* pasteboard = [NSPasteboard generalPasteboard]; | 43 NSPasteboard* pasteboard = [NSPasteboard generalPasteboard]; |
| 42 DCHECK(pasteboard); | 44 DCHECK(pasteboard); |
| 43 return pasteboard; | 45 return pasteboard; |
| 44 } | 46 } |
| 45 | 47 |
| 46 } // namespace | 48 } // namespace |
| 47 | 49 |
| 50 Clipboard::FormatType::FormatType() { |
| 51 } |
| 52 |
| 53 Clipboard::FormatType::FormatType(NSString* native_format) |
| 54 : data_([native_format retain]) { |
| 55 } |
| 56 |
| 57 Clipboard::FormatType::~FormatType() { |
| 58 [data_ release]; |
| 59 } |
| 60 |
| 48 Clipboard::Clipboard() { | 61 Clipboard::Clipboard() { |
| 49 } | 62 } |
| 50 | 63 |
| 51 Clipboard::~Clipboard() { | 64 Clipboard::~Clipboard() { |
| 52 } | 65 } |
| 53 | 66 |
| 54 void Clipboard::WriteObjects(const ObjectMap& objects) { | 67 void Clipboard::WriteObjects(const ObjectMap& objects) { |
| 55 NSPasteboard* pb = GetPasteboard(); | 68 NSPasteboard* pb = GetPasteboard(); |
| 56 [pb declareTypes:[NSArray array] owner:nil]; | 69 [pb declareTypes:[NSArray array] owner:nil]; |
| 57 | 70 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 // For now, spit out the image as a TIFF. | 163 // For now, spit out the image as a TIFF. |
| 151 NSPasteboard* pb = GetPasteboard(); | 164 NSPasteboard* pb = GetPasteboard(); |
| 152 [pb addTypes:[NSArray arrayWithObject:NSTIFFPboardType] owner:nil]; | 165 [pb addTypes:[NSArray arrayWithObject:NSTIFFPboardType] owner:nil]; |
| 153 NSData *tiff_data = [image TIFFRepresentation]; | 166 NSData *tiff_data = [image TIFFRepresentation]; |
| 154 LOG_IF(ERROR, tiff_data == NULL) << "Failed to allocate image for clipboard"; | 167 LOG_IF(ERROR, tiff_data == NULL) << "Failed to allocate image for clipboard"; |
| 155 if (tiff_data) { | 168 if (tiff_data) { |
| 156 [pb setData:tiff_data forType:NSTIFFPboardType]; | 169 [pb setData:tiff_data forType:NSTIFFPboardType]; |
| 157 } | 170 } |
| 158 } | 171 } |
| 159 | 172 |
| 160 void Clipboard::WriteData(const char* format_name, size_t format_len, | 173 void Clipboard::WriteData(const FormatType& format, |
| 161 const char* data_data, size_t data_len) { | 174 const char* data_data, |
| 175 size_t data_len) { |
| 162 NSPasteboard* pb = GetPasteboard(); | 176 NSPasteboard* pb = GetPasteboard(); |
| 163 scoped_nsobject<NSString> format( | 177 [pb addTypes:[NSArray arrayWithObject:format.data()] owner:nil]; |
| 164 [[NSString alloc] initWithBytes:format_name | |
| 165 length:format_len | |
| 166 encoding:NSUTF8StringEncoding]); | |
| 167 [pb addTypes:[NSArray arrayWithObject:format] owner:nil]; | |
| 168 [pb setData:[NSData dataWithBytes:data_data length:data_len] | 178 [pb setData:[NSData dataWithBytes:data_data length:data_len] |
| 169 forType:format]; | 179 forType:format.data()]; |
| 170 } | 180 } |
| 171 | 181 |
| 172 // Write an extra flavor that signifies WebKit was the last to modify the | 182 // Write an extra flavor that signifies WebKit was the last to modify the |
| 173 // pasteboard. This flavor has no data. | 183 // pasteboard. This flavor has no data. |
| 174 void Clipboard::WriteWebSmartPaste() { | 184 void Clipboard::WriteWebSmartPaste() { |
| 175 NSPasteboard* pb = GetPasteboard(); | 185 NSPasteboard* pb = GetPasteboard(); |
| 176 NSString* format = base::SysUTF8ToNSString(GetWebKitSmartPasteFormatType()); | 186 NSString* format = GetWebKitSmartPasteFormatType().data(); |
| 177 [pb addTypes:[NSArray arrayWithObject:format] owner:nil]; | 187 [pb addTypes:[NSArray arrayWithObject:format] owner:nil]; |
| 178 [pb setData:nil forType:format]; | 188 [pb setData:nil forType:format]; |
| 179 } | 189 } |
| 180 | 190 |
| 181 uint64 Clipboard::GetSequenceNumber(Buffer buffer) { | 191 uint64 Clipboard::GetSequenceNumber(Buffer buffer) { |
| 182 DCHECK_EQ(buffer, BUFFER_STANDARD); | 192 DCHECK_EQ(buffer, BUFFER_STANDARD); |
| 183 | 193 |
| 184 NSPasteboard* pb = GetPasteboard(); | 194 NSPasteboard* pb = GetPasteboard(); |
| 185 return [pb changeCount]; | 195 return [pb changeCount]; |
| 186 } | 196 } |
| 187 | 197 |
| 188 bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format, | 198 bool Clipboard::IsFormatAvailable(const FormatType& format, |
| 189 Buffer buffer) const { | 199 Buffer buffer) const { |
| 190 DCHECK_EQ(buffer, BUFFER_STANDARD); | 200 DCHECK_EQ(buffer, BUFFER_STANDARD); |
| 191 NSString* format_ns = base::SysUTF8ToNSString(format); | |
| 192 | 201 |
| 193 NSPasteboard* pb = GetPasteboard(); | 202 NSPasteboard* pb = GetPasteboard(); |
| 194 NSArray* types = [pb types]; | 203 NSArray* types = [pb types]; |
| 195 | 204 |
| 196 // Safari only places RTF on the pasteboard, never HTML. We can convert RTF | 205 // Safari only places RTF on the pasteboard, never HTML. We can convert RTF |
| 197 // to HTML, so the presence of either indicates success when looking for HTML. | 206 // to HTML, so the presence of either indicates success when looking for HTML. |
| 198 if ([format_ns isEqualToString:NSHTMLPboardType]) { | 207 if ([format.data() isEqualToString:NSHTMLPboardType]) { |
| 199 return [types containsObject:NSHTMLPboardType] || | 208 return [types containsObject:NSHTMLPboardType] || |
| 200 [types containsObject:NSRTFPboardType]; | 209 [types containsObject:NSRTFPboardType]; |
| 201 } | 210 } |
| 202 return [types containsObject:format_ns]; | 211 return [types containsObject:format.data()]; |
| 203 } | |
| 204 | |
| 205 bool Clipboard::IsFormatAvailableByString(const std::string& format, | |
| 206 Buffer buffer) const { | |
| 207 DCHECK_EQ(buffer, BUFFER_STANDARD); | |
| 208 NSString* format_ns = base::SysUTF8ToNSString(format); | |
| 209 | |
| 210 NSPasteboard* pb = GetPasteboard(); | |
| 211 NSArray* types = [pb types]; | |
| 212 | |
| 213 return [types containsObject:format_ns]; | |
| 214 } | 212 } |
| 215 | 213 |
| 216 void Clipboard::ReadAvailableTypes(Clipboard::Buffer buffer, | 214 void Clipboard::ReadAvailableTypes(Clipboard::Buffer buffer, |
| 217 std::vector<string16>* types, | 215 std::vector<string16>* types, |
| 218 bool* contains_filenames) const { | 216 bool* contains_filenames) const { |
| 219 if (!types || !contains_filenames) { | |
| 220 NOTREACHED(); | |
| 221 return; | |
| 222 } | |
| 223 | |
| 224 types->clear(); | 217 types->clear(); |
| 225 if (IsFormatAvailable(Clipboard::GetPlainTextFormatType(), buffer)) | 218 if (IsFormatAvailable(Clipboard::GetPlainTextFormatType(), buffer)) |
| 226 types->push_back(UTF8ToUTF16(kMimeTypeText)); | 219 types->push_back(UTF8ToUTF16(kMimeTypeText)); |
| 227 if (IsFormatAvailable(Clipboard::GetHtmlFormatType(), buffer)) | 220 if (IsFormatAvailable(Clipboard::GetHtmlFormatType(), buffer)) |
| 228 types->push_back(UTF8ToUTF16(kMimeTypeHTML)); | 221 types->push_back(UTF8ToUTF16(kMimeTypeHTML)); |
| 229 if ([NSImage canInitWithPasteboard:GetPasteboard()]) | 222 if ([NSImage canInitWithPasteboard:GetPasteboard()]) |
| 230 types->push_back(UTF8ToUTF16(kMimeTypePNG)); | 223 types->push_back(UTF8ToUTF16(kMimeTypePNG)); |
| 231 *contains_filenames = false; | 224 *contains_filenames = false; |
| 232 | 225 |
| 233 NSPasteboard* pb = GetPasteboard(); | 226 NSPasteboard* pb = GetPasteboard(); |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 | 368 |
| 376 NSPasteboard* pb = GetPasteboard(); | 369 NSPasteboard* pb = GetPasteboard(); |
| 377 NSArray* fileList = [pb propertyListForType:NSFilenamesPboardType]; | 370 NSArray* fileList = [pb propertyListForType:NSFilenamesPboardType]; |
| 378 | 371 |
| 379 for (unsigned int i = 0; i < [fileList count]; ++i) { | 372 for (unsigned int i = 0; i < [fileList count]; ++i) { |
| 380 std::string file = [[fileList objectAtIndex:i] UTF8String]; | 373 std::string file = [[fileList objectAtIndex:i] UTF8String]; |
| 381 files->push_back(FilePath(file)); | 374 files->push_back(FilePath(file)); |
| 382 } | 375 } |
| 383 } | 376 } |
| 384 | 377 |
| 385 void Clipboard::ReadData(const std::string& format, std::string* result) const { | 378 void Clipboard::ReadData(const FormatType& format, std::string* result) const { |
| 386 NSPasteboard* pb = GetPasteboard(); | 379 NSPasteboard* pb = GetPasteboard(); |
| 387 NSData* data = [pb dataForType:base::SysUTF8ToNSString(format)]; | 380 NSData* data = [pb dataForType:format.data()]; |
| 388 if ([data length]) | 381 if ([data length]) |
| 389 result->assign(static_cast<const char*>([data bytes]), [data length]); | 382 result->assign(static_cast<const char*>([data bytes]), [data length]); |
| 390 } | 383 } |
| 391 | 384 |
| 392 // static | 385 // static |
| 393 Clipboard::FormatType Clipboard::GetUrlFormatType() { | 386 Clipboard::FormatType Clipboard::RegisterFormatType( |
| 394 return base::SysNSStringToUTF8(NSURLPboardType); | 387 const std::string& format_string) { |
| 388 return StringToFormatType(format_string); |
| 389 } |
| 390 |
| 391 std::string Clipboard::FormatTypeToString(const FormatType& type) { |
| 392 return base::SysNSStringToUTF8(type.data()); |
| 395 } | 393 } |
| 396 | 394 |
| 397 // static | 395 // static |
| 398 Clipboard::FormatType Clipboard::GetUrlWFormatType() { | 396 Clipboard::FormatType Clipboard::StringToFormatType( |
| 399 return base::SysNSStringToUTF8(NSURLPboardType); | 397 const std::string& format_string) { |
| 398 return FormatType(base::SysUTF8ToNSString(format_string)); |
| 400 } | 399 } |
| 401 | 400 |
| 402 // static | 401 // static |
| 403 Clipboard::FormatType Clipboard::GetPlainTextFormatType() { | 402 const Clipboard::FormatType& Clipboard::GetUrlFormatType() { |
| 404 return base::SysNSStringToUTF8(NSStringPboardType); | 403 CR_DEFINE_STATIC_LOCAL(FormatType, type, (NSURLPboardType)); |
| 404 return type; |
| 405 } | 405 } |
| 406 | 406 |
| 407 // static | 407 // static |
| 408 Clipboard::FormatType Clipboard::GetPlainTextWFormatType() { | 408 const Clipboard::FormatType& Clipboard::GetUrlWFormatType() { |
| 409 return base::SysNSStringToUTF8(NSStringPboardType); | 409 return GetUrlFormatType(); |
| 410 } | 410 } |
| 411 | 411 |
| 412 // static | 412 // static |
| 413 Clipboard::FormatType Clipboard::GetFilenameFormatType() { | 413 const Clipboard::FormatType& Clipboard::GetPlainTextFormatType() { |
| 414 return base::SysNSStringToUTF8(NSFilenamesPboardType); | 414 CR_DEFINE_STATIC_LOCAL(FormatType, type, (NSStringPboardType)); |
| 415 return type; |
| 415 } | 416 } |
| 416 | 417 |
| 417 // static | 418 // static |
| 418 Clipboard::FormatType Clipboard::GetFilenameWFormatType() { | 419 const Clipboard::FormatType& Clipboard::GetPlainTextWFormatType() { |
| 419 return base::SysNSStringToUTF8(NSFilenamesPboardType); | 420 return GetPlainTextFormatType(); |
| 420 } | 421 } |
| 421 | 422 |
| 422 // static | 423 // static |
| 423 Clipboard::FormatType Clipboard::GetHtmlFormatType() { | 424 const Clipboard::FormatType& Clipboard::GetFilenameFormatType() { |
| 424 return base::SysNSStringToUTF8(NSHTMLPboardType); | 425 CR_DEFINE_STATIC_LOCAL(FormatType, type, (NSFilenamesPboardType)); |
| 426 return type; |
| 425 } | 427 } |
| 426 | 428 |
| 427 // static | 429 // static |
| 428 Clipboard::FormatType Clipboard::GetBitmapFormatType() { | 430 const Clipboard::FormatType& Clipboard::GetFilenameWFormatType() { |
| 429 return base::SysNSStringToUTF8(NSTIFFPboardType); | 431 return GetFilenameFormatType(); |
| 430 } | 432 } |
| 431 | 433 |
| 432 // static | 434 // static |
| 433 Clipboard::FormatType Clipboard::GetWebKitSmartPasteFormatType() { | 435 const Clipboard::FormatType& Clipboard::GetHtmlFormatType() { |
| 434 return base::SysNSStringToUTF8(kWebSmartPastePboardType); | 436 CR_DEFINE_STATIC_LOCAL(FormatType, type, (NSHTMLPboardType)); |
| 437 return type; |
| 435 } | 438 } |
| 436 | 439 |
| 437 // static | 440 // static |
| 438 Clipboard::FormatType Clipboard::GetWebCustomDataFormatType() { | 441 const Clipboard::FormatType& Clipboard::GetBitmapFormatType() { |
| 439 return base::SysNSStringToUTF8(kWebCustomDataType); | 442 CR_DEFINE_STATIC_LOCAL(FormatType, type, (NSTIFFPboardType)); |
| 443 return type; |
| 444 } |
| 445 |
| 446 // static |
| 447 const Clipboard::FormatType& Clipboard::GetWebKitSmartPasteFormatType() { |
| 448 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kWebSmartPastePboardType)); |
| 449 return type; |
| 450 } |
| 451 |
| 452 // static |
| 453 const Clipboard::FormatType& Clipboard::GetWebCustomDataFormatType() { |
| 454 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kWebCustomDataType)); |
| 455 return type; |
| 440 } | 456 } |
| 441 | 457 |
| 442 } // namespace ui | 458 } // namespace ui |
| OLD | NEW |