Chromium Code Reviews| 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 817 } | 817 } |
| 818 } | 818 } |
| 819 | 819 |
| 820 for (CSSStyleSheet* cssStyleSheet : addedSheets) { | 820 for (CSSStyleSheet* cssStyleSheet : addedSheets) { |
| 821 InspectorStyleSheet* newStyleSheet = bindStyleSheet(cssStyleSheet); | 821 InspectorStyleSheet* newStyleSheet = bindStyleSheet(cssStyleSheet); |
| 822 documentCSSStyleSheets->insert(cssStyleSheet); | 822 documentCSSStyleSheets->insert(cssStyleSheet); |
| 823 if (frontend()) { | 823 if (frontend()) { |
| 824 frontend()->styleSheetAdded( | 824 frontend()->styleSheetAdded( |
| 825 newStyleSheet->buildObjectForStyleSheetInfo()); | 825 newStyleSheet->buildObjectForStyleSheetInfo()); |
| 826 } | 826 } |
| 827 if (m_tracker && m_hadFirstCoverageRequest) | |
| 828 m_coveragePendingStylsheets.push_back(newStyleSheet); | |
| 827 } | 829 } |
| 828 | 830 |
| 829 if (documentCSSStyleSheets->isEmpty()) | 831 if (documentCSSStyleSheets->isEmpty()) |
| 830 m_documentToCSSStyleSheets.remove(document); | 832 m_documentToCSSStyleSheets.remove(document); |
| 831 } | 833 } |
| 832 | 834 |
| 833 void InspectorCSSAgent::documentDetached(Document* document) { | 835 void InspectorCSSAgent::documentDetached(Document* document) { |
| 834 m_invalidatedDocuments.erase(document); | 836 m_invalidatedDocuments.erase(document); |
| 835 setActiveStyleSheets(document, HeapVector<Member<CSSStyleSheet>>()); | 837 setActiveStyleSheets(document, HeapVector<Member<CSSStyleSheet>>()); |
| 836 } | 838 } |
| (...skipping 1569 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2406 .build()); | 2408 .build()); |
| 2407 } | 2409 } |
| 2408 layoutTreeNode->setInlineTextNodes(std::move(inlineTextNodes)); | 2410 layoutTreeNode->setInlineTextNodes(std::move(inlineTextNodes)); |
| 2409 } | 2411 } |
| 2410 } | 2412 } |
| 2411 | 2413 |
| 2412 layoutTreeNodes.addItem(std::move(layoutTreeNode)); | 2414 layoutTreeNodes.addItem(std::move(layoutTreeNode)); |
| 2413 } | 2415 } |
| 2414 } | 2416 } |
| 2415 | 2417 |
| 2416 void InspectorCSSAgent::setUsageTrackerStatus(bool enabled) { | 2418 void InspectorCSSAgent::setCoverageEnabled(bool enabled) { |
| 2419 if (enabled == !!m_tracker) | |
| 2420 return; | |
| 2417 if (enabled) { | 2421 if (enabled) { |
| 2418 if (!m_tracker) | 2422 m_tracker = new StyleRuleUsageTracker(); |
| 2419 m_tracker = new StyleRuleUsageTracker(); | 2423 m_hadFirstCoverageRequest = false; |
| 2420 } else { | 2424 } else { |
| 2421 m_tracker = nullptr; | 2425 m_tracker = nullptr; |
| 2426 m_coveragePendingStylsheets.clear(); | |
| 2427 m_unusedStyleRuleToCSSStyleRule.clear(); | |
| 2422 } | 2428 } |
| 2423 | 2429 |
| 2424 HeapVector<Member<Document>> documents = m_domAgent->documents(); | 2430 for (Document* document : m_domAgent->documents()) |
| 2425 for (Document* document : documents) { | |
| 2426 document->styleEngine().setRuleUsageTracker(m_tracker); | 2431 document->styleEngine().setRuleUsageTracker(m_tracker); |
| 2427 | |
| 2428 document->setNeedsStyleRecalc( | |
| 2429 SubtreeStyleChange, | |
| 2430 StyleChangeReasonForTracing::create(StyleChangeReason::Inspector)); | |
| 2431 } | |
| 2432 } | 2432 } |
| 2433 | 2433 |
| 2434 Response InspectorCSSAgent::startRuleUsageTracking() { | 2434 Response InspectorCSSAgent::startRuleUsageTracking() { |
| 2435 m_state->setBoolean(CSSAgentState::ruleRecordingEnabled, true); | 2435 m_state->setBoolean(CSSAgentState::ruleRecordingEnabled, true); |
| 2436 setUsageTrackerStatus(true); | 2436 setCoverageEnabled(true); |
| 2437 return Response::OK(); | 2437 return Response::OK(); |
| 2438 } | 2438 } |
| 2439 | 2439 |
| 2440 std::unique_ptr<protocol::CSS::RuleUsage> | |
| 2441 InspectorCSSAgent::buildObjectForRuleUsage(CSSStyleRule* rule, bool used) { | |
| 2442 InspectorStyleSheet* inspectorStyleSheet = inspectorStyleSheetForRule(rule); | |
| 2443 if (!inspectorStyleSheet) | |
| 2444 return nullptr; | |
| 2445 | |
| 2446 std::unique_ptr<protocol::CSS::RuleUsage> result = | |
| 2447 inspectorStyleSheet->buildObjectForRuleUsage(rule, used); | |
| 2448 | |
| 2449 return result; | |
| 2450 } | |
| 2451 | |
| 2452 Response InspectorCSSAgent::stopRuleUsageTracking( | 2440 Response InspectorCSSAgent::stopRuleUsageTracking( |
| 2453 std::unique_ptr<protocol::Array<protocol::CSS::RuleUsage>>* result) { | 2441 std::unique_ptr<protocol::Array<protocol::CSS::RuleUsage>>* result) { |
| 2454 if (!m_tracker) { | 2442 Response response = takeCoverageDelta(result); |
| 2455 return Response::Error("CSS rule usage tracking is not enabled"); | 2443 setCoverageEnabled(false); |
| 2456 } | 2444 return response; |
| 2445 } | |
| 2457 | 2446 |
| 2458 *result = protocol::Array<protocol::CSS::RuleUsage>::create(); | 2447 void InspectorCSSAgent::collectStyleSheetsForCoverage() { |
| 2448 DCHECK_EQ(0UL, m_coveragePendingStylsheets.size()); | |
| 2459 | 2449 |
| 2460 HeapVector<Member<Document>> documents = m_domAgent->documents(); | 2450 HeapVector<Member<Document>> documents = m_domAgent->documents(); |
| 2461 for (Document* document : documents) { | 2451 for (Document* document : documents) { |
| 2462 HeapHashSet<Member<CSSStyleSheet>>* newSheetsVector = | 2452 document->setNeedsStyleRecalc( |
| 2463 m_documentToCSSStyleSheets.at(document); | 2453 SubtreeStyleChange, |
| 2454 StyleChangeReasonForTracing::create(StyleChangeReason::Inspector)); | |
| 2455 document->updateStyleAndLayoutTree(); | |
| 2456 } | |
| 2457 m_coveragePendingStylsheets.appendRange( | |
| 2458 m_idToInspectorStyleSheet.values().begin(), | |
| 2459 m_idToInspectorStyleSheet.values().end()); | |
| 2460 } | |
| 2464 | 2461 |
| 2465 if (!newSheetsVector) | 2462 Response InspectorCSSAgent::takeCoverageDelta( |
| 2466 continue; | 2463 std::unique_ptr<protocol::Array<protocol::CSS::RuleUsage>>* result) { |
| 2464 if (!m_tracker) | |
| 2465 return Response::Error("CSS rule usage tracking is not enabled"); | |
| 2467 | 2466 |
| 2468 for (auto sheet : *newSheetsVector) { | 2467 if (!m_hadFirstCoverageRequest) { |
| 2469 InspectorStyleSheet* styleSheet = | 2468 m_hadFirstCoverageRequest = true; |
| 2470 m_cssStyleSheetToInspectorStyleSheet.at(sheet); | 2469 collectStyleSheetsForCoverage(); |
| 2471 const CSSRuleVector ruleVector = styleSheet->flatRules(); | 2470 } |
| 2472 for (auto rule : ruleVector) { | |
| 2473 if (rule->type() != CSSRule::kStyleRule) | |
| 2474 continue; | |
| 2475 | 2471 |
| 2476 CSSStyleRule* cssRule = static_cast<CSSStyleRule*>(rule.get()); | 2472 HeapVector<Member<StyleRule>> coverageDeltaVector = m_tracker->takeDelta(); |
| 2473 HeapHashSet<Member<StyleRule>> coverageDelta; | |
| 2474 for (auto item : coverageDeltaVector) | |
| 2475 coverageDelta.insert(item); | |
| 2477 | 2476 |
| 2478 StyleRule* styleRule = cssRule->styleRule(); | 2477 *result = protocol::Array<protocol::CSS::RuleUsage>::create(); |
| 2478 for (auto styleSheet : m_coveragePendingStylsheets) { | |
| 2479 const CSSRuleVector& ruleVector = styleSheet->flatRules(); | |
| 2480 for (auto rule : ruleVector) { | |
| 2481 if (rule->type() != CSSRule::kStyleRule) | |
| 2482 continue; | |
| 2479 | 2483 |
| 2480 std::unique_ptr<protocol::CSS::RuleUsage> protocolRule = | 2484 StyleRule* styleRule = |
| 2481 buildObjectForRuleUsage(cssRule, m_tracker->contains(styleRule)); | 2485 static_cast<CSSStyleRule*>(rule.get())->styleRule(); |
| 2482 if (!protocolRule) | 2486 bool used = !!coverageDelta.take(styleRule); |
|
alph
2017/03/20 20:48:31
contains
| |
| 2483 continue; | 2487 std::unique_ptr<protocol::CSS::RuleUsage> ruleUsageObject = |
| 2488 styleSheet->buildObjectForRuleUsage(rule, used); | |
| 2484 | 2489 |
| 2485 result->get()->addItem(std::move(protocolRule)); | 2490 if (ruleUsageObject) |
| 2486 } | 2491 (*result)->addItem(std::move(ruleUsageObject)); |
| 2492 if (!used) | |
| 2493 m_unusedStyleRuleToCSSStyleRule.set(styleRule, asCSSStyleRule(rule)); | |
| 2487 } | 2494 } |
| 2488 } | 2495 } |
| 2496 m_coveragePendingStylsheets.clear(); | |
| 2489 | 2497 |
| 2490 setUsageTrackerStatus(false); | 2498 for (Member<StyleRule> rule : coverageDelta) { |
| 2499 HeapHashMap<Member<const StyleRule>, Member<CSSStyleRule>>::iterator it = | |
|
alph
2017/03/20 20:48:31
nit: auto it =
| |
| 2500 m_unusedStyleRuleToCSSStyleRule.find(rule); | |
| 2501 if (it == m_unusedStyleRuleToCSSStyleRule.end()) | |
| 2502 continue; | |
| 2503 InspectorStyleSheet* styleSheet = inspectorStyleSheetForRule(it->value); | |
| 2504 if (!styleSheet) | |
| 2505 continue; | |
| 2506 std::unique_ptr<protocol::CSS::RuleUsage> ruleUsageObject = | |
| 2507 styleSheet->buildObjectForRuleUsage(it->value, true); | |
| 2508 if (ruleUsageObject) | |
| 2509 (*result)->addItem(std::move(ruleUsageObject)); | |
| 2510 m_unusedStyleRuleToCSSStyleRule.remove(it); | |
| 2511 } | |
| 2491 | 2512 |
| 2492 return Response::OK(); | 2513 return Response::OK(); |
| 2493 } | 2514 } |
| 2494 | 2515 |
| 2495 DEFINE_TRACE(InspectorCSSAgent) { | 2516 DEFINE_TRACE(InspectorCSSAgent) { |
| 2496 visitor->trace(m_domAgent); | 2517 visitor->trace(m_domAgent); |
| 2497 visitor->trace(m_inspectedFrames); | 2518 visitor->trace(m_inspectedFrames); |
| 2498 visitor->trace(m_networkAgent); | 2519 visitor->trace(m_networkAgent); |
| 2499 visitor->trace(m_resourceContentLoader); | 2520 visitor->trace(m_resourceContentLoader); |
| 2500 visitor->trace(m_resourceContainer); | 2521 visitor->trace(m_resourceContainer); |
| 2501 visitor->trace(m_idToInspectorStyleSheet); | 2522 visitor->trace(m_idToInspectorStyleSheet); |
| 2502 visitor->trace(m_idToInspectorStyleSheetForInlineStyle); | 2523 visitor->trace(m_idToInspectorStyleSheetForInlineStyle); |
| 2503 visitor->trace(m_cssStyleSheetToInspectorStyleSheet); | 2524 visitor->trace(m_cssStyleSheetToInspectorStyleSheet); |
| 2504 visitor->trace(m_documentToCSSStyleSheets); | 2525 visitor->trace(m_documentToCSSStyleSheets); |
| 2505 visitor->trace(m_invalidatedDocuments); | 2526 visitor->trace(m_invalidatedDocuments); |
| 2506 visitor->trace(m_nodeToInspectorStyleSheet); | 2527 visitor->trace(m_nodeToInspectorStyleSheet); |
| 2507 visitor->trace(m_inspectorUserAgentStyleSheet); | 2528 visitor->trace(m_inspectorUserAgentStyleSheet); |
| 2508 visitor->trace(m_tracker); | 2529 visitor->trace(m_tracker); |
| 2530 visitor->trace(m_unusedStyleRuleToCSSStyleRule); | |
| 2531 visitor->trace(m_coveragePendingStylsheets); | |
| 2509 InspectorBaseAgent::trace(visitor); | 2532 InspectorBaseAgent::trace(visitor); |
| 2510 } | 2533 } |
| 2511 | 2534 |
| 2512 } // namespace blink | 2535 } // namespace blink |
| OLD | NEW |