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 mozilla_GlobalStyleSheetCache_h__
8 #define mozilla_GlobalStyleSheetCache_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/UserAgentStyleSheetID.h"
19 #include "mozilla/css/Loader.h"
26 } // namespace mozilla
31 // Enum defining how error should be handled.
32 enum FailureAction
{ eCrash
= 0, eLogToConsole
};
36 class GlobalStyleSheetCache final
: public nsIObserver
,
37 public nsIMemoryReporter
{
41 NS_DECL_NSIMEMORYREPORTER
43 static GlobalStyleSheetCache
* Singleton();
45 #define STYLE_SHEET(identifier_, url_, shared_) \
46 NotNull<StyleSheet*> identifier_##Sheet();
47 #include "mozilla/UserAgentStyleSheetList.h"
50 StyleSheet
* GetUserContentSheet();
51 StyleSheet
* GetUserChromeSheet();
52 StyleSheet
* ChromePreferenceSheet();
53 StyleSheet
* ContentPreferenceSheet();
55 static void InvalidatePreferenceSheets();
56 static bool AffectedByPref(const nsACString
&);
58 static void Shutdown();
60 static void SetUserContentCSSURL(nsIURI
* aURI
);
62 size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf
) const;
64 // Set the shared memory segment to load the shared UA sheets from.
65 // Called early on in a content process' life from
66 // ContentChild::InitSharedUASheets, before the GlobalStyleSheetCache
67 // singleton has been created.
68 static void SetSharedMemory(base::SharedMemoryHandle aHandle
,
71 // Obtain a shared memory handle for the shared UA sheets to pass into a
72 // content process. Called by ContentParent::InitInternal shortly after
73 // a content process has been created.
74 base::SharedMemoryHandle
CloneHandle();
76 // Returns the address of the shared memory segment that holds the shared UA
78 uintptr_t GetSharedMemoryAddress() {
79 return sSharedMemory
? uintptr_t(sSharedMemory
->memory()) : 0;
82 // Size of the shared memory buffer we'll create to store the shared UA
83 // sheets. We choose a value that is big enough on both 64 bit and 32 bit.
85 // If this isn't big enough for the current contents of the shared UA
86 // sheets, we'll crash under InitSharedSheetsInParent.
87 static constexpr size_t kSharedMemorySize
= 1024 * 450;
90 // Shared memory header.
92 static constexpr uint32_t kMagic
= 0x55415353;
93 uint32_t mMagic
; // Must be set to kMagic.
94 const StyleLockedCssRules
* mSheets
[size_t(UserAgentStyleSheetID::Count
)];
98 GlobalStyleSheetCache();
99 ~GlobalStyleSheetCache();
101 void InitFromProfile();
102 void InitSharedSheetsInParent();
103 void InitMemoryReporter();
104 RefPtr
<StyleSheet
> LoadSheetURL(const char* aURL
,
105 css::SheetParsingMode aParsingMode
,
106 css::FailureAction aFailureAction
);
107 RefPtr
<StyleSheet
> LoadSheetFile(nsIFile
* aFile
,
108 css::SheetParsingMode aParsingMode
);
109 RefPtr
<StyleSheet
> LoadSheet(nsIURI
* aURI
, css::SheetParsingMode aParsingMode
,
110 css::FailureAction aFailureAction
);
111 void LoadSheetFromSharedMemory(const char* aURL
, RefPtr
<StyleSheet
>* aSheet
,
112 css::SheetParsingMode
, Header
*,
113 UserAgentStyleSheetID
);
114 void BuildPreferenceSheet(RefPtr
<StyleSheet
>* aSheet
,
115 const PreferenceSheet::Prefs
&);
117 static StaticRefPtr
<GlobalStyleSheetCache
> gStyleCache
;
118 static StaticRefPtr
<css::Loader
> gCSSLoader
;
119 static StaticRefPtr
<nsIURI
> gUserContentSheetURL
;
121 #define STYLE_SHEET(identifier_, url_, shared_) \
122 RefPtr<StyleSheet> m##identifier_##Sheet;
123 #include "mozilla/UserAgentStyleSheetList.h"
126 RefPtr
<StyleSheet
> mChromePreferenceSheet
;
127 RefPtr
<StyleSheet
> mContentPreferenceSheet
;
128 RefPtr
<StyleSheet
> mUserChromeSheet
;
129 RefPtr
<StyleSheet
> mUserContentSheet
;
131 // Shared memory segment storing shared style sheets.
132 static StaticAutoPtr
<base::SharedMemory
> sSharedMemory
;
134 // How much of the shared memory buffer we ended up using. Used for memory
135 // reporting in the parent process.
136 static size_t sUsedSharedMemory
;
139 } // namespace mozilla