| 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 "ash/common/devtools/ash_devtools_css_agent.h" | 5 #include "ash/common/devtools/ash_devtools_css_agent.h" |
| 6 | 6 |
| 7 #include "ash/common/wm_window.h" | 7 #include "ash/common/wm_window.h" |
| 8 | 8 |
| 9 namespace ash { | 9 namespace ash { |
| 10 namespace devtools { | 10 namespace devtools { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 std::unique_ptr<Array<CSS::CSSProperty>> BuildBoundsCSSPropertyArray( | 23 std::unique_ptr<Array<CSS::CSSProperty>> BuildBoundsCSSPropertyArray( |
| 24 const gfx::Rect& bounds) { | 24 const gfx::Rect& bounds) { |
| 25 auto cssProperties = Array<CSS::CSSProperty>::create(); | 25 auto cssProperties = Array<CSS::CSSProperty>::create(); |
| 26 cssProperties->addItem(BuildCSSProperty("height", bounds.height())); | 26 cssProperties->addItem(BuildCSSProperty("height", bounds.height())); |
| 27 cssProperties->addItem(BuildCSSProperty("width", bounds.width())); | 27 cssProperties->addItem(BuildCSSProperty("width", bounds.width())); |
| 28 cssProperties->addItem(BuildCSSProperty("x", bounds.x())); | 28 cssProperties->addItem(BuildCSSProperty("x", bounds.x())); |
| 29 cssProperties->addItem(BuildCSSProperty("y", bounds.y())); | 29 cssProperties->addItem(BuildCSSProperty("y", bounds.y())); |
| 30 return cssProperties; | 30 return cssProperties; |
| 31 } | 31 } |
| 32 | 32 |
| 33 std::unique_ptr<CSS::CSSStyle> BuildCSSStyle( | 33 std::unique_ptr<CSS::CSSStyle> BuildCSSStyle(int node_id, |
| 34 std::unique_ptr<Array<CSS::CSSProperty>> cssProperties) { | 34 const gfx::Rect& bounds) { |
| 35 return CSS::CSSStyle::create() | 35 return CSS::CSSStyle::create() |
| 36 .setCssProperties(std::move(cssProperties)) | 36 .setCssProperties(BuildBoundsCSSPropertyArray(bounds)) |
| 37 .setShorthandEntries(Array<std::string>::create()) | 37 .setShorthandEntries(Array<std::string>::create()) |
| 38 .build(); | 38 .build(); |
| 39 } | 39 } |
| 40 | 40 |
| 41 std::unique_ptr<CSS::CSSStyle> BuildCSSStyleForBounds(const gfx::Rect& bounds) { | |
| 42 return BuildCSSStyle(BuildBoundsCSSPropertyArray(bounds)); | |
| 43 } | |
| 44 | |
| 45 std::unique_ptr<CSS::CSSStyle> BuildStyles(WmWindow* window) { | |
| 46 return BuildCSSStyleForBounds(window->GetBounds()); | |
| 47 } | |
| 48 | |
| 49 std::unique_ptr<CSS::CSSStyle> BuildStyles(views::Widget* widget) { | |
| 50 return BuildCSSStyleForBounds(widget->GetWindowBoundsInScreen()); | |
| 51 } | |
| 52 | |
| 53 std::unique_ptr<CSS::CSSStyle> BuildStyles(views::View* view) { | |
| 54 return BuildCSSStyleForBounds(view->bounds()); | |
| 55 } | |
| 56 | |
| 57 } // namespace | 41 } // namespace |
| 58 | 42 |
| 59 AshDevToolsCSSAgent::AshDevToolsCSSAgent(AshDevToolsDOMAgent* dom_agent) | 43 AshDevToolsCSSAgent::AshDevToolsCSSAgent(AshDevToolsDOMAgent* dom_agent) |
| 60 : dom_agent_(dom_agent) { | 44 : dom_agent_(dom_agent) { |
| 61 DCHECK(dom_agent_); | 45 DCHECK(dom_agent_); |
| 62 } | 46 } |
| 63 | 47 |
| 64 AshDevToolsCSSAgent::~AshDevToolsCSSAgent() {} | 48 AshDevToolsCSSAgent::~AshDevToolsCSSAgent() { |
| 49 disable(); |
| 50 } |
| 51 |
| 52 ui::devtools::protocol::Response AshDevToolsCSSAgent::enable() { |
| 53 dom_agent_->AddObserver(this); |
| 54 return ui::devtools::protocol::Response::OK(); |
| 55 } |
| 56 |
| 57 ui::devtools::protocol::Response AshDevToolsCSSAgent::disable() { |
| 58 dom_agent_->RemoveObserver(this); |
| 59 return ui::devtools::protocol::Response::OK(); |
| 60 } |
| 65 | 61 |
| 66 ui::devtools::protocol::Response AshDevToolsCSSAgent::getMatchedStylesForNode( | 62 ui::devtools::protocol::Response AshDevToolsCSSAgent::getMatchedStylesForNode( |
| 67 int node_id, | 63 int node_id, |
| 68 ui::devtools::protocol::Maybe<ui::devtools::protocol::CSS::CSSStyle>* | 64 ui::devtools::protocol::Maybe<ui::devtools::protocol::CSS::CSSStyle>* |
| 69 inlineStyle) { | 65 inline_style) { |
| 70 *inlineStyle = GetStylesForNode(node_id); | 66 *inline_style = GetStylesForNode(node_id); |
| 71 if (!inlineStyle) { | 67 if (!inline_style) { |
| 72 return ui::devtools::protocol::Response::Error( | 68 return ui::devtools::protocol::Response::Error( |
| 73 "Node with that id not found"); | 69 "Node with that id not found"); |
| 74 } | 70 } |
| 75 return ui::devtools::protocol::Response::OK(); | 71 return ui::devtools::protocol::Response::OK(); |
| 76 } | 72 } |
| 77 | 73 |
| 74 void AshDevToolsCSSAgent::OnWindowBoundsChanged(WmWindow* window) { |
| 75 InvalidateStyleSheet(dom_agent_->GetNodeIdFromWindow(window)); |
| 76 } |
| 77 |
| 78 void AshDevToolsCSSAgent::OnWidgetBoundsChanged(views::Widget* widget) { |
| 79 InvalidateStyleSheet(dom_agent_->GetNodeIdFromWidget(widget)); |
| 80 } |
| 81 |
| 82 void AshDevToolsCSSAgent::OnViewBoundsChanged(views::View* view) { |
| 83 InvalidateStyleSheet(dom_agent_->GetNodeIdFromView(view)); |
| 84 } |
| 85 |
| 78 std::unique_ptr<ui::devtools::protocol::CSS::CSSStyle> | 86 std::unique_ptr<ui::devtools::protocol::CSS::CSSStyle> |
| 79 AshDevToolsCSSAgent::GetStylesForNode(int node_id) { | 87 AshDevToolsCSSAgent::GetStylesForNode(int node_id) { |
| 80 WmWindow* window = dom_agent_->GetWindowFromNodeId(node_id); | 88 WmWindow* window = dom_agent_->GetWindowFromNodeId(node_id); |
| 81 if (window) | 89 if (window) |
| 82 return BuildStyles(window); | 90 return BuildCSSStyle(node_id, window->GetBounds()); |
| 83 | 91 |
| 84 views::Widget* widget = dom_agent_->GetWidgetFromNodeId(node_id); | 92 views::Widget* widget = dom_agent_->GetWidgetFromNodeId(node_id); |
| 85 if (widget) | 93 if (widget) |
| 86 return BuildStyles(widget); | 94 return BuildCSSStyle(node_id, widget->GetWindowBoundsInScreen()); |
| 87 | 95 |
| 88 views::View* view = dom_agent_->GetViewFromNodeId(node_id); | 96 views::View* view = dom_agent_->GetViewFromNodeId(node_id); |
| 89 if (view) | 97 if (view) |
| 90 return BuildStyles(view); | 98 return BuildCSSStyle(node_id, view->bounds()); |
| 91 | 99 |
| 92 return nullptr; | 100 return nullptr; |
| 93 } | 101 } |
| 94 | 102 |
| 103 void AshDevToolsCSSAgent::InvalidateStyleSheet(int node_id) { |
| 104 // The stylesheetId for each node is equivalent to its node_id (as a string). |
| 105 frontend()->styleSheetChanged(base::IntToString(node_id)); |
| 106 } |
| 107 |
| 95 } // namespace devtools | 108 } // namespace devtools |
| 96 } // namespace ash | 109 } // namespace ash |
| OLD | NEW |