1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <algorithm>
#include "mozilla/gfx/2D.h"
#include "mozilla/Logging.h"
#include "mozilla/Unused.h"
#include "gfxPlatform.h"
#include "nsArrayUtils.h"
#include "nsCOMPtr.h"
#include "nsClipboard.h"
#include "nsString.h"
#include "nsISupportsPrimitives.h"
#include "nsPrimitiveHelpers.h"
#include "nsIFile.h"
#include "nsStringStream.h"
#include "nsEscape.h"
#include "nsPrintfCString.h"
#include "nsObjCExceptions.h"
#include "imgIContainer.h"
#include "nsCocoaUtils.h"
using mozilla::gfx::DataSourceSurface;
using mozilla::gfx::SourceSurface;
mozilla::StaticRefPtr<nsITransferable> nsClipboard::sSelectionCache;
int32_t nsClipboard::sSelectionCacheChangeCount = 0;
nsClipboard::nsClipboard()
: nsBaseClipboard(mozilla::dom::ClipboardCapabilities(
false /* supportsSelectionClipboard */,
true /* supportsFindClipboard */,
true /* supportsSelectionCache */)) {}
nsClipboard::~nsClipboard() { ClearSelectionCache(); }
NS_IMPL_ISUPPORTS_INHERITED0(nsClipboard, nsBaseClipboard)
namespace {
// We separate this into its own function because after an @try, all local
// variables within that function get marked as volatile, and our C++ type
// system doesn't like volatile things.
static NSData* GetDataFromPasteboard(NSPasteboard* aPasteboard,
NSString* aType) {
NSData* data = nil;
@try {
data = [aPasteboard dataForType:aType];
} @catch (NSException* e) {
NS_WARNING(nsPrintfCString("Exception raised while getting data from the "
"pasteboard: \"%s - %s\"",
[[e name] UTF8String], [[e reason] UTF8String])
.get());
mozilla::Unused << e;
}
return data;
}
static NSPasteboard* GetPasteboard(int32_t aWhichClipboard) {
switch (aWhichClipboard) {
case nsIClipboard::kGlobalClipboard:
return [NSPasteboard generalPasteboard];
case nsIClipboard::kFindClipboard:
return [NSPasteboard pasteboardWithName:NSPasteboardNameFind];
default:
return nil;
}
}
} // namespace
void nsClipboard::SetSelectionCache(nsITransferable* aTransferable) {
sSelectionCacheChangeCount++;
sSelectionCache = aTransferable;
}
void nsClipboard::ClearSelectionCache() { SetSelectionCache(nullptr); }
NS_IMETHODIMP
nsClipboard::SetNativeClipboardData(nsITransferable* aTransferable,
int32_t aWhichClipboard) {
NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
MOZ_ASSERT(aTransferable);
MOZ_ASSERT(nsIClipboard::IsClipboardTypeSupported(aWhichClipboard));
if (aWhichClipboard == kSelectionCache) {
SetSelectionCache(aTransferable);
return NS_OK;
}
NSDictionary* pasteboardOutputDict =
PasteboardDictFromTransferable(aTransferable);
if (!pasteboardOutputDict) return NS_ERROR_FAILURE;
unsigned int outputCount = [pasteboardOutputDict count];
NSArray* outputKeys = [pasteboardOutputDict allKeys];
NSPasteboard* cocoaPasteboard = GetPasteboard(aWhichClipboard);
MOZ_ASSERT(cocoaPasteboard);
if (aWhichClipboard == kFindClipboard) {
NSString* stringType =
[UTIHelper stringFromPboardType:NSPasteboardTypeString];
[cocoaPasteboard declareTypes:[NSArray arrayWithObject:stringType]
owner:nil];
} else {
// Write everything else out to the general pasteboard.
MOZ_ASSERT(aWhichClipboard == kGlobalClipboard);
[cocoaPasteboard declareTypes:outputKeys owner:nil];
}
for (unsigned int i = 0; i < outputCount; i++) {
NSString* currentKey = [outputKeys objectAtIndex:i];
id currentValue = [pasteboardOutputDict valueForKey:currentKey];
if (aWhichClipboard == kFindClipboard) {
if ([currentKey isEqualToString:[UTIHelper stringFromPboardType:
NSPasteboardTypeString]]) {
[cocoaPasteboard setString:currentValue forType:currentKey];
}
} else {
if ([currentKey isEqualToString:[UTIHelper stringFromPboardType:
NSPasteboardTypeString]] ||
[currentKey
isEqualToString:[UTIHelper
stringFromPboardType:kPublicUrlPboardType]] ||
[currentKey
isEqualToString:
[UTIHelper stringFromPboardType:kPublicUrlNamePboardType]]) {
[cocoaPasteboard setString:currentValue forType:currentKey];
} else if ([currentKey
isEqualToString:
[UTIHelper
stringFromPboardType:kUrlsWithTitlesPboardType]]) {
[cocoaPasteboard
setPropertyList:[pasteboardOutputDict valueForKey:currentKey]
forType:currentKey];
} else if ([currentKey
isEqualToString:[UTIHelper stringFromPboardType:
NSPasteboardTypeHTML]]) {
[cocoaPasteboard
setString:(nsClipboard::WrapHtmlForSystemPasteboard(currentValue))
forType:currentKey];
} else if ([currentKey
isEqualToString:[UTIHelper stringFromPboardType:
kMozFileUrlsPboardType]]) {
[cocoaPasteboard writeObjects:currentValue];
} else if ([currentKey
isEqualToString:
[UTIHelper
stringFromPboardType:(NSString*)kUTTypeFileURL]]) {
[cocoaPasteboard setString:currentValue forType:currentKey];
} else if ([currentKey
isEqualToString:
[UTIHelper
stringFromPboardType:kPasteboardConcealedType]]) {
// It's fine to set the data to null for this field - this field is an
// addition to a value's other type and works like a flag.
[cocoaPasteboard setData:NULL forType:currentKey];
} else {
[cocoaPasteboard setData:currentValue forType:currentKey];
}
}
}
return NS_OK;
NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
}
nsresult nsClipboard::TransferableFromPasteboard(
nsITransferable* aTransferable, NSPasteboard* cocoaPasteboard) {
NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
// get flavor list that includes all acceptable flavors (including ones
// obtained through conversion)
nsTArray<nsCString> flavors;
nsresult rv = aTransferable->FlavorsTransferableCanImport(flavors);
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
for (uint32_t i = 0; i < flavors.Length(); i++) {
nsCString& flavorStr = flavors[i];
// printf("looking for clipboard data of type %s\n", flavorStr.get());
NSString* pboardType = nil;
if (nsClipboard::IsStringType(flavorStr, &pboardType)) {
NSString* pString = [cocoaPasteboard stringForType:pboardType];
if (!pString) {
continue;
}
NSData* stringData;
bool isRTF = [pboardType
isEqualToString:[UTIHelper stringFromPboardType:NSPasteboardTypeRTF]];
if (isRTF) {
stringData = [pString dataUsingEncoding:NSASCIIStringEncoding];
} else {
stringData = [pString dataUsingEncoding:NSUnicodeStringEncoding];
}
unsigned int dataLength = [stringData length];
void* clipboardDataPtr = malloc(dataLength);
if (!clipboardDataPtr) {
return NS_ERROR_OUT_OF_MEMORY;
}
[stringData getBytes:clipboardDataPtr length:dataLength];
// The DOM only wants LF, so convert from MacOS line endings to DOM line
// endings.
int32_t signedDataLength = dataLength;
nsLinebreakHelpers::ConvertPlatformToDOMLinebreaks(
isRTF, &clipboardDataPtr, &signedDataLength);
dataLength = signedDataLength;
// skip BOM (Byte Order Mark to distinguish little or big endian)
char16_t* clipboardDataPtrNoBOM = (char16_t*)clipboardDataPtr;
if ((dataLength > 2) && ((clipboardDataPtrNoBOM[0] == 0xFEFF) ||
(clipboardDataPtrNoBOM[0] == 0xFFFE))) {
dataLength -= sizeof(char16_t);
clipboardDataPtrNoBOM += 1;
}
nsCOMPtr<nsISupports> genericDataWrapper;
nsPrimitiveHelpers::CreatePrimitiveForData(
flavorStr, clipboardDataPtrNoBOM, dataLength,
getter_AddRefs(genericDataWrapper));
aTransferable->SetTransferData(flavorStr.get(), genericDataWrapper);
free(clipboardDataPtr);
break;
} else if (flavorStr.EqualsLiteral(kFileMime)) {
NSArray* items = [cocoaPasteboard pasteboardItems];
if (!items || [items count] <= 0) {
continue;
}
// XXX we don't support multiple clipboard item on DOM and XPCOM interface
// for now, so we only get the data from the first pasteboard item.
NSPasteboardItem* item = [items objectAtIndex:0];
if (!item) {
continue;
}
nsCocoaUtils::SetTransferDataForTypeFromPasteboardItem(aTransferable,
flavorStr, item);
} else if (flavorStr.EqualsLiteral(kCustomTypesMime)) {
NSString* type = [cocoaPasteboard
availableTypeFromArray:
[NSArray
arrayWithObject:[UTIHelper stringFromPboardType:
kMozCustomTypesPboardType]]];
if (!type) {
continue;
}
NSData* pasteboardData = GetDataFromPasteboard(cocoaPasteboard, type);
if (!pasteboardData) {
continue;
}
unsigned int dataLength = [pasteboardData length];
void* clipboardDataPtr = malloc(dataLength);
if (!clipboardDataPtr) {
return NS_ERROR_OUT_OF_MEMORY;
}
[pasteboardData getBytes:clipboardDataPtr length:dataLength];
nsCOMPtr<nsISupports> genericDataWrapper;
nsPrimitiveHelpers::CreatePrimitiveForData(
flavorStr, clipboardDataPtr, dataLength,
getter_AddRefs(genericDataWrapper));
aTransferable->SetTransferData(flavorStr.get(), genericDataWrapper);
free(clipboardDataPtr);
} else if (flavorStr.EqualsLiteral(kJPEGImageMime) ||
flavorStr.EqualsLiteral(kJPGImageMime) ||
flavorStr.EqualsLiteral(kPNGImageMime) ||
flavorStr.EqualsLiteral(kGIFImageMime)) {
// Figure out if there's data on the pasteboard we can grab (sanity check)
NSString* type = [cocoaPasteboard
availableTypeFromArray:
[NSArray
arrayWithObjects:
[UTIHelper
stringFromPboardType:(NSString*)kUTTypeFileURL],
[UTIHelper stringFromPboardType:NSPasteboardTypeTIFF],
[UTIHelper stringFromPboardType:NSPasteboardTypePNG],
nil]];
if (!type) continue;
// Read data off the clipboard
NSData* pasteboardData = GetDataFromPasteboard(cocoaPasteboard, type);
if (!pasteboardData) continue;
// Figure out what type we're converting to
CFStringRef outputType = NULL;
if (flavorStr.EqualsLiteral(kJPEGImageMime) ||
flavorStr.EqualsLiteral(kJPGImageMime))
outputType = CFSTR("public.jpeg");
else if (flavorStr.EqualsLiteral(kPNGImageMime))
outputType = CFSTR("public.png");
else if (flavorStr.EqualsLiteral(kGIFImageMime))
outputType = CFSTR("com.compuserve.gif");
else
continue;
// Use ImageIO to interpret the data on the clipboard and transcode.
// Note that ImageIO, like all CF APIs, allows NULLs to propagate freely
// and safely in most cases (like ObjC). A notable exception is CFRelease.
NSDictionary* options = [NSDictionary
dictionaryWithObjectsAndKeys:(NSNumber*)kCFBooleanTrue,
kCGImageSourceShouldAllowFloat, type,
kCGImageSourceTypeIdentifierHint, nil];
CGImageSourceRef source = nullptr;
if (type == [UTIHelper stringFromPboardType:(NSString*)kUTTypeFileURL]) {
NSString* urlStr = [cocoaPasteboard stringForType:type];
NSURL* url = [NSURL URLWithString:urlStr];
source =
CGImageSourceCreateWithURL((CFURLRef)url, (CFDictionaryRef)options);
} else {
source = CGImageSourceCreateWithData((CFDataRef)pasteboardData,
(CFDictionaryRef)options);
}
NSMutableData* encodedData = [NSMutableData data];
CGImageDestinationRef dest = CGImageDestinationCreateWithData(
(CFMutableDataRef)encodedData, outputType, 1, NULL);
CGImageDestinationAddImageFromSource(dest, source, 0, NULL);
bool successfullyConverted = CGImageDestinationFinalize(dest);
if (successfullyConverted) {
// Put the converted data in a form Gecko can understand
nsCOMPtr<nsIInputStream> byteStream;
NS_NewByteInputStream(getter_AddRefs(byteStream),
mozilla::Span((const char*)[encodedData bytes],
[encodedData length]),
NS_ASSIGNMENT_COPY);
aTransferable->SetTransferData(flavorStr.get(), byteStream);
}
if (dest) CFRelease(dest);
if (source) CFRelease(source);
if (successfullyConverted) {
// XXX Maybe try to fill in more types? Is there a point?
break;
} else {
continue;
}
}
}
return NS_OK;
NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
}
NS_IMETHODIMP
nsClipboard::GetNativeClipboardData(nsITransferable* aTransferable,
int32_t aWhichClipboard) {
NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
MOZ_DIAGNOSTIC_ASSERT(aTransferable);
MOZ_DIAGNOSTIC_ASSERT(
nsIClipboard::IsClipboardTypeSupported(aWhichClipboard));
if (kSelectionCache == aWhichClipboard) {
if (!sSelectionCache) {
return NS_OK;
}
// get flavor list that includes all acceptable flavors (including ones
// obtained through conversion)
nsTArray<nsCString> flavors;
nsresult rv = aTransferable->FlavorsTransferableCanImport(flavors);
if (NS_FAILED(rv)) {
return NS_ERROR_FAILURE;
}
for (const auto& flavor : flavors) {
nsCOMPtr<nsISupports> dataSupports;
rv = sSelectionCache->GetTransferData(flavor.get(),
getter_AddRefs(dataSupports));
if (NS_SUCCEEDED(rv)) {
MOZ_CLIPBOARD_LOG("%s: getting %s from cache.", __FUNCTION__,
flavor.get());
aTransferable->SetTransferData(flavor.get(), dataSupports);
// XXX Maybe try to fill in more types? Is there a point?
break;
}
}
return NS_OK;
}
NSPasteboard* cocoaPasteboard = GetPasteboard(aWhichClipboard);
if (!cocoaPasteboard) {
return NS_ERROR_FAILURE;
}
return TransferableFromPasteboard(aTransferable, cocoaPasteboard);
NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
}
// returns true if we have *any* of the passed in flavors available for pasting
mozilla::Result<bool, nsresult>
nsClipboard::HasNativeClipboardDataMatchingFlavors(
const nsTArray<nsCString>& aFlavorList, int32_t aWhichClipboard) {
NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
MOZ_DIAGNOSTIC_ASSERT(
nsIClipboard::IsClipboardTypeSupported(aWhichClipboard));
if (kSelectionCache == aWhichClipboard) {
nsTArray<nsCString> transferableFlavors;
if (sSelectionCache &&
NS_SUCCEEDED(sSelectionCache->FlavorsTransferableCanExport(
transferableFlavors))) {
if (MOZ_CLIPBOARD_LOG_ENABLED()) {
MOZ_CLIPBOARD_LOG(" SelectionCache types (nums %zu)\n",
transferableFlavors.Length());
for (const auto& transferableFlavor : transferableFlavors) {
MOZ_CLIPBOARD_LOG(" MIME %s", transferableFlavor.get());
}
}
for (const auto& transferableFlavor : transferableFlavors) {
for (const auto& flavor : aFlavorList) {
if (transferableFlavor.Equals(flavor)) {
MOZ_CLIPBOARD_LOG(" has %s", flavor.get());
return true;
}
}
}
}
if (MOZ_CLIPBOARD_LOG_ENABLED()) {
MOZ_CLIPBOARD_LOG(" no targets at clipboard (bad match)\n");
}
return false;
}
NSPasteboard* cocoaPasteboard = GetPasteboard(aWhichClipboard);
MOZ_ASSERT(cocoaPasteboard);
if (MOZ_CLIPBOARD_LOG_ENABLED()) {
NSArray* types = [cocoaPasteboard types];
uint32_t count = [types count];
MOZ_CLIPBOARD_LOG(" Pasteboard types (nums %d)\n", count);
for (uint32_t i = 0; i < count; i++) {
NSPasteboardType type = [types objectAtIndex:i];
if (!type) {
MOZ_CLIPBOARD_LOG(" failed to get MIME\n");
continue;
}
MOZ_CLIPBOARD_LOG(" MIME %s\n", [type UTF8String]);
}
}
for (auto& mimeType : aFlavorList) {
NSString* pboardType = nil;
if (nsClipboard::IsStringType(mimeType, &pboardType)) {
NSString* availableType = [cocoaPasteboard
availableTypeFromArray:[NSArray arrayWithObject:pboardType]];
if (availableType && [availableType isEqualToString:pboardType]) {
MOZ_CLIPBOARD_LOG(" has %s\n", mimeType.get());
return true;
}
} else if (mimeType.EqualsLiteral(kCustomTypesMime)) {
NSString* availableType = [cocoaPasteboard
availableTypeFromArray:
[NSArray
arrayWithObject:[UTIHelper stringFromPboardType:
kMozCustomTypesPboardType]]];
if (availableType) {
MOZ_CLIPBOARD_LOG(" has %s\n", mimeType.get());
return true;
}
} else if (mimeType.EqualsLiteral(kJPEGImageMime) ||
mimeType.EqualsLiteral(kJPGImageMime) ||
mimeType.EqualsLiteral(kPNGImageMime) ||
mimeType.EqualsLiteral(kGIFImageMime)) {
NSString* availableType = [cocoaPasteboard
availableTypeFromArray:
[NSArray
arrayWithObjects:
[UTIHelper stringFromPboardType:NSPasteboardTypeTIFF],
[UTIHelper stringFromPboardType:NSPasteboardTypePNG],
nil]];
if (availableType) {
MOZ_CLIPBOARD_LOG(" has %s\n", mimeType.get());
return true;
}
} else if (mimeType.EqualsLiteral(kFileMime)) {
NSArray* items = [cocoaPasteboard pasteboardItems];
if (items && [items count] > 0) {
// XXX we only check the first pasteboard item as we only get data from
// first item in TransferableFromPasteboard for now.
if (NSPasteboardItem* item = [items objectAtIndex:0]) {
if (NSString *availableType = [item
availableTypeFromArray:
[NSArray
arrayWithObjects:[UTIHelper
stringFromPboardType:
(NSString*)kUTTypeFileURL],
nil]]) {
MOZ_CLIPBOARD_LOG(" has %s\n", mimeType.get());
return true;
}
}
}
}
}
if (MOZ_CLIPBOARD_LOG_ENABLED()) {
MOZ_CLIPBOARD_LOG(" no targets at clipboard (bad match)\n");
}
return false;
NS_OBJC_END_TRY_BLOCK_RETURN(mozilla::Err(NS_ERROR_FAILURE));
}
// static
mozilla::Maybe<uint32_t> nsClipboard::FindIndexOfImageFlavor(
const nsTArray<nsCString>& aMIMETypes) {
for (uint32_t i = 0; i < aMIMETypes.Length(); ++i) {
if (nsClipboard::IsImageType(aMIMETypes[i])) {
return mozilla::Some(i);
}
}
return mozilla::Nothing();
}
// This function converts anything that other applications might understand into
// the system format and puts it into a dictionary which it returns. static
NSDictionary* nsClipboard::PasteboardDictFromTransferable(
nsITransferable* aTransferable) {
NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
if (!aTransferable) {
return nil;
}
NSMutableDictionary* pasteboardOutputDict = [NSMutableDictionary dictionary];
nsTArray<nsCString> flavors;
nsresult rv = aTransferable->FlavorsTransferableCanExport(flavors);
if (NS_FAILED(rv)) {
return nil;
}
const mozilla::Maybe<uint32_t> imageFlavorIndex =
nsClipboard::FindIndexOfImageFlavor(flavors);
if (imageFlavorIndex) {
// When right-clicking and "Copy Image" is clicked on macOS, some apps
// expect the first flavor to be the image flavor. See bug 1689992. For
// other apps, the order shouldn't matter.
std::swap(*flavors.begin(), flavors[*imageFlavorIndex]);
}
for (uint32_t i = 0; i < flavors.Length(); i++) {
nsCString& flavorStr = flavors[i];
MOZ_CLIPBOARD_LOG("writing out clipboard data of type %s (%d)\n",
flavorStr.get(), i);
NSString* pboardType = nil;
if (nsClipboard::IsStringType(flavorStr, &pboardType)) {
nsCOMPtr<nsISupports> genericDataWrapper;
rv = aTransferable->GetTransferData(flavorStr.get(),
getter_AddRefs(genericDataWrapper));
if (NS_FAILED(rv)) {
continue;
}
nsAutoString data;
if (nsCOMPtr<nsISupportsString> text =
do_QueryInterface(genericDataWrapper)) {
text->GetData(data);
}
NSString* nativeString;
if (!data.IsEmpty()) {
nativeString = [NSString stringWithCharacters:(const unichar*)data.get()
length:data.Length()];
} else {
nativeString = [NSString string];
}
// be nice to Carbon apps, normalize the receiver's contents using Form C.
nativeString = [nativeString precomposedStringWithCanonicalMapping];
if (nativeString) {
[pasteboardOutputDict setObject:nativeString forKey:pboardType];
}
if (aTransferable->GetIsPrivateData()) {
// In the case of password strings, we want to include the key for
// concealed type. These will be flagged as private data.
[pasteboardOutputDict
setObject:nativeString
forKey:[UTIHelper
stringFromPboardType:kPasteboardConcealedType]];
}
} else if (flavorStr.EqualsLiteral(kCustomTypesMime)) {
nsCOMPtr<nsISupports> genericDataWrapper;
rv = aTransferable->GetTransferData(flavorStr.get(),
getter_AddRefs(genericDataWrapper));
if (NS_FAILED(rv)) {
continue;
}
nsAutoCString data;
if (nsCOMPtr<nsISupportsCString> text =
do_QueryInterface(genericDataWrapper)) {
text->GetData(data);
}
if (!data.IsEmpty()) {
NSData* nativeData = [NSData dataWithBytes:data.get()
length:data.Length()];
NSString* customType =
[UTIHelper stringFromPboardType:kMozCustomTypesPboardType];
if (!nativeData) {
continue;
}
[pasteboardOutputDict setObject:nativeData forKey:customType];
}
} else if (nsClipboard::IsImageType(flavorStr)) {
nsCOMPtr<nsISupports> transferSupports;
rv = aTransferable->GetTransferData(flavorStr.get(),
getter_AddRefs(transferSupports));
if (NS_FAILED(rv)) {
continue;
}
nsCOMPtr<imgIContainer> image(do_QueryInterface(transferSupports));
if (!image) {
NS_WARNING("Image isn't an imgIContainer in transferable");
continue;
}
RefPtr<SourceSurface> surface = image->GetFrame(
imgIContainer::FRAME_CURRENT,
imgIContainer::FLAG_SYNC_DECODE | imgIContainer::FLAG_ASYNC_NOTIFY);
if (!surface) {
continue;
}
CGImageRef imageRef = NULL;
rv = nsCocoaUtils::CreateCGImageFromSurface(surface, &imageRef);
if (NS_FAILED(rv) || !imageRef) {
continue;
}
// Convert the CGImageRef to TIFF and PNG data.
CFMutableDataRef tiffData = CFDataCreateMutable(kCFAllocatorDefault, 0);
CFMutableDataRef pngData = CFDataCreateMutable(kCFAllocatorDefault, 0);
CGImageDestinationRef destRefTIFF = CGImageDestinationCreateWithData(
tiffData, CFSTR("public.tiff"), 1, NULL);
CGImageDestinationRef destRefPNG = CGImageDestinationCreateWithData(
pngData, CFSTR("public.png"), 1, NULL);
CGImageDestinationAddImage(destRefTIFF, imageRef, NULL);
CGImageDestinationAddImage(destRefPNG, imageRef, NULL);
const bool successfullyConvertedTIFF =
CGImageDestinationFinalize(destRefTIFF);
const bool successfullyConvertedPNG =
CGImageDestinationFinalize(destRefPNG);
CGImageRelease(imageRef);
if (destRefTIFF) {
CFRelease(destRefTIFF);
}
if (destRefPNG) {
CFRelease(destRefPNG);
}
if (successfullyConvertedTIFF && tiffData) {
NSString* tiffType =
[UTIHelper stringFromPboardType:NSPasteboardTypeTIFF];
[pasteboardOutputDict setObject:(NSMutableData*)tiffData
forKey:tiffType];
}
if (successfullyConvertedPNG && pngData) {
NSString* pngType =
[UTIHelper stringFromPboardType:NSPasteboardTypePNG];
[pasteboardOutputDict setObject:(NSMutableData*)pngData forKey:pngType];
}
if (tiffData) {
CFRelease(tiffData);
}
if (pngData) {
CFRelease(pngData);
}
} else if (flavorStr.EqualsLiteral(kFileMime)) {
nsCOMPtr<nsISupports> genericFile;
rv = aTransferable->GetTransferData(flavorStr.get(),
getter_AddRefs(genericFile));
if (NS_FAILED(rv)) {
continue;
}
nsCOMPtr<nsIFile> file(do_QueryInterface(genericFile));
if (!file) {
continue;
}
nsAutoString fileURI;
rv = file->GetPath(fileURI);
if (NS_FAILED(rv)) {
continue;
}
NSString* str = nsCocoaUtils::ToNSString(fileURI);
NSURL* url = [NSURL fileURLWithPath:str isDirectory:NO];
if (!url || ![url absoluteString]) {
continue;
}
NSString* fileUTType =
[UTIHelper stringFromPboardType:(NSString*)kUTTypeFileURL];
[pasteboardOutputDict setObject:[url absoluteString] forKey:fileUTType];
} else if (flavorStr.EqualsLiteral(kFilePromiseMime)) {
NSString* urlPromise = [UTIHelper
stringFromPboardType:(NSString*)kPasteboardTypeFileURLPromise];
NSString* urlPromiseContent = [UTIHelper
stringFromPboardType:(NSString*)kPasteboardTypeFilePromiseContent];
[pasteboardOutputDict setObject:[NSArray arrayWithObject:@""]
forKey:urlPromise];
[pasteboardOutputDict setObject:[NSArray arrayWithObject:@""]
forKey:urlPromiseContent];
} else if (flavorStr.EqualsLiteral(kURLMime)) {
nsCOMPtr<nsISupports> genericURL;
rv = aTransferable->GetTransferData(flavorStr.get(),
getter_AddRefs(genericURL));
nsCOMPtr<nsISupportsString> urlObject(do_QueryInterface(genericURL));
nsAutoString url;
urlObject->GetData(url);
NSString* nativeTitle = nil;
// A newline embedded in the URL means that the form is actually URL +
// title. This embedding occurs in nsDragService::GetData.
int32_t newlinePos = url.FindChar(char16_t('\n'));
if (newlinePos >= 0) {
url.Truncate(newlinePos);
nsAutoString urlTitle;
urlObject->GetData(urlTitle);
urlTitle.Mid(urlTitle, newlinePos + 1,
urlTitle.Length() - (newlinePos + 1));
nativeTitle =
[NSString stringWithCharacters:reinterpret_cast<const unichar*>(
urlTitle.get())
length:urlTitle.Length()];
}
// The Finder doesn't like getting random binary data aka
// Unicode, so change it into an escaped URL containing only
// ASCII.
nsAutoCString utf8Data = NS_ConvertUTF16toUTF8(url.get(), url.Length());
nsAutoCString escData;
NS_EscapeURL(utf8Data.get(), utf8Data.Length(),
esc_OnlyNonASCII | esc_AlwaysCopy, escData);
NSString* nativeURL = [NSString stringWithUTF8String:escData.get()];
NSString* publicUrl =
[UTIHelper stringFromPboardType:kPublicUrlPboardType];
if (!nativeURL) {
continue;
}
[pasteboardOutputDict setObject:nativeURL forKey:publicUrl];
if (nativeTitle) {
NSArray* urlsAndTitles = @[ @[ nativeURL ], @[ nativeTitle ] ];
NSString* urlName =
[UTIHelper stringFromPboardType:kPublicUrlNamePboardType];
NSString* urlsWithTitles =
[UTIHelper stringFromPboardType:kUrlsWithTitlesPboardType];
[pasteboardOutputDict setObject:nativeTitle forKey:urlName];
[pasteboardOutputDict setObject:urlsAndTitles forKey:urlsWithTitles];
}
}
// If it wasn't a type that we recognize as exportable we don't put it on
// the system clipboard. We'll just access it from our cached transferable
// when we need it.
}
return pasteboardOutputDict;
NS_OBJC_END_TRY_BLOCK_RETURN(nil);
}
bool nsClipboard::IsStringType(const nsCString& aMIMEType,
NSString** aPboardType) {
if (aMIMEType.EqualsLiteral(kTextMime)) {
*aPboardType = [UTIHelper stringFromPboardType:NSPasteboardTypeString];
return true;
} else if (aMIMEType.EqualsLiteral(kRTFMime)) {
*aPboardType = [UTIHelper stringFromPboardType:NSPasteboardTypeRTF];
return true;
} else if (aMIMEType.EqualsLiteral(kHTMLMime)) {
*aPboardType = [UTIHelper stringFromPboardType:NSPasteboardTypeHTML];
return true;
} else {
return false;
}
}
// static
bool nsClipboard::IsImageType(const nsACString& aMIMEType) {
return aMIMEType.EqualsLiteral(kPNGImageMime) ||
aMIMEType.EqualsLiteral(kJPEGImageMime) ||
aMIMEType.EqualsLiteral(kJPGImageMime) ||
aMIMEType.EqualsLiteral(kGIFImageMime) ||
aMIMEType.EqualsLiteral(kNativeImageMime);
}
NSString* nsClipboard::WrapHtmlForSystemPasteboard(NSString* aString) {
NSString* wrapped =
[NSString stringWithFormat:@"<html>"
"<head>"
"<meta http-equiv=\"content-type\" "
"content=\"text/html; charset=utf-8\">"
"</head>"
"<body>"
"%@"
"</body>"
"</html>",
aString];
return wrapped;
}
nsresult nsClipboard::EmptyNativeClipboardData(int32_t aWhichClipboard) {
NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
MOZ_DIAGNOSTIC_ASSERT(
nsIClipboard::IsClipboardTypeSupported(aWhichClipboard));
if (kSelectionCache == aWhichClipboard) {
ClearSelectionCache();
return NS_OK;
}
if (NSPasteboard* cocoaPasteboard = GetPasteboard(aWhichClipboard)) {
[cocoaPasteboard clearContents];
}
return NS_OK;
NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
}
mozilla::Result<int32_t, nsresult>
nsClipboard::GetNativeClipboardSequenceNumber(int32_t aWhichClipboard) {
NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
MOZ_DIAGNOSTIC_ASSERT(
nsIClipboard::IsClipboardTypeSupported(aWhichClipboard));
if (kSelectionCache == aWhichClipboard) {
return sSelectionCacheChangeCount;
}
NSPasteboard* cocoaPasteboard = GetPasteboard(aWhichClipboard);
if (!cocoaPasteboard) {
return mozilla::Err(NS_ERROR_FAILURE);
}
return [cocoaPasteboard changeCount];
NS_OBJC_END_TRY_BLOCK_RETURN(mozilla::Err(NS_ERROR_FAILURE));
}
|