Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: components/ui_devtools/views/ui_devtools_css_agent.cc

Issue 2899783002: Move DevTools out of ash and turn it to a component. (Closed)
Patch Set: add README.md Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/devtools/ash_devtools_css_agent.h" 5 #include "components/ui_devtools/views/ui_devtools_css_agent.h"
6 6
7 #include "ash/devtools/ui_element.h"
8 #include "base/strings/string_split.h" 7 #include "base/strings/string_split.h"
9 #include "base/strings/string_util.h" 8 #include "base/strings/string_util.h"
9 #include "components/ui_devtools/views/ui_element.h"
10 #include "ui/aura/window.h" 10 #include "ui/aura/window.h"
11 11
12 namespace ash { 12 namespace ui_devtools {
13 namespace devtools {
14 namespace { 13 namespace {
15 14
16 using namespace ui::devtools::protocol; 15 using namespace ui_devtools::protocol;
17 16
18 const char kHeight[] = "height"; 17 const char kHeight[] = "height";
19 const char kWidth[] = "width"; 18 const char kWidth[] = "width";
20 const char kX[] = "x"; 19 const char kX[] = "x";
21 const char kY[] = "y"; 20 const char kY[] = "y";
22 const char kVisibility[] = "visibility"; 21 const char kVisibility[] = "visibility";
23 22
24 std::unique_ptr<CSS::SourceRange> BuildDefaultSourceRange() { 23 std::unique_ptr<CSS::SourceRange> BuildDefaultSourceRange() {
25 // These tell the frontend where in the stylesheet a certain style 24 // These tell the frontend where in the stylesheet a certain style
26 // 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.
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 const gfx::Rect& bounds, 58 const gfx::Rect& bounds,
60 bool visible) { 59 bool visible) {
61 return CSS::CSSStyle::create() 60 return CSS::CSSStyle::create()
62 .setRange(BuildDefaultSourceRange()) 61 .setRange(BuildDefaultSourceRange())
63 .setStyleSheetId(base::IntToString(node_id)) 62 .setStyleSheetId(base::IntToString(node_id))
64 .setCssProperties(BuildCSSPropertyArray(bounds, visible)) 63 .setCssProperties(BuildCSSPropertyArray(bounds, visible))
65 .setShorthandEntries(Array<std::string>::create()) 64 .setShorthandEntries(Array<std::string>::create())
66 .build(); 65 .build();
67 } 66 }
68 67
69 ui::devtools::protocol::Response NodeNotFoundError(int node_id) { 68 ui_devtools::protocol::Response NodeNotFoundError(int node_id) {
70 return ui::devtools::protocol::Response::Error( 69 return ui_devtools::protocol::Response::Error(
71 "Node with id=" + std::to_string(node_id) + " not found"); 70 "Node with id=" + std::to_string(node_id) + " not found");
72 } 71 }
73 72
74 Response ParseProperties(const std::string& style_text, 73 Response ParseProperties(const std::string& style_text,
75 gfx::Rect* bounds, 74 gfx::Rect* bounds,
76 bool* visible) { 75 bool* visible) {
77 std::vector<std::string> tokens = base::SplitString( 76 std::vector<std::string> tokens = base::SplitString(
78 style_text, ":;", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); 77 style_text, ":;", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
79 for (size_t i = 0; i < tokens.size() - 1; i += 2) { 78 for (size_t i = 0; i < tokens.size() - 1; i += 2) {
80 const std::string& property = tokens.at(i); 79 const std::string& property = tokens.at(i);
(...skipping 12 matching lines...) Expand all
93 else if (property == kVisibility) 92 else if (property == kVisibility)
94 *visible = std::max(0, value) == 1; 93 *visible = std::max(0, value) == 1;
95 else 94 else
96 return Response::Error("Unsupported property=" + property); 95 return Response::Error("Unsupported property=" + property);
97 } 96 }
98 return Response::OK(); 97 return Response::OK();
99 } 98 }
100 99
101 } // namespace 100 } // namespace
102 101
103 AshDevToolsCSSAgent::AshDevToolsCSSAgent(AshDevToolsDOMAgent* dom_agent) 102 UIDevToolsCSSAgent::UIDevToolsCSSAgent(UIDevToolsDOMAgent* dom_agent)
104 : dom_agent_(dom_agent) { 103 : dom_agent_(dom_agent) {
105 DCHECK(dom_agent_); 104 DCHECK(dom_agent_);
106 } 105 }
107 106
108 AshDevToolsCSSAgent::~AshDevToolsCSSAgent() { 107 UIDevToolsCSSAgent::~UIDevToolsCSSAgent() {
109 disable(); 108 disable();
110 } 109 }
111 110
112 ui::devtools::protocol::Response AshDevToolsCSSAgent::enable() { 111 ui_devtools::protocol::Response UIDevToolsCSSAgent::enable() {
113 dom_agent_->AddObserver(this); 112 dom_agent_->AddObserver(this);
114 return ui::devtools::protocol::Response::OK(); 113 return ui_devtools::protocol::Response::OK();
115 } 114 }
116 115
117 ui::devtools::protocol::Response AshDevToolsCSSAgent::disable() { 116 ui_devtools::protocol::Response UIDevToolsCSSAgent::disable() {
118 dom_agent_->RemoveObserver(this); 117 dom_agent_->RemoveObserver(this);
119 return ui::devtools::protocol::Response::OK(); 118 return ui_devtools::protocol::Response::OK();
120 } 119 }
121 120
122 ui::devtools::protocol::Response AshDevToolsCSSAgent::getMatchedStylesForNode( 121 ui_devtools::protocol::Response UIDevToolsCSSAgent::getMatchedStylesForNode(
123 int node_id, 122 int node_id,
124 ui::devtools::protocol::Maybe<ui::devtools::protocol::CSS::CSSStyle>* 123 ui_devtools::protocol::Maybe<ui_devtools::protocol::CSS::CSSStyle>*
125 inline_style) { 124 inline_style) {
126 *inline_style = GetStylesForNode(node_id); 125 *inline_style = GetStylesForNode(node_id);
127 if (!inline_style) 126 if (!inline_style)
128 return NodeNotFoundError(node_id); 127 return NodeNotFoundError(node_id);
129 return ui::devtools::protocol::Response::OK(); 128 return ui_devtools::protocol::Response::OK();
130 } 129 }
131 130
132 ui::devtools::protocol::Response AshDevToolsCSSAgent::setStyleTexts( 131 ui_devtools::protocol::Response UIDevToolsCSSAgent::setStyleTexts(
133 std::unique_ptr<ui::devtools::protocol::Array< 132 std::unique_ptr<ui_devtools::protocol::Array<
134 ui::devtools::protocol::CSS::StyleDeclarationEdit>> edits, 133 ui_devtools::protocol::CSS::StyleDeclarationEdit>> edits,
135 std::unique_ptr< 134 std::unique_ptr<
136 ui::devtools::protocol::Array<ui::devtools::protocol::CSS::CSSStyle>>* 135 ui_devtools::protocol::Array<ui_devtools::protocol::CSS::CSSStyle>>*
137 result) { 136 result) {
138 std::unique_ptr< 137 std::unique_ptr<
139 ui::devtools::protocol::Array<ui::devtools::protocol::CSS::CSSStyle>> 138 ui_devtools::protocol::Array<ui_devtools::protocol::CSS::CSSStyle>>
140 updated_styles = ui::devtools::protocol::Array< 139 updated_styles = ui_devtools::protocol::Array<
141 ui::devtools::protocol::CSS::CSSStyle>::create(); 140 ui_devtools::protocol::CSS::CSSStyle>::create();
142 for (size_t i = 0; i < edits->length(); i++) { 141 for (size_t i = 0; i < edits->length(); i++) {
143 auto* edit = edits->get(i); 142 auto* edit = edits->get(i);
144 int node_id; 143 int node_id;
145 if (!base::StringToInt(edit->getStyleSheetId(), &node_id)) 144 if (!base::StringToInt(edit->getStyleSheetId(), &node_id))
146 return ui::devtools::protocol::Response::Error("Invalid node id"); 145 return ui_devtools::protocol::Response::Error("Invalid node id");
147 146
148 gfx::Rect updated_bounds; 147 gfx::Rect updated_bounds;
149 bool visible = false; 148 bool visible = false;
150 if (!GetPropertiesForNodeId(node_id, &updated_bounds, &visible)) 149 if (!GetPropertiesForNodeId(node_id, &updated_bounds, &visible))
151 return NodeNotFoundError(node_id); 150 return NodeNotFoundError(node_id);
152 151
153 ui::devtools::protocol::Response response( 152 ui_devtools::protocol::Response response(
154 ParseProperties(edit->getText(), &updated_bounds, &visible)); 153 ParseProperties(edit->getText(), &updated_bounds, &visible));
155 if (!response.isSuccess()) 154 if (!response.isSuccess())
156 return response; 155 return response;
157 156
158 updated_styles->addItem(BuildCSSStyle(node_id, updated_bounds, visible)); 157 updated_styles->addItem(BuildCSSStyle(node_id, updated_bounds, visible));
159 158
160 if (!SetPropertiesForNodeId(node_id, updated_bounds, visible)) 159 if (!SetPropertiesForNodeId(node_id, updated_bounds, visible))
161 return NodeNotFoundError(node_id); 160 return NodeNotFoundError(node_id);
162 } 161 }
163 *result = std::move(updated_styles); 162 *result = std::move(updated_styles);
164 return ui::devtools::protocol::Response::OK(); 163 return ui_devtools::protocol::Response::OK();
165 } 164 }
166 165
167 void AshDevToolsCSSAgent::OnNodeBoundsChanged(int node_id) { 166 void UIDevToolsCSSAgent::OnNodeBoundsChanged(int node_id) {
168 InvalidateStyleSheet(node_id); 167 InvalidateStyleSheet(node_id);
169 } 168 }
170 169
171 std::unique_ptr<ui::devtools::protocol::CSS::CSSStyle> 170 std::unique_ptr<ui_devtools::protocol::CSS::CSSStyle>
172 AshDevToolsCSSAgent::GetStylesForNode(int node_id) { 171 UIDevToolsCSSAgent::GetStylesForNode(int node_id) {
173 gfx::Rect bounds; 172 gfx::Rect bounds;
174 bool visible = false; 173 bool visible = false;
175 return GetPropertiesForNodeId(node_id, &bounds, &visible) 174 return GetPropertiesForNodeId(node_id, &bounds, &visible)
176 ? BuildCSSStyle(node_id, bounds, visible) 175 ? BuildCSSStyle(node_id, bounds, visible)
177 : nullptr; 176 : nullptr;
178 } 177 }
179 178
180 void AshDevToolsCSSAgent::InvalidateStyleSheet(int node_id) { 179 void UIDevToolsCSSAgent::InvalidateStyleSheet(int node_id) {
181 // The stylesheetId for each node is equivalent to its node_id (as a string). 180 // The stylesheetId for each node is equivalent to its node_id (as a string).
182 frontend()->styleSheetChanged(base::IntToString(node_id)); 181 frontend()->styleSheetChanged(base::IntToString(node_id));
183 } 182 }
184 183
185 bool AshDevToolsCSSAgent::GetPropertiesForNodeId(int node_id, 184 bool UIDevToolsCSSAgent::GetPropertiesForNodeId(int node_id,
186 gfx::Rect* bounds, 185 gfx::Rect* bounds,
187 bool* visible) { 186 bool* visible) {
188 UIElement* ui_element = dom_agent_->GetElementFromNodeId(node_id); 187 UIElement* ui_element = dom_agent_->GetElementFromNodeId(node_id);
189 if (ui_element) { 188 if (ui_element) {
190 ui_element->GetBounds(bounds); 189 ui_element->GetBounds(bounds);
191 ui_element->GetVisible(visible); 190 ui_element->GetVisible(visible);
192 return true; 191 return true;
193 } 192 }
194 return false; 193 return false;
195 } 194 }
196 195
197 bool AshDevToolsCSSAgent::SetPropertiesForNodeId(int node_id, 196 bool UIDevToolsCSSAgent::SetPropertiesForNodeId(int node_id,
198 const gfx::Rect& bounds, 197 const gfx::Rect& bounds,
199 bool visible) { 198 bool visible) {
200 UIElement* ui_element = dom_agent_->GetElementFromNodeId(node_id); 199 UIElement* ui_element = dom_agent_->GetElementFromNodeId(node_id);
201 if (ui_element) { 200 if (ui_element) {
202 ui_element->SetBounds(bounds); 201 ui_element->SetBounds(bounds);
203 ui_element->SetVisible(visible); 202 ui_element->SetVisible(visible);
204 return true; 203 return true;
205 } 204 }
206 return false; 205 return false;
207 } 206 }
208 207
209 } // namespace devtools 208 } // namespace ui_devtools
210 } // namespace ash
OLDNEW
« no previous file with comments | « components/ui_devtools/views/ui_devtools_css_agent.h ('k') | components/ui_devtools/views/ui_devtools_dom_agent.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698