1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 // vim:cindent:ts=2:et:sw=2:
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 /* code for loading in @font-face defined font data */
9 #ifndef nsFontFaceLoader_h_
10 #define nsFontFaceLoader_h_
13 #include "nsIStreamLoader.h"
14 #include "nsIChannel.h"
15 #include "gfxUserFontSet.h"
16 #include "nsHashKeys.h"
17 #include "nsTHashtable.h"
18 #include "nsCSSRules.h"
24 class nsFontFaceLoader
;
25 class nsCSSFontFaceRule
;
27 // nsUserFontSet - defines the loading mechanism for downloadable fonts
28 class nsUserFontSet
: public gfxUserFontSet
31 nsUserFontSet(nsPresContext
*aContext
);
34 // Called when this font set is no longer associated with a presentation.
37 // starts loading process, creating and initializing a nsFontFaceLoader obj
38 // returns whether load process successfully started or not
39 nsresult
StartLoad(gfxMixedFontFamily
*aFamily
,
40 gfxProxyFontEntry
*aFontToLoad
,
41 const gfxFontFaceSrc
*aFontFaceSrc
);
43 // Called by nsFontFaceLoader when the loader has completed normally.
44 // It's removed from the mLoaders set.
45 void RemoveLoader(nsFontFaceLoader
*aLoader
);
47 bool UpdateRules(const nsTArray
<nsFontFaceRuleContainer
>& aRules
);
49 nsPresContext
*GetPresContext() { return mPresContext
; }
51 virtual void ReplaceFontEntry(gfxMixedFontFamily
*aFamily
,
52 gfxProxyFontEntry
*aProxy
,
53 gfxFontEntry
*aFontEntry
);
55 nsCSSFontFaceRule
*FindRuleForEntry(gfxFontEntry
*aFontEntry
);
58 // The font-set keeps track of the collection of rules, and their
59 // corresponding font entries (whether proxies or real entries),
60 // so that we can update the set without having to throw away
61 // all the existing fonts.
62 struct FontFaceRuleRecord
{
63 nsRefPtr
<gfxFontEntry
> mFontEntry
;
64 nsFontFaceRuleContainer mContainer
;
67 void InsertRule(nsCSSFontFaceRule
*aRule
, uint8_t aSheetType
,
68 nsTArray
<FontFaceRuleRecord
>& oldRules
,
69 bool& aFontSetModified
);
71 virtual nsresult
LogMessage(gfxMixedFontFamily
*aFamily
,
72 gfxProxyFontEntry
*aProxy
,
74 uint32_t aFlags
= nsIScriptError::errorFlag
,
75 nsresult aStatus
= NS_OK
);
77 virtual nsresult
CheckFontLoad(const gfxFontFaceSrc
*aFontFaceSrc
,
78 nsIPrincipal
**aPrincipal
);
80 virtual nsresult
SyncLoadFontData(gfxProxyFontEntry
*aFontToLoad
,
81 const gfxFontFaceSrc
*aFontFaceSrc
,
83 uint32_t &aBufferLength
);
85 nsPresContext
*mPresContext
; // weak reference
87 // Set of all loaders pointing to us. These are not strong pointers,
88 // but that's OK because nsFontFaceLoader always calls RemoveLoader on
89 // us before it dies (unless we die first).
90 nsTHashtable
< nsPtrHashKey
<nsFontFaceLoader
> > mLoaders
;
92 nsTArray
<FontFaceRuleRecord
> mRules
;
95 class nsFontFaceLoader
: public nsIStreamLoaderObserver
98 nsFontFaceLoader(gfxMixedFontFamily
*aFontFamily
,
99 gfxProxyFontEntry
*aFontToLoad
, nsIURI
*aFontURI
,
100 nsUserFontSet
*aFontSet
, nsIChannel
*aChannel
);
102 virtual ~nsFontFaceLoader();
105 NS_DECL_NSISTREAMLOADEROBSERVER
109 // cancel the load and remove its reference to mFontSet
112 void DropChannel() { mChannel
= nullptr; }
114 void StartedLoading(nsIStreamLoader
*aStreamLoader
);
116 static void LoadTimerCallback(nsITimer
*aTimer
, void *aClosure
);
118 static nsresult
CheckLoadAllowed(nsIPrincipal
* aSourcePrincipal
,
120 nsISupports
* aContext
);
123 nsRefPtr
<gfxMixedFontFamily
> mFontFamily
;
124 nsRefPtr
<gfxProxyFontEntry
> mFontEntry
;
125 nsCOMPtr
<nsIURI
> mFontURI
;
126 nsRefPtr
<nsUserFontSet
> mFontSet
;
127 nsCOMPtr
<nsIChannel
> mChannel
;
128 nsCOMPtr
<nsITimer
> mLoadTimer
;
130 nsIStreamLoader
*mStreamLoader
;
133 #endif /* !defined(nsFontFaceLoader_h_) */