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 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) { |
| 69 return ui::devtools::protocol::Response::Error( |
| 70 "Node with id=" + std::to_string(node_id) + " not found"); |
| 71 } |
| 72 |
| 73 Response ParseProperties(const std::string& style_text, |
| 74 gfx::Rect* bounds, |
| 75 bool* visible) { |
65 std::vector<std::string> tokens = base::SplitString( | 76 std::vector<std::string> tokens = base::SplitString( |
66 style_text, ":;", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | 77 style_text, ":;", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
67 for (size_t i = 0; i < tokens.size() - 1; i += 2) { | 78 for (size_t i = 0; i < tokens.size() - 1; i += 2) { |
68 const std::string& property = tokens.at(i); | 79 const std::string& property = tokens.at(i); |
69 int value; | 80 int value; |
70 if (!base::StringToInt(tokens.at(i + 1), &value)) | 81 if (!base::StringToInt(tokens.at(i + 1), &value)) |
71 return Response::Error("Unable to parse value for property=" + property); | 82 return Response::Error("Unable to parse value for property=" + property); |
72 | 83 |
73 if (property == kHeight) | 84 if (property == kHeight) |
74 bounds.set_height(std::max(0, value)); | 85 bounds->set_height(std::max(0, value)); |
75 else if (property == kWidth) | 86 else if (property == kWidth) |
76 bounds.set_width(std::max(0, value)); | 87 bounds->set_width(std::max(0, value)); |
77 else if (property == kX) | 88 else if (property == kX) |
78 bounds.set_x(value); | 89 bounds->set_x(value); |
79 else if (property == kY) | 90 else if (property == kY) |
80 bounds.set_y(value); | 91 bounds->set_y(value); |
| 92 else if (property == kVisibility) |
| 93 *visible = std::max(0, value) == 1; |
81 else | 94 else |
82 return Response::Error("Unsupported property=" + property); | 95 return Response::Error("Unsupported property=" + property); |
83 } | 96 } |
84 return Response::OK(); | 97 return Response::OK(); |
85 } | 98 } |
86 | 99 |
87 } // namespace | 100 } // namespace |
88 | 101 |
89 AshDevToolsCSSAgent::AshDevToolsCSSAgent(AshDevToolsDOMAgent* dom_agent) | 102 AshDevToolsCSSAgent::AshDevToolsCSSAgent(AshDevToolsDOMAgent* dom_agent) |
90 : dom_agent_(dom_agent) { | 103 : dom_agent_(dom_agent) { |
(...skipping 12 matching lines...) Expand all Loading... |
103 ui::devtools::protocol::Response AshDevToolsCSSAgent::disable() { | 116 ui::devtools::protocol::Response AshDevToolsCSSAgent::disable() { |
104 dom_agent_->RemoveObserver(this); | 117 dom_agent_->RemoveObserver(this); |
105 return ui::devtools::protocol::Response::OK(); | 118 return ui::devtools::protocol::Response::OK(); |
106 } | 119 } |
107 | 120 |
108 ui::devtools::protocol::Response AshDevToolsCSSAgent::getMatchedStylesForNode( | 121 ui::devtools::protocol::Response AshDevToolsCSSAgent::getMatchedStylesForNode( |
109 int node_id, | 122 int node_id, |
110 ui::devtools::protocol::Maybe<ui::devtools::protocol::CSS::CSSStyle>* | 123 ui::devtools::protocol::Maybe<ui::devtools::protocol::CSS::CSSStyle>* |
111 inline_style) { | 124 inline_style) { |
112 *inline_style = GetStylesForNode(node_id); | 125 *inline_style = GetStylesForNode(node_id); |
113 if (!inline_style) { | 126 if (!inline_style) |
114 return ui::devtools::protocol::Response::Error( | 127 return NodeNotFoundError(node_id); |
115 "Node with that id not found"); | |
116 } | |
117 return ui::devtools::protocol::Response::OK(); | 128 return ui::devtools::protocol::Response::OK(); |
118 } | 129 } |
119 | 130 |
120 ui::devtools::protocol::Response AshDevToolsCSSAgent::setStyleTexts( | 131 ui::devtools::protocol::Response AshDevToolsCSSAgent::setStyleTexts( |
121 std::unique_ptr<ui::devtools::protocol::Array< | 132 std::unique_ptr<ui::devtools::protocol::Array< |
122 ui::devtools::protocol::CSS::StyleDeclarationEdit>> edits, | 133 ui::devtools::protocol::CSS::StyleDeclarationEdit>> edits, |
123 std::unique_ptr< | 134 std::unique_ptr< |
124 ui::devtools::protocol::Array<ui::devtools::protocol::CSS::CSSStyle>>* | 135 ui::devtools::protocol::Array<ui::devtools::protocol::CSS::CSSStyle>>* |
125 result) { | 136 result) { |
126 std::unique_ptr< | 137 std::unique_ptr< |
127 ui::devtools::protocol::Array<ui::devtools::protocol::CSS::CSSStyle>> | 138 ui::devtools::protocol::Array<ui::devtools::protocol::CSS::CSSStyle>> |
128 updated_styles = ui::devtools::protocol::Array< | 139 updated_styles = ui::devtools::protocol::Array< |
129 ui::devtools::protocol::CSS::CSSStyle>::create(); | 140 ui::devtools::protocol::CSS::CSSStyle>::create(); |
130 for (size_t i = 0; i < edits->length(); i++) { | 141 for (size_t i = 0; i < edits->length(); i++) { |
131 auto* edit = edits->get(i); | 142 auto* edit = edits->get(i); |
132 int node_id; | 143 int node_id; |
133 if (!base::StringToInt(edit->getStyleSheetId(), &node_id)) | 144 if (!base::StringToInt(edit->getStyleSheetId(), &node_id)) |
134 return ui::devtools::protocol::Response::Error("Invalid node id"); | 145 return ui::devtools::protocol::Response::Error("Invalid node id"); |
135 | 146 |
136 gfx::Rect updated_bounds; | 147 gfx::Rect updated_bounds; |
137 if (!GetBoundsForNodeId(node_id, &updated_bounds)) { | 148 bool visible = false; |
138 return ui::devtools::protocol::Response::Error( | 149 if (!GetPropertiesForNodeId(node_id, &updated_bounds, &visible)) |
139 "No node found with that id"); | 150 return NodeNotFoundError(node_id); |
140 } | |
141 | 151 |
142 ui::devtools::protocol::Response response( | 152 ui::devtools::protocol::Response response( |
143 ParseBounds(edit->getText(), updated_bounds)); | 153 ParseProperties(edit->getText(), &updated_bounds, &visible)); |
144 if (!response.isSuccess()) | 154 if (!response.isSuccess()) |
145 return response; | 155 return response; |
146 | 156 |
147 updated_styles->addItem(BuildCSSStyle(node_id, updated_bounds)); | 157 updated_styles->addItem(BuildCSSStyle(node_id, updated_bounds, visible)); |
148 if (!UpdateBounds(node_id, updated_bounds)) { | 158 |
149 return ui::devtools::protocol::Response::Error( | 159 if (!SetPropertiesForNodeId(node_id, updated_bounds, visible)) |
150 "No node found with that id"); | 160 return NodeNotFoundError(node_id); |
151 } | |
152 } | 161 } |
153 | |
154 *result = std::move(updated_styles); | 162 *result = std::move(updated_styles); |
155 return ui::devtools::protocol::Response::OK(); | 163 return ui::devtools::protocol::Response::OK(); |
156 } | 164 } |
157 | 165 |
158 void AshDevToolsCSSAgent::OnWindowBoundsChanged(WmWindow* window) { | 166 void AshDevToolsCSSAgent::OnWindowBoundsChanged(WmWindow* window) { |
159 InvalidateStyleSheet(dom_agent_->GetNodeIdFromWindow(window)); | 167 InvalidateStyleSheet(dom_agent_->GetNodeIdFromWindow(window)); |
160 } | 168 } |
161 | 169 |
162 void AshDevToolsCSSAgent::OnWidgetBoundsChanged(views::Widget* widget) { | 170 void AshDevToolsCSSAgent::OnWidgetBoundsChanged(views::Widget* widget) { |
163 InvalidateStyleSheet(dom_agent_->GetNodeIdFromWidget(widget)); | 171 InvalidateStyleSheet(dom_agent_->GetNodeIdFromWidget(widget)); |
164 } | 172 } |
165 | 173 |
166 void AshDevToolsCSSAgent::OnViewBoundsChanged(views::View* view) { | 174 void AshDevToolsCSSAgent::OnViewBoundsChanged(views::View* view) { |
167 InvalidateStyleSheet(dom_agent_->GetNodeIdFromView(view)); | 175 InvalidateStyleSheet(dom_agent_->GetNodeIdFromView(view)); |
168 } | 176 } |
169 | 177 |
170 std::unique_ptr<ui::devtools::protocol::CSS::CSSStyle> | 178 std::unique_ptr<ui::devtools::protocol::CSS::CSSStyle> |
171 AshDevToolsCSSAgent::GetStylesForNode(int node_id) { | 179 AshDevToolsCSSAgent::GetStylesForNode(int node_id) { |
172 gfx::Rect bounds; | 180 gfx::Rect bounds; |
173 return GetBoundsForNodeId(node_id, &bounds) ? BuildCSSStyle(node_id, bounds) | 181 bool visible = false; |
174 : nullptr; | 182 return GetPropertiesForNodeId(node_id, &bounds, &visible) |
| 183 ? BuildCSSStyle(node_id, bounds, visible) |
| 184 : nullptr; |
175 } | 185 } |
176 | 186 |
177 void AshDevToolsCSSAgent::InvalidateStyleSheet(int node_id) { | 187 void AshDevToolsCSSAgent::InvalidateStyleSheet(int node_id) { |
178 // The stylesheetId for each node is equivalent to its node_id (as a string). | 188 // The stylesheetId for each node is equivalent to its node_id (as a string). |
179 frontend()->styleSheetChanged(base::IntToString(node_id)); | 189 frontend()->styleSheetChanged(base::IntToString(node_id)); |
180 } | 190 } |
181 | 191 |
182 bool AshDevToolsCSSAgent::GetBoundsForNodeId(int node_id, gfx::Rect* bounds) { | 192 bool AshDevToolsCSSAgent::GetPropertiesForNodeId(int node_id, |
| 193 gfx::Rect* bounds, |
| 194 bool* visible) { |
183 WmWindow* window = dom_agent_->GetWindowFromNodeId(node_id); | 195 WmWindow* window = dom_agent_->GetWindowFromNodeId(node_id); |
184 if (window) { | 196 if (window) { |
185 *bounds = window->GetBounds(); | 197 *bounds = window->GetBounds(); |
| 198 *visible = window->IsVisible(); |
186 return true; | 199 return true; |
187 } | 200 } |
188 | |
189 views::Widget* widget = dom_agent_->GetWidgetFromNodeId(node_id); | 201 views::Widget* widget = dom_agent_->GetWidgetFromNodeId(node_id); |
190 if (widget) { | 202 if (widget) { |
191 *bounds = widget->GetRestoredBounds(); | 203 *bounds = widget->GetRestoredBounds(); |
| 204 *visible = widget->IsVisible(); |
192 return true; | 205 return true; |
193 } | 206 } |
194 | |
195 views::View* view = dom_agent_->GetViewFromNodeId(node_id); | 207 views::View* view = dom_agent_->GetViewFromNodeId(node_id); |
196 if (view) { | 208 if (view) { |
197 *bounds = view->bounds(); | 209 *bounds = view->bounds(); |
| 210 *visible = view->visible(); |
198 return true; | 211 return true; |
199 } | 212 } |
200 | |
201 return false; | 213 return false; |
202 } | 214 } |
203 | 215 |
204 bool AshDevToolsCSSAgent::UpdateBounds(int node_id, const gfx::Rect& bounds) { | 216 bool AshDevToolsCSSAgent::SetPropertiesForNodeId(int node_id, |
| 217 const gfx::Rect& bounds, |
| 218 bool visible) { |
205 WmWindow* window = dom_agent_->GetWindowFromNodeId(node_id); | 219 WmWindow* window = dom_agent_->GetWindowFromNodeId(node_id); |
206 if (window) { | 220 if (window) { |
207 window->SetBounds(bounds); | 221 window->SetBounds(bounds); |
| 222 if (visible != window->IsVisible()) { |
| 223 if (visible) |
| 224 window->Show(); |
| 225 else |
| 226 window->Hide(); |
| 227 } |
208 return true; | 228 return true; |
209 } | 229 } |
210 | |
211 views::Widget* widget = dom_agent_->GetWidgetFromNodeId(node_id); | 230 views::Widget* widget = dom_agent_->GetWidgetFromNodeId(node_id); |
212 if (widget) { | 231 if (widget) { |
213 widget->SetBounds(bounds); | 232 widget->SetBounds(bounds); |
| 233 if (visible != widget->IsVisible()) { |
| 234 if (visible) |
| 235 widget->Show(); |
| 236 else |
| 237 widget->Hide(); |
| 238 } |
214 return true; | 239 return true; |
215 } | 240 } |
216 | |
217 views::View* view = dom_agent_->GetViewFromNodeId(node_id); | 241 views::View* view = dom_agent_->GetViewFromNodeId(node_id); |
218 if (view) { | 242 if (view) { |
219 view->SetBoundsRect(bounds); | 243 view->SetBoundsRect(bounds); |
| 244 if (visible != view->visible()) |
| 245 view->SetVisible(visible); |
220 return true; | 246 return true; |
221 } | 247 } |
222 | |
223 return false; | 248 return false; |
224 } | 249 } |
225 | 250 |
226 } // namespace devtools | 251 } // namespace devtools |
227 } // namespace ash | 252 } // namespace ash |
OLD | NEW |