no bug - Import translations from android-l10n r=release a=l10n CLOSED TREE
[gecko.git] / dom / quota / StorageOriginAttributes.cpp
blobc07b4d04bbab713fe3b3ed14326f31b7d0b77599
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "StorageOriginAttributes.h"
9 #include "nsString.h"
10 #include "nsURLHelper.h"
11 #include "mozilla/Assertions.h"
12 #include "mozilla/dom/quota/QuotaManager.h"
14 namespace mozilla {
16 void StorageOriginAttributes::CreateSuffix(nsACString& aStr) const {
17 nsCString str1;
19 URLParams params;
20 nsAutoString value;
22 if (mInIsolatedMozBrowser) {
23 params.Set(u"inBrowser"_ns, u"1"_ns);
26 str1.Truncate();
28 params.Serialize(value, true);
29 if (!value.IsEmpty()) {
30 str1.AppendLiteral("^");
31 str1.Append(NS_ConvertUTF16toUTF8(value));
34 // Make sure that the string don't contain characters that would get replaced
35 // with the plus character by quota manager, potentially causing ambiguity.
36 MOZ_ASSERT(str1.FindCharInSet(dom::quota::QuotaManager::kReplaceChars) ==
37 kNotFound);
39 // Let OriginAttributes::CreateSuffix serialize other origin attributes.
40 nsCString str2;
41 mOriginAttributes.CreateSuffix(str2);
43 aStr.Truncate();
45 if (str1.IsEmpty()) {
46 aStr.Append(str2);
47 return;
50 if (str2.IsEmpty()) {
51 aStr.Append(str1);
52 return;
55 // If both strings are not empty, we need to combine them.
56 aStr.Append(str1);
57 aStr.Append('&');
58 aStr.Append(Substring(str2, 1, str2.Length() - 1));
61 bool StorageOriginAttributes::PopulateFromSuffix(const nsACString& aStr) {
62 if (aStr.IsEmpty()) {
63 return true;
66 if (aStr[0] != '^') {
67 return false;
70 bool ok =
71 URLParams::Parse(Substring(aStr, 1, aStr.Length() - 1),
72 [this](const nsAString& aName, const nsAString& aValue) {
73 if (aName.EqualsLiteral("inBrowser")) {
74 if (!aValue.EqualsLiteral("1")) {
75 return false;
78 mInIsolatedMozBrowser = true;
79 return true;
82 // Let OriginAttributes::PopulateFromSuffix parse other
83 // origin attributes.
84 return true;
85 });
86 if (!ok) {
87 return false;
90 return mOriginAttributes.PopulateFromSuffix(aStr);
93 bool StorageOriginAttributes::PopulateFromOrigin(const nsACString& aOrigin,
94 nsACString& aOriginNoSuffix) {
95 // RFindChar is only available on nsCString.
96 nsCString origin(aOrigin);
97 int32_t pos = origin.RFindChar('^');
99 if (pos == kNotFound) {
100 aOriginNoSuffix = origin;
101 return true;
104 aOriginNoSuffix = Substring(origin, 0, pos);
105 return PopulateFromSuffix(Substring(origin, pos));
108 } // namespace mozilla