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();
53 static void Shutdown();
55 static void SetUserContentCSSURL(nsIURI
* aURI
);
57 size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf
) const;
59 // Set the shared memory segment to load the shared UA sheets from.
60 // Called early on in a content process' life from
61 // ContentChild::InitSharedUASheets, before the GlobalStyleSheetCache
62 // singleton has been created.
63 static void SetSharedMemory(base::SharedMemoryHandle aHandle
,
66 // Obtain a shared memory handle for the shared UA sheets to pass into a
67 // content process. Called by ContentParent::InitInternal shortly after
68 // a content process has been created.
69 base::SharedMemoryHandle
CloneHandle();
71 // Returns the address of the shared memory segment that holds the shared UA
73 uintptr_t GetSharedMemoryAddress() {
74 return sSharedMemory
? uintptr_t(sSharedMemory
->memory()) : 0;
77 // Size of the shared memory buffer we'll create to store the shared UA
78 // sheets. We choose a value that is big enough on both 64 bit and 32 bit.
80 // If this isn't big enough for the current contents of the shared UA
81 // sheets, we'll crash under InitSharedSheetsInParent.
82 static constexpr size_t kSharedMemorySize
= 1024 * 450;
85 // Shared memory header.
87 static constexpr uint32_t kMagic
= 0x55415353;
88 uint32_t mMagic
; // Must be set to kMagic.
89 const StyleLockedCssRules
* mSheets
[size_t(UserAgentStyleSheetID::Count
)];
93 GlobalStyleSheetCache();
94 ~GlobalStyleSheetCache();
96 void InitFromProfile();
97 void InitSharedSheetsInParent();
98 void InitMemoryReporter();
99 RefPtr
<StyleSheet
> LoadSheetURL(const char* aURL
,
100 css::SheetParsingMode aParsingMode
,
101 css::FailureAction aFailureAction
);
102 RefPtr
<StyleSheet
> LoadSheetFile(nsIFile
* aFile
,
103 css::SheetParsingMode aParsingMode
);
104 RefPtr
<StyleSheet
> LoadSheet(nsIURI
* aURI
, css::SheetParsingMode aParsingMode
,
105 css::FailureAction aFailureAction
);
106 void LoadSheetFromSharedMemory(const char* aURL
, RefPtr
<StyleSheet
>* aSheet
,
107 css::SheetParsingMode
, Header
*,
108 UserAgentStyleSheetID
);
110 static StaticRefPtr
<GlobalStyleSheetCache
> gStyleCache
;
111 static StaticRefPtr
<css::Loader
> gCSSLoader
;
112 static StaticRefPtr
<nsIURI
> gUserContentSheetURL
;
114 #define STYLE_SHEET(identifier_, url_, shared_) \
115 RefPtr<StyleSheet> m##identifier_##Sheet;
116 #include "mozilla/UserAgentStyleSheetList.h"
119 RefPtr
<StyleSheet
> mUserChromeSheet
;
120 RefPtr
<StyleSheet
> mUserContentSheet
;
122 // Shared memory segment storing shared style sheets.
123 static StaticAutoPtr
<base::SharedMemory
> sSharedMemory
;
125 // How much of the shared memory buffer we ended up using. Used for memory
126 // reporting in the parent process.
127 static size_t sUsedSharedMemory
;
130 } // namespace mozilla