Use nsAnonymousTemporaryFile for clipboard cache

Base on ecc04a9245
This commit is contained in:
Sandra 2023-04-11 11:58:51 +03:00 • committed by roytam1
commit 42723b1636
3 changed files with 67 additions and 127 deletions

View file

@ -150,12 +150,6 @@ interface nsITransferable : nsISupports
void getAnyTransferData ( out ACString aFlavor, out nsISupports aData, void getAnyTransferData ( out ACString aFlavor, out nsISupports aData,
out unsigned long aDataLen ) ; out unsigned long aDataLen ) ;
/**
* Returns true if the data is large.
*/
boolean isLargeDataSet ( ) ;
/////////////////////////////// ///////////////////////////////
// Setter part of interface // Setter part of interface
/////////////////////////////// ///////////////////////////////

View file

@ -14,6 +14,7 @@ Notes to self:
#include "nsTransferable.h" #include "nsTransferable.h"
#include "nsAnonymousTemporaryFile.h"
#include "nsArray.h" #include "nsArray.h"
#include "nsArrayUtils.h" #include "nsArrayUtils.h"
#include "nsString.h" #include "nsString.h"
@ -27,7 +28,6 @@ Notes to self:
#include "nsISupportsPrimitives.h" #include "nsISupportsPrimitives.h"
#include "nsMemory.h" #include "nsMemory.h"
#include "nsPrimitiveHelpers.h" #include "nsPrimitiveHelpers.h"
#include "nsXPIDLString.h"
#include "nsDirectoryServiceDefs.h" #include "nsDirectoryServiceDefs.h"
#include "nsDirectoryService.h" #include "nsDirectoryService.h"
#include "nsCRT.h" #include "nsCRT.h"
@ -36,7 +36,6 @@ Notes to self:
#include "nsIOutputStream.h" #include "nsIOutputStream.h"
#include "nsIInputStream.h" #include "nsIInputStream.h"
#include "nsIWeakReferenceUtils.h" #include "nsIWeakReferenceUtils.h"
#include "nsIFile.h"
#include "nsILoadContext.h" #include "nsILoadContext.h"
#include "mozilla/UniquePtr.h" #include "mozilla/UniquePtr.h"
@ -56,164 +55,126 @@ size_t GetDataForFlavor (const nsTArray<DataStruct>& aArray,
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
DataStruct::~DataStruct() DataStruct::~DataStruct()
{ {
if (mCacheFileName) free(mCacheFileName); if (mCacheFD) {
PR_Close(mCacheFD);
}
} }
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
void void
DataStruct::SetData ( nsISupports* aData, uint32_t aDataLen, bool aIsPrivateData ) DataStruct::SetData(nsISupports* aData, uint32_t aDataLen, bool aIsPrivateData)
{ {
// Now, check to see if we consider the data to be "too large" // Now, check to see if we consider the data to be "too large"
// as well as ensuring that private browsing mode is disabled // as well as ensuring that private browsing mode is disabled
if (aDataLen > kLargeDatasetSize && !aIsPrivateData) { if (aDataLen > kLargeDatasetSize && !aIsPrivateData) {
// if so, cache it to disk instead of memory // if so, cache it to disk instead of memory
if ( NS_SUCCEEDED(WriteCache(aData, aDataLen)) ) if (NS_SUCCEEDED(WriteCache(aData, aDataLen))) {
// Clear previously set small data.
mData = nullptr;
mDataLen = 0;
return; return;
else }
NS_WARNING("Oh no, couldn't write data to the cache file"); NS_WARNING("Oh no, couldn't write data to the cache file");
} }
if (mCacheFD) {
// Clear previously set big data.
PR_Close(mCacheFD);
mCacheFD = nullptr;
}
mData = aData; mData = aData;
mDataLen = aDataLen; mDataLen = aDataLen;
} }
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
void void
DataStruct::GetData ( nsISupports** aData, uint32_t *aDataLen ) DataStruct::GetData(nsISupports** aData, uint32_t* aDataLen)
{ {
// check here to see if the data is cached on disk // check here to see if the data is cached on disk
if ( !mData && mCacheFileName ) { if (mCacheFD) {
// if so, read it in and pass it back // if so, read it in and pass it back
// ReadCache creates memory and copies the data into it. // ReadCache creates memory and copies the data into it.
if ( NS_SUCCEEDED(ReadCache(aData, aDataLen)) ) if (NS_SUCCEEDED(ReadCache(aData, aDataLen)))
return; return;
else { else {
// oh shit, something went horribly wrong here. // oh shit, something went horribly wrong here.
NS_WARNING("Oh no, couldn't read data in from the cache file"); NS_WARNING("Oh no, couldn't read data in from the cache file");
*aData = nullptr; *aData = nullptr;
*aDataLen = 0; *aDataLen = 0;
PR_Close(mCacheFD);
mCacheFD = nullptr;
return; return;
} }
} }
*aData = mData; *aData = mData;
if ( mData ) if (mData)
NS_ADDREF(*aData); NS_ADDREF(*aData);
*aDataLen = mDataLen; *aDataLen = mDataLen;
} }
//-------------------------------------------------------------------------
already_AddRefed<nsIFile>
DataStruct::GetFileSpec(const char* aFileName)
{
nsCOMPtr<nsIFile> cacheFile;
NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(cacheFile));
if (!cacheFile)
return nullptr;
// 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"));
nsresult rv = cacheFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
if (NS_FAILED(rv))
return nullptr;
} else {
cacheFile->AppendNative(nsDependentCString(aFileName));
}
return cacheFile.forget();
}
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
nsresult nsresult
DataStruct::WriteCache(nsISupports* aData, uint32_t aDataLen) DataStruct::WriteCache(nsISupports* aData, uint32_t aDataLen)
{ {
// Get a new path and file to the temp directory nsresult rv;
nsCOMPtr<nsIFile> cacheFile = GetFileSpec(mCacheFileName); if (!mCacheFD) {
if (cacheFile) { rv = NS_OpenAnonymousTemporaryFile(&mCacheFD);
// remember the file name if (NS_FAILED(rv)) {
if (!mCacheFileName) { return NS_ERROR_FAILURE;
nsXPIDLCString fName; }
cacheFile->GetNativeLeafName(fName); } else if (PR_Seek64(mCacheFD, 0, PR_SEEK_SET) == -1) {
mCacheFileName = strdup(fName); return NS_ERROR_FAILURE;
} }
// write out the contents of the clipboard // write out the contents of the clipboard to the file
// to the file
//uint32_t bytes;
nsCOMPtr<nsIOutputStream> outStr;
NS_NewLocalFileOutputStream(getter_AddRefs(outStr),
cacheFile);
if (!outStr) return NS_ERROR_FAILURE;
void* buff = nullptr; void* buff = nullptr;
nsPrimitiveHelpers::CreateDataFromPrimitive ( mFlavor.get(), aData, &buff, aDataLen ); nsPrimitiveHelpers::CreateDataFromPrimitive(mFlavor.get(), aData, &buff, aDataLen);
if ( buff ) { if ( buff ) {
uint32_t ignored; int32_t written = PR_Write(mCacheFD, buff, aDataLen);
outStr->Write(reinterpret_cast<char*>(buff), aDataLen, &ignored);
free(buff); free(buff);
if (written) {
return NS_OK; return NS_OK;
} }
} }
PR_Close(mCacheFD);
mCacheFD = nullptr;
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
nsresult nsresult
DataStruct::ReadCache(nsISupports** aData, uint32_t* aDataLen) DataStruct::ReadCache(nsISupports** aData, uint32_t* aDataLen)
{ {
// if we don't have a cache filename we are out of luck // if we don't have a cache filename we are out of luck
if (!mCacheFileName) if (!mCacheFD) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
// get the path and file name
nsCOMPtr<nsIFile> cacheFile = GetFileSpec(mCacheFileName);
bool exists;
if ( cacheFile && NS_SUCCEEDED(cacheFile->Exists(&exists)) && exists ) {
// get the size of the file
int64_t fileSize;
int64_t max32 = 0xFFFFFFFF;
cacheFile->GetFileSize(&fileSize);
if (fileSize > max32)
return NS_ERROR_OUT_OF_MEMORY;
uint32_t size = uint32_t(fileSize);
// create new memory for the large clipboard data
auto data = mozilla::MakeUnique<char[]>(size);
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;
nsresult rv = inStr->Read(data.get(), fileSize, aDataLen);
// make sure we got all the data ok
if (NS_SUCCEEDED(rv) && *aDataLen == size) {
nsPrimitiveHelpers::CreatePrimitiveForData(mFlavor.get(), data.get(),
fileSize, aData);
return *aData ? NS_OK : NS_ERROR_FAILURE;
} }
// zero the return params PRFileInfo fileInfo;
*aData = nullptr; if (PR_GetOpenFileInfo(mCacheFD, &fileInfo) != PR_SUCCESS) {
*aDataLen = 0; return NS_ERROR_FAILURE;
}
if (PR_Seek64(mCacheFD, 0, PR_SEEK_SET) == -1) {
return NS_ERROR_FAILURE;
}
uint32_t fileSize = fileInfo.size;
auto data = mozilla::MakeUnique<char[]>(fileSize);
if (!data) {
return NS_ERROR_OUT_OF_MEMORY;
} }
uint32_t actual = PR_Read(mCacheFD, data.get(), fileSize);
if (actual != fileSize) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
}
nsPrimitiveHelpers::CreatePrimitiveForData(mFlavor.get(), data.get(), fileSize, aData);
*aDataLen = fileSize;
return NS_OK;
} }
@ -484,22 +445,9 @@ nsTransferable::RemoveDataFlavor(const char *aDataFlavor)
* *
* *
*/ */
NS_IMETHODIMP NS_IMETHODIMP
nsTransferable::IsLargeDataSet(bool *_retval) nsTransferable::SetConverter(nsIFormatConverter * aConverter)
{
MOZ_ASSERT(mInitialized);
NS_ENSURE_ARG_POINTER(_retval);
*_retval = false;
return NS_OK;
}
/**
*
*
*/
NS_IMETHODIMP nsTransferable::SetConverter(nsIFormatConverter * aConverter)
{ {
MOZ_ASSERT(mInitialized); MOZ_ASSERT(mInitialized);
@ -512,7 +460,8 @@ NS_IMETHODIMP nsTransferable::SetConverter(nsIFormatConverter * aConverter)
* *
* *
*/ */
NS_IMETHODIMP nsTransferable::GetConverter(nsIFormatConverter * *aConverter) NS_IMETHODIMP
nsTransferable::GetConverter(nsIFormatConverter * *aConverter)
{ {
MOZ_ASSERT(mInitialized); MOZ_ASSERT(mInitialized);
@ -522,7 +471,6 @@ NS_IMETHODIMP nsTransferable::GetConverter(nsIFormatConverter * *aConverter)
return NS_OK; return NS_OK;
} }
// //
// FlavorsTransferableCanImport // FlavorsTransferableCanImport
// //

View file

@ -6,16 +6,15 @@
#ifndef nsTransferable_h__ #ifndef nsTransferable_h__
#define nsTransferable_h__ #define nsTransferable_h__
#include "nsIContentPolicyBase.h"
#include "nsIFormatConverter.h" #include "nsIFormatConverter.h"
#include "nsITransferable.h" #include "nsITransferable.h"
#include "nsCOMPtr.h" #include "nsCOMPtr.h"
#include "nsString.h" #include "nsString.h"
#include "nsTArray.h" #include "nsTArray.h"
#include "nsIPrincipal.h" #include "nsIPrincipal.h"
#include "prio.h"
class nsIMutableArray; class nsIMutableArray;
class nsString;
// //
// DataStruct // DataStruct
@ -25,14 +24,13 @@ class nsString;
struct DataStruct struct DataStruct
{ {
explicit DataStruct ( const char* aFlavor ) explicit DataStruct ( const char* aFlavor )
: mDataLen(0), mFlavor(aFlavor), mCacheFileName(nullptr) { } : mDataLen(0), mFlavor(aFlavor), mCacheFD(nullptr) { }
~DataStruct(); ~DataStruct();
const nsCString& GetFlavor() const { return mFlavor; } const nsCString& GetFlavor() const { return mFlavor; }
void SetData( nsISupports* inData, uint32_t inDataLen, bool aIsPrivateData ); void SetData( nsISupports* inData, uint32_t inDataLen, bool aIsPrivateData );
void GetData( nsISupports** outData, uint32_t *outDataLen ); void GetData( nsISupports** outData, uint32_t *outDataLen );
already_AddRefed<nsIFile> GetFileSpec(const char* aFileName); bool IsDataAvailable() const { return mData ? mDataLen > 0 : mCacheFD != nullptr; }
bool IsDataAvailable() const { return (mData && mDataLen > 0) || (!mData && mCacheFileName); }
protected: protected:
@ -47,8 +45,8 @@ protected:
nsCOMPtr<nsISupports> mData; // OWNER - some varient of primitive wrapper nsCOMPtr<nsISupports> mData; // OWNER - some varient of primitive wrapper
uint32_t mDataLen; uint32_t mDataLen;
PRFileDesc* mCacheFD;
const nsCString mFlavor; const nsCString mFlavor;
char * mCacheFileName;
}; };