author Andreas Pehrson <>
Tue, 12 Nov 2019 10:30:16 +0000
changeset 501560 9e3f44e87a1a2be927c7fab135653c6b86b982c9
parent 500906 4d77157a8980ce8842f53ef8a8911aa2982c0321
permissions -rw-r--r--
Bug 1594466 - Don't rewrite mimeType if fully defined. r=bryce Prior to this patch the mimetype was rewritten per the printf format "%s; codecs=%s" also when codecs were defined in the constrained mime type. The latter '%s' would be the codecs string from the mime type parser, which would have dropped any quotation marks surrounding the string. Hence 'codecs="vp8, opus"' would be considered supported (quotation marks included), but when selecting mime type in start(), it would be rewritten with quotation marks dropped. Thus looking like 'codecs=vp8,' which is not supported. This patch removes the rewrite step when the mime type is fully defined with codecs, so that the quotation marks are left in place as given to the constructor. Differential Revision:

/* -*- 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 */

#ifndef nsAttrValueInlines_h__
#define nsAttrValueInlines_h__

#include <stdint.h>

#include "nsAttrValue.h"
#include "mozilla/Atomics.h"
#include "mozilla/Attributes.h"
#include "mozilla/ServoUtils.h"

namespace mozilla {
class ShadowParts;

struct MiscContainer final {
  typedef nsAttrValue::ValueType ValueType;

  ValueType mType;
  // mStringBits points to either nsAtom* or nsStringBuffer* and is used when
  // mType isn't eCSSDeclaration.
  // Note eStringBase and eAtomBase is used also to handle the type of
  // mStringBits.
  // Note that we use an atomic here so that we can use Compare-And-Swap
  // to cache the serialization during the parallel servo traversal. This case
  // (which happens when the main thread is blocked) is the only case where
  // mStringBits is mutated off-main-thread. The Atomic needs to be
  // ReleaseAcquire so that the pointer to the serialization does not become
  // observable to other threads before the initialization of the pointed-to
  // memory is also observable.
  mozilla::Atomic<uintptr_t, mozilla::ReleaseAcquire> mStringBits;
  union {
    struct {
      union {
        int32_t mInteger;
        nscolor mColor;
        uint32_t mEnumValue;
        mozilla::DeclarationBlock* mCSSDeclaration;
        nsIURI* mURL;
        mozilla::AtomArray* mAtomArray;
        nsIntMargin* mIntMargin;
        const mozilla::ShadowParts* mShadowParts;
        const mozilla::SVGAnimatedIntegerPair* mSVGAnimatedIntegerPair;
        const mozilla::SVGAnimatedLength* mSVGLength;
        const mozilla::SVGAnimatedNumberPair* mSVGAnimatedNumberPair;
        const mozilla::SVGAnimatedOrient* mSVGAnimatedOrient;
        const mozilla::SVGAnimatedPreserveAspectRatio*
        const mozilla::SVGAnimatedViewBox* mSVGAnimatedViewBox;
        const mozilla::SVGLengthList* mSVGLengthList;
        const mozilla::SVGNumberList* mSVGNumberList;
        const mozilla::SVGPathData* mSVGPathData;
        const mozilla::SVGPointList* mSVGPointList;
        const mozilla::SVGStringList* mSVGStringList;
        const mozilla::SVGTransformList* mSVGTransformList;
      uint32_t mRefCount : 31;
      uint32_t mCached : 1;
    } mValue;
    double mDoubleValue;

  MiscContainer() : mType(nsAttrValue::eColor), mStringBits(0) {
    mValue.mColor = 0;
    mValue.mRefCount = 0;
    mValue.mCached = 0;

  // Only nsAttrValue should be able to delete us.
  friend class nsAttrValue;

  ~MiscContainer() {
    if (IsRefCounted()) {
      MOZ_ASSERT(mValue.mRefCount == 0);

  bool GetString(nsAString& aString) const;

  void SetStringBitsMainThread(uintptr_t aBits) {
    // mStringBits is atomic, but the callers of this function are
    // single-threaded so they don't have to worry about it.
    mStringBits = aBits;

  inline bool IsRefCounted() const {
    // Nothing stops us from refcounting (and sharing) other types of
    // MiscContainer (except eDoubleValue types) but there's no compelling
    // reason to.
    return mType == nsAttrValue::eCSSDeclaration ||
           mType == nsAttrValue::eShadowParts;

  inline int32_t AddRef() {
    return ++mValue.mRefCount;

  inline int32_t Release() {
    return --mValue.mRefCount;

  void Cache();
  void Evict();

 * Implementation of inline methods

inline int32_t nsAttrValue::GetIntegerValue() const {
  MOZ_ASSERT(Type() == eInteger, "wrong type");
  return (BaseType() == eIntegerBase) ? GetIntInternal()
                                      : GetMiscContainer()->mValue.mInteger;

inline int16_t nsAttrValue::GetEnumValue() const {
  MOZ_ASSERT(Type() == eEnum, "wrong type");
  // We don't need to worry about sign extension here since we're
  // returning an int16_t which will cut away the top bits.
  return static_cast<int16_t>(((BaseType() == eIntegerBase)
                                   ? static_cast<uint32_t>(GetIntInternal())
                                   : GetMiscContainer()->mValue.mEnumValue) >>

inline double nsAttrValue::GetPercentValue() const {
  MOZ_ASSERT(Type() == ePercent, "wrong type");
  if (BaseType() == eIntegerBase) {
    return GetIntInternal() / 100.0f;
  return GetMiscContainer()->mDoubleValue / 100.0f;

inline mozilla::AtomArray* nsAttrValue::GetAtomArrayValue() const {
  MOZ_ASSERT(Type() == eAtomArray, "wrong type");
  return GetMiscContainer()->mValue.mAtomArray;

inline mozilla::DeclarationBlock* nsAttrValue::GetCSSDeclarationValue() const {
  MOZ_ASSERT(Type() == eCSSDeclaration, "wrong type");
  return GetMiscContainer()->mValue.mCSSDeclaration;

inline nsIURI* nsAttrValue::GetURLValue() const {
  MOZ_ASSERT(Type() == eURL, "wrong type");
  return GetMiscContainer()->mValue.mURL;

inline double nsAttrValue::GetDoubleValue() const {
  MOZ_ASSERT(Type() == eDoubleValue, "wrong type");
  return GetMiscContainer()->mDoubleValue;

inline bool nsAttrValue::GetIntMarginValue(nsIntMargin& aMargin) const {
  MOZ_ASSERT(Type() == eIntMarginValue, "wrong type");
  nsIntMargin* m = GetMiscContainer()->mValue.mIntMargin;
  if (!m) return false;
  aMargin = *m;
  return true;

inline bool nsAttrValue::IsSVGType(ValueType aType) const {
  return aType >= eSVGTypesBegin && aType <= eSVGTypesEnd;

inline bool nsAttrValue::StoresOwnData() const {
  if (BaseType() != eOtherBase) {
    return true;
  ValueType t = Type();
  return t != eCSSDeclaration && !IsSVGType(t);

inline void nsAttrValue::SetPtrValueAndType(void* aValue, ValueBaseType aType) {
               "pointer not properly aligned, this will crash");
  mBits = reinterpret_cast<intptr_t>(aValue) | aType;

inline void nsAttrValue::ResetIfSet() {
  if (mBits) {

inline MiscContainer* nsAttrValue::GetMiscContainer() const {
  NS_ASSERTION(BaseType() == eOtherBase, "wrong type");
  return static_cast<MiscContainer*>(GetPtr());

inline int32_t nsAttrValue::GetIntInternal() const {
  NS_ASSERTION(BaseType() == eIntegerBase, "getting integer from non-integer");
  // Make sure we get a signed value.
  // Lets hope the optimizer optimizes this into a shift. Unfortunatly signed
  // bitshift right is implementaion dependant.
  return static_cast<int32_t>(mBits & ~NS_ATTRVALUE_INTEGERTYPE_MASK) /

inline nsAttrValue::ValueType nsAttrValue::Type() const {
  switch (BaseType()) {
    case eIntegerBase: {
      return static_cast<ValueType>(mBits & NS_ATTRVALUE_INTEGERTYPE_MASK);
    case eOtherBase: {
      return GetMiscContainer()->mType;
    default: {
      return static_cast<ValueType>(static_cast<uint16_t>(BaseType()));

inline nsAtom* nsAttrValue::GetAtomValue() const {
  MOZ_ASSERT(Type() == eAtom, "wrong type");
  return reinterpret_cast<nsAtom*>(GetPtr());

inline void nsAttrValue::ToString(mozilla::dom::DOMString& aResult) const {
  switch (Type()) {
    case eString: {
      nsStringBuffer* str = static_cast<nsStringBuffer*>(GetPtr());
      if (str) {
            str, str->StorageSize() / sizeof(char16_t) - 1);
      // else aResult is already empty
    case eAtom: {
      nsAtom* atom = static_cast<nsAtom*>(GetPtr());
      aResult.SetKnownLiveAtom(atom, mozilla::dom::DOMString::eNullNotExpected);
    default: {