2019-04-29 07:05:56 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_localstorage_LSValue_h
|
|
|
|
#define mozilla_dom_localstorage_LSValue_h
|
|
|
|
|
2020-10-21 16:17:18 +03:00
|
|
|
#include <cstdint>
|
|
|
|
#include "ErrorList.h"
|
2019-04-29 07:10:42 +03:00
|
|
|
#include "SnappyUtils.h"
|
2020-10-21 16:17:18 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Span.h"
|
2019-12-21 04:13:29 +03:00
|
|
|
#include "nsString.h"
|
2020-10-21 16:17:18 +03:00
|
|
|
#include "nsStringFwd.h"
|
|
|
|
#include "nsTStringRepr.h"
|
2019-04-29 07:10:42 +03:00
|
|
|
|
2019-09-14 08:33:01 +03:00
|
|
|
class mozIStorageStatement;
|
|
|
|
|
2019-12-21 04:13:29 +03:00
|
|
|
namespace IPC {
|
|
|
|
template <typename>
|
|
|
|
struct ParamTraits;
|
|
|
|
}
|
|
|
|
|
2019-04-29 07:05:56 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a LocalStorage value. From content's perspective, values (if
|
|
|
|
* present) are always DOMStrings. This is also true from a quota-tracking
|
|
|
|
* perspective. However, for memory and disk efficiency it's preferable to store
|
2019-04-29 07:10:42 +03:00
|
|
|
* the value in alternate compressed or utf-8 encoding representations. The
|
|
|
|
* LSValue type exists to support these alternate representations, dynamically
|
|
|
|
* decompressing/re-encoding to utf-16 while still tracking value size on a
|
|
|
|
* utf-16 basis for quota purposes.
|
2019-04-29 07:05:56 +03:00
|
|
|
*/
|
|
|
|
class LSValue final {
|
|
|
|
friend struct IPC::ParamTraits<LSValue>;
|
|
|
|
|
2019-04-29 07:09:42 +03:00
|
|
|
nsCString mBuffer;
|
|
|
|
uint32_t mUTF16Length;
|
2019-04-29 07:10:42 +03:00
|
|
|
bool mCompressed;
|
2019-04-29 07:05:56 +03:00
|
|
|
|
|
|
|
public:
|
2019-09-14 08:33:01 +03:00
|
|
|
LSValue() : mUTF16Length(0), mCompressed(false) { SetIsVoid(true); }
|
|
|
|
|
|
|
|
bool InitFromString(const nsAString& aBuffer);
|
|
|
|
|
|
|
|
nsresult InitFromStatement(mozIStorageStatement* aStatement, uint32_t aIndex);
|
2019-04-29 07:05:56 +03:00
|
|
|
|
|
|
|
bool IsVoid() const { return mBuffer.IsVoid(); }
|
|
|
|
|
|
|
|
void SetIsVoid(bool aVal) { mBuffer.SetIsVoid(aVal); }
|
|
|
|
|
2019-04-29 07:09:42 +03:00
|
|
|
/**
|
|
|
|
* This represents the "physical" length that the parent process uses for
|
|
|
|
* the size of value/item computation. This can also be used to see how much
|
|
|
|
* memory the value is using at rest or what the cost is for sending the value
|
|
|
|
* over IPC.
|
|
|
|
*/
|
2019-04-29 07:05:56 +03:00
|
|
|
uint32_t Length() const { return mBuffer.Length(); }
|
|
|
|
|
2019-04-29 07:09:42 +03:00
|
|
|
/*
|
|
|
|
* This represents the "logical" length that content sees and that is also
|
|
|
|
* used for quota management purposes.
|
|
|
|
*/
|
|
|
|
uint32_t UTF16Length() const { return mUTF16Length; }
|
|
|
|
|
2019-04-29 07:10:42 +03:00
|
|
|
bool IsCompressed() const { return mCompressed; }
|
|
|
|
|
2019-04-29 07:05:56 +03:00
|
|
|
bool Equals(const LSValue& aOther) const {
|
|
|
|
return mBuffer == aOther.mBuffer &&
|
2019-04-29 07:09:42 +03:00
|
|
|
mBuffer.IsVoid() == aOther.mBuffer.IsVoid() &&
|
2019-04-29 07:10:42 +03:00
|
|
|
mUTF16Length == aOther.mUTF16Length &&
|
|
|
|
mCompressed == aOther.mCompressed;
|
2019-04-29 07:05:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const LSValue& aOther) const { return Equals(aOther); }
|
|
|
|
|
|
|
|
bool operator!=(const LSValue& aOther) const { return !Equals(aOther); }
|
|
|
|
|
2019-04-29 07:09:42 +03:00
|
|
|
operator const nsCString&() const { return mBuffer; }
|
|
|
|
|
|
|
|
operator Span<const char>() const { return mBuffer; }
|
|
|
|
|
|
|
|
class Converter {
|
|
|
|
nsString mBuffer;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit Converter(const LSValue& aValue) {
|
|
|
|
if (aValue.mBuffer.IsVoid()) {
|
|
|
|
mBuffer.SetIsVoid(true);
|
2019-04-29 07:10:42 +03:00
|
|
|
} else if (aValue.mCompressed) {
|
|
|
|
nsCString buffer;
|
|
|
|
MOZ_ALWAYS_TRUE(SnappyUncompress(aValue.mBuffer, buffer));
|
|
|
|
CopyUTF8toUTF16(buffer, mBuffer);
|
2019-04-29 07:09:42 +03:00
|
|
|
} else {
|
|
|
|
CopyUTF8toUTF16(aValue.mBuffer, mBuffer);
|
|
|
|
}
|
|
|
|
}
|
2020-02-05 19:09:52 +03:00
|
|
|
Converter(Converter&& aOther) = default;
|
|
|
|
~Converter() = default;
|
2019-04-29 07:09:42 +03:00
|
|
|
|
|
|
|
operator const nsString&() const { return mBuffer; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Converter() = delete;
|
|
|
|
Converter(const Converter&) = delete;
|
|
|
|
Converter& operator=(const Converter&) = delete;
|
|
|
|
Converter& operator=(const Converter&&) = delete;
|
|
|
|
};
|
|
|
|
|
2020-02-05 19:09:52 +03:00
|
|
|
Converter AsString() const { return Converter{*this}; }
|
2019-04-29 07:05:56 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const LSValue& VoidLSValue();
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_localstorage_LSValue_h
|