StorageOriginAttributes.cpp (2769B)
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/. */ 6 7 #include "StorageOriginAttributes.h" 8 9 #include "mozilla/Assertions.h" 10 #include "mozilla/dom/quota/QuotaManager.h" 11 #include "nsString.h" 12 #include "nsURLHelper.h" 13 14 namespace mozilla { 15 16 void StorageOriginAttributes::CreateSuffix(nsACString& aStr) const { 17 nsCString str1; 18 19 URLParams params; 20 nsAutoCString value; 21 22 if (mInIsolatedMozBrowser) { 23 params.Set("inBrowser"_ns, "1"_ns); 24 } 25 26 str1.Truncate(); 27 params.Serialize(value, true); 28 if (!value.IsEmpty()) { 29 str1.AppendLiteral("^"); 30 str1.Append(value); 31 } 32 33 // Make sure that the string don't contain characters that would get replaced 34 // with the plus character by quota manager, potentially causing ambiguity. 35 MOZ_ASSERT(str1.FindCharInSet(dom::quota::QuotaManager::kReplaceChars) == 36 kNotFound); 37 38 // Let OriginAttributes::CreateSuffix serialize other origin attributes. 39 nsCString str2; 40 mOriginAttributes.CreateSuffix(str2); 41 42 aStr.Truncate(); 43 44 if (str1.IsEmpty()) { 45 aStr.Append(str2); 46 return; 47 } 48 49 if (str2.IsEmpty()) { 50 aStr.Append(str1); 51 return; 52 } 53 54 // If both strings are not empty, we need to combine them. 55 aStr.Append(str1); 56 aStr.Append('&'); 57 aStr.Append(Substring(str2, 1, str2.Length() - 1)); 58 } 59 60 bool StorageOriginAttributes::PopulateFromSuffix(const nsACString& aStr) { 61 if (aStr.IsEmpty()) { 62 return true; 63 } 64 65 if (aStr[0] != '^') { 66 return false; 67 } 68 69 bool ok = URLParams::Parse( 70 Substring(aStr, 1, aStr.Length() - 1), true, 71 [this](const nsACString& aName, const nsACString& aValue) { 72 if (aName.EqualsLiteral("inBrowser")) { 73 if (!aValue.EqualsLiteral("1")) { 74 return false; 75 } 76 77 mInIsolatedMozBrowser = true; 78 return true; 79 } 80 81 // Let OriginAttributes::PopulateFromSuffix parse other 82 // origin attributes. 83 return true; 84 }); 85 if (!ok) { 86 return false; 87 } 88 89 return mOriginAttributes.PopulateFromSuffix(aStr); 90 } 91 92 bool StorageOriginAttributes::PopulateFromOrigin(const nsACString& aOrigin, 93 nsACString& aOriginNoSuffix) { 94 // RFindChar is only available on nsCString. 95 nsCString origin(aOrigin); 96 int32_t pos = origin.RFindChar('^'); 97 98 if (pos == kNotFound) { 99 aOriginNoSuffix = origin; 100 return true; 101 } 102 103 aOriginNoSuffix = Substring(origin, 0, pos); 104 return PopulateFromSuffix(Substring(origin, pos)); 105 } 106 107 } // namespace mozilla