diff options
author | Sandra <sandra_oth@ua.fm> | 2023-04-11 11:58:51 +0300 |
---|---|---|
committer | Sandra <sandra_oth@ua.fm> | 2023-04-11 11:58:51 +0300 |
commit | 1efbe485bb55d7312b2a09aac9948bc301393940 (patch) | |
tree | 922b8c37a5296d15ba32abc9e06ff9c71089c1c9 /widget | |
parent | ff29aa37a86fcbc97cba6870d7bdb50020b442ae (diff) | |
download | uxp-1efbe485bb55d7312b2a09aac9948bc301393940.tar.gz |
Use nsAnonymousTemporaryFile for clipboard cache
Base on https://github.com/mozilla/gecko-dev/commit/ecc04a9245b409d7eed7df63ba733cd2073180de
Diffstat (limited to 'widget')
-rw-r--r-- | widget/nsITransferable.idl | 6 | ||||
-rw-r--r-- | widget/nsTransferable.cpp | 178 | ||||
-rw-r--r-- | widget/nsTransferable.h | 10 |
3 files changed, 67 insertions, 127 deletions
diff --git a/widget/nsITransferable.idl b/widget/nsITransferable.idl index e580673f5e..e8f6ca6c61 100644 --- a/widget/nsITransferable.idl +++ b/widget/nsITransferable.idl @@ -150,12 +150,6 @@ interface nsITransferable : nsISupports void getAnyTransferData ( out ACString aFlavor, out nsISupports aData, out unsigned long aDataLen ) ; - /** - * Returns true if the data is large. - */ - boolean isLargeDataSet ( ) ; - - /////////////////////////////// // Setter part of interface /////////////////////////////// diff --git a/widget/nsTransferable.cpp b/widget/nsTransferable.cpp index e99d45401d..f0c10dc96b 100644 --- a/widget/nsTransferable.cpp +++ b/widget/nsTransferable.cpp @@ -14,6 +14,7 @@ Notes to self: #include "nsTransferable.h" +#include "nsAnonymousTemporaryFile.h" #include "nsArray.h" #include "nsArrayUtils.h" #include "nsString.h" @@ -27,7 +28,6 @@ Notes to self: #include "nsISupportsPrimitives.h" #include "nsMemory.h" #include "nsPrimitiveHelpers.h" -#include "nsXPIDLString.h" #include "nsDirectoryServiceDefs.h" #include "nsDirectoryService.h" #include "nsCRT.h" @@ -36,7 +36,6 @@ Notes to self: #include "nsIOutputStream.h" #include "nsIInputStream.h" #include "nsIWeakReferenceUtils.h" -#include "nsIFile.h" #include "nsILoadContext.h" #include "mozilla/UniquePtr.h" @@ -56,164 +55,126 @@ size_t GetDataForFlavor (const nsTArray<DataStruct>& aArray, //------------------------------------------------------------------------- DataStruct::~DataStruct() { - if (mCacheFileName) free(mCacheFileName); + if (mCacheFD) { + PR_Close(mCacheFD); + } } //------------------------------------------------------------------------- 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" // as well as ensuring that private browsing mode is disabled if (aDataLen > kLargeDatasetSize && !aIsPrivateData) { // 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; - 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; mDataLen = aDataLen; } - //------------------------------------------------------------------------- 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 - if ( !mData && mCacheFileName ) { + if (mCacheFD) { // if so, read it in and pass it back // ReadCache creates memory and copies the data into it. - if ( NS_SUCCEEDED(ReadCache(aData, aDataLen)) ) + 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"); *aData = nullptr; *aDataLen = 0; + PR_Close(mCacheFD); + mCacheFD = nullptr; return; } } *aData = mData; - if ( mData ) + if (mData) NS_ADDREF(*aData); *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 DataStruct::WriteCache(nsISupports* aData, uint32_t aDataLen) { - // Get a new path and file to the temp directory - nsCOMPtr<nsIFile> cacheFile = GetFileSpec(mCacheFileName); - if (cacheFile) { - // remember the file name - if (!mCacheFileName) { - nsXPIDLCString fName; - cacheFile->GetNativeLeafName(fName); - mCacheFileName = strdup(fName); + nsresult rv; + if (!mCacheFD) { + rv = NS_OpenAnonymousTemporaryFile(&mCacheFD); + if (NS_FAILED(rv)) { + return NS_ERROR_FAILURE; } + } else if (PR_Seek64(mCacheFD, 0, PR_SEEK_SET) == -1) { + return NS_ERROR_FAILURE; + } - // write out the contents of the clipboard - // to the file - //uint32_t bytes; - nsCOMPtr<nsIOutputStream> outStr; - - NS_NewLocalFileOutputStream(getter_AddRefs(outStr), - cacheFile); - - if (!outStr) return NS_ERROR_FAILURE; - + // write out the contents of the clipboard to the file void* buff = nullptr; - nsPrimitiveHelpers::CreateDataFromPrimitive ( mFlavor.get(), aData, &buff, aDataLen ); + nsPrimitiveHelpers::CreateDataFromPrimitive(mFlavor.get(), aData, &buff, aDataLen); if ( buff ) { - uint32_t ignored; - outStr->Write(reinterpret_cast<char*>(buff), aDataLen, &ignored); - free(buff); + int32_t written = PR_Write(mCacheFD, buff, aDataLen); + free(buff); + if (written) { return NS_OK; } } + PR_Close(mCacheFD); + mCacheFD = nullptr; return NS_ERROR_FAILURE; } - //------------------------------------------------------------------------- nsresult DataStruct::ReadCache(nsISupports** aData, uint32_t* aDataLen) { // if we don't have a cache filename we are out of luck - if (!mCacheFileName) + if (!mCacheFD) { 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; - } + 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; - // zero the return params - *aData = nullptr; - *aDataLen = 0; + auto data = mozilla::MakeUnique<char[]>(fileSize); + if (!data) { + return NS_ERROR_OUT_OF_MEMORY; } - return NS_ERROR_FAILURE; + uint32_t actual = PR_Read(mCacheFD, data.get(), fileSize); + if (actual != fileSize) { + 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 -nsTransferable::IsLargeDataSet(bool *_retval) -{ - MOZ_ASSERT(mInitialized); - - NS_ENSURE_ARG_POINTER(_retval); - *_retval = false; - return NS_OK; -} - -/** - * - * - */ -NS_IMETHODIMP nsTransferable::SetConverter(nsIFormatConverter * aConverter) +NS_IMETHODIMP +nsTransferable::SetConverter(nsIFormatConverter * aConverter) { 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); @@ -522,7 +471,6 @@ NS_IMETHODIMP nsTransferable::GetConverter(nsIFormatConverter * *aConverter) return NS_OK; } - // // FlavorsTransferableCanImport // diff --git a/widget/nsTransferable.h b/widget/nsTransferable.h index d0db18a46d..1be0fec3fd 100644 --- a/widget/nsTransferable.h +++ b/widget/nsTransferable.h @@ -6,16 +6,15 @@ #ifndef nsTransferable_h__ #define nsTransferable_h__ -#include "nsIContentPolicyBase.h" #include "nsIFormatConverter.h" #include "nsITransferable.h" #include "nsCOMPtr.h" #include "nsString.h" #include "nsTArray.h" #include "nsIPrincipal.h" +#include "prio.h" class nsIMutableArray; -class nsString; // // DataStruct @@ -25,14 +24,13 @@ class nsString; struct DataStruct { explicit DataStruct ( const char* aFlavor ) - : mDataLen(0), mFlavor(aFlavor), mCacheFileName(nullptr) { } + : mDataLen(0), mFlavor(aFlavor), mCacheFD(nullptr) { } ~DataStruct(); const nsCString& GetFlavor() const { return mFlavor; } void SetData( nsISupports* inData, uint32_t inDataLen, bool aIsPrivateData ); void GetData( nsISupports** outData, uint32_t *outDataLen ); - already_AddRefed<nsIFile> GetFileSpec(const char* aFileName); - bool IsDataAvailable() const { return (mData && mDataLen > 0) || (!mData && mCacheFileName); } + bool IsDataAvailable() const { return mData ? mDataLen > 0 : mCacheFD != nullptr; } protected: @@ -47,8 +45,8 @@ protected: nsCOMPtr<nsISupports> mData; // OWNER - some varient of primitive wrapper uint32_t mDataLen; + PRFileDesc* mCacheFD; const nsCString mFlavor; - char * mCacheFileName; }; |