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 /* representation of CSSRuleList for stylo */
8 
9 #ifndef mozilla_ServoCSSRuleList_h
10 #define mozilla_ServoCSSRuleList_h
11 
12 #include "mozilla/ServoBindingTypes.h"
13 #include "mozilla/dom/CSSRuleList.h"
14 #include "nsDataHashtable.h"
15 
16 namespace mozilla {
17 
18 class ServoStyleRule;
19 class ServoStyleSheet;
20 namespace css {
21 class GroupRule;
22 class Rule;
23 }  // namespace css
24 
25 class ServoCSSRuleList final : public dom::CSSRuleList {
26  public:
27   // @param aDirectOwnerStyleSheet should be set to the owner stylesheet
28   // if this rule list is owned directly by a stylesheet, which means it
29   // is a top level CSSRuleList. If it's owned by a group rule, nullptr.
30   // If this param is set, the caller doesn't need to call SetStyleSheet.
31   ServoCSSRuleList(already_AddRefed<ServoCssRules> aRawRules,
32                    ServoStyleSheet* aDirectOwnerStyleSheet);
GetParentRule()33   css::GroupRule* GetParentRule() const { return mParentRule; }
34   void SetParentRule(css::GroupRule* aParentRule);
35   void SetStyleSheet(StyleSheet* aSheet);
36 
37   NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ServoCSSRuleList,dom::CSSRuleList)38   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ServoCSSRuleList, dom::CSSRuleList)
39 
40   ServoStyleSheet* GetParentObject() final { return mStyleSheet; }
41 
42   css::Rule* IndexedGetter(uint32_t aIndex, bool& aFound) final;
Length()43   uint32_t Length() final { return mRules.Length(); }
44 
45   void DropReference();
46 
47   css::Rule* GetRule(uint32_t aIndex);
48   nsresult InsertRule(const nsAString& aRule, uint32_t aIndex);
49   nsresult DeleteRule(uint32_t aIndex);
50 
51   uint16_t GetDOMCSSRuleType(uint32_t aIndex) const;
52 
53  private:
54   virtual ~ServoCSSRuleList();
55 
56   // XXX Is it possible to have an address lower than or equal to 255?
57   //     Is it possible to have more than 255 CSS rule types?
58   static const uintptr_t kMaxRuleType = UINT8_MAX;
59 
CastToUint(css::Rule * aPtr)60   static uintptr_t CastToUint(css::Rule* aPtr) {
61     return reinterpret_cast<uintptr_t>(aPtr);
62   }
CastToPtr(uintptr_t aInt)63   static css::Rule* CastToPtr(uintptr_t aInt) {
64     MOZ_ASSERT(aInt > kMaxRuleType);
65     return reinterpret_cast<css::Rule*>(aInt);
66   }
67 
68   template <typename Func>
69   void EnumerateInstantiatedRules(Func aCallback);
70 
71   void DropAllRules();
72 
73   // mStyleSheet may be nullptr when it drops the reference to us.
74   ServoStyleSheet* mStyleSheet = nullptr;
75   // mParentRule is nullptr if it isn't a nested rule list.
76   css::GroupRule* mParentRule = nullptr;
77   RefPtr<ServoCssRules> mRawRules;
78   // Array stores either a number indicating rule type, or a pointer to
79   // css::Rule. If the value is less than kMaxRuleType, the given rule
80   // instance has not been constructed, and the value means the type
81   // of the rule. Otherwise, it is a pointer.
82   nsTArray<uintptr_t> mRules;
83 };
84 
85 }  // namespace mozilla
86 
87 #endif  // mozilla_ServoCSSRuleList_h
88