/*static */already_AddRefed<nsDOMParser> nsDOMParser::Constructor(const GlobalObject& aOwner, ErrorResult& rv) { nsCOMPtr<nsIPrincipal> prin; nsCOMPtr<nsIURI> documentURI; nsCOMPtr<nsIURI> baseURI; // No arguments; use the subject principal nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager(); if (!secMan) { rv.Throw(NS_ERROR_UNEXPECTED); return nullptr; } rv = secMan->GetSubjectPrincipal(getter_AddRefs(prin)); if (rv.Failed()) { return nullptr; } // We're called from JS; there better be a subject principal, really. if (!prin) { rv.Throw(NS_ERROR_UNEXPECTED); return nullptr; } nsRefPtr<nsDOMParser> domParser = new nsDOMParser(aOwner.Get()); rv = domParser->InitInternal(aOwner.Get(), prin, documentURI, baseURI); if (rv.Failed()) { return nullptr; } return domParser.forget(); }
void URL::CreateObjectURLInternal(const GlobalObject& aGlobal, nsISupports* aObject, const nsACString& aScheme, const objectURLOptions& aOptions, nsAString& aResult, ErrorResult& aRv) { nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports()); if (!global) { aRv.Throw(NS_ERROR_FAILURE); return; } nsCOMPtr<nsIPrincipal> principal = nsContentUtils::ObjectPrincipal(aGlobal.Get()); nsAutoCString url; nsresult rv = nsHostObjectProtocolHandler::AddDataEntry(aScheme, aObject, principal, url); if (NS_FAILED(rv)) { aRv.Throw(rv); return; } global->RegisterHostObjectURI(url); CopyASCIItoUTF16(url, aResult); }
/* static */ void URLMainThread::CreateObjectURL(const GlobalObject& aGlobal, MediaSource& aSource, const objectURLOptions& aOptions, nsAString& aResult, ErrorResult& aRv) { MOZ_ASSERT(NS_IsMainThread()); nsCOMPtr<nsIPrincipal> principal = nsContentUtils::ObjectPrincipal(aGlobal.Get()); nsAutoCString url; aRv = nsHostObjectProtocolHandler::AddDataEntry(&aSource, principal, url); if (NS_WARN_IF(aRv.Failed())) { return; } nsCOMPtr<nsIRunnable> revocation = NS_NewRunnableFunction( [url] { nsHostObjectProtocolHandler::RemoveDataEntry(url); }); nsContentUtils::RunInStableState(revocation.forget()); CopyASCIItoUTF16(url, aResult); }
// static already_AddRefed<HTMLImageElement> HTMLImageElement::Image(const GlobalObject& aGlobal, const Optional<uint32_t>& aWidth, const Optional<uint32_t>& aHeight, ErrorResult& aError) { nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(aGlobal.Get()); nsIDocument* doc; if (!win || !(doc = win->GetExtantDoc())) { aError.Throw(NS_ERROR_FAILURE); return nullptr; } nsCOMPtr<nsINodeInfo> nodeInfo = doc->NodeInfoManager()->GetNodeInfo(nsGkAtoms::img, nullptr, kNameSpaceID_XHTML, nsIDOMNode::ELEMENT_NODE); nsRefPtr<HTMLImageElement> img = new HTMLImageElement(nodeInfo.forget()); if (aWidth.WasPassed()) { img->SetWidth(aWidth.Value(), aError); if (aError.Failed()) { return nullptr; } if (aHeight.WasPassed()) { img->SetHeight(aHeight.Value(), aError); if (aError.Failed()) { return nullptr; } } } return img.forget(); }
void URL::CreateObjectURLInternal(const GlobalObject& aGlobal, nsISupports* aObject, const nsACString& aScheme, const objectURLOptions& aOptions, nsString& aResult, ErrorResult& aError) { nsCOMPtr<nsIPrincipal> principal = nsContentUtils::GetObjectPrincipal(aGlobal.Get()); nsCString url; nsresult rv = nsHostObjectProtocolHandler::AddDataEntry(aScheme, aObject, principal, url); if (NS_FAILED(rv)) { aError.Throw(rv); return; } nsCOMPtr<nsPIDOMWindow> w = do_QueryInterface(aGlobal.GetAsSupports()); nsGlobalWindow* window = static_cast<nsGlobalWindow*>(w.get()); if (window) { NS_PRECONDITION(window->IsInnerWindow(), "Should be inner window"); if (!window->GetExtantDoc()) { aError.Throw(NS_ERROR_INVALID_POINTER); return; } nsIDocument* doc = window->GetExtantDoc(); if (doc) { doc->RegisterHostObjectUri(url); } } CopyASCIItoUTF16(url, aResult); }
void URL::CreateObjectURL(const GlobalObject& aGlobal, MediaSource& aSource, const objectURLOptions& aOptions, nsAString& aResult, ErrorResult& aError) { nsCOMPtr<nsIPrincipal> principal = nsContentUtils::ObjectPrincipal(aGlobal.Get()); nsCString url; nsresult rv = nsHostObjectProtocolHandler:: AddDataEntry(NS_LITERAL_CSTRING(MEDIASOURCEURI_SCHEME), &aSource, principal, url); if (NS_FAILED(rv)) { aError.Throw(rv); return; } nsCOMPtr<nsIRunnable> revocation = NS_NewRunnableFunction( [url] { nsHostObjectProtocolHandler::RemoveDataEntry(url); }); nsContentUtils::RunInStableState(revocation.forget()); CopyASCIItoUTF16(url, aResult); }
/* static */ already_AddRefed<Promise> Promise::Constructor(const GlobalObject& aGlobal, JSContext* aCx, PromiseInit& aInit, ErrorResult& aRv) { MOZ_ASSERT(PrefEnabled()); nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.Get()); if (!window) { aRv.Throw(NS_ERROR_UNEXPECTED); return nullptr; } nsRefPtr<Promise> promise = new Promise(window); aInit.Call(promise, *promise->mResolver, aRv, CallbackObject::eRethrowExceptions); aRv.WouldReportJSException(); if (aRv.IsJSException()) { Optional<JS::Handle<JS::Value> > value(aCx); aRv.StealJSException(aCx, &value.Value()); Maybe<JSAutoCompartment> ac; EnterCompartment(ac, aCx, value); promise->mResolver->Reject(aCx, value); } return promise.forget(); }
/*static */already_AddRefed<nsDOMParser> nsDOMParser::Constructor(const GlobalObject& aOwner, nsIPrincipal* aPrincipal, nsIURI* aDocumentURI, nsIURI* aBaseURI, ErrorResult& rv) { if (!nsContentUtils::IsCallerChrome()) { rv.Throw(NS_ERROR_DOM_SECURITY_ERR); return nullptr; } nsRefPtr<nsDOMParser> domParser = new nsDOMParser(aOwner.Get()); rv = domParser->InitInternal(aOwner.Get(), aPrincipal, aDocumentURI, aBaseURI); if (rv.Failed()) { return nullptr; } return domParser.forget(); }
/* static */ already_AddRefed<MediaRecorder> MediaRecorder::Constructor(const GlobalObject& aGlobal, JSContext* aCx, DOMMediaStream& aStream, ErrorResult& aRv) { nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(aGlobal.Get()); if (!sgo) { aRv.Throw(NS_ERROR_FAILURE); return nullptr; } nsCOMPtr<nsPIDOMWindow> ownerWindow = do_QueryInterface(aGlobal.Get()); if (!ownerWindow) { aRv.Throw(NS_ERROR_FAILURE); return nullptr; } nsRefPtr<MediaRecorder> object = new MediaRecorder(aStream); object->Init(aCx, ownerWindow); return object.forget(); }
/* static */ already_AddRefed<nsFormData> nsFormData::Constructor(const GlobalObject& aGlobal, const Optional<nsHTMLFormElement*>& aFormElement, ErrorResult& aRv) { nsRefPtr<nsFormData> formData = new nsFormData(aGlobal.Get()); if (aFormElement.WasPassed()) { MOZ_ASSERT(aFormElement.Value()); aRv = aFormElement.Value()->WalkFormElements(formData); } return formData.forget(); }
/* static */ already_AddRefed<Comment> Comment::Constructor(const GlobalObject& aGlobal, const nsAString& aData, ErrorResult& aRv) { nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.Get()); if (!window || !window->GetDoc()) { aRv.Throw(NS_ERROR_FAILURE); return nullptr; } return window->GetDoc()->CreateComment(aData); }
/* static */ bool CSS::Supports(const GlobalObject& aGlobal, const nsAString& aCondition, ErrorResult& aRv) { nsCSSParser parser; SupportsParsingInfo info; nsresult rv = GetParsingInfo(aGlobal.Get(), info); if (NS_FAILED(rv)) { aRv.Throw(rv); return false; } return parser.EvaluateSupportsCondition(aCondition, info.mDocURI, info.mBaseURI, info.mPrincipal); }
/* static */ already_AddRefed<Promise> Promise::Reject(const GlobalObject& aGlobal, JSContext* aCx, JS::Handle<JS::Value> aValue, ErrorResult& aRv) { MOZ_ASSERT(PrefEnabled()); nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.Get()); if (!window) { aRv.Throw(NS_ERROR_UNEXPECTED); return nullptr; } nsRefPtr<Promise> promise = new Promise(window); Optional<JS::Handle<JS::Value> > value(aCx, aValue); promise->mResolver->Reject(aCx, value); return promise.forget(); }
static nsresult GetParsingInfo(const GlobalObject& aGlobal, SupportsParsingInfo& aInfo) { nsGlobalWindow* win = xpc::WindowOrNull(aGlobal.Get()); if (!win) { return NS_ERROR_FAILURE; } nsCOMPtr<nsIDocument> doc = win->GetDoc(); if (!doc) { return NS_ERROR_FAILURE; } aInfo.mDocURI = nsCOMPtr<nsIURI>(doc->GetDocumentURI()); aInfo.mBaseURI = nsCOMPtr<nsIURI>(doc->GetBaseURI()); aInfo.mPrincipal = win->GetPrincipal(); return NS_OK; }
void URL::RevokeObjectURL(const GlobalObject& aGlobal, const nsAString& aURL) { nsIPrincipal* principal = nsContentUtils::GetObjectPrincipal(aGlobal.Get()); NS_LossyConvertUTF16toASCII asciiurl(aURL); nsIPrincipal* urlPrincipal = nsHostObjectProtocolHandler::GetDataEntryPrincipal(asciiurl); if (urlPrincipal && principal->Subsumes(urlPrincipal)) { nsCOMPtr<nsPIDOMWindow> w = do_QueryInterface(aGlobal.GetAsSupports()); nsGlobalWindow* window = static_cast<nsGlobalWindow*>(w.get()); if (window && window->GetExtantDoc()) { window->GetExtantDoc()->UnregisterHostObjectUri(asciiurl); } nsHostObjectProtocolHandler::RemoveDataEntry(asciiurl); } }
USING_FILE_NAMESPACE /* static */ already_AddRefed<ArchiveReader> ArchiveReader::Constructor(const GlobalObject& aGlobal, nsIDOMBlob* aBlob, const ArchiveReaderOptions& aOptions, ErrorResult& aError) { MOZ_ASSERT(aBlob); MOZ_ASSERT(PrefEnabled()); nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.Get()); if (!window) { aError.Throw(NS_ERROR_UNEXPECTED); return nullptr; } nsRefPtr<ArchiveReader> reader = new ArchiveReader(aBlob, window, aOptions.mEncoding); return reader.forget(); }
/* static */ void URLMainThread::RevokeObjectURL(const GlobalObject& aGlobal, const nsAString& aURL, ErrorResult& aRv) { MOZ_ASSERT(NS_IsMainThread()); nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports()); if (!global) { aRv.Throw(NS_ERROR_FAILURE); return; } nsIPrincipal* principal = nsContentUtils::ObjectPrincipal(aGlobal.Get()); NS_LossyConvertUTF16toASCII asciiurl(aURL); nsIPrincipal* urlPrincipal = BlobURLProtocolHandler::GetDataEntryPrincipal(asciiurl); if (urlPrincipal && principal->Subsumes(urlPrincipal)) { global->UnregisterHostObjectURI(asciiurl); BlobURLProtocolHandler::RemoveDataEntry(asciiurl); } }
void CreateObjectURLInternal(const GlobalObject& aGlobal, T aObject, nsAString& aResult, ErrorResult& aRv) { nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports()); if (NS_WARN_IF(!global)) { aRv.Throw(NS_ERROR_FAILURE); return; } nsCOMPtr<nsIPrincipal> principal = nsContentUtils::ObjectPrincipal(aGlobal.Get()); nsAutoCString url; aRv = nsHostObjectProtocolHandler::AddDataEntry(aObject, principal, url); if (NS_WARN_IF(aRv.Failed())) { return; } global->RegisterHostObjectURI(url); CopyASCIItoUTF16(url, aResult); }
/* static */ void URLMainThread::CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob, nsAString& aResult, ErrorResult& aRv) { MOZ_ASSERT(NS_IsMainThread()); nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports()); if (NS_WARN_IF(!global)) { aRv.Throw(NS_ERROR_FAILURE); return; } nsCOMPtr<nsIPrincipal> principal = nsContentUtils::ObjectPrincipal(aGlobal.Get()); nsAutoCString url; aRv = BlobURLProtocolHandler::AddDataEntry(aBlob.Impl(), principal, url); if (NS_WARN_IF(aRv.Failed())) { return; } global->RegisterHostObjectURI(url); CopyASCIItoUTF16(url, aResult); }
/* static */ already_AddRefed<nsDOMFileReader> nsDOMFileReader::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv) { nsRefPtr<nsDOMFileReader> fileReader = new nsDOMFileReader(); nsCOMPtr<nsPIDOMWindow> owner = do_QueryInterface(aGlobal.Get()); if (!owner) { NS_WARNING("Unexpected nsIJSNativeInitializer owner"); aRv.Throw(NS_ERROR_FAILURE); return nullptr; } fileReader->BindToOwner(owner); // This object is bound to a |window|, // so reset the principal. nsCOMPtr<nsIScriptObjectPrincipal> scriptPrincipal = do_QueryInterface(owner); if (!scriptPrincipal) { aRv.Throw(NS_ERROR_FAILURE); return nullptr; } fileReader->mPrincipal = scriptPrincipal->GetPrincipal(); return fileReader.forget(); }
already_AddRefed<DOMWheelEvent> DOMWheelEvent::Constructor(const GlobalObject& aGlobal, const nsAString& aType, const WheelEventInit& aParam, mozilla::ErrorResult& aRv) { nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.Get()); nsRefPtr<DOMWheelEvent> e = new DOMWheelEvent(t, nullptr, nullptr); bool trusted = e->Init(t); nsAutoString modifierList; GetModifierList(aParam.mCtrlKey, aParam.mShiftKey, aParam.mAltKey, aParam.mMetaKey, modifierList); aRv = e->InitWheelEvent(aType, aParam.mBubbles, aParam.mCancelable, aParam.mView, aParam.mDetail, aParam.mScreenX, aParam.mScreenY, aParam.mClientX, aParam.mClientY, aParam.mButton, aParam.mRelatedTarget, modifierList, aParam.mDeltaX, aParam.mDeltaY, aParam.mDeltaZ, aParam.mDeltaMode); static_cast<widget::WheelEvent*>(e->mEvent)->buttons = aParam.mButtons; e->SetTrusted(trusted); return e.forget(); }
already_AddRefed<WebSocket> WebSocket::Constructor(const GlobalObject& aGlobal, JSContext* aCx, const nsAString& aUrl, const Sequence<nsString>& aProtocols, ErrorResult& aRv) { if (!PrefEnabled()) { aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); return nullptr; } nsCOMPtr<nsIScriptObjectPrincipal> scriptPrincipal = do_QueryInterface(aGlobal.Get()); if (!scriptPrincipal) { aRv.Throw(NS_ERROR_FAILURE); return nullptr; } nsCOMPtr<nsIPrincipal> principal = scriptPrincipal->GetPrincipal(); if (!principal) { aRv.Throw(NS_ERROR_FAILURE); return nullptr; } nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(aGlobal.Get()); if (!sgo) { aRv.Throw(NS_ERROR_FAILURE); return nullptr; } nsCOMPtr<nsPIDOMWindow> ownerWindow = do_QueryInterface(aGlobal.Get()); if (!ownerWindow) { aRv.Throw(NS_ERROR_FAILURE); return nullptr; } nsTArray<nsString> protocolArray; for (uint32_t index = 0, len = aProtocols.Length(); index < len; ++index) { const nsString& protocolElement = aProtocols[index]; if (protocolElement.IsEmpty()) { aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR); return nullptr; } if (protocolArray.Contains(protocolElement)) { aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR); return nullptr; } if (protocolElement.FindChar(',') != -1) /* interferes w/list */ { aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR); return nullptr; } protocolArray.AppendElement(protocolElement); } nsRefPtr<WebSocket> webSocket = new WebSocket(); nsresult rv = webSocket->Init(aCx, principal, ownerWindow, aUrl, protocolArray); if (NS_FAILED(rv)) { aRv.Throw(rv); return nullptr; } return webSocket.forget(); }