1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 /*
7  * internal interface representing CSS style rules that contain other
8  * rules, such as @media rules
9  */
10 
11 #ifndef mozilla_css_GroupRule_h__
12 #define mozilla_css_GroupRule_h__
13 
14 #include "mozilla/Attributes.h"
15 #include "mozilla/IncrementalClearCOMRuleArray.h"
16 #include "mozilla/MemoryReporting.h"
17 #include "mozilla/css/Rule.h"
18 #include "nsCycleCollectionParticipant.h"
19 
20 class nsPresContext;
21 class nsMediaQueryResultCacheKey;
22 
23 namespace mozilla {
24 
25 class CSSStyleSheet;
26 
27 namespace css {
28 
29 class GroupRuleRuleList;
30 
31 // inherits from Rule so it can be shared between
32 // MediaRule and DocumentRule
33 class GroupRule : public Rule
34 {
35 protected:
36   GroupRule(uint32_t aLineNumber, uint32_t aColumnNumber);
37   GroupRule(const GroupRule& aCopy);
38   virtual ~GroupRule();
39 public:
40 
41   NS_DECL_CYCLE_COLLECTION_CLASS(GroupRule)
42   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
43 
44   // implement part of Rule
45   DECL_STYLE_RULE_INHERIT_NO_DOMRULE
46 #ifdef DEBUG
47   virtual void List(FILE* out = stdout, int32_t aIndent = 0) const override;
48 #endif
49   virtual void SetStyleSheet(CSSStyleSheet* aSheet) override;
50 
51 public:
52   void AppendStyleRule(Rule* aRule);
53 
StyleRuleCount()54   int32_t StyleRuleCount() const { return mRules.Count(); }
55   Rule* GetStyleRuleAt(int32_t aIndex) const;
56 
57   typedef IncrementalClearCOMRuleArray::nsCOMArrayEnumFunc RuleEnumFunc;
58   bool EnumerateRulesForwards(RuleEnumFunc aFunc, void * aData) const;
59 
60   /*
61    * The next three methods should never be called unless you have first
62    * called WillDirty() on the parent stylesheet.  After they are
63    * called, DidDirty() needs to be called on the sheet.
64    */
65   nsresult DeleteStyleRuleAt(uint32_t aIndex);
66   nsresult InsertStyleRuleAt(uint32_t aIndex, Rule* aRule);
67 
68   virtual bool UseForPresentation(nsPresContext* aPresContext,
69                                     nsMediaQueryResultCacheKey& aKey) = 0;
70 
71   // non-virtual -- it is only called by subclasses
72   size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
73   virtual size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const override = 0;
74 
75   static bool
CloneRuleInto(Rule * aRule,void * aArray)76   CloneRuleInto(Rule* aRule, void* aArray)
77   {
78     RefPtr<Rule> clone = aRule->Clone();
79     static_cast<IncrementalClearCOMRuleArray*>(aArray)->AppendObject(clone);
80     return true;
81   }
82 
83 protected:
84   // to help implement nsIDOMCSSRule
85   void AppendRulesToCssText(nsAString& aCssText);
86 
87   // to implement common methods on nsIDOMCSSMediaRule and
88   // nsIDOMCSSMozDocumentRule
89   nsresult GetCssRules(nsIDOMCSSRuleList* *aRuleList);
90   nsresult InsertRule(const nsAString & aRule, uint32_t aIndex,
91                       uint32_t* _retval);
92   nsresult DeleteRule(uint32_t aIndex);
93 
94   IncrementalClearCOMRuleArray mRules;
95   RefPtr<GroupRuleRuleList> mRuleCollection; // lazily constructed
96 };
97 
98 } // namespace css
99 } // namespace mozilla
100 
101 #endif /* mozilla_css_GroupRule_h__ */
102