From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- netwerk/base/LoadContextInfo.cpp | 168 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 netwerk/base/LoadContextInfo.cpp (limited to 'netwerk/base/LoadContextInfo.cpp') diff --git a/netwerk/base/LoadContextInfo.cpp b/netwerk/base/LoadContextInfo.cpp new file mode 100644 index 0000000000..d544bf7275 --- /dev/null +++ b/netwerk/base/LoadContextInfo.cpp @@ -0,0 +1,168 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "LoadContextInfo.h" + +#include "mozilla/dom/ToJSValue.h" +#include "mozilla/StoragePrincipalHelper.h" +#include "nsDocShell.h" +#include "nsIChannel.h" +#include "nsILoadContext.h" +#include "nsIWebNavigation.h" +#include "nsNetUtil.h" + +using namespace mozilla::dom; +namespace mozilla { +namespace net { + +// LoadContextInfo + +NS_IMPL_ISUPPORTS(LoadContextInfo, nsILoadContextInfo) + +LoadContextInfo::LoadContextInfo(bool aIsAnonymous, + OriginAttributes aOriginAttributes) + : mIsAnonymous(aIsAnonymous), + mOriginAttributes(std::move(aOriginAttributes)) {} + +NS_IMETHODIMP LoadContextInfo::GetIsPrivate(bool* aIsPrivate) { + *aIsPrivate = mOriginAttributes.mPrivateBrowsingId > 0; + return NS_OK; +} + +NS_IMETHODIMP LoadContextInfo::GetIsAnonymous(bool* aIsAnonymous) { + *aIsAnonymous = mIsAnonymous; + return NS_OK; +} + +OriginAttributes const* LoadContextInfo::OriginAttributesPtr() { + return &mOriginAttributes; +} + +NS_IMETHODIMP LoadContextInfo::GetOriginAttributes( + JSContext* aCx, JS::MutableHandle aVal) { + if (NS_WARN_IF(!ToJSValue(aCx, mOriginAttributes, aVal))) { + return NS_ERROR_FAILURE; + } + return NS_OK; +} + +// LoadContextInfoFactory + +NS_IMPL_ISUPPORTS(LoadContextInfoFactory, nsILoadContextInfoFactory) + +NS_IMETHODIMP LoadContextInfoFactory::GetDefault( + nsILoadContextInfo** aDefault) { + nsCOMPtr info = + GetLoadContextInfo(false, OriginAttributes()); + info.forget(aDefault); + return NS_OK; +} + +NS_IMETHODIMP LoadContextInfoFactory::GetPrivate( + nsILoadContextInfo** aPrivate) { + OriginAttributes attrs; + attrs.SyncAttributesWithPrivateBrowsing(true); + nsCOMPtr info = GetLoadContextInfo(false, attrs); + info.forget(aPrivate); + return NS_OK; +} + +NS_IMETHODIMP LoadContextInfoFactory::GetAnonymous( + nsILoadContextInfo** aAnonymous) { + nsCOMPtr info = + GetLoadContextInfo(true, OriginAttributes()); + info.forget(aAnonymous); + return NS_OK; +} + +NS_IMETHODIMP LoadContextInfoFactory::Custom( + bool aAnonymous, JS::Handle aOriginAttributes, JSContext* cx, + nsILoadContextInfo** _retval) { + OriginAttributes attrs; + bool status = attrs.Init(cx, aOriginAttributes); + NS_ENSURE_TRUE(status, NS_ERROR_FAILURE); + + nsCOMPtr info = GetLoadContextInfo(aAnonymous, attrs); + info.forget(_retval); + return NS_OK; +} + +NS_IMETHODIMP LoadContextInfoFactory::FromLoadContext( + nsILoadContext* aLoadContext, bool aAnonymous, + nsILoadContextInfo** _retval) { + nsCOMPtr info = + GetLoadContextInfo(aLoadContext, aAnonymous); + info.forget(_retval); + return NS_OK; +} + +NS_IMETHODIMP LoadContextInfoFactory::FromWindow(nsIDOMWindow* aWindow, + bool aAnonymous, + nsILoadContextInfo** _retval) { + nsCOMPtr info = GetLoadContextInfo(aWindow, aAnonymous); + info.forget(_retval); + return NS_OK; +} + +// Helper functions + +LoadContextInfo* GetLoadContextInfo(nsIChannel* aChannel) { + nsresult rv; + + DebugOnly pb = NS_UsePrivateBrowsing(aChannel); + + bool anon = false; + nsLoadFlags loadFlags; + rv = aChannel->GetLoadFlags(&loadFlags); + if (NS_SUCCEEDED(rv)) { + anon = !!(loadFlags & nsIChannel::LOAD_ANONYMOUS); + } + + OriginAttributes oa; + StoragePrincipalHelper::GetOriginAttributesForNetworkState(aChannel, oa); + MOZ_ASSERT(pb == (oa.mPrivateBrowsingId > 0)); + + return new LoadContextInfo(anon, oa); +} + +LoadContextInfo* GetLoadContextInfo(nsILoadContext* aLoadContext, + bool aIsAnonymous) { + if (!aLoadContext) { + return new LoadContextInfo(aIsAnonymous, OriginAttributes()); + } + + OriginAttributes oa; + aLoadContext->GetOriginAttributes(oa); + +#ifdef DEBUG + nsCOMPtr docShell = do_QueryInterface(aLoadContext); + if (!docShell || + nsDocShell::Cast(docShell)->GetBrowsingContext()->IsContent()) { + MOZ_ASSERT(aLoadContext->UsePrivateBrowsing() == + (oa.mPrivateBrowsingId > 0)); + } +#endif + + return new LoadContextInfo(aIsAnonymous, oa); +} + +LoadContextInfo* GetLoadContextInfo(nsIDOMWindow* aWindow, bool aIsAnonymous) { + nsCOMPtr webNav = do_GetInterface(aWindow); + nsCOMPtr loadContext = do_QueryInterface(webNav); + + return GetLoadContextInfo(loadContext, aIsAnonymous); +} + +LoadContextInfo* GetLoadContextInfo(nsILoadContextInfo* aInfo) { + return new LoadContextInfo(aInfo->IsAnonymous(), + *aInfo->OriginAttributesPtr()); +} + +LoadContextInfo* GetLoadContextInfo(bool const aIsAnonymous, + OriginAttributes const& aOriginAttributes) { + return new LoadContextInfo(aIsAnonymous, aOriginAttributes); +} + +} // namespace net +} // namespace mozilla -- cgit v1.2.3