blob: 189c37cf93c37d6fbc89ef659c8e25ff6ec25444 [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
Torne (Richard Coles)76c265b2014-06-25 10:31:22 +01008#include "core/css/CSSFontSelector.h"
Torne (Richard Coles)d6cdb822014-06-03 10:59:05 +01009#include "core/fetch/FontResource.h"
10#include "core/fetch/ResourceFetcher.h"
11
12namespace WebCore {
13
Torne (Richard Coles)76c265b2014-06-25 10:31:22 +010014FontLoader::FontLoader(CSSFontSelector* fontSelector, ResourceFetcher* resourceFetcher)
Torne (Richard Coles)d6cdb822014-06-03 10:59:05 +010015 : m_beginLoadingTimer(this, &FontLoader::beginLoadTimerFired)
Torne (Richard Coles)76c265b2014-06-25 10:31:22 +010016 , m_fontSelector(fontSelector)
Torne (Richard Coles)d6cdb822014-06-03 10:59:05 +010017 , m_resourceFetcher(resourceFetcher)
18{
19}
20
21FontLoader::~FontLoader()
22{
23#if ENABLE(OILPAN)
24 if (!m_resourceFetcher) {
25 ASSERT(m_fontsToBeginLoading.isEmpty());
26 return;
27 }
28 m_beginLoadingTimer.stop();
29
30 // When the m_fontsToBeginLoading vector is destroyed it will decrement the
31 // request counts on the ResourceFetcher for all the fonts that were pending
32 // at the time the FontLoader dies.
33#endif
34}
35
36void FontLoader::addFontToBeginLoading(FontResource* fontResource)
37{
38 if (!m_resourceFetcher || !fontResource->stillNeedsLoad())
39 return;
40
41 m_fontsToBeginLoading.append(
42 std::make_pair(fontResource, ResourceLoader::RequestCountTracker(m_resourceFetcher, fontResource)));
43 if (!m_beginLoadingTimer.isActive())
44 m_beginLoadingTimer.startOneShot(0, FROM_HERE);
45}
46
47void FontLoader::beginLoadTimerFired(Timer<WebCore::FontLoader>*)
48{
49 loadPendingFonts();
50}
51
52void FontLoader::loadPendingFonts()
53{
54 ASSERT(m_resourceFetcher);
55
56 FontsToLoadVector fontsToBeginLoading;
57 fontsToBeginLoading.swap(m_fontsToBeginLoading);
58 for (FontsToLoadVector::iterator it = fontsToBeginLoading.begin(); it != fontsToBeginLoading.end(); ++it) {
59 FontResource* fontResource = it->first.get();
60 fontResource->beginLoadIfNeeded(m_resourceFetcher);
61 }
62
63 // When the local fontsToBeginLoading vector goes out of scope it will
64 // decrement the request counts on the ResourceFetcher for all the fonts
65 // that were just loaded.
66}
67
Torne (Richard Coles)76c265b2014-06-25 10:31:22 +010068void FontLoader::fontFaceInvalidated()
69{
70 if (m_fontSelector)
71 m_fontSelector->fontFaceInvalidated();
72}
73
Torne (Richard Coles)d6cdb822014-06-03 10:59:05 +010074#if !ENABLE(OILPAN)
Torne (Richard Coles)76c265b2014-06-25 10:31:22 +010075void FontLoader::clearResourceFetcherAndFontSelector()
Torne (Richard Coles)d6cdb822014-06-03 10:59:05 +010076{
77 if (!m_resourceFetcher) {
78 ASSERT(m_fontsToBeginLoading.isEmpty());
79 return;
80 }
81
82 m_beginLoadingTimer.stop();
83 m_fontsToBeginLoading.clear();
84 m_resourceFetcher = nullptr;
Torne (Richard Coles)76c265b2014-06-25 10:31:22 +010085 m_fontSelector = nullptr;
Torne (Richard Coles)d6cdb822014-06-03 10:59:05 +010086}
87#endif
88
89void FontLoader::trace(Visitor* visitor)
90{
91 visitor->trace(m_resourceFetcher);
Torne (Richard Coles)76c265b2014-06-25 10:31:22 +010092 visitor->trace(m_fontSelector);
Torne (Richard Coles)d6cdb822014-06-03 10:59:05 +010093}
94
95} // namespace WebCore