Bug 1046240 - Expose the principal for a blob URI for chrome-only js code. r=smaug

This commit is contained in:
Andrea Marchesini 2014-08-13 08:29:00 -04:00
parent e984d4c73c
commit 6aa8d79e5d
7 changed files with 54 additions and 0 deletions

View File

@ -203,6 +203,16 @@ URL::RevokeObjectURL(const GlobalObject& aGlobal, const nsAString& aURL)
}
}
nsIPrincipal*
URL::GetPrincipalFromURL(const GlobalObject& aGlobal, const nsAString& aURL,
ErrorResult& aRv)
{
MOZ_ASSERT(nsContentUtils::IsCallerChrome());
NS_LossyConvertUTF16toASCII asciiurl(aURL);
return nsHostObjectProtocolHandler::GetDataEntryPrincipal(asciiurl);
}
void
URL::GetHref(nsString& aHref, ErrorResult& aRv) const
{

View File

@ -12,6 +12,7 @@
#include "nsString.h"
class nsIDOMBlob;
class nsIPrincipal;
class nsISupports;
class nsIURI;
@ -68,6 +69,9 @@ public:
ErrorResult& aError);
static void RevokeObjectURL(const GlobalObject& aGlobal,
const nsAString& aURL);
static nsIPrincipal* GetPrincipalFromURL(const GlobalObject& aGlobal,
const nsAString& aURL,
ErrorResult& aError);
void GetHref(nsString& aHref, ErrorResult& aRv) const;

View File

@ -13,6 +13,10 @@ this.checkFromJSM = function checkFromJSM(ok, is) {
var url = URL.createObjectURL(blob);
ok(url, "URL is created!");
var p = URL.getPrincipalFromURL(url);
ok(p, "Principal exists.");
ok(p instanceof Components.interfaces.nsIPrincipal, "Principal is a nsIPrincipal");
URL.revokeObjectURL(url);
ok(true, "URL is revoked");
}

View File

@ -319,5 +319,19 @@
url.hostname = "2001::1";
is(url.hostname, "localhost", "Setting bad hostname fails");
</script>
<script>
var blob = new Blob(['a']);
var url = URL.createObjectURL(blob);
ok(!('getPrincipalFromURL' in URL), "URL.getPrincipalFromURL is not exposed");
var fakeP = SpecialPowers.wrap(URL).getPrincipalFromURL("hello world!");
ok(!fakeP, "Principal doesn't exists.");
var p = SpecialPowers.wrap(URL).getPrincipalFromURL(url);
ok(p, "Principal exists.");
ok(p.URI, "Principal.URI exists.");
is(p.URI.spec, window.location.href, "Principal.URI is correct");
</script>
</body>
</html>

View File

@ -38,3 +38,9 @@ partial interface URL {
[Throws]
static DOMString? createObjectURL(MediaSource source, optional objectURLOptions options);
};
// mozilla extensions
partial interface URL {
[Throws, ChromeOnly]
static Principal getPrincipalFromURL(DOMString blobURL);
};

View File

@ -893,6 +893,16 @@ URL::RevokeObjectURL(const GlobalObject& aGlobal, const nsAString& aUrl)
}
}
// static
nsIPrincipal*
URL::GetPrincipalFromURL(const GlobalObject& aGlobal, const nsAString& aUrl,
ErrorResult& aRv)
{
// This method is not implemented in workers.
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
return nullptr;
}
void
URL::URLSearchParamsUpdated(URLSearchParams* aSearchParams)
{

View File

@ -13,6 +13,8 @@
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/URLSearchParams.h"
class nsIPrincipal;
namespace mozilla {
namespace dom {
struct objectURLOptions;
@ -67,6 +69,10 @@ public:
static void
RevokeObjectURL(const GlobalObject& aGlobal, const nsAString& aUrl);
static nsIPrincipal* GetPrincipalFromURL(const GlobalObject& aGlobal,
const nsAString& aURL,
ErrorResult& aError);
void GetHref(nsString& aHref, ErrorResult& aRv) const;
void SetHref(const nsAString& aHref, ErrorResult& aRv);