Bug 596385: Update syntax for getting an url for a File to latest spec. r=jst a=blocker

This commit is contained in:
Jonas Sicking 2010-09-15 15:52:49 -07:00
Родитель 0889c46542
Коммит 93b4f19090
6 изменённых файлов: 83 добавлений и 17 удалений

Просмотреть файл

@ -1420,12 +1420,13 @@ public:
virtual nsISupports* GetCurrentContentSink() = 0;
/**
* Register a filedata uri as being "owned" by this document. I.e. that its
* lifetime is connected with this document. When the document goes away it
* should "kill" the uri by calling
* Register/Unregister a filedata uri as being "owned" by this document.
* I.e. that its lifetime is connected with this document. When the document
* goes away it should "kill" the uri by calling
* nsFileDataProtocolHandler::RemoveFileDataEntry
*/
virtual void RegisterFileDataUri(nsACString& aUri) = 0;
virtual void RegisterFileDataUri(const nsACString& aUri) = 0;
virtual void UnregisterFileDataUri(const nsACString& aUri) = 0;
virtual void SetScrollToRef(nsIURI *aDocumentURI) = 0;
virtual void ScrollToRef() = 0;

Просмотреть файл

@ -7910,11 +7910,17 @@ nsDocument::GetCurrentContentSink()
}
void
nsDocument::RegisterFileDataUri(nsACString& aUri)
nsDocument::RegisterFileDataUri(const nsACString& aUri)
{
mFileDataUris.AppendElement(aUri);
}
void
nsDocument::UnregisterFileDataUri(const nsACString& aUri)
{
mFileDataUris.RemoveElement(aUri);
}
void
nsDocument::SetScrollToRef(nsIURI *aDocumentURI)
{

Просмотреть файл

@ -956,7 +956,8 @@ public:
virtual PRInt32 GetDocumentState();
virtual void RegisterFileDataUri(nsACString& aUri);
virtual void RegisterFileDataUri(const nsACString& aUri);
virtual void UnregisterFileDataUri(const nsACString& aUri);
// Only BlockOnload should call this!
void AsyncBlockOnload();

Просмотреть файл

@ -2,7 +2,7 @@
<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<title>Test for Cross Site XMLHttpRequest</title>
<title>Test for File urls</title>
<script type="text/javascript" src="/MochiKit/packed.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
@ -38,16 +38,33 @@ function runTest() {
// Attempt to load a image in this document
var file = getFile("file_bug543870_img.jpg");
img.src = file.url;
var fileurl = createBlobURL(file);
img.src = fileurl;
var e = (yield);
is(e.type, "load", "loaded successfully");
is(img.width, 120, "correct width");
is(img.height, 90, "correct height");
// Revoke url and attempt to load a image in this document
img.src = "file_bug543870_img.jpg";
is((yield).type, "load", "successfull reset image");
revokeBlobURL(fileurl);
todo(false, "urls need to act like 404s, not fail to parse");
/* img.src = fileurl;
var e = (yield);
is(e.type, "error", "failed successfully");
isnot(img.width, 120, "correct error width");
isnot(img.height, 90, "correct error height");
*/
// Generate new fileurl and make sure it's different from the old
var oldFileurl = fileurl;
var fileurl = createBlobURL(file);
isnot(fileurl, oldFileurl, "createBlobURL generated the same url twice");
// Attempt to load an image in a different same-origin document
inner.src = innerSameSiteURI;
yield;
inner.contentWindow.postMessage(JSON.stringify({img:file.url}), "*");
inner.contentWindow.postMessage(JSON.stringify({img:fileurl}), "*");
var res = (yield);
is(res.type, "load", "loaded successfully");
is(res.width, 120, "correct width");
@ -56,7 +73,7 @@ function runTest() {
// Attempt to load an image in a different cross-origin document
inner.src = innerCrossSiteURI;
yield;
inner.contentWindow.postMessage(JSON.stringify({img:file.url}), "*");
inner.contentWindow.postMessage(JSON.stringify({img:fileurl}), "*");
var res = (yield);
is(res.type, "error", "failed successfully");
isnot(res.width, 120, "correct error width");
@ -76,7 +93,8 @@ function runTest() {
// Attempt to load a HTML document in an iframe in this document, using file url
file = getFile("file_bug543870_doc.html");
iframe.src = file.url;
fileurl = createBlobURL(file);
iframe.src = fileurl;
yield;
is(iframe.contentDocument.getElementsByTagName("p")[0].textContent,
"This here is a document!",
@ -96,7 +114,7 @@ function runTest() {
is(res.imgWidth, 120, "correct width");
// Attempt to load a HTML document in an iframe in inner document, using file url
inner.contentWindow.postMessage(JSON.stringify({iframe:file.url}), "*");
inner.contentWindow.postMessage(JSON.stringify({iframe:fileurl}), "*");
var res = (yield);
is(res.type, "load", "loaded successfully");
is(res.text, "This here is a document!", "loaded successfully");
@ -105,15 +123,16 @@ function runTest() {
// Attempt to load a HTML document in an iframe in inner cross-site document, using file url
inner.src = innerCrossSiteURI;
is((yield), "inner loaded", "correct gen.next()");
inner.contentWindow.postMessage(JSON.stringify({iframe:file.url}), "*");
inner.contentWindow.postMessage(JSON.stringify({iframe:fileurl}), "*");
var res = (yield);
is(res.type, "error", "load failed successfully");
// Attempt to load file url using XHR
file = getFile("file_bug543870_text.txt");
fileurl = createBlobURL(file);
xhr = new XMLHttpRequest;
xhr.onload = function() { gen.send("XHR finished"); };
xhr.open("GET", file.url);
xhr.open("GET", fileurl);
xhr.send();
is((yield), "XHR finished", "correct gen.next()");
xhr.responseText == "Yarr, here be plaintext file, ya landlubber\n";
@ -121,7 +140,7 @@ function runTest() {
// Attempt to load file url using XHR in inner document
inner.src = innerSameSiteURI;
is((yield), "inner loaded", "correct gen.next()");
inner.contentWindow.postMessage(JSON.stringify({xhr:file.url}), "*");
inner.contentWindow.postMessage(JSON.stringify({xhr:fileurl}), "*");
var res = (yield);
is(res.didThrow, undefined, "load successful");
is(res.text, "Yarr, here be plaintext file, ya landlubber\n", "load successful");
@ -129,7 +148,7 @@ function runTest() {
// Attempt to load file url using XHR
inner.src = innerCrossSiteURI;
is((yield), "inner loaded", "correct gen.next()");
inner.contentWindow.postMessage(JSON.stringify({xhr:file.url}), "*");
inner.contentWindow.postMessage(JSON.stringify({xhr:fileurl}), "*");
var res = (yield);
is(res.didThrow, true, "load failed successfully");

Просмотреть файл

@ -168,6 +168,8 @@
#include "nsAutoPtr.h"
#include "nsContentUtils.h"
#include "nsCSSProps.h"
#include "nsFileDataProtocolHandler.h"
#include "nsIDOMFile.h"
#include "nsIURIFixup.h"
#include "mozilla/FunctionTimer.h"
#include "nsCDefaultURIFixup.h"
@ -2947,6 +2949,35 @@ nsGlobalWindow::GetApplicationCache(nsIDOMOfflineResourceList **aApplicationCach
return NS_OK;
}
NS_IMETHODIMP
nsGlobalWindow::CreateBlobURL(nsIDOMFile* aFile, nsAString& aURL)
{
FORWARD_TO_INNER(CreateBlobURL, (aFile, aURL), NS_ERROR_UNEXPECTED);
NS_ENSURE_STATE(mDoc);
nsresult rv = aFile->GetInternalUrl(aURL);
NS_ENSURE_SUCCESS(rv, rv);
mDoc->RegisterFileDataUri(NS_LossyConvertUTF16toASCII(aURL));
return NS_OK;
}
NS_IMETHODIMP
nsGlobalWindow::RevokeBlobURL(const nsAString& aURL)
{
FORWARD_TO_INNER(RevokeBlobURL, (aURL), NS_ERROR_UNEXPECTED);
NS_ENSURE_STATE(mDoc);
NS_LossyConvertUTF16toASCII asciiurl(aURL);
mDoc->UnregisterFileDataUri(asciiurl);
nsFileDataProtocolHandler::RemoveFileDataEntry(asciiurl);
return NS_OK;
}
NS_IMETHODIMP
nsGlobalWindow::GetCrypto(nsIDOMCrypto** aCrypto)
{

Просмотреть файл

@ -38,8 +38,9 @@
#include "nsIDOMWindow.idl"
interface nsIDOMOfflineResourceList;
interface nsIDOMFile;
[scriptable, uuid(73c5fa35-3add-4c87-a303-a850ccf4d65a)]
[scriptable, uuid(63239526-27b0-44ff-be4e-ee1c8ee2212e)]
interface nsIDOMWindow2 : nsIDOMWindow
{
/**
@ -53,4 +54,11 @@ interface nsIDOMWindow2 : nsIDOMWindow
* Get the application cache object for this window.
*/
readonly attribute nsIDOMOfflineResourceList applicationCache;
/**
* Create and revoke blob urls. createBlobURL will always return a new URL
* with the lifetime of the current Document.
*/
DOMString createBlobURL(in nsIDOMFile file);
void revokeBlobURL(in DOMString URL);
};