|
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/. */ |
|
6 |
|
7 /* code for loading in @font-face defined font data */ |
|
8 |
|
9 #ifndef nsFontFaceLoader_h_ |
|
10 #define nsFontFaceLoader_h_ |
|
11 |
|
12 #include "mozilla/Attributes.h" |
|
13 #include "nsCOMPtr.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" |
|
20 |
|
21 class nsPresContext; |
|
22 class nsIPrincipal; |
|
23 |
|
24 class nsFontFaceLoader; |
|
25 |
|
26 // nsUserFontSet - defines the loading mechanism for downloadable fonts |
|
27 class nsUserFontSet : public gfxUserFontSet |
|
28 { |
|
29 public: |
|
30 nsUserFontSet(nsPresContext* aContext); |
|
31 |
|
32 // Called when this font set is no longer associated with a presentation. |
|
33 void Destroy(); |
|
34 |
|
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; |
|
40 |
|
41 // Called by nsFontFaceLoader when the loader has completed normally. |
|
42 // It's removed from the mLoaders set. |
|
43 void RemoveLoader(nsFontFaceLoader* aLoader); |
|
44 |
|
45 bool UpdateRules(const nsTArray<nsFontFaceRuleContainer>& aRules); |
|
46 |
|
47 virtual nsPresContext* GetPresContext() { return mPresContext; } |
|
48 |
|
49 virtual void ReplaceFontEntry(gfxMixedFontFamily* aFamily, |
|
50 gfxProxyFontEntry* aProxy, |
|
51 gfxFontEntry* aFontEntry) MOZ_OVERRIDE; |
|
52 |
|
53 nsCSSFontFaceRule* FindRuleForEntry(gfxFontEntry* aFontEntry); |
|
54 |
|
55 protected: |
|
56 // Protected destructor, to discourage deletion outside of Release() |
|
57 // (since we inherit from refcounted class gfxUserFontSet): |
|
58 ~nsUserFontSet(); |
|
59 |
|
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; |
|
67 }; |
|
68 |
|
69 void InsertRule(nsCSSFontFaceRule* aRule, uint8_t aSheetType, |
|
70 nsTArray<FontFaceRuleRecord>& oldRules, |
|
71 bool& aFontSetModified); |
|
72 |
|
73 virtual nsresult LogMessage(gfxMixedFontFamily* aFamily, |
|
74 gfxProxyFontEntry* aProxy, |
|
75 const char* aMessage, |
|
76 uint32_t aFlags = nsIScriptError::errorFlag, |
|
77 nsresult aStatus = NS_OK) MOZ_OVERRIDE; |
|
78 |
|
79 virtual nsresult CheckFontLoad(const gfxFontFaceSrc* aFontFaceSrc, |
|
80 nsIPrincipal** aPrincipal, |
|
81 bool* aBypassCache) MOZ_OVERRIDE; |
|
82 |
|
83 virtual nsresult SyncLoadFontData(gfxProxyFontEntry* aFontToLoad, |
|
84 const gfxFontFaceSrc* aFontFaceSrc, |
|
85 uint8_t*& aBuffer, |
|
86 uint32_t& aBufferLength) MOZ_OVERRIDE; |
|
87 |
|
88 virtual bool GetPrivateBrowsing() MOZ_OVERRIDE; |
|
89 |
|
90 virtual void DoRebuildUserFontSet() MOZ_OVERRIDE; |
|
91 |
|
92 nsPresContext* mPresContext; // weak reference |
|
93 |
|
94 // Set of all loaders pointing to us. These are not strong pointers, |
|
95 // but that's OK because nsFontFaceLoader always calls RemoveLoader on |
|
96 // us before it dies (unless we die first). |
|
97 nsTHashtable< nsPtrHashKey<nsFontFaceLoader> > mLoaders; |
|
98 |
|
99 nsTArray<FontFaceRuleRecord> mRules; |
|
100 }; |
|
101 |
|
102 class nsFontFaceLoader : public nsIStreamLoaderObserver |
|
103 { |
|
104 public: |
|
105 nsFontFaceLoader(gfxMixedFontFamily* aFontFamily, |
|
106 gfxProxyFontEntry* aFontToLoad, nsIURI* aFontURI, |
|
107 nsUserFontSet* aFontSet, nsIChannel* aChannel); |
|
108 |
|
109 virtual ~nsFontFaceLoader(); |
|
110 |
|
111 NS_DECL_ISUPPORTS |
|
112 NS_DECL_NSISTREAMLOADEROBSERVER |
|
113 |
|
114 // initiate the load |
|
115 nsresult Init(); |
|
116 // cancel the load and remove its reference to mFontSet |
|
117 void Cancel(); |
|
118 |
|
119 void DropChannel() { mChannel = nullptr; } |
|
120 |
|
121 void StartedLoading(nsIStreamLoader* aStreamLoader); |
|
122 |
|
123 static void LoadTimerCallback(nsITimer* aTimer, void* aClosure); |
|
124 |
|
125 static nsresult CheckLoadAllowed(nsIPrincipal* aSourcePrincipal, |
|
126 nsIURI* aTargetURI, |
|
127 nsISupports* aContext); |
|
128 |
|
129 private: |
|
130 nsRefPtr<gfxMixedFontFamily> mFontFamily; |
|
131 nsRefPtr<gfxProxyFontEntry> mFontEntry; |
|
132 nsCOMPtr<nsIURI> mFontURI; |
|
133 nsRefPtr<nsUserFontSet> mFontSet; |
|
134 nsCOMPtr<nsIChannel> mChannel; |
|
135 nsCOMPtr<nsITimer> mLoadTimer; |
|
136 |
|
137 nsIStreamLoader* mStreamLoader; |
|
138 }; |
|
139 |
|
140 #endif /* !defined(nsFontFaceLoader_h_) */ |