2015-05-03 22:32:37 +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: */
|
2013-10-11 20:28:23 +04:00
|
|
|
/* 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_DOMString_h
|
|
|
|
#define mozilla_dom_DOMString_h
|
|
|
|
|
2017-12-07 03:52:51 +03:00
|
|
|
#include "nsString.h"
|
2013-10-11 20:28:23 +04:00
|
|
|
#include "nsStringBuffer.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/Maybe.h"
|
|
|
|
#include "nsDOMString.h"
|
2017-10-03 01:05:19 +03:00
|
|
|
#include "nsAtom.h"
|
2013-10-11 20:28:23 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A class for representing string return values. This can be either passed to
|
|
|
|
* callees that have an nsString or nsAString out param or passed to a callee
|
|
|
|
* that actually knows about this class and can work with it. Such a callee may
|
2017-12-22 21:02:51 +03:00
|
|
|
* call these setters:
|
|
|
|
*
|
|
|
|
* SetKnownLiveStringBuffer
|
|
|
|
* SetStringBuffer
|
|
|
|
* SetKnownLiveString
|
|
|
|
* SetKnownLiveAtom
|
|
|
|
* SetNull
|
|
|
|
*
|
|
|
|
* to assign a value to the DOMString without instantiating an actual nsString
|
|
|
|
* in the process, or use AsAString() to instantiate an nsString and work with
|
|
|
|
* it. These options are mutually exclusive! Don't do more than one of them.
|
|
|
|
*
|
|
|
|
* It's only OK to call
|
2017-12-22 21:02:51 +03:00
|
|
|
* SetKnownLiveStringBuffer/SetKnownLiveString/SetKnownLiveAtom if the caller of
|
|
|
|
* the method in question plans to keep holding a strong ref to the stringbuffer
|
|
|
|
* involved, whether it's a raw nsStringBuffer, or stored inside the string or
|
|
|
|
* atom being passed. In the string/atom cases that means the caller must own
|
|
|
|
* the string or atom, and not mutate it (in the string case) for the lifetime
|
|
|
|
* of the DOMString.
|
2013-10-11 20:28:23 +04:00
|
|
|
*
|
|
|
|
* The proper way to extract a value is to check IsNull(). If not null, then
|
2017-12-22 21:02:51 +03:00
|
|
|
* check IsEmpty(). If neither of those is true, check HasStringBuffer(). If
|
2017-12-22 21:02:51 +03:00
|
|
|
* that's true, call StringBuffer()/StringBufferLength(). If HasStringBuffer()
|
|
|
|
* returns false, check HasLiteral, and if that returns true call
|
|
|
|
* Literal()/LiteralLength(). If HasLiteral() is false, call AsAString() and
|
|
|
|
* get the value from that.
|
2013-10-11 20:28:23 +04:00
|
|
|
*/
|
|
|
|
class MOZ_STACK_CLASS DOMString {
|
|
|
|
public:
|
|
|
|
DOMString() : mStringBuffer(nullptr), mLength(0), mState(State::Empty) {}
|
|
|
|
~DOMString() {
|
2014-08-14 02:39:41 +04:00
|
|
|
MOZ_ASSERT(!mString || !mStringBuffer, "Shouldn't have both present!");
|
2017-12-22 21:02:51 +03:00
|
|
|
if (mState == State::OwnedStringBuffer) {
|
2017-01-19 06:20:14 +03:00
|
|
|
MOZ_ASSERT(mStringBuffer);
|
|
|
|
mStringBuffer->Release();
|
|
|
|
}
|
2013-10-11 20:28:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
operator nsString&() { return AsAString(); }
|
|
|
|
|
2015-02-13 02:39:26 +03:00
|
|
|
// It doesn't make any sense to convert a DOMString to a const nsString or
|
|
|
|
// nsAString reference; this class is meant for outparams only.
|
|
|
|
operator const nsString&() = delete;
|
|
|
|
operator const nsAString&() = delete;
|
|
|
|
|
2013-10-11 20:28:23 +04:00
|
|
|
nsString& AsAString() {
|
2017-12-22 21:02:51 +03:00
|
|
|
MOZ_ASSERT(mState == State::Empty || mState == State::String,
|
|
|
|
"Moving from nonempty state to another nonempty state?");
|
2013-10-11 20:28:23 +04:00
|
|
|
MOZ_ASSERT(!mStringBuffer, "We already have a stringbuffer?");
|
2014-08-14 02:39:41 +04:00
|
|
|
if (!mString) {
|
|
|
|
mString.emplace();
|
2017-12-22 21:02:51 +03:00
|
|
|
mState = State::String;
|
2013-10-11 20:28:23 +04:00
|
|
|
}
|
2014-08-14 02:39:41 +04:00
|
|
|
return *mString;
|
2013-10-11 20:28:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HasStringBuffer() const {
|
2014-08-14 02:39:41 +04:00
|
|
|
MOZ_ASSERT(!mString || !mStringBuffer, "Shouldn't have both present!");
|
2017-12-22 21:02:51 +03:00
|
|
|
MOZ_ASSERT(mState > State::Null,
|
|
|
|
"Caller should have checked IsNull() and IsEmpty() first");
|
|
|
|
return mState >= State::OwnedStringBuffer;
|
2013-10-11 20:28:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the stringbuffer. This can only be called if HasStringBuffer()
|
2017-12-22 21:02:51 +03:00
|
|
|
// returned true. If that's true, it will never return null. Note that
|
|
|
|
// constructing a string from this nsStringBuffer with length given by
|
|
|
|
// StringBufferLength() might give you something that is not null-terminated.
|
2013-10-11 20:28:23 +04:00
|
|
|
nsStringBuffer* StringBuffer() const {
|
|
|
|
MOZ_ASSERT(HasStringBuffer(),
|
|
|
|
"Don't ask for the stringbuffer if we don't have it");
|
2017-12-22 21:02:51 +03:00
|
|
|
MOZ_ASSERT(mStringBuffer, "We better have a stringbuffer if we claim to");
|
2013-10-11 20:28:23 +04:00
|
|
|
return mStringBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the length of the stringbuffer. Can only be called if
|
|
|
|
// HasStringBuffer().
|
|
|
|
uint32_t StringBufferLength() const {
|
|
|
|
MOZ_ASSERT(HasStringBuffer(),
|
|
|
|
"Don't call this if there is no stringbuffer");
|
|
|
|
return mLength;
|
|
|
|
}
|
|
|
|
|
2017-01-19 06:20:14 +03:00
|
|
|
// Tell the DOMString to relinquish ownership of its nsStringBuffer to the
|
|
|
|
// caller. Can only be called if HasStringBuffer().
|
|
|
|
void RelinquishBufferOwnership() {
|
|
|
|
MOZ_ASSERT(HasStringBuffer(),
|
|
|
|
"Don't call this if there is no stringbuffer");
|
2017-12-22 21:02:51 +03:00
|
|
|
if (mState == State::OwnedStringBuffer) {
|
2017-01-19 06:20:14 +03:00
|
|
|
// Just hand that ref over.
|
2017-12-22 21:02:51 +03:00
|
|
|
mState = State::UnownedStringBuffer;
|
2017-01-19 06:20:14 +03:00
|
|
|
} else {
|
|
|
|
// Caller should end up holding a ref.
|
|
|
|
mStringBuffer->AddRef();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-22 21:02:51 +03:00
|
|
|
bool HasLiteral() const {
|
|
|
|
MOZ_ASSERT(!mString || !mStringBuffer, "Shouldn't have both present!");
|
|
|
|
MOZ_ASSERT(mState > State::Null,
|
|
|
|
"Caller should have checked IsNull() and IsEmpty() first");
|
|
|
|
return mState == State::Literal;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the literal string. This can only be called if HasLiteral()
|
|
|
|
// returned true. If that's true, it will never return null.
|
|
|
|
const char16_t* Literal() const {
|
|
|
|
MOZ_ASSERT(HasLiteral(), "Don't ask for the literal if we don't have it");
|
|
|
|
MOZ_ASSERT(mLiteral, "We better have a literal if we claim to");
|
|
|
|
return mLiteral;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the length of the literal. Can only be called if HasLiteral().
|
|
|
|
uint32_t LiteralLength() const {
|
|
|
|
MOZ_ASSERT(HasLiteral(), "Don't call this if there is no literal");
|
|
|
|
return mLength;
|
|
|
|
}
|
|
|
|
|
2018-07-10 21:21:42 +03:00
|
|
|
bool HasAtom() const {
|
|
|
|
MOZ_ASSERT(!mString || !mStringBuffer, "Shouldn't have both present!");
|
|
|
|
MOZ_ASSERT(mState > State::Null,
|
|
|
|
"Caller should have checked IsNull() and IsEmpty() first");
|
|
|
|
return mState == State::UnownedAtom;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the atom. This can only be called if HasAtom() returned true. If
|
|
|
|
// that's true, it will never return null.
|
|
|
|
nsDynamicAtom* Atom() const {
|
|
|
|
MOZ_ASSERT(HasAtom(), "Don't ask for the atom if we don't have it");
|
|
|
|
MOZ_ASSERT(mAtom, "We better have an atom if we claim to");
|
|
|
|
return mAtom;
|
|
|
|
}
|
|
|
|
|
2017-01-19 06:20:14 +03:00
|
|
|
// Initialize the DOMString to a (nsStringBuffer, length) pair. The length
|
|
|
|
// does NOT have to be the full length of the (null-terminated) string in the
|
|
|
|
// nsStringBuffer.
|
2017-12-22 21:02:51 +03:00
|
|
|
void SetKnownLiveStringBuffer(nsStringBuffer* aStringBuffer,
|
|
|
|
uint32_t aLength) {
|
2017-12-22 21:02:51 +03:00
|
|
|
MOZ_ASSERT(mState == State::Empty, "We're already set to a value");
|
|
|
|
if (aLength != 0) {
|
|
|
|
SetStringBufferInternal(aStringBuffer, aLength);
|
|
|
|
mState = State::UnownedStringBuffer;
|
|
|
|
}
|
|
|
|
// else nothing to do
|
2013-10-11 20:28:23 +04:00
|
|
|
}
|
|
|
|
|
2017-12-22 21:02:51 +03:00
|
|
|
// Like SetKnownLiveStringBuffer, but holds a reference to the nsStringBuffer.
|
|
|
|
void SetStringBuffer(nsStringBuffer* aStringBuffer, uint32_t aLength) {
|
2017-12-22 21:02:51 +03:00
|
|
|
MOZ_ASSERT(mState == State::Empty, "We're already set to a value");
|
|
|
|
if (aLength != 0) {
|
|
|
|
SetStringBufferInternal(aStringBuffer, aLength);
|
|
|
|
aStringBuffer->AddRef();
|
|
|
|
mState = State::OwnedStringBuffer;
|
|
|
|
}
|
|
|
|
// else nothing to do
|
2017-01-19 06:20:14 +03:00
|
|
|
}
|
|
|
|
|
2017-12-22 21:02:51 +03:00
|
|
|
void SetKnownLiveString(const nsAString& aString) {
|
2014-08-14 02:39:41 +04:00
|
|
|
MOZ_ASSERT(mString.isNothing(), "We already have a string?");
|
2017-12-22 21:02:51 +03:00
|
|
|
MOZ_ASSERT(mState == State::Empty, "We're already set to a value");
|
2014-02-26 04:45:08 +04:00
|
|
|
MOZ_ASSERT(!mStringBuffer, "Setting stringbuffer twice?");
|
2017-12-22 21:02:51 +03:00
|
|
|
if (MOZ_UNLIKELY(aString.IsVoid())) {
|
2014-02-26 04:45:08 +04:00
|
|
|
SetNull();
|
|
|
|
} else if (!aString.IsEmpty()) {
|
2017-12-22 21:02:51 +03:00
|
|
|
nsStringBuffer* buf = nsStringBuffer::FromString(aString);
|
|
|
|
if (buf) {
|
|
|
|
SetKnownLiveStringBuffer(buf, aString.Length());
|
2017-12-22 21:02:51 +03:00
|
|
|
} else if (aString.IsLiteral()) {
|
|
|
|
SetLiteralInternal(aString.BeginReading(), aString.Length());
|
2017-12-22 21:02:51 +03:00
|
|
|
} else {
|
|
|
|
AsAString() = aString;
|
|
|
|
}
|
2014-02-26 04:45:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum NullHandling { eTreatNullAsNull, eTreatNullAsEmpty, eNullNotExpected };
|
|
|
|
|
2017-12-22 21:02:51 +03:00
|
|
|
void SetKnownLiveAtom(nsAtom* aAtom, NullHandling aNullHandling) {
|
2014-08-14 02:39:41 +04:00
|
|
|
MOZ_ASSERT(mString.isNothing(), "We already have a string?");
|
2017-12-22 21:02:51 +03:00
|
|
|
MOZ_ASSERT(mState == State::Empty, "We're already set to a value");
|
2018-07-10 21:21:42 +03:00
|
|
|
MOZ_ASSERT(!mAtom, "Setting atom twice?");
|
2014-02-26 04:45:08 +04:00
|
|
|
MOZ_ASSERT(aAtom || aNullHandling != eNullNotExpected);
|
|
|
|
if (aNullHandling == eNullNotExpected || aAtom) {
|
2018-03-07 07:17:34 +03:00
|
|
|
if (aAtom->IsStatic()) {
|
2018-07-10 21:21:42 +03:00
|
|
|
// Static atoms are backed by literals. Explicitly call AsStatic() here
|
|
|
|
// to avoid the extra IsStatic() checks in nsAtom::GetUTF16String().
|
|
|
|
SetLiteralInternal(aAtom->AsStatic()->GetUTF16String(),
|
|
|
|
aAtom->GetLength());
|
2017-10-12 02:52:17 +03:00
|
|
|
} else {
|
2018-07-10 21:21:42 +03:00
|
|
|
mAtom = aAtom->AsDynamic();
|
|
|
|
mState = State::UnownedAtom;
|
2017-10-12 02:52:17 +03:00
|
|
|
}
|
2014-02-26 04:45:08 +04:00
|
|
|
} else if (aNullHandling == eTreatNullAsNull) {
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-11 20:28:23 +04:00
|
|
|
void SetNull() {
|
|
|
|
MOZ_ASSERT(!mStringBuffer, "Should have no stringbuffer if null");
|
2014-08-14 02:39:41 +04:00
|
|
|
MOZ_ASSERT(mString.isNothing(), "Should have no string if null");
|
2017-12-22 21:02:51 +03:00
|
|
|
MOZ_ASSERT(mState == State::Empty, "Already set to a value?");
|
|
|
|
mState = State::Null;
|
2013-10-11 20:28:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsNull() const {
|
2014-08-14 02:39:41 +04:00
|
|
|
MOZ_ASSERT(!mStringBuffer || mString.isNothing(),
|
2013-10-11 20:28:23 +04:00
|
|
|
"How could we have a stringbuffer and a nonempty string?");
|
2017-12-22 21:02:51 +03:00
|
|
|
return mState == State::Null || (mString && mString->IsVoid());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsEmpty() const {
|
|
|
|
MOZ_ASSERT(!mStringBuffer || mString.isNothing(),
|
|
|
|
"How could we have a stringbuffer and a nonempty string?");
|
|
|
|
// This is not exact, because we might still have an empty XPCOM string.
|
|
|
|
// But that's OK; in that case the callers will try the XPCOM string
|
|
|
|
// themselves.
|
|
|
|
return mState == State::Empty;
|
2013-10-11 20:28:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ToString(nsAString& aString) {
|
|
|
|
if (IsNull()) {
|
|
|
|
SetDOMStringToNull(aString);
|
2017-12-22 21:02:51 +03:00
|
|
|
} else if (IsEmpty()) {
|
|
|
|
aString.Truncate();
|
2013-10-11 20:28:23 +04:00
|
|
|
} else if (HasStringBuffer()) {
|
2017-12-22 21:02:51 +03:00
|
|
|
// Don't share the nsStringBuffer with aString if the result would not
|
|
|
|
// be null-terminated.
|
|
|
|
nsStringBuffer* buf = StringBuffer();
|
|
|
|
uint32_t len = StringBufferLength();
|
|
|
|
auto chars = static_cast<char16_t*>(buf->Data());
|
|
|
|
if (chars[len] == '\0') {
|
|
|
|
// Safe to share the buffer.
|
|
|
|
buf->ToString(len, aString);
|
2013-10-11 20:28:23 +04:00
|
|
|
} else {
|
2017-12-22 21:02:51 +03:00
|
|
|
// We need to copy, unfortunately.
|
|
|
|
aString.Assign(chars, len);
|
2013-10-11 20:28:23 +04:00
|
|
|
}
|
2017-12-22 21:02:51 +03:00
|
|
|
} else if (HasLiteral()) {
|
|
|
|
aString.AssignLiteral(Literal(), LiteralLength());
|
2018-07-10 21:21:42 +03:00
|
|
|
} else if (HasAtom()) {
|
|
|
|
mAtom->ToString(aString);
|
2013-10-11 20:28:23 +04:00
|
|
|
} else {
|
|
|
|
aString = AsAString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-12-22 21:02:51 +03:00
|
|
|
void SetStringBufferInternal(nsStringBuffer* aStringBuffer,
|
|
|
|
uint32_t aLength) {
|
|
|
|
MOZ_ASSERT(mString.isNothing(), "We already have a string?");
|
|
|
|
MOZ_ASSERT(mState == State::Empty, "We're already set to a value");
|
|
|
|
MOZ_ASSERT(!mStringBuffer, "Setting stringbuffer twice?");
|
|
|
|
MOZ_ASSERT(aStringBuffer, "Why are we getting null?");
|
|
|
|
MOZ_ASSERT(aLength != 0, "Should not have empty string here");
|
|
|
|
mStringBuffer = aStringBuffer;
|
|
|
|
mLength = aLength;
|
|
|
|
}
|
|
|
|
|
2017-12-22 21:02:51 +03:00
|
|
|
void SetLiteralInternal(const char16_t* aLiteral, uint32_t aLength) {
|
|
|
|
MOZ_ASSERT(!mLiteral, "What's going on here?");
|
|
|
|
mLiteral = aLiteral;
|
|
|
|
mLength = aLength;
|
|
|
|
mState = State::Literal;
|
|
|
|
}
|
|
|
|
|
2017-12-22 21:02:51 +03:00
|
|
|
enum class State : uint8_t {
|
|
|
|
Empty, // An empty string. Default state.
|
|
|
|
Null, // Null (not a string at all)
|
|
|
|
|
|
|
|
// All states that involve actual string data should come after
|
|
|
|
// Empty and Null.
|
|
|
|
|
|
|
|
String, // An XPCOM string stored in mString.
|
2017-12-22 21:02:51 +03:00
|
|
|
Literal, // A string literal (static lifetime).
|
2018-07-10 21:21:42 +03:00
|
|
|
UnownedAtom, // mAtom is valid and we are not holding a ref.
|
|
|
|
// If we ever add an OwnedAtom state, XPCStringConvert::DynamicAtomToJSVal
|
|
|
|
// will need to grow an out param for whether the atom was shared.
|
2017-12-22 21:02:51 +03:00
|
|
|
OwnedStringBuffer, // mStringBuffer is valid and we have a ref to it.
|
|
|
|
UnownedStringBuffer, // mStringBuffer is valid; we are not holding a ref.
|
|
|
|
// The two string buffer values must come last. This lets us avoid doing
|
|
|
|
// two tests to figure out whether we have a stringbuffer.
|
|
|
|
};
|
|
|
|
|
2013-10-11 20:28:23 +04:00
|
|
|
// We need to be able to act like a string as needed
|
|
|
|
Maybe<nsAutoString> mString;
|
|
|
|
|
2017-12-22 21:02:51 +03:00
|
|
|
union {
|
|
|
|
// The nsStringBuffer in the OwnedStringBuffer/UnownedStringBuffer cases.
|
|
|
|
nsStringBuffer* MOZ_UNSAFE_REF(
|
|
|
|
"The ways in which this can be safe are "
|
2015-01-18 22:15:48 +03:00
|
|
|
"documented above and enforced through "
|
|
|
|
"assertions") mStringBuffer;
|
2017-12-22 21:02:51 +03:00
|
|
|
// The literal in the Literal case.
|
|
|
|
const char16_t* mLiteral;
|
2018-07-10 21:21:42 +03:00
|
|
|
// The atom in the UnownedAtom case.
|
|
|
|
nsDynamicAtom* MOZ_UNSAFE_REF(
|
|
|
|
"The ways in which this can be safe are "
|
|
|
|
"documented above and enforced through "
|
|
|
|
"assertions") mAtom;
|
2017-12-22 21:02:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Length in the stringbuffer and literal cases.
|
2013-10-11 20:28:23 +04:00
|
|
|
uint32_t mLength;
|
2017-12-22 21:02:51 +03:00
|
|
|
|
|
|
|
State mState;
|
2013-10-11 20:28:23 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_DOMString_h
|