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