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
|
2016-10-16 22:43:56 +03:00
|
|
|
this crappy API of nsIArrays.
|
1999-08-25 12:35:06 +04:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
1999-03-25 07:14:33 +03:00
|
|
|
#include "nsTransferable.h"
|
2017-09-03 03:29:10 +03:00
|
|
|
#include "nsAnonymousTemporaryFile.h"
|
2016-10-16 22:43:56 +03:00
|
|
|
#include "nsArray.h"
|
|
|
|
#include "nsArrayUtils.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"
|
2015-08-10 22:56:47 +03:00
|
|
|
#include "nsIContentPolicy.h"
|
1999-04-01 18:39:00 +04:00
|
|
|
#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"
|
2003-03-15 04:04:32 +03:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsDirectoryService.h"
|
2016-07-22 11:56:13 +03:00
|
|
|
#include "nsCRT.h"
|
2003-03-15 04:04:32 +03:00
|
|
|
#include "nsNetUtil.h"
|
1999-07-16 18:27:38 +04:00
|
|
|
#include "nsIOutputStream.h"
|
|
|
|
#include "nsIInputStream.h"
|
2012-04-17 06:14:01 +04:00
|
|
|
#include "nsILoadContext.h"
|
2018-11-14 20:03:36 +03:00
|
|
|
#include "nsXULAppAPI.h"
|
2016-06-22 18:38:40 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
1999-03-26 18:03:13 +03:00
|
|
|
|
2018-10-25 13:04:43 +03:00
|
|
|
using namespace mozilla;
|
2018-10-25 01:26:25 +03:00
|
|
|
|
2018-10-25 13:04:43 +03:00
|
|
|
NS_IMPL_ISUPPORTS(nsTransferable, nsITransferable)
|
2018-10-25 01:26:25 +03:00
|
|
|
|
2018-02-25 19:36:08 +03:00
|
|
|
DataStruct::DataStruct(DataStruct&& aRHS)
|
2018-10-17 19:48:43 +03:00
|
|
|
: mData(aRHS.mData.forget()),
|
|
|
|
mCacheFD(aRHS.mCacheFD),
|
|
|
|
mFlavor(aRHS.mFlavor) {
|
2018-02-25 19:36:08 +03:00
|
|
|
aRHS.mCacheFD = nullptr;
|
|
|
|
}
|
|
|
|
|
1999-07-16 18:27:38 +04:00
|
|
|
//-------------------------------------------------------------------------
|
2016-07-22 11:56:13 +03:00
|
|
|
DataStruct::~DataStruct() {
|
2017-09-03 03:29:10 +03:00
|
|
|
if (mCacheFD) {
|
|
|
|
PR_Close(mCacheFD);
|
|
|
|
}
|
1999-07-16 18:27:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2018-11-20 19:59:39 +03:00
|
|
|
|
2018-11-20 20:01:42 +03:00
|
|
|
void DataStruct::SetData(nsISupports* aData, bool aIsPrivateData) {
|
1999-07-16 18:27:38 +04:00
|
|
|
// Now, check to see if we consider the data to be "too large"
|
2018-11-20 19:59:39 +03:00
|
|
|
// as well as ensuring that private browsing mode is disabled.
|
|
|
|
// File IO is not allowed in content processes.
|
|
|
|
if (!aIsPrivateData && XRE_IsParentProcess()) {
|
|
|
|
void* data = nullptr;
|
|
|
|
uint32_t dataLen = 0;
|
|
|
|
nsPrimitiveHelpers::CreateDataFromPrimitive(mFlavor, aData, &data,
|
|
|
|
&dataLen);
|
|
|
|
|
|
|
|
if (dataLen > kLargeDatasetSize) {
|
|
|
|
// Too large, cache it to disk instead of memory.
|
|
|
|
if (NS_SUCCEEDED(WriteCache(data, dataLen))) {
|
|
|
|
free(data);
|
|
|
|
// Clear previously set small data.
|
|
|
|
mData = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_WARNING("Oh no, couldn't write data to the cache file");
|
2017-09-03 04:21:45 +03:00
|
|
|
}
|
2018-11-20 19:59:39 +03:00
|
|
|
|
|
|
|
free(data);
|
2017-09-03 04:21:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mCacheFD) {
|
|
|
|
// Clear previously set big data.
|
|
|
|
PR_Close(mCacheFD);
|
|
|
|
mCacheFD = nullptr;
|
2016-07-22 11:56:13 +03:00
|
|
|
}
|
2000-12-19 03:35:27 +03:00
|
|
|
|
2018-10-17 19:48:43 +03:00
|
|
|
mData = aData;
|
1999-07-16 18:27:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2018-11-20 20:01:42 +03:00
|
|
|
void DataStruct::GetData(nsISupports** aData) {
|
1999-07-16 18:27:38 +04:00
|
|
|
// check here to see if the data is cached on disk
|
2017-09-03 04:21:45 +03:00
|
|
|
if (mCacheFD) {
|
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.
|
2018-11-20 20:01:42 +03:00
|
|
|
if (NS_SUCCEEDED(ReadCache(aData))) {
|
1999-09-10 09:45:08 +04:00
|
|
|
return;
|
|
|
|
}
|
2018-10-23 15:17:02 +03:00
|
|
|
|
|
|
|
// oh shit, something went horribly wrong here.
|
|
|
|
NS_WARNING("Oh no, couldn't read data in from the cache file");
|
|
|
|
*aData = nullptr;
|
|
|
|
PR_Close(mCacheFD);
|
|
|
|
mCacheFD = nullptr;
|
|
|
|
return;
|
1999-07-16 18:27:38 +04:00
|
|
|
}
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2018-10-23 15:17:02 +03:00
|
|
|
nsCOMPtr<nsISupports> data = mData;
|
|
|
|
data.forget(aData);
|
1999-07-16 18:27:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2018-11-20 19:59:39 +03:00
|
|
|
nsresult DataStruct::WriteCache(void* aData, uint32_t aDataLen) {
|
|
|
|
MOZ_ASSERT(aData && aDataLen);
|
|
|
|
MOZ_ASSERT(aDataLen <= uint32_t(std::numeric_limits<int32_t>::max()),
|
|
|
|
"too large size for PR_Write");
|
|
|
|
|
2017-09-03 03:29:10 +03:00
|
|
|
nsresult rv;
|
|
|
|
if (!mCacheFD) {
|
|
|
|
rv = NS_OpenAnonymousTemporaryFile(&mCacheFD);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2003-03-15 04:04:32 +03:00
|
|
|
}
|
2017-09-03 03:29:10 +03:00
|
|
|
} else if (PR_Seek64(mCacheFD, 0, PR_SEEK_SET) == -1) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
1999-08-25 12:35:06 +04:00
|
|
|
|
2018-11-20 19:59:39 +03:00
|
|
|
// Write out the contents of the clipboard to the file.
|
|
|
|
int32_t written = PR_Write(mCacheFD, aData, aDataLen);
|
|
|
|
if (written == int32_t(aDataLen)) {
|
|
|
|
return NS_OK;
|
1999-07-16 18:27:38 +04:00
|
|
|
}
|
2018-11-20 19:59:39 +03:00
|
|
|
|
2017-09-03 04:21:45 +03:00
|
|
|
PR_Close(mCacheFD);
|
|
|
|
mCacheFD = nullptr;
|
1999-07-16 18:27:38 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2018-11-20 20:01:42 +03:00
|
|
|
nsresult DataStruct::ReadCache(nsISupports** aData) {
|
2017-09-03 03:29:10 +03:00
|
|
|
if (!mCacheFD) {
|
2017-09-03 03:29:10 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2017-09-03 03:29:10 +03:00
|
|
|
}
|
1999-07-16 18:27:38 +04:00
|
|
|
|
2017-09-03 03:29:10 +03:00
|
|
|
PRFileInfo fileInfo;
|
|
|
|
if (PR_GetOpenFileInfo(mCacheFD, &fileInfo) != PR_SUCCESS) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
if (PR_Seek64(mCacheFD, 0, PR_SEEK_SET) == -1) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
uint32_t fileSize = fileInfo.size;
|
2017-09-03 03:29:10 +03:00
|
|
|
|
2018-10-25 13:04:43 +03:00
|
|
|
auto data = MakeUnique<char[]>(fileSize);
|
2017-09-03 03:29:10 +03:00
|
|
|
if (!data) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2017-09-03 03:29:10 +03:00
|
|
|
}
|
|
|
|
|
2017-09-03 03:29:10 +03:00
|
|
|
uint32_t actual = PR_Read(mCacheFD, data.get(), fileSize);
|
|
|
|
if (actual != fileSize) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2018-10-17 19:48:43 +03:00
|
|
|
nsPrimitiveHelpers::CreatePrimitiveForData(mFlavor, data.get(), fileSize,
|
|
|
|
aData);
|
2017-09-03 03:29:10 +03:00
|
|
|
return NS_OK;
|
1999-07-16 18:27:38 +04:00
|
|
|
}
|
|
|
|
|
1999-03-25 07:14:33 +03:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Transferable constructor
|
|
|
|
//
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
nsTransferable::nsTransferable()
|
2018-03-07 12:21:50 +03:00
|
|
|
: mPrivateData(false),
|
2015-08-10 22:56:47 +03:00
|
|
|
mContentPolicyType(nsIContentPolicy::TYPE_OTHER)
|
2012-04-17 06:14:01 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
,
|
|
|
|
mInitialized(false)
|
|
|
|
#endif
|
1999-03-25 07:14:33 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Transferable destructor
|
|
|
|
//
|
|
|
|
//-------------------------------------------------------------------------
|
2018-10-17 19:48:43 +03:00
|
|
|
nsTransferable::~nsTransferable() {}
|
1999-03-25 07:14:33 +03:00
|
|
|
|
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
|
|
|
|
//
|
2016-07-22 11:56:13 +03:00
|
|
|
// Returns a copy of the internal list of flavors. This does NOT take into
|
2018-10-10 23:25:54 +03:00
|
|
|
// account any converter that may be registered.
|
1999-08-25 12:35:06 +04:00
|
|
|
//
|
2018-10-10 23:25:54 +03:00
|
|
|
void nsTransferable::GetTransferDataFlavors(nsTArray<nsCString>& aFlavors) {
|
2012-04-17 06:14:01 +04:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2014-05-20 23:43:57 +04:00
|
|
|
for (size_t i = 0; i < mDataArray.Length(); ++i) {
|
2009-03-11 05:26:03 +03:00
|
|
|
DataStruct& data = mDataArray.ElementAt(i);
|
2018-10-10 23:25:54 +03:00
|
|
|
aFlavors.AppendElement(data.GetFlavor());
|
1999-04-01 18:39:00 +04:00
|
|
|
}
|
1999-03-25 07:14:33 +03:00
|
|
|
}
|
|
|
|
|
2018-10-25 13:04:43 +03:00
|
|
|
Maybe<size_t> nsTransferable::FindDataFlavor(const char* aFlavor) {
|
|
|
|
nsDependentCString flavor(aFlavor);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < mDataArray.Length(); ++i) {
|
|
|
|
if (mDataArray[i].GetFlavor().Equals(flavor)) {
|
|
|
|
return Some(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
1999-08-25 12:35:06 +04:00
|
|
|
//
|
|
|
|
// GetTransferData
|
|
|
|
//
|
2018-10-17 19:48:43 +03:00
|
|
|
// 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.
|
1999-08-25 12:35:06 +04:00
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
2018-10-17 19:48:43 +03:00
|
|
|
nsTransferable::GetTransferData(const char* aFlavor, nsISupports** aData) {
|
2012-04-17 06:14:01 +04:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2018-10-25 00:32:04 +03:00
|
|
|
*aData = nullptr;
|
1999-03-25 07:14:33 +03:00
|
|
|
|
2003-04-19 00:20:04 +04:00
|
|
|
nsresult rv = NS_OK;
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2018-10-25 13:04:43 +03:00
|
|
|
// First look and see if the data is present in one of the intrinsic flavors.
|
|
|
|
if (Maybe<size_t> index = FindDataFlavor(aFlavor)) {
|
|
|
|
nsCOMPtr<nsISupports> dataBytes;
|
2018-11-20 20:01:42 +03:00
|
|
|
mDataArray[index.value()].GetData(getter_AddRefs(dataBytes));
|
2018-10-25 13:04:43 +03:00
|
|
|
|
|
|
|
// Do we have a (lazy) data provider?
|
|
|
|
if (nsCOMPtr<nsIFlavorDataProvider> dataProvider =
|
|
|
|
do_QueryInterface(dataBytes)) {
|
|
|
|
rv =
|
|
|
|
dataProvider->GetFlavorData(this, aFlavor, getter_AddRefs(dataBytes));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
dataBytes = nullptr;
|
|
|
|
// The provider failed, fall into the converter code below.
|
2018-10-25 01:26:25 +03:00
|
|
|
}
|
2018-10-25 13:04:43 +03:00
|
|
|
}
|
2018-10-25 00:34:32 +03:00
|
|
|
|
2018-10-25 13:04:43 +03:00
|
|
|
if (dataBytes) {
|
|
|
|
dataBytes.forget(aData);
|
|
|
|
return NS_OK;
|
2018-10-25 01:26:25 +03:00
|
|
|
}
|
2018-10-25 13:04:43 +03:00
|
|
|
|
|
|
|
// Empty data
|
1999-03-25 07:14:33 +03:00
|
|
|
}
|
|
|
|
|
2018-10-25 13:04:43 +03:00
|
|
|
// If not, try using a format converter to get the requested flavor.
|
2018-10-17 19:48:43 +03:00
|
|
|
if (mFormatConv) {
|
2014-05-20 23:43:57 +04:00
|
|
|
for (size_t i = 0; i < mDataArray.Length(); ++i) {
|
2009-03-11 05:26:03 +03:00
|
|
|
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);
|
2018-10-17 19:48:43 +03:00
|
|
|
if (canConvert) {
|
1999-08-25 12:35:06 +04:00
|
|
|
nsCOMPtr<nsISupports> dataBytes;
|
2018-11-20 20:01:42 +03:00
|
|
|
data.GetData(getter_AddRefs(dataBytes));
|
2018-10-16 23:35:10 +03:00
|
|
|
|
|
|
|
// Do we have a (lazy) data provider?
|
|
|
|
if (nsCOMPtr<nsIFlavorDataProvider> dataProvider =
|
|
|
|
do_QueryInterface(dataBytes)) {
|
|
|
|
rv = dataProvider->GetFlavorData(this, aFlavor,
|
2018-11-20 20:01:44 +03:00
|
|
|
getter_AddRefs(dataBytes));
|
2018-10-16 23:35:10 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Give up.
|
2018-10-25 00:32:04 +03:00
|
|
|
return rv;
|
2003-04-19 00:20:04 +04:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 23:35:10 +03:00
|
|
|
|
2018-11-20 20:01:46 +03:00
|
|
|
return mFormatConv->Convert(data.GetFlavor().get(), dataBytes, aFlavor,
|
|
|
|
aData);
|
1999-04-01 18:39:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-09 23:08:09 +04:00
|
|
|
|
2018-10-25 00:32:04 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-03-25 07:14:33 +03:00
|
|
|
}
|
|
|
|
|
1999-08-25 12:35:06 +04:00
|
|
|
//
|
|
|
|
// GetAnyTransferData
|
|
|
|
//
|
2018-10-17 19:48:43 +03:00
|
|
|
// Returns the data of the first flavor found. Caller is responsible for
|
|
|
|
// deleting the flavor string.
|
2016-07-22 11:56:13 +03:00
|
|
|
//
|
1999-08-25 12:35:06 +04:00
|
|
|
NS_IMETHODIMP
|
2018-10-17 19:48:43 +03:00
|
|
|
nsTransferable::GetAnyTransferData(nsACString& aFlavor, nsISupports** aData) {
|
2012-04-17 06:14:01 +04:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2014-05-20 23:43:57 +04:00
|
|
|
for (size_t i = 0; i < mDataArray.Length(); ++i) {
|
2009-03-11 05:26:03 +03:00
|
|
|
DataStruct& data = mDataArray.ElementAt(i);
|
|
|
|
if (data.IsDataAvailable()) {
|
2016-10-26 05:07:16 +03:00
|
|
|
aFlavor.Assign(data.GetFlavor());
|
2018-11-20 20:01:42 +03:00
|
|
|
data.GetData(aData);
|
1999-05-15 18:29:32 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
1999-08-25 12:35:06 +04:00
|
|
|
//
|
|
|
|
// SetTransferData
|
|
|
|
//
|
|
|
|
//
|
2016-07-22 11:56:13 +03:00
|
|
|
//
|
1999-08-25 12:35:06 +04:00
|
|
|
NS_IMETHODIMP
|
2018-10-17 19:48:43 +03:00
|
|
|
nsTransferable::SetTransferData(const char* aFlavor, nsISupports* aData) {
|
2012-04-17 06:14:01 +04:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2000-12-19 03:35:27 +03:00
|
|
|
// first check our intrinsic flavors to see if one has been registered.
|
2018-10-25 13:04:43 +03:00
|
|
|
if (Maybe<size_t> index = FindDataFlavor(aFlavor)) {
|
|
|
|
DataStruct& data = mDataArray.ElementAt(index.value());
|
2018-11-20 20:01:42 +03:00
|
|
|
data.SetData(aData, mPrivateData);
|
2018-10-25 13:04:43 +03: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
|
2018-10-17 19:48:43 +03:00
|
|
|
if (mFormatConv) {
|
2014-05-20 23:43:57 +04:00
|
|
|
for (size_t i = 0; i < mDataArray.Length(); ++i) {
|
2009-03-11 05:26:03 +03:00
|
|
|
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
|
|
|
|
2018-10-17 19:48:43 +03:00
|
|
|
if (canConvert) {
|
2000-12-19 03:35:27 +03:00
|
|
|
nsCOMPtr<nsISupports> ConvertedData;
|
2018-10-17 19:48:43 +03:00
|
|
|
mFormatConv->Convert(aFlavor, aData, data.GetFlavor().get(),
|
2018-11-20 20:01:46 +03:00
|
|
|
getter_AddRefs(ConvertedData));
|
2018-11-20 20:01:42 +03:00
|
|
|
data.SetData(ConvertedData, mPrivateData);
|
2000-12-19 03:35:27 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 19:48:43 +03:00
|
|
|
// Can't set data neither directly nor through converter. Just add this flavor
|
|
|
|
// and try again
|
2018-10-23 15:17:02 +03:00
|
|
|
if (NS_SUCCEEDED(AddDataFlavor(aFlavor))) {
|
2018-11-20 20:00:09 +03:00
|
|
|
return SetTransferData(aFlavor, aData);
|
2018-10-23 15:17:02 +03:00
|
|
|
}
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2018-10-23 15:17:02 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
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.
|
2016-07-22 11:56:13 +03:00
|
|
|
//
|
1999-08-25 12:35:06 +04:00
|
|
|
NS_IMETHODIMP
|
2018-10-17 19:48:43 +03:00
|
|
|
nsTransferable::AddDataFlavor(const char* aDataFlavor) {
|
2012-04-17 06:14:01 +04:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2018-10-25 13:04:43 +03:00
|
|
|
if (FindDataFlavor(aDataFlavor).isSome()) {
|
2000-12-19 03:35:27 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2018-10-23 15:17:02 +03:00
|
|
|
}
|
1999-03-25 07:14:33 +03:00
|
|
|
|
1999-04-01 18:39:00 +04:00
|
|
|
// Create a new "slot" for the data
|
2018-10-17 19:48:43 +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-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
|
2018-10-17 19:48:43 +03:00
|
|
|
nsTransferable::RemoveDataFlavor(const char* aDataFlavor) {
|
2012-04-17 06:14:01 +04:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2018-10-25 13:04:43 +03:00
|
|
|
if (Maybe<size_t> index = FindDataFlavor(aDataFlavor)) {
|
|
|
|
mDataArray.RemoveElementAt(index.value());
|
2000-12-19 03:35:27 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-10-25 13:04:43 +03:00
|
|
|
|
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
|
|
|
|
2018-10-17 19:48:43 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTransferable::SetConverter(nsIFormatConverter* aConverter) {
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-17 19:48:43 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTransferable::GetConverter(nsIFormatConverter** aConverter) {
|
2012-04-17 06:14:01 +04:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2018-10-23 15:17:02 +03:00
|
|
|
nsCOMPtr<nsIFormatConverter> converter = mFormatConv;
|
|
|
|
converter.forget(aConverter);
|
1999-03-26 18:03:13 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-04-02 22:22:36 +04:00
|
|
|
//
|
|
|
|
// FlavorsTransferableCanImport
|
|
|
|
//
|
2018-10-17 19:48:43 +03:00
|
|
|
// Computes a list of flavors that the transferable can accept into it, either
|
|
|
|
// through intrinsic knowledge or input data converters.
|
1999-04-02 22:22:36 +04:00
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
2018-10-10 23:25:54 +03:00
|
|
|
nsTransferable::FlavorsTransferableCanImport(nsTArray<nsCString>& aFlavors) {
|
2012-04-17 06:14:01 +04:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
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
|
2018-10-17 19:48:43 +03:00
|
|
|
// 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.
|
2018-10-10 23:25:54 +03:00
|
|
|
GetTransferDataFlavors(aFlavors);
|
|
|
|
|
|
|
|
if (mFormatConv) {
|
2018-10-10 23:26:06 +03:00
|
|
|
nsTArray<nsCString> convertedList;
|
|
|
|
mFormatConv->GetInputDataFlavors(convertedList);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < convertedList.Length(); ++i) {
|
|
|
|
nsCString& flavorStr = convertedList[i];
|
|
|
|
|
|
|
|
// Don't append if already in intrinsic list
|
|
|
|
if (!aFlavors.Contains(flavorStr)) {
|
|
|
|
aFlavors.AppendElement(flavorStr);
|
2018-10-10 23:25:54 +03:00
|
|
|
}
|
1999-04-02 22:22:36 +04:00
|
|
|
}
|
2018-10-10 23:25:54 +03:00
|
|
|
}
|
1999-04-02 22:22:36 +04:00
|
|
|
|
2016-07-22 11:56:13 +03:00
|
|
|
return NS_OK;
|
2018-10-10 23:25:54 +03:00
|
|
|
}
|
1999-04-02 22:22:36 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// FlavorsTransferableCanExport
|
|
|
|
//
|
|
|
|
// Computes a list of flavors that the transferable can export, either through
|
|
|
|
// intrinsic knowledge or output data converters.
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
2018-10-10 23:25:54 +03:00
|
|
|
nsTransferable::FlavorsTransferableCanExport(nsTArray<nsCString>& aFlavors) {
|
2012-04-17 06:14:01 +04:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
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
|
2018-10-17 19:48:43 +03:00
|
|
|
// 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.
|
2018-10-10 23:25:54 +03:00
|
|
|
GetTransferDataFlavors(aFlavors);
|
|
|
|
|
|
|
|
if (mFormatConv) {
|
2018-10-10 23:26:06 +03:00
|
|
|
nsTArray<nsCString> convertedList;
|
|
|
|
mFormatConv->GetOutputDataFlavors(convertedList);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < convertedList.Length(); ++i) {
|
|
|
|
nsCString& flavorStr = convertedList[i];
|
|
|
|
|
|
|
|
// Don't append if already in intrinsic list
|
|
|
|
if (!aFlavors.Contains(flavorStr)) {
|
|
|
|
aFlavors.AppendElement(flavorStr);
|
2018-10-10 23:25:54 +03:00
|
|
|
}
|
1999-04-02 22:22:36 +04:00
|
|
|
}
|
2018-10-10 23:25:54 +03:00
|
|
|
}
|
1999-04-02 22:22:36 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2018-10-10 23:25:54 +03:00
|
|
|
}
|
2012-06-26 19:41:12 +04:00
|
|
|
|
2018-11-20 04:17:54 +03:00
|
|
|
bool nsTransferable::GetIsPrivateData() {
|
2012-06-26 19:41:12 +04:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2018-11-20 04:17:54 +03:00
|
|
|
return mPrivateData;
|
2012-06-26 19:41:12 +04:00
|
|
|
}
|
2012-08-15 07:37:44 +04:00
|
|
|
|
|
|
|
void nsTransferable::SetIsPrivateData(bool aIsPrivateData) {
|
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
|
|
|
mPrivateData = aIsPrivateData;
|
|
|
|
}
|
|
|
|
|
2018-11-20 04:17:54 +03:00
|
|
|
nsIPrincipal* nsTransferable::GetRequestingPrincipal() {
|
2018-10-23 15:17:02 +03:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2018-11-20 04:17:54 +03:00
|
|
|
return mRequestingPrincipal;
|
2014-09-21 20:41:56 +04:00
|
|
|
}
|
|
|
|
|
2016-07-01 11:01:11 +03:00
|
|
|
void nsTransferable::SetRequestingPrincipal(
|
|
|
|
nsIPrincipal* aRequestingPrincipal) {
|
2018-10-23 15:17:02 +03:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2016-07-01 11:01:11 +03:00
|
|
|
mRequestingPrincipal = aRequestingPrincipal;
|
2014-09-21 20:41:56 +04:00
|
|
|
}
|
2015-08-10 22:56:47 +03:00
|
|
|
|
2018-11-20 04:17:54 +03:00
|
|
|
nsContentPolicyType nsTransferable::GetContentPolicyType() {
|
2018-10-23 15:17:02 +03:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2018-11-20 04:17:54 +03:00
|
|
|
return mContentPolicyType;
|
2015-08-10 22:56:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsTransferable::SetContentPolicyType(
|
|
|
|
nsContentPolicyType aContentPolicyType) {
|
2018-10-23 15:17:02 +03:00
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
2015-08-10 22:56:47 +03:00
|
|
|
mContentPolicyType = aContentPolicyType;
|
|
|
|
}
|