Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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/paint/FirstMeaningfulPaintDetector.h" | |
| 6 | |
| 7 #include "core/css/FontFaceSet.h" | |
| 8 #include "core/fetch/ResourceFetcher.h" | |
| 9 #include "core/paint/PaintTiming.h" | |
| 10 | |
| 11 namespace blink { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // Web fonts that laid out more than this number of characters block First | |
| 16 // Meaningful Paint. | |
| 17 const int kBlankCharactersThreshold = 200; | |
| 18 | |
| 19 // FirstMeaningfulPaintDetector stops observing layouts and reports First | |
| 20 // Meaningful Paint when this duration passed from last network activity. | |
| 21 const double kTimeWithoutNetworkActivity = 2.0; | |
|
Charlie Harrison
2016/08/09 16:27:32
Hm, this name isn't quite clear. The name as-is ma
Kunihiko Sakamoto
2016/08/10 02:33:32
Thanks for the suggestion. Done.
| |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 FirstMeaningfulPaintDetector& FirstMeaningfulPaintDetector::from(Document& docum ent) | |
| 26 { | |
| 27 return PaintTiming::from(document).firstMeaningfulPaintDetector(); | |
| 28 } | |
| 29 | |
| 30 FirstMeaningfulPaintDetector::FirstMeaningfulPaintDetector(PaintTiming* paintTim ing) | |
| 31 : m_paintTiming(paintTiming) | |
| 32 , m_networkStableTimer(this, &FirstMeaningfulPaintDetector::networkStableTim erFired) | |
| 33 { | |
| 34 } | |
| 35 | |
| 36 Document* FirstMeaningfulPaintDetector::document() | |
| 37 { | |
| 38 return m_paintTiming->document(); | |
| 39 } | |
| 40 | |
| 41 void FirstMeaningfulPaintDetector::computeLayoutSignificance( | |
|
Charlie Harrison
2016/08/09 16:27:32
It would help to document this method with justifi
kouhei (in TOK)
2016/08/10 01:58:13
markNextPaintAsMeaningfulIfNeeded?
Kunihiko Sakamoto
2016/08/10 02:33:32
Done.
Kunihiko Sakamoto
2016/08/10 02:33:32
Yeah, that's a better description. Done.
| |
| 42 const LayoutObjectCounter& counter, int contentsHeightBeforeLayout, | |
| 43 int contentsHeightAfterLayout, int visibleHeight) | |
| 44 { | |
| 45 if (m_state == Reported) | |
| 46 return; | |
| 47 | |
| 48 unsigned delta = counter.count() - m_prevLayoutObjectCount; | |
| 49 m_prevLayoutObjectCount = counter.count(); | |
| 50 | |
| 51 double ratioBefore = std::max(1.0, static_cast<double>(contentsHeightBeforeL ayout) / visibleHeight); | |
|
Charlie Harrison
2016/08/09 16:27:32
Can visibleHeight ever be 0? If not can you add a
Kunihiko Sakamoto
2016/08/10 02:33:31
Good point, actually it can be 0.
Added an early r
| |
| 52 double ratioAfter = std::max(1.0, static_cast<double>(contentsHeightAfterLay out) / visibleHeight); | |
| 53 double significance = delta / ((ratioBefore + ratioAfter) / 2); | |
| 54 | |
| 55 int approximateBlankCharacterCount = FontFaceSet::approximateBlankCharacterC ount(*document()); | |
| 56 if (approximateBlankCharacterCount > kBlankCharactersThreshold) { | |
| 57 m_accumulatedSignificanceWhileHavingBlankText += significance; | |
| 58 } else { | |
| 59 significance += m_accumulatedSignificanceWhileHavingBlankText; | |
| 60 m_accumulatedSignificanceWhileHavingBlankText = 0; | |
| 61 if (significance > m_maxSignificanceSoFar) { | |
| 62 m_state = NextPaintIsMeaningful; | |
| 63 m_maxSignificanceSoFar = significance; | |
| 64 } | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 void FirstMeaningfulPaintDetector::notifyPaint() | |
| 69 { | |
| 70 if (m_state != NextPaintIsMeaningful) | |
| 71 return; | |
| 72 | |
| 73 // Skip document background-only paints. | |
| 74 if (m_paintTiming->firstPaint() == 0.0) | |
| 75 return; | |
| 76 | |
| 77 m_provisionalFirstMeaningfulPaint = monotonicallyIncreasingTime(); | |
| 78 m_state = NextPaintIsNotMeaningful; | |
| 79 } | |
| 80 | |
| 81 void FirstMeaningfulPaintDetector::checkNetworkStable() | |
| 82 { | |
| 83 if (m_state == Reported || !document() || document()->fetcher()->hasPendingR equest()) | |
|
kouhei (in TOK)
2016/08/10 01:58:13
Move !document() -> DCHECK(document());
Kunihiko Sakamoto
2016/08/10 02:33:32
Done.
| |
| 84 return; | |
| 85 | |
| 86 if (m_networkStableTimer.isActive()) | |
|
kouhei (in TOK)
2016/08/10 01:58:13
if (!m_networkStableTimer.isActive())
m_networ
Kunihiko Sakamoto
2016/08/10 02:33:31
We need to update the timer to fire after 2 secs a
| |
| 87 m_networkStableTimer.stop(); | |
| 88 m_networkStableTimer.startOneShot(kTimeWithoutNetworkActivity, BLINK_FROM_HE RE); | |
| 89 } | |
| 90 | |
| 91 void FirstMeaningfulPaintDetector::networkStableTimerFired(TimerBase*) | |
| 92 { | |
| 93 if (m_state == Reported || !document() || document()->fetcher()->hasPendingR equest()) | |
| 94 return; | |
| 95 | |
| 96 if (m_provisionalFirstMeaningfulPaint) | |
| 97 m_paintTiming->setFirstMeaningfulPaint(m_provisionalFirstMeaningfulPaint ); | |
| 98 m_state = Reported; | |
| 99 } | |
| 100 | |
| 101 DEFINE_TRACE(FirstMeaningfulPaintDetector) | |
| 102 { | |
| 103 visitor->trace(m_paintTiming); | |
| 104 } | |
| 105 | |
| 106 } // namespace blink | |
| OLD | NEW |