Bug 1528736 [wpt PR 15405] - Update interfaces/font-metrics-api.idl, a=testonly
[gecko.git] / caps / ExpandedPrincipal.cpp
blob64e87fdfa1b8b318e3f45684ad45c0a3b61255a7
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 sw=2 et 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/. */
7 #include "ExpandedPrincipal.h"
8 #include "nsIClassInfoImpl.h"
10 using namespace mozilla;
12 NS_IMPL_CLASSINFO(ExpandedPrincipal, nullptr, nsIClassInfo::MAIN_THREAD_ONLY,
13 NS_EXPANDEDPRINCIPAL_CID)
14 NS_IMPL_QUERY_INTERFACE_CI(ExpandedPrincipal, nsIPrincipal,
15 nsIExpandedPrincipal, nsISerializable)
16 NS_IMPL_CI_INTERFACE_GETTER(ExpandedPrincipal, nsIPrincipal,
17 nsIExpandedPrincipal, nsISerializable)
19 struct OriginComparator {
20 bool LessThan(nsIPrincipal* a, nsIPrincipal* b) const {
21 nsAutoCString originA;
22 DebugOnly<nsresult> rv = a->GetOrigin(originA);
23 MOZ_ASSERT(NS_SUCCEEDED(rv));
24 nsAutoCString originB;
25 rv = b->GetOrigin(originB);
26 MOZ_ASSERT(NS_SUCCEEDED(rv));
27 return originA < originB;
30 bool Equals(nsIPrincipal* a, nsIPrincipal* b) const {
31 nsAutoCString originA;
32 DebugOnly<nsresult> rv = a->GetOrigin(originA);
33 MOZ_ASSERT(NS_SUCCEEDED(rv));
34 nsAutoCString originB;
35 rv = b->GetOrigin(originB);
36 MOZ_ASSERT(NS_SUCCEEDED(rv));
37 return a == b;
41 ExpandedPrincipal::ExpandedPrincipal(
42 nsTArray<nsCOMPtr<nsIPrincipal>>& aAllowList)
43 : BasePrincipal(eExpandedPrincipal) {
44 // We force the principals to be sorted by origin so that ExpandedPrincipal
45 // origins can have a canonical form.
46 OriginComparator c;
47 for (size_t i = 0; i < aAllowList.Length(); ++i) {
48 mPrincipals.InsertElementSorted(aAllowList[i], c);
52 ExpandedPrincipal::ExpandedPrincipal() : BasePrincipal(eExpandedPrincipal) {}
54 ExpandedPrincipal::~ExpandedPrincipal() {}
56 already_AddRefed<ExpandedPrincipal> ExpandedPrincipal::Create(
57 nsTArray<nsCOMPtr<nsIPrincipal>>& aAllowList,
58 const OriginAttributes& aAttrs) {
59 RefPtr<ExpandedPrincipal> ep = new ExpandedPrincipal(aAllowList);
61 nsAutoCString origin;
62 origin.AssignLiteral("[Expanded Principal [");
63 for (size_t i = 0; i < ep->mPrincipals.Length(); ++i) {
64 if (i != 0) {
65 origin.AppendLiteral(", ");
68 nsAutoCString subOrigin;
69 DebugOnly<nsresult> rv = ep->mPrincipals.ElementAt(i)->GetOrigin(subOrigin);
70 MOZ_ASSERT(NS_SUCCEEDED(rv));
71 origin.Append(subOrigin);
73 origin.AppendLiteral("]]");
75 ep->FinishInit(origin, aAttrs);
76 return ep.forget();
79 NS_IMETHODIMP
80 ExpandedPrincipal::GetDomain(nsIURI** aDomain) {
81 *aDomain = nullptr;
82 return NS_OK;
85 NS_IMETHODIMP
86 ExpandedPrincipal::SetDomain(nsIURI* aDomain) { return NS_OK; }
88 bool ExpandedPrincipal::SubsumesInternal(
89 nsIPrincipal* aOther,
90 BasePrincipal::DocumentDomainConsideration aConsideration) {
91 // If aOther is an ExpandedPrincipal too, we break it down into its component
92 // nsIPrincipals, and check subsumes on each one.
93 if (Cast(aOther)->Is<ExpandedPrincipal>()) {
94 auto* expanded = Cast(aOther)->As<ExpandedPrincipal>();
96 for (auto& other : expanded->AllowList()) {
97 // Use SubsumesInternal rather than Subsumes here, since OriginAttribute
98 // checks are only done between non-expanded sub-principals, and we don't
99 // need to incur the extra virtual call overhead.
100 if (!SubsumesInternal(other, aConsideration)) {
101 return false;
104 return true;
107 // We're dealing with a regular principal. One of our principals must subsume
108 // it.
109 for (uint32_t i = 0; i < mPrincipals.Length(); ++i) {
110 if (Cast(mPrincipals[i])->Subsumes(aOther, aConsideration)) {
111 return true;
115 return false;
118 bool ExpandedPrincipal::MayLoadInternal(nsIURI* uri) {
119 for (uint32_t i = 0; i < mPrincipals.Length(); ++i) {
120 if (BasePrincipal::Cast(mPrincipals[i])->MayLoadInternal(uri)) {
121 return true;
125 return false;
128 uint32_t ExpandedPrincipal::GetHashValue() {
129 MOZ_CRASH("extended principal should never be used as key in a hash map");
132 NS_IMETHODIMP
133 ExpandedPrincipal::GetURI(nsIURI** aURI) {
134 *aURI = nullptr;
135 return NS_OK;
138 const nsTArray<nsCOMPtr<nsIPrincipal>>& ExpandedPrincipal::AllowList() {
139 return mPrincipals;
142 NS_IMETHODIMP
143 ExpandedPrincipal::GetBaseDomain(nsACString& aBaseDomain) {
144 return NS_ERROR_NOT_AVAILABLE;
147 NS_IMETHODIMP
148 ExpandedPrincipal::GetAddonId(nsAString& aAddonId) {
149 aAddonId.Truncate();
150 return NS_OK;
153 bool ExpandedPrincipal::AddonHasPermission(const nsAtom* aPerm) {
154 for (size_t i = 0; i < mPrincipals.Length(); ++i) {
155 if (BasePrincipal::Cast(mPrincipals[i])->AddonHasPermission(aPerm)) {
156 return true;
159 return false;
162 bool ExpandedPrincipal::AddonAllowsLoad(nsIURI* aURI,
163 bool aExplicit /* = false */) {
164 for (const auto& principal : mPrincipals) {
165 if (Cast(principal)->AddonAllowsLoad(aURI, aExplicit)) {
166 return true;
169 return false;
172 nsIPrincipal* ExpandedPrincipal::PrincipalToInherit(nsIURI* aRequestedURI) {
173 if (aRequestedURI) {
174 // If a given sub-principal subsumes the given URI, use that principal for
175 // inheritance. In general, this only happens with certain CORS modes, loads
176 // with forced principal inheritance, and creation of XML documents from
177 // XMLHttpRequests or fetch requests. For URIs that normally inherit a
178 // principal (such as data: URIs), we fall back to the last principal in the
179 // allowlist.
180 for (const auto& principal : mPrincipals) {
181 if (Cast(principal)->MayLoadInternal(aRequestedURI)) {
182 return principal;
186 return mPrincipals.LastElement();
189 nsresult ExpandedPrincipal::GetScriptLocation(nsACString& aStr) {
190 aStr.AssignLiteral("[Expanded Principal [");
191 for (size_t i = 0; i < mPrincipals.Length(); ++i) {
192 if (i != 0) {
193 aStr.AppendLiteral(", ");
196 nsAutoCString spec;
197 nsresult rv =
198 nsJSPrincipals::get(mPrincipals.ElementAt(i))->GetScriptLocation(spec);
199 NS_ENSURE_SUCCESS(rv, rv);
201 aStr.Append(spec);
203 aStr.AppendLiteral("]]");
204 return NS_OK;
207 //////////////////////////////////////////
208 // Methods implementing nsISerializable //
209 //////////////////////////////////////////
211 // We've had way too many issues with unversioned serializations, so
212 // explicitly version this one.
213 static const uint32_t kSerializationVersion = 1;
215 NS_IMETHODIMP
216 ExpandedPrincipal::Read(nsIObjectInputStream* aStream) {
217 uint32_t version;
218 nsresult rv = aStream->Read32(&version);
219 if (version != kSerializationVersion) {
220 MOZ_ASSERT(false,
221 "We really need to add handling of the old(?) version here");
222 return NS_ERROR_UNEXPECTED;
225 uint32_t count;
226 rv = aStream->Read32(&count);
227 if (NS_FAILED(rv)) {
228 return rv;
231 if (!mPrincipals.SetCapacity(count, fallible)) {
232 return NS_ERROR_OUT_OF_MEMORY;
235 OriginComparator c;
236 for (uint32_t i = 0; i < count; ++i) {
237 nsCOMPtr<nsISupports> read;
238 rv = aStream->ReadObject(true, getter_AddRefs(read));
239 if (NS_FAILED(rv)) {
240 return rv;
243 nsCOMPtr<nsIPrincipal> principal = do_QueryInterface(read);
244 if (!principal) {
245 return NS_ERROR_UNEXPECTED;
248 // Play it safe and InsertElementSorted, in case the sort order
249 // changed for some bizarre reason.
250 mPrincipals.InsertElementSorted(std::move(principal), c);
253 return NS_OK;
256 NS_IMETHODIMP
257 ExpandedPrincipal::Write(nsIObjectOutputStream* aStream) {
258 nsresult rv = aStream->Write32(kSerializationVersion);
259 if (NS_FAILED(rv)) {
260 return rv;
263 rv = aStream->Write32(mPrincipals.Length());
264 if (NS_FAILED(rv)) {
265 return rv;
268 for (auto& principal : mPrincipals) {
269 rv = aStream->WriteObject(principal, true);
270 if (NS_FAILED(rv)) {
271 return rv;
275 return NS_OK;
278 nsresult ExpandedPrincipal::GetSiteIdentifier(SiteIdentifier& aSite) {
279 // Call GetSiteIdentifier on each of our principals and return a new
280 // ExpandedPrincipal.
282 nsTArray<nsCOMPtr<nsIPrincipal>> allowlist;
283 for (const auto& principal : mPrincipals) {
284 SiteIdentifier site;
285 nsresult rv = Cast(principal)->GetSiteIdentifier(site);
286 NS_ENSURE_SUCCESS(rv, rv);
287 allowlist.AppendElement(site.GetPrincipal());
290 RefPtr<ExpandedPrincipal> expandedPrincipal =
291 ExpandedPrincipal::Create(allowlist, OriginAttributesRef());
292 MOZ_ASSERT(expandedPrincipal, "ExpandedPrincipal::Create returned nullptr?");
294 aSite.Init(expandedPrincipal);
295 return NS_OK;