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

Side by Side Diff: ash/devtools/ash_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
« no previous file with comments | « ash/devtools/ash_devtools_css_agent.h ('k') | ash/devtools/ash_devtools_dom_agent.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ash/devtools/ash_devtools_css_agent.h"
6
7 #include "ash/devtools/ui_element.h"
8 #include "base/strings/string_split.h"
9 #include "base/strings/string_util.h"
10 #include "ui/aura/window.h"
11
12 namespace ash {
13 namespace devtools {
14 namespace {
15
16 using namespace ui::devtools::protocol;
17
18 const char kHeight[] = "height";
19 const char kWidth[] = "width";
20 const char kX[] = "x";
21 const char kY[] = "y";
22 const char kVisibility[] = "visibility";
23
24 std::unique_ptr<CSS::SourceRange> BuildDefaultSourceRange() {
25 // These tell the frontend where in the stylesheet a certain style
26 // is located. Since we don't have stylesheets, this is all 0.
27 // We need this because CSS fields are not editable unless
28 // the range is provided.
29 return CSS::SourceRange::create()
30 .setStartLine(0)
31 .setEndLine(0)
32 .setStartColumn(0)
33 .setEndColumn(0)
34 .build();
35 }
36
37 std::unique_ptr<CSS::CSSProperty> BuildCSSProperty(const std::string& name,
38 int value) {
39 return CSS::CSSProperty::create()
40 .setRange(BuildDefaultSourceRange())
41 .setName(name)
42 .setValue(base::IntToString(value))
43 .build();
44 }
45
46 std::unique_ptr<Array<CSS::CSSProperty>> BuildCSSPropertyArray(
47 const gfx::Rect& bounds,
48 const bool visible) {
49 auto cssProperties = Array<CSS::CSSProperty>::create();
50 cssProperties->addItem(BuildCSSProperty(kHeight, bounds.height()));
51 cssProperties->addItem(BuildCSSProperty(kWidth, bounds.width()));
52 cssProperties->addItem(BuildCSSProperty(kX, bounds.x()));
53 cssProperties->addItem(BuildCSSProperty(kY, bounds.y()));
54 cssProperties->addItem(BuildCSSProperty(kVisibility, visible));
55 return cssProperties;
56 }
57
58 std::unique_ptr<CSS::CSSStyle> BuildCSSStyle(int node_id,
59 const gfx::Rect& bounds,
60 bool visible) {
61 return CSS::CSSStyle::create()
62 .setRange(BuildDefaultSourceRange())
63 .setStyleSheetId(base::IntToString(node_id))
64 .setCssProperties(BuildCSSPropertyArray(bounds, visible))
65 .setShorthandEntries(Array<std::string>::create())
66 .build();
67 }
68
69 ui::devtools::protocol::Response NodeNotFoundError(int node_id) {
70 return ui::devtools::protocol::Response::Error(
71 "Node with id=" + std::to_string(node_id) + " not found");
72 }
73
74 Response ParseProperties(const std::string& style_text,
75 gfx::Rect* bounds,
76 bool* visible) {
77 std::vector<std::string> tokens = base::SplitString(
78 style_text, ":;", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
79 for (size_t i = 0; i < tokens.size() - 1; i += 2) {
80 const std::string& property = tokens.at(i);
81 int value;
82 if (!base::StringToInt(tokens.at(i + 1), &value))
83 return Response::Error("Unable to parse value for property=" + property);
84
85 if (property == kHeight)
86 bounds->set_height(std::max(0, value));
87 else if (property == kWidth)
88 bounds->set_width(std::max(0, value));
89 else if (property == kX)
90 bounds->set_x(value);
91 else if (property == kY)
92 bounds->set_y(value);
93 else if (property == kVisibility)
94 *visible = std::max(0, value) == 1;
95 else
96 return Response::Error("Unsupported property=" + property);
97 }
98 return Response::OK();
99 }
100
101 } // namespace
102
103 AshDevToolsCSSAgent::AshDevToolsCSSAgent(AshDevToolsDOMAgent* dom_agent)
104 : dom_agent_(dom_agent) {
105 DCHECK(dom_agent_);
106 }
107
108 AshDevToolsCSSAgent::~AshDevToolsCSSAgent() {
109 disable();
110 }
111
112 ui::devtools::protocol::Response AshDevToolsCSSAgent::enable() {
113 dom_agent_->AddObserver(this);
114 return ui::devtools::protocol::Response::OK();
115 }
116
117 ui::devtools::protocol::Response AshDevToolsCSSAgent::disable() {
118 dom_agent_->RemoveObserver(this);
119 return ui::devtools::protocol::Response::OK();
120 }
121
122 ui::devtools::protocol::Response AshDevToolsCSSAgent::getMatchedStylesForNode(
123 int node_id,
124 ui::devtools::protocol::Maybe<ui::devtools::protocol::CSS::CSSStyle>*
125 inline_style) {
126 *inline_style = GetStylesForNode(node_id);
127 if (!inline_style)
128 return NodeNotFoundError(node_id);
129 return ui::devtools::protocol::Response::OK();
130 }
131
132 ui::devtools::protocol::Response AshDevToolsCSSAgent::setStyleTexts(
133 std::unique_ptr<ui::devtools::protocol::Array<
134 ui::devtools::protocol::CSS::StyleDeclarationEdit>> edits,
135 std::unique_ptr<
136 ui::devtools::protocol::Array<ui::devtools::protocol::CSS::CSSStyle>>*
137 result) {
138 std::unique_ptr<
139 ui::devtools::protocol::Array<ui::devtools::protocol::CSS::CSSStyle>>
140 updated_styles = ui::devtools::protocol::Array<
141 ui::devtools::protocol::CSS::CSSStyle>::create();
142 for (size_t i = 0; i < edits->length(); i++) {
143 auto* edit = edits->get(i);
144 int node_id;
145 if (!base::StringToInt(edit->getStyleSheetId(), &node_id))
146 return ui::devtools::protocol::Response::Error("Invalid node id");
147
148 gfx::Rect updated_bounds;
149 bool visible = false;
150 if (!GetPropertiesForNodeId(node_id, &updated_bounds, &visible))
151 return NodeNotFoundError(node_id);
152
153 ui::devtools::protocol::Response response(
154 ParseProperties(edit->getText(), &updated_bounds, &visible));
155 if (!response.isSuccess())
156 return response;
157
158 updated_styles->addItem(BuildCSSStyle(node_id, updated_bounds, visible));
159
160 if (!SetPropertiesForNodeId(node_id, updated_bounds, visible))
161 return NodeNotFoundError(node_id);
162 }
163 *result = std::move(updated_styles);
164 return ui::devtools::protocol::Response::OK();
165 }
166
167 void AshDevToolsCSSAgent::OnNodeBoundsChanged(int node_id) {
168 InvalidateStyleSheet(node_id);
169 }
170
171 std::unique_ptr<ui::devtools::protocol::CSS::CSSStyle>
172 AshDevToolsCSSAgent::GetStylesForNode(int node_id) {
173 gfx::Rect bounds;
174 bool visible = false;
175 return GetPropertiesForNodeId(node_id, &bounds, &visible)
176 ? BuildCSSStyle(node_id, bounds, visible)
177 : nullptr;
178 }
179
180 void AshDevToolsCSSAgent::InvalidateStyleSheet(int node_id) {
181 // The stylesheetId for each node is equivalent to its node_id (as a string).
182 frontend()->styleSheetChanged(base::IntToString(node_id));
183 }
184
185 bool AshDevToolsCSSAgent::GetPropertiesForNodeId(int node_id,
186 gfx::Rect* bounds,
187 bool* visible) {
188 UIElement* ui_element = dom_agent_->GetElementFromNodeId(node_id);
189 if (ui_element) {
190 ui_element->GetBounds(bounds);
191 ui_element->GetVisible(visible);
192 return true;
193 }
194 return false;
195 }
196
197 bool AshDevToolsCSSAgent::SetPropertiesForNodeId(int node_id,
198 const gfx::Rect& bounds,
199 bool visible) {
200 UIElement* ui_element = dom_agent_->GetElementFromNodeId(node_id);
201 if (ui_element) {
202 ui_element->SetBounds(bounds);
203 ui_element->SetVisible(visible);
204 return true;
205 }
206 return false;
207 }
208
209 } // namespace devtools
210 } // namespace ash
OLDNEW
« no previous file with comments | « ash/devtools/ash_devtools_css_agent.h ('k') | ash/devtools/ash_devtools_dom_agent.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698