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