Bug 1160908 - [EME] Delete GMPRecords that are 0 bytes in size. r=cpearce, a=lizzard
authorGerald Squelart <gsquelart@mozilla.com>
Wed, 06 May 2015 04:08:00 -0400
changeset 266217 d787fb3b86be
parent 266216 c5327254125d
child 266218 35fad3eed522
push id4792
push userryanvm@gmail.com
push date2015-06-10 20:30 +0000
treeherdermozilla-beta@f137fedd1455 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscpearce, lizzard
bugs1160908
milestone39.0
Bug 1160908 - [EME] Delete GMPRecords that are 0 bytes in size. r=cpearce, a=lizzard
dom/media/gmp/GMPStorageParent.cpp
--- a/dom/media/gmp/GMPStorageParent.cpp
+++ b/dom/media/gmp/GMPStorageParent.cpp
@@ -86,17 +86,17 @@ GetGMPStorageDir(nsIFile** aTempDir, con
 
   tmpFile.forget(aTempDir);
 
   return NS_OK;
 }
 
 enum OpenFileMode  { ReadWrite, Truncate };
 
-nsresult
+static nsresult
 OpenStorageFile(const nsCString& aRecordName,
                 const nsCString& aNodeId,
                 const OpenFileMode aMode,
                 PRFileDesc** aOutFD)
 {
   MOZ_ASSERT(aOutFD);
 
   nsCOMPtr<nsIFile> f;
@@ -112,21 +112,38 @@ OpenStorageFile(const nsCString& aRecord
   auto mode = PR_RDWR | PR_CREATE_FILE;
   if (aMode == Truncate) {
     mode |= PR_TRUNCATE;
   }
 
   return f->OpenNSPRFileDesc(mode, PR_IRWXU, aOutFD);
 }
 
+static nsresult
+RemoveStorageFile(const nsCString& aRecordName,
+                  const nsCString& aNodeId)
+{
+  nsCOMPtr<nsIFile> f;
+  nsresult rv = GetGMPStorageDir(getter_AddRefs(f), aNodeId);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  nsAutoString recordNameHash;
+  recordNameHash.AppendInt(HashString(aRecordName.get()));
+  f->Append(recordNameHash);
+
+  return f->Remove(/* bool recursive= */ false);
+}
+
 PLDHashOperator
 CloseFile(const nsACString& key, PRFileDesc*& entry, void* cx)
 {
   if (PR_Close(entry) != PR_SUCCESS) {
-    NS_WARNING("GMPDiskStorage Failed to clsose file.");
+    NS_WARNING("GMPDiskStorage failed to close file.");
   }
   return PL_DHASH_REMOVE;
 }
 
 class GMPDiskStorage : public GMPStorage {
 public:
   explicit GMPDiskStorage(const nsCString& aNodeId)
     : mNodeId(aNodeId)
@@ -148,16 +165,17 @@ public:
     mFiles.Put(aRecordName, fd);
     return GMPNoErr;
   }
 
   virtual bool IsOpen(const nsCString& aRecordName) override {
     return mFiles.Contains(aRecordName);
   }
 
+  static
   GMPErr ReadRecordMetadata(PRFileDesc* aFd,
                             int32_t& aOutFileLength,
                             int32_t& aOutRecordLength,
                             nsACString& aOutRecordName)
   {
     int32_t fileLength = PR_Seek(aFd, 0, PR_SEEK_END);
     PR_Seek(aFd, 0, PR_SEEK_SET);
 
@@ -252,20 +270,32 @@ public:
   virtual GMPErr Write(const nsCString& aRecordName,
                        const nsTArray<uint8_t>& aBytes) override
   {
     PRFileDesc* fd = mFiles.Get(aRecordName);
     if (!fd) {
       return GMPGenericErr;
     }
 
+    // Write operations overwrite the entire record. So close it now.
+    PR_Close(fd);
+    mFiles.Remove(aRecordName);
+
+    // Writing 0 bytes means removing (deleting) the file.
+    if (aBytes.Length() == 0) {
+      nsresult rv = RemoveStorageFile(aRecordName, mNodeId);
+      if (NS_WARN_IF(NS_FAILED(rv))) {
+        // Could not delete file -> Continue with trying to erase the contents.
+      } else {
+        return GMPNoErr;
+      }
+    }
+
     // Write operations overwrite the entire record. So re-open the file
     // in truncate mode, to clear its contents.
-    PR_Close(fd);
-    mFiles.Remove(aRecordName);
     if (NS_FAILED(OpenStorageFile(aRecordName, mNodeId, Truncate, &fd))) {
       return GMPGenericErr;
     }
     mFiles.Put(aRecordName, fd);
 
     // Store the length of the record name followed by the record name
     // at the start of the file.
     int32_t bytesWritten = 0;
@@ -353,17 +383,17 @@ public:
 
   virtual void Close(const nsCString& aRecordName) override
   {
     PRFileDesc* fd = mFiles.Get(aRecordName);
     if (fd) {
       if (PR_Close(fd) == PR_SUCCESS) {
         mFiles.Remove(aRecordName);
       } else {
-        NS_WARNING("GMPDiskStorage Failed to clsose file.");
+        NS_WARNING("GMPDiskStorage failed to close file.");
       }
     }
   }
 
 private:
   nsDataHashtable<nsCStringHashKey, PRFileDesc*> mFiles;
   const nsAutoCString mNodeId;
 };