Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 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 "config.h" |
| 6 | #include "core/css/FontLoader.h" |
| 7 | |
Torne (Richard Coles) | 76c265b | 2014-06-25 10:31:22 +0100 | [diff] [blame] | 8 | #include "core/css/CSSFontSelector.h" |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 9 | #include "core/fetch/FontResource.h" |
| 10 | #include "core/fetch/ResourceFetcher.h" |
| 11 | |
| 12 | namespace WebCore { |
| 13 | |
Torne (Richard Coles) | 76c265b | 2014-06-25 10:31:22 +0100 | [diff] [blame] | 14 | FontLoader::FontLoader(CSSFontSelector* fontSelector, ResourceFetcher* resourceFetcher) |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 15 | : m_beginLoadingTimer(this, &FontLoader::beginLoadTimerFired) |
Torne (Richard Coles) | 76c265b | 2014-06-25 10:31:22 +0100 | [diff] [blame] | 16 | , m_fontSelector(fontSelector) |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 17 | , m_resourceFetcher(resourceFetcher) |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | FontLoader::~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 | |
| 36 | void 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 | |
| 47 | void FontLoader::beginLoadTimerFired(Timer<WebCore::FontLoader>*) |
| 48 | { |
| 49 | loadPendingFonts(); |
| 50 | } |
| 51 | |
| 52 | void 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) | 76c265b | 2014-06-25 10:31:22 +0100 | [diff] [blame] | 68 | void FontLoader::fontFaceInvalidated() |
| 69 | { |
| 70 | if (m_fontSelector) |
| 71 | m_fontSelector->fontFaceInvalidated(); |
| 72 | } |
| 73 | |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 74 | #if !ENABLE(OILPAN) |
Torne (Richard Coles) | 76c265b | 2014-06-25 10:31:22 +0100 | [diff] [blame] | 75 | void FontLoader::clearResourceFetcherAndFontSelector() |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 76 | { |
| 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) | 76c265b | 2014-06-25 10:31:22 +0100 | [diff] [blame] | 85 | m_fontSelector = nullptr; |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 86 | } |
| 87 | #endif |
| 88 | |
| 89 | void FontLoader::trace(Visitor* visitor) |
| 90 | { |
| 91 | visitor->trace(m_resourceFetcher); |
Torne (Richard Coles) | 76c265b | 2014-06-25 10:31:22 +0100 | [diff] [blame] | 92 | visitor->trace(m_fontSelector); |
Torne (Richard Coles) | d6cdb82 | 2014-06-03 10:59:05 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | } // namespace WebCore |