OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010, Google Inc. All rights reserved. | 2 * Copyright (C) 2010, Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 672 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
683 m_resourceContainer(resourceContainer), | 683 m_resourceContainer(resourceContainer), |
684 m_resourceContentLoaderClientId(resourceContentLoader->createClientId()) { | 684 m_resourceContentLoaderClientId(resourceContentLoader->createClientId()) { |
685 } | 685 } |
686 | 686 |
687 InspectorCSSAgent::~InspectorCSSAgent() {} | 687 InspectorCSSAgent::~InspectorCSSAgent() {} |
688 | 688 |
689 void InspectorCSSAgent::restore() { | 689 void InspectorCSSAgent::restore() { |
690 if (m_state->booleanProperty(CSSAgentState::cssAgentEnabled, false)) | 690 if (m_state->booleanProperty(CSSAgentState::cssAgentEnabled, false)) |
691 wasEnabled(); | 691 wasEnabled(); |
692 if (m_state->booleanProperty(CSSAgentState::ruleRecordingEnabled, false)) | 692 if (m_state->booleanProperty(CSSAgentState::ruleRecordingEnabled, false)) |
693 setUsageTrackerStatus(true); | 693 setCoverageEnabled(true); |
694 } | 694 } |
695 | 695 |
696 void InspectorCSSAgent::flushPendingProtocolNotifications() { | 696 void InspectorCSSAgent::flushPendingProtocolNotifications() { |
697 if (!m_invalidatedDocuments.size()) | 697 if (!m_invalidatedDocuments.size()) |
698 return; | 698 return; |
699 HeapHashSet<Member<Document>> invalidatedDocuments; | 699 HeapHashSet<Member<Document>> invalidatedDocuments; |
700 m_invalidatedDocuments.swap(invalidatedDocuments); | 700 m_invalidatedDocuments.swap(invalidatedDocuments); |
701 for (Document* document : invalidatedDocuments) | 701 for (Document* document : invalidatedDocuments) |
702 updateActiveStyleSheets(document); | 702 updateActiveStyleSheets(document); |
703 } | 703 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
748 updateActiveStyleSheets(document); | 748 updateActiveStyleSheets(document); |
749 } | 749 } |
750 | 750 |
751 Response InspectorCSSAgent::disable() { | 751 Response InspectorCSSAgent::disable() { |
752 reset(); | 752 reset(); |
753 m_domAgent->setDOMListener(nullptr); | 753 m_domAgent->setDOMListener(nullptr); |
754 m_instrumentingAgents->removeInspectorCSSAgent(this); | 754 m_instrumentingAgents->removeInspectorCSSAgent(this); |
755 m_state->setBoolean(CSSAgentState::cssAgentEnabled, false); | 755 m_state->setBoolean(CSSAgentState::cssAgentEnabled, false); |
756 m_resourceContentLoader->cancel(m_resourceContentLoaderClientId); | 756 m_resourceContentLoader->cancel(m_resourceContentLoaderClientId); |
757 m_state->setBoolean(CSSAgentState::ruleRecordingEnabled, false); | 757 m_state->setBoolean(CSSAgentState::ruleRecordingEnabled, false); |
758 setUsageTrackerStatus(false); | 758 setCoverageEnabled(false); |
759 return Response::OK(); | 759 return Response::OK(); |
760 } | 760 } |
761 | 761 |
762 void InspectorCSSAgent::didCommitLoadForLocalFrame(LocalFrame* frame) { | 762 void InspectorCSSAgent::didCommitLoadForLocalFrame(LocalFrame* frame) { |
763 if (frame == m_inspectedFrames->root()) | 763 if (frame == m_inspectedFrames->root()) |
764 reset(); | 764 reset(); |
765 } | 765 } |
766 | 766 |
767 void InspectorCSSAgent::mediaQueryResultChanged() { | 767 void InspectorCSSAgent::mediaQueryResultChanged() { |
768 flushPendingProtocolNotifications(); | 768 flushPendingProtocolNotifications(); |
(...skipping 1638 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2407 .build()); | 2407 .build()); |
2408 } | 2408 } |
2409 layoutTreeNode->setInlineTextNodes(std::move(inlineTextNodes)); | 2409 layoutTreeNode->setInlineTextNodes(std::move(inlineTextNodes)); |
2410 } | 2410 } |
2411 } | 2411 } |
2412 | 2412 |
2413 layoutTreeNodes.addItem(std::move(layoutTreeNode)); | 2413 layoutTreeNodes.addItem(std::move(layoutTreeNode)); |
2414 } | 2414 } |
2415 } | 2415 } |
2416 | 2416 |
2417 void InspectorCSSAgent::setUsageTrackerStatus(bool enabled) { | 2417 void InspectorCSSAgent::setCoverageEnabled(bool enabled) { |
2418 if (enabled) { | 2418 if (enabled == !!m_tracker) |
2419 if (!m_tracker) | 2419 return; |
2420 m_tracker = new StyleRuleUsageTracker(); | 2420 m_tracker = enabled ? new StyleRuleUsageTracker() : nullptr; |
2421 } else { | |
2422 m_tracker = nullptr; | |
2423 } | |
2424 | 2421 |
2425 HeapVector<Member<Document>> documents = m_domAgent->documents(); | 2422 for (Document* document : m_domAgent->documents()) |
2426 for (Document* document : documents) { | |
2427 document->styleEngine().setRuleUsageTracker(m_tracker); | 2423 document->styleEngine().setRuleUsageTracker(m_tracker); |
2428 | |
2429 document->setNeedsStyleRecalc( | |
2430 SubtreeStyleChange, | |
2431 StyleChangeReasonForTracing::create(StyleChangeReason::Inspector)); | |
2432 } | |
2433 } | 2424 } |
2434 | 2425 |
2435 Response InspectorCSSAgent::startRuleUsageTracking() { | 2426 Response InspectorCSSAgent::startRuleUsageTracking() { |
2436 m_state->setBoolean(CSSAgentState::ruleRecordingEnabled, true); | 2427 m_state->setBoolean(CSSAgentState::ruleRecordingEnabled, true); |
2437 setUsageTrackerStatus(true); | 2428 setCoverageEnabled(true); |
| 2429 |
| 2430 for (Document* document : m_domAgent->documents()) { |
| 2431 document->setNeedsStyleRecalc( |
| 2432 SubtreeStyleChange, |
| 2433 StyleChangeReasonForTracing::create(StyleChangeReason::Inspector)); |
| 2434 document->updateStyleAndLayoutTree(); |
| 2435 } |
| 2436 |
2438 return Response::OK(); | 2437 return Response::OK(); |
2439 } | 2438 } |
2440 | 2439 |
2441 std::unique_ptr<protocol::CSS::RuleUsage> | |
2442 InspectorCSSAgent::buildObjectForRuleUsage(CSSStyleRule* rule, bool used) { | |
2443 InspectorStyleSheet* inspectorStyleSheet = inspectorStyleSheetForRule(rule); | |
2444 if (!inspectorStyleSheet) | |
2445 return nullptr; | |
2446 | |
2447 std::unique_ptr<protocol::CSS::RuleUsage> result = | |
2448 inspectorStyleSheet->buildObjectForRuleUsage(rule, used); | |
2449 | |
2450 return result; | |
2451 } | |
2452 | |
2453 Response InspectorCSSAgent::stopRuleUsageTracking( | 2440 Response InspectorCSSAgent::stopRuleUsageTracking( |
2454 std::unique_ptr<protocol::Array<protocol::CSS::RuleUsage>>* result) { | 2441 std::unique_ptr<protocol::Array<protocol::CSS::RuleUsage>>* result) { |
2455 if (!m_tracker) { | 2442 Response response = takeCoverageDelta(result); |
| 2443 setCoverageEnabled(false); |
| 2444 return response; |
| 2445 } |
| 2446 |
| 2447 Response InspectorCSSAgent::takeCoverageDelta( |
| 2448 std::unique_ptr<protocol::Array<protocol::CSS::RuleUsage>>* result) { |
| 2449 if (!m_tracker) |
2456 return Response::Error("CSS rule usage tracking is not enabled"); | 2450 return Response::Error("CSS rule usage tracking is not enabled"); |
2457 } | 2451 |
| 2452 StyleRuleUsageTracker::RuleListByStyleSheet coverageDelta = |
| 2453 m_tracker->takeDelta(); |
2458 | 2454 |
2459 *result = protocol::Array<protocol::CSS::RuleUsage>::create(); | 2455 *result = protocol::Array<protocol::CSS::RuleUsage>::create(); |
2460 | 2456 |
2461 HeapVector<Member<Document>> documents = m_domAgent->documents(); | 2457 for (const auto& entry : coverageDelta) { |
2462 for (Document* document : documents) { | 2458 const CSSStyleSheet* cssStyleSheet = entry.key.get(); |
2463 HeapHashSet<Member<CSSStyleSheet>>* newSheetsVector = | 2459 InspectorStyleSheet* styleSheet = m_cssStyleSheetToInspectorStyleSheet.at( |
2464 m_documentToCSSStyleSheets.at(document); | 2460 const_cast<CSSStyleSheet*>(cssStyleSheet)); |
2465 | 2461 if (!styleSheet) |
2466 if (!newSheetsVector) | |
2467 continue; | 2462 continue; |
2468 | 2463 |
2469 for (auto sheet : *newSheetsVector) { | 2464 HeapHashMap<Member<const StyleRule>, Member<CSSStyleRule>> ruleToCSSRule; |
2470 InspectorStyleSheet* styleSheet = | 2465 const CSSRuleVector& cssRules = styleSheet->flatRules(); |
2471 m_cssStyleSheetToInspectorStyleSheet.at(sheet); | 2466 for (auto cssRule : cssRules) { |
2472 const CSSRuleVector ruleVector = styleSheet->flatRules(); | 2467 if (cssRule->type() != CSSRule::kStyleRule) |
2473 for (auto rule : ruleVector) { | 2468 continue; |
2474 if (rule->type() != CSSRule::kStyleRule) | 2469 CSSStyleRule* cssStyleRule = asCSSStyleRule(cssRule); |
2475 continue; | 2470 ruleToCSSRule.set(cssStyleRule->styleRule(), cssStyleRule); |
2476 | 2471 } |
2477 CSSStyleRule* cssRule = static_cast<CSSStyleRule*>(rule.get()); | 2472 for (auto usedRule : entry.value) { |
2478 | 2473 CSSStyleRule* cssStyleRule = ruleToCSSRule.at(usedRule); |
2479 StyleRule* styleRule = cssRule->styleRule(); | 2474 if (std::unique_ptr<protocol::CSS::RuleUsage> ruleUsageObject = |
2480 | 2475 styleSheet->buildObjectForRuleUsage(cssStyleRule, true)) { |
2481 std::unique_ptr<protocol::CSS::RuleUsage> protocolRule = | 2476 (*result)->addItem(std::move(ruleUsageObject)); |
2482 buildObjectForRuleUsage(cssRule, m_tracker->contains(styleRule)); | |
2483 if (!protocolRule) | |
2484 continue; | |
2485 | |
2486 result->get()->addItem(std::move(protocolRule)); | |
2487 } | 2477 } |
2488 } | 2478 } |
2489 } | 2479 } |
2490 | 2480 |
2491 setUsageTrackerStatus(false); | |
2492 | |
2493 return Response::OK(); | 2481 return Response::OK(); |
2494 } | 2482 } |
2495 | 2483 |
2496 DEFINE_TRACE(InspectorCSSAgent) { | 2484 DEFINE_TRACE(InspectorCSSAgent) { |
2497 visitor->trace(m_domAgent); | 2485 visitor->trace(m_domAgent); |
2498 visitor->trace(m_inspectedFrames); | 2486 visitor->trace(m_inspectedFrames); |
2499 visitor->trace(m_networkAgent); | 2487 visitor->trace(m_networkAgent); |
2500 visitor->trace(m_resourceContentLoader); | 2488 visitor->trace(m_resourceContentLoader); |
2501 visitor->trace(m_resourceContainer); | 2489 visitor->trace(m_resourceContainer); |
2502 visitor->trace(m_idToInspectorStyleSheet); | 2490 visitor->trace(m_idToInspectorStyleSheet); |
2503 visitor->trace(m_idToInspectorStyleSheetForInlineStyle); | 2491 visitor->trace(m_idToInspectorStyleSheetForInlineStyle); |
2504 visitor->trace(m_cssStyleSheetToInspectorStyleSheet); | 2492 visitor->trace(m_cssStyleSheetToInspectorStyleSheet); |
2505 visitor->trace(m_documentToCSSStyleSheets); | 2493 visitor->trace(m_documentToCSSStyleSheets); |
2506 visitor->trace(m_invalidatedDocuments); | 2494 visitor->trace(m_invalidatedDocuments); |
2507 visitor->trace(m_nodeToInspectorStyleSheet); | 2495 visitor->trace(m_nodeToInspectorStyleSheet); |
2508 visitor->trace(m_inspectorUserAgentStyleSheet); | 2496 visitor->trace(m_inspectorUserAgentStyleSheet); |
2509 visitor->trace(m_tracker); | 2497 visitor->trace(m_tracker); |
2510 InspectorBaseAgent::trace(visitor); | 2498 InspectorBaseAgent::trace(visitor); |
2511 } | 2499 } |
2512 | 2500 |
2513 } // namespace blink | 2501 } // namespace blink |
OLD | NEW |