2013-09-20 13:11:25 +04:00
|
|
|
/* 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 "nsIChannel.h"
|
|
|
|
#include "nsILoadContext.h"
|
2015-10-11 19:13:09 +03:00
|
|
|
#include "nsIWebNavigation.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2015-10-11 19:13:09 +03:00
|
|
|
// LoadContextInfo
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(LoadContextInfo, nsILoadContextInfo)
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2015-10-11 19:13:09 +03:00
|
|
|
LoadContextInfo::LoadContextInfo(bool aIsPrivate, bool aIsAnonymous, OriginAttributes aOriginAttributes)
|
|
|
|
: mIsPrivate(aIsPrivate)
|
2013-09-20 13:11:25 +04:00
|
|
|
, mIsAnonymous(aIsAnonymous)
|
2015-10-11 19:13:09 +03:00
|
|
|
, mOriginAttributes(aOriginAttributes)
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadContextInfo::~LoadContextInfo()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP LoadContextInfo::GetIsPrivate(bool *aIsPrivate)
|
|
|
|
{
|
|
|
|
*aIsPrivate = mIsPrivate;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-11 19:13:09 +03:00
|
|
|
NS_IMETHODIMP LoadContextInfo::GetIsAnonymous(bool *aIsAnonymous)
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
2015-10-11 19:13:09 +03:00
|
|
|
*aIsAnonymous = mIsAnonymous;
|
2013-09-20 13:11:25 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-11 19:13:09 +03:00
|
|
|
OriginAttributes const* LoadContextInfo::OriginAttributesPtr()
|
|
|
|
{
|
|
|
|
return &mOriginAttributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP LoadContextInfo::GetOriginAttributes(JSContext *aCx,
|
|
|
|
JS::MutableHandle<JS::Value> aVal)
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
2015-10-11 19:13:09 +03:00
|
|
|
if (NS_WARN_IF(!ToJSValue(aCx, mOriginAttributes, aVal))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-11 19:13:09 +03:00
|
|
|
// LoadContextInfoFactory
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(LoadContextInfoFactory, nsILoadContextInfoFactory)
|
|
|
|
|
|
|
|
NS_IMETHODIMP LoadContextInfoFactory::GetDefault(nsILoadContextInfo * *aDefault)
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
2015-10-11 19:13:09 +03:00
|
|
|
nsCOMPtr<nsILoadContextInfo> info = GetLoadContextInfo(false, false, OriginAttributes());
|
|
|
|
info.forget(aDefault);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP LoadContextInfoFactory::GetPrivate(nsILoadContextInfo * *aPrivate)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsILoadContextInfo> info = GetLoadContextInfo(true, false, OriginAttributes());
|
|
|
|
info.forget(aPrivate);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP LoadContextInfoFactory::GetAnonymous(nsILoadContextInfo * *aAnonymous)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsILoadContextInfo> info = GetLoadContextInfo(false, true, OriginAttributes());
|
|
|
|
info.forget(aAnonymous);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP LoadContextInfoFactory::Custom(bool aPrivate, bool aAnonymous,
|
|
|
|
JS::HandleValue aOriginAttributes, JSContext *cx,
|
|
|
|
nsILoadContextInfo * *_retval)
|
|
|
|
{
|
|
|
|
OriginAttributes attrs;
|
|
|
|
bool status = attrs.Init(cx, aOriginAttributes);
|
|
|
|
NS_ENSURE_TRUE(status, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsCOMPtr<nsILoadContextInfo> info = GetLoadContextInfo(aPrivate, aAnonymous, attrs);
|
|
|
|
info.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP LoadContextInfoFactory::FromLoadContext(nsILoadContext *aLoadContext, bool aAnonymous,
|
|
|
|
nsILoadContextInfo * *_retval)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsILoadContextInfo> info = GetLoadContextInfo(aLoadContext, aAnonymous);
|
|
|
|
info.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP LoadContextInfoFactory::FromWindow(nsIDOMWindow *aWindow, bool aAnonymous,
|
|
|
|
nsILoadContextInfo * *_retval)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsILoadContextInfo> info = GetLoadContextInfo(aWindow, aAnonymous);
|
|
|
|
info.forget(_retval);
|
2013-09-20 13:11:25 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-11 19:13:09 +03:00
|
|
|
// Helper functions
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
LoadContextInfo *
|
|
|
|
GetLoadContextInfo(nsIChannel * aChannel)
|
|
|
|
{
|
2015-10-11 19:13:09 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
bool pb = NS_UsePrivateBrowsing(aChannel);
|
|
|
|
|
|
|
|
bool anon = false;
|
|
|
|
nsLoadFlags loadFlags;
|
2015-10-11 19:13:09 +03:00
|
|
|
rv = aChannel->GetLoadFlags(&loadFlags);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2013-09-20 13:11:25 +04:00
|
|
|
anon = !!(loadFlags & nsIChannel::LOAD_ANONYMOUS);
|
2015-10-11 19:13:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
OriginAttributes oa;
|
|
|
|
NS_GetOriginAttributes(aChannel, oa);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2015-10-11 19:13:09 +03:00
|
|
|
return new LoadContextInfo(pb, anon, oa);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
LoadContextInfo *
|
2015-10-11 19:13:09 +03:00
|
|
|
GetLoadContextInfo(nsILoadContext *aLoadContext, bool aIsAnonymous)
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
2015-10-11 19:13:09 +03:00
|
|
|
if (!aLoadContext) {
|
|
|
|
return new LoadContextInfo(false, aIsAnonymous,
|
|
|
|
OriginAttributes(nsILoadContextInfo::NO_APP_ID, false));
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
bool pb = aLoadContext->UsePrivateBrowsing();
|
2015-10-11 19:13:09 +03:00
|
|
|
OriginAttributes oa;
|
|
|
|
aLoadContext->GetOriginAttributes(oa);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2015-10-11 19:13:09 +03:00
|
|
|
return new LoadContextInfo(pb, aIsAnonymous, oa);
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2015-10-11 19:13:09 +03:00
|
|
|
LoadContextInfo*
|
|
|
|
GetLoadContextInfo(nsIDOMWindow *aWindow,
|
|
|
|
bool aIsAnonymous)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIWebNavigation> webNav = do_GetInterface(aWindow);
|
|
|
|
nsCOMPtr<nsILoadContext> loadContext = do_QueryInterface(webNav);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2015-10-11 19:13:09 +03:00
|
|
|
return GetLoadContextInfo(loadContext, aIsAnonymous);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
LoadContextInfo *
|
2015-10-11 19:13:09 +03:00
|
|
|
GetLoadContextInfo(nsILoadContextInfo *aInfo)
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
|
|
|
return new LoadContextInfo(aInfo->IsPrivate(),
|
2015-10-11 19:13:09 +03:00
|
|
|
aInfo->IsAnonymous(),
|
|
|
|
*aInfo->OriginAttributesPtr());
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
LoadContextInfo *
|
|
|
|
GetLoadContextInfo(bool const aIsPrivate,
|
2015-10-11 19:13:09 +03:00
|
|
|
bool const aIsAnonymous,
|
|
|
|
OriginAttributes const &aOriginAttributes)
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
|
|
|
return new LoadContextInfo(aIsPrivate,
|
2015-10-11 19:13:09 +03:00
|
|
|
aIsAnonymous,
|
|
|
|
aOriginAttributes);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|