2015-06-16 22:27:45 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +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/. */
|
2001-03-29 06:11:48 +04:00
|
|
|
|
2020-04-10 11:36:37 +03:00
|
|
|
#include "mozilla/Permission.h"
|
2013-06-06 23:59:31 +04:00
|
|
|
#include "nsIClassInfoImpl.h"
|
2015-06-10 19:48:22 +03:00
|
|
|
#include "nsIEffectiveTLDService.h"
|
2019-04-27 01:31:57 +03:00
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsNetUtil.h"
|
2015-06-16 22:27:45 +03:00
|
|
|
#include "mozilla/BasePrincipal.h"
|
2019-10-30 15:21:14 +03:00
|
|
|
#include "mozilla/StaticPrefs_permissions.h"
|
2001-03-29 06:11:48 +04:00
|
|
|
|
2020-04-10 11:36:37 +03:00
|
|
|
namespace mozilla {
|
2001-03-29 06:11:48 +04:00
|
|
|
|
2020-04-10 11:36:37 +03:00
|
|
|
// Permission Implementation
|
2001-03-29 06:11:48 +04:00
|
|
|
|
2020-04-10 11:36:37 +03:00
|
|
|
NS_IMPL_CLASSINFO(Permission, nullptr, 0, {0})
|
|
|
|
NS_IMPL_ISUPPORTS_CI(Permission, nsIPermission)
|
|
|
|
|
|
|
|
Permission::Permission(nsIPrincipal* aPrincipal, const nsACString& aType,
|
|
|
|
uint32_t aCapability, uint32_t aExpireType,
|
|
|
|
int64_t aExpireTime, int64_t aModificationTime)
|
2015-06-10 19:48:22 +03:00
|
|
|
: mPrincipal(aPrincipal),
|
2003-06-18 04:52:59 +04:00
|
|
|
mType(aType),
|
|
|
|
mCapability(aCapability),
|
2009-10-17 01:01:04 +04:00
|
|
|
mExpireType(aExpireType),
|
2019-04-18 16:43:10 +03:00
|
|
|
mExpireTime(aExpireTime),
|
|
|
|
mModificationTime(aModificationTime) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-04-10 11:36:37 +03:00
|
|
|
already_AddRefed<nsIPrincipal> Permission::ClonePrincipalForPermission(
|
2019-04-15 13:26:09 +03:00
|
|
|
nsIPrincipal* aPrincipal) {
|
|
|
|
MOZ_ASSERT(aPrincipal);
|
|
|
|
|
|
|
|
mozilla::OriginAttributes attrs = aPrincipal->OriginAttributesRef();
|
2019-10-30 15:21:14 +03:00
|
|
|
if (!StaticPrefs::permissions_isolateBy_userContext()) {
|
|
|
|
attrs.StripAttributes(mozilla::OriginAttributes::STRIP_USER_CONTEXT_ID);
|
|
|
|
}
|
2019-04-15 13:26:09 +03:00
|
|
|
|
|
|
|
nsAutoCString originNoSuffix;
|
|
|
|
nsresult rv = aPrincipal->GetOriginNoSuffix(originNoSuffix);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), originNoSuffix);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
2019-07-08 19:37:45 +03:00
|
|
|
return mozilla::BasePrincipal::CreateContentPrincipal(uri, attrs);
|
2019-04-15 13:26:09 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 11:36:37 +03:00
|
|
|
already_AddRefed<Permission> Permission::Create(
|
2019-04-18 16:43:10 +03:00
|
|
|
nsIPrincipal* aPrincipal, const nsACString& aType, uint32_t aCapability,
|
|
|
|
uint32_t aExpireType, int64_t aExpireTime, int64_t aModificationTime) {
|
2016-09-20 11:35:21 +03:00
|
|
|
NS_ENSURE_TRUE(aPrincipal, nullptr);
|
|
|
|
|
2019-04-15 13:26:09 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal =
|
2020-04-10 11:36:37 +03:00
|
|
|
Permission::ClonePrincipalForPermission(aPrincipal);
|
2016-09-20 11:35:21 +03:00
|
|
|
NS_ENSURE_TRUE(principal, nullptr);
|
|
|
|
|
2020-04-10 11:36:37 +03:00
|
|
|
RefPtr<Permission> permission =
|
|
|
|
new Permission(principal, aType, aCapability, aExpireType, aExpireTime,
|
|
|
|
aModificationTime);
|
2016-09-20 11:35:21 +03:00
|
|
|
return permission.forget();
|
|
|
|
}
|
|
|
|
|
2012-08-23 22:37:31 +04:00
|
|
|
NS_IMETHODIMP
|
2020-04-10 11:36:37 +03:00
|
|
|
Permission::GetPrincipal(nsIPrincipal** aPrincipal) {
|
2015-06-10 19:48:22 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> copy = mPrincipal;
|
|
|
|
copy.forget(aPrincipal);
|
2012-08-23 22:37:31 +04:00
|
|
|
return NS_OK;
|
2001-03-29 06:11:48 +04:00
|
|
|
}
|
|
|
|
|
2003-06-18 04:52:59 +04:00
|
|
|
NS_IMETHODIMP
|
2020-04-10 11:36:37 +03:00
|
|
|
Permission::GetType(nsACString& aType) {
|
2015-06-10 19:48:22 +03:00
|
|
|
aType = mType;
|
2012-08-23 22:37:31 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2020-04-10 11:36:37 +03:00
|
|
|
Permission::GetCapability(uint32_t* aCapability) {
|
2015-06-10 19:48:22 +03:00
|
|
|
*aCapability = mCapability;
|
2003-03-22 04:24:51 +03:00
|
|
|
return NS_OK;
|
2001-03-29 06:11:48 +04:00
|
|
|
}
|
|
|
|
|
2003-06-18 04:52:59 +04:00
|
|
|
NS_IMETHODIMP
|
2020-04-10 11:36:37 +03:00
|
|
|
Permission::GetExpireType(uint32_t* aExpireType) {
|
2015-06-10 19:48:22 +03:00
|
|
|
*aExpireType = mExpireType;
|
2001-03-29 06:11:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-06-18 04:52:59 +04:00
|
|
|
NS_IMETHODIMP
|
2020-04-10 11:36:37 +03:00
|
|
|
Permission::GetExpireTime(int64_t* aExpireTime) {
|
2015-06-10 19:48:22 +03:00
|
|
|
*aExpireTime = mExpireTime;
|
2001-03-29 06:11:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-10-17 01:01:04 +04:00
|
|
|
|
2019-04-18 16:43:10 +03:00
|
|
|
NS_IMETHODIMP
|
2020-04-10 11:36:37 +03:00
|
|
|
Permission::GetModificationTime(int64_t* aModificationTime) {
|
2019-04-18 16:43:10 +03:00
|
|
|
*aModificationTime = mModificationTime;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-10-17 01:01:04 +04:00
|
|
|
NS_IMETHODIMP
|
2020-04-10 11:36:37 +03:00
|
|
|
Permission::Matches(nsIPrincipal* aPrincipal, bool aExactHost, bool* aMatches) {
|
2015-06-10 19:48:22 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aPrincipal);
|
|
|
|
NS_ENSURE_ARG_POINTER(aMatches);
|
|
|
|
|
|
|
|
*aMatches = false;
|
|
|
|
|
2016-09-20 11:35:21 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal =
|
2020-04-10 11:36:37 +03:00
|
|
|
Permission::ClonePrincipalForPermission(aPrincipal);
|
2016-09-20 11:35:21 +03:00
|
|
|
if (!principal) {
|
|
|
|
*aMatches = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-06-03 14:14:38 +03:00
|
|
|
return MatchesPrincipalForPermission(principal, aExactHost, aMatches);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2020-04-10 11:36:37 +03:00
|
|
|
Permission::MatchesPrincipalForPermission(nsIPrincipal* aPrincipal,
|
2020-04-20 20:09:23 +03:00
|
|
|
bool aExactHost, bool* aMatches) {
|
2020-04-23 16:11:56 +03:00
|
|
|
return mPrincipal->EqualsForPermission(aPrincipal, aExactHost, aMatches);
|
2009-10-17 01:01:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2020-04-10 11:36:37 +03:00
|
|
|
Permission::MatchesURI(nsIURI* aURI, bool aExactHost, bool* aMatches) {
|
2015-06-10 19:48:22 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
|
|
|
|
2017-01-12 19:38:48 +03:00
|
|
|
mozilla::OriginAttributes attrs;
|
2015-08-18 10:01:42 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal =
|
2019-07-08 19:37:45 +03:00
|
|
|
mozilla::BasePrincipal::CreateContentPrincipal(aURI, attrs);
|
2015-08-18 10:01:42 +03:00
|
|
|
NS_ENSURE_TRUE(principal, NS_ERROR_FAILURE);
|
2015-06-10 19:48:22 +03:00
|
|
|
|
|
|
|
return Matches(principal, aExactHost, aMatches);
|
2009-10-17 01:01:04 +04:00
|
|
|
}
|
2020-04-10 11:36:37 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|