OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/css/RemoteFontFaceSource.h" | 5 #include "core/css/RemoteFontFaceSource.h" |
6 | 6 |
7 #include "core/css/CSSCustomFontData.h" | 7 #include "core/css/CSSCustomFontData.h" |
8 #include "core/css/CSSFontFace.h" | 8 #include "core/css/CSSFontFace.h" |
9 #include "core/css/CSSFontSelector.h" | 9 #include "core/css/CSSFontSelector.h" |
10 #include "core/dom/Document.h" | 10 #include "core/dom/Document.h" |
11 #include "core/fetch/ResourceFetcher.h" | 11 #include "core/fetch/ResourceFetcher.h" |
| 12 #include "core/fetch/ResourceLoader.h" |
12 #include "core/inspector/ConsoleMessage.h" | 13 #include "core/inspector/ConsoleMessage.h" |
13 #include "core/loader/FrameLoaderClient.h" | 14 #include "core/loader/FrameLoaderClient.h" |
14 #include "core/page/NetworkStateNotifier.h" | 15 #include "core/page/NetworkStateNotifier.h" |
15 #include "platform/Histogram.h" | 16 #include "platform/Histogram.h" |
16 #include "platform/RuntimeEnabledFeatures.h" | 17 #include "platform/RuntimeEnabledFeatures.h" |
17 #include "platform/fonts/FontCache.h" | 18 #include "platform/fonts/FontCache.h" |
18 #include "platform/fonts/FontDescription.h" | 19 #include "platform/fonts/FontDescription.h" |
19 #include "platform/fonts/SimpleFontData.h" | 20 #include "platform/fonts/SimpleFontData.h" |
20 #include "public/platform/WebEffectiveConnectionType.h" | 21 #include "public/platform/WebEffectiveConnectionType.h" |
21 #include "wtf/CurrentTime.h" | 22 #include "wtf/CurrentTime.h" |
(...skipping 27 matching lines...) Expand all Loading... |
49 } | 50 } |
50 | 51 |
51 } // namespace | 52 } // namespace |
52 | 53 |
53 RemoteFontFaceSource::RemoteFontFaceSource(FontResource* font, | 54 RemoteFontFaceSource::RemoteFontFaceSource(FontResource* font, |
54 CSSFontSelector* fontSelector, | 55 CSSFontSelector* fontSelector, |
55 FontDisplay display) | 56 FontDisplay display) |
56 : m_font(font), | 57 : m_font(font), |
57 m_fontSelector(fontSelector), | 58 m_fontSelector(fontSelector), |
58 m_display(display), | 59 m_display(display), |
59 m_period(display == FontDisplaySwap ? SwapPeriod : BlockPeriod), | 60 m_period(BlockPeriod), |
60 m_histograms(font->url().protocolIsData() | 61 m_histograms(font->url().protocolIsData() |
61 ? FontLoadHistograms::FromDataURL | 62 ? FontLoadHistograms::FromDataURL |
62 : font->isLoaded() ? FontLoadHistograms::FromMemoryCache | 63 : font->isLoaded() ? FontLoadHistograms::FromMemoryCache |
63 : FontLoadHistograms::FromUnknown, | 64 : FontLoadHistograms::FromUnknown, |
64 m_display), | 65 m_display), |
65 m_isInterventionTriggered(false) { | 66 m_isInterventionTriggered(false) { |
66 ThreadState::current()->registerPreFinalizer(this); | 67 ThreadState::current()->registerPreFinalizer(this); |
67 m_font->addClient(this); | |
68 | 68 |
69 if (shouldTriggerWebFontsIntervention()) { | 69 if (shouldTriggerWebFontsIntervention()) { |
70 m_isInterventionTriggered = true; | 70 m_isInterventionTriggered = true; |
71 m_period = SwapPeriod; | 71 m_period = SwapPeriod; |
72 m_fontSelector->document()->addConsoleMessage(ConsoleMessage::create( | 72 m_fontSelector->document()->addConsoleMessage(ConsoleMessage::create( |
73 OtherMessageSource, InfoMessageLevel, | 73 OtherMessageSource, InfoMessageLevel, |
74 "Slow network is detected. Fallback font will be used while loading: " + | 74 "Slow network is detected. Fallback font will be used while loading: " + |
75 m_font->url().elidedString())); | 75 m_font->url().elidedString())); |
76 } | 76 } |
| 77 |
| 78 if (m_display == FontDisplaySwap && !shouldTriggerCacheAwareAdaptation()) |
| 79 m_period = SwapPeriod; |
| 80 |
| 81 // Font loading callbacks may be called in addClient(), make sure |m_period| |
| 82 // is properly set before calling. |
| 83 m_font->addClient(this); |
77 } | 84 } |
78 | 85 |
79 RemoteFontFaceSource::~RemoteFontFaceSource() {} | 86 RemoteFontFaceSource::~RemoteFontFaceSource() {} |
80 | 87 |
81 void RemoteFontFaceSource::dispose() { | 88 void RemoteFontFaceSource::dispose() { |
82 m_font->removeClient(this); | 89 m_font->removeClient(this); |
83 m_font = nullptr; | 90 m_font = nullptr; |
84 pruneTable(); | 91 pruneTable(); |
85 } | 92 } |
86 | 93 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 | 161 |
155 if (m_display == FontDisplayBlock || | 162 if (m_display == FontDisplayBlock || |
156 (!m_isInterventionTriggered && m_display == FontDisplayAuto)) | 163 (!m_isInterventionTriggered && m_display == FontDisplayAuto)) |
157 switchToSwapPeriod(); | 164 switchToSwapPeriod(); |
158 else if (m_display == FontDisplayFallback) | 165 else if (m_display == FontDisplayFallback) |
159 switchToFailurePeriod(); | 166 switchToFailurePeriod(); |
160 | 167 |
161 m_histograms.longLimitExceeded(m_isInterventionTriggered); | 168 m_histograms.longLimitExceeded(m_isInterventionTriggered); |
162 } | 169 } |
163 | 170 |
| 171 void RemoteFontFaceSource::willReloadAfterDiskCacheMiss(const FontResource*) { |
| 172 DCHECK(m_font->isLoading()); |
| 173 if (m_display == FontDisplaySwap) |
| 174 switchToSwapPeriod(); |
| 175 } |
| 176 |
164 void RemoteFontFaceSource::switchToSwapPeriod() { | 177 void RemoteFontFaceSource::switchToSwapPeriod() { |
165 ASSERT(m_period == BlockPeriod); | 178 DCHECK_EQ(m_period, BlockPeriod); |
166 m_period = SwapPeriod; | 179 m_period = SwapPeriod; |
167 | 180 |
168 pruneTable(); | 181 pruneTable(); |
169 if (m_face) { | 182 if (m_face) { |
170 m_fontSelector->fontFaceInvalidated(); | 183 m_fontSelector->fontFaceInvalidated(); |
171 m_face->didBecomeVisibleFallback(this); | 184 m_face->didBecomeVisibleFallback(this); |
172 } | 185 } |
173 | 186 |
174 m_histograms.recordFallbackTime(m_font.get()); | 187 m_histograms.recordFallbackTime(m_font.get()); |
175 } | 188 } |
176 | 189 |
177 void RemoteFontFaceSource::switchToFailurePeriod() { | 190 void RemoteFontFaceSource::switchToFailurePeriod() { |
178 if (m_period == BlockPeriod) | 191 if (m_period == BlockPeriod) |
179 switchToSwapPeriod(); | 192 switchToSwapPeriod(); |
180 ASSERT(m_period == SwapPeriod); | 193 DCHECK_EQ(m_period, SwapPeriod); |
181 m_period = FailurePeriod; | 194 m_period = FailurePeriod; |
182 } | 195 } |
183 | 196 |
184 bool RemoteFontFaceSource::shouldTriggerWebFontsIntervention() { | 197 bool RemoteFontFaceSource::shouldTriggerWebFontsIntervention() { |
185 if (RuntimeEnabledFeatures::webFontsInterventionTriggerEnabled()) | 198 if (RuntimeEnabledFeatures::webFontsInterventionTriggerEnabled()) |
186 return true; | 199 return true; |
187 if (m_histograms.dataSource() == FontLoadHistograms::FromMemoryCache || | 200 if (m_histograms.dataSource() == FontLoadHistograms::FromMemoryCache || |
188 m_histograms.dataSource() == FontLoadHistograms::FromDataURL) | 201 m_histograms.dataSource() == FontLoadHistograms::FromDataURL) |
189 return false; | 202 return false; |
190 | 203 |
191 bool isV2Enabled = | 204 bool isV2Enabled = |
192 RuntimeEnabledFeatures::webFontsInterventionV2With2GEnabled() || | 205 RuntimeEnabledFeatures::webFontsInterventionV2With2GEnabled() || |
193 RuntimeEnabledFeatures::webFontsInterventionV2With3GEnabled() || | 206 RuntimeEnabledFeatures::webFontsInterventionV2With3GEnabled() || |
194 RuntimeEnabledFeatures::webFontsInterventionV2WithSlow2GEnabled(); | 207 RuntimeEnabledFeatures::webFontsInterventionV2WithSlow2GEnabled(); |
195 | 208 |
196 bool networkIsSlow = | 209 bool networkIsSlow = |
197 isV2Enabled ? isEffectiveConnectionTypeSlowFor(m_fontSelector->document()) | 210 isV2Enabled ? isEffectiveConnectionTypeSlowFor(m_fontSelector->document()) |
198 : isConnectionTypeSlow(); | 211 : isConnectionTypeSlow(); |
199 | 212 |
200 return networkIsSlow && m_display == FontDisplayAuto; | 213 return networkIsSlow && m_display == FontDisplayAuto; |
201 } | 214 } |
202 | 215 |
| 216 bool RemoteFontFaceSource::shouldTriggerCacheAwareAdaptation() { |
| 217 if (m_font->stillNeedsLoad() && |
| 218 m_font->options().cacheAwareLoadingEnabled == IsCacheAwareLoadingEnabled) |
| 219 return true; |
| 220 if (m_font->isLoading() && m_font->loader()->isCacheAwareLoadingActivated()) |
| 221 return true; |
| 222 // willReloadAfterDiskCacheMiss() will not be called. |
| 223 return false; |
| 224 } |
| 225 |
203 PassRefPtr<SimpleFontData> RemoteFontFaceSource::createFontData( | 226 PassRefPtr<SimpleFontData> RemoteFontFaceSource::createFontData( |
204 const FontDescription& fontDescription) { | 227 const FontDescription& fontDescription) { |
205 if (!isLoaded()) | 228 if (!isLoaded()) |
206 return createLoadingFallbackFontData(fontDescription); | 229 return createLoadingFallbackFontData(fontDescription); |
207 | 230 |
208 if (!m_font->ensureCustomFontData() || m_period == FailurePeriod) | 231 if (!m_font->ensureCustomFontData() || m_period == FailurePeriod) |
209 return nullptr; | 232 return nullptr; |
210 | 233 |
211 m_histograms.recordFallbackTime(m_font.get()); | 234 m_histograms.recordFallbackTime(m_font.get()); |
212 | 235 |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 return Miss; | 475 return Miss; |
453 case FromUnknown: | 476 case FromUnknown: |
454 // Fall through. | 477 // Fall through. |
455 default: | 478 default: |
456 NOTREACHED(); | 479 NOTREACHED(); |
457 } | 480 } |
458 return Miss; | 481 return Miss; |
459 } | 482 } |
460 | 483 |
461 } // namespace blink | 484 } // namespace blink |
OLD | NEW |