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"
|
|
|
|
|
2016-01-09 04:20:50 +03:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
2017-01-13 17:41:28 +03:00
|
|
|
#include "nsDocShell.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsILoadContext.h"
|
2015-10-11 19:13:09 +03:00
|
|
|
#include "nsIWebNavigation.h"
|
2016-01-09 04:20:50 +03:00
|
|
|
#include "nsNetUtil.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2016-01-09 04:20:50 +03:00
|
|
|
using namespace mozilla::dom;
|
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
|
|
|
|
2017-01-12 19:38:48 +03:00
|
|
|
LoadContextInfo::LoadContextInfo(bool aIsAnonymous,
|
|
|
|
OriginAttributes aOriginAttributes)
|
2016-08-24 01:07:56 +03: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)
|
|
|
|
{
|
2016-08-24 01:07:56 +03:00
|
|
|
*aIsPrivate = mOriginAttributes.mPrivateBrowsingId > 0;
|
2013-09-20 13:11:25 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-12 19:38:48 +03:00
|
|
|
OriginAttributes const* LoadContextInfo::OriginAttributesPtr()
|
2015-10-11 19:13:09 +03:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2017-01-12 19:38:48 +03:00
|
|
|
nsCOMPtr<nsILoadContextInfo> info =
|
|
|
|
GetLoadContextInfo(false, OriginAttributes());
|
2015-10-11 19:13:09 +03:00
|
|
|
info.forget(aDefault);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP LoadContextInfoFactory::GetPrivate(nsILoadContextInfo * *aPrivate)
|
|
|
|
{
|
2017-01-12 19:38:48 +03:00
|
|
|
OriginAttributes attrs;
|
2016-08-24 01:07:56 +03:00
|
|
|
attrs.SyncAttributesWithPrivateBrowsing(true);
|
|
|
|
nsCOMPtr<nsILoadContextInfo> info = GetLoadContextInfo(false, attrs);
|
2015-10-11 19:13:09 +03:00
|
|
|
info.forget(aPrivate);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP LoadContextInfoFactory::GetAnonymous(nsILoadContextInfo * *aAnonymous)
|
|
|
|
{
|
2017-01-12 19:38:48 +03:00
|
|
|
nsCOMPtr<nsILoadContextInfo> info =
|
|
|
|
GetLoadContextInfo(true, OriginAttributes());
|
2015-10-11 19:13:09 +03:00
|
|
|
info.forget(aAnonymous);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-24 01:07:56 +03:00
|
|
|
NS_IMETHODIMP LoadContextInfoFactory::Custom(bool aAnonymous,
|
2015-10-11 19:13:09 +03:00
|
|
|
JS::HandleValue aOriginAttributes, JSContext *cx,
|
|
|
|
nsILoadContextInfo * *_retval)
|
|
|
|
{
|
2017-01-12 19:38:48 +03:00
|
|
|
OriginAttributes attrs;
|
2015-10-11 19:13:09 +03:00
|
|
|
bool status = attrs.Init(cx, aOriginAttributes);
|
|
|
|
NS_ENSURE_TRUE(status, NS_ERROR_FAILURE);
|
|
|
|
|
2016-08-24 01:07:56 +03:00
|
|
|
nsCOMPtr<nsILoadContextInfo> info = GetLoadContextInfo(aAnonymous, attrs);
|
2015-10-11 19:13:09 +03:00
|
|
|
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;
|
|
|
|
|
2016-08-24 01:07:56 +03:00
|
|
|
DebugOnly<bool> pb = NS_UsePrivateBrowsing(aChannel);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-01-12 19:38:48 +03:00
|
|
|
OriginAttributes oa;
|
2015-10-11 19:13:09 +03:00
|
|
|
NS_GetOriginAttributes(aChannel, oa);
|
2016-08-24 01:07:56 +03:00
|
|
|
MOZ_ASSERT(pb == (oa.mPrivateBrowsingId > 0));
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2016-08-24 01:07:56 +03:00
|
|
|
return new LoadContextInfo(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) {
|
2017-01-12 19:38:48 +03:00
|
|
|
return new LoadContextInfo(aIsAnonymous, OriginAttributes());
|
2015-10-11 19:13:09 +03:00
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2017-01-12 19:38:48 +03:00
|
|
|
OriginAttributes oa;
|
|
|
|
aLoadContext->GetOriginAttributes(oa);
|
2016-06-03 00:02:29 +03:00
|
|
|
|
2017-01-13 17:41:28 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsCOMPtr<nsIDocShellTreeItem> docShell = do_QueryInterface(aLoadContext);
|
|
|
|
if (!docShell || docShell->ItemType() != nsIDocShellTreeItem::typeChrome) {
|
|
|
|
MOZ_ASSERT(aLoadContext->UsePrivateBrowsing() == (oa.mPrivateBrowsingId > 0));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-01-12 19:38:48 +03:00
|
|
|
return new LoadContextInfo(aIsAnonymous, oa);
|
2015-10-11 19:13:09 +03:00
|
|
|
}
|
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
|
|
|
{
|
2016-08-24 01:07:56 +03:00
|
|
|
return new LoadContextInfo(aInfo->IsAnonymous(),
|
2015-10-11 19:13:09 +03:00
|
|
|
*aInfo->OriginAttributesPtr());
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
LoadContextInfo *
|
2016-08-24 01:07:56 +03:00
|
|
|
GetLoadContextInfo(bool const aIsAnonymous,
|
2017-01-12 19:38:48 +03:00
|
|
|
OriginAttributes const &aOriginAttributes)
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
2017-01-12 19:38:48 +03:00
|
|
|
return new LoadContextInfo(aIsAnonymous, aOriginAttributes);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|