GlobalStyleSheetCache.h (4743B)
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 mozilla_GlobalStyleSheetCache_h__ 8 #define mozilla_GlobalStyleSheetCache_h__ 9 10 #include "mozilla/BuiltInStyleSheets.h" 11 #include "mozilla/MemoryReporting.h" 12 #include "mozilla/NotNull.h" 13 #include "mozilla/PreferenceSheet.h" 14 #include "mozilla/StaticPtr.h" 15 #include "mozilla/css/Loader.h" 16 #include "mozilla/ipc/SharedMemoryHandle.h" 17 #include "mozilla/ipc/SharedMemoryMapping.h" 18 #include "nsIMemoryReporter.h" 19 #include "nsIObserver.h" 20 21 class nsIFile; 22 class nsIURI; 23 24 namespace mozilla { 25 class CSSStyleSheet; 26 } // namespace mozilla 27 28 namespace mozilla { 29 namespace css { 30 31 // Enum defining how error should be handled. 32 enum FailureAction { eCrash = 0, eLogToConsole }; 33 34 } // namespace css 35 36 class GlobalStyleSheetCache final : public nsIObserver, 37 public nsIMemoryReporter { 38 public: 39 NS_DECL_ISUPPORTS 40 NS_DECL_NSIOBSERVER 41 NS_DECL_NSIMEMORYREPORTER 42 43 static GlobalStyleSheetCache* Singleton(); 44 45 #define STYLE_SHEET(identifier_, url_, flags_) \ 46 NotNull<StyleSheet*> identifier_##Sheet() { \ 47 return BuiltInSheet(BuiltInStyleSheet::identifier_); \ 48 } 49 #include "mozilla/BuiltInStyleSheetList.h" 50 #undef STYLE_SHEET 51 52 NotNull<StyleSheet*> BuiltInSheet(BuiltInStyleSheet); 53 54 StyleSheet* GetUserContentSheet(); 55 StyleSheet* GetUserChromeSheet(); 56 57 static void Shutdown(); 58 59 static void SetUserContentCSSURL(nsIURI* aURI); 60 61 size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const; 62 63 // Set the shared memory segment to load the shared UA sheets from. 64 // Called early on in a content process' life from 65 // ContentChild::InitSharedUASheets, before the GlobalStyleSheetCache 66 // singleton has been created. 67 static void SetSharedMemory(mozilla::ipc::ReadOnlySharedMemoryHandle aHandle, 68 uintptr_t aAddress); 69 70 // Obtain a shared memory handle for the shared UA sheets to pass into a 71 // content process. Called by ContentParent::InitInternal shortly after 72 // a content process has been created. 73 mozilla::ipc::ReadOnlySharedMemoryHandle CloneHandle(); 74 75 // Returns the address of the shared memory segment that holds the shared UA 76 // sheets. 77 uintptr_t GetSharedMemoryAddress() { 78 return sSharedMemory.IsEmpty() ? 0 : uintptr_t(sSharedMemory.data()); 79 } 80 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. 83 // 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; 87 88 private: 89 // Shared memory header. 90 struct Header { 91 static constexpr uint32_t kMagic = 0x55415353; 92 uint32_t mMagic; // Must be set to kMagic. 93 const StyleLockedCssRules* mSheets[size_t(BuiltInStyleSheet::Count)]; 94 uint8_t mBuffer[1]; 95 }; 96 97 GlobalStyleSheetCache(); 98 ~GlobalStyleSheetCache(); 99 100 void InitFromProfile(); 101 void InitSharedSheetsInParent(); 102 void InitMemoryReporter(); 103 RefPtr<StyleSheet> LoadSheetURL(const nsACString& 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 nsACString& aURL, 111 RefPtr<StyleSheet>* aSheet, 112 css::SheetParsingMode, const Header*, 113 BuiltInStyleSheet); 114 115 static StaticRefPtr<GlobalStyleSheetCache> gStyleCache; 116 static StaticRefPtr<css::Loader> gCSSLoader; 117 static StaticRefPtr<nsIURI> gUserContentSheetURL; 118 119 EnumeratedArray<BuiltInStyleSheet, RefPtr<StyleSheet>, 120 size_t(BuiltInStyleSheet::Count)> 121 mBuiltIns; 122 123 RefPtr<StyleSheet> mUserChromeSheet; 124 RefPtr<StyleSheet> mUserContentSheet; 125 126 // Shared memory segment storing shared style sheets. 127 static mozilla::ipc::shared_memory::LeakedReadOnlyMapping sSharedMemory; 128 129 // How much of the shared memory buffer we ended up using. Used for memory 130 // reporting in the parent process. 131 static size_t sUsedSharedMemory; 132 }; 133 134 } // namespace mozilla 135 136 #endif