Skip to content

Commit

Permalink
Bug 809727 - Wait for the write transaction to finish so that the blo…
Browse files Browse the repository at this point in the history
…bs are expired. r=janv
  • Loading branch information
philikon committed Nov 10, 2012
1 parent 96a67b8 commit cf52633
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
13 changes: 13 additions & 0 deletions content/base/public/nsDOMFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,19 @@ class nsDOMFileFile : public nsDOMFile,
mFile->GetLeafName(mName);
}

nsDOMFileFile(nsIFile *aFile, FileInfo *aFileInfo)
: nsDOMFile(EmptyString(), EmptyString(), UINT64_MAX, UINT64_MAX),
mFile(aFile), mWholeFile(true), mStoredFile(true)
{
NS_ASSERTION(mFile, "must have file");
NS_ASSERTION(aFileInfo, "must have file info");
// Lazily get the content type and size
mContentType.SetIsVoid(true);
mFile->GetLeafName(mName);

mFileInfos.AppendElement(aFileInfo);
}

// Create as a file
nsDOMFileFile(const nsAString& aName, const nsAString& aContentType,
uint64_t aLength, nsIFile *aFile)
Expand Down
2 changes: 1 addition & 1 deletion dom/indexedDB/IDBObjectStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,7 @@ IDBObjectStore::ConvertBlobsToActors(
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
}

nsCOMPtr<nsIDOMBlob> blob = new nsDOMFileFile(nativeFile);
nsCOMPtr<nsIDOMBlob> blob = new nsDOMFileFile(nativeFile, file.mFileInfo);

BlobParent* actor =
aContentParent->GetOrCreateActorForBlob(blob);
Expand Down
61 changes: 58 additions & 3 deletions dom/indexedDB/test/test_blob_simple.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
<script type="text/javascript;version=1.7">
function testSteps()
{
const BLOB_DATA = ["fun ", "times ", "all ", "around!"];
const INDEX_KEY = 5;
info("Setting up test fixtures: create an IndexedDB database and object store.");

let request = indexedDB.open(window.location.pathname, 1);
request.onerror = errorHandler;
Expand All @@ -30,6 +29,11 @@
request.onsuccess = grabEventAndContinueHandler;
event = yield;


info("Let's create a blob and store it in IndexedDB twice.");

const BLOB_DATA = ["fun ", "times ", "all ", "around!"];
const INDEX_KEY = 5;
let blob = new Blob(BLOB_DATA, { type: "text/plain" });
let data = { blob: blob, index: INDEX_KEY };

Expand All @@ -42,6 +46,9 @@
objectStore.add(data).onsuccess = grabEventAndContinueHandler;
event = yield;


info("Let's retrieve the blob again and verify the contents is the same.");

objectStore = db.transaction("foo").objectStore("foo");
objectStore.get(key).onsuccess = grabEventAndContinueHandler;
event = yield;
Expand All @@ -53,7 +60,9 @@

is(event.target.result, BLOB_DATA.join(""), "Correct text");

info("Trying blob url");

info("Let's retrieve it again, create an object URL for the blob, load" +
"it via an XMLHttpRequest, and verify the contents is the same.");

objectStore = db.transaction("foo").objectStore("foo");
objectStore.get(key).onsuccess = grabEventAndContinueHandler;
Expand All @@ -71,6 +80,9 @@

is(xhr.responseText, BLOB_DATA.join(""), "Correct responseText");


info("Retrieve both blob entries from the database and verify contents.");

objectStore = db.transaction("foo").objectStore("foo");
objectStore.mozGetAll().onsuccess = grabEventAndContinueHandler;
event = yield;
Expand Down Expand Up @@ -108,6 +120,9 @@

is(event.target.result, BLOB_DATA.join(""), "Correct text");


info("Retrieve blobs from database via index and verify contents.");

let index = db.transaction("foo").objectStore("foo").index("foo");
index.get(INDEX_KEY).onsuccess = grabEventAndContinueHandler;
event = yield;
Expand Down Expand Up @@ -163,6 +178,9 @@

is(event.target.result, BLOB_DATA.join(""), "Correct text");


info("Slice the the retrieved blob and verify its contents.");

let slice = cursorResults[1].blob.slice(0, BLOB_DATA[0].length);

fileReader = new FileReader();
Expand All @@ -172,6 +190,9 @@

is(event.target.result, BLOB_DATA[0], "Correct text");


info("Send blob to a worker, read its contents there, and verify results.");

function workerScript() {
onmessage = function(event) {
var reader = new FileReaderSync();
Expand All @@ -196,6 +217,40 @@

is(event.data, BLOB_DATA[0][1], "Correct text");


info("Store a blob back in the database, and keep holding on to the " +
"blob, verifying that it still can be read.");

objectStore = db.transaction("foo").objectStore("foo");
objectStore.get(key).onsuccess = grabEventAndContinueHandler;
event = yield;

let blobFromDB = event.target.result.blob;
let txn = db.transaction("foo", "readwrite");
txn.objectStore("foo").put(event.target.result, key);
txn.oncomplete = grabEventAndContinueHandler;
event = yield;

let fileReader = new FileReader();
fileReader.onload = grabEventAndContinueHandler;
fileReader.readAsText(blobFromDB);
event = yield;

is(event.target.result, BLOB_DATA.join(""), "Correct text");

let blobURL = URL.createObjectURL(blobFromDB);

let xhr = new XMLHttpRequest();
xhr.open("GET", blobURL);
xhr.onload = grabEventAndContinueHandler;
xhr.send();
yield;

URL.revokeObjectURL(blobURL);

is(xhr.responseText, BLOB_DATA.join(""), "Correct responseText");


finishTest();
yield;
}
Expand Down

0 comments on commit cf52633

Please sign in to comment.