| 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 "core/css/FontLoader.h" | |
| 6 | |
| 7 #include "core/css/CSSFontSelector.h" | |
| 8 #include "core/dom/Document.h" | |
| 9 #include "core/dom/IncrementLoadEventDelayCount.h" | |
| 10 #include "core/fetch/FontResource.h" | |
| 11 #include "core/inspector/ConsoleMessage.h" | |
| 12 | |
| 13 namespace blink { | |
| 14 | |
| 15 struct FontLoader::FontToLoad : public GarbageCollectedFinalized<FontLoader::Fon
tToLoad> { | |
| 16 public: | |
| 17 static FontToLoad* create(FontResource* fontResource, Document& document) | |
| 18 { | |
| 19 return new FontToLoad(fontResource, document); | |
| 20 } | |
| 21 | |
| 22 virtual ~FontToLoad() | |
| 23 { | |
| 24 ASSERT(!fontResource); | |
| 25 } | |
| 26 | |
| 27 Member<FontResource> fontResource; | |
| 28 OwnPtr<IncrementLoadEventDelayCount> delay; | |
| 29 | |
| 30 void dispose() | |
| 31 { | |
| 32 fontResource = nullptr; | |
| 33 delay.clear(); | |
| 34 } | |
| 35 | |
| 36 DEFINE_INLINE_TRACE() { visitor->trace(fontResource); } | |
| 37 | |
| 38 private: | |
| 39 FontToLoad(FontResource* resource, Document& document) | |
| 40 : fontResource(resource) | |
| 41 , delay(IncrementLoadEventDelayCount::create(document)) | |
| 42 { | |
| 43 } | |
| 44 }; | |
| 45 | |
| 46 FontLoader::FontLoader(CSSFontSelector* fontSelector, Document* document) | |
| 47 : m_beginLoadingTimer(this, &FontLoader::beginLoadTimerFired) | |
| 48 , m_fontSelector(fontSelector) | |
| 49 , m_document(document) | |
| 50 { | |
| 51 ThreadState::current()->registerPreFinalizer(this); | |
| 52 } | |
| 53 | |
| 54 FontLoader::~FontLoader() | |
| 55 { | |
| 56 } | |
| 57 | |
| 58 void FontLoader::dispose() | |
| 59 { | |
| 60 if (!m_document) { | |
| 61 ASSERT(m_fontsToBeginLoading.isEmpty()); | |
| 62 return; | |
| 63 } | |
| 64 m_beginLoadingTimer.stop(); | |
| 65 // This will decrement the request counts on the ResourceFetcher for all the | |
| 66 // fonts that were pending at the time the FontLoader dies. | |
| 67 clearPendingFonts(); | |
| 68 } | |
| 69 | |
| 70 void FontLoader::addFontToBeginLoading(FontResource* fontResource) | |
| 71 { | |
| 72 if (!m_document || !fontResource->stillNeedsLoad() || fontResource->loadSche
duled()) | |
| 73 return; | |
| 74 | |
| 75 m_fontsToBeginLoading.append(FontToLoad::create(fontResource, *m_document)); | |
| 76 fontResource->didScheduleLoad(); | |
| 77 if (!m_beginLoadingTimer.isActive()) | |
| 78 m_beginLoadingTimer.startOneShot(0, BLINK_FROM_HERE); | |
| 79 } | |
| 80 | |
| 81 void FontLoader::beginLoadTimerFired(Timer<blink::FontLoader>*) | |
| 82 { | |
| 83 loadPendingFonts(); | |
| 84 } | |
| 85 | |
| 86 void FontLoader::loadPendingFonts() | |
| 87 { | |
| 88 ASSERT(m_document); | |
| 89 | |
| 90 FontsToLoadVector fontsToBeginLoading; | |
| 91 fontsToBeginLoading.swap(m_fontsToBeginLoading); | |
| 92 for (const auto& fontToLoad : fontsToBeginLoading) { | |
| 93 if (m_document->frame()) | |
| 94 fontToLoad->fontResource->beginLoadIfNeeded(m_document->fetcher()); | |
| 95 else | |
| 96 fontToLoad->fontResource->error(Resource::LoadError); | |
| 97 fontToLoad->dispose(); | |
| 98 } | |
| 99 | |
| 100 // When the local fontsToBeginLoading vector goes out of scope it will | |
| 101 // decrement the request counts on the ResourceFetcher for all the fonts | |
| 102 // that were just loaded. | |
| 103 } | |
| 104 | |
| 105 void FontLoader::fontFaceInvalidated() | |
| 106 { | |
| 107 if (m_fontSelector) | |
| 108 m_fontSelector->fontFaceInvalidated(); | |
| 109 } | |
| 110 | |
| 111 void FontLoader::didFailToDecode(FontResource* fontResource) | |
| 112 { | |
| 113 // FIXME: Provide more useful message such as OTS rejection reason. | |
| 114 // See crbug.com/97467 | |
| 115 if (m_fontSelector && m_fontSelector->document()) { | |
| 116 m_fontSelector->document()->addConsoleMessage(ConsoleMessage::create(Oth
erMessageSource, WarningMessageLevel, "Failed to decode downloaded font: " + fon
tResource->url().elidedString())); | |
| 117 if (fontResource->otsParsingMessage().length() > 1) | |
| 118 m_fontSelector->document()->addConsoleMessage(ConsoleMessage::create
(OtherMessageSource, WarningMessageLevel, "OTS parsing error: " + fontResource->
otsParsingMessage())); | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 #if !ENABLE(OILPAN) | |
| 123 void FontLoader::clearDocumentAndFontSelector() | |
| 124 { | |
| 125 if (!m_document) { | |
| 126 ASSERT(m_fontsToBeginLoading.isEmpty()); | |
| 127 return; | |
| 128 } | |
| 129 | |
| 130 m_beginLoadingTimer.stop(); | |
| 131 clearPendingFonts(); | |
| 132 m_document = nullptr; | |
| 133 m_fontSelector = nullptr; | |
| 134 } | |
| 135 #endif | |
| 136 | |
| 137 void FontLoader::clearPendingFonts() | |
| 138 { | |
| 139 for (const auto& fontToLoad : m_fontsToBeginLoading) { | |
| 140 fontToLoad->fontResource->didUnscheduleLoad(); | |
| 141 fontToLoad->dispose(); | |
| 142 } | |
| 143 m_fontsToBeginLoading.clear(); | |
| 144 } | |
| 145 | |
| 146 DEFINE_TRACE(FontLoader) | |
| 147 { | |
| 148 visitor->trace(m_fontsToBeginLoading); | |
| 149 visitor->trace(m_document); | |
| 150 visitor->trace(m_fontSelector); | |
| 151 } | |
| 152 | |
| 153 } // namespace blink | |
| OLD | NEW |