michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsHostObjectURI.h" michael@0: michael@0: #include "nsAutoPtr.h" michael@0: #include "nsIObjectInputStream.h" michael@0: #include "nsIObjectOutputStream.h" michael@0: #include "nsIProgrammingLanguage.h" michael@0: michael@0: static NS_DEFINE_CID(kHOSTOBJECTURICID, NS_HOSTOBJECTURI_CID); michael@0: michael@0: static NS_DEFINE_CID(kThisSimpleURIImplementationCID, michael@0: NS_THIS_SIMPLEURI_IMPLEMENTATION_CID); michael@0: michael@0: NS_IMPL_ADDREF_INHERITED(nsHostObjectURI, nsSimpleURI) michael@0: NS_IMPL_RELEASE_INHERITED(nsHostObjectURI, nsSimpleURI) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN(nsHostObjectURI) michael@0: NS_INTERFACE_MAP_ENTRY(nsIURIWithPrincipal) michael@0: if (aIID.Equals(kHOSTOBJECTURICID)) michael@0: foundInterface = static_cast(this); michael@0: else if (aIID.Equals(kThisSimpleURIImplementationCID)) { michael@0: // Need to return explicitly here, because if we just set foundInterface michael@0: // to null the NS_INTERFACE_MAP_END_INHERITING will end up calling into michael@0: // nsSimplURI::QueryInterface and finding something for this CID. michael@0: *aInstancePtr = nullptr; michael@0: return NS_NOINTERFACE; michael@0: } michael@0: else michael@0: NS_INTERFACE_MAP_END_INHERITING(nsSimpleURI) michael@0: michael@0: // nsIURIWithPrincipal methods: michael@0: michael@0: NS_IMETHODIMP michael@0: nsHostObjectURI::GetPrincipal(nsIPrincipal** aPrincipal) michael@0: { michael@0: NS_IF_ADDREF(*aPrincipal = mPrincipal); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHostObjectURI::GetPrincipalUri(nsIURI** aUri) michael@0: { michael@0: if (mPrincipal) { michael@0: mPrincipal->GetURI(aUri); michael@0: } michael@0: else { michael@0: *aUri = nullptr; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: // nsISerializable methods: michael@0: michael@0: NS_IMETHODIMP michael@0: nsHostObjectURI::Read(nsIObjectInputStream* aStream) michael@0: { michael@0: nsresult rv = nsSimpleURI::Read(aStream); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr supports; michael@0: rv = NS_ReadOptionalObject(aStream, true, getter_AddRefs(supports)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: mPrincipal = do_QueryInterface(supports, &rv); michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHostObjectURI::Write(nsIObjectOutputStream* aStream) michael@0: { michael@0: nsresult rv = nsSimpleURI::Write(aStream); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return NS_WriteOptionalCompoundObject(aStream, mPrincipal, michael@0: NS_GET_IID(nsIPrincipal), michael@0: true); michael@0: } michael@0: michael@0: // nsIURI methods: michael@0: nsresult michael@0: nsHostObjectURI::CloneInternal(nsSimpleURI::RefHandlingEnum aRefHandlingMode, michael@0: nsIURI** aClone) michael@0: { michael@0: nsCOMPtr simpleClone; michael@0: nsresult rv = michael@0: nsSimpleURI::CloneInternal(aRefHandlingMode, getter_AddRefs(simpleClone)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: #ifdef DEBUG michael@0: nsRefPtr uriCheck; michael@0: rv = simpleClone->QueryInterface(kHOSTOBJECTURICID, getter_AddRefs(uriCheck)); michael@0: NS_ABORT_IF_FALSE(NS_SUCCEEDED(rv) && uriCheck, michael@0: "Unexpected!"); michael@0: #endif michael@0: michael@0: nsHostObjectURI* u = static_cast(simpleClone.get()); michael@0: michael@0: u->mPrincipal = mPrincipal; michael@0: michael@0: simpleClone.forget(aClone); michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* virtual */ nsresult michael@0: nsHostObjectURI::EqualsInternal(nsIURI* aOther, michael@0: nsSimpleURI::RefHandlingEnum aRefHandlingMode, michael@0: bool* aResult) michael@0: { michael@0: if (!aOther) { michael@0: *aResult = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsRefPtr otherUri; michael@0: aOther->QueryInterface(kHOSTOBJECTURICID, getter_AddRefs(otherUri)); michael@0: if (!otherUri) { michael@0: *aResult = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Compare the member data that our base class knows about. michael@0: if (!nsSimpleURI::EqualsInternal(otherUri, aRefHandlingMode)) { michael@0: *aResult = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Compare the piece of additional member data that we add to base class. michael@0: if (mPrincipal && otherUri->mPrincipal) { michael@0: // Both of us have mPrincipals. Compare them. michael@0: return mPrincipal->Equals(otherUri->mPrincipal, aResult); michael@0: } michael@0: // else, at least one of us lacks a principal; only equal if *both* lack it. michael@0: *aResult = (!mPrincipal && !otherUri->mPrincipal); michael@0: return NS_OK; michael@0: } michael@0: michael@0: // nsIClassInfo methods: michael@0: NS_IMETHODIMP michael@0: nsHostObjectURI::GetInterfaces(uint32_t *count, nsIID * **array) michael@0: { michael@0: *count = 0; michael@0: *array = nullptr; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHostObjectURI::GetHelperForLanguage(uint32_t language, nsISupports **_retval) michael@0: { michael@0: *_retval = nullptr; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHostObjectURI::GetContractID(char * *aContractID) michael@0: { michael@0: // Make sure to modify any subclasses as needed if this ever michael@0: // changes. michael@0: *aContractID = nullptr; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHostObjectURI::GetClassDescription(char * *aClassDescription) michael@0: { michael@0: *aClassDescription = nullptr; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHostObjectURI::GetClassID(nsCID * *aClassID) michael@0: { michael@0: // Make sure to modify any subclasses as needed if this ever michael@0: // changes to not call the virtual GetClassIDNoAlloc. michael@0: *aClassID = (nsCID*) nsMemory::Alloc(sizeof(nsCID)); michael@0: NS_ENSURE_TRUE(*aClassID, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: return GetClassIDNoAlloc(*aClassID); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHostObjectURI::GetImplementationLanguage(uint32_t *aImplementationLanguage) michael@0: { michael@0: *aImplementationLanguage = nsIProgrammingLanguage::CPLUSPLUS; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHostObjectURI::GetFlags(uint32_t *aFlags) michael@0: { michael@0: *aFlags = nsIClassInfo::MAIN_THREAD_ONLY; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHostObjectURI::GetClassIDNoAlloc(nsCID *aClassIDNoAlloc) michael@0: { michael@0: *aClassIDNoAlloc = kHOSTOBJECTURICID; michael@0: return NS_OK; michael@0: }