tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

CSSFontFaceRule.cpp (7621B)


      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 #include "mozilla/dom/CSSFontFaceRule.h"
      8 
      9 #include "mozilla/ServoBindings.h"
     10 #include "mozilla/dom/CSSFontFaceRuleBinding.h"
     11 #include "mozilla/dom/CSSStyleDeclarationBinding.h"
     12 #include "nsCSSProps.h"
     13 
     14 using namespace mozilla;
     15 using namespace mozilla::dom;
     16 
     17 // -------------------------------------------
     18 // CSSFontFaceRuleDecl and related routines
     19 //
     20 
     21 // QueryInterface implementation for CSSFontFaceRuleDecl
     22 NS_INTERFACE_MAP_BEGIN(CSSFontFaceRuleDecl)
     23  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
     24  NS_INTERFACE_MAP_ENTRY(nsICSSDeclaration)
     25  NS_INTERFACE_MAP_ENTRY(nsISupports)
     26  // We forward the cycle collection interfaces to ContainingRule(), which is
     27  // never null (in fact, we're part of that object!)
     28  if (aIID.Equals(NS_GET_IID(nsCycleCollectionISupports)) ||
     29      aIID.Equals(NS_GET_IID(nsXPCOMCycleCollectionParticipant))) {
     30    return ContainingRule()->QueryInterface(aIID, aInstancePtr);
     31  } else
     32 NS_INTERFACE_MAP_END
     33 
     34 NS_IMPL_ADDREF_USING_AGGREGATOR(CSSFontFaceRuleDecl, ContainingRule())
     35 NS_IMPL_RELEASE_USING_AGGREGATOR(CSSFontFaceRuleDecl, ContainingRule())
     36 
     37 void CSSFontFaceRuleDecl::SetRawAfterClone(
     38    RefPtr<StyleLockedFontFaceRule> aRaw) {
     39  mRawRule = std::move(aRaw);
     40 }
     41 
     42 // helper for string GetPropertyValue and RemovePropertyValue
     43 void CSSFontFaceRuleDecl::GetPropertyValue(nsCSSFontDesc aFontDescID,
     44                                           nsACString& aResult) const {
     45  MOZ_ASSERT(aResult.IsEmpty());
     46  Servo_FontFaceRule_GetDescriptorCssText(mRawRule, aFontDescID, &aResult);
     47 }
     48 
     49 void CSSFontFaceRuleDecl::GetCssText(nsACString& aCssText) {
     50  MOZ_ASSERT(aCssText.IsEmpty());
     51  Servo_FontFaceRule_GetDeclCssText(mRawRule, &aCssText);
     52 }
     53 
     54 void CSSFontFaceRuleDecl::SetCssText(const nsACString& aCssText,
     55                                     nsIPrincipal* aSubjectPrincipal,
     56                                     ErrorResult& aRv) {
     57  if (ContainingRule()->IsReadOnly()) {
     58    return;
     59  }
     60  // bug 443978
     61  aRv.ThrowNotSupportedError(
     62      "Can't set cssText on CSSFontFaceRule declarations");
     63 }
     64 
     65 void CSSFontFaceRuleDecl::GetPropertyValue(const nsACString& aPropName,
     66                                           nsACString& aResult) {
     67  aResult.Truncate();
     68  nsCSSFontDesc descID = nsCSSProps::LookupFontDesc(aPropName);
     69  if (descID != eCSSFontDesc_UNKNOWN) {
     70    GetPropertyValue(descID, aResult);
     71  }
     72 }
     73 
     74 void CSSFontFaceRuleDecl::RemoveProperty(const nsACString& aPropName,
     75                                         nsACString& aResult,
     76                                         ErrorResult& aRv) {
     77  nsCSSFontDesc descID = nsCSSProps::LookupFontDesc(aPropName);
     78  NS_ASSERTION(descID >= eCSSFontDesc_UNKNOWN && descID < eCSSFontDesc_COUNT,
     79               "LookupFontDesc returned value out of range");
     80 
     81  if (ContainingRule()->IsReadOnly()) {
     82    return;
     83  }
     84 
     85  aResult.Truncate();
     86  if (descID != eCSSFontDesc_UNKNOWN) {
     87    GetPropertyValue(descID, aResult);
     88    Servo_FontFaceRule_ResetDescriptor(mRawRule, descID);
     89  }
     90 }
     91 
     92 void CSSFontFaceRuleDecl::GetPropertyPriority(const nsACString& aPropName,
     93                                              nsACString& aResult) {
     94  // font descriptors do not have priorities at present
     95  aResult.Truncate();
     96 }
     97 
     98 void CSSFontFaceRuleDecl::SetProperty(const nsACString& aPropName,
     99                                      const nsACString& aValue,
    100                                      const nsACString& aPriority,
    101                                      nsIPrincipal* aSubjectPrincipal,
    102                                      ErrorResult& aRv) {
    103  // FIXME(heycam): If we are changing unicode-range, then a FontFace object
    104  // representing this rule must have its mUnicodeRange value invalidated.
    105 
    106  if (ContainingRule()->IsReadOnly()) {
    107    return;
    108  }
    109 
    110  // bug 443978
    111  aRv.ThrowNotSupportedError(
    112      "Can't set properties on CSSFontFaceRule declarations");
    113 }
    114 
    115 uint32_t CSSFontFaceRuleDecl::Length() {
    116  return Servo_FontFaceRule_Length(mRawRule);
    117 }
    118 
    119 void CSSFontFaceRuleDecl::IndexedGetter(uint32_t aIndex, bool& aFound,
    120                                        nsACString& aResult) {
    121  nsCSSFontDesc id = Servo_FontFaceRule_IndexGetter(mRawRule, aIndex);
    122  if (id != eCSSFontDesc_UNKNOWN) {
    123    aFound = true;
    124    aResult.Assign(nsCSSProps::GetStringValue(id));
    125  } else {
    126    aFound = false;
    127  }
    128 }
    129 
    130 css::Rule* CSSFontFaceRuleDecl::GetParentRule() { return ContainingRule(); }
    131 
    132 nsINode* CSSFontFaceRuleDecl::GetAssociatedNode() const {
    133  return ContainingRule()->GetAssociatedDocumentOrShadowRoot();
    134 }
    135 
    136 nsISupports* CSSFontFaceRuleDecl::GetParentObject() const {
    137  return ContainingRule()->GetParentObject();
    138 }
    139 
    140 JSObject* CSSFontFaceRuleDecl::WrapObject(JSContext* cx,
    141                                          JS::Handle<JSObject*> aGivenProto) {
    142  // If this changes to use a different type, remove the 'concrete'
    143  // annotation from CSSStyleDeclaration.
    144  return CSSStyleDeclaration_Binding::Wrap(cx, this, aGivenProto);
    145 }
    146 
    147 // -------------------------------------------
    148 // CSSFontFaceRule
    149 //
    150 
    151 NS_IMPL_CYCLE_COLLECTION_CLASS(CSSFontFaceRule)
    152 
    153 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(CSSFontFaceRule,
    154                                               mozilla::css::Rule)
    155  // Keep this in sync with IsCCLeaf.
    156 
    157  // Trace the wrapper for our declaration.  This just expands out
    158  // NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER which we can't use
    159  // directly because the wrapper is on the declaration, not on us.
    160  tmp->mDecl.TraceWrapper(aCallbacks, aClosure);
    161 NS_IMPL_CYCLE_COLLECTION_TRACE_END
    162 
    163 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(CSSFontFaceRule)
    164  // Keep this in sync with IsCCLeaf.
    165 
    166  // Unlink the wrapper for our declaration.
    167  //
    168  // Note that this has to happen before unlinking css::Rule.
    169  tmp->UnlinkDeclarationWrapper(tmp->mDecl);
    170 NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(mozilla::css::Rule)
    171 
    172 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(CSSFontFaceRule,
    173                                                  mozilla::css::Rule)
    174  // Keep this in sync with IsCCLeaf.
    175 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
    176 
    177 bool CSSFontFaceRule::IsCCLeaf() const {
    178  if (!Rule::IsCCLeaf()) {
    179    return false;
    180  }
    181 
    182  return !mDecl.PreservingWrapper();
    183 }
    184 
    185 NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(CSSFontFaceRule,
    186                                               mozilla::css::Rule)
    187 
    188 #ifdef DEBUG
    189 void CSSFontFaceRule::List(FILE* out, int32_t aIndent) const {
    190  nsAutoCString str;
    191  for (int32_t i = 0; i < aIndent; i++) {
    192    str.AppendLiteral("  ");
    193  }
    194  Servo_FontFaceRule_Debug(Raw(), &str);
    195  fprintf_stderr(out, "%s\n", str.get());
    196 }
    197 #endif
    198 
    199 StyleCssRuleType CSSFontFaceRule::Type() const {
    200  return StyleCssRuleType::FontFace;
    201 }
    202 
    203 void CSSFontFaceRule::SetRawAfterClone(RefPtr<StyleLockedFontFaceRule> aRaw) {
    204  mDecl.SetRawAfterClone(std::move(aRaw));
    205 }
    206 
    207 void CSSFontFaceRule::GetCssText(nsACString& aCssText) const {
    208  aCssText.Truncate();
    209  Servo_FontFaceRule_GetCssText(Raw(), &aCssText);
    210 }
    211 
    212 nsICSSDeclaration* CSSFontFaceRule::Style() { return &mDecl; }
    213 
    214 /* virtual */
    215 size_t CSSFontFaceRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
    216  return aMallocSizeOf(this);
    217 }
    218 
    219 /* virtual */
    220 JSObject* CSSFontFaceRule::WrapObject(JSContext* aCx,
    221                                      JS::Handle<JSObject*> aGivenProto) {
    222  return CSSFontFaceRule_Binding::Wrap(aCx, this, aGivenProto);
    223 }