Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "core/css/parser/CSSLazyParsingState.h" | 5 #include "core/css/parser/CSSLazyParsingState.h" |
| 6 #include "core/css/parser/CSSLazyPropertyParserImpl.h" | 6 #include "core/css/parser/CSSLazyPropertyParserImpl.h" |
| 7 #include "core/css/parser/CSSParserTokenRange.h" | 7 #include "core/css/parser/CSSParserTokenRange.h" |
| 8 #include "core/dom/Document.h" | |
| 8 #include "core/frame/UseCounter.h" | 9 #include "core/frame/UseCounter.h" |
| 9 #include "platform/Histogram.h" | 10 #include "platform/Histogram.h" |
| 10 | 11 |
| 11 namespace blink { | 12 namespace blink { |
| 12 | 13 |
| 13 CSSLazyParsingState::CSSLazyParsingState(const CSSParserContext& context, | 14 CSSLazyParsingState::CSSLazyParsingState(const CSSParserContext& context, |
| 14 Vector<String> escapedStrings, | 15 Vector<String> escapedStrings, |
| 15 const String& sheetText, | 16 const String& sheetText, |
| 16 StyleSheetContents* contents) | 17 StyleSheetContents* contents) |
| 17 : m_context(context), | 18 : m_context(context), |
| 18 m_escapedStrings(std::move(escapedStrings)), | 19 m_escapedStrings(std::move(escapedStrings)), |
| 19 m_sheetText(sheetText), | 20 m_sheetText(sheetText), |
| 20 m_owningContents(contents), | 21 m_owningContents(contents), |
| 21 m_parsedStyleRules(0), | 22 m_parsedStyleRules(0), |
| 22 m_totalStyleRules(0), | 23 m_totalStyleRules(0), |
| 23 m_styleRulesNeededForNextMilestone(0), | 24 m_styleRulesNeededForNextMilestone(0), |
| 24 m_usage(UsageGe0) { | 25 m_usage(UsageGe0), |
| 26 m_shouldUseCount(!!m_context.useCounter()) { | |
| 25 recordUsageMetrics(); | 27 recordUsageMetrics(); |
| 26 } | 28 } |
| 27 | 29 |
| 28 CSSLazyPropertyParserImpl* CSSLazyParsingState::createLazyParser( | 30 CSSLazyPropertyParserImpl* CSSLazyParsingState::createLazyParser( |
| 29 const CSSParserTokenRange& block) { | 31 const CSSParserTokenRange& block) { |
| 30 ++m_totalStyleRules; | 32 ++m_totalStyleRules; |
| 31 return new CSSLazyPropertyParserImpl(std::move(block), this); | 33 return new CSSLazyPropertyParserImpl(std::move(block), this); |
| 32 } | 34 } |
| 33 | 35 |
| 34 const CSSParserContext& CSSLazyParsingState::context() { | 36 const CSSParserContext& CSSLazyParsingState::context() { |
| 35 DCHECK(m_owningContents); | 37 DCHECK(m_owningContents); |
| 36 UseCounter* sheetCounter = UseCounter::getFrom(m_owningContents); | 38 if (!m_shouldUseCount) { |
| 37 if (sheetCounter != m_context.useCounter()) | 39 DCHECK(!m_context.useCounter()); |
| 38 m_context = CSSParserContext(m_context, sheetCounter); | 40 return m_context; |
| 41 } | |
| 42 | |
| 43 // Try as best as possible to grab a valid UseCounter if the underlying | |
| 44 // document has gone away. | |
| 45 if (!m_document) | |
| 46 m_document = m_owningContents->anyOwnerDocument(); | |
| 47 | |
| 48 // Always refresh the UseCounter, as the Document can outlive its | |
| 49 // underlying frame host causing a use-after-free of m_context's counter. | |
| 50 UseCounter* useCounter = UseCounter::getFrom(m_document); | |
|
rune
2016/12/05 22:21:05
If we always retrieve the UseCounter, could we sim
Charlie Harrison
2016/12/05 22:27:59
I don't have the trace now, but I remember seeing
rune
2016/12/05 22:31:53
Acknowledged.
| |
| 51 if (useCounter != m_context.useCounter()) | |
| 52 m_context = CSSParserContext(m_context, useCounter); | |
| 39 return m_context; | 53 return m_context; |
| 40 } | 54 } |
| 41 | 55 |
| 42 void CSSLazyParsingState::countRuleParsed() { | 56 void CSSLazyParsingState::countRuleParsed() { |
| 43 ++m_parsedStyleRules; | 57 ++m_parsedStyleRules; |
| 44 while (m_parsedStyleRules > m_styleRulesNeededForNextMilestone) { | 58 while (m_parsedStyleRules > m_styleRulesNeededForNextMilestone) { |
| 45 DCHECK_NE(UsageAll, m_usage); | 59 DCHECK_NE(UsageAll, m_usage); |
| 46 ++m_usage; | 60 ++m_usage; |
| 47 recordUsageMetrics(); | 61 recordUsageMetrics(); |
| 48 } | 62 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 99 m_styleRulesNeededForNextMilestone = m_totalStyleRules - 1; | 113 m_styleRulesNeededForNextMilestone = m_totalStyleRules - 1; |
| 100 break; | 114 break; |
| 101 case UsageAll: | 115 case UsageAll: |
| 102 m_styleRulesNeededForNextMilestone = m_totalStyleRules; | 116 m_styleRulesNeededForNextMilestone = m_totalStyleRules; |
| 103 break; | 117 break; |
| 104 } | 118 } |
| 105 | 119 |
| 106 usageHistogram.count(m_usage); | 120 usageHistogram.count(m_usage); |
| 107 } | 121 } |
| 108 | 122 |
| 123 DEFINE_TRACE(CSSLazyParsingState) { | |
| 124 visitor->trace(m_owningContents); | |
| 125 visitor->trace(m_document); | |
| 126 } | |
| 127 | |
| 109 } // namespace blink | 128 } // namespace blink |
| OLD | NEW |