Chromium Code Reviews| 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 #include "base/strings/string_split.h" | 8 #include "base/strings/string_split.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 | 10 |
| 11 namespace ash { | 11 namespace ash { |
| 12 namespace devtools { | 12 namespace devtools { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 using namespace ui::devtools::protocol; | 15 using namespace ui::devtools::protocol; |
| 16 | 16 |
| 17 const char kHeight[] = "height"; | 17 const char kHeight[] = "height"; |
| 18 const char kWidth[] = "width"; | 18 const char kWidth[] = "width"; |
| 19 const char kX[] = "x"; | 19 const char kX[] = "x"; |
| 20 const char kY[] = "y"; | 20 const char kY[] = "y"; |
| 21 const char kVisibility[] = "visibility"; | |
| 21 | 22 |
| 22 std::unique_ptr<CSS::SourceRange> BuildDefaultSourceRange() { | 23 std::unique_ptr<CSS::SourceRange> BuildDefaultSourceRange() { |
| 23 // These tell the frontend where in the stylesheet a certain style | 24 // These tell the frontend where in the stylesheet a certain style |
| 24 // is located. Since we don't have stylesheets, this is all 0. | 25 // is located. Since we don't have stylesheets, this is all 0. |
| 25 // We need this because CSS fields are not editable unless | 26 // We need this because CSS fields are not editable unless |
| 26 // the range is provided. | 27 // the range is provided. |
| 27 return CSS::SourceRange::create() | 28 return CSS::SourceRange::create() |
| 28 .setStartLine(0) | 29 .setStartLine(0) |
| 29 .setEndLine(0) | 30 .setEndLine(0) |
| 30 .setStartColumn(0) | 31 .setStartColumn(0) |
| 31 .setEndColumn(0) | 32 .setEndColumn(0) |
| 32 .build(); | 33 .build(); |
| 33 } | 34 } |
| 34 | 35 |
| 35 std::unique_ptr<CSS::CSSProperty> BuildCSSProperty(const std::string& name, | 36 std::unique_ptr<CSS::CSSProperty> BuildCSSProperty(const std::string& name, |
| 36 int value) { | 37 int value) { |
| 37 return CSS::CSSProperty::create() | 38 return CSS::CSSProperty::create() |
| 38 .setRange(BuildDefaultSourceRange()) | 39 .setRange(BuildDefaultSourceRange()) |
| 39 .setName(name) | 40 .setName(name) |
| 40 .setValue(base::IntToString(value)) | 41 .setValue(base::IntToString(value)) |
| 41 .build(); | 42 .build(); |
| 42 } | 43 } |
| 43 | 44 |
| 44 std::unique_ptr<Array<CSS::CSSProperty>> BuildBoundsCSSPropertyArray( | 45 std::unique_ptr<Array<CSS::CSSProperty>> BuildCSSPropertyArray( |
| 45 const gfx::Rect& bounds) { | 46 const gfx::Rect& bounds, |
| 47 const bool visible) { | |
| 46 auto cssProperties = Array<CSS::CSSProperty>::create(); | 48 auto cssProperties = Array<CSS::CSSProperty>::create(); |
| 47 cssProperties->addItem(BuildCSSProperty(kHeight, bounds.height())); | 49 cssProperties->addItem(BuildCSSProperty(kHeight, bounds.height())); |
| 48 cssProperties->addItem(BuildCSSProperty(kWidth, bounds.width())); | 50 cssProperties->addItem(BuildCSSProperty(kWidth, bounds.width())); |
| 49 cssProperties->addItem(BuildCSSProperty(kX, bounds.x())); | 51 cssProperties->addItem(BuildCSSProperty(kX, bounds.x())); |
| 50 cssProperties->addItem(BuildCSSProperty(kY, bounds.y())); | 52 cssProperties->addItem(BuildCSSProperty(kY, bounds.y())); |
| 53 cssProperties->addItem(BuildCSSProperty(kVisibility, visible)); | |
| 51 return cssProperties; | 54 return cssProperties; |
| 52 } | 55 } |
| 53 | 56 |
| 54 std::unique_ptr<CSS::CSSStyle> BuildCSSStyle(int node_id, | 57 std::unique_ptr<CSS::CSSStyle> BuildCSSStyle(int node_id, |
| 55 const gfx::Rect& bounds) { | 58 const gfx::Rect& bounds, |
| 59 const bool visible) { | |
| 56 return CSS::CSSStyle::create() | 60 return CSS::CSSStyle::create() |
| 57 .setRange(BuildDefaultSourceRange()) | 61 .setRange(BuildDefaultSourceRange()) |
| 58 .setStyleSheetId(base::IntToString(node_id)) | 62 .setStyleSheetId(base::IntToString(node_id)) |
| 59 .setCssProperties(BuildBoundsCSSPropertyArray(bounds)) | 63 .setCssProperties(BuildCSSPropertyArray(bounds, visible)) |
| 60 .setShorthandEntries(Array<std::string>::create()) | 64 .setShorthandEntries(Array<std::string>::create()) |
| 61 .build(); | 65 .build(); |
| 62 } | 66 } |
| 63 | 67 |
| 64 Response ParseBounds(const std::string& style_text, gfx::Rect& bounds) { | 68 ui::devtools::protocol::Response NodeNotFoundError(int node_id) { |
| 65 std::vector<std::string> tokens = base::SplitString( | 69 return ui::devtools::protocol::Response::Error( |
| 66 style_text, ":;", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | 70 "Node with id=" + std::to_string(node_id) + " not found"); |
| 67 for (size_t i = 0; i < tokens.size() - 1; i += 2) { | |
| 68 const std::string& property = tokens.at(i); | |
| 69 int value; | |
| 70 if (!base::StringToInt(tokens.at(i + 1), &value)) | |
| 71 return Response::Error("Unable to parse value for property=" + property); | |
| 72 | |
| 73 if (property == kHeight) | |
| 74 bounds.set_height(std::max(0, value)); | |
| 75 else if (property == kWidth) | |
| 76 bounds.set_width(std::max(0, value)); | |
| 77 else if (property == kX) | |
| 78 bounds.set_x(value); | |
| 79 else if (property == kY) | |
| 80 bounds.set_y(value); | |
| 81 else | |
| 82 return Response::Error("Unsupported property=" + property); | |
| 83 } | |
| 84 return Response::OK(); | |
| 85 } | 71 } |
| 86 | 72 |
| 87 } // namespace | 73 } // namespace |
| 88 | 74 |
| 89 AshDevToolsCSSAgent::AshDevToolsCSSAgent(AshDevToolsDOMAgent* dom_agent) | 75 AshDevToolsCSSAgent::AshDevToolsCSSAgent(AshDevToolsDOMAgent* dom_agent) |
| 90 : dom_agent_(dom_agent) { | 76 : dom_agent_(dom_agent) { |
| 91 DCHECK(dom_agent_); | 77 DCHECK(dom_agent_); |
| 92 } | 78 } |
| 93 | 79 |
| 94 AshDevToolsCSSAgent::~AshDevToolsCSSAgent() { | 80 AshDevToolsCSSAgent::~AshDevToolsCSSAgent() { |
| 95 disable(); | 81 disable(); |
| 96 } | 82 } |
| 97 | 83 |
| 98 ui::devtools::protocol::Response AshDevToolsCSSAgent::enable() { | 84 ui::devtools::protocol::Response AshDevToolsCSSAgent::enable() { |
| 99 dom_agent_->AddObserver(this); | 85 dom_agent_->AddObserver(this); |
| 100 return ui::devtools::protocol::Response::OK(); | 86 return ui::devtools::protocol::Response::OK(); |
| 101 } | 87 } |
| 102 | 88 |
| 103 ui::devtools::protocol::Response AshDevToolsCSSAgent::disable() { | 89 ui::devtools::protocol::Response AshDevToolsCSSAgent::disable() { |
| 104 dom_agent_->RemoveObserver(this); | 90 dom_agent_->RemoveObserver(this); |
| 105 return ui::devtools::protocol::Response::OK(); | 91 return ui::devtools::protocol::Response::OK(); |
| 106 } | 92 } |
| 107 | 93 |
| 108 ui::devtools::protocol::Response AshDevToolsCSSAgent::getMatchedStylesForNode( | 94 ui::devtools::protocol::Response AshDevToolsCSSAgent::getMatchedStylesForNode( |
| 109 int node_id, | 95 int node_id, |
| 110 ui::devtools::protocol::Maybe<ui::devtools::protocol::CSS::CSSStyle>* | 96 ui::devtools::protocol::Maybe<ui::devtools::protocol::CSS::CSSStyle>* |
| 111 inline_style) { | 97 inline_style) { |
| 112 *inline_style = GetStylesForNode(node_id); | 98 *inline_style = GetStylesForNode(node_id); |
| 113 if (!inline_style) { | 99 if (!inline_style) |
| 114 return ui::devtools::protocol::Response::Error( | 100 NodeNotFoundError(node_id); |
|
sadrul
2017/03/09 18:11:33
return NodeNotFoundError(node_id);
thanhph
2017/03/09 18:50:17
oops, thanks!
| |
| 115 "Node with that id not found"); | |
| 116 } | |
| 117 return ui::devtools::protocol::Response::OK(); | 101 return ui::devtools::protocol::Response::OK(); |
| 118 } | 102 } |
| 119 | 103 |
| 120 ui::devtools::protocol::Response AshDevToolsCSSAgent::setStyleTexts( | 104 ui::devtools::protocol::Response AshDevToolsCSSAgent::setStyleTexts( |
| 121 std::unique_ptr<ui::devtools::protocol::Array< | 105 std::unique_ptr<ui::devtools::protocol::Array< |
| 122 ui::devtools::protocol::CSS::StyleDeclarationEdit>> edits, | 106 ui::devtools::protocol::CSS::StyleDeclarationEdit>> edits, |
| 123 std::unique_ptr< | 107 std::unique_ptr< |
| 124 ui::devtools::protocol::Array<ui::devtools::protocol::CSS::CSSStyle>>* | 108 ui::devtools::protocol::Array<ui::devtools::protocol::CSS::CSSStyle>>* |
| 125 result) { | 109 result) { |
| 126 std::unique_ptr< | 110 std::unique_ptr< |
| 127 ui::devtools::protocol::Array<ui::devtools::protocol::CSS::CSSStyle>> | 111 ui::devtools::protocol::Array<ui::devtools::protocol::CSS::CSSStyle>> |
| 128 updated_styles = ui::devtools::protocol::Array< | 112 updated_styles = ui::devtools::protocol::Array< |
| 129 ui::devtools::protocol::CSS::CSSStyle>::create(); | 113 ui::devtools::protocol::CSS::CSSStyle>::create(); |
| 130 for (size_t i = 0; i < edits->length(); i++) { | 114 for (size_t i = 0; i < edits->length(); i++) { |
| 131 auto* edit = edits->get(i); | 115 auto* edit = edits->get(i); |
| 132 int node_id; | 116 int node_id; |
| 133 if (!base::StringToInt(edit->getStyleSheetId(), &node_id)) | 117 if (!base::StringToInt(edit->getStyleSheetId(), &node_id)) |
| 134 return ui::devtools::protocol::Response::Error("Invalid node id"); | 118 return ui::devtools::protocol::Response::Error("Invalid node id"); |
| 135 | 119 |
| 136 gfx::Rect updated_bounds; | 120 gfx::Rect updated_bounds; |
| 137 if (!GetBoundsForNodeId(node_id, &updated_bounds)) { | 121 bool visible; |
| 138 return ui::devtools::protocol::Response::Error( | 122 if (!GetPropertiesForNodeId(node_id, &updated_bounds, &visible)) |
| 139 "No node found with that id"); | 123 NodeNotFoundError(node_id); |
|
sadrul
2017/03/09 18:11:33
return
(applies same comment applies to code belo
thanhph
2017/03/09 18:50:17
Done.
| |
| 140 } | |
| 141 | 124 |
| 142 ui::devtools::protocol::Response response( | 125 ui::devtools::protocol::Response response( |
| 143 ParseBounds(edit->getText(), updated_bounds)); | 126 ParseProperties(edit->getText(), &updated_bounds, &visible)); |
| 144 if (!response.isSuccess()) | 127 if (!response.isSuccess()) |
| 145 return response; | 128 return response; |
| 146 | 129 |
| 147 updated_styles->addItem(BuildCSSStyle(node_id, updated_bounds)); | 130 updated_styles->addItem(BuildCSSStyle(node_id, updated_bounds, visible)); |
| 148 if (!UpdateBounds(node_id, updated_bounds)) { | 131 |
| 149 return ui::devtools::protocol::Response::Error( | 132 if (!SetPropertiesForNodeId(node_id, updated_bounds, visible)) |
| 150 "No node found with that id"); | 133 NodeNotFoundError(node_id); |
| 151 } | |
| 152 } | 134 } |
| 153 | |
| 154 *result = std::move(updated_styles); | 135 *result = std::move(updated_styles); |
| 155 return ui::devtools::protocol::Response::OK(); | 136 return ui::devtools::protocol::Response::OK(); |
| 156 } | 137 } |
| 157 | 138 |
| 158 void AshDevToolsCSSAgent::OnWindowBoundsChanged(WmWindow* window) { | 139 void AshDevToolsCSSAgent::OnWindowBoundsChanged(WmWindow* window) { |
| 159 InvalidateStyleSheet(dom_agent_->GetNodeIdFromWindow(window)); | 140 InvalidateStyleSheet(dom_agent_->GetNodeIdFromWindow(window)); |
| 160 } | 141 } |
| 161 | 142 |
| 162 void AshDevToolsCSSAgent::OnWidgetBoundsChanged(views::Widget* widget) { | 143 void AshDevToolsCSSAgent::OnWidgetBoundsChanged(views::Widget* widget) { |
| 163 InvalidateStyleSheet(dom_agent_->GetNodeIdFromWidget(widget)); | 144 InvalidateStyleSheet(dom_agent_->GetNodeIdFromWidget(widget)); |
| 164 } | 145 } |
| 165 | 146 |
| 166 void AshDevToolsCSSAgent::OnViewBoundsChanged(views::View* view) { | 147 void AshDevToolsCSSAgent::OnViewBoundsChanged(views::View* view) { |
| 167 InvalidateStyleSheet(dom_agent_->GetNodeIdFromView(view)); | 148 InvalidateStyleSheet(dom_agent_->GetNodeIdFromView(view)); |
| 168 } | 149 } |
| 169 | 150 |
| 170 std::unique_ptr<ui::devtools::protocol::CSS::CSSStyle> | 151 std::unique_ptr<ui::devtools::protocol::CSS::CSSStyle> |
| 171 AshDevToolsCSSAgent::GetStylesForNode(int node_id) { | 152 AshDevToolsCSSAgent::GetStylesForNode(int node_id) { |
| 172 gfx::Rect bounds; | 153 gfx::Rect bounds; |
| 173 return GetBoundsForNodeId(node_id, &bounds) ? BuildCSSStyle(node_id, bounds) | 154 bool visible; |
| 174 : nullptr; | 155 return GetPropertiesForNodeId(node_id, &bounds, &visible) |
| 156 ? BuildCSSStyle(node_id, bounds, visible) | |
| 157 : nullptr; | |
| 175 } | 158 } |
| 176 | 159 |
| 177 void AshDevToolsCSSAgent::InvalidateStyleSheet(int node_id) { | 160 void AshDevToolsCSSAgent::InvalidateStyleSheet(int node_id) { |
| 178 // The stylesheetId for each node is equivalent to its node_id (as a string). | 161 // The stylesheetId for each node is equivalent to its node_id (as a string). |
| 179 frontend()->styleSheetChanged(base::IntToString(node_id)); | 162 frontend()->styleSheetChanged(base::IntToString(node_id)); |
| 180 } | 163 } |
| 181 | 164 |
| 182 bool AshDevToolsCSSAgent::GetBoundsForNodeId(int node_id, gfx::Rect* bounds) { | 165 bool AshDevToolsCSSAgent::GetPropertiesForNodeId(int node_id, |
| 166 gfx::Rect* bounds, | |
| 167 bool* visible) { | |
| 183 WmWindow* window = dom_agent_->GetWindowFromNodeId(node_id); | 168 WmWindow* window = dom_agent_->GetWindowFromNodeId(node_id); |
| 184 if (window) { | 169 if (window) { |
| 185 *bounds = window->GetBounds(); | 170 *bounds = window->GetBounds(); |
| 171 *visible = window->IsVisible(); | |
| 186 return true; | 172 return true; |
| 187 } | 173 } |
| 188 | |
| 189 views::Widget* widget = dom_agent_->GetWidgetFromNodeId(node_id); | 174 views::Widget* widget = dom_agent_->GetWidgetFromNodeId(node_id); |
| 190 if (widget) { | 175 if (widget) { |
| 191 *bounds = widget->GetRestoredBounds(); | 176 *bounds = widget->GetRestoredBounds(); |
| 177 *visible = widget->IsVisible(); | |
| 192 return true; | 178 return true; |
| 193 } | 179 } |
| 194 | |
| 195 views::View* view = dom_agent_->GetViewFromNodeId(node_id); | 180 views::View* view = dom_agent_->GetViewFromNodeId(node_id); |
| 196 if (view) { | 181 if (view) { |
| 197 *bounds = view->bounds(); | 182 *bounds = view->bounds(); |
| 183 *visible = view->visible(); | |
| 198 return true; | 184 return true; |
| 199 } | 185 } |
| 200 | |
| 201 return false; | 186 return false; |
| 202 } | 187 } |
| 203 | 188 |
| 204 bool AshDevToolsCSSAgent::UpdateBounds(int node_id, const gfx::Rect& bounds) { | 189 bool AshDevToolsCSSAgent::SetPropertiesForNodeId(int node_id, |
| 190 const gfx::Rect& bounds, | |
| 191 const bool visible) { | |
| 205 WmWindow* window = dom_agent_->GetWindowFromNodeId(node_id); | 192 WmWindow* window = dom_agent_->GetWindowFromNodeId(node_id); |
| 206 if (window) { | 193 if (window) { |
| 207 window->SetBounds(bounds); | 194 window->SetBounds(bounds); |
| 195 if (visible != window->IsVisible()) { | |
| 196 if (visible) | |
| 197 window->Show(); | |
| 198 else | |
| 199 window->Hide(); | |
| 200 } | |
| 208 return true; | 201 return true; |
| 209 } | 202 } |
| 210 | |
| 211 views::Widget* widget = dom_agent_->GetWidgetFromNodeId(node_id); | 203 views::Widget* widget = dom_agent_->GetWidgetFromNodeId(node_id); |
| 212 if (widget) { | 204 if (widget) { |
| 213 widget->SetBounds(bounds); | 205 widget->SetBounds(bounds); |
| 206 if (visible != widget->IsVisible()) { | |
| 207 if (visible) | |
| 208 widget->Show(); | |
| 209 else | |
| 210 widget->Hide(); | |
| 211 } | |
| 214 return true; | 212 return true; |
| 215 } | 213 } |
| 216 | |
| 217 views::View* view = dom_agent_->GetViewFromNodeId(node_id); | 214 views::View* view = dom_agent_->GetViewFromNodeId(node_id); |
| 218 if (view) { | 215 if (view) { |
| 219 view->SetBoundsRect(bounds); | 216 view->SetBoundsRect(bounds); |
| 217 if (visible != view->visible()) | |
| 218 view->SetVisible(visible); | |
| 220 return true; | 219 return true; |
| 221 } | 220 } |
| 221 return false; | |
| 222 } | |
| 222 | 223 |
| 223 return false; | 224 Response AshDevToolsCSSAgent::ParseProperties(const std::string& style_text, |
|
sadrul
2017/03/09 18:11:33
Is there a reason this function can't remain as an
thanhph
2017/03/09 18:50:17
My bad, I was using |dom_agent_| here to reduce co
| |
| 225 gfx::Rect* bounds, | |
| 226 bool* visible) { | |
| 227 std::vector<std::string> tokens = base::SplitString( | |
| 228 style_text, ":;", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
| 229 for (size_t i = 0; i < tokens.size() - 1; i += 2) { | |
| 230 const std::string& property = tokens.at(i); | |
| 231 int value; | |
| 232 if (!base::StringToInt(tokens.at(i + 1), &value)) | |
|
sadrul
2017/03/09 18:11:33
Is visibility sent as an int too?
thanhph
2017/03/09 18:50:17
Yes, 0 for non-visible and 1 is visible. We could
| |
| 233 return Response::Error("Unable to parse value for property=" + property); | |
| 234 | |
| 235 if (property == kHeight) | |
| 236 bounds->set_height(std::max(0, value)); | |
| 237 else if (property == kWidth) | |
| 238 bounds->set_width(std::max(0, value)); | |
| 239 else if (property == kX) | |
| 240 bounds->set_x(value); | |
| 241 else if (property == kY) | |
| 242 bounds->set_y(value); | |
| 243 else if (property == kVisibility) | |
| 244 *visible = std::max(0, value) == 1; | |
| 245 else | |
| 246 return Response::Error("Unsupported property=" + property); | |
| 247 } | |
| 248 return Response::OK(); | |
| 224 } | 249 } |
| 225 | 250 |
| 226 } // namespace devtools | 251 } // namespace devtools |
| 227 } // namespace ash | 252 } // namespace ash |
| OLD | NEW |