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/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/mac/mac_util.h" | 11 #include "base/mac/mac_util.h" |
12 #include "base/mac/scoped_cftyperef.h" | 12 #include "base/mac/scoped_cftyperef.h" |
13 #include "base/memory/scoped_nsobject.h" | 13 #include "base/memory/scoped_nsobject.h" |
14 #include "base/sys_string_conversions.h" | 14 #include "base/sys_string_conversions.h" |
15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
16 #import "third_party/mozilla/NSPasteboard+Utils.h" | 16 #import "third_party/mozilla/NSPasteboard+Utils.h" |
17 #include "third_party/skia/include/core/SkBitmap.h" | 17 #include "third_party/skia/include/core/SkBitmap.h" |
| 18 #include "ui/base/clipboard/custom_data_helper.h" |
18 #include "ui/gfx/canvas_skia.h" | 19 #include "ui/gfx/canvas_skia.h" |
19 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 20 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
20 #include "ui/gfx/size.h" | 21 #include "ui/gfx/size.h" |
21 | 22 |
22 namespace ui { | 23 namespace ui { |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 // Would be nice if this were in UTCoreTypes.h, but it isn't | 27 // Would be nice if this were in UTCoreTypes.h, but it isn't |
27 NSString* const kUTTypeURLName = @"public.url-name"; | 28 NSString* const kUTTypeURLName = @"public.url-name"; |
28 | 29 |
29 // Tells us if WebKit was the last to write to the pasteboard. There's no | 30 // Tells us if WebKit was the last to write to the pasteboard. There's no |
30 // actual data associated with this type. | 31 // actual data associated with this type. |
31 NSString* const kWebSmartPastePboardType = @"NeXT smart paste pasteboard type"; | 32 NSString* const kWebSmartPastePboardType = @"NeXT smart paste pasteboard type"; |
32 | 33 |
| 34 // TODO(dcheng): This name is temporary. See crbug.com/106449. |
| 35 NSString* const kWebCustomDataType = @"org.chromium.web-custom-data"; |
| 36 |
33 NSPasteboard* GetPasteboard() { | 37 NSPasteboard* GetPasteboard() { |
34 // The pasteboard should not be nil in a UI session, but this handy DCHECK | 38 // The pasteboard should not be nil in a UI session, but this handy DCHECK |
35 // can help track down problems if someone tries using clipboard code outside | 39 // can help track down problems if someone tries using clipboard code outside |
36 // of a UI session. | 40 // of a UI session. |
37 NSPasteboard* pasteboard = [NSPasteboard generalPasteboard]; | 41 NSPasteboard* pasteboard = [NSPasteboard generalPasteboard]; |
38 DCHECK(pasteboard); | 42 DCHECK(pasteboard); |
39 return pasteboard; | 43 return pasteboard; |
40 } | 44 } |
41 | 45 |
42 } // namespace | 46 } // namespace |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 NSData *tiff_data = [image TIFFRepresentation]; | 153 NSData *tiff_data = [image TIFFRepresentation]; |
150 LOG_IF(ERROR, tiff_data == NULL) << "Failed to allocate image for clipboard"; | 154 LOG_IF(ERROR, tiff_data == NULL) << "Failed to allocate image for clipboard"; |
151 if (tiff_data) { | 155 if (tiff_data) { |
152 [pb setData:tiff_data forType:NSTIFFPboardType]; | 156 [pb setData:tiff_data forType:NSTIFFPboardType]; |
153 } | 157 } |
154 } | 158 } |
155 | 159 |
156 void Clipboard::WriteData(const char* format_name, size_t format_len, | 160 void Clipboard::WriteData(const char* format_name, size_t format_len, |
157 const char* data_data, size_t data_len) { | 161 const char* data_data, size_t data_len) { |
158 NSPasteboard* pb = GetPasteboard(); | 162 NSPasteboard* pb = GetPasteboard(); |
159 NSString* format = [[NSString alloc] initWithBytes:format_name | 163 scoped_nsobject<NSString> format( |
160 length:format_len | 164 [[NSString alloc] initWithBytes:format_name |
161 encoding:NSUTF8StringEncoding]; | 165 length:format_len |
| 166 encoding:NSUTF8StringEncoding]); |
162 [pb addTypes:[NSArray arrayWithObject:format] owner:nil]; | 167 [pb addTypes:[NSArray arrayWithObject:format] owner:nil]; |
163 [pb setData:[NSData dataWithBytes:data_data length:data_len] | 168 [pb setData:[NSData dataWithBytes:data_data length:data_len] |
164 forType:format]; | 169 forType:format]; |
165 } | 170 } |
166 | 171 |
167 // Write an extra flavor that signifies WebKit was the last to modify the | 172 // Write an extra flavor that signifies WebKit was the last to modify the |
168 // pasteboard. This flavor has no data. | 173 // pasteboard. This flavor has no data. |
169 void Clipboard::WriteWebSmartPaste() { | 174 void Clipboard::WriteWebSmartPaste() { |
170 NSPasteboard* pb = GetPasteboard(); | 175 NSPasteboard* pb = GetPasteboard(); |
171 NSString* format = base::SysUTF8ToNSString(GetWebKitSmartPasteFormatType()); | 176 NSString* format = base::SysUTF8ToNSString(GetWebKitSmartPasteFormatType()); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 } | 222 } |
218 | 223 |
219 types->clear(); | 224 types->clear(); |
220 if (IsFormatAvailable(Clipboard::GetPlainTextFormatType(), buffer)) | 225 if (IsFormatAvailable(Clipboard::GetPlainTextFormatType(), buffer)) |
221 types->push_back(UTF8ToUTF16(kMimeTypeText)); | 226 types->push_back(UTF8ToUTF16(kMimeTypeText)); |
222 if (IsFormatAvailable(Clipboard::GetHtmlFormatType(), buffer)) | 227 if (IsFormatAvailable(Clipboard::GetHtmlFormatType(), buffer)) |
223 types->push_back(UTF8ToUTF16(kMimeTypeHTML)); | 228 types->push_back(UTF8ToUTF16(kMimeTypeHTML)); |
224 if ([NSImage canInitWithPasteboard:GetPasteboard()]) | 229 if ([NSImage canInitWithPasteboard:GetPasteboard()]) |
225 types->push_back(UTF8ToUTF16(kMimeTypePNG)); | 230 types->push_back(UTF8ToUTF16(kMimeTypePNG)); |
226 *contains_filenames = false; | 231 *contains_filenames = false; |
| 232 |
| 233 NSPasteboard* pb = GetPasteboard(); |
| 234 if ([[pb types] containsObject:kWebCustomDataType]) { |
| 235 NSData* data = [pb dataForType:kWebCustomDataType]; |
| 236 if ([data length]) |
| 237 ReadCustomDataTypes([data bytes], [data length], types); |
| 238 } |
227 } | 239 } |
228 | 240 |
229 void Clipboard::ReadText(Clipboard::Buffer buffer, string16* result) const { | 241 void Clipboard::ReadText(Clipboard::Buffer buffer, string16* result) const { |
230 DCHECK_EQ(buffer, BUFFER_STANDARD); | 242 DCHECK_EQ(buffer, BUFFER_STANDARD); |
231 NSPasteboard* pb = GetPasteboard(); | 243 NSPasteboard* pb = GetPasteboard(); |
232 NSString* contents = [pb stringForType:NSStringPboardType]; | 244 NSString* contents = [pb stringForType:NSStringPboardType]; |
233 | 245 |
234 UTF8ToUTF16([contents UTF8String], | 246 UTF8ToUTF16([contents UTF8String], |
235 [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding], | 247 [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding], |
236 result); | 248 result); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 fromRect:NSZeroRect | 314 fromRect:NSZeroRect |
303 operation:NSCompositeCopy | 315 operation:NSCompositeCopy |
304 fraction:1.0]; | 316 fraction:1.0]; |
305 } | 317 } |
306 return canvas.ExtractBitmap(); | 318 return canvas.ExtractBitmap(); |
307 } | 319 } |
308 | 320 |
309 void Clipboard::ReadCustomData(Buffer buffer, | 321 void Clipboard::ReadCustomData(Buffer buffer, |
310 const string16& type, | 322 const string16& type, |
311 string16* result) const { | 323 string16* result) const { |
312 // TODO(dcheng): Implement this. | 324 DCHECK_EQ(buffer, BUFFER_STANDARD); |
313 NOTIMPLEMENTED(); | 325 |
| 326 NSPasteboard* pb = GetPasteboard(); |
| 327 if ([[pb types] containsObject:kWebCustomDataType]) { |
| 328 NSData* data = [pb dataForType:kWebCustomDataType]; |
| 329 if ([data length]) |
| 330 ReadCustomDataForType([data bytes], [data length], type, result); |
| 331 } |
314 } | 332 } |
315 | 333 |
316 void Clipboard::ReadBookmark(string16* title, std::string* url) const { | 334 void Clipboard::ReadBookmark(string16* title, std::string* url) const { |
317 NSPasteboard* pb = GetPasteboard(); | 335 NSPasteboard* pb = GetPasteboard(); |
318 | 336 |
319 if (title) { | 337 if (title) { |
320 NSString* contents = [pb stringForType:kUTTypeURLName]; | 338 NSString* contents = [pb stringForType:kUTTypeURLName]; |
321 UTF8ToUTF16([contents UTF8String], | 339 UTF8ToUTF16([contents UTF8String], |
322 [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding], | 340 [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding], |
323 title); | 341 title); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 // static | 427 // static |
410 Clipboard::FormatType Clipboard::GetBitmapFormatType() { | 428 Clipboard::FormatType Clipboard::GetBitmapFormatType() { |
411 return base::SysNSStringToUTF8(NSTIFFPboardType); | 429 return base::SysNSStringToUTF8(NSTIFFPboardType); |
412 } | 430 } |
413 | 431 |
414 // static | 432 // static |
415 Clipboard::FormatType Clipboard::GetWebKitSmartPasteFormatType() { | 433 Clipboard::FormatType Clipboard::GetWebKitSmartPasteFormatType() { |
416 return base::SysNSStringToUTF8(kWebSmartPastePboardType); | 434 return base::SysNSStringToUTF8(kWebSmartPastePboardType); |
417 } | 435 } |
418 | 436 |
| 437 // static |
| 438 Clipboard::FormatType Clipboard::GetWebCustomDataFormatType() { |
| 439 return base::SysNSStringToUTF8(kWebCustomDataType); |
| 440 } |
| 441 |
419 } // namespace ui | 442 } // namespace ui |
OLD | NEW |