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/. */
7 #ifndef nsLayoutStylesheetCache_h__
8 #define nsLayoutStylesheetCache_h__
10 #include "nsIMemoryReporter.h"
11 #include "nsIObserver.h"
12 #include "base/shared_memory.h"
13 #include "mozilla/Attributes.h"
14 #include "mozilla/MemoryReporting.h"
15 #include "mozilla/PreferenceSheet.h"
16 #include "mozilla/NotNull.h"
17 #include "mozilla/StaticPtr.h"
18 #include "mozilla/css/Loader.h"
25 } // namespace mozilla
30 // Enum defining how error should be handled.
31 enum FailureAction
{ eCrash
= 0, eLogToConsole
};
34 } // namespace mozilla
36 // Reference counted wrapper around a base::SharedMemory that will store the
37 // User Agent style sheets.
38 struct nsLayoutStylesheetCacheShm final
{
39 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(nsLayoutStylesheetCacheShm
)
40 base::SharedMemory mShm
;
43 ~nsLayoutStylesheetCacheShm() = default;
46 class nsLayoutStylesheetCache final
: public nsIObserver
,
47 public nsIMemoryReporter
{
49 using Shm
= nsLayoutStylesheetCacheShm
;
53 NS_DECL_NSIMEMORYREPORTER
55 static nsLayoutStylesheetCache
* Singleton();
57 #define STYLE_SHEET(identifier_, url_, shared_) \
58 mozilla::NotNull<mozilla::StyleSheet*> identifier_##Sheet();
59 #include "mozilla/UserAgentStyleSheetList.h"
62 mozilla::StyleSheet
* GetUserContentSheet();
63 mozilla::StyleSheet
* GetUserChromeSheet();
64 mozilla::StyleSheet
* ChromePreferenceSheet();
65 mozilla::StyleSheet
* ContentPreferenceSheet();
67 static void InvalidatePreferenceSheets();
69 static void Shutdown();
71 static void SetUserContentCSSURL(nsIURI
* aURI
);
73 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf
) const;
75 // Set the shared memory segment to load the shared UA sheets from.
76 // Called early on in a content process' life from
77 // ContentChild::InitSharedUASheets, before the nsLayoutStylesheetCache
78 // singleton has been created.
79 static void SetSharedMemory(const base::SharedMemoryHandle
& aHandle
,
82 // Obtain a shared memory handle for the shared UA sheets to pass into a
83 // content process. Called by ContentParent::InitInternal shortly after
84 // a content process has been created.
85 bool ShareToProcess(base::ProcessId aProcessId
,
86 base::SharedMemoryHandle
* aHandle
);
88 // Returns the address of the shared memory segment that holds the shared UA
90 uintptr_t GetSharedMemoryAddress() {
91 return mSharedMemory
? uintptr_t(mSharedMemory
->mShm
.memory()) : 0;
94 // Size of the shared memory buffer we'll create to store the shared UA
95 // sheets. We choose a value that is big enough on both 64 bit and 32 bit.
97 // If this isn't big enough for the current contents of the shared UA
98 // sheets, we'll crash under InitSharedSheetsInParent.
99 static constexpr size_t kSharedMemorySize
= 1024 * 450;
102 // Shared memory header.
104 static constexpr uint32_t kMagic
= 0x55415353;
105 uint32_t mMagic
; // Must be set to kMagic.
106 const ServoCssRules
* mSheets
[size_t(mozilla::UserAgentStyleSheetID::Count
)];
110 nsLayoutStylesheetCache();
111 ~nsLayoutStylesheetCache();
113 void InitFromProfile();
114 void InitSharedSheetsInParent();
115 void InitSharedSheetsInChild(already_AddRefed
<Shm
> aSharedMemory
);
116 void InitMemoryReporter();
117 RefPtr
<mozilla::StyleSheet
> LoadSheetURL(
118 const char* aURL
, mozilla::css::SheetParsingMode aParsingMode
,
119 mozilla::css::FailureAction aFailureAction
);
120 RefPtr
<mozilla::StyleSheet
> LoadSheetFile(
121 nsIFile
* aFile
, mozilla::css::SheetParsingMode aParsingMode
);
122 RefPtr
<mozilla::StyleSheet
> LoadSheet(
123 nsIURI
* aURI
, mozilla::css::SheetParsingMode aParsingMode
,
124 mozilla::css::FailureAction aFailureAction
);
125 void LoadSheetFromSharedMemory(const char* aURL
,
126 RefPtr
<mozilla::StyleSheet
>* aSheet
,
127 mozilla::css::SheetParsingMode aParsingMode
,
128 Shm
* aSharedMemory
, Header
* aHeader
,
129 mozilla::UserAgentStyleSheetID aSheetID
);
130 void BuildPreferenceSheet(RefPtr
<mozilla::StyleSheet
>* aSheet
,
131 const mozilla::PreferenceSheet::Prefs
&);
133 static mozilla::StaticRefPtr
<nsLayoutStylesheetCache
> gStyleCache
;
134 static mozilla::StaticRefPtr
<mozilla::css::Loader
> gCSSLoader
;
135 static mozilla::StaticRefPtr
<nsIURI
> gUserContentSheetURL
;
137 #define STYLE_SHEET(identifier_, url_, shared_) \
138 RefPtr<mozilla::StyleSheet> m##identifier_##Sheet;
139 #include "mozilla/UserAgentStyleSheetList.h"
142 RefPtr
<mozilla::StyleSheet
> mChromePreferenceSheet
;
143 RefPtr
<mozilla::StyleSheet
> mContentPreferenceSheet
;
144 RefPtr
<mozilla::StyleSheet
> mUserChromeSheet
;
145 RefPtr
<mozilla::StyleSheet
> mUserContentSheet
;
147 // Shared memory segment storing shared style sheets.
148 RefPtr
<Shm
> mSharedMemory
;
150 // How much of the shared memory buffer we ended up using. Used for memory
152 size_t mUsedSharedMemory
;
154 // The shared memory to use once the nsLayoutStylesheetCache instance is
156 static mozilla::StaticRefPtr
<Shm
> sSharedMemory
;