Bug 761076 - FileHandle: Add optional location parameter to LockedFile.truncate(). r=sicking
authorJan Varga <jan.varga@gmail.com>
Mon, 04 Jun 2012 14:31:22 +0200
changeset 95702 deae92fe42b26a730775e8c343897172d6b014a4
parent 95701 77f792cd4483559c943f55bb77cfaa3c00dfa9b2
child 95703 f2b089df69b30de2bfb24e88ba84ac87a8bd514e
push id22831
push userJan.Varga@gmail.com
push dateMon, 04 Jun 2012 12:40:59 +0000
treeherdermozilla-central@deae92fe42b2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssicking
bugs761076
milestone15.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 761076 - FileHandle: Add optional location parameter to LockedFile.truncate(). r=sicking
dom/file/LockedFile.cpp
dom/file/nsIDOMLockedFile.idl
dom/file/test/test_truncate.html
--- a/dom/file/LockedFile.cpp
+++ b/dom/file/LockedFile.cpp
@@ -657,17 +657,19 @@ LockedFile::Append(const jsval& aValue,
                    nsIDOMFileRequest** _retval)
 {
   NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
 
   return WriteOrAppend(aValue, aCx, _retval, true);
 }
 
 NS_IMETHODIMP
-LockedFile::Truncate(nsIDOMFileRequest** _retval)
+LockedFile::Truncate(PRUint64 aLocation,
+                     PRUint8 aOptionalArgCount,
+                     nsIDOMFileRequest** _retval)
 {
   NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
 
   if (!IsOpen()) {
     return NS_ERROR_DOM_FILEHANDLE_LOCKEDFILE_INACTIVE_ERR;
   }
 
   if (mMode != READ_WRITE) {
@@ -677,22 +679,28 @@ LockedFile::Truncate(nsIDOMFileRequest**
   // Do nothing if the window is closed
   if (!GetOwner()) {
     return NS_OK;
   }
 
   nsRefPtr<FileRequest> fileRequest = GenerateFileRequest();
   NS_ENSURE_TRUE(fileRequest, NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
 
+  PRUint64 location = aOptionalArgCount ? aLocation : mLocation;
+
   nsRefPtr<TruncateHelper> helper =
-    new TruncateHelper(this, fileRequest, mLocation);
+    new TruncateHelper(this, fileRequest, location);
 
   nsresult rv = helper->Enqueue();
   NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
 
+  if (aOptionalArgCount) {
+    mLocation = aLocation;
+  }
+
   fileRequest.forget(_retval);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 LockedFile::Flush(nsIDOMFileRequest** _retval)
 {
   NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
--- a/dom/file/nsIDOMLockedFile.idl
+++ b/dom/file/nsIDOMLockedFile.idl
@@ -11,17 +11,17 @@ interface nsIDOMFileHandle;
 interface nsIDOMFileRequest;
 
 dictionary DOMFileMetadataParameters
 {
   boolean size;
   boolean lastModified;
 };
 
-[scriptable, builtinclass, uuid(63055eeb-cc19-468b-bafa-7b7961796340)]
+[scriptable, builtinclass, uuid(e1f69cc5-c6ce-4850-bc09-c4211b1d4290)]
 interface nsIDOMLockedFile : nsISupports
 {
   readonly attribute nsIDOMFileHandle fileHandle;
 
   // "readonly" or "readwrite"
   readonly attribute DOMString mode;
 
   readonly attribute boolean active;
@@ -44,18 +44,19 @@ interface nsIDOMLockedFile : nsISupports
   [implicit_jscontext]
   nsIDOMFileRequest
   write(in jsval value);
 
   [implicit_jscontext]
   nsIDOMFileRequest
   append(in jsval value);
 
+  [optional_argc]
   nsIDOMFileRequest
-  truncate();
+  truncate([optional] in unsigned long long location);
 
   nsIDOMFileRequest
   flush();
 
   void
   abort();
 
   attribute nsIDOMEventListener oncomplete;
--- a/dom/file/test/test_truncate.html
+++ b/dom/file/test/test_truncate.html
@@ -26,31 +26,52 @@
       let lockedFile = fileHandle.open("readwrite");
       request = lockedFile.write(testBuffer);
       request.onsuccess = grabEventAndContinueHandler;
       event = yield;
 
       is(lockedFile.location, 100000, "Correct location");
 
       for (let i = 0; i < 10; i++) {
-        let location = lockedFile.location - 10000
+        let location = lockedFile.location - 10000;
         lockedFile.location = location;
 
         request = lockedFile.truncate();
         request.onsuccess = grabEventAndContinueHandler;
         event = yield;
 
         is(lockedFile.location, location, "Correct location");
 
         request = lockedFile.getMetadata({ size: true });
         request.onsuccess = grabEventAndContinueHandler;
         event = yield;
 
         is(event.target.result.size, location, "Correct size");
       }
+
+      request = lockedFile.write(testBuffer);
+      request.onsuccess = grabEventAndContinueHandler;
+      event = yield;
+
+      let location = lockedFile.location;
+      for (let i = 0; i < 10; i++) {
+        location -= 10000;
+
+        request = lockedFile.truncate(location);
+        request.onsuccess = grabEventAndContinueHandler;
+        event = yield;
+
+        is(lockedFile.location, location, "Correct location");
+
+        request = lockedFile.getMetadata({ size: true });
+        request.onsuccess = grabEventAndContinueHandler;
+        event = yield;
+
+        is(event.target.result.size, location, "Correct size");
+      }
     }
 
     finishTest();
     yield;
   }
   </script>
   <script type="text/javascript;version=1.7" src="helpers.js"></script>