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 } | |
52 | |
53 FontLoader::~FontLoader() | |
54 { | |
55 #if ENABLE(OILPAN) | |
56 if (!m_document) { | |
57 ASSERT(m_fontsToBeginLoading.isEmpty()); | |
58 return; | |
59 } | |
60 m_beginLoadingTimer.stop(); | |
61 // This will decrement the request counts on the ResourceFetcher for all the | |
62 // fonts that were pending at the time the FontLoader dies. | |
63 clearPendingFonts(); | |
64 #endif | |
65 } | |
66 | |
67 void FontLoader::addFontToBeginLoading(FontResource* fontResource) | |
68 { | |
69 if (!m_document || !fontResource->stillNeedsLoad() || fontResource->loadSche
duled()) | |
70 return; | |
71 | |
72 m_fontsToBeginLoading.append(FontToLoad::create(fontResource, *m_document)); | |
73 fontResource->didScheduleLoad(); | |
74 if (!m_beginLoadingTimer.isActive()) | |
75 m_beginLoadingTimer.startOneShot(0, BLINK_FROM_HERE); | |
76 } | |
77 | |
78 void FontLoader::beginLoadTimerFired(Timer<blink::FontLoader>*) | |
79 { | |
80 loadPendingFonts(); | |
81 } | |
82 | |
83 void FontLoader::loadPendingFonts() | |
84 { | |
85 ASSERT(m_document); | |
86 | |
87 FontsToLoadVector fontsToBeginLoading; | |
88 fontsToBeginLoading.swap(m_fontsToBeginLoading); | |
89 for (const auto& fontToLoad : fontsToBeginLoading) { | |
90 if (m_document->frame()) | |
91 fontToLoad->fontResource->beginLoadIfNeeded(m_document->fetcher()); | |
92 else | |
93 fontToLoad->fontResource->error(Resource::LoadError); | |
94 fontToLoad->dispose(); | |
95 } | |
96 | |
97 // When the local fontsToBeginLoading vector goes out of scope it will | |
98 // decrement the request counts on the ResourceFetcher for all the fonts | |
99 // that were just loaded. | |
100 } | |
101 | |
102 void FontLoader::fontFaceInvalidated() | |
103 { | |
104 if (m_fontSelector) | |
105 m_fontSelector->fontFaceInvalidated(); | |
106 } | |
107 | |
108 void FontLoader::didFailToDecode(FontResource* fontResource) | |
109 { | |
110 // FIXME: Provide more useful message such as OTS rejection reason. | |
111 // See crbug.com/97467 | |
112 if (m_fontSelector && m_fontSelector->document()) { | |
113 m_fontSelector->document()->addConsoleMessage(ConsoleMessage::create(Oth
erMessageSource, WarningMessageLevel, "Failed to decode downloaded font: " + fon
tResource->url().elidedString())); | |
114 if (fontResource->otsParsingMessage().length() > 1) | |
115 m_fontSelector->document()->addConsoleMessage(ConsoleMessage::create
(OtherMessageSource, WarningMessageLevel, "OTS parsing error: " + fontResource->
otsParsingMessage())); | |
116 } | |
117 } | |
118 | |
119 #if !ENABLE(OILPAN) | |
120 void FontLoader::clearDocumentAndFontSelector() | |
121 { | |
122 if (!m_document) { | |
123 ASSERT(m_fontsToBeginLoading.isEmpty()); | |
124 return; | |
125 } | |
126 | |
127 m_beginLoadingTimer.stop(); | |
128 clearPendingFonts(); | |
129 m_document = nullptr; | |
130 m_fontSelector = nullptr; | |
131 } | |
132 #endif | |
133 | |
134 void FontLoader::clearPendingFonts() | |
135 { | |
136 for (const auto& fontToLoad : m_fontsToBeginLoading) { | |
137 fontToLoad->fontResource->didUnscheduleLoad(); | |
138 fontToLoad->dispose(); | |
139 } | |
140 m_fontsToBeginLoading.clear(); | |
141 } | |
142 | |
143 DEFINE_TRACE(FontLoader) | |
144 { | |
145 visitor->trace(m_fontsToBeginLoading); | |
146 visitor->trace(m_document); | |
147 visitor->trace(m_fontSelector); | |
148 } | |
149 | |
150 } // namespace blink | |
OLD | NEW |