clipboardcache-x in Temp dir

Talk about code development, features, specific bugs, enhancements, patches, and similar things.
Forum rules
Please keep everything here strictly on-topic.
This board is meant for Pale Moon source code development related subjects only like code snippets, patches, specific bugs, git, the repositories, etc.

This is not for tech support! Please do not post tech support questions in the "Development" board!
Please make sure not to use this board for support questions. Please post issues with specific websites, extensions, etc. in the relevant boards for those topics.

Please keep things on-topic as this forum will be used for reference for Pale Moon development. Expect topics that aren't relevant as such to be moved or deleted.
User avatar
__Sandra__
Hobby Astronomer
Hobby Astronomer
Posts: 27
Joined: 2022-05-16, 08:00
Location: Chernihiv, Ukraine

clipboardcache-x in Temp dir

Unread post by __Sandra__ » 2023-04-09, 10:58

Each time copy or paste a large text in the browser, "clipboardcache" files in a temporary folder are created. They do not delete after the program is closed.

This is something from the file \platform\widget\nsTransferable.cpp

Code: Select all

  enum {
    // The size of data over which we write the data to disk rather than
    // keep it around in memory.
    kLargeDatasetSize = 1000000        // 1 million bytes
  };

...

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);
    }

    // write out the contents of the clipboard
    // to the file
...

...
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(); 
Why is this data recorded to the disk? Is there a real need for this?

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35477
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: clipboardcache-x in Temp dir

Unread post by Moonchild » 2023-04-09, 12:30

__Sandra__ wrote:
2023-04-09, 10:58
Is there a real need for this?
Yes :)
You wouldn't want to have massive clipboard buffers hanging around in the browser.

That being said though, the value can probably be bumped to something higher since a lot has changed since this was introduced in 1999 :coffee:
But I would certainly not do away with it entirely as it would set the browser up for OOM failures in that case.
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

User avatar
__Sandra__
Hobby Astronomer
Hobby Astronomer
Posts: 27
Joined: 2022-05-16, 08:00
Location: Chernihiv, Ukraine

Re: clipboardcache-x in Temp dir

Unread post by __Sandra__ » 2023-04-09, 14:24

The main question was that these files could not be deleted. Mozilla made some changes related to this https://github.com/mozilla/gecko-dev/commit/ecc04a9245b409d7eed7df63ba733cd2073180de

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35477
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: clipboardcache-x in Temp dir

Unread post by Moonchild » 2023-04-09, 17:22

__Sandra__ wrote:
2023-04-09, 14:24
The main question was that these files could not be deleted.
Well the only question I see is the one I answered. The "isn't deleted after close" leaving it up to the OS or user to clean up the temp folder on a bad shutdown was just a statement.
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

User avatar
__Sandra__
Hobby Astronomer
Hobby Astronomer
Posts: 27
Joined: 2022-05-16, 08:00
Location: Chernihiv, Ukraine

Re: clipboardcache-x in Temp dir

Unread post by __Sandra__ » 2023-04-11, 06:03

Use nsAnonymousTemporaryFile for clipboard cache

Code: Select all

--- D:/WS/!incoming/pm/platform_old/widget/nsITransferable.idl	Sat Apr 01 09:01:32 2023
+++ D:/WS/!incoming/pm/platform/widget/nsITransferable.idl	Mon Apr 10 15:29:24 2023
@@ -150,12 +150,6 @@
   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 
     ///////////////////////////////

Code: Select all

--- D:/WS/!incoming/pm/platform_old/widget/nsTransferable.cpp	Sun Nov 27 20:24:24 2022
+++ D:/WS/!incoming/pm/platform/widget/nsTransferable.cpp	Mon Apr 10 15:35:20 2023
@@ -14,6 +14,7 @@
 
 
 #include "nsTransferable.h"
+#include "nsAnonymousTemporaryFile.h"
 #include "nsArray.h"
 #include "nsArrayUtils.h"
 #include "nsString.h"
@@ -27,7 +28,6 @@
 #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 @@
 #include "nsIOutputStream.h"
 #include "nsIInputStream.h"
 #include "nsIWeakReferenceUtils.h"
-#include "nsIFile.h"
 #include "nsILoadContext.h"
 #include "mozilla/UniquePtr.h"
 
@@ -56,164 +55,126 @@
 //-------------------------------------------------------------------------
 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 @@
   *
   *
   */
-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::GetConverter(nsIFormatConverter * *aConverter)
+NS_IMETHODIMP
+nsTransferable::GetConverter(nsIFormatConverter * *aConverter)
 {
   MOZ_ASSERT(mInitialized);
 
@@ -521,7 +470,6 @@
   NS_IF_ADDREF(*aConverter);
   return NS_OK;
 }
-
 
 //
 // FlavorsTransferableCanImport

Code: Select all

--- D:/WS/!incoming/pm/platform_old/widget/nsTransferable.h	Sun Nov 27 20:24:24 2022
+++ D:/WS/!incoming/pm/platform/widget/nsTransferable.h	Mon Apr 10 15:27:36 2023
@@ -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 @@
 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 @@
   
   nsCOMPtr<nsISupports> mData;   // OWNER - some varient of primitive wrapper
   uint32_t mDataLen;
+  PRFileDesc* mCacheFD;
   const nsCString mFlavor;
-  char *   mCacheFileName;
 
 };
 

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35477
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: clipboardcache-x in Temp dir

Unread post by Moonchild » 2023-04-11, 06:17

If you want to contribute code, please make a pull request on the repo.
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

User avatar
__Sandra__
Hobby Astronomer
Hobby Astronomer
Posts: 27
Joined: 2022-05-16, 08:00
Location: Chernihiv, Ukraine

Re: clipboardcache-x in Temp dir

Unread post by __Sandra__ » 2023-04-11, 09:09

Moonchild wrote:
2023-04-11, 06:17
make a pull request on the repo.
I did. I have little experience in this, I hope I did everything right

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35477
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: clipboardcache-x in Temp dir

Unread post by Moonchild » 2023-04-12, 08:47

__Sandra__ wrote:
2023-04-11, 09:09
I hope I did everything right
Almost. You based your work on Franklin's branch instead of master. I'll figure out how best to evaluate the changes though, no worries.
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

Locked