LSValue.h (3950B)
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 #ifndef mozilla_dom_localstorage_LSValue_h 8 #define mozilla_dom_localstorage_LSValue_h 9 10 #include <cstdint> 11 12 #include "ErrorList.h" 13 #include "SnappyUtils.h" 14 #include "nsString.h" 15 #include "nsStringFwd.h" 16 #include "nsTStringRepr.h" 17 18 class mozIStorageStatement; 19 20 namespace IPC { 21 template <typename> 22 struct ParamTraits; 23 } 24 25 namespace mozilla::dom { 26 27 /** 28 * Represents a LocalStorage value. From content's perspective, values (if 29 * present) are always DOMStrings. This is also true from a quota-tracking 30 * perspective. However, for memory and disk efficiency it's preferable to store 31 * the value in alternate compressed or utf-8 encoding representations. The 32 * LSValue type exists to support these alternate representations, dynamically 33 * decompressing/re-encoding to utf-16 while still tracking value size on a 34 * utf-16 basis for quota purposes. 35 */ 36 class LSValue final { 37 friend struct IPC::ParamTraits<LSValue>; 38 39 public: 40 enum class ConversionType : uint8_t { 41 NONE = 0u, 42 UTF16_UTF8 = 1u, 43 NUM_TYPES = 2u 44 }; 45 46 enum class CompressionType : uint8_t { 47 UNCOMPRESSED = 0u, 48 SNAPPY = 1u, 49 NUM_TYPES = 2u 50 }; 51 52 nsCString mBuffer; 53 uint32_t mUTF16Length; 54 ConversionType mConversionType; 55 CompressionType mCompressionType; 56 57 explicit LSValue() 58 : mUTF16Length(0u), 59 mConversionType(ConversionType::NONE), 60 mCompressionType(CompressionType::UNCOMPRESSED) { 61 SetIsVoid(true); 62 } 63 64 bool InitFromString(const nsAString& aBuffer); 65 66 nsresult InitFromStatement(mozIStorageStatement* aStatement, uint32_t aIndex); 67 68 bool IsVoid() const { return mBuffer.IsVoid(); } 69 70 void SetIsVoid(bool aVal) { mBuffer.SetIsVoid(aVal); } 71 72 /** 73 * This represents the "physical" length that the parent process uses for 74 * the size of value/item computation. This can also be used to see how much 75 * memory the value is using at rest or what the cost is for sending the value 76 * over IPC. 77 */ 78 uint32_t Length() const { return mBuffer.Length(); } 79 80 /* 81 * This represents the "logical" length that content sees and that is also 82 * used for quota management purposes. 83 */ 84 uint32_t UTF16Length() const { return mUTF16Length; } 85 86 ConversionType GetConversionType() const { return mConversionType; } 87 88 CompressionType GetCompressionType() const { return mCompressionType; } 89 90 bool Equals(const LSValue& aOther) const { 91 return mBuffer == aOther.mBuffer && 92 mBuffer.IsVoid() == aOther.mBuffer.IsVoid() && 93 mUTF16Length == aOther.mUTF16Length && 94 mConversionType == aOther.mConversionType && 95 mCompressionType == aOther.mCompressionType; 96 } 97 98 bool operator==(const LSValue& aOther) const { return Equals(aOther); } 99 100 bool operator!=(const LSValue& aOther) const { return !Equals(aOther); } 101 102 constexpr const nsCString& AsCString() const { return mBuffer; } 103 104 class Converter { 105 nsString mBuffer; 106 107 public: 108 explicit Converter(const LSValue& aValue); 109 Converter(Converter&& aOther) = default; 110 ~Converter() = default; 111 112 operator const nsString&() const { return mBuffer; } 113 114 private: 115 Converter() = delete; 116 Converter(const Converter&) = delete; 117 Converter& operator=(const Converter&) = delete; 118 Converter& operator=(const Converter&&) = delete; 119 }; 120 121 Converter AsString() const { return Converter{*this}; } 122 }; 123 124 const LSValue& VoidLSValue(); 125 126 /** 127 * XXX: This function doesn't have to be public 128 * once the support for shadow writes is removed. 129 */ 130 bool PutCStringBytesToString(const nsACString& aSrc, nsString& aDest); 131 132 } // namespace mozilla::dom 133 134 #endif // mozilla_dom_localstorage_LSValue_h