2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
Notes to self:
|
|
|
|
|
|
|
|
- at some point, strings will be accessible from JS, so we won't have to wrap
|
|
|
|
flavors in an nsISupportsCString. Until then, we're kinda stuck with
|
|
|
|
this crappy API of nsISupportsArrays.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "nsTransferable.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
2009-02-22 17:05:24 -08:00
|
|
|
#include "nsTArray.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIFormatConverter.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "nsPrimitiveHelpers.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsDirectoryService.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsNetUtil.h"
|
2015-07-06 19:17:00 -07:00
|
|
|
#include "nsIDOMNode.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIOutputStream.h"
|
|
|
|
#include "nsIInputStream.h"
|
2015-07-06 19:17:00 -07:00
|
|
|
#include "nsIWeakReferenceUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIFile.h"
|
2012-04-16 19:14:01 -07:00
|
|
|
#include "nsILoadContext.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsTransferable, nsITransferable)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-08 18:03:35 -07:00
|
|
|
size_t GetDataForFlavor (const nsTArray<DataStruct>& aArray,
|
2009-03-10 19:26:03 -07:00
|
|
|
const char* aDataFlavor)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-08 18:03:35 -07:00
|
|
|
for (size_t i = 0 ; i < aArray.Length () ; ++i) {
|
2009-03-10 19:26:03 -07:00
|
|
|
if (aArray[i].GetFlavor().Equals (aDataFlavor))
|
|
|
|
return i;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-03-10 19:26:03 -07:00
|
|
|
return aArray.NoIndex;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
DataStruct::~DataStruct()
|
|
|
|
{
|
2013-08-22 03:31:48 -07:00
|
|
|
if (mCacheFileName) free(mCacheFileName);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
void
|
2015-02-18 03:52:00 -08:00
|
|
|
DataStruct::SetData ( nsISupports* aData, uint32_t aDataLen, bool aIsPrivateData )
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Now, check to see if we consider the data to be "too large"
|
2015-02-18 03:52:00 -08:00
|
|
|
// as well as ensuring that private browsing mode is disabled
|
|
|
|
if (aDataLen > kLargeDatasetSize && !aIsPrivateData) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// if so, cache it to disk instead of memory
|
|
|
|
if ( NS_SUCCEEDED(WriteCache(aData, aDataLen)) )
|
|
|
|
return;
|
|
|
|
else
|
|
|
|
NS_WARNING("Oh no, couldn't write data to the cache file");
|
|
|
|
}
|
|
|
|
|
|
|
|
mData = aData;
|
|
|
|
mDataLen = aDataLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
DataStruct::GetData ( nsISupports** aData, uint32_t *aDataLen )
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// check here to see if the data is cached on disk
|
|
|
|
if ( !mData && mCacheFileName ) {
|
|
|
|
// if so, read it in and pass it back
|
|
|
|
// ReadCache creates memory and copies the data into it.
|
|
|
|
if ( NS_SUCCEEDED(ReadCache(aData, aDataLen)) )
|
|
|
|
return;
|
|
|
|
else {
|
|
|
|
// oh shit, something went horribly wrong here.
|
|
|
|
NS_WARNING("Oh no, couldn't read data in from the cache file");
|
2012-07-30 07:20:58 -07:00
|
|
|
*aData = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
*aDataLen = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aData = mData;
|
|
|
|
if ( mData )
|
|
|
|
NS_ADDREF(*aData);
|
|
|
|
*aDataLen = mDataLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2013-07-10 02:56:21 -07:00
|
|
|
already_AddRefed<nsIFile>
|
|
|
|
DataStruct::GetFileSpec(const char* aFileName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-07-10 02:56:21 -07:00
|
|
|
nsCOMPtr<nsIFile> cacheFile;
|
|
|
|
NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(cacheFile));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-10 02:56:21 -07:00
|
|
|
if (!cacheFile)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// if the param aFileName contains a name we should use that
|
|
|
|
// because the file probably already exists
|
|
|
|
// otherwise create a unique name
|
|
|
|
if (!aFileName) {
|
|
|
|
cacheFile->AppendNative(NS_LITERAL_CSTRING("clipboardcache"));
|
|
|
|
cacheFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
|
|
|
|
} else {
|
|
|
|
cacheFile->AppendNative(nsDependentCString(aFileName));
|
|
|
|
}
|
|
|
|
|
2013-07-10 02:56:21 -07:00
|
|
|
return cacheFile.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
DataStruct::WriteCache(nsISupports* aData, uint32_t aDataLen)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Get a new path and file to the temp directory
|
2013-07-10 02:56:21 -07:00
|
|
|
nsCOMPtr<nsIFile> cacheFile = GetFileSpec(mCacheFileName);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (cacheFile) {
|
|
|
|
// remember the file name
|
|
|
|
if (!mCacheFileName) {
|
|
|
|
nsXPIDLCString fName;
|
|
|
|
cacheFile->GetNativeLeafName(fName);
|
2013-08-22 03:31:48 -07:00
|
|
|
mCacheFileName = strdup(fName);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// write out the contents of the clipboard
|
|
|
|
// to the file
|
2012-08-22 08:56:38 -07:00
|
|
|
//uint32_t bytes;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIOutputStream> outStr;
|
|
|
|
|
|
|
|
NS_NewLocalFileOutputStream(getter_AddRefs(outStr),
|
|
|
|
cacheFile);
|
|
|
|
|
|
|
|
if (!outStr) return NS_ERROR_FAILURE;
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
void* buff = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsPrimitiveHelpers::CreateDataFromPrimitive ( mFlavor.get(), aData, &buff, aDataLen );
|
|
|
|
if ( buff ) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t ignored;
|
2007-07-08 00:08:04 -07:00
|
|
|
outStr->Write(reinterpret_cast<char*>(buff), aDataLen, &ignored);
|
2015-03-26 17:01:12 -07:00
|
|
|
free(buff);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
DataStruct::ReadCache(nsISupports** aData, uint32_t* aDataLen)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// if we don't have a cache filename we are out of luck
|
|
|
|
if (!mCacheFileName)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// get the path and file name
|
2013-07-10 02:56:21 -07:00
|
|
|
nsCOMPtr<nsIFile> cacheFile = GetFileSpec(mCacheFileName);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool exists;
|
2007-03-22 10:30:00 -07:00
|
|
|
if ( cacheFile && NS_SUCCEEDED(cacheFile->Exists(&exists)) && exists ) {
|
|
|
|
// get the size of the file
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t fileSize;
|
2012-11-01 17:57:25 -07:00
|
|
|
int64_t max32 = 0xFFFFFFFF;
|
2007-03-22 10:30:00 -07:00
|
|
|
cacheFile->GetFileSize(&fileSize);
|
2012-10-03 07:13:19 -07:00
|
|
|
if (fileSize > max32)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2012-10-12 10:29:11 -07:00
|
|
|
uint32_t size = uint32_t(fileSize);
|
2007-03-22 10:30:00 -07:00
|
|
|
// create new memory for the large clipboard data
|
2015-10-30 11:35:28 -07:00
|
|
|
auto data = MakeUnique<char[]>(size);
|
2007-03-22 10:30:00 -07:00
|
|
|
if ( !data )
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
// now read it all in
|
|
|
|
nsCOMPtr<nsIInputStream> inStr;
|
|
|
|
NS_NewLocalFileInputStream( getter_AddRefs(inStr),
|
|
|
|
cacheFile);
|
|
|
|
|
|
|
|
if (!cacheFile) return NS_ERROR_FAILURE;
|
|
|
|
|
2015-10-30 11:35:28 -07:00
|
|
|
nsresult rv = inStr->Read(data.get(), fileSize, aDataLen);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// make sure we got all the data ok
|
|
|
|
if (NS_SUCCEEDED(rv) && *aDataLen == size) {
|
2015-10-30 11:35:28 -07:00
|
|
|
nsPrimitiveHelpers::CreatePrimitiveForData(mFlavor.get(), data.get(),
|
|
|
|
fileSize, aData);
|
2007-03-22 10:30:00 -07:00
|
|
|
return *aData ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// zero the return params
|
2012-07-30 07:20:58 -07:00
|
|
|
*aData = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
*aDataLen = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Transferable constructor
|
|
|
|
//
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
nsTransferable::nsTransferable()
|
2012-04-16 19:14:01 -07:00
|
|
|
: mPrivateData(false)
|
|
|
|
#ifdef DEBUG
|
|
|
|
, mInitialized(false)
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Transferable destructor
|
|
|
|
//
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
nsTransferable::~nsTransferable()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-16 19:14:01 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTransferable::Init(nsILoadContext* aContext)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mInitialized);
|
|
|
|
|
|
|
|
if (aContext) {
|
|
|
|
mPrivateData = aContext->UsePrivateBrowsing();
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
mInitialized = true;
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//
|
|
|
|
// GetTransferDataFlavors
|
|
|
|
//
|
|
|
|
// Returns a copy of the internal list of flavors. This does NOT take into
|
|
|
|
// account any converter that may be registered. This list consists of
|
|
|
|
// nsISupportsCString objects so that the flavor list can be accessed from JS.
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsTransferable::GetTransferDataFlavors(nsISupportsArray ** aDataFlavorList)
|
|
|
|
{
|
2012-04-16 19:14:01 -07:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = NS_NewISupportsArray ( aDataFlavorList );
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2014-05-20 12:43:57 -07:00
|
|
|
for (size_t i = 0; i < mDataArray.Length(); ++i) {
|
2009-03-10 19:26:03 -07:00
|
|
|
DataStruct& data = mDataArray.ElementAt(i);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsISupportsCString> flavorWrapper = do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID);
|
|
|
|
if ( flavorWrapper ) {
|
2009-03-10 19:26:03 -07:00
|
|
|
flavorWrapper->SetData ( data.GetFlavor() );
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsISupports> genericWrapper ( do_QueryInterface(flavorWrapper) );
|
|
|
|
(*aDataFlavorList)->AppendElement( genericWrapper );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// GetTransferData
|
|
|
|
//
|
|
|
|
// Returns the data of the requested flavor, obtained from either having the data on hand or
|
|
|
|
// using a converter to get it. The data is wrapped in a nsISupports primitive so that it is
|
|
|
|
// accessible from JS.
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTransferable::GetTransferData(const char *aFlavor, nsISupports **aData, uint32_t *aDataLen)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-04-16 19:14:01 -07:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aFlavor && aData && aDataLen);
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
2010-06-09 12:08:09 -07:00
|
|
|
nsCOMPtr<nsISupports> savedData;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// first look and see if the data is present in one of the intrinsic flavors
|
2014-05-20 12:43:57 -07:00
|
|
|
for (size_t i = 0; i < mDataArray.Length(); ++i) {
|
2009-03-10 19:26:03 -07:00
|
|
|
DataStruct& data = mDataArray.ElementAt(i);
|
|
|
|
if ( data.GetFlavor().Equals(aFlavor) ) {
|
2010-06-06 07:25:48 -07:00
|
|
|
nsCOMPtr<nsISupports> dataBytes;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len;
|
2010-06-06 07:25:48 -07:00
|
|
|
data.GetData(getter_AddRefs(dataBytes), &len);
|
|
|
|
if (len == kFlavorHasDataProvider && dataBytes) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// do we have a data provider?
|
2010-06-06 07:25:48 -07:00
|
|
|
nsCOMPtr<nsIFlavorDataProvider> dataProvider = do_QueryInterface(dataBytes);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (dataProvider) {
|
2010-06-06 07:25:48 -07:00
|
|
|
rv = dataProvider->GetFlavorData(this, aFlavor,
|
|
|
|
getter_AddRefs(dataBytes), &len);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break; // the provider failed. fall into the converter code below.
|
|
|
|
}
|
|
|
|
}
|
2010-06-06 07:25:48 -07:00
|
|
|
if (dataBytes && len > 0) { // XXXmats why is zero length not ok?
|
|
|
|
*aDataLen = len;
|
|
|
|
dataBytes.forget(aData);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2010-06-06 07:25:48 -07:00
|
|
|
}
|
2010-06-09 12:08:09 -07:00
|
|
|
savedData = dataBytes; // return this if format converter fails
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool found = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// if not, try using a format converter to get the requested flavor
|
|
|
|
if ( mFormatConv ) {
|
2014-05-20 12:43:57 -07:00
|
|
|
for (size_t i = 0; i < mDataArray.Length(); ++i) {
|
2009-03-10 19:26:03 -07:00
|
|
|
DataStruct& data = mDataArray.ElementAt(i);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool canConvert = false;
|
2009-03-10 19:26:03 -07:00
|
|
|
mFormatConv->CanConvert(data.GetFlavor().get(), aFlavor, &canConvert);
|
2007-03-22 10:30:00 -07:00
|
|
|
if ( canConvert ) {
|
|
|
|
nsCOMPtr<nsISupports> dataBytes;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len;
|
2009-03-10 19:26:03 -07:00
|
|
|
data.GetData(getter_AddRefs(dataBytes), &len);
|
2010-06-06 07:25:48 -07:00
|
|
|
if (len == kFlavorHasDataProvider && dataBytes) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// do we have a data provider?
|
|
|
|
nsCOMPtr<nsIFlavorDataProvider> dataProvider = do_QueryInterface(dataBytes);
|
|
|
|
if (dataProvider) {
|
2010-06-06 07:25:48 -07:00
|
|
|
rv = dataProvider->GetFlavorData(this, aFlavor,
|
|
|
|
getter_AddRefs(dataBytes), &len);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break; // give up
|
|
|
|
}
|
|
|
|
}
|
2009-03-10 19:26:03 -07:00
|
|
|
mFormatConv->Convert(data.GetFlavor().get(), dataBytes, len, aFlavor, aData, aDataLen);
|
2011-10-17 07:59:28 -07:00
|
|
|
found = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-09 12:08:09 -07:00
|
|
|
|
|
|
|
// for backward compatibility
|
|
|
|
if (!found) {
|
|
|
|
savedData.forget(aData);
|
|
|
|
*aDataLen = 0;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return found ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// GetAnyTransferData
|
|
|
|
//
|
|
|
|
// Returns the data of the first flavor found. Caller is responsible for deleting the
|
|
|
|
// flavor string.
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTransferable::GetAnyTransferData(char **aFlavor, nsISupports **aData, uint32_t *aDataLen)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-04-16 19:14:01 -07:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aFlavor && aData && aDataLen);
|
|
|
|
|
2014-05-20 12:43:57 -07:00
|
|
|
for (size_t i = 0; i < mDataArray.Length(); ++i) {
|
2009-03-10 19:26:03 -07:00
|
|
|
DataStruct& data = mDataArray.ElementAt(i);
|
|
|
|
if (data.IsDataAvailable()) {
|
|
|
|
*aFlavor = ToNewCString(data.GetFlavor());
|
|
|
|
data.GetData(aData, aDataLen);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// SetTransferData
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTransferable::SetTransferData(const char *aFlavor, nsISupports *aData, uint32_t aDataLen)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-04-16 19:14:01 -07:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG(aFlavor);
|
|
|
|
|
|
|
|
// first check our intrinsic flavors to see if one has been registered.
|
2014-05-20 12:43:57 -07:00
|
|
|
for (size_t i = 0; i < mDataArray.Length(); ++i) {
|
2009-03-10 19:26:03 -07:00
|
|
|
DataStruct& data = mDataArray.ElementAt(i);
|
|
|
|
if ( data.GetFlavor().Equals(aFlavor) ) {
|
2015-02-18 03:52:00 -08:00
|
|
|
data.SetData ( aData, aDataLen, mPrivateData );
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if not, try using a format converter to find a flavor to put the data in
|
|
|
|
if ( mFormatConv ) {
|
2014-05-20 12:43:57 -07:00
|
|
|
for (size_t i = 0; i < mDataArray.Length(); ++i) {
|
2009-03-10 19:26:03 -07:00
|
|
|
DataStruct& data = mDataArray.ElementAt(i);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool canConvert = false;
|
2009-03-10 19:26:03 -07:00
|
|
|
mFormatConv->CanConvert(aFlavor, data.GetFlavor().get(), &canConvert);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if ( canConvert ) {
|
|
|
|
nsCOMPtr<nsISupports> ConvertedData;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t ConvertedLen;
|
2009-03-10 19:26:03 -07:00
|
|
|
mFormatConv->Convert(aFlavor, aData, aDataLen, data.GetFlavor().get(), getter_AddRefs(ConvertedData), &ConvertedLen);
|
2015-02-18 03:52:00 -08:00
|
|
|
data.SetData(ConvertedData, ConvertedLen, mPrivateData);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can't set data neither directly nor through converter. Just add this flavor and try again
|
|
|
|
nsresult result = NS_ERROR_FAILURE;
|
|
|
|
if ( NS_SUCCEEDED(AddDataFlavor(aFlavor)) )
|
|
|
|
result = SetTransferData (aFlavor, aData, aDataLen);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// AddDataFlavor
|
|
|
|
//
|
|
|
|
// Adds a data flavor to our list with no data. Error if it already exists.
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTransferable::AddDataFlavor(const char *aDataFlavor)
|
|
|
|
{
|
2012-04-16 19:14:01 -07:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2009-03-10 19:26:03 -07:00
|
|
|
if (GetDataForFlavor (mDataArray, aDataFlavor) != mDataArray.NoIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Create a new "slot" for the data
|
2009-03-10 19:26:03 -07:00
|
|
|
mDataArray.AppendElement(DataStruct ( aDataFlavor ));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// RemoveDataFlavor
|
|
|
|
//
|
|
|
|
// Removes a data flavor (and causes the data to be destroyed). Error if
|
|
|
|
// the requested flavor is not present.
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTransferable::RemoveDataFlavor(const char *aDataFlavor)
|
|
|
|
{
|
2012-04-16 19:14:01 -07:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2014-05-20 12:43:57 -07:00
|
|
|
size_t idx = GetDataForFlavor(mDataArray, aDataFlavor);
|
|
|
|
if (idx != mDataArray.NoIndex) {
|
2009-03-10 19:26:03 -07:00
|
|
|
mDataArray.RemoveElementAt (idx);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-03-10 19:26:03 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsTransferable::IsLargeDataSet(bool *_retval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-04-16 19:14:01 -07:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
2011-10-17 07:59:28 -07:00
|
|
|
*_retval = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP nsTransferable::SetConverter(nsIFormatConverter * aConverter)
|
|
|
|
{
|
2012-04-16 19:14:01 -07:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mFormatConv = aConverter;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP nsTransferable::GetConverter(nsIFormatConverter * *aConverter)
|
|
|
|
{
|
2012-04-16 19:14:01 -07:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aConverter);
|
|
|
|
*aConverter = mFormatConv;
|
|
|
|
NS_IF_ADDREF(*aConverter);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// FlavorsTransferableCanImport
|
|
|
|
//
|
|
|
|
// Computes a list of flavors that the transferable can accept into it, either through
|
|
|
|
// intrinsic knowledge or input data converters.
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTransferable::FlavorsTransferableCanImport(nsISupportsArray **_retval)
|
|
|
|
{
|
2012-04-16 19:14:01 -07:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
|
|
|
|
// Get the flavor list, and on to the end of it, append the list of flavors we
|
|
|
|
// can also get to through a converter. This is so that we can just walk the list
|
|
|
|
// in one go, looking for the desired flavor.
|
|
|
|
GetTransferDataFlavors(_retval); // addrefs
|
|
|
|
nsCOMPtr<nsIFormatConverter> converter;
|
|
|
|
GetConverter(getter_AddRefs(converter));
|
|
|
|
if ( converter ) {
|
|
|
|
nsCOMPtr<nsISupportsArray> convertedList;
|
|
|
|
converter->GetInputDataFlavors(getter_AddRefs(convertedList));
|
|
|
|
|
|
|
|
if ( convertedList ) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t importListLen;
|
2007-03-22 10:30:00 -07:00
|
|
|
convertedList->Count(&importListLen);
|
|
|
|
|
2014-05-08 18:03:35 -07:00
|
|
|
for (uint32_t i = 0; i < importListLen; ++i ) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsISupports> genericFlavor;
|
|
|
|
convertedList->GetElementAt ( i, getter_AddRefs(genericFlavor) );
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupportsCString> flavorWrapper ( do_QueryInterface (genericFlavor) );
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString flavorStr;
|
2007-03-22 10:30:00 -07:00
|
|
|
flavorWrapper->GetData( flavorStr );
|
|
|
|
|
2009-03-10 19:26:03 -07:00
|
|
|
if (GetDataForFlavor (mDataArray, flavorStr.get())
|
|
|
|
== mDataArray.NoIndex) // Don't append if already in intrinsic list
|
2007-03-22 10:30:00 -07:00
|
|
|
(*_retval)->AppendElement (genericFlavor);
|
|
|
|
} // foreach flavor that can be converted to
|
|
|
|
}
|
|
|
|
} // if a converter exists
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
} // FlavorsTransferableCanImport
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// FlavorsTransferableCanExport
|
|
|
|
//
|
|
|
|
// Computes a list of flavors that the transferable can export, either through
|
|
|
|
// intrinsic knowledge or output data converters.
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTransferable::FlavorsTransferableCanExport(nsISupportsArray **_retval)
|
|
|
|
{
|
2012-04-16 19:14:01 -07:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
|
|
|
|
// Get the flavor list, and on to the end of it, append the list of flavors we
|
|
|
|
// can also get to through a converter. This is so that we can just walk the list
|
|
|
|
// in one go, looking for the desired flavor.
|
|
|
|
GetTransferDataFlavors(_retval); // addrefs
|
|
|
|
nsCOMPtr<nsIFormatConverter> converter;
|
|
|
|
GetConverter(getter_AddRefs(converter));
|
|
|
|
if ( converter ) {
|
|
|
|
nsCOMPtr<nsISupportsArray> convertedList;
|
|
|
|
converter->GetOutputDataFlavors(getter_AddRefs(convertedList));
|
|
|
|
|
|
|
|
if ( convertedList ) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t importListLen;
|
2007-03-22 10:30:00 -07:00
|
|
|
convertedList->Count(&importListLen);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for ( uint32_t i=0; i < importListLen; ++i ) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsISupports> genericFlavor;
|
|
|
|
convertedList->GetElementAt ( i, getter_AddRefs(genericFlavor) );
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupportsCString> flavorWrapper ( do_QueryInterface (genericFlavor) );
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString flavorStr;
|
2007-03-22 10:30:00 -07:00
|
|
|
flavorWrapper->GetData( flavorStr );
|
|
|
|
|
2009-03-10 19:26:03 -07:00
|
|
|
if (GetDataForFlavor (mDataArray, flavorStr.get())
|
|
|
|
== mDataArray.NoIndex) // Don't append if already in intrinsic list
|
2007-03-22 10:30:00 -07:00
|
|
|
(*_retval)->AppendElement (genericFlavor);
|
|
|
|
} // foreach flavor that can be converted to
|
|
|
|
}
|
|
|
|
} // if a converter exists
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
} // FlavorsTransferableCanExport
|
2012-06-26 08:41:12 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTransferable::GetIsPrivateData(bool *aIsPrivateData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(aIsPrivateData);
|
|
|
|
|
|
|
|
*aIsPrivateData = mPrivateData;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-08-14 20:37:44 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTransferable::SetIsPrivateData(bool aIsPrivateData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
|
|
|
mPrivateData = aIsPrivateData;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-09-21 09:41:56 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTransferable::GetRequestingNode(nsIDOMNode** outRequestingNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> node = do_QueryReferent(mRequestingNode);
|
|
|
|
node.forget(outRequestingNode);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTransferable::SetRequestingNode(nsIDOMNode* aRequestingNode)
|
|
|
|
{
|
|
|
|
mRequestingNode = do_GetWeakReference(aRequestingNode);
|
|
|
|
return NS_OK;
|
|
|
|
}
|