Source code

Revision control

Other Tools

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