fune/dom/storage/StorageUtils.cpp
Narcis Beleuzu 7eae8c1064 Backed out 16 changesets (bug 1770944) as req by asuth.
Backed out changeset 61af32f40777 (bug 1770944)
Backed out changeset 4ff0c45db93b (bug 1770944)
Backed out changeset 8a217eff7bcd (bug 1770944)
Backed out changeset 6435f48c96bf (bug 1770944)
Backed out changeset 0d2432765ca0 (bug 1770944)
Backed out changeset 58e02566db85 (bug 1770944)
Backed out changeset 0a8c4c2460ee (bug 1770944)
Backed out changeset 9416bafd9982 (bug 1770944)
Backed out changeset 79de4f83fe2e (bug 1770944)
Backed out changeset 63ac518aceb0 (bug 1770944)
Backed out changeset 14952f872b77 (bug 1770944)
Backed out changeset f65e0967ad75 (bug 1770944)
Backed out changeset bd53c42038f7 (bug 1770944)
Backed out changeset 36c378ba8212 (bug 1770944)
Backed out changeset 9ba54ab06348 (bug 1770944)
Backed out changeset fb5a54b3cbe9 (bug 1770944)
2024-02-23 21:11:08 +02:00

110 lines
3.3 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "nsIURL.h"
#include "StorageUtils.h"
#include "mozilla/OriginAttributes.h"
#include "nsDebug.h"
#include "nsIPrincipal.h"
#include "nsIURI.h"
#include "nsNetUtil.h"
#include "nsPrintfCString.h"
namespace mozilla::dom::StorageUtils {
bool PrincipalsEqual(nsIPrincipal* aObjectPrincipal,
nsIPrincipal* aSubjectPrincipal) {
if (!aSubjectPrincipal) {
return true;
}
if (!aObjectPrincipal) {
return false;
}
return aSubjectPrincipal->Equals(aObjectPrincipal);
}
void ReverseString(const nsACString& aSource, nsACString& aResult) {
nsACString::const_iterator sourceBegin, sourceEnd;
aSource.BeginReading(sourceBegin);
aSource.EndReading(sourceEnd);
aResult.SetLength(aSource.Length());
auto destEnd = aResult.EndWriting();
while (sourceBegin != sourceEnd) {
*(--destEnd) = *sourceBegin;
++sourceBegin;
}
}
nsresult CreateReversedDomain(const nsACString& aAsciiDomain,
nsACString& aKey) {
if (aAsciiDomain.IsEmpty()) {
return NS_ERROR_NOT_AVAILABLE;
}
ReverseString(aAsciiDomain, aKey);
aKey.Append('.');
return NS_OK;
}
// This is only a compatibility code for schema version 0. Returns the 'scope'
// key in the schema version 0 format for the scope column.
nsCString Scheme0Scope(const nsACString& aOriginSuffix,
const nsACString& aOriginNoSuffix) {
nsCString result;
OriginAttributes oa;
if (!aOriginSuffix.IsEmpty()) {
DebugOnly<bool> success = oa.PopulateFromSuffix(aOriginSuffix);
MOZ_ASSERT(success);
}
if (oa.mInIsolatedMozBrowser) {
result.AppendInt(0); // This is the appId to be removed.
result.Append(':');
result.Append(oa.mInIsolatedMozBrowser ? 't' : 'f');
result.Append(':');
}
// If there is more than just appid and/or inbrowser stored in origin
// attributes, put it to the schema 0 scope as well. We must do that
// to keep the scope column unique (same resolution as schema 1 has
// with originAttributes and originKey columns) so that switch between
// schema 1 and 0 always works in both ways.
nsAutoCString remaining;
oa.mInIsolatedMozBrowser = false;
oa.CreateSuffix(remaining);
if (!remaining.IsEmpty()) {
MOZ_ASSERT(!aOriginSuffix.IsEmpty());
if (result.IsEmpty()) {
// Must contain the old prefix, otherwise we won't search for the whole
// origin attributes suffix.
result.AppendLiteral("0:f:");
}
// Append the whole origin attributes suffix despite we have already stored
// appid and inbrowser. We are only looking for it when the scope string
// starts with "$appid:$inbrowser:" (with whatever valid values).
//
// The OriginAttributes suffix is a string in a form like:
// "^addonId=101&userContextId=5" and it's ensured it always starts with '^'
// and never contains ':'. See OriginAttributes::CreateSuffix.
result.Append(aOriginSuffix);
result.Append(':');
}
result.Append(aOriginNoSuffix);
return result;
}
} // namespace mozilla::dom::StorageUtils