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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 #include "core/css/MediaList.h" | 45 #include "core/css/MediaList.h" |
46 #include "core/css/MediaQuery.h" | 46 #include "core/css/MediaQuery.h" |
47 #include "core/css/MediaValues.h" | 47 #include "core/css/MediaValues.h" |
48 #include "core/css/StylePropertySet.h" | 48 #include "core/css/StylePropertySet.h" |
49 #include "core/css/StyleRule.h" | 49 #include "core/css/StyleRule.h" |
50 #include "core/css/StyleSheet.h" | 50 #include "core/css/StyleSheet.h" |
51 #include "core/css/StyleSheetContents.h" | 51 #include "core/css/StyleSheetContents.h" |
52 #include "core/css/StyleSheetList.h" | 52 #include "core/css/StyleSheetList.h" |
53 #include "core/css/parser/CSSParser.h" | 53 #include "core/css/parser/CSSParser.h" |
54 #include "core/css/resolver/StyleResolver.h" | 54 #include "core/css/resolver/StyleResolver.h" |
| 55 #include "core/dom/DOMNodeIds.h" |
55 #include "core/dom/Node.h" | 56 #include "core/dom/Node.h" |
56 #include "core/dom/StyleChangeReason.h" | 57 #include "core/dom/StyleChangeReason.h" |
57 #include "core/dom/StyleEngine.h" | 58 #include "core/dom/StyleEngine.h" |
58 #include "core/dom/Text.h" | 59 #include "core/dom/Text.h" |
| 60 #include "core/dom/shadow/ElementShadow.h" |
59 #include "core/frame/FrameView.h" | 61 #include "core/frame/FrameView.h" |
60 #include "core/frame/LocalFrame.h" | 62 #include "core/frame/LocalFrame.h" |
61 #include "core/html/HTMLFrameOwnerElement.h" | 63 #include "core/html/HTMLFrameOwnerElement.h" |
62 #include "core/html/HTMLHeadElement.h" | 64 #include "core/html/HTMLHeadElement.h" |
63 #include "core/html/VoidCallback.h" | 65 #include "core/html/VoidCallback.h" |
64 #include "core/inspector/IdentifiersFactory.h" | 66 #include "core/inspector/IdentifiersFactory.h" |
65 #include "core/inspector/InspectedFrames.h" | 67 #include "core/inspector/InspectedFrames.h" |
66 #include "core/inspector/InspectorHistory.h" | 68 #include "core/inspector/InspectorHistory.h" |
67 #include "core/inspector/InspectorNetworkAgent.h" | 69 #include "core/inspector/InspectorNetworkAgent.h" |
68 #include "core/inspector/InspectorResourceContainer.h" | 70 #include "core/inspector/InspectorResourceContainer.h" |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 bool contains = foundTopElement || element->boundingBox().contains(rect); | 281 bool contains = foundTopElement || element->boundingBox().contains(rect); |
280 if (!contains && foundNonTransparentColor) { | 282 if (!contains && foundNonTransparentColor) { |
281 // Only return colors if some opaque element covers up this one. | 283 // Only return colors if some opaque element covers up this one. |
282 colors.clear(); | 284 colors.clear(); |
283 foundOpaqueColor = false; | 285 foundOpaqueColor = false; |
284 } | 286 } |
285 } | 287 } |
286 return foundOpaqueColor; | 288 return foundOpaqueColor; |
287 } | 289 } |
288 | 290 |
| 291 std::unique_ptr<protocol::DOM::Rect> buildRectForFloatRect( |
| 292 const FloatRect& rect) { |
| 293 return protocol::DOM::Rect::create() |
| 294 .setX(rect.x()) |
| 295 .setY(rect.y()) |
| 296 .setWidth(rect.width()) |
| 297 .setHeight(rect.height()) |
| 298 .build(); |
| 299 } |
| 300 |
289 } // namespace | 301 } // namespace |
290 | 302 |
291 namespace CSSAgentState { | 303 namespace CSSAgentState { |
292 static const char cssAgentEnabled[] = "cssAgentEnabled"; | 304 static const char cssAgentEnabled[] = "cssAgentEnabled"; |
293 } | 305 } |
294 | 306 |
295 typedef blink::protocol::CSS::Backend::EnableCallback EnableCallback; | 307 typedef blink::protocol::CSS::Backend::EnableCallback EnableCallback; |
296 | 308 |
297 namespace blink { | 309 namespace blink { |
298 | 310 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 WTF_MAKE_NONCOPYABLE(StyleSheetAction); | 345 WTF_MAKE_NONCOPYABLE(StyleSheetAction); |
334 | 346 |
335 public: | 347 public: |
336 StyleSheetAction(const String& name) : InspectorHistory::Action(name) {} | 348 StyleSheetAction(const String& name) : InspectorHistory::Action(name) {} |
337 | 349 |
338 virtual std::unique_ptr<protocol::CSS::CSSStyle> takeSerializedStyle() { | 350 virtual std::unique_ptr<protocol::CSS::CSSStyle> takeSerializedStyle() { |
339 return nullptr; | 351 return nullptr; |
340 } | 352 } |
341 }; | 353 }; |
342 | 354 |
| 355 struct InspectorCSSAgent::VectorStringHashTraits |
| 356 : public WTF::GenericHashTraits<Vector<String>> { |
| 357 static unsigned hash(const Vector<String>& vec) { |
| 358 unsigned h = DefaultHash<String>::Hash::hash(vec[0]); |
| 359 for (size_t i = 1; i < vec.size(); i++) { |
| 360 h = WTF::hashInts(h, DefaultHash<String>::Hash::hash(vec[i])); |
| 361 } |
| 362 return h; |
| 363 } |
| 364 |
| 365 static bool equal(const Vector<String>& a, const Vector<String>& b) { |
| 366 if (a.size() != b.size()) |
| 367 return false; |
| 368 for (size_t i = 0; i < a.size(); i++) { |
| 369 if (a[i] != b[i]) |
| 370 return false; |
| 371 } |
| 372 return true; |
| 373 } |
| 374 |
| 375 static void constructDeletedValue(Vector<String>& vec, bool) { |
| 376 vec.clear(); |
| 377 vec.append(String(WTF::HashTableDeletedValue)); |
| 378 } |
| 379 |
| 380 static bool isDeletedValue(const Vector<String>& vec) { |
| 381 return !vec.isEmpty() && vec[0].isHashTableDeletedValue(); |
| 382 } |
| 383 |
| 384 static bool isEmptyValue(const Vector<String>& vec) { return vec.isEmpty(); } |
| 385 |
| 386 static const bool emptyValueIsZero = false; |
| 387 static const bool safeToCompareToEmptyOrDeleted = false; |
| 388 static const bool hasIsEmptyValueFunction = true; |
| 389 }; |
| 390 |
343 class InspectorCSSAgent::SetStyleSheetTextAction final | 391 class InspectorCSSAgent::SetStyleSheetTextAction final |
344 : public InspectorCSSAgent::StyleSheetAction { | 392 : public InspectorCSSAgent::StyleSheetAction { |
345 WTF_MAKE_NONCOPYABLE(SetStyleSheetTextAction); | 393 WTF_MAKE_NONCOPYABLE(SetStyleSheetTextAction); |
346 | 394 |
347 public: | 395 public: |
348 SetStyleSheetTextAction(InspectorStyleSheetBase* styleSheet, | 396 SetStyleSheetTextAction(InspectorStyleSheetBase* styleSheet, |
349 const String& text) | 397 const String& text) |
350 : InspectorCSSAgent::StyleSheetAction("SetStyleSheetText"), | 398 : InspectorCSSAgent::StyleSheetAction("SetStyleSheetText"), |
351 m_styleSheet(styleSheet), | 399 m_styleSheet(styleSheet), |
352 m_text(text) {} | 400 m_text(text) {} |
(...skipping 1930 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2283 foundOpaqueColor = getColorsFromRect(textBounds, ownerElement->document(), | 2331 foundOpaqueColor = getColorsFromRect(textBounds, ownerElement->document(), |
2284 nullptr, colors); | 2332 nullptr, colors); |
2285 } | 2333 } |
2286 } | 2334 } |
2287 | 2335 |
2288 *result = protocol::Array<String>::create(); | 2336 *result = protocol::Array<String>::create(); |
2289 for (auto color : colors) | 2337 for (auto color : colors) |
2290 result->fromJust()->addItem(color.serializedAsCSSComponentValue()); | 2338 result->fromJust()->addItem(color.serializedAsCSSComponentValue()); |
2291 } | 2339 } |
2292 | 2340 |
| 2341 void InspectorCSSAgent::getLayoutTreeAndStyles( |
| 2342 ErrorString* errorString, |
| 2343 std::unique_ptr<protocol::Array<String>> styleWhitelist, |
| 2344 std::unique_ptr<protocol::Array<protocol::CSS::LayoutTreeNode>>* |
| 2345 layoutTreeNodes, |
| 2346 std::unique_ptr<protocol::Array<protocol::CSS::ComputedStyle>>* |
| 2347 computedStyles) { |
| 2348 m_domAgent->document()->updateStyleAndLayoutTree(); |
| 2349 |
| 2350 // Look up the CSSPropertyIDs for each entry in |styleWhitelist|. |
| 2351 Vector<std::pair<String, CSSPropertyID>> cssPropertyWhitelist; |
| 2352 for (size_t i = 0; i < styleWhitelist->length(); i++) { |
| 2353 CSSPropertyID propertyId = cssPropertyID(styleWhitelist->get(i)); |
| 2354 if (propertyId == CSSPropertyInvalid) |
| 2355 continue; |
| 2356 cssPropertyWhitelist.append( |
| 2357 std::make_pair(styleWhitelist->get(i), propertyId)); |
| 2358 } |
| 2359 |
| 2360 *layoutTreeNodes = protocol::Array<protocol::CSS::LayoutTreeNode>::create(); |
| 2361 *computedStyles = protocol::Array<protocol::CSS::ComputedStyle>::create(); |
| 2362 |
| 2363 ComputedStylesMap styleToIndexMap; |
| 2364 visitLayoutTreeNodes(m_domAgent->document(), *layoutTreeNodes->get(), |
| 2365 cssPropertyWhitelist, styleToIndexMap, |
| 2366 *computedStyles->get()); |
| 2367 } |
| 2368 |
| 2369 int InspectorCSSAgent::getStyleIndexForNode( |
| 2370 Node* node, |
| 2371 const Vector<std::pair<String, CSSPropertyID>>& cssPropertyWhitelist, |
| 2372 ComputedStylesMap& styleToIndexMap, |
| 2373 protocol::Array<protocol::CSS::ComputedStyle>& computedStyles) { |
| 2374 CSSComputedStyleDeclaration* computedStyleInfo = |
| 2375 CSSComputedStyleDeclaration::create(node, true); |
| 2376 |
| 2377 Vector<String> style; |
| 2378 for (const auto& pair : cssPropertyWhitelist) { |
| 2379 style.append(computedStyleInfo->getPropertyValue(pair.second)); |
| 2380 } |
| 2381 |
| 2382 ComputedStylesMap::iterator it = styleToIndexMap.find(style); |
| 2383 if (it != styleToIndexMap.end()) |
| 2384 return it->value; |
| 2385 |
| 2386 // It's a distinct style, so append to |computedStyles|. |
| 2387 std::unique_ptr<protocol::Array<protocol::CSS::CSSComputedStyleProperty>> |
| 2388 styleProperties = |
| 2389 protocol::Array<protocol::CSS::CSSComputedStyleProperty>::create(); |
| 2390 for (size_t i = 0; i < style.size(); i++) { |
| 2391 styleProperties->addItem(protocol::CSS::CSSComputedStyleProperty::create() |
| 2392 .setName(cssPropertyWhitelist[i].first) |
| 2393 .setValue(style[i]) |
| 2394 .build()); |
| 2395 } |
| 2396 computedStyles.addItem(protocol::CSS::ComputedStyle::create() |
| 2397 .setProperties(std::move(styleProperties)) |
| 2398 .build()); |
| 2399 |
| 2400 size_t index = styleToIndexMap.size(); |
| 2401 styleToIndexMap.add(std::move(style), index); |
| 2402 return index; |
| 2403 } |
| 2404 |
| 2405 void InspectorCSSAgent::visitLayoutTreeNodes( |
| 2406 Node* node, |
| 2407 protocol::Array<protocol::CSS::LayoutTreeNode>& layoutTreeNodes, |
| 2408 const Vector<std::pair<String, CSSPropertyID>>& cssPropertyWhitelist, |
| 2409 ComputedStylesMap& styleToIndexMap, |
| 2410 protocol::Array<protocol::CSS::ComputedStyle>& computedStyles) { |
| 2411 for (; node; node = NodeTraversal::next(*node)) { |
| 2412 // Visit shadow dom nodes. |
| 2413 if (node->isElementNode()) { |
| 2414 const Element* element = toElement(node); |
| 2415 ElementShadow* elementShadow = element->shadow(); |
| 2416 if (elementShadow) { |
| 2417 visitLayoutTreeNodes(&elementShadow->youngestShadowRoot(), |
| 2418 layoutTreeNodes, cssPropertyWhitelist, |
| 2419 styleToIndexMap, computedStyles); |
| 2420 } |
| 2421 } |
| 2422 |
| 2423 // Pierce iframe boundaries. |
| 2424 if (node->isFrameOwnerElement()) { |
| 2425 Document* contentDocument = |
| 2426 toHTMLFrameOwnerElement(node)->contentDocument(); |
| 2427 contentDocument->updateStyleAndLayoutTree(); |
| 2428 visitLayoutTreeNodes(contentDocument->documentElement(), layoutTreeNodes, |
| 2429 cssPropertyWhitelist, styleToIndexMap, |
| 2430 computedStyles); |
| 2431 } |
| 2432 |
| 2433 LayoutObject* layoutObject = node->layoutObject(); |
| 2434 if (!layoutObject) |
| 2435 continue; |
| 2436 |
| 2437 int backendNodeId = DOMNodeIds::idForNode(node); |
| 2438 std::unique_ptr<protocol::CSS::LayoutTreeNode> layoutTreeNode = |
| 2439 protocol::CSS::LayoutTreeNode::create() |
| 2440 .setBackendNodeId(backendNodeId) |
| 2441 .setStyleIndex(getStyleIndexForNode( |
| 2442 node, cssPropertyWhitelist, styleToIndexMap, computedStyles)) |
| 2443 .setBoundingBox(buildRectForFloatRect( |
| 2444 node->isElementNode() |
| 2445 ? FloatRect(toElement(node)->boundsInViewport()) |
| 2446 : layoutObject->absoluteBoundingBoxRect())) |
| 2447 .build(); |
| 2448 |
| 2449 if (layoutObject->isText()) { |
| 2450 LayoutText* layoutText = toLayoutText(layoutObject); |
| 2451 layoutTreeNode->setLayoutText(layoutText->text()); |
| 2452 if (layoutText->hasTextBoxes()) { |
| 2453 std::unique_ptr<protocol::Array<protocol::CSS::InlineTextBox>> |
| 2454 inlineTextNodes = |
| 2455 protocol::Array<protocol::CSS::InlineTextBox>::create(); |
| 2456 for (const InlineTextBox* textBox = layoutText->firstTextBox(); textBox; |
| 2457 textBox = textBox->nextTextBox()) { |
| 2458 FloatRect localCoordsTextBoxRect(textBox->calculateBoundaries()); |
| 2459 FloatRect absoluteCoordsTextBoxRect = |
| 2460 layoutObject->localToAbsoluteQuad(localCoordsTextBoxRect) |
| 2461 .boundingBox(); |
| 2462 inlineTextNodes->addItem(protocol::CSS::InlineTextBox::create() |
| 2463 .setStartCharacterIndex(textBox->start()) |
| 2464 .setNumCharacters(textBox->len()) |
| 2465 .setBoundingBox(buildRectForFloatRect( |
| 2466 absoluteCoordsTextBoxRect)) |
| 2467 .build()); |
| 2468 } |
| 2469 layoutTreeNode->setInlineTextNodes(std::move(inlineTextNodes)); |
| 2470 } |
| 2471 } |
| 2472 |
| 2473 layoutTreeNodes.addItem(std::move(layoutTreeNode)); |
| 2474 } |
| 2475 } |
| 2476 |
2293 DEFINE_TRACE(InspectorCSSAgent) { | 2477 DEFINE_TRACE(InspectorCSSAgent) { |
2294 visitor->trace(m_domAgent); | 2478 visitor->trace(m_domAgent); |
2295 visitor->trace(m_inspectedFrames); | 2479 visitor->trace(m_inspectedFrames); |
2296 visitor->trace(m_networkAgent); | 2480 visitor->trace(m_networkAgent); |
2297 visitor->trace(m_resourceContentLoader); | 2481 visitor->trace(m_resourceContentLoader); |
2298 visitor->trace(m_resourceContainer); | 2482 visitor->trace(m_resourceContainer); |
2299 visitor->trace(m_idToInspectorStyleSheet); | 2483 visitor->trace(m_idToInspectorStyleSheet); |
2300 visitor->trace(m_idToInspectorStyleSheetForInlineStyle); | 2484 visitor->trace(m_idToInspectorStyleSheetForInlineStyle); |
2301 visitor->trace(m_cssStyleSheetToInspectorStyleSheet); | 2485 visitor->trace(m_cssStyleSheetToInspectorStyleSheet); |
2302 visitor->trace(m_documentToCSSStyleSheets); | 2486 visitor->trace(m_documentToCSSStyleSheets); |
2303 visitor->trace(m_invalidatedDocuments); | 2487 visitor->trace(m_invalidatedDocuments); |
2304 visitor->trace(m_nodeToInspectorStyleSheet); | 2488 visitor->trace(m_nodeToInspectorStyleSheet); |
2305 visitor->trace(m_inspectorUserAgentStyleSheet); | 2489 visitor->trace(m_inspectorUserAgentStyleSheet); |
2306 InspectorBaseAgent::trace(visitor); | 2490 InspectorBaseAgent::trace(visitor); |
2307 } | 2491 } |
2308 | 2492 |
2309 } // namespace blink | 2493 } // namespace blink |
OLD | NEW |