| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "sky/engine/core/css/RemoteFontFaceSource.h" | |
| 6 | |
| 7 #include "sky/engine/core/css/CSSCustomFontData.h" | |
| 8 #include "sky/engine/core/css/CSSFontFace.h" | |
| 9 #include "sky/engine/core/css/FontLoader.h" | |
| 10 #include "sky/engine/platform/fonts/FontCache.h" | |
| 11 #include "sky/engine/platform/fonts/FontDescription.h" | |
| 12 #include "sky/engine/platform/fonts/SimpleFontData.h" | |
| 13 #include "sky/engine/public/platform/Platform.h" | |
| 14 #include "sky/engine/wtf/CurrentTime.h" | |
| 15 | |
| 16 namespace blink { | |
| 17 | |
| 18 RemoteFontFaceSource::RemoteFontFaceSource(FontResource* font, PassRefPtr<FontLo
ader> fontLoader) | |
| 19 : m_font(font) | |
| 20 , m_fontLoader(fontLoader) | |
| 21 { | |
| 22 m_font->addClient(this); | |
| 23 } | |
| 24 | |
| 25 RemoteFontFaceSource::~RemoteFontFaceSource() | |
| 26 { | |
| 27 m_font->removeClient(this); | |
| 28 pruneTable(); | |
| 29 } | |
| 30 | |
| 31 void RemoteFontFaceSource::pruneTable() | |
| 32 { | |
| 33 if (m_fontDataTable.isEmpty()) | |
| 34 return; | |
| 35 | |
| 36 for (FontDataTable::iterator it = m_fontDataTable.begin(); it != m_fontDataT
able.end(); ++it) { | |
| 37 SimpleFontData* fontData = it->value.get(); | |
| 38 if (fontData && fontData->customFontData()) | |
| 39 fontData->customFontData()->clearFontFaceSource(); | |
| 40 } | |
| 41 m_fontDataTable.clear(); | |
| 42 } | |
| 43 | |
| 44 bool RemoteFontFaceSource::isLoading() const | |
| 45 { | |
| 46 return !m_font->stillNeedsLoad() && !m_font->isLoaded(); | |
| 47 } | |
| 48 | |
| 49 bool RemoteFontFaceSource::isLoaded() const | |
| 50 { | |
| 51 return m_font->isLoaded(); | |
| 52 } | |
| 53 | |
| 54 bool RemoteFontFaceSource::isValid() const | |
| 55 { | |
| 56 return !m_font->errorOccurred(); | |
| 57 } | |
| 58 | |
| 59 void RemoteFontFaceSource::didStartFontLoad(FontResource*) | |
| 60 { | |
| 61 // We may send duplicated reports when multiple CSSFontFaceSource are | |
| 62 // registered at this FontResource. Associating the same URL to different | |
| 63 // font-family causes the case, but we treat them as indivisual resources. | |
| 64 m_histograms.loadStarted(); | |
| 65 } | |
| 66 | |
| 67 void RemoteFontFaceSource::fontLoaded(FontResource*) | |
| 68 { | |
| 69 m_histograms.recordRemoteFont(m_font.get()); | |
| 70 | |
| 71 pruneTable(); | |
| 72 if (m_face) { | |
| 73 m_fontLoader->fontFaceInvalidated(); | |
| 74 m_face->fontLoaded(this); | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 void RemoteFontFaceSource::fontLoadWaitLimitExceeded(FontResource*) | |
| 79 { | |
| 80 pruneTable(); | |
| 81 if (m_face) { | |
| 82 m_fontLoader->fontFaceInvalidated(); | |
| 83 m_face->fontLoadWaitLimitExceeded(this); | |
| 84 } | |
| 85 | |
| 86 m_histograms.recordFallbackTime(m_font.get()); | |
| 87 } | |
| 88 | |
| 89 PassRefPtr<SimpleFontData> RemoteFontFaceSource::createFontData(const FontDescri
ption& fontDescription) | |
| 90 { | |
| 91 if (!isLoaded()) | |
| 92 return createLoadingFallbackFontData(fontDescription); | |
| 93 | |
| 94 // Create new FontPlatformData from our CGFontRef, point size and ATSFontRef
. | |
| 95 if (!m_font->ensureCustomFontData()) | |
| 96 return nullptr; | |
| 97 | |
| 98 m_histograms.recordFallbackTime(m_font.get()); | |
| 99 | |
| 100 return SimpleFontData::create( | |
| 101 m_font->platformDataFromCustomData(fontDescription.effectiveFontSize(), | |
| 102 fontDescription.isSyntheticBold(), fontDescription.isSyntheticItalic
(), | |
| 103 fontDescription.orientation(), fontDescription.widthVariant()), Cust
omFontData::create()); | |
| 104 } | |
| 105 | |
| 106 PassRefPtr<SimpleFontData> RemoteFontFaceSource::createLoadingFallbackFontData(c
onst FontDescription& fontDescription) | |
| 107 { | |
| 108 // This temporary font is not retained and should not be returned. | |
| 109 FontCachePurgePreventer fontCachePurgePreventer; | |
| 110 SimpleFontData* temporaryFont = FontCache::fontCache()->getNonRetainedLastRe
sortFallbackFont(fontDescription); | |
| 111 if (!temporaryFont) { | |
| 112 ASSERT_NOT_REACHED(); | |
| 113 return nullptr; | |
| 114 } | |
| 115 RefPtr<CSSCustomFontData> cssFontData = CSSCustomFontData::create(this, m_fo
nt->exceedsFontLoadWaitLimit() ? CSSCustomFontData::VisibleFallback : CSSCustomF
ontData::InvisibleFallback); | |
| 116 return SimpleFontData::create(temporaryFont->platformData(), cssFontData); | |
| 117 } | |
| 118 | |
| 119 void RemoteFontFaceSource::beginLoadIfNeeded() | |
| 120 { | |
| 121 if (m_font->stillNeedsLoad()) | |
| 122 m_fontLoader->addFontToBeginLoading(m_font.get()); | |
| 123 | |
| 124 if (m_face) | |
| 125 m_face->didBeginLoad(); | |
| 126 } | |
| 127 | |
| 128 bool RemoteFontFaceSource::ensureFontData() | |
| 129 { | |
| 130 return m_font->ensureCustomFontData(); | |
| 131 } | |
| 132 | |
| 133 void RemoteFontFaceSource::FontLoadHistograms::loadStarted() | |
| 134 { | |
| 135 if (!m_loadStartTime) | |
| 136 m_loadStartTime = currentTimeMS(); | |
| 137 } | |
| 138 | |
| 139 void RemoteFontFaceSource::FontLoadHistograms::fallbackFontPainted() | |
| 140 { | |
| 141 if (!m_fallbackPaintTime) | |
| 142 m_fallbackPaintTime = currentTimeMS(); | |
| 143 } | |
| 144 | |
| 145 void RemoteFontFaceSource::FontLoadHistograms::recordFallbackTime(const FontReso
urce* font) | |
| 146 { | |
| 147 if (m_fallbackPaintTime <= 0) | |
| 148 return; | |
| 149 int duration = static_cast<int>(currentTimeMS() - m_fallbackPaintTime); | |
| 150 blink::Platform::current()->histogramCustomCounts("WebFont.BlankTextShownTim
e", duration, 0, 10000, 50); | |
| 151 m_fallbackPaintTime = -1; | |
| 152 } | |
| 153 | |
| 154 void RemoteFontFaceSource::FontLoadHistograms::recordRemoteFont(const FontResour
ce* font) | |
| 155 { | |
| 156 if (m_loadStartTime > 0 && font && !font->isLoading()) { | |
| 157 int duration = static_cast<int>(currentTimeMS() - m_loadStartTime); | |
| 158 blink::Platform::current()->histogramCustomCounts(histogramName(font), d
uration, 0, 10000, 50); | |
| 159 m_loadStartTime = -1; | |
| 160 | |
| 161 enum { Miss, Hit, DataUrl, CacheHitEnumMax }; | |
| 162 int histogramValue = font->url().protocolIsData() ? DataUrl | |
| 163 : font->response().wasCached() ? Hit | |
| 164 : Miss; | |
| 165 blink::Platform::current()->histogramEnumeration("WebFont.CacheHit", his
togramValue, CacheHitEnumMax); | |
| 166 | |
| 167 enum { CORSFail, CORSSuccess, CORSEnumMax }; | |
| 168 int corsValue = font->isCORSFailed() ? CORSFail : CORSSuccess; | |
| 169 blink::Platform::current()->histogramEnumeration("WebFont.CORSSuccess",
corsValue, CORSEnumMax); | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 const char* RemoteFontFaceSource::FontLoadHistograms::histogramName(const FontRe
source* font) | |
| 174 { | |
| 175 if (font->errorOccurred()) | |
| 176 return "WebFont.DownloadTime.LoadError"; | |
| 177 | |
| 178 unsigned size = font->encodedSize(); | |
| 179 if (size < 10 * 1024) | |
| 180 return "WebFont.DownloadTime.0.Under10KB"; | |
| 181 if (size < 50 * 1024) | |
| 182 return "WebFont.DownloadTime.1.10KBTo50KB"; | |
| 183 if (size < 100 * 1024) | |
| 184 return "WebFont.DownloadTime.2.50KBTo100KB"; | |
| 185 if (size < 1024 * 1024) | |
| 186 return "WebFont.DownloadTime.3.100KBTo1MB"; | |
| 187 return "WebFont.DownloadTime.4.Over1MB"; | |
| 188 } | |
| 189 | |
| 190 } // namespace blink | |
| OLD | NEW |