Feature.cpp (1840B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "Feature.h" 8 9 #include "mozilla/BasePrincipal.h" 10 11 namespace mozilla::dom { 12 13 void Feature::GetAllowList(nsTArray<nsCOMPtr<nsIPrincipal>>& aList) const { 14 MOZ_ASSERT(mPolicy == eAllowList); 15 aList.AppendElements(mAllowList); 16 } 17 18 bool Feature::Allows(nsIPrincipal* aPrincipal) const { 19 if (mPolicy == eNone) { 20 return false; 21 } 22 23 if (mPolicy == eAll) { 24 return true; 25 } 26 27 return AllowListContains(aPrincipal); 28 } 29 30 Feature::Feature(const nsAString& aFeatureName) 31 : mFeatureName(aFeatureName), mPolicy(eAllowList) {} 32 33 Feature::~Feature() = default; 34 35 const nsAString& Feature::Name() const { return mFeatureName; } 36 37 void Feature::SetAllowsNone() { 38 mPolicy = eNone; 39 mAllowList.Clear(); 40 } 41 42 bool Feature::AllowsNone() const { return mPolicy == eNone; } 43 44 void Feature::SetAllowsAll() { 45 mPolicy = eAll; 46 mAllowList.Clear(); 47 } 48 49 bool Feature::AllowsAll() const { return mPolicy == eAll; } 50 51 void Feature::AppendToAllowList(nsIPrincipal* aPrincipal) { 52 MOZ_ASSERT(aPrincipal); 53 54 mPolicy = eAllowList; 55 mAllowList.AppendElement(aPrincipal); 56 } 57 58 bool Feature::AllowListContains(nsIPrincipal* aPrincipal) const { 59 MOZ_ASSERT(aPrincipal); 60 61 if (!HasAllowList()) { 62 return false; 63 } 64 65 for (nsIPrincipal* principal : mAllowList) { 66 if (BasePrincipal::Cast(principal)->Subsumes( 67 aPrincipal, BasePrincipal::ConsiderDocumentDomain)) { 68 return true; 69 } 70 } 71 72 return false; 73 } 74 75 bool Feature::HasAllowList() const { return mPolicy == eAllowList; } 76 77 } // namespace mozilla::dom