Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
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"
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"
21 class nsPresContext;
22 class nsIPrincipal;
24 class nsFontFaceLoader;
26 // nsUserFontSet - defines the loading mechanism for downloadable fonts
27 class nsUserFontSet : public gfxUserFontSet
28 {
29 public:
30 nsUserFontSet(nsPresContext* aContext);
32 // Called when this font set is no longer associated with a presentation.
33 void Destroy();
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 virtual nsPresContext* GetPresContext() { return mPresContext; }
49 virtual void ReplaceFontEntry(gfxMixedFontFamily* aFamily,
50 gfxProxyFontEntry* aProxy,
51 gfxFontEntry* aFontEntry) MOZ_OVERRIDE;
53 nsCSSFontFaceRule* FindRuleForEntry(gfxFontEntry* aFontEntry);
55 protected:
56 // Protected destructor, to discourage deletion outside of Release()
57 // (since we inherit from refcounted class gfxUserFontSet):
58 ~nsUserFontSet();
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 };
69 void InsertRule(nsCSSFontFaceRule* aRule, uint8_t aSheetType,
70 nsTArray<FontFaceRuleRecord>& oldRules,
71 bool& aFontSetModified);
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;
79 virtual nsresult CheckFontLoad(const gfxFontFaceSrc* aFontFaceSrc,
80 nsIPrincipal** aPrincipal,
81 bool* aBypassCache) MOZ_OVERRIDE;
83 virtual nsresult SyncLoadFontData(gfxProxyFontEntry* aFontToLoad,
84 const gfxFontFaceSrc* aFontFaceSrc,
85 uint8_t*& aBuffer,
86 uint32_t& aBufferLength) MOZ_OVERRIDE;
88 virtual bool GetPrivateBrowsing() MOZ_OVERRIDE;
90 virtual void DoRebuildUserFontSet() MOZ_OVERRIDE;
92 nsPresContext* mPresContext; // weak reference
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;
99 nsTArray<FontFaceRuleRecord> mRules;
100 };
102 class nsFontFaceLoader : public nsIStreamLoaderObserver
103 {
104 public:
105 nsFontFaceLoader(gfxMixedFontFamily* aFontFamily,
106 gfxProxyFontEntry* aFontToLoad, nsIURI* aFontURI,
107 nsUserFontSet* aFontSet, nsIChannel* aChannel);
109 virtual ~nsFontFaceLoader();
111 NS_DECL_ISUPPORTS
112 NS_DECL_NSISTREAMLOADEROBSERVER
114 // initiate the load
115 nsresult Init();
116 // cancel the load and remove its reference to mFontSet
117 void Cancel();
119 void DropChannel() { mChannel = nullptr; }
121 void StartedLoading(nsIStreamLoader* aStreamLoader);
123 static void LoadTimerCallback(nsITimer* aTimer, void* aClosure);
125 static nsresult CheckLoadAllowed(nsIPrincipal* aSourcePrincipal,
126 nsIURI* aTargetURI,
127 nsISupports* aContext);
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;
137 nsIStreamLoader* mStreamLoader;
138 };
140 #endif /* !defined(nsFontFaceLoader_h_) */