blob: 20528ea4443a5f05c6043108a7ef98456a4b6226 [file] [log] [blame]
Torne (Richard Coles)d6cdb822014-06-03 10:59:05 +01001// 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 "config.h"
6#include "core/css/FontLoader.h"
7
8#include "core/fetch/FontResource.h"
9#include "core/fetch/ResourceFetcher.h"
10
11namespace WebCore {
12
13FontLoader::FontLoader(ResourceFetcher* resourceFetcher)
14 : m_beginLoadingTimer(this, &FontLoader::beginLoadTimerFired)
15 , m_resourceFetcher(resourceFetcher)
16{
17}
18
19FontLoader::~FontLoader()
20{
21#if ENABLE(OILPAN)
22 if (!m_resourceFetcher) {
23 ASSERT(m_fontsToBeginLoading.isEmpty());
24 return;
25 }
26 m_beginLoadingTimer.stop();
27
28 // When the m_fontsToBeginLoading vector is destroyed it will decrement the
29 // request counts on the ResourceFetcher for all the fonts that were pending
30 // at the time the FontLoader dies.
31#endif
32}
33
34void FontLoader::addFontToBeginLoading(FontResource* fontResource)
35{
36 if (!m_resourceFetcher || !fontResource->stillNeedsLoad())
37 return;
38
39 m_fontsToBeginLoading.append(
40 std::make_pair(fontResource, ResourceLoader::RequestCountTracker(m_resourceFetcher, fontResource)));
41 if (!m_beginLoadingTimer.isActive())
42 m_beginLoadingTimer.startOneShot(0, FROM_HERE);
43}
44
45void FontLoader::beginLoadTimerFired(Timer<WebCore::FontLoader>*)
46{
47 loadPendingFonts();
48}
49
50void FontLoader::loadPendingFonts()
51{
52 ASSERT(m_resourceFetcher);
53
54 FontsToLoadVector fontsToBeginLoading;
55 fontsToBeginLoading.swap(m_fontsToBeginLoading);
56 for (FontsToLoadVector::iterator it = fontsToBeginLoading.begin(); it != fontsToBeginLoading.end(); ++it) {
57 FontResource* fontResource = it->first.get();
58 fontResource->beginLoadIfNeeded(m_resourceFetcher);
59 }
60
61 // When the local fontsToBeginLoading vector goes out of scope it will
62 // decrement the request counts on the ResourceFetcher for all the fonts
63 // that were just loaded.
64}
65
66#if !ENABLE(OILPAN)
67void FontLoader::clearResourceFetcher()
68{
69 if (!m_resourceFetcher) {
70 ASSERT(m_fontsToBeginLoading.isEmpty());
71 return;
72 }
73
74 m_beginLoadingTimer.stop();
75 m_fontsToBeginLoading.clear();
76 m_resourceFetcher = nullptr;
77}
78#endif
79
80void FontLoader::trace(Visitor* visitor)
81{
82 visitor->trace(m_resourceFetcher);
83}
84
85} // namespace WebCore