Bug 335545 - Store clipboard data in memory XOR file r=mstange
☠☠ backed out by ffcfc5b57722 ☠ ☠
authorRob Wu <rob@robwu.nl>
Sun, 03 Sep 2017 03:21:45 +0200
changeset 433863 b83ddb70c8b515b89b3e385d5b46f22b165792ed
parent 433862 ea69ee15ed90aa56ea1811b6ec473b0b0c31bad8
child 433864 25a686779a940f1478aa2f4d1595f81344caac5c
push id8114
push userjlorenzo@mozilla.com
push dateThu, 02 Nov 2017 16:33:21 +0000
treeherdermozilla-beta@73e0d89a540f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmstange
bugs335545
milestone58.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 335545 - Store clipboard data in memory XOR file r=mstange Ensure that only DataStruct::mData + mDataLen, XOR DataStruct::mCacheFD is used. (Previously it was possible that all of these members were populated, which is a waste of memory.) The effect of this change is visible when SetTransferData is called multiple times with the same flavor, but with one below the threshold for storing in-memory, and the other above (=store in a file). MozReview-Commit-ID: 4UlkKAYsjf
widget/nsTransferable.cpp
widget/nsTransferable.h
--- a/widget/nsTransferable.cpp
+++ b/widget/nsTransferable.cpp
@@ -63,42 +63,47 @@ DataStruct::~DataStruct()
 //-------------------------------------------------------------------------
 void
 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");
   }
 
   mData    = aData;
   mDataLen = aDataLen;
 }
 
 
 //-------------------------------------------------------------------------
 void
 DataStruct::GetData ( nsISupports** aData, uint32_t *aDataLen )
 {
   // check here to see if the data is cached on disk
-  if ( !mData && mCacheFD ) {
+  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)) )
       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 )
     NS_ADDREF(*aData);
   *aDataLen = mDataLen;
@@ -122,16 +127,18 @@ DataStruct::WriteCache(nsISupports* aDat
   nsPrimitiveHelpers::CreateDataFromPrimitive(mFlavor, aData, &buff, aDataLen);
   if (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)
 {
--- a/widget/nsTransferable.h
+++ b/widget/nsTransferable.h
@@ -38,16 +38,17 @@ protected:
     // 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 WriteCache(nsISupports* aData, uint32_t aDataLen );
   nsresult ReadCache(nsISupports** aData, uint32_t* aDataLen );
   
+  // mData + mDataLen OR mCacheFD should be used, not both.
   nsCOMPtr<nsISupports> mData;   // OWNER - some varient of primitive wrapper
   uint32_t mDataLen;
   PRFileDesc* mCacheFD;
   const nsCString mFlavor;
 
 };
 
 /**