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_
12 #include "mozilla/Attributes.h"
14 #include "nsIStreamLoader.h"
15 #include "nsIChannel.h"
16 #include "gfxUserFontSet.h"
17 #include "nsHashKeys.h"
18 #include "nsTHashtable.h"
19 #include "nsCSSRules.h"
24 class nsFontFaceLoader
;
26 // nsUserFontSet - defines the loading mechanism for downloadable fonts
27 class nsUserFontSet
: public gfxUserFontSet
30 explicit nsUserFontSet(nsPresContext
* aContext
);
32 // Called when this font set is no longer associated with a presentation.
35 // starts loading process, creating and initializing a nsFontFaceLoader obj
36 // returns whether load process successfully started or not
37 nsresult
StartLoad(gfxMixedFontFamily
* aFamily
,
38 gfxProxyFontEntry
* aFontToLoad
,
39 const gfxFontFaceSrc
* aFontFaceSrc
) MOZ_OVERRIDE
;
41 // Called by nsFontFaceLoader when the loader has completed normally.
42 // It's removed from the mLoaders set.
43 void RemoveLoader(nsFontFaceLoader
* aLoader
);
45 bool UpdateRules(const nsTArray
<nsFontFaceRuleContainer
>& aRules
);
47 nsPresContext
* GetPresContext() { return mPresContext
; }
49 virtual void ReplaceFontEntry(gfxMixedFontFamily
* aFamily
,
50 gfxProxyFontEntry
* aProxy
,
51 gfxFontEntry
* aFontEntry
) MOZ_OVERRIDE
;
53 nsCSSFontFaceRule
* FindRuleForEntry(gfxFontEntry
* aFontEntry
);
56 // Protected destructor, to discourage deletion outside of Release()
57 // (since we inherit from refcounted class gfxUserFontSet):
60 // The font-set keeps track of the collection of rules, and their
61 // corresponding font entries (whether proxies or real entries),
62 // so that we can update the set without having to throw away
63 // all the existing fonts.
64 struct FontFaceRuleRecord
{
65 nsRefPtr
<gfxFontEntry
> mFontEntry
;
66 nsFontFaceRuleContainer mContainer
;
69 void InsertRule(nsCSSFontFaceRule
* aRule
, uint8_t aSheetType
,
70 nsTArray
<FontFaceRuleRecord
>& oldRules
,
71 bool& aFontSetModified
);
73 already_AddRefed
<gfxFontEntry
> FindOrCreateFontFaceFromRule(
74 const nsAString
& aFamilyName
,
75 nsCSSFontFaceRule
* aRule
,
78 virtual nsresult
LogMessage(gfxMixedFontFamily
* aFamily
,
79 gfxProxyFontEntry
* aProxy
,
81 uint32_t aFlags
= nsIScriptError::errorFlag
,
82 nsresult aStatus
= NS_OK
) MOZ_OVERRIDE
;
84 virtual nsresult
CheckFontLoad(const gfxFontFaceSrc
* aFontFaceSrc
,
85 nsIPrincipal
** aPrincipal
,
86 bool* aBypassCache
) MOZ_OVERRIDE
;
88 virtual nsresult
SyncLoadFontData(gfxProxyFontEntry
* aFontToLoad
,
89 const gfxFontFaceSrc
* aFontFaceSrc
,
91 uint32_t& aBufferLength
) MOZ_OVERRIDE
;
93 virtual bool GetPrivateBrowsing() MOZ_OVERRIDE
;
95 virtual void DoRebuildUserFontSet() MOZ_OVERRIDE
;
97 nsPresContext
* mPresContext
; // weak reference
99 // Set of all loaders pointing to us. These are not strong pointers,
100 // but that's OK because nsFontFaceLoader always calls RemoveLoader on
101 // us before it dies (unless we die first).
102 nsTHashtable
< nsPtrHashKey
<nsFontFaceLoader
> > mLoaders
;
104 nsTArray
<FontFaceRuleRecord
> mRules
;
107 class nsFontFaceLoader
: public nsIStreamLoaderObserver
110 nsFontFaceLoader(gfxMixedFontFamily
* aFontFamily
,
111 gfxProxyFontEntry
* aFontToLoad
, nsIURI
* aFontURI
,
112 nsUserFontSet
* aFontSet
, nsIChannel
* aChannel
);
115 NS_DECL_NSISTREAMLOADEROBSERVER
119 // cancel the load and remove its reference to mFontSet
122 void DropChannel() { mChannel
= nullptr; }
124 void StartedLoading(nsIStreamLoader
* aStreamLoader
);
126 static void LoadTimerCallback(nsITimer
* aTimer
, void* aClosure
);
128 static nsresult
CheckLoadAllowed(nsIPrincipal
* aSourcePrincipal
,
130 nsISupports
* aContext
);
133 virtual ~nsFontFaceLoader();
136 nsRefPtr
<gfxMixedFontFamily
> mFontFamily
;
137 nsRefPtr
<gfxProxyFontEntry
> mFontEntry
;
138 nsCOMPtr
<nsIURI
> mFontURI
;
139 nsRefPtr
<nsUserFontSet
> mFontSet
;
140 nsCOMPtr
<nsIChannel
> mChannel
;
141 nsCOMPtr
<nsITimer
> mLoadTimer
;
143 nsIStreamLoader
* mStreamLoader
;
146 #endif /* !defined(nsFontFaceLoader_h_) */