| 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/basictypes.h" |
| 10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 return base::SysNSStringToUTF8(data_); | 72 return base::SysNSStringToUTF8(data_); |
| 73 } | 73 } |
| 74 | 74 |
| 75 // static | 75 // static |
| 76 Clipboard::FormatType Clipboard::FormatType::Deserialize( | 76 Clipboard::FormatType Clipboard::FormatType::Deserialize( |
| 77 const std::string& serialization) { | 77 const std::string& serialization) { |
| 78 return FormatType(base::SysUTF8ToNSString(serialization)); | 78 return FormatType(base::SysUTF8ToNSString(serialization)); |
| 79 } | 79 } |
| 80 | 80 |
| 81 Clipboard::Clipboard() { | 81 Clipboard::Clipboard() { |
| 82 DCHECK(CalledOnValidThread()); |
| 82 } | 83 } |
| 83 | 84 |
| 84 Clipboard::~Clipboard() { | 85 Clipboard::~Clipboard() { |
| 86 DCHECK(CalledOnValidThread()); |
| 85 } | 87 } |
| 86 | 88 |
| 87 void Clipboard::WriteObjects(const ObjectMap& objects) { | 89 void Clipboard::WriteObjects(const ObjectMap& objects) { |
| 90 DCHECK(CalledOnValidThread()); |
| 88 NSPasteboard* pb = GetPasteboard(); | 91 NSPasteboard* pb = GetPasteboard(); |
| 89 [pb declareTypes:[NSArray array] owner:nil]; | 92 [pb declareTypes:[NSArray array] owner:nil]; |
| 90 | 93 |
| 91 for (ObjectMap::const_iterator iter = objects.begin(); | 94 for (ObjectMap::const_iterator iter = objects.begin(); |
| 92 iter != objects.end(); ++iter) { | 95 iter != objects.end(); ++iter) { |
| 93 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); | 96 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); |
| 94 } | 97 } |
| 95 } | 98 } |
| 96 | 99 |
| 97 void Clipboard::WriteText(const char* text_data, size_t text_len) { | 100 void Clipboard::WriteText(const char* text_data, size_t text_len) { |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 // Write an extra flavor that signifies WebKit was the last to modify the | 205 // Write an extra flavor that signifies WebKit was the last to modify the |
| 203 // pasteboard. This flavor has no data. | 206 // pasteboard. This flavor has no data. |
| 204 void Clipboard::WriteWebSmartPaste() { | 207 void Clipboard::WriteWebSmartPaste() { |
| 205 NSPasteboard* pb = GetPasteboard(); | 208 NSPasteboard* pb = GetPasteboard(); |
| 206 NSString* format = GetWebKitSmartPasteFormatType().ToNSString(); | 209 NSString* format = GetWebKitSmartPasteFormatType().ToNSString(); |
| 207 [pb addTypes:[NSArray arrayWithObject:format] owner:nil]; | 210 [pb addTypes:[NSArray arrayWithObject:format] owner:nil]; |
| 208 [pb setData:nil forType:format]; | 211 [pb setData:nil forType:format]; |
| 209 } | 212 } |
| 210 | 213 |
| 211 uint64 Clipboard::GetSequenceNumber(Buffer buffer) { | 214 uint64 Clipboard::GetSequenceNumber(Buffer buffer) { |
| 215 DCHECK(CalledOnValidThread()); |
| 212 DCHECK_EQ(buffer, BUFFER_STANDARD); | 216 DCHECK_EQ(buffer, BUFFER_STANDARD); |
| 213 | 217 |
| 214 NSPasteboard* pb = GetPasteboard(); | 218 NSPasteboard* pb = GetPasteboard(); |
| 215 return [pb changeCount]; | 219 return [pb changeCount]; |
| 216 } | 220 } |
| 217 | 221 |
| 218 bool Clipboard::IsFormatAvailable(const FormatType& format, | 222 bool Clipboard::IsFormatAvailable(const FormatType& format, |
| 219 Buffer buffer) const { | 223 Buffer buffer) const { |
| 224 DCHECK(CalledOnValidThread()); |
| 220 DCHECK_EQ(buffer, BUFFER_STANDARD); | 225 DCHECK_EQ(buffer, BUFFER_STANDARD); |
| 221 | 226 |
| 222 NSPasteboard* pb = GetPasteboard(); | 227 NSPasteboard* pb = GetPasteboard(); |
| 223 NSArray* types = [pb types]; | 228 NSArray* types = [pb types]; |
| 224 | 229 |
| 225 // Safari only places RTF on the pasteboard, never HTML. We can convert RTF | 230 // Safari only places RTF on the pasteboard, never HTML. We can convert RTF |
| 226 // to HTML, so the presence of either indicates success when looking for HTML. | 231 // to HTML, so the presence of either indicates success when looking for HTML. |
| 227 if ([format.ToNSString() isEqualToString:NSHTMLPboardType]) { | 232 if ([format.ToNSString() isEqualToString:NSHTMLPboardType]) { |
| 228 return [types containsObject:NSHTMLPboardType] || | 233 return [types containsObject:NSHTMLPboardType] || |
| 229 [types containsObject:NSRTFPboardType]; | 234 [types containsObject:NSRTFPboardType]; |
| 230 } | 235 } |
| 231 return [types containsObject:format.ToNSString()]; | 236 return [types containsObject:format.ToNSString()]; |
| 232 } | 237 } |
| 233 | 238 |
| 234 void Clipboard::ReadAvailableTypes(Clipboard::Buffer buffer, | 239 void Clipboard::ReadAvailableTypes(Clipboard::Buffer buffer, |
| 235 std::vector<string16>* types, | 240 std::vector<string16>* types, |
| 236 bool* contains_filenames) const { | 241 bool* contains_filenames) const { |
| 242 DCHECK(CalledOnValidThread()); |
| 237 types->clear(); | 243 types->clear(); |
| 238 if (IsFormatAvailable(Clipboard::GetPlainTextFormatType(), buffer)) | 244 if (IsFormatAvailable(Clipboard::GetPlainTextFormatType(), buffer)) |
| 239 types->push_back(UTF8ToUTF16(kMimeTypeText)); | 245 types->push_back(UTF8ToUTF16(kMimeTypeText)); |
| 240 if (IsFormatAvailable(Clipboard::GetHtmlFormatType(), buffer)) | 246 if (IsFormatAvailable(Clipboard::GetHtmlFormatType(), buffer)) |
| 241 types->push_back(UTF8ToUTF16(kMimeTypeHTML)); | 247 types->push_back(UTF8ToUTF16(kMimeTypeHTML)); |
| 242 if ([NSImage canInitWithPasteboard:GetPasteboard()]) | 248 if ([NSImage canInitWithPasteboard:GetPasteboard()]) |
| 243 types->push_back(UTF8ToUTF16(kMimeTypePNG)); | 249 types->push_back(UTF8ToUTF16(kMimeTypePNG)); |
| 244 *contains_filenames = false; | 250 *contains_filenames = false; |
| 245 | 251 |
| 246 NSPasteboard* pb = GetPasteboard(); | 252 NSPasteboard* pb = GetPasteboard(); |
| 247 if ([[pb types] containsObject:kWebCustomDataPboardType]) { | 253 if ([[pb types] containsObject:kWebCustomDataPboardType]) { |
| 248 NSData* data = [pb dataForType:kWebCustomDataPboardType]; | 254 NSData* data = [pb dataForType:kWebCustomDataPboardType]; |
| 249 if ([data length]) | 255 if ([data length]) |
| 250 ReadCustomDataTypes([data bytes], [data length], types); | 256 ReadCustomDataTypes([data bytes], [data length], types); |
| 251 } | 257 } |
| 252 } | 258 } |
| 253 | 259 |
| 254 void Clipboard::ReadText(Clipboard::Buffer buffer, string16* result) const { | 260 void Clipboard::ReadText(Clipboard::Buffer buffer, string16* result) const { |
| 261 DCHECK(CalledOnValidThread()); |
| 255 DCHECK_EQ(buffer, BUFFER_STANDARD); | 262 DCHECK_EQ(buffer, BUFFER_STANDARD); |
| 256 NSPasteboard* pb = GetPasteboard(); | 263 NSPasteboard* pb = GetPasteboard(); |
| 257 NSString* contents = [pb stringForType:NSStringPboardType]; | 264 NSString* contents = [pb stringForType:NSStringPboardType]; |
| 258 | 265 |
| 259 UTF8ToUTF16([contents UTF8String], | 266 UTF8ToUTF16([contents UTF8String], |
| 260 [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding], | 267 [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding], |
| 261 result); | 268 result); |
| 262 } | 269 } |
| 263 | 270 |
| 264 void Clipboard::ReadAsciiText(Clipboard::Buffer buffer, | 271 void Clipboard::ReadAsciiText(Clipboard::Buffer buffer, |
| 265 std::string* result) const { | 272 std::string* result) const { |
| 273 DCHECK(CalledOnValidThread()); |
| 266 DCHECK_EQ(buffer, BUFFER_STANDARD); | 274 DCHECK_EQ(buffer, BUFFER_STANDARD); |
| 267 NSPasteboard* pb = GetPasteboard(); | 275 NSPasteboard* pb = GetPasteboard(); |
| 268 NSString* contents = [pb stringForType:NSStringPboardType]; | 276 NSString* contents = [pb stringForType:NSStringPboardType]; |
| 269 | 277 |
| 270 if (!contents) | 278 if (!contents) |
| 271 result->clear(); | 279 result->clear(); |
| 272 else | 280 else |
| 273 result->assign([contents UTF8String]); | 281 result->assign([contents UTF8String]); |
| 274 } | 282 } |
| 275 | 283 |
| 276 void Clipboard::ReadHTML(Clipboard::Buffer buffer, string16* markup, | 284 void Clipboard::ReadHTML(Clipboard::Buffer buffer, string16* markup, |
| 277 std::string* src_url, uint32* fragment_start, | 285 std::string* src_url, uint32* fragment_start, |
| 278 uint32* fragment_end) const { | 286 uint32* fragment_end) const { |
| 287 DCHECK(CalledOnValidThread()); |
| 279 DCHECK_EQ(buffer, BUFFER_STANDARD); | 288 DCHECK_EQ(buffer, BUFFER_STANDARD); |
| 280 | 289 |
| 281 // TODO(avi): src_url? | 290 // TODO(avi): src_url? |
| 282 markup->clear(); | 291 markup->clear(); |
| 283 if (src_url) | 292 if (src_url) |
| 284 src_url->clear(); | 293 src_url->clear(); |
| 285 | 294 |
| 286 NSPasteboard* pb = GetPasteboard(); | 295 NSPasteboard* pb = GetPasteboard(); |
| 287 NSArray* supportedTypes = [NSArray arrayWithObjects:NSHTMLPboardType, | 296 NSArray* supportedTypes = [NSArray arrayWithObjects:NSHTMLPboardType, |
| 288 NSRTFPboardType, | 297 NSRTFPboardType, |
| 289 NSStringPboardType, | 298 NSStringPboardType, |
| 290 nil]; | 299 nil]; |
| 291 NSString* bestType = [pb availableTypeFromArray:supportedTypes]; | 300 NSString* bestType = [pb availableTypeFromArray:supportedTypes]; |
| 292 if (bestType) { | 301 if (bestType) { |
| 293 NSString* contents = [pb stringForType:bestType]; | 302 NSString* contents = [pb stringForType:bestType]; |
| 294 if ([bestType isEqualToString:NSRTFPboardType]) | 303 if ([bestType isEqualToString:NSRTFPboardType]) |
| 295 contents = [pb htmlFromRtf]; | 304 contents = [pb htmlFromRtf]; |
| 296 UTF8ToUTF16([contents UTF8String], | 305 UTF8ToUTF16([contents UTF8String], |
| 297 [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding], | 306 [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding], |
| 298 markup); | 307 markup); |
| 299 } | 308 } |
| 300 | 309 |
| 301 *fragment_start = 0; | 310 *fragment_start = 0; |
| 302 DCHECK(markup->length() <= kuint32max); | 311 DCHECK(markup->length() <= kuint32max); |
| 303 *fragment_end = static_cast<uint32>(markup->length()); | 312 *fragment_end = static_cast<uint32>(markup->length()); |
| 304 } | 313 } |
| 305 | 314 |
| 306 SkBitmap Clipboard::ReadImage(Buffer buffer) const { | 315 SkBitmap Clipboard::ReadImage(Buffer buffer) const { |
| 316 DCHECK(CalledOnValidThread()); |
| 307 DCHECK_EQ(buffer, BUFFER_STANDARD); | 317 DCHECK_EQ(buffer, BUFFER_STANDARD); |
| 308 | 318 |
| 309 scoped_nsobject<NSImage> image( | 319 scoped_nsobject<NSImage> image( |
| 310 [[NSImage alloc] initWithPasteboard:GetPasteboard()]); | 320 [[NSImage alloc] initWithPasteboard:GetPasteboard()]); |
| 311 if (!image.get()) | 321 if (!image.get()) |
| 312 return SkBitmap(); | 322 return SkBitmap(); |
| 313 | 323 |
| 314 gfx::ScopedNSGraphicsContextSaveGState scoped_state; | 324 gfx::ScopedNSGraphicsContextSaveGState scoped_state; |
| 315 [image setFlipped:YES]; | 325 [image setFlipped:YES]; |
| 316 int width = [image size].width; | 326 int width = [image size].width; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 327 fromRect:NSZeroRect | 337 fromRect:NSZeroRect |
| 328 operation:NSCompositeCopy | 338 operation:NSCompositeCopy |
| 329 fraction:1.0]; | 339 fraction:1.0]; |
| 330 } | 340 } |
| 331 return canvas.ExtractBitmap(); | 341 return canvas.ExtractBitmap(); |
| 332 } | 342 } |
| 333 | 343 |
| 334 void Clipboard::ReadCustomData(Buffer buffer, | 344 void Clipboard::ReadCustomData(Buffer buffer, |
| 335 const string16& type, | 345 const string16& type, |
| 336 string16* result) const { | 346 string16* result) const { |
| 347 DCHECK(CalledOnValidThread()); |
| 337 DCHECK_EQ(buffer, BUFFER_STANDARD); | 348 DCHECK_EQ(buffer, BUFFER_STANDARD); |
| 338 | 349 |
| 339 NSPasteboard* pb = GetPasteboard(); | 350 NSPasteboard* pb = GetPasteboard(); |
| 340 if ([[pb types] containsObject:kWebCustomDataPboardType]) { | 351 if ([[pb types] containsObject:kWebCustomDataPboardType]) { |
| 341 NSData* data = [pb dataForType:kWebCustomDataPboardType]; | 352 NSData* data = [pb dataForType:kWebCustomDataPboardType]; |
| 342 if ([data length]) | 353 if ([data length]) |
| 343 ReadCustomDataForType([data bytes], [data length], type, result); | 354 ReadCustomDataForType([data bytes], [data length], type, result); |
| 344 } | 355 } |
| 345 } | 356 } |
| 346 | 357 |
| 347 void Clipboard::ReadBookmark(string16* title, std::string* url) const { | 358 void Clipboard::ReadBookmark(string16* title, std::string* url) const { |
| 359 DCHECK(CalledOnValidThread()); |
| 348 NSPasteboard* pb = GetPasteboard(); | 360 NSPasteboard* pb = GetPasteboard(); |
| 349 | 361 |
| 350 if (title) { | 362 if (title) { |
| 351 NSString* contents = [pb stringForType:kUTTypeURLName]; | 363 NSString* contents = [pb stringForType:kUTTypeURLName]; |
| 352 UTF8ToUTF16([contents UTF8String], | 364 UTF8ToUTF16([contents UTF8String], |
| 353 [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding], | 365 [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding], |
| 354 title); | 366 title); |
| 355 } | 367 } |
| 356 | 368 |
| 357 if (url) { | 369 if (url) { |
| 358 NSString* url_string = [[NSURL URLFromPasteboard:pb] absoluteString]; | 370 NSString* url_string = [[NSURL URLFromPasteboard:pb] absoluteString]; |
| 359 if (!url_string) | 371 if (!url_string) |
| 360 url->clear(); | 372 url->clear(); |
| 361 else | 373 else |
| 362 url->assign([url_string UTF8String]); | 374 url->assign([url_string UTF8String]); |
| 363 } | 375 } |
| 364 } | 376 } |
| 365 | 377 |
| 366 void Clipboard::ReadFile(FilePath* file) const { | 378 void Clipboard::ReadFile(FilePath* file) const { |
| 379 DCHECK(CalledOnValidThread()); |
| 367 if (!file) { | 380 if (!file) { |
| 368 NOTREACHED(); | 381 NOTREACHED(); |
| 369 return; | 382 return; |
| 370 } | 383 } |
| 371 | 384 |
| 372 *file = FilePath(); | 385 *file = FilePath(); |
| 373 std::vector<FilePath> files; | 386 std::vector<FilePath> files; |
| 374 ReadFiles(&files); | 387 ReadFiles(&files); |
| 375 | 388 |
| 376 // Take the first file, if available. | 389 // Take the first file, if available. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 389 NSPasteboard* pb = GetPasteboard(); | 402 NSPasteboard* pb = GetPasteboard(); |
| 390 NSArray* fileList = [pb propertyListForType:NSFilenamesPboardType]; | 403 NSArray* fileList = [pb propertyListForType:NSFilenamesPboardType]; |
| 391 | 404 |
| 392 for (unsigned int i = 0; i < [fileList count]; ++i) { | 405 for (unsigned int i = 0; i < [fileList count]; ++i) { |
| 393 std::string file = [[fileList objectAtIndex:i] UTF8String]; | 406 std::string file = [[fileList objectAtIndex:i] UTF8String]; |
| 394 files->push_back(FilePath(file)); | 407 files->push_back(FilePath(file)); |
| 395 } | 408 } |
| 396 } | 409 } |
| 397 | 410 |
| 398 void Clipboard::ReadData(const FormatType& format, std::string* result) const { | 411 void Clipboard::ReadData(const FormatType& format, std::string* result) const { |
| 412 DCHECK(CalledOnValidThread()); |
| 399 NSPasteboard* pb = GetPasteboard(); | 413 NSPasteboard* pb = GetPasteboard(); |
| 400 NSData* data = [pb dataForType:format.ToNSString()]; | 414 NSData* data = [pb dataForType:format.ToNSString()]; |
| 401 if ([data length]) | 415 if ([data length]) |
| 402 result->assign(static_cast<const char*>([data bytes]), [data length]); | 416 result->assign(static_cast<const char*>([data bytes]), [data length]); |
| 403 } | 417 } |
| 404 | 418 |
| 405 // static | 419 // static |
| 406 Clipboard::FormatType Clipboard::GetFormatType( | 420 Clipboard::FormatType Clipboard::GetFormatType( |
| 407 const std::string& format_string) { | 421 const std::string& format_string) { |
| 408 return FormatType::Deserialize(format_string); | 422 return FormatType::Deserialize(format_string); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 return type; | 473 return type; |
| 460 } | 474 } |
| 461 | 475 |
| 462 // static | 476 // static |
| 463 const Clipboard::FormatType& Clipboard::GetWebCustomDataFormatType() { | 477 const Clipboard::FormatType& Clipboard::GetWebCustomDataFormatType() { |
| 464 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kWebCustomDataPboardType)); | 478 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kWebCustomDataPboardType)); |
| 465 return type; | 479 return type; |
| 466 } | 480 } |
| 467 | 481 |
| 468 } // namespace ui | 482 } // namespace ui |
| OLD | NEW |