Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(442)

Unified Diff: third_party/WebKit/Source/core/inspector/InspectorCSSAgent.cpp

Issue 2759703003: DevTools: add support for polling for coverage data in CSS agent (Closed)
Patch Set: moved ranges sorting so it also works for CSS ranges Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/inspector/InspectorCSSAgent.cpp
diff --git a/third_party/WebKit/Source/core/inspector/InspectorCSSAgent.cpp b/third_party/WebKit/Source/core/inspector/InspectorCSSAgent.cpp
index 655a092e4b9f40c5dd5a4012fee0c83c5badb6d0..b0c68eb5a607796e2269f86b3a422b92957b8f76 100644
--- a/third_party/WebKit/Source/core/inspector/InspectorCSSAgent.cpp
+++ b/third_party/WebKit/Source/core/inspector/InspectorCSSAgent.cpp
@@ -690,7 +690,7 @@ void InspectorCSSAgent::restore() {
if (m_state->booleanProperty(CSSAgentState::cssAgentEnabled, false))
wasEnabled();
if (m_state->booleanProperty(CSSAgentState::ruleRecordingEnabled, false))
- setUsageTrackerStatus(true);
+ setCoverageEnabled(true);
}
void InspectorCSSAgent::flushPendingProtocolNotifications() {
@@ -755,7 +755,7 @@ Response InspectorCSSAgent::disable() {
m_state->setBoolean(CSSAgentState::cssAgentEnabled, false);
m_resourceContentLoader->cancel(m_resourceContentLoaderClientId);
m_state->setBoolean(CSSAgentState::ruleRecordingEnabled, false);
- setUsageTrackerStatus(false);
+ setCoverageEnabled(false);
return Response::OK();
}
@@ -2414,82 +2414,70 @@ void InspectorCSSAgent::visitLayoutTreeNodes(
}
}
-void InspectorCSSAgent::setUsageTrackerStatus(bool enabled) {
- if (enabled) {
- if (!m_tracker)
- m_tracker = new StyleRuleUsageTracker();
- } else {
- m_tracker = nullptr;
- }
+void InspectorCSSAgent::setCoverageEnabled(bool enabled) {
+ if (enabled == !!m_tracker)
+ return;
+ m_tracker = enabled ? new StyleRuleUsageTracker() : nullptr;
- HeapVector<Member<Document>> documents = m_domAgent->documents();
- for (Document* document : documents) {
+ for (Document* document : m_domAgent->documents())
document->styleEngine().setRuleUsageTracker(m_tracker);
+}
+
+Response InspectorCSSAgent::startRuleUsageTracking() {
+ m_state->setBoolean(CSSAgentState::ruleRecordingEnabled, true);
+ setCoverageEnabled(true);
+ for (Document* document : m_domAgent->documents()) {
document->setNeedsStyleRecalc(
SubtreeStyleChange,
StyleChangeReasonForTracing::create(StyleChangeReason::Inspector));
+ document->updateStyleAndLayoutTree();
}
-}
-Response InspectorCSSAgent::startRuleUsageTracking() {
- m_state->setBoolean(CSSAgentState::ruleRecordingEnabled, true);
- setUsageTrackerStatus(true);
return Response::OK();
}
-std::unique_ptr<protocol::CSS::RuleUsage>
-InspectorCSSAgent::buildObjectForRuleUsage(CSSStyleRule* rule, bool used) {
- InspectorStyleSheet* inspectorStyleSheet = inspectorStyleSheetForRule(rule);
- if (!inspectorStyleSheet)
- return nullptr;
-
- std::unique_ptr<protocol::CSS::RuleUsage> result =
- inspectorStyleSheet->buildObjectForRuleUsage(rule, used);
-
- return result;
+Response InspectorCSSAgent::stopRuleUsageTracking(
+ std::unique_ptr<protocol::Array<protocol::CSS::RuleUsage>>* result) {
+ Response response = takeCoverageDelta(result);
+ setCoverageEnabled(false);
+ return response;
}
-Response InspectorCSSAgent::stopRuleUsageTracking(
+Response InspectorCSSAgent::takeCoverageDelta(
std::unique_ptr<protocol::Array<protocol::CSS::RuleUsage>>* result) {
- if (!m_tracker) {
+ if (!m_tracker)
return Response::Error("CSS rule usage tracking is not enabled");
- }
- *result = protocol::Array<protocol::CSS::RuleUsage>::create();
+ StyleRuleUsageTracker::RuleListByStyleSheet coverageDelta =
+ m_tracker->takeDelta();
- HeapVector<Member<Document>> documents = m_domAgent->documents();
- for (Document* document : documents) {
- HeapHashSet<Member<CSSStyleSheet>>* newSheetsVector =
- m_documentToCSSStyleSheets.at(document);
+ *result = protocol::Array<protocol::CSS::RuleUsage>::create();
- if (!newSheetsVector)
+ for (const auto& entry : coverageDelta) {
+ const CSSStyleSheet* cssStyleSheet = entry.key.get();
+ InspectorStyleSheet* styleSheet = m_cssStyleSheetToInspectorStyleSheet.at(
+ const_cast<CSSStyleSheet*>(cssStyleSheet));
+ if (!styleSheet)
continue;
- for (auto sheet : *newSheetsVector) {
- InspectorStyleSheet* styleSheet =
- m_cssStyleSheetToInspectorStyleSheet.at(sheet);
- const CSSRuleVector ruleVector = styleSheet->flatRules();
- for (auto rule : ruleVector) {
- if (rule->type() != CSSRule::kStyleRule)
- continue;
-
- CSSStyleRule* cssRule = static_cast<CSSStyleRule*>(rule.get());
-
- StyleRule* styleRule = cssRule->styleRule();
-
- std::unique_ptr<protocol::CSS::RuleUsage> protocolRule =
- buildObjectForRuleUsage(cssRule, m_tracker->contains(styleRule));
- if (!protocolRule)
- continue;
-
- result->get()->addItem(std::move(protocolRule));
+ HeapHashMap<Member<const StyleRule>, Member<CSSStyleRule>> ruleToCSSRule;
+ const CSSRuleVector& cssRules = styleSheet->flatRules();
+ for (auto cssRule : cssRules) {
+ if (cssRule->type() != CSSRule::kStyleRule)
+ continue;
+ CSSStyleRule* cssStyleRule = asCSSStyleRule(cssRule);
+ ruleToCSSRule.set(cssStyleRule->styleRule(), cssStyleRule);
+ }
+ for (auto usedRule : entry.value) {
+ CSSStyleRule* cssStyleRule = ruleToCSSRule.at(usedRule);
+ if (std::unique_ptr<protocol::CSS::RuleUsage> ruleUsageObject =
+ styleSheet->buildObjectForRuleUsage(cssStyleRule, true)) {
+ (*result)->addItem(std::move(ruleUsageObject));
}
}
}
- setUsageTrackerStatus(false);
-
return Response::OK();
}

Powered by Google App Engine
This is Rietveld 408576698