Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef GFX_FONT_INFO_LOADER_H |
michael@0 | 7 | #define GFX_FONT_INFO_LOADER_H |
michael@0 | 8 | |
michael@0 | 9 | #include "nsAutoPtr.h" |
michael@0 | 10 | #include "nsCOMPtr.h" |
michael@0 | 11 | #include "nsIObserver.h" |
michael@0 | 12 | #include "nsITimer.h" |
michael@0 | 13 | #include "nsIThread.h" |
michael@0 | 14 | #include "nsRefPtrHashtable.h" |
michael@0 | 15 | #include "nsString.h" |
michael@0 | 16 | #include "gfxFont.h" |
michael@0 | 17 | #include "nsIRunnable.h" |
michael@0 | 18 | #include "mozilla/TimeStamp.h" |
michael@0 | 19 | #include "nsISupportsImpl.h" |
michael@0 | 20 | |
michael@0 | 21 | // data retrieved for a given face |
michael@0 | 22 | |
michael@0 | 23 | struct FontFaceData { |
michael@0 | 24 | FontFaceData() : mUVSOffset(0), mSymbolFont(false) {} |
michael@0 | 25 | |
michael@0 | 26 | FontFaceData(const FontFaceData& aFontFaceData) { |
michael@0 | 27 | mFullName = aFontFaceData.mFullName; |
michael@0 | 28 | mPostscriptName = aFontFaceData.mPostscriptName; |
michael@0 | 29 | mCharacterMap = aFontFaceData.mCharacterMap; |
michael@0 | 30 | mUVSOffset = aFontFaceData.mUVSOffset; |
michael@0 | 31 | mSymbolFont = aFontFaceData.mSymbolFont; |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | nsString mFullName; |
michael@0 | 35 | nsString mPostscriptName; |
michael@0 | 36 | nsRefPtr<gfxCharacterMap> mCharacterMap; |
michael@0 | 37 | uint32_t mUVSOffset; |
michael@0 | 38 | bool mSymbolFont; |
michael@0 | 39 | }; |
michael@0 | 40 | |
michael@0 | 41 | // base class used to contain cached system-wide font info. |
michael@0 | 42 | // methods in this class are called on off-main threads so |
michael@0 | 43 | // all methods use only static methods or other thread-safe |
michael@0 | 44 | // font data access API's. specifically, no use is made of |
michael@0 | 45 | // gfxPlatformFontList, gfxFontFamily, gfxFamily or any |
michael@0 | 46 | // harfbuzz API methods within FontInfoData subclasses. |
michael@0 | 47 | |
michael@0 | 48 | class FontInfoData { |
michael@0 | 49 | public: |
michael@0 | 50 | NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FontInfoData) |
michael@0 | 51 | |
michael@0 | 52 | FontInfoData(bool aLoadOtherNames, |
michael@0 | 53 | bool aLoadFaceNames, |
michael@0 | 54 | bool aLoadCmaps) : |
michael@0 | 55 | mLoadOtherNames(aLoadOtherNames), |
michael@0 | 56 | mLoadFaceNames(aLoadFaceNames), |
michael@0 | 57 | mLoadCmaps(aLoadCmaps) |
michael@0 | 58 | { |
michael@0 | 59 | MOZ_COUNT_CTOR(FontInfoData); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | protected: |
michael@0 | 63 | // Protected destructor, to discourage deletion outside of Release(): |
michael@0 | 64 | virtual ~FontInfoData() { |
michael@0 | 65 | MOZ_COUNT_DTOR(FontInfoData); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | public: |
michael@0 | 69 | virtual void Load(); |
michael@0 | 70 | |
michael@0 | 71 | // loads font data for all fonts of a given family |
michael@0 | 72 | // (called on async thread) |
michael@0 | 73 | virtual void LoadFontFamilyData(const nsAString& aFamilyName) = 0; |
michael@0 | 74 | |
michael@0 | 75 | // -- methods overriden by platform-specific versions -- |
michael@0 | 76 | |
michael@0 | 77 | // fetches cmap data for a particular font from cached font data |
michael@0 | 78 | virtual already_AddRefed<gfxCharacterMap> |
michael@0 | 79 | GetCMAP(const nsAString& aFontName, |
michael@0 | 80 | uint32_t& aUVSOffset, |
michael@0 | 81 | bool& aSymbolFont) |
michael@0 | 82 | { |
michael@0 | 83 | FontFaceData faceData; |
michael@0 | 84 | if (!mFontFaceData.Get(aFontName, &faceData) || |
michael@0 | 85 | !faceData.mCharacterMap) { |
michael@0 | 86 | return nullptr; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | aUVSOffset = faceData.mUVSOffset; |
michael@0 | 90 | aSymbolFont = faceData.mSymbolFont; |
michael@0 | 91 | nsRefPtr<gfxCharacterMap> cmap = faceData.mCharacterMap; |
michael@0 | 92 | return cmap.forget(); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | // fetches fullname/postscript names from cached font data |
michael@0 | 96 | virtual void GetFaceNames(const nsAString& aFontName, |
michael@0 | 97 | nsAString& aFullName, |
michael@0 | 98 | nsAString& aPostscriptName) |
michael@0 | 99 | { |
michael@0 | 100 | FontFaceData faceData; |
michael@0 | 101 | if (!mFontFaceData.Get(aFontName, &faceData)) { |
michael@0 | 102 | return; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | aFullName = faceData.mFullName; |
michael@0 | 106 | aPostscriptName = faceData.mPostscriptName; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | // fetches localized family name data from cached font data |
michael@0 | 110 | virtual bool GetOtherFamilyNames(const nsAString& aFamilyName, |
michael@0 | 111 | nsTArray<nsString>& aOtherFamilyNames) |
michael@0 | 112 | { |
michael@0 | 113 | return mOtherFamilyNames.Get(aFamilyName, &aOtherFamilyNames); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | nsTArray<nsString> mFontFamiliesToLoad; |
michael@0 | 117 | |
michael@0 | 118 | // time spent on the loader thread |
michael@0 | 119 | mozilla::TimeDuration mLoadTime; |
michael@0 | 120 | |
michael@0 | 121 | struct FontCounts { |
michael@0 | 122 | uint32_t families; |
michael@0 | 123 | uint32_t fonts; |
michael@0 | 124 | uint32_t cmaps; |
michael@0 | 125 | uint32_t facenames; |
michael@0 | 126 | uint32_t othernames; |
michael@0 | 127 | }; |
michael@0 | 128 | |
michael@0 | 129 | FontCounts mLoadStats; |
michael@0 | 130 | |
michael@0 | 131 | bool mLoadOtherNames; |
michael@0 | 132 | bool mLoadFaceNames; |
michael@0 | 133 | bool mLoadCmaps; |
michael@0 | 134 | |
michael@0 | 135 | // face name ==> per-face data |
michael@0 | 136 | nsDataHashtable<nsStringHashKey, FontFaceData> mFontFaceData; |
michael@0 | 137 | |
michael@0 | 138 | // canonical family name ==> array of localized family names |
michael@0 | 139 | nsDataHashtable<nsStringHashKey, nsTArray<nsString> > mOtherFamilyNames; |
michael@0 | 140 | }; |
michael@0 | 141 | |
michael@0 | 142 | // gfxFontInfoLoader - helper class for loading font info on async thread |
michael@0 | 143 | // For large, "all fonts on system" data, data needed on a given platform |
michael@0 | 144 | // (e.g. localized names, face names, cmaps) are loaded async. |
michael@0 | 145 | |
michael@0 | 146 | // helper class for loading in font info on a separate async thread |
michael@0 | 147 | // once async thread completes, completion process is run on regular |
michael@0 | 148 | // intervals to prevent tying up the main thread |
michael@0 | 149 | |
michael@0 | 150 | class gfxFontInfoLoader { |
michael@0 | 151 | public: |
michael@0 | 152 | |
michael@0 | 153 | // state transitions: |
michael@0 | 154 | // initial ---StartLoader with delay---> timer on delay |
michael@0 | 155 | // initial ---StartLoader without delay---> timer on interval |
michael@0 | 156 | // timer on delay ---LoaderTimerFire---> timer on interval |
michael@0 | 157 | // timer on delay ---CancelLoader---> timer off |
michael@0 | 158 | // timer on interval ---CancelLoader---> timer off |
michael@0 | 159 | // timer off ---StartLoader with delay---> timer on delay |
michael@0 | 160 | // timer off ---StartLoader without delay---> timer on interval |
michael@0 | 161 | typedef enum { |
michael@0 | 162 | stateInitial, |
michael@0 | 163 | stateTimerOnDelay, |
michael@0 | 164 | stateAsyncLoad, |
michael@0 | 165 | stateTimerOnInterval, |
michael@0 | 166 | stateTimerOff |
michael@0 | 167 | } TimerState; |
michael@0 | 168 | |
michael@0 | 169 | gfxFontInfoLoader() : |
michael@0 | 170 | mInterval(0), mState(stateInitial) |
michael@0 | 171 | { |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | virtual ~gfxFontInfoLoader(); |
michael@0 | 175 | |
michael@0 | 176 | // start timer with an initial delay, then call Run method at regular intervals |
michael@0 | 177 | void StartLoader(uint32_t aDelay, uint32_t aInterval); |
michael@0 | 178 | |
michael@0 | 179 | // Finalize - async load complete, transfer data (on intervals if necessary) |
michael@0 | 180 | virtual void FinalizeLoader(FontInfoData *aFontInfo); |
michael@0 | 181 | |
michael@0 | 182 | // cancel the timer and cleanup |
michael@0 | 183 | void CancelLoader(); |
michael@0 | 184 | |
michael@0 | 185 | uint32_t GetInterval() { return mInterval; } |
michael@0 | 186 | |
michael@0 | 187 | protected: |
michael@0 | 188 | class ShutdownObserver : public nsIObserver |
michael@0 | 189 | { |
michael@0 | 190 | public: |
michael@0 | 191 | NS_DECL_ISUPPORTS |
michael@0 | 192 | NS_DECL_NSIOBSERVER |
michael@0 | 193 | |
michael@0 | 194 | ShutdownObserver(gfxFontInfoLoader *aLoader) |
michael@0 | 195 | : mLoader(aLoader) |
michael@0 | 196 | { } |
michael@0 | 197 | |
michael@0 | 198 | virtual ~ShutdownObserver() |
michael@0 | 199 | { } |
michael@0 | 200 | |
michael@0 | 201 | protected: |
michael@0 | 202 | gfxFontInfoLoader *mLoader; |
michael@0 | 203 | }; |
michael@0 | 204 | |
michael@0 | 205 | // CreateFontInfo - create platform-specific object used |
michael@0 | 206 | // to load system-wide font info |
michael@0 | 207 | virtual already_AddRefed<FontInfoData> CreateFontInfoData() { |
michael@0 | 208 | return nullptr; |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | // Init - initialization before async loader thread runs |
michael@0 | 212 | virtual void InitLoader() = 0; |
michael@0 | 213 | |
michael@0 | 214 | // LoadFontInfo - transfer font info data within a time limit, return |
michael@0 | 215 | // true when done |
michael@0 | 216 | virtual bool LoadFontInfo() = 0; |
michael@0 | 217 | |
michael@0 | 218 | // Cleanup - finish and cleanup after done, including possible reflows |
michael@0 | 219 | virtual void CleanupLoader() { |
michael@0 | 220 | mFontInfo = nullptr; |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | // Timer interval callbacks |
michael@0 | 224 | static void LoadFontInfoCallback(nsITimer *aTimer, void *aThis) { |
michael@0 | 225 | gfxFontInfoLoader *loader = static_cast<gfxFontInfoLoader*>(aThis); |
michael@0 | 226 | loader->LoadFontInfoTimerFire(); |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | static void DelayedStartCallback(nsITimer *aTimer, void *aThis) { |
michael@0 | 230 | gfxFontInfoLoader *loader = static_cast<gfxFontInfoLoader*>(aThis); |
michael@0 | 231 | loader->StartLoader(0, loader->GetInterval()); |
michael@0 | 232 | } |
michael@0 | 233 | |
michael@0 | 234 | void LoadFontInfoTimerFire(); |
michael@0 | 235 | |
michael@0 | 236 | void AddShutdownObserver(); |
michael@0 | 237 | void RemoveShutdownObserver(); |
michael@0 | 238 | |
michael@0 | 239 | nsCOMPtr<nsITimer> mTimer; |
michael@0 | 240 | nsCOMPtr<nsIObserver> mObserver; |
michael@0 | 241 | nsCOMPtr<nsIThread> mFontLoaderThread; |
michael@0 | 242 | uint32_t mInterval; |
michael@0 | 243 | TimerState mState; |
michael@0 | 244 | |
michael@0 | 245 | // after async font loader completes, data is stored here |
michael@0 | 246 | nsRefPtr<FontInfoData> mFontInfo; |
michael@0 | 247 | |
michael@0 | 248 | // time spent on the loader thread |
michael@0 | 249 | mozilla::TimeDuration mLoadTime; |
michael@0 | 250 | }; |
michael@0 | 251 | |
michael@0 | 252 | #endif /* GFX_FONT_INFO_LOADER_H */ |