Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(227)

Side by Side Diff: third_party/WebKit/Source/core/fetch/FontResource.cpp

Issue 2501543002: Loading: move FontResource to core/loader/resource (Closed)
Patch Set: one rebase error for webkit_unit_tests build Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Torch Mobile, Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include "core/fetch/FontResource.h"
28
29 #include "core/fetch/FetchRequest.h"
30 #include "core/fetch/ResourceClientWalker.h"
31 #include "core/fetch/ResourceFetcher.h"
32 #include "platform/Histogram.h"
33 #include "platform/SharedBuffer.h"
34 #include "platform/fonts/FontCustomPlatformData.h"
35 #include "platform/fonts/FontPlatformData.h"
36 #include "wtf/CurrentTime.h"
37
38 namespace blink {
39
40 // Durations of font-display periods.
41 // https://tabatkins.github.io/specs/css-font-display/#font-display-desc
42 static const double fontLoadWaitShortLimitSec = 0.1;
43 static const double fontLoadWaitLongLimitSec = 3.0;
44
45 enum FontPackageFormat {
46 PackageFormatUnknown,
47 PackageFormatSFNT,
48 PackageFormatWOFF,
49 PackageFormatWOFF2,
50 PackageFormatSVG,
51 PackageFormatEnumMax
52 };
53
54 static FontPackageFormat packageFormatOf(SharedBuffer* buffer) {
55 if (buffer->size() < 4)
56 return PackageFormatUnknown;
57
58 const char* data = buffer->data();
59 if (data[0] == 'w' && data[1] == 'O' && data[2] == 'F' && data[3] == 'F')
60 return PackageFormatWOFF;
61 if (data[0] == 'w' && data[1] == 'O' && data[2] == 'F' && data[3] == '2')
62 return PackageFormatWOFF2;
63 return PackageFormatSFNT;
64 }
65
66 static void recordPackageFormatHistogram(FontPackageFormat format) {
67 DEFINE_THREAD_SAFE_STATIC_LOCAL(
68 EnumerationHistogram, packageFormatHistogram,
69 new EnumerationHistogram("WebFont.PackageFormat", PackageFormatEnumMax));
70 packageFormatHistogram.count(format);
71 }
72
73 FontResource* FontResource::fetch(FetchRequest& request,
74 ResourceFetcher* fetcher) {
75 DCHECK_EQ(request.resourceRequest().frameType(),
76 WebURLRequest::FrameTypeNone);
77 request.mutableResourceRequest().setRequestContext(
78 WebURLRequest::RequestContextFont);
79 return toFontResource(
80 fetcher->requestResource(request, FontResourceFactory()));
81 }
82
83 FontResource::FontResource(const ResourceRequest& resourceRequest,
84 const ResourceLoaderOptions& options)
85 : Resource(resourceRequest, Font, options),
86 m_loadLimitState(LoadNotStarted),
87 m_corsFailed(false),
88 m_fontLoadShortLimitTimer(this,
89 &FontResource::fontLoadShortLimitCallback),
90 m_fontLoadLongLimitTimer(this, &FontResource::fontLoadLongLimitCallback) {
91 }
92
93 FontResource::~FontResource() {}
94
95 void FontResource::didAddClient(ResourceClient* c) {
96 DCHECK(FontResourceClient::isExpectedType(c));
97 Resource::didAddClient(c);
98 if (m_loadLimitState == ShortLimitExceeded ||
99 m_loadLimitState == LongLimitExceeded)
100 static_cast<FontResourceClient*>(c)->fontLoadShortLimitExceeded(this);
101 if (m_loadLimitState == LongLimitExceeded)
102 static_cast<FontResourceClient*>(c)->fontLoadLongLimitExceeded(this);
103 }
104
105 void FontResource::setRevalidatingRequest(const ResourceRequest& request) {
106 // Reload will use the same object, and needs to reset |m_loadLimitState|
107 // before any didAddClient() is called again.
108 m_loadLimitState = LoadNotStarted;
109 Resource::setRevalidatingRequest(request);
110 }
111
112 void FontResource::startLoadLimitTimers() {
113 DCHECK(isLoading());
114 DCHECK_EQ(m_loadLimitState, LoadNotStarted);
115 m_loadLimitState = UnderLimit;
116 m_fontLoadShortLimitTimer.startOneShot(fontLoadWaitShortLimitSec,
117 BLINK_FROM_HERE);
118 m_fontLoadLongLimitTimer.startOneShot(fontLoadWaitLongLimitSec,
119 BLINK_FROM_HERE);
120 }
121
122 bool FontResource::ensureCustomFontData() {
123 if (!m_fontData && !errorOccurred() && !isLoading()) {
124 if (data())
125 m_fontData = FontCustomPlatformData::create(data(), m_otsParsingMessage);
126
127 if (m_fontData) {
128 recordPackageFormatHistogram(packageFormatOf(data()));
129 } else {
130 setStatus(DecodeError);
131 recordPackageFormatHistogram(PackageFormatUnknown);
132 }
133 }
134 return m_fontData.get();
135 }
136
137 FontPlatformData FontResource::platformDataFromCustomData(
138 float size,
139 bool bold,
140 bool italic,
141 FontOrientation orientation) {
142 DCHECK(m_fontData);
143 return m_fontData->fontPlatformData(size, bold, italic, orientation);
144 }
145
146 void FontResource::fontLoadShortLimitCallback(TimerBase*) {
147 if (!isLoading())
148 return;
149 DCHECK_EQ(m_loadLimitState, UnderLimit);
150 m_loadLimitState = ShortLimitExceeded;
151 ResourceClientWalker<FontResourceClient> walker(clients());
152 while (FontResourceClient* client = walker.next())
153 client->fontLoadShortLimitExceeded(this);
154 }
155
156 void FontResource::fontLoadLongLimitCallback(TimerBase*) {
157 if (!isLoading())
158 return;
159 DCHECK_EQ(m_loadLimitState, ShortLimitExceeded);
160 m_loadLimitState = LongLimitExceeded;
161 ResourceClientWalker<FontResourceClient> walker(clients());
162 while (FontResourceClient* client = walker.next())
163 client->fontLoadLongLimitExceeded(this);
164 }
165
166 void FontResource::allClientsAndObserversRemoved() {
167 m_fontData.reset();
168 Resource::allClientsAndObserversRemoved();
169 }
170
171 void FontResource::checkNotify() {
172 m_fontLoadShortLimitTimer.stop();
173 m_fontLoadLongLimitTimer.stop();
174
175 Resource::checkNotify();
176 }
177
178 bool FontResource::isLowPriorityLoadingAllowedForRemoteFont() const {
179 DCHECK(!url().protocolIsData());
180 DCHECK(!isLoaded());
181 ResourceClientWalker<FontResourceClient> walker(clients());
182 while (FontResourceClient* client = walker.next()) {
183 if (!client->isLowPriorityLoadingAllowedForRemoteFont()) {
184 return false;
185 }
186 }
187 return true;
188 }
189
190 void FontResource::onMemoryDump(WebMemoryDumpLevelOfDetail level,
191 WebProcessMemoryDump* memoryDump) const {
192 Resource::onMemoryDump(level, memoryDump);
193 if (!m_fontData)
194 return;
195 const String name = getMemoryDumpName() + "/decoded_webfont";
196 WebMemoryAllocatorDump* dump = memoryDump->createMemoryAllocatorDump(name);
197 dump->addScalar("size", "bytes", m_fontData->dataSize());
198 memoryDump->addSuballocation(dump->guid(), "malloc");
199 }
200
201 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/fetch/FontResource.h ('k') | third_party/WebKit/Source/core/frame/FrameSerializer.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698