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.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" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/mac/mac_util.h" | 12 #include "base/mac/mac_util.h" |
13 #include "base/mac/scoped_cftyperef.h" | 13 #include "base/mac/scoped_cftyperef.h" |
14 #include "base/memory/scoped_nsobject.h" | 14 #include "base/memory/scoped_nsobject.h" |
15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
16 #include "base/sys_string_conversions.h" | 16 #include "base/sys_string_conversions.h" |
17 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
18 #import "third_party/mozilla/NSPasteboard+Utils.h" | 18 #import "third_party/mozilla/NSPasteboard+Utils.h" |
19 #include "third_party/skia/include/core/SkBitmap.h" | 19 #include "third_party/skia/include/core/SkBitmap.h" |
20 #include "ui/base/clipboard/custom_data_helper.h" | 20 #include "ui/base/clipboard/custom_data_helper.h" |
21 #include "ui/gfx/canvas.h" | 21 #include "ui/gfx/canvas.h" |
22 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 22 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
23 #include "ui/gfx/size.h" | 23 #include "ui/gfx/size.h" |
24 | 24 |
25 namespace ui { | 25 namespace ui { |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
| 29 // Source tag format type. |
| 30 NSString* const kSourceTagPboardType = @"org.chromium.source-tag-data"; |
| 31 |
29 // Would be nice if this were in UTCoreTypes.h, but it isn't | 32 // Would be nice if this were in UTCoreTypes.h, but it isn't |
30 NSString* const kUTTypeURLName = @"public.url-name"; | 33 NSString* const kUTTypeURLName = @"public.url-name"; |
31 | 34 |
32 // Tells us if WebKit was the last to write to the pasteboard. There's no | 35 // Tells us if WebKit was the last to write to the pasteboard. There's no |
33 // actual data associated with this type. | 36 // actual data associated with this type. |
34 NSString* const kWebSmartPastePboardType = @"NeXT smart paste pasteboard type"; | 37 NSString* const kWebSmartPastePboardType = @"NeXT smart paste pasteboard type"; |
35 | 38 |
36 // Pepper custom data format type. | 39 // Pepper custom data format type. |
37 NSString* const kPepperCustomDataPboardType = | 40 NSString* const kPepperCustomDataPboardType = |
38 @"org.chromium.pepper-custom-data"; | 41 @"org.chromium.pepper-custom-data"; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 } | 86 } |
84 | 87 |
85 Clipboard::Clipboard() { | 88 Clipboard::Clipboard() { |
86 DCHECK(CalledOnValidThread()); | 89 DCHECK(CalledOnValidThread()); |
87 } | 90 } |
88 | 91 |
89 Clipboard::~Clipboard() { | 92 Clipboard::~Clipboard() { |
90 DCHECK(CalledOnValidThread()); | 93 DCHECK(CalledOnValidThread()); |
91 } | 94 } |
92 | 95 |
93 void Clipboard::WriteObjects(Buffer buffer, const ObjectMap& objects) { | 96 void Clipboard::WriteObjectsImpl(Buffer buffer, |
| 97 const ObjectMap& objects, |
| 98 SourceTag tag) { |
94 DCHECK(CalledOnValidThread()); | 99 DCHECK(CalledOnValidThread()); |
95 DCHECK_EQ(buffer, BUFFER_STANDARD); | 100 DCHECK_EQ(buffer, BUFFER_STANDARD); |
96 | 101 |
97 NSPasteboard* pb = GetPasteboard(); | 102 NSPasteboard* pb = GetPasteboard(); |
98 [pb declareTypes:[NSArray array] owner:nil]; | 103 [pb declareTypes:[NSArray array] owner:nil]; |
99 | 104 |
100 for (ObjectMap::const_iterator iter = objects.begin(); | 105 for (ObjectMap::const_iterator iter = objects.begin(); |
101 iter != objects.end(); ++iter) { | 106 iter != objects.end(); ++iter) { |
102 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); | 107 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); |
103 } | 108 } |
| 109 WriteSourceTag(tag); |
104 } | 110 } |
105 | 111 |
106 void Clipboard::WriteText(const char* text_data, size_t text_len) { | 112 void Clipboard::WriteText(const char* text_data, size_t text_len) { |
107 std::string text_str(text_data, text_len); | 113 std::string text_str(text_data, text_len); |
108 NSString *text = base::SysUTF8ToNSString(text_str); | 114 NSString *text = base::SysUTF8ToNSString(text_str); |
109 NSPasteboard* pb = GetPasteboard(); | 115 NSPasteboard* pb = GetPasteboard(); |
110 [pb addTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; | 116 [pb addTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; |
111 [pb setString:text forType:NSStringPboardType]; | 117 [pb setString:text forType:NSStringPboardType]; |
112 } | 118 } |
113 | 119 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 | 211 |
206 void Clipboard::WriteData(const FormatType& format, | 212 void Clipboard::WriteData(const FormatType& format, |
207 const char* data_data, | 213 const char* data_data, |
208 size_t data_len) { | 214 size_t data_len) { |
209 NSPasteboard* pb = GetPasteboard(); | 215 NSPasteboard* pb = GetPasteboard(); |
210 [pb addTypes:[NSArray arrayWithObject:format.ToNSString()] owner:nil]; | 216 [pb addTypes:[NSArray arrayWithObject:format.ToNSString()] owner:nil]; |
211 [pb setData:[NSData dataWithBytes:data_data length:data_len] | 217 [pb setData:[NSData dataWithBytes:data_data length:data_len] |
212 forType:format.ToNSString()]; | 218 forType:format.ToNSString()]; |
213 } | 219 } |
214 | 220 |
| 221 void Clipboard::WriteSourceTag(SourceTag tag) { |
| 222 if (tag != SourceTag()) { |
| 223 ObjectMapParam binary = SourceTag2Binary(tag); |
| 224 WriteData(GetSourceTagFormatType(), &binary[0], binary.size()); |
| 225 } |
| 226 } |
| 227 |
215 // Write an extra flavor that signifies WebKit was the last to modify the | 228 // Write an extra flavor that signifies WebKit was the last to modify the |
216 // pasteboard. This flavor has no data. | 229 // pasteboard. This flavor has no data. |
217 void Clipboard::WriteWebSmartPaste() { | 230 void Clipboard::WriteWebSmartPaste() { |
218 NSPasteboard* pb = GetPasteboard(); | 231 NSPasteboard* pb = GetPasteboard(); |
219 NSString* format = GetWebKitSmartPasteFormatType().ToNSString(); | 232 NSString* format = GetWebKitSmartPasteFormatType().ToNSString(); |
220 [pb addTypes:[NSArray arrayWithObject:format] owner:nil]; | 233 [pb addTypes:[NSArray arrayWithObject:format] owner:nil]; |
221 [pb setData:nil forType:format]; | 234 [pb setData:nil forType:format]; |
222 } | 235 } |
223 | 236 |
224 uint64 Clipboard::GetSequenceNumber(Buffer buffer) { | 237 uint64 Clipboard::GetSequenceNumber(Buffer buffer) { |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 } | 416 } |
404 | 417 |
405 void Clipboard::ReadData(const FormatType& format, std::string* result) const { | 418 void Clipboard::ReadData(const FormatType& format, std::string* result) const { |
406 DCHECK(CalledOnValidThread()); | 419 DCHECK(CalledOnValidThread()); |
407 NSPasteboard* pb = GetPasteboard(); | 420 NSPasteboard* pb = GetPasteboard(); |
408 NSData* data = [pb dataForType:format.ToNSString()]; | 421 NSData* data = [pb dataForType:format.ToNSString()]; |
409 if ([data length]) | 422 if ([data length]) |
410 result->assign(static_cast<const char*>([data bytes]), [data length]); | 423 result->assign(static_cast<const char*>([data bytes]), [data length]); |
411 } | 424 } |
412 | 425 |
| 426 Clipboard::SourceTag Clipboard::ReadSourceTag(Buffer buffer) const { |
| 427 DCHECK_EQ(buffer, BUFFER_STANDARD); |
| 428 std::string result; |
| 429 ReadData(GetSourceTagFormatType(), &result); |
| 430 return Binary2SourceTag(result); |
| 431 } |
| 432 |
413 // static | 433 // static |
414 Clipboard::FormatType Clipboard::GetFormatType( | 434 Clipboard::FormatType Clipboard::GetFormatType( |
415 const std::string& format_string) { | 435 const std::string& format_string) { |
416 return FormatType::Deserialize(format_string); | 436 return FormatType::Deserialize(format_string); |
417 } | 437 } |
418 | 438 |
419 // static | 439 // static |
420 const Clipboard::FormatType& Clipboard::GetUrlFormatType() { | 440 const Clipboard::FormatType& Clipboard::GetUrlFormatType() { |
421 CR_DEFINE_STATIC_LOCAL(FormatType, type, (NSURLPboardType)); | 441 CR_DEFINE_STATIC_LOCAL(FormatType, type, (NSURLPboardType)); |
422 return type; | 442 return type; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
478 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kWebCustomDataPboardType)); | 498 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kWebCustomDataPboardType)); |
479 return type; | 499 return type; |
480 } | 500 } |
481 | 501 |
482 // static | 502 // static |
483 const Clipboard::FormatType& Clipboard::GetPepperCustomDataFormatType() { | 503 const Clipboard::FormatType& Clipboard::GetPepperCustomDataFormatType() { |
484 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kPepperCustomDataPboardType)); | 504 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kPepperCustomDataPboardType)); |
485 return type; | 505 return type; |
486 } | 506 } |
487 | 507 |
| 508 // static |
| 509 const Clipboard::FormatType& Clipboard::GetSourceTagFormatType() { |
| 510 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kSourceTagPboardType)); |
| 511 return type; |
| 512 } |
| 513 |
488 } // namespace ui | 514 } // namespace ui |
OLD | NEW |