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_SharedStyleSheetCache_h__
8 #define mozilla_SharedStyleSheetCache_h__
10 // The shared style sheet cache is a cache that allows us to share sheets across
13 // It's generally a singleton, but it is different from GlobalStyleSheetCache in
16 // * It needs to be cycle-collectable, as it can keep alive style sheets from
19 // * It is conceptually a singleton, but given its cycle-collectable nature, we
20 // might re-create it.
22 #include "mozilla/SharedSubResourceCache.h"
23 #include "nsRefPtrHashtable.h"
24 #include "mozilla/MemoryReporting.h"
25 #include "mozilla/css/Loader.h"
30 class SheetLoadDataHashKey
;
37 struct SharedStyleSheetCacheTraits
{
38 using Loader
= css::Loader
;
39 using Key
= SheetLoadDataHashKey
;
40 using Value
= StyleSheet
;
41 using LoadingValue
= css::SheetLoadData
;
43 static SheetLoadDataHashKey
KeyFromLoadingValue(const LoadingValue
& aValue
) {
44 return SheetLoadDataHashKey(aValue
);
48 class SharedStyleSheetCache final
49 : public SharedSubResourceCache
<SharedStyleSheetCacheTraits
,
50 SharedStyleSheetCache
>,
51 public nsIMemoryReporter
{
53 using Base
= SharedSubResourceCache
<SharedStyleSheetCacheTraits
,
54 SharedStyleSheetCache
>;
57 NS_DECL_NSIMEMORYREPORTER
59 SharedStyleSheetCache();
62 // This has to be static because it's also called for loaders that don't have
63 // a sheet cache (loaders that are not owned by a document).
64 static void LoadCompleted(SharedStyleSheetCache
*, css::SheetLoadData
&,
66 using Base::LoadCompleted
;
67 static void LoadCompletedInternal(SharedStyleSheetCache
*, css::SheetLoadData
&,
68 nsTArray
<RefPtr
<css::SheetLoadData
>>&);
69 static void Clear(nsIPrincipal
* aForPrincipal
= nullptr,
70 const nsACString
* aBaseDomain
= nullptr);
73 void InsertIfNeeded(css::SheetLoadData
&);
75 ~SharedStyleSheetCache();
78 } // namespace mozilla