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

Side by Side Diff: ash/common/devtools/ash_devtools_css_agent.cc

Issue 2736573002: chromeos: Move files in //ash/common to //ash, part 2 (Closed)
Patch Set: Created 3 years, 9 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
(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/common/devtools/ash_devtools_css_agent.h"
6
7 #include "ash/common/wm_window.h"
8 #include "base/strings/string_split.h"
9 #include "base/strings/string_util.h"
10
11 namespace ash {
12 namespace devtools {
13
14 namespace {
15 using namespace ui::devtools::protocol;
16
17 const char kHeight[] = "height";
18 const char kWidth[] = "width";
19 const char kX[] = "x";
20 const char kY[] = "y";
21
22 std::unique_ptr<CSS::SourceRange> BuildDefaultSourceRange() {
23 // 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 // We need this because CSS fields are not editable unless
26 // the range is provided.
27 return CSS::SourceRange::create()
28 .setStartLine(0)
29 .setEndLine(0)
30 .setStartColumn(0)
31 .setEndColumn(0)
32 .build();
33 }
34
35 std::unique_ptr<CSS::CSSProperty> BuildCSSProperty(const std::string& name,
36 int value) {
37 return CSS::CSSProperty::create()
38 .setRange(BuildDefaultSourceRange())
39 .setName(name)
40 .setValue(base::IntToString(value))
41 .build();
42 }
43
44 std::unique_ptr<Array<CSS::CSSProperty>> BuildBoundsCSSPropertyArray(
45 const gfx::Rect& bounds) {
46 auto cssProperties = Array<CSS::CSSProperty>::create();
47 cssProperties->addItem(BuildCSSProperty(kHeight, bounds.height()));
48 cssProperties->addItem(BuildCSSProperty(kWidth, bounds.width()));
49 cssProperties->addItem(BuildCSSProperty(kX, bounds.x()));
50 cssProperties->addItem(BuildCSSProperty(kY, bounds.y()));
51 return cssProperties;
52 }
53
54 std::unique_ptr<CSS::CSSStyle> BuildCSSStyle(int node_id,
55 const gfx::Rect& bounds) {
56 return CSS::CSSStyle::create()
57 .setRange(BuildDefaultSourceRange())
58 .setStyleSheetId(base::IntToString(node_id))
59 .setCssProperties(BuildBoundsCSSPropertyArray(bounds))
60 .setShorthandEntries(Array<std::string>::create())
61 .build();
62 }
63
64 Response ParseBounds(const std::string& style_text, gfx::Rect& bounds) {
65 std::vector<std::string> tokens = base::SplitString(
66 style_text, ":;", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
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 }
86
87 } // namespace
88
89 AshDevToolsCSSAgent::AshDevToolsCSSAgent(AshDevToolsDOMAgent* dom_agent)
90 : dom_agent_(dom_agent) {
91 DCHECK(dom_agent_);
92 }
93
94 AshDevToolsCSSAgent::~AshDevToolsCSSAgent() {
95 disable();
96 }
97
98 ui::devtools::protocol::Response AshDevToolsCSSAgent::enable() {
99 dom_agent_->AddObserver(this);
100 return ui::devtools::protocol::Response::OK();
101 }
102
103 ui::devtools::protocol::Response AshDevToolsCSSAgent::disable() {
104 dom_agent_->RemoveObserver(this);
105 return ui::devtools::protocol::Response::OK();
106 }
107
108 ui::devtools::protocol::Response AshDevToolsCSSAgent::getMatchedStylesForNode(
109 int node_id,
110 ui::devtools::protocol::Maybe<ui::devtools::protocol::CSS::CSSStyle>*
111 inline_style) {
112 *inline_style = GetStylesForNode(node_id);
113 if (!inline_style) {
114 return ui::devtools::protocol::Response::Error(
115 "Node with that id not found");
116 }
117 return ui::devtools::protocol::Response::OK();
118 }
119
120 ui::devtools::protocol::Response AshDevToolsCSSAgent::setStyleTexts(
121 std::unique_ptr<ui::devtools::protocol::Array<
122 ui::devtools::protocol::CSS::StyleDeclarationEdit>> edits,
123 std::unique_ptr<
124 ui::devtools::protocol::Array<ui::devtools::protocol::CSS::CSSStyle>>*
125 result) {
126 std::unique_ptr<
127 ui::devtools::protocol::Array<ui::devtools::protocol::CSS::CSSStyle>>
128 updated_styles = ui::devtools::protocol::Array<
129 ui::devtools::protocol::CSS::CSSStyle>::create();
130 for (size_t i = 0; i < edits->length(); i++) {
131 auto* edit = edits->get(i);
132 int node_id;
133 if (!base::StringToInt(edit->getStyleSheetId(), &node_id))
134 return ui::devtools::protocol::Response::Error("Invalid node id");
135
136 gfx::Rect updated_bounds;
137 if (!GetBoundsForNodeId(node_id, &updated_bounds)) {
138 return ui::devtools::protocol::Response::Error(
139 "No node found with that id");
140 }
141
142 ui::devtools::protocol::Response response(
143 ParseBounds(edit->getText(), updated_bounds));
144 if (!response.isSuccess())
145 return response;
146
147 updated_styles->addItem(BuildCSSStyle(node_id, updated_bounds));
148 if (!UpdateBounds(node_id, updated_bounds)) {
149 return ui::devtools::protocol::Response::Error(
150 "No node found with that id");
151 }
152 }
153
154 *result = std::move(updated_styles);
155 return ui::devtools::protocol::Response::OK();
156 }
157
158 void AshDevToolsCSSAgent::OnWindowBoundsChanged(WmWindow* window) {
159 InvalidateStyleSheet(dom_agent_->GetNodeIdFromWindow(window));
160 }
161
162 void AshDevToolsCSSAgent::OnWidgetBoundsChanged(views::Widget* widget) {
163 InvalidateStyleSheet(dom_agent_->GetNodeIdFromWidget(widget));
164 }
165
166 void AshDevToolsCSSAgent::OnViewBoundsChanged(views::View* view) {
167 InvalidateStyleSheet(dom_agent_->GetNodeIdFromView(view));
168 }
169
170 std::unique_ptr<ui::devtools::protocol::CSS::CSSStyle>
171 AshDevToolsCSSAgent::GetStylesForNode(int node_id) {
172 gfx::Rect bounds;
173 return GetBoundsForNodeId(node_id, &bounds) ? BuildCSSStyle(node_id, bounds)
174 : nullptr;
175 }
176
177 void AshDevToolsCSSAgent::InvalidateStyleSheet(int node_id) {
178 // The stylesheetId for each node is equivalent to its node_id (as a string).
179 frontend()->styleSheetChanged(base::IntToString(node_id));
180 }
181
182 bool AshDevToolsCSSAgent::GetBoundsForNodeId(int node_id, gfx::Rect* bounds) {
183 WmWindow* window = dom_agent_->GetWindowFromNodeId(node_id);
184 if (window) {
185 *bounds = window->GetBounds();
186 return true;
187 }
188
189 views::Widget* widget = dom_agent_->GetWidgetFromNodeId(node_id);
190 if (widget) {
191 *bounds = widget->GetRestoredBounds();
192 return true;
193 }
194
195 views::View* view = dom_agent_->GetViewFromNodeId(node_id);
196 if (view) {
197 *bounds = view->bounds();
198 return true;
199 }
200
201 return false;
202 }
203
204 bool AshDevToolsCSSAgent::UpdateBounds(int node_id, const gfx::Rect& bounds) {
205 WmWindow* window = dom_agent_->GetWindowFromNodeId(node_id);
206 if (window) {
207 window->SetBounds(bounds);
208 return true;
209 }
210
211 views::Widget* widget = dom_agent_->GetWidgetFromNodeId(node_id);
212 if (widget) {
213 widget->SetBounds(bounds);
214 return true;
215 }
216
217 views::View* view = dom_agent_->GetViewFromNodeId(node_id);
218 if (view) {
219 view->SetBoundsRect(bounds);
220 return true;
221 }
222
223 return false;
224 }
225
226 } // namespace devtools
227 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/devtools/ash_devtools_css_agent.h ('k') | ash/common/devtools/ash_devtools_dom_agent.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698