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/. */
8 #include "StorageUtils.h"
10 #include "mozilla/OriginAttributes.h"
12 #include "nsIPrincipal.h"
14 #include "nsNetUtil.h"
15 #include "nsPrintfCString.h"
16 #include "mozilla/StorageOriginAttributes.h"
18 namespace mozilla::dom::StorageUtils
{
20 bool PrincipalsEqual(nsIPrincipal
* aObjectPrincipal
,
21 nsIPrincipal
* aSubjectPrincipal
) {
22 if (!aSubjectPrincipal
) {
26 if (!aObjectPrincipal
) {
30 return aSubjectPrincipal
->Equals(aObjectPrincipal
);
33 void ReverseString(const nsACString
& aSource
, nsACString
& aResult
) {
34 nsACString::const_iterator sourceBegin
, sourceEnd
;
35 aSource
.BeginReading(sourceBegin
);
36 aSource
.EndReading(sourceEnd
);
38 aResult
.SetLength(aSource
.Length());
39 auto destEnd
= aResult
.EndWriting();
41 while (sourceBegin
!= sourceEnd
) {
42 *(--destEnd
) = *sourceBegin
;
47 nsresult
CreateReversedDomain(const nsACString
& aAsciiDomain
,
49 if (aAsciiDomain
.IsEmpty()) {
50 return NS_ERROR_NOT_AVAILABLE
;
53 ReverseString(aAsciiDomain
, aKey
);
59 // This is only a compatibility code for schema version 0. Returns the 'scope'
60 // key in the schema version 0 format for the scope column.
61 nsCString
Scheme0Scope(const nsACString
& aOriginSuffix
,
62 const nsACString
& aOriginNoSuffix
) {
65 StorageOriginAttributes oa
;
66 if (!aOriginSuffix
.IsEmpty()) {
67 DebugOnly
<bool> success
= oa
.PopulateFromSuffix(aOriginSuffix
);
71 if (oa
.InIsolatedMozBrowser()) {
72 result
.AppendInt(0); // This is the appId to be removed.
74 result
.Append(oa
.InIsolatedMozBrowser() ? 't' : 'f');
78 // If there is more than just appid and/or inbrowser stored in origin
79 // attributes, put it to the schema 0 scope as well. We must do that
80 // to keep the scope column unique (same resolution as schema 1 has
81 // with originAttributes and originKey columns) so that switch between
82 // schema 1 and 0 always works in both ways.
83 nsAutoCString remaining
;
84 oa
.SetInIsolatedMozBrowser(false);
85 oa
.CreateSuffix(remaining
);
86 if (!remaining
.IsEmpty()) {
87 MOZ_ASSERT(!aOriginSuffix
.IsEmpty());
89 if (result
.IsEmpty()) {
90 // Must contain the old prefix, otherwise we won't search for the whole
91 // origin attributes suffix.
92 result
.AppendLiteral("0:f:");
95 // Append the whole origin attributes suffix despite we have already stored
96 // appid and inbrowser. We are only looking for it when the scope string
97 // starts with "$appid:$inbrowser:" (with whatever valid values).
99 // The OriginAttributes suffix is a string in a form like:
100 // "^addonId=101&userContextId=5" and it's ensured it always starts with '^'
101 // and never contains ':'. See OriginAttributes::CreateSuffix.
102 result
.Append(aOriginSuffix
);
106 result
.Append(aOriginNoSuffix
);
111 } // namespace mozilla::dom::StorageUtils