From 6aa8d79e5d74ed91b9d30cc5a4a20d76ea738ea6 Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Wed, 13 Aug 2014 08:29:00 -0400 Subject: [PATCH] Bug 1046240 - Expose the principal for a blob URI for chrome-only js code. r=smaug --- dom/base/URL.cpp | 10 ++++++++++ dom/base/URL.h | 4 ++++ dom/base/test/file_url.jsm | 4 ++++ dom/base/test/test_url.html | 14 ++++++++++++++ dom/webidl/URL.webidl | 6 ++++++ dom/workers/URL.cpp | 10 ++++++++++ dom/workers/URL.h | 6 ++++++ 7 files changed, 54 insertions(+) diff --git a/dom/base/URL.cpp b/dom/base/URL.cpp index 7a73c265e7e4..354ad29412f5 100644 --- a/dom/base/URL.cpp +++ b/dom/base/URL.cpp @@ -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 { diff --git a/dom/base/URL.h b/dom/base/URL.h index 23cfa85e0d8b..6f5315976865 100644 --- a/dom/base/URL.h +++ b/dom/base/URL.h @@ -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; diff --git a/dom/base/test/file_url.jsm b/dom/base/test/file_url.jsm index 5a83b798b685..ec0a5a4f7b3c 100644 --- a/dom/base/test/file_url.jsm +++ b/dom/base/test/file_url.jsm @@ -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"); } diff --git a/dom/base/test/test_url.html b/dom/base/test/test_url.html index 7b7e8ba4bd63..09af8cf32dee 100644 --- a/dom/base/test/test_url.html +++ b/dom/base/test/test_url.html @@ -319,5 +319,19 @@ url.hostname = "2001::1"; is(url.hostname, "localhost", "Setting bad hostname fails"); + + diff --git a/dom/webidl/URL.webidl b/dom/webidl/URL.webidl index 042fcbae6a80..66e7d0496034 100644 --- a/dom/webidl/URL.webidl +++ b/dom/webidl/URL.webidl @@ -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); +}; diff --git a/dom/workers/URL.cpp b/dom/workers/URL.cpp index 9a44abb0fc8a..cc937ae48554 100644 --- a/dom/workers/URL.cpp +++ b/dom/workers/URL.cpp @@ -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) { diff --git a/dom/workers/URL.h b/dom/workers/URL.h index 7a7c47559828..10897194c44a 100644 --- a/dom/workers/URL.h +++ b/dom/workers/URL.h @@ -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);