| 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/dom/Document.h" |
| 9 #include "core/frame/UseCounter.h" | 9 #include "core/frame/UseCounter.h" |
| 10 #include "platform/Histogram.h" | 10 #include "platform/Histogram.h" |
| 11 | 11 |
| 12 namespace blink { | 12 namespace blink { |
| 13 | 13 |
| 14 CSSLazyParsingState::CSSLazyParsingState(const CSSParserContext& context, | 14 CSSLazyParsingState::CSSLazyParsingState(const CSSParserContext* context, |
| 15 Vector<String> escapedStrings, | 15 Vector<String> escapedStrings, |
| 16 const String& sheetText, | 16 const String& sheetText, |
| 17 StyleSheetContents* contents) | 17 StyleSheetContents* contents) |
| 18 : m_context(context), | 18 : m_context(context), |
| 19 m_escapedStrings(std::move(escapedStrings)), | 19 m_escapedStrings(std::move(escapedStrings)), |
| 20 m_sheetText(sheetText), | 20 m_sheetText(sheetText), |
| 21 m_owningContents(contents), | 21 m_owningContents(contents), |
| 22 m_parsedStyleRules(0), | 22 m_parsedStyleRules(0), |
| 23 m_totalStyleRules(0), | 23 m_totalStyleRules(0), |
| 24 m_styleRulesNeededForNextMilestone(0), | 24 m_styleRulesNeededForNextMilestone(0), |
| 25 m_usage(UsageGe0), | 25 m_usage(UsageGe0), |
| 26 m_shouldUseCount(!!m_context.useCounter()) { | 26 m_shouldUseCount(m_context->isUseCounterRecordingEnabled()) { |
| 27 recordUsageMetrics(); | 27 recordUsageMetrics(); |
| 28 } | 28 } |
| 29 | 29 |
| 30 CSSLazyPropertyParserImpl* CSSLazyParsingState::createLazyParser( | 30 CSSLazyPropertyParserImpl* CSSLazyParsingState::createLazyParser( |
| 31 const CSSParserTokenRange& block) { | 31 const CSSParserTokenRange& block) { |
| 32 ++m_totalStyleRules; | 32 ++m_totalStyleRules; |
| 33 return new CSSLazyPropertyParserImpl(std::move(block), this); | 33 return new CSSLazyPropertyParserImpl(std::move(block), this); |
| 34 } | 34 } |
| 35 | 35 |
| 36 const CSSParserContext& CSSLazyParsingState::context() { | 36 const CSSParserContext* CSSLazyParsingState::context() { |
| 37 DCHECK(m_owningContents); | 37 DCHECK(m_owningContents); |
| 38 if (!m_shouldUseCount) { | 38 if (!m_shouldUseCount) { |
| 39 DCHECK(!m_context.useCounter()); | 39 DCHECK(!m_context->isUseCounterRecordingEnabled()); |
| 40 return m_context; | 40 return m_context; |
| 41 } | 41 } |
| 42 | 42 |
| 43 // Try as best as possible to grab a valid UseCounter if the underlying | 43 // Try as best as possible to grab a valid Document if the old Document has |
| 44 // document has gone away. | 44 // gone away so we can still use UseCounter. |
| 45 if (!m_document) | 45 if (!m_document) |
| 46 m_document = m_owningContents->anyOwnerDocument(); | 46 m_document = m_owningContents->anyOwnerDocument(); |
| 47 | 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); | 48 UseCounter* useCounter = UseCounter::getFrom(m_document); |
| 51 if (useCounter != m_context.useCounter()) | 49 if (useCounter != m_context->useCounter()) |
| 52 m_context = CSSParserContext(m_context, useCounter); | 50 m_context = CSSParserContext::create(m_context, useCounter); |
| 53 return m_context; | 51 return m_context; |
| 54 } | 52 } |
| 55 | 53 |
| 56 void CSSLazyParsingState::countRuleParsed() { | 54 void CSSLazyParsingState::countRuleParsed() { |
| 57 ++m_parsedStyleRules; | 55 ++m_parsedStyleRules; |
| 58 while (m_parsedStyleRules > m_styleRulesNeededForNextMilestone) { | 56 while (m_parsedStyleRules > m_styleRulesNeededForNextMilestone) { |
| 59 DCHECK_NE(UsageAll, m_usage); | 57 DCHECK_NE(UsageAll, m_usage); |
| 60 ++m_usage; | 58 ++m_usage; |
| 61 recordUsageMetrics(); | 59 recordUsageMetrics(); |
| 62 } | 60 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 m_styleRulesNeededForNextMilestone = m_totalStyleRules; | 114 m_styleRulesNeededForNextMilestone = m_totalStyleRules; |
| 117 break; | 115 break; |
| 118 } | 116 } |
| 119 | 117 |
| 120 usageHistogram.count(m_usage); | 118 usageHistogram.count(m_usage); |
| 121 } | 119 } |
| 122 | 120 |
| 123 DEFINE_TRACE(CSSLazyParsingState) { | 121 DEFINE_TRACE(CSSLazyParsingState) { |
| 124 visitor->trace(m_owningContents); | 122 visitor->trace(m_owningContents); |
| 125 visitor->trace(m_document); | 123 visitor->trace(m_document); |
| 124 visitor->trace(m_context); |
| 126 } | 125 } |
| 127 | 126 |
| 128 } // namespace blink | 127 } // namespace blink |
| OLD | NEW |