Bug 1866894 - Update failing subtest for content-visibility-auto-resize.html. r=fredw
[gecko.git] / dom / storage / StorageUtils.cpp
blob39a69c3f0843b92dd62c9b65a5f5b88ab0874d50
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/. */
7 #include "nsIURL.h"
8 #include "StorageUtils.h"
10 #include "mozilla/OriginAttributes.h"
11 #include "nsDebug.h"
12 #include "nsIPrincipal.h"
13 #include "nsIURI.h"
14 #include "nsNetUtil.h"
15 #include "nsPrintfCString.h"
17 namespace mozilla::dom::StorageUtils {
19 bool PrincipalsEqual(nsIPrincipal* aObjectPrincipal,
20 nsIPrincipal* aSubjectPrincipal) {
21 if (!aSubjectPrincipal) {
22 return true;
25 if (!aObjectPrincipal) {
26 return false;
29 return aSubjectPrincipal->Equals(aObjectPrincipal);
32 void ReverseString(const nsACString& aSource, nsACString& aResult) {
33 nsACString::const_iterator sourceBegin, sourceEnd;
34 aSource.BeginReading(sourceBegin);
35 aSource.EndReading(sourceEnd);
37 aResult.SetLength(aSource.Length());
38 auto destEnd = aResult.EndWriting();
40 while (sourceBegin != sourceEnd) {
41 *(--destEnd) = *sourceBegin;
42 ++sourceBegin;
46 nsresult CreateReversedDomain(const nsACString& aAsciiDomain,
47 nsACString& aKey) {
48 if (aAsciiDomain.IsEmpty()) {
49 return NS_ERROR_NOT_AVAILABLE;
52 ReverseString(aAsciiDomain, aKey);
54 aKey.Append('.');
55 return NS_OK;
58 // This is only a compatibility code for schema version 0. Returns the 'scope'
59 // key in the schema version 0 format for the scope column.
60 nsCString Scheme0Scope(const nsACString& aOriginSuffix,
61 const nsACString& aOriginNoSuffix) {
62 nsCString result;
64 OriginAttributes oa;
65 if (!aOriginSuffix.IsEmpty()) {
66 DebugOnly<bool> success = oa.PopulateFromSuffix(aOriginSuffix);
67 MOZ_ASSERT(success);
70 if (oa.mInIsolatedMozBrowser) {
71 result.AppendInt(0); // This is the appId to be removed.
72 result.Append(':');
73 result.Append(oa.mInIsolatedMozBrowser ? 't' : 'f');
74 result.Append(':');
77 // If there is more than just appid and/or inbrowser stored in origin
78 // attributes, put it to the schema 0 scope as well. We must do that
79 // to keep the scope column unique (same resolution as schema 1 has
80 // with originAttributes and originKey columns) so that switch between
81 // schema 1 and 0 always works in both ways.
82 nsAutoCString remaining;
83 oa.mInIsolatedMozBrowser = false;
84 oa.CreateSuffix(remaining);
85 if (!remaining.IsEmpty()) {
86 MOZ_ASSERT(!aOriginSuffix.IsEmpty());
88 if (result.IsEmpty()) {
89 // Must contain the old prefix, otherwise we won't search for the whole
90 // origin attributes suffix.
91 result.AppendLiteral("0:f:");
94 // Append the whole origin attributes suffix despite we have already stored
95 // appid and inbrowser. We are only looking for it when the scope string
96 // starts with "$appid:$inbrowser:" (with whatever valid values).
98 // The OriginAttributes suffix is a string in a form like:
99 // "^addonId=101&userContextId=5" and it's ensured it always starts with '^'
100 // and never contains ':'. See OriginAttributes::CreateSuffix.
101 result.Append(aOriginSuffix);
102 result.Append(':');
105 result.Append(aOriginNoSuffix);
107 return result;
110 } // namespace mozilla::dom::StorageUtils