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