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 #ifndef nsLayoutStylesheetCache_h__
8 #define nsLayoutStylesheetCache_h__
9 
10 #include "nsIMemoryReporter.h"
11 #include "nsIObserver.h"
12 #include "mozilla/Attributes.h"
13 #include "mozilla/MemoryReporting.h"
14 #include "mozilla/StaticPtr.h"
15 #include "mozilla/StyleBackendType.h"
16 #include "mozilla/css/Loader.h"
17 
18 class nsIFile;
19 class nsIURI;
20 
21 namespace mozilla {
22 class CSSStyleSheet;
23 }  // namespace mozilla
24 
25 namespace mozilla {
26 namespace css {
27 
28 // Enum defining how error should be handled.
29 enum FailureAction { eCrash = 0, eLogToConsole };
30 
31 }  // namespace css
32 }  // namespace mozilla
33 
34 class nsLayoutStylesheetCache final : public nsIObserver,
35                                       public nsIMemoryReporter {
36   NS_DECL_ISUPPORTS
37   NS_DECL_NSIOBSERVER
38   NS_DECL_NSIMEMORYREPORTER
39 
40   /**
41    * Returns the nsLayoutStylesheetCache for the given style backend type.
42    * Callers should pass in a value for aType that matches the style system
43    * backend type for the style set in use.  (A process may call For
44    * and obtain nsLayoutStylesheetCache objects for both backend types,
45    * and a particular UA style sheet might be cached in both, one or neither
46    * nsLayoutStylesheetCache.)
47    */
48   static nsLayoutStylesheetCache* For(mozilla::StyleBackendType aType);
49 
50   mozilla::StyleSheet* ScrollbarsSheet();
51   mozilla::StyleSheet* FormsSheet();
52   // This function is expected to return nullptr when the dom.forms.number
53   // pref is disabled.
54   mozilla::StyleSheet* NumberControlSheet();
55   mozilla::StyleSheet* UserContentSheet();
56   mozilla::StyleSheet* UserChromeSheet();
57   mozilla::StyleSheet* UASheet();
58   mozilla::StyleSheet* HTMLSheet();
59   mozilla::StyleSheet* MinimalXULSheet();
60   mozilla::StyleSheet* XULSheet();
61   mozilla::StyleSheet* XULComponentsSheet();
62   mozilla::StyleSheet* QuirkSheet();
63   mozilla::StyleSheet* SVGSheet();
64   mozilla::StyleSheet* MathMLSheet();
65   mozilla::StyleSheet* CounterStylesSheet();
66   mozilla::StyleSheet* NoScriptSheet();
67   mozilla::StyleSheet* NoFramesSheet();
68   mozilla::StyleSheet* ChromePreferenceSheet(nsPresContext* aPresContext);
69   mozilla::StyleSheet* ContentPreferenceSheet(nsPresContext* aPresContext);
70   mozilla::StyleSheet* ContentEditableSheet();
71   mozilla::StyleSheet* DesignModeSheet();
72 
73   static void InvalidatePreferenceSheets();
74 
75   static void Shutdown();
76 
77   static void SetUserContentCSSURL(nsIURI* aURI);
78 
79   size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
80 
81  private:
82   explicit nsLayoutStylesheetCache(mozilla::StyleBackendType aImpl);
83   ~nsLayoutStylesheetCache();
84 
85   void InitFromProfile();
86   void InitMemoryReporter();
87   void LoadSheetURL(const char* aURL, RefPtr<mozilla::StyleSheet>* aSheet,
88                     mozilla::css::SheetParsingMode aParsingMode,
89                     mozilla::css::FailureAction aFailureAction);
90   void LoadSheetFile(nsIFile* aFile, RefPtr<mozilla::StyleSheet>* aSheet,
91                      mozilla::css::SheetParsingMode aParsingMode,
92                      mozilla::css::FailureAction aFailureAction);
93   void LoadSheet(nsIURI* aURI, RefPtr<mozilla::StyleSheet>* aSheet,
94                  mozilla::css::SheetParsingMode aParsingMode,
95                  mozilla::css::FailureAction aFailureAction);
96   static void InvalidateSheet(RefPtr<mozilla::StyleSheet>* aGeckoSheet,
97                               RefPtr<mozilla::StyleSheet>* aServoSheet);
98   static void DependentPrefChanged(const char* aPref, void* aData);
99   void BuildPreferenceSheet(RefPtr<mozilla::StyleSheet>* aSheet,
100                             nsPresContext* aPresContext);
101 
102   static mozilla::StaticRefPtr<nsLayoutStylesheetCache> gStyleCache_Gecko;
103   static mozilla::StaticRefPtr<nsLayoutStylesheetCache> gStyleCache_Servo;
104   static mozilla::StaticRefPtr<mozilla::css::Loader> gCSSLoader_Gecko;
105   static mozilla::StaticRefPtr<mozilla::css::Loader> gCSSLoader_Servo;
106   static mozilla::StaticRefPtr<nsIURI> gUserContentSheetURL_Gecko;
107   static mozilla::StaticRefPtr<nsIURI> gUserContentSheetURL_Servo;
108   mozilla::StyleBackendType mBackendType;
109   RefPtr<mozilla::StyleSheet> mChromePreferenceSheet;
110   RefPtr<mozilla::StyleSheet> mContentEditableSheet;
111   RefPtr<mozilla::StyleSheet> mContentPreferenceSheet;
112   RefPtr<mozilla::StyleSheet> mCounterStylesSheet;
113   RefPtr<mozilla::StyleSheet> mDesignModeSheet;
114   RefPtr<mozilla::StyleSheet> mFormsSheet;
115   RefPtr<mozilla::StyleSheet> mHTMLSheet;
116   RefPtr<mozilla::StyleSheet> mMathMLSheet;
117   RefPtr<mozilla::StyleSheet> mMinimalXULSheet;
118   RefPtr<mozilla::StyleSheet> mNoFramesSheet;
119   RefPtr<mozilla::StyleSheet> mNoScriptSheet;
120   RefPtr<mozilla::StyleSheet> mNumberControlSheet;
121   RefPtr<mozilla::StyleSheet> mQuirkSheet;
122   RefPtr<mozilla::StyleSheet> mSVGSheet;
123   RefPtr<mozilla::StyleSheet> mScrollbarsSheet;
124   RefPtr<mozilla::StyleSheet> mUASheet;
125   RefPtr<mozilla::StyleSheet> mUserChromeSheet;
126   RefPtr<mozilla::StyleSheet> mUserContentSheet;
127   RefPtr<mozilla::StyleSheet> mXULSheet;
128   RefPtr<mozilla::StyleSheet> mXULComponentsSheet;
129 };
130 
131 #endif
132