2006-10-03 00:28:40 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2006-10-03 00:28:40 +04:00
|
|
|
|
2010-06-28 04:25:04 +04:00
|
|
|
#ifdef MOZ_LOGGING
|
|
|
|
#define FORCE_PR_LOG
|
|
|
|
#endif
|
|
|
|
#include "prlog.h"
|
|
|
|
|
2006-10-03 00:28:40 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsClipboard.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsPrimitiveHelpers.h"
|
|
|
|
#include "nsMemory.h"
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2007-09-17 22:02:50 +04:00
|
|
|
#include "nsStringStream.h"
|
2007-09-25 00:43:01 +04:00
|
|
|
#include "nsDragService.h"
|
|
|
|
#include "nsEscape.h"
|
2007-10-23 09:44:25 +04:00
|
|
|
#include "nsPrintfCString.h"
|
2008-02-18 20:30:59 +03:00
|
|
|
#include "nsObjCExceptions.h"
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-21 05:50:15 +04:00
|
|
|
#include "imgIContainer.h"
|
2010-03-13 17:39:55 +03:00
|
|
|
#include "nsCocoaUtils.h"
|
2007-09-17 22:02:50 +04:00
|
|
|
|
|
|
|
// Screenshots use the (undocumented) png pasteboard type.
|
|
|
|
#define IMAGE_PASTEBOARD_TYPES NSTIFFPboardType, @"Apple PNG pasteboard type", nil
|
2006-10-03 00:28:40 +04:00
|
|
|
|
2007-09-05 10:58:16 +04:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
extern PRLogModuleInfo* sCocoaLog;
|
|
|
|
#endif
|
|
|
|
|
2011-07-06 07:41:27 +04:00
|
|
|
extern void EnsureLogInitialized();
|
|
|
|
|
2006-10-03 00:28:40 +04:00
|
|
|
nsClipboard::nsClipboard() : nsBaseClipboard()
|
|
|
|
{
|
2007-07-03 11:16:19 +04:00
|
|
|
mChangeCount = 0;
|
2011-07-06 07:41:27 +04:00
|
|
|
|
|
|
|
EnsureLogInitialized();
|
2006-10-03 00:28:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsClipboard::~nsClipboard()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-10-23 09:44:25 +04:00
|
|
|
// 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) {
|
2012-04-24 22:43:00 +04:00
|
|
|
NS_WARNING(nsPrintfCString("Exception raised while getting data from the pasteboard: \"%s - %s\"",
|
2007-10-23 09:44:25 +04:00
|
|
|
[[e name] UTF8String], [[e reason] UTF8String]).get());
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2006-10-03 00:28:40 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsClipboard::SetNativeClipboardData(int32_t aWhichClipboard)
|
2006-10-03 00:28:40 +04:00
|
|
|
{
|
2008-02-18 20:30:59 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2006-10-03 00:28:40 +04:00
|
|
|
if ((aWhichClipboard != kGlobalClipboard) || !mTransferable)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2011-10-01 04:20:33 +04:00
|
|
|
mIgnoreEmptyNotification = true;
|
2006-10-03 00:28:40 +04:00
|
|
|
|
2007-06-27 09:26:18 +04:00
|
|
|
NSDictionary* pasteboardOutputDict = PasteboardDictFromTransferable(mTransferable);
|
|
|
|
if (!pasteboardOutputDict)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// write everything out to the general pasteboard
|
|
|
|
unsigned int outputCount = [pasteboardOutputDict count];
|
|
|
|
NSArray* outputKeys = [pasteboardOutputDict allKeys];
|
|
|
|
NSPasteboard* generalPBoard = [NSPasteboard generalPasteboard];
|
|
|
|
[generalPBoard declareTypes:outputKeys owner:nil];
|
|
|
|
for (unsigned int i = 0; i < outputCount; i++) {
|
|
|
|
NSString* currentKey = [outputKeys objectAtIndex:i];
|
|
|
|
id currentValue = [pasteboardOutputDict valueForKey:currentKey];
|
2007-10-19 03:37:39 +04:00
|
|
|
if (currentKey == NSStringPboardType ||
|
|
|
|
currentKey == kCorePboardType_url ||
|
|
|
|
currentKey == kCorePboardType_urld ||
|
2009-01-01 17:26:16 +03:00
|
|
|
currentKey == kCorePboardType_urln) {
|
2007-06-27 09:26:18 +04:00
|
|
|
[generalPBoard setString:currentValue forType:currentKey];
|
2009-01-01 17:26:16 +03:00
|
|
|
} else if (currentKey == NSHTMLPboardType) {
|
|
|
|
[generalPBoard setString:(nsClipboard::WrapHtmlForSystemPasteboard(currentValue))
|
|
|
|
forType:currentKey];
|
|
|
|
} else {
|
2007-06-27 09:26:18 +04:00
|
|
|
[generalPBoard setData:currentValue forType:currentKey];
|
2009-01-01 17:26:16 +03:00
|
|
|
}
|
2007-06-27 09:26:18 +04:00
|
|
|
}
|
|
|
|
|
2007-07-03 11:16:19 +04:00
|
|
|
mChangeCount = [generalPBoard changeCount];
|
|
|
|
|
2011-10-01 04:20:33 +04:00
|
|
|
mIgnoreEmptyNotification = false;
|
2007-06-27 09:26:18 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2008-02-18 20:30:59 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-06-27 09:26:18 +04:00
|
|
|
}
|
|
|
|
|
2010-01-11 04:45:45 +03:00
|
|
|
nsresult
|
|
|
|
nsClipboard::TransferableFromPasteboard(nsITransferable *aTransferable, NSPasteboard *cocoaPasteboard)
|
2007-07-03 11:16:19 +04:00
|
|
|
{
|
2008-02-18 20:30:59 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-07-03 11:16:19 +04:00
|
|
|
// get flavor list that includes all acceptable flavors (including ones obtained through conversion)
|
|
|
|
nsCOMPtr<nsISupportsArray> flavorList;
|
|
|
|
nsresult rv = aTransferable->FlavorsTransferableCanImport(getter_AddRefs(flavorList));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t flavorCount;
|
2007-07-03 11:16:19 +04:00
|
|
|
flavorList->Count(&flavorCount);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < flavorCount; i++) {
|
2007-07-03 11:16:19 +04:00
|
|
|
nsCOMPtr<nsISupports> genericFlavor;
|
|
|
|
flavorList->GetElementAt(i, getter_AddRefs(genericFlavor));
|
|
|
|
nsCOMPtr<nsISupportsCString> currentFlavor(do_QueryInterface(genericFlavor));
|
|
|
|
if (!currentFlavor)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nsXPIDLCString flavorStr;
|
|
|
|
currentFlavor->ToString(getter_Copies(flavorStr)); // i has a flavr
|
|
|
|
|
|
|
|
// printf("looking for clipboard data of type %s\n", flavorStr.get());
|
|
|
|
|
2010-11-18 00:42:05 +03:00
|
|
|
NSString *pboardType = nil;
|
2008-11-07 04:08:35 +03:00
|
|
|
if (nsClipboard::IsStringType(flavorStr, &pboardType)) {
|
|
|
|
NSString* pString = [cocoaPasteboard stringForType:pboardType];
|
2007-07-03 11:16:19 +04:00
|
|
|
if (!pString)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
NSData* stringData = [pString dataUsingEncoding:NSUnicodeStringEncoding];
|
|
|
|
unsigned int dataLength = [stringData length];
|
|
|
|
void* clipboardDataPtr = malloc(dataLength);
|
|
|
|
if (!clipboardDataPtr)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
[stringData getBytes:clipboardDataPtr];
|
|
|
|
|
|
|
|
// The DOM only wants LF, so convert from MacOS line endings to DOM line endings.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t signedDataLength = dataLength;
|
2007-07-03 11:16:19 +04:00
|
|
|
nsLinebreakHelpers::ConvertPlatformToDOMLinebreaks(flavorStr, &clipboardDataPtr, &signedDataLength);
|
|
|
|
dataLength = signedDataLength;
|
|
|
|
|
|
|
|
// skip BOM (Byte Order Mark to distinguish little or big endian)
|
|
|
|
PRUnichar* clipboardDataPtrNoBOM = (PRUnichar*)clipboardDataPtr;
|
|
|
|
if ((dataLength > 2) &&
|
|
|
|
((clipboardDataPtrNoBOM[0] == 0xFEFF) ||
|
|
|
|
(clipboardDataPtrNoBOM[0] == 0xFFFE))) {
|
|
|
|
dataLength -= sizeof(PRUnichar);
|
|
|
|
clipboardDataPtrNoBOM += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> genericDataWrapper;
|
|
|
|
nsPrimitiveHelpers::CreatePrimitiveForData(flavorStr, clipboardDataPtrNoBOM, dataLength,
|
|
|
|
getter_AddRefs(genericDataWrapper));
|
|
|
|
aTransferable->SetTransferData(flavorStr, genericDataWrapper, dataLength);
|
|
|
|
free(clipboardDataPtr);
|
|
|
|
break;
|
|
|
|
}
|
2007-09-17 22:02:50 +04:00
|
|
|
else if (flavorStr.EqualsLiteral(kJPEGImageMime) ||
|
2012-06-11 03:44:50 +04:00
|
|
|
flavorStr.EqualsLiteral(kJPGImageMime) ||
|
2007-09-17 22:02:50 +04:00
|
|
|
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:IMAGE_PASTEBOARD_TYPES]];
|
|
|
|
if (!type)
|
|
|
|
continue;
|
2007-07-03 11:16:19 +04:00
|
|
|
|
2007-10-23 09:44:25 +04:00
|
|
|
// Read data off the clipboard
|
|
|
|
NSData *pasteboardData = GetDataFromPasteboard(cocoaPasteboard, type);
|
2007-10-12 23:36:19 +04:00
|
|
|
if (!pasteboardData)
|
|
|
|
continue;
|
|
|
|
|
2007-09-17 22:02:50 +04:00
|
|
|
// Figure out what type we're converting to
|
|
|
|
CFStringRef outputType = NULL;
|
2012-06-11 03:44:50 +04:00
|
|
|
if (flavorStr.EqualsLiteral(kJPEGImageMime) ||
|
|
|
|
flavorStr.EqualsLiteral(kJPGImageMime))
|
2007-09-17 22:02:50 +04:00
|
|
|
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 == NSTIFFPboardType ? @"public.tiff" : @"public.png"),
|
|
|
|
kCGImageSourceTypeIdentifierHint, nil];
|
|
|
|
|
|
|
|
CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)pasteboardData,
|
|
|
|
(CFDictionaryRef)options);
|
|
|
|
NSMutableData *encodedData = [NSMutableData data];
|
|
|
|
CGImageDestinationRef dest = CGImageDestinationCreateWithData((CFMutableDataRef)encodedData,
|
|
|
|
outputType,
|
|
|
|
1, NULL);
|
|
|
|
CGImageDestinationAddImageFromSource(dest, source, 0, NULL);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool successfullyConverted = CGImageDestinationFinalize(dest);
|
2007-09-17 22:02:50 +04:00
|
|
|
|
|
|
|
if (successfullyConverted) {
|
|
|
|
// Put the converted data in a form Gecko can understand
|
|
|
|
nsCOMPtr<nsIInputStream> byteStream;
|
|
|
|
NS_NewByteInputStream(getter_AddRefs(byteStream), (const char*)[encodedData bytes],
|
|
|
|
[encodedData length], NS_ASSIGNMENT_COPY);
|
2007-10-12 23:36:19 +04:00
|
|
|
|
|
|
|
aTransferable->SetTransferData(flavorStr, byteStream, sizeof(nsIInputStream*));
|
2007-09-17 22:02:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dest)
|
|
|
|
CFRelease(dest);
|
|
|
|
if (source)
|
|
|
|
CFRelease(source);
|
|
|
|
|
|
|
|
if (successfullyConverted)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
continue;
|
2007-07-03 11:16:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2008-02-18 20:30:59 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-06-27 09:26:18 +04:00
|
|
|
}
|
|
|
|
|
2010-01-11 04:45:45 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsClipboard::GetNativeClipboardData(nsITransferable* aTransferable, int32_t aWhichClipboard)
|
2010-01-11 04:45:45 +03:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
|
|
|
if ((aWhichClipboard != kGlobalClipboard) || !aTransferable)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
NSPasteboard* cocoaPasteboard = [NSPasteboard generalPasteboard];
|
|
|
|
if (!cocoaPasteboard)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// get flavor list that includes all acceptable flavors (including ones obtained through conversion)
|
|
|
|
nsCOMPtr<nsISupportsArray> flavorList;
|
|
|
|
nsresult rv = aTransferable->FlavorsTransferableCanImport(getter_AddRefs(flavorList));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t flavorCount;
|
2010-01-11 04:45:45 +03:00
|
|
|
flavorList->Count(&flavorCount);
|
|
|
|
|
|
|
|
// If we were the last ones to put something on the pasteboard, then just use the cached
|
|
|
|
// transferable. Otherwise clear it because it isn't relevant any more.
|
|
|
|
if (mChangeCount == [cocoaPasteboard changeCount]) {
|
|
|
|
if (mTransferable) {
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < flavorCount; i++) {
|
2010-01-11 04:45:45 +03:00
|
|
|
nsCOMPtr<nsISupports> genericFlavor;
|
|
|
|
flavorList->GetElementAt(i, getter_AddRefs(genericFlavor));
|
|
|
|
nsCOMPtr<nsISupportsCString> currentFlavor(do_QueryInterface(genericFlavor));
|
|
|
|
if (!currentFlavor)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nsXPIDLCString flavorStr;
|
|
|
|
currentFlavor->ToString(getter_Copies(flavorStr));
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> dataSupports;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t dataSize = 0;
|
2010-01-11 04:45:45 +03:00
|
|
|
rv = mTransferable->GetTransferData(flavorStr, getter_AddRefs(dataSupports), &dataSize);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
aTransferable->SetTransferData(flavorStr, dataSupports, dataSize);
|
|
|
|
return NS_OK; // maybe try to fill in more types? Is there a point?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nsBaseClipboard::EmptyClipboard(kGlobalClipboard);
|
|
|
|
}
|
|
|
|
|
|
|
|
// at this point we can't satisfy the request from cache data so let's look
|
|
|
|
// for things other people put on the system clipboard
|
|
|
|
|
|
|
|
return nsClipboard::TransferableFromPasteboard(aTransferable, cocoaPasteboard);
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
}
|
|
|
|
|
2007-07-04 03:13:28 +04:00
|
|
|
// returns true if we have *any* of the passed in flavors available for pasting
|
2007-06-27 09:26:18 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsClipboard::HasDataMatchingFlavors(const char** aFlavorList, uint32_t aLength,
|
|
|
|
int32_t aWhichClipboard, bool* outResult)
|
2007-06-27 09:26:18 +04:00
|
|
|
{
|
2008-02-18 20:30:59 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2011-10-01 04:20:33 +04:00
|
|
|
*outResult = false;
|
2007-06-27 09:26:18 +04:00
|
|
|
|
|
|
|
if ((aWhichClipboard != kGlobalClipboard) || !aFlavorList)
|
|
|
|
return NS_OK;
|
|
|
|
|
2007-07-04 03:13:28 +04:00
|
|
|
// first see if we have data for this in our cached transferable
|
|
|
|
if (mTransferable) {
|
|
|
|
nsCOMPtr<nsISupportsArray> transferableFlavorList;
|
|
|
|
nsresult rv = mTransferable->FlavorsTransferableCanImport(getter_AddRefs(transferableFlavorList));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t transferableFlavorCount;
|
2007-07-04 03:13:28 +04:00
|
|
|
transferableFlavorList->Count(&transferableFlavorCount);
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t j = 0; j < transferableFlavorCount; j++) {
|
2007-07-04 03:13:28 +04:00
|
|
|
nsCOMPtr<nsISupports> transferableFlavorSupports;
|
|
|
|
transferableFlavorList->GetElementAt(j, getter_AddRefs(transferableFlavorSupports));
|
|
|
|
nsCOMPtr<nsISupportsCString> currentTransferableFlavor(do_QueryInterface(transferableFlavorSupports));
|
|
|
|
if (!currentTransferableFlavor)
|
|
|
|
continue;
|
|
|
|
nsXPIDLCString transferableFlavorStr;
|
|
|
|
currentTransferableFlavor->ToString(getter_Copies(transferableFlavorStr));
|
2007-11-20 22:06:30 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t k = 0; k < aLength; k++) {
|
2007-11-20 22:06:30 +03:00
|
|
|
if (transferableFlavorStr.Equals(aFlavorList[k])) {
|
2011-10-01 04:20:33 +04:00
|
|
|
*outResult = true;
|
2007-07-04 03:13:28 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-27 09:26:18 +04:00
|
|
|
NSPasteboard* generalPBoard = [NSPasteboard generalPasteboard];
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < aLength; i++) {
|
2008-11-07 04:08:35 +03:00
|
|
|
nsDependentCString mimeType(aFlavorList[i]);
|
2010-11-18 00:42:05 +03:00
|
|
|
NSString *pboardType = nil;
|
2008-11-07 04:08:35 +03:00
|
|
|
|
|
|
|
if (nsClipboard::IsStringType(mimeType, &pboardType)) {
|
|
|
|
NSString* availableType = [generalPBoard availableTypeFromArray:[NSArray arrayWithObject:pboardType]];
|
|
|
|
if (availableType && [availableType isEqualToString:pboardType]) {
|
2011-10-01 04:20:33 +04:00
|
|
|
*outResult = true;
|
2007-11-20 22:06:30 +03:00
|
|
|
break;
|
2007-06-27 09:26:18 +04:00
|
|
|
}
|
2007-11-20 22:06:30 +03:00
|
|
|
} else if (!strcmp(aFlavorList[i], kJPEGImageMime) ||
|
2012-06-11 03:44:50 +04:00
|
|
|
!strcmp(aFlavorList[i], kJPGImageMime) ||
|
2007-11-20 22:06:30 +03:00
|
|
|
!strcmp(aFlavorList[i], kPNGImageMime) ||
|
|
|
|
!strcmp(aFlavorList[i], kGIFImageMime)) {
|
|
|
|
NSString* availableType = [generalPBoard availableTypeFromArray:
|
|
|
|
[NSArray arrayWithObjects:IMAGE_PASTEBOARD_TYPES]];
|
|
|
|
if (availableType) {
|
2011-10-01 04:20:33 +04:00
|
|
|
*outResult = true;
|
2007-11-20 22:06:30 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-06-27 09:26:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2008-02-18 20:30:59 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-06-27 09:26:18 +04:00
|
|
|
}
|
|
|
|
|
2007-07-03 11:16:19 +04:00
|
|
|
// This function converts anything that other applications might understand into the system format
|
|
|
|
// and puts it into a dictionary which it returns.
|
2007-06-27 09:26:18 +04:00
|
|
|
// static
|
|
|
|
NSDictionary*
|
|
|
|
nsClipboard::PasteboardDictFromTransferable(nsITransferable* aTransferable)
|
|
|
|
{
|
2008-02-18 20:30:59 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
|
|
|
|
|
2007-06-27 09:26:18 +04:00
|
|
|
if (!aTransferable)
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
NSMutableDictionary* pasteboardOutputDict = [NSMutableDictionary dictionary];
|
2006-10-03 00:28:40 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsISupportsArray> flavorList;
|
2007-06-27 09:26:18 +04:00
|
|
|
nsresult rv = aTransferable->FlavorsTransferableCanExport(getter_AddRefs(flavorList));
|
2006-10-03 00:28:40 +04:00
|
|
|
if (NS_FAILED(rv))
|
2007-06-27 09:26:18 +04:00
|
|
|
return nil;
|
2006-10-03 00:28:40 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t flavorCount;
|
2006-10-03 00:28:40 +04:00
|
|
|
flavorList->Count(&flavorCount);
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < flavorCount; i++) {
|
2006-10-03 00:28:40 +04:00
|
|
|
nsCOMPtr<nsISupports> genericFlavor;
|
|
|
|
flavorList->GetElementAt(i, getter_AddRefs(genericFlavor));
|
|
|
|
nsCOMPtr<nsISupportsCString> currentFlavor(do_QueryInterface(genericFlavor));
|
|
|
|
if (!currentFlavor)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nsXPIDLCString flavorStr;
|
|
|
|
currentFlavor->ToString(getter_Copies(flavorStr));
|
|
|
|
|
2007-07-17 06:24:05 +04:00
|
|
|
PR_LOG(sCocoaLog, PR_LOG_ALWAYS, ("writing out clipboard data of type %s (%d)\n", flavorStr.get(), i));
|
2006-10-03 00:28:40 +04:00
|
|
|
|
2010-11-18 00:42:05 +03:00
|
|
|
NSString *pboardType = nil;
|
2008-11-07 04:08:35 +03:00
|
|
|
|
|
|
|
if (nsClipboard::IsStringType(flavorStr, &pboardType)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
void* data = nullptr;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t dataSize = 0;
|
2007-07-03 11:16:19 +04:00
|
|
|
nsCOMPtr<nsISupports> genericDataWrapper;
|
|
|
|
rv = aTransferable->GetTransferData(flavorStr, getter_AddRefs(genericDataWrapper), &dataSize);
|
|
|
|
nsPrimitiveHelpers::CreateDataFromPrimitive(flavorStr, genericDataWrapper, &data, dataSize);
|
2011-08-18 17:22:52 +04:00
|
|
|
|
|
|
|
NSString* nativeString;
|
|
|
|
if (data)
|
|
|
|
nativeString = [NSString stringWithCharacters:(const unichar*)data length:(dataSize / sizeof(PRUnichar))];
|
|
|
|
else
|
|
|
|
nativeString = [NSString string];
|
2007-07-03 11:16:19 +04:00
|
|
|
|
|
|
|
// be nice to Carbon apps, normalize the receiver's contents using Form C.
|
|
|
|
nativeString = [nativeString precomposedStringWithCanonicalMapping];
|
2008-11-07 04:08:35 +03:00
|
|
|
|
|
|
|
[pasteboardOutputDict setObject:nativeString forKey:pboardType];
|
2007-07-03 11:16:19 +04:00
|
|
|
|
|
|
|
nsMemory::Free(data);
|
|
|
|
}
|
|
|
|
else if (flavorStr.EqualsLiteral(kPNGImageMime) || flavorStr.EqualsLiteral(kJPEGImageMime) ||
|
2012-06-11 03:44:50 +04:00
|
|
|
flavorStr.EqualsLiteral(kJPGImageMime) || flavorStr.EqualsLiteral(kGIFImageMime) ||
|
|
|
|
flavorStr.EqualsLiteral(kNativeImageMime)) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t dataSize = 0;
|
2006-10-03 00:28:40 +04:00
|
|
|
nsCOMPtr<nsISupports> transferSupports;
|
2007-06-27 09:26:18 +04:00
|
|
|
aTransferable->GetTransferData(flavorStr, getter_AddRefs(transferSupports), &dataSize);
|
2006-10-03 00:28:40 +04:00
|
|
|
nsCOMPtr<nsISupportsInterfacePointer> ptrPrimitive(do_QueryInterface(transferSupports));
|
|
|
|
if (!ptrPrimitive)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> primitiveData;
|
|
|
|
ptrPrimitive->GetData(getter_AddRefs(primitiveData));
|
2006-12-19 23:47:53 +03:00
|
|
|
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-21 05:50:15 +04:00
|
|
|
nsCOMPtr<imgIContainer> image(do_QueryInterface(primitiveData));
|
2006-10-03 00:28:40 +04:00
|
|
|
if (!image) {
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-21 05:50:15 +04:00
|
|
|
NS_WARNING("Image isn't an imgIContainer in transferable");
|
2006-10-03 00:28:40 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-12-13 12:34:24 +04:00
|
|
|
nsRefPtr<gfxASurface> surface =
|
|
|
|
image->GetFrame(imgIContainer::FRAME_CURRENT,
|
|
|
|
imgIContainer::FLAG_SYNC_DECODE);
|
2013-03-06 03:39:48 +04:00
|
|
|
if (!surface) {
|
2010-03-25 19:07:36 +03:00
|
|
|
continue;
|
2013-03-06 03:39:48 +04:00
|
|
|
}
|
|
|
|
nsRefPtr<gfxImageSurface> frame(surface->GetAsReadableARGB32ImageSurface());
|
|
|
|
if (!frame) {
|
|
|
|
continue;
|
|
|
|
}
|
2010-03-13 17:39:55 +03:00
|
|
|
CGImageRef imageRef = NULL;
|
2010-03-25 19:07:36 +03:00
|
|
|
nsresult rv = nsCocoaUtils::CreateCGImageFromSurface(frame, &imageRef);
|
2010-03-13 17:39:55 +03:00
|
|
|
if (NS_FAILED(rv) || !imageRef) {
|
2006-10-03 00:28:40 +04:00
|
|
|
continue;
|
2010-03-13 17:39:55 +03:00
|
|
|
}
|
|
|
|
|
2008-02-06 09:22:51 +03:00
|
|
|
// Convert the CGImageRef to TIFF data.
|
|
|
|
CFMutableDataRef tiffData = CFDataCreateMutable(kCFAllocatorDefault, 0);
|
|
|
|
CGImageDestinationRef destRef = CGImageDestinationCreateWithData(tiffData,
|
|
|
|
CFSTR("public.tiff"),
|
|
|
|
1,
|
|
|
|
NULL);
|
|
|
|
CGImageDestinationAddImage(destRef, imageRef, NULL);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool successfullyConverted = CGImageDestinationFinalize(destRef);
|
2008-02-06 09:22:51 +03:00
|
|
|
|
2007-08-30 06:10:32 +04:00
|
|
|
CGImageRelease(imageRef);
|
2008-02-06 09:22:51 +03:00
|
|
|
if (destRef)
|
|
|
|
CFRelease(destRef);
|
|
|
|
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-21 05:50:15 +04:00
|
|
|
if (!successfullyConverted) {
|
2008-02-06 09:22:51 +03:00
|
|
|
if (tiffData)
|
|
|
|
CFRelease(tiffData);
|
|
|
|
continue;
|
|
|
|
}
|
2006-10-03 00:28:40 +04:00
|
|
|
|
2008-02-06 09:22:51 +03:00
|
|
|
[pasteboardOutputDict setObject:(NSMutableData*)tiffData forKey:NSTIFFPboardType];
|
|
|
|
if (tiffData)
|
|
|
|
CFRelease(tiffData);
|
2006-10-03 00:28:40 +04:00
|
|
|
}
|
2013-08-02 05:21:31 +04:00
|
|
|
else if (flavorStr.EqualsLiteral(kFileMime)) {
|
|
|
|
uint32_t len = 0;
|
|
|
|
nsCOMPtr<nsISupports> genericFile;
|
|
|
|
rv = aTransferable->GetTransferData(flavorStr, getter_AddRefs(genericFile), &len);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file(do_QueryInterface(genericFile));
|
|
|
|
if (!file) {
|
|
|
|
nsCOMPtr<nsISupportsInterfacePointer> ptr(do_QueryInterface(genericFile));
|
|
|
|
|
|
|
|
if (ptr) {
|
|
|
|
ptr->GetData(getter_AddRefs(genericFile));
|
|
|
|
file = do_QueryInterface(genericFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString fileURI;
|
|
|
|
rv = file->GetPath(fileURI);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString* str = nsCocoaUtils::ToNSString(fileURI);
|
|
|
|
NSArray* fileList = [NSArray arrayWithObjects:str, nil];
|
|
|
|
[pasteboardOutputDict setObject:fileList forKey:NSFilenamesPboardType];
|
|
|
|
}
|
2007-07-19 22:09:43 +04:00
|
|
|
else if (flavorStr.EqualsLiteral(kFilePromiseMime)) {
|
|
|
|
[pasteboardOutputDict setObject:[NSArray arrayWithObject:@""] forKey:NSFilesPromisePboardType];
|
|
|
|
}
|
2007-09-25 00:43:01 +04:00
|
|
|
else if (flavorStr.EqualsLiteral(kURLMime)) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t len = 0;
|
2007-09-25 00:43:01 +04:00
|
|
|
nsCOMPtr<nsISupports> genericURL;
|
|
|
|
rv = aTransferable->GetTransferData(flavorStr, getter_AddRefs(genericURL), &len);
|
|
|
|
nsCOMPtr<nsISupportsString> urlObject(do_QueryInterface(genericURL));
|
|
|
|
|
|
|
|
nsAutoString url;
|
|
|
|
urlObject->GetData(url);
|
|
|
|
|
|
|
|
// A newline embedded in the URL means that the form is actually URL + title.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t newlinePos = url.FindChar(PRUnichar('\n'));
|
2007-09-25 00:43:01 +04:00
|
|
|
if (newlinePos >= 0) {
|
|
|
|
url.Truncate(newlinePos);
|
|
|
|
|
|
|
|
nsAutoString urlTitle;
|
|
|
|
urlObject->GetData(urlTitle);
|
|
|
|
urlTitle.Mid(urlTitle, newlinePos + 1, len - (newlinePos + 1));
|
|
|
|
|
2013-10-10 20:59:40 +04:00
|
|
|
NSString *nativeTitle = [[NSString alloc] initWithCharacters:reinterpret_cast<const unichar*>(urlTitle.get())
|
|
|
|
length:urlTitle.Length()];
|
2007-09-25 00:43:01 +04:00
|
|
|
// be nice to Carbon apps, normalize the receiver's contents using Form C.
|
2007-09-25 02:41:10 +04:00
|
|
|
[pasteboardOutputDict setObject:[nativeTitle precomposedStringWithCanonicalMapping] forKey:kCorePboardType_urln];
|
2007-09-28 05:50:09 +04:00
|
|
|
// Also put the title out as 'urld', since some recipients will look for that.
|
|
|
|
[pasteboardOutputDict setObject:[nativeTitle precomposedStringWithCanonicalMapping] forKey:kCorePboardType_urld];
|
2007-09-25 00:43:01 +04:00
|
|
|
[nativeTitle release];
|
|
|
|
}
|
|
|
|
|
|
|
|
// The Finder doesn't like getting random binary data aka
|
|
|
|
// Unicode, so change it into an escaped URL containing only
|
|
|
|
// ASCII.
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString utf8Data = NS_ConvertUTF16toUTF8(url.get(), url.Length());
|
|
|
|
nsAutoCString escData;
|
2007-09-25 00:43:01 +04:00
|
|
|
NS_EscapeURL(utf8Data.get(), utf8Data.Length(), esc_OnlyNonASCII|esc_AlwaysCopy, escData);
|
2006-10-03 00:28:40 +04:00
|
|
|
|
2007-09-25 00:43:01 +04:00
|
|
|
// printf("Escaped url is %s, length %d\n", escData.get(), escData.Length());
|
|
|
|
|
|
|
|
NSString *nativeURL = [NSString stringWithUTF8String:escData.get()];
|
2007-09-25 02:41:10 +04:00
|
|
|
[pasteboardOutputDict setObject:nativeURL forKey:kCorePboardType_url];
|
2007-09-25 00:43:01 +04:00
|
|
|
}
|
2007-07-03 11:16:19 +04:00
|
|
|
// 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.
|
2006-10-03 00:28:40 +04:00
|
|
|
}
|
|
|
|
|
2007-07-03 11:16:19 +04:00
|
|
|
return pasteboardOutputDict;
|
2008-02-18 20:30:59 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
2006-10-03 00:28:40 +04:00
|
|
|
}
|
2008-11-07 04:08:35 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsClipboard::IsStringType(const nsCString& aMIMEType, NSString** aPasteboardType)
|
2008-11-07 04:08:35 +03:00
|
|
|
{
|
|
|
|
if (aMIMEType.EqualsLiteral(kUnicodeMime) ||
|
|
|
|
aMIMEType.EqualsLiteral(kHTMLMime)) {
|
|
|
|
if (aMIMEType.EqualsLiteral(kUnicodeMime))
|
|
|
|
*aPasteboardType = NSStringPboardType;
|
|
|
|
else
|
|
|
|
*aPasteboardType = NSHTMLPboardType;
|
2011-10-01 04:20:33 +04:00
|
|
|
return true;
|
2008-11-07 04:08:35 +03:00
|
|
|
} else {
|
2011-10-01 04:20:33 +04:00
|
|
|
return false;
|
2008-11-07 04:08:35 +03:00
|
|
|
}
|
|
|
|
}
|
2009-01-01 17:26:16 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|