Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(302)

Side by Side Diff: ui/base/clipboard/clipboard_mac.mm

Issue 8801038: Make Clipboard::FormatType an opaque handle type. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Windows compile fixes. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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(const FormatType& other)
58 : data_([other.data_ retain]) {
59 }
60
61 Clipboard::FormatType& Clipboard::FormatType::operator=(
62 const FormatType& other) {
tony 2011/12/06 23:57:20 Why does mac need this?
dcheng 2011/12/07 00:09:02 It's there for other platforms as well, it's just
63 if (this != &other) {
64 [data_ release];
65 data_ = [other.data_ retain];
66 }
67 return *this;
68 }
69
70 Clipboard::FormatType::~FormatType() {
71 [data_ release];
72 }
73
74 std::string Clipboard::FormatType::Serialize() const {
75 return base::SysNSStringToUTF8(data_);
76 }
77
78 // static
79 Clipboard::FormatType Clipboard::FormatType::Deserialize(
80 const std::string& serialization) {
81 return FormatType(base::SysUTF8ToNSString(serialization));
82 }
tony 2011/12/06 23:57:21 Where's Equals? Maybe we don't need it? Wouldn't
dcheng 2011/12/07 00:09:02 I intentionally left it out since there's no one t
83
48 Clipboard::Clipboard() { 84 Clipboard::Clipboard() {
49 } 85 }
50 86
51 Clipboard::~Clipboard() { 87 Clipboard::~Clipboard() {
52 } 88 }
53 89
54 void Clipboard::WriteObjects(const ObjectMap& objects) { 90 void Clipboard::WriteObjects(const ObjectMap& objects) {
55 NSPasteboard* pb = GetPasteboard(); 91 NSPasteboard* pb = GetPasteboard();
56 [pb declareTypes:[NSArray array] owner:nil]; 92 [pb declareTypes:[NSArray array] owner:nil];
57 93
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 // For now, spit out the image as a TIFF. 186 // For now, spit out the image as a TIFF.
151 NSPasteboard* pb = GetPasteboard(); 187 NSPasteboard* pb = GetPasteboard();
152 [pb addTypes:[NSArray arrayWithObject:NSTIFFPboardType] owner:nil]; 188 [pb addTypes:[NSArray arrayWithObject:NSTIFFPboardType] owner:nil];
153 NSData *tiff_data = [image TIFFRepresentation]; 189 NSData *tiff_data = [image TIFFRepresentation];
154 LOG_IF(ERROR, tiff_data == NULL) << "Failed to allocate image for clipboard"; 190 LOG_IF(ERROR, tiff_data == NULL) << "Failed to allocate image for clipboard";
155 if (tiff_data) { 191 if (tiff_data) {
156 [pb setData:tiff_data forType:NSTIFFPboardType]; 192 [pb setData:tiff_data forType:NSTIFFPboardType];
157 } 193 }
158 } 194 }
159 195
160 void Clipboard::WriteData(const char* format_name, size_t format_len, 196 void Clipboard::WriteData(const FormatType& format,
161 const char* data_data, size_t data_len) { 197 const char* data_data,
198 size_t data_len) {
162 NSPasteboard* pb = GetPasteboard(); 199 NSPasteboard* pb = GetPasteboard();
163 scoped_nsobject<NSString> format( 200 [pb addTypes:[NSArray arrayWithObject:format.ToNSString()] 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] 201 [pb setData:[NSData dataWithBytes:data_data length:data_len]
169 forType:format]; 202 forType:format.ToNSString()];
170 } 203 }
171 204
172 // 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
173 // pasteboard. This flavor has no data. 206 // pasteboard. This flavor has no data.
174 void Clipboard::WriteWebSmartPaste() { 207 void Clipboard::WriteWebSmartPaste() {
175 NSPasteboard* pb = GetPasteboard(); 208 NSPasteboard* pb = GetPasteboard();
176 NSString* format = base::SysUTF8ToNSString(GetWebKitSmartPasteFormatType()); 209 NSString* format = GetWebKitSmartPasteFormatType().ToNSString();
177 [pb addTypes:[NSArray arrayWithObject:format] owner:nil]; 210 [pb addTypes:[NSArray arrayWithObject:format] owner:nil];
178 [pb setData:nil forType:format]; 211 [pb setData:nil forType:format];
179 } 212 }
180 213
181 uint64 Clipboard::GetSequenceNumber(Buffer buffer) { 214 uint64 Clipboard::GetSequenceNumber(Buffer buffer) {
182 DCHECK_EQ(buffer, BUFFER_STANDARD); 215 DCHECK_EQ(buffer, BUFFER_STANDARD);
183 216
184 NSPasteboard* pb = GetPasteboard(); 217 NSPasteboard* pb = GetPasteboard();
185 return [pb changeCount]; 218 return [pb changeCount];
186 } 219 }
187 220
188 bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format, 221 bool Clipboard::IsFormatAvailable(const FormatType& format,
189 Buffer buffer) const { 222 Buffer buffer) const {
190 DCHECK_EQ(buffer, BUFFER_STANDARD); 223 DCHECK_EQ(buffer, BUFFER_STANDARD);
191 NSString* format_ns = base::SysUTF8ToNSString(format);
192 224
193 NSPasteboard* pb = GetPasteboard(); 225 NSPasteboard* pb = GetPasteboard();
194 NSArray* types = [pb types]; 226 NSArray* types = [pb types];
195 227
196 // Safari only places RTF on the pasteboard, never HTML. We can convert RTF 228 // 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. 229 // to HTML, so the presence of either indicates success when looking for HTML.
198 if ([format_ns isEqualToString:NSHTMLPboardType]) { 230 if ([format.ToNSString() isEqualToString:NSHTMLPboardType]) {
199 return [types containsObject:NSHTMLPboardType] || 231 return [types containsObject:NSHTMLPboardType] ||
200 [types containsObject:NSRTFPboardType]; 232 [types containsObject:NSRTFPboardType];
201 } 233 }
202 return [types containsObject:format_ns]; 234 return [types containsObject:format.ToNSString()];
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 } 235 }
215 236
216 void Clipboard::ReadAvailableTypes(Clipboard::Buffer buffer, 237 void Clipboard::ReadAvailableTypes(Clipboard::Buffer buffer,
217 std::vector<string16>* types, 238 std::vector<string16>* types,
218 bool* contains_filenames) const { 239 bool* contains_filenames) const {
219 if (!types || !contains_filenames) {
220 NOTREACHED();
221 return;
222 }
223
224 types->clear(); 240 types->clear();
225 if (IsFormatAvailable(Clipboard::GetPlainTextFormatType(), buffer)) 241 if (IsFormatAvailable(Clipboard::GetPlainTextFormatType(), buffer))
226 types->push_back(UTF8ToUTF16(kMimeTypeText)); 242 types->push_back(UTF8ToUTF16(kMimeTypeText));
227 if (IsFormatAvailable(Clipboard::GetHtmlFormatType(), buffer)) 243 if (IsFormatAvailable(Clipboard::GetHtmlFormatType(), buffer))
228 types->push_back(UTF8ToUTF16(kMimeTypeHTML)); 244 types->push_back(UTF8ToUTF16(kMimeTypeHTML));
229 if ([NSImage canInitWithPasteboard:GetPasteboard()]) 245 if ([NSImage canInitWithPasteboard:GetPasteboard()])
230 types->push_back(UTF8ToUTF16(kMimeTypePNG)); 246 types->push_back(UTF8ToUTF16(kMimeTypePNG));
231 *contains_filenames = false; 247 *contains_filenames = false;
232 248
233 NSPasteboard* pb = GetPasteboard(); 249 NSPasteboard* pb = GetPasteboard();
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 391
376 NSPasteboard* pb = GetPasteboard(); 392 NSPasteboard* pb = GetPasteboard();
377 NSArray* fileList = [pb propertyListForType:NSFilenamesPboardType]; 393 NSArray* fileList = [pb propertyListForType:NSFilenamesPboardType];
378 394
379 for (unsigned int i = 0; i < [fileList count]; ++i) { 395 for (unsigned int i = 0; i < [fileList count]; ++i) {
380 std::string file = [[fileList objectAtIndex:i] UTF8String]; 396 std::string file = [[fileList objectAtIndex:i] UTF8String];
381 files->push_back(FilePath(file)); 397 files->push_back(FilePath(file));
382 } 398 }
383 } 399 }
384 400
385 void Clipboard::ReadData(const std::string& format, std::string* result) const { 401 void Clipboard::ReadData(const FormatType& format, std::string* result) const {
386 NSPasteboard* pb = GetPasteboard(); 402 NSPasteboard* pb = GetPasteboard();
387 NSData* data = [pb dataForType:base::SysUTF8ToNSString(format)]; 403 NSData* data = [pb dataForType:format.ToNSString()];
388 if ([data length]) 404 if ([data length])
389 result->assign(static_cast<const char*>([data bytes]), [data length]); 405 result->assign(static_cast<const char*>([data bytes]), [data length]);
390 } 406 }
391 407
392 // static 408 // static
393 Clipboard::FormatType Clipboard::GetUrlFormatType() { 409 Clipboard::FormatType Clipboard::GetFormatType(
394 return base::SysNSStringToUTF8(NSURLPboardType); 410 const std::string& format_string) {
411 return FormatType::Deserialize(format_string);
395 } 412 }
396 413
397 // static 414 // static
398 Clipboard::FormatType Clipboard::GetUrlWFormatType() { 415 const Clipboard::FormatType& Clipboard::GetUrlFormatType() {
399 return base::SysNSStringToUTF8(NSURLPboardType); 416 CR_DEFINE_STATIC_LOCAL(FormatType, type, (NSURLPboardType));
417 return type;
400 } 418 }
401 419
402 // static 420 // static
403 Clipboard::FormatType Clipboard::GetPlainTextFormatType() { 421 const Clipboard::FormatType& Clipboard::GetUrlWFormatType() {
404 return base::SysNSStringToUTF8(NSStringPboardType); 422 return GetUrlFormatType();
405 } 423 }
406 424
407 // static 425 // static
408 Clipboard::FormatType Clipboard::GetPlainTextWFormatType() { 426 const Clipboard::FormatType& Clipboard::GetPlainTextFormatType() {
409 return base::SysNSStringToUTF8(NSStringPboardType); 427 CR_DEFINE_STATIC_LOCAL(FormatType, type, (NSStringPboardType));
428 return type;
410 } 429 }
411 430
412 // static 431 // static
413 Clipboard::FormatType Clipboard::GetFilenameFormatType() { 432 const Clipboard::FormatType& Clipboard::GetPlainTextWFormatType() {
414 return base::SysNSStringToUTF8(NSFilenamesPboardType); 433 return GetPlainTextFormatType();
415 } 434 }
416 435
417 // static 436 // static
418 Clipboard::FormatType Clipboard::GetFilenameWFormatType() { 437 const Clipboard::FormatType& Clipboard::GetFilenameFormatType() {
419 return base::SysNSStringToUTF8(NSFilenamesPboardType); 438 CR_DEFINE_STATIC_LOCAL(FormatType, type, (NSFilenamesPboardType));
439 return type;
420 } 440 }
421 441
422 // static 442 // static
423 Clipboard::FormatType Clipboard::GetHtmlFormatType() { 443 const Clipboard::FormatType& Clipboard::GetFilenameWFormatType() {
424 return base::SysNSStringToUTF8(NSHTMLPboardType); 444 return GetFilenameFormatType();
425 } 445 }
426 446
427 // static 447 // static
428 Clipboard::FormatType Clipboard::GetBitmapFormatType() { 448 const Clipboard::FormatType& Clipboard::GetHtmlFormatType() {
429 return base::SysNSStringToUTF8(NSTIFFPboardType); 449 CR_DEFINE_STATIC_LOCAL(FormatType, type, (NSHTMLPboardType));
450 return type;
430 } 451 }
431 452
432 // static 453 // static
433 Clipboard::FormatType Clipboard::GetWebKitSmartPasteFormatType() { 454 const Clipboard::FormatType& Clipboard::GetBitmapFormatType() {
434 return base::SysNSStringToUTF8(kWebSmartPastePboardType); 455 CR_DEFINE_STATIC_LOCAL(FormatType, type, (NSTIFFPboardType));
456 return type;
435 } 457 }
436 458
437 // static 459 // static
438 Clipboard::FormatType Clipboard::GetWebCustomDataFormatType() { 460 const Clipboard::FormatType& Clipboard::GetWebKitSmartPasteFormatType() {
439 return base::SysNSStringToUTF8(kWebCustomDataType); 461 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kWebSmartPastePboardType));
462 return type;
463 }
464
465 // static
466 const Clipboard::FormatType& Clipboard::GetWebCustomDataFormatType() {
467 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kWebCustomDataType));
468 return type;
440 } 469 }
441 470
442 } // namespace ui 471 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698