Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / ash / touch / touch_hud_debug.cc
blobbac5c7de1a449ff715f7dc4c1e92d1bb3ce80fc0
1 // Copyright 2013 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.
5 #include "ash/touch/touch_hud_debug.h"
7 #include "ash/display/display_manager.h"
8 #include "ash/root_window_controller.h"
9 #include "ash/shell.h"
10 #include "base/json/json_string_value_serializer.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "third_party/skia/include/core/SkPath.h"
15 #include "ui/aura/window_event_dispatcher.h"
16 #include "ui/events/event.h"
17 #include "ui/gfx/animation/animation_delegate.h"
18 #include "ui/gfx/canvas.h"
19 #include "ui/gfx/display.h"
20 #include "ui/gfx/geometry/size.h"
21 #include "ui/gfx/geometry/vector2d.h"
22 #include "ui/gfx/transform.h"
23 #include "ui/views/controls/label.h"
24 #include "ui/views/layout/box_layout.h"
25 #include "ui/views/widget/widget.h"
27 #if defined(USE_X11)
28 #include <X11/extensions/XInput2.h>
29 #include <X11/Xlib.h>
31 #include "ui/events/devices/x11/device_data_manager_x11.h"
32 #endif
34 namespace ash {
36 const int kPointRadius = 20;
37 const SkColor kColors[] = {
38 SK_ColorYELLOW,
39 SK_ColorGREEN,
40 SK_ColorRED,
41 SK_ColorBLUE,
42 SK_ColorGRAY,
43 SK_ColorMAGENTA,
44 SK_ColorCYAN,
45 SK_ColorWHITE,
46 SK_ColorBLACK,
47 SkColorSetRGB(0xFF, 0x8C, 0x00),
48 SkColorSetRGB(0x8B, 0x45, 0x13),
49 SkColorSetRGB(0xFF, 0xDE, 0xAD),
51 const int kAlpha = 0x60;
52 const int kMaxPaths = arraysize(kColors);
53 const int kReducedScale = 10;
55 const char* GetTouchEventLabel(ui::EventType type) {
56 switch (type) {
57 case ui::ET_UNKNOWN:
58 return " ";
59 case ui::ET_TOUCH_PRESSED:
60 return "P";
61 case ui::ET_TOUCH_MOVED:
62 return "M";
63 case ui::ET_TOUCH_RELEASED:
64 return "R";
65 case ui::ET_TOUCH_CANCELLED:
66 return "C";
67 default:
68 break;
70 return "?";
73 int GetTrackingId(const ui::TouchEvent& event) {
74 if (!event.HasNativeEvent())
75 return 0;
76 #if defined(USE_X11)
77 ui::DeviceDataManagerX11* manager = ui::DeviceDataManagerX11::GetInstance();
78 double tracking_id;
79 if (manager->GetEventData(*event.native_event(),
80 ui::DeviceDataManagerX11::DT_TOUCH_TRACKING_ID,
81 &tracking_id)) {
82 return static_cast<int>(tracking_id);
84 #endif
85 return 0;
88 // A TouchPointLog represents a single touch-event of a touch point.
89 struct TouchPointLog {
90 public:
91 explicit TouchPointLog(const ui::TouchEvent& touch)
92 : id(touch.touch_id()),
93 type(touch.type()),
94 location(touch.root_location()),
95 timestamp(touch.time_stamp().InMillisecondsF()),
96 radius_x(touch.pointer_details().radius_x()),
97 radius_y(touch.pointer_details().radius_y()),
98 pressure(touch.pointer_details().force()),
99 tracking_id(GetTrackingId(touch)),
100 source_device(touch.source_device_id()) {}
102 // Populates a dictionary value with all the information about the touch
103 // point.
104 scoped_ptr<base::DictionaryValue> GetAsDictionary() const {
105 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
107 value->SetInteger("id", id);
108 value->SetString("type", std::string(GetTouchEventLabel(type)));
109 value->SetString("location", location.ToString());
110 value->SetDouble("timestamp", timestamp);
111 value->SetDouble("radius_x", radius_x);
112 value->SetDouble("radius_y", radius_y);
113 value->SetDouble("pressure", pressure);
114 value->SetInteger("tracking_id", tracking_id);
115 value->SetInteger("source_device", source_device);
117 return value.Pass();
120 int id;
121 ui::EventType type;
122 gfx::Point location;
123 double timestamp;
124 float radius_x;
125 float radius_y;
126 float pressure;
127 int tracking_id;
128 int source_device;
131 // A TouchTrace keeps track of all the touch events of a single touch point
132 // (starting from a touch-press and ending at a touch-release or touch-cancel).
133 class TouchTrace {
134 public:
135 typedef std::vector<TouchPointLog>::iterator iterator;
136 typedef std::vector<TouchPointLog>::const_iterator const_iterator;
137 typedef std::vector<TouchPointLog>::reverse_iterator reverse_iterator;
138 typedef std::vector<TouchPointLog>::const_reverse_iterator
139 const_reverse_iterator;
141 TouchTrace() {
144 void AddTouchPoint(const ui::TouchEvent& touch) {
145 log_.push_back(TouchPointLog(touch));
148 const std::vector<TouchPointLog>& log() const { return log_; }
150 bool active() const {
151 return !log_.empty() && log_.back().type != ui::ET_TOUCH_RELEASED &&
152 log_.back().type != ui::ET_TOUCH_CANCELLED;
155 // Returns a list containing data from all events for the touch point.
156 scoped_ptr<base::ListValue> GetAsList() const {
157 scoped_ptr<base::ListValue> list(new base::ListValue());
158 for (const_iterator i = log_.begin(); i != log_.end(); ++i)
159 list->Append((*i).GetAsDictionary().release());
160 return list.Pass();
163 void Reset() {
164 log_.clear();
167 private:
168 std::vector<TouchPointLog> log_;
170 DISALLOW_COPY_AND_ASSIGN(TouchTrace);
173 // A TouchLog keeps track of all touch events of all touch points.
174 class TouchLog {
175 public:
176 TouchLog() : next_trace_index_(0) {
179 void AddTouchPoint(const ui::TouchEvent& touch) {
180 if (touch.type() == ui::ET_TOUCH_PRESSED)
181 StartTrace(touch);
182 AddToTrace(touch);
185 void Reset() {
186 next_trace_index_ = 0;
187 for (int i = 0; i < kMaxPaths; ++i)
188 traces_[i].Reset();
191 scoped_ptr<base::ListValue> GetAsList() const {
192 scoped_ptr<base::ListValue> list(new base::ListValue());
193 for (int i = 0; i < kMaxPaths; ++i) {
194 if (!traces_[i].log().empty())
195 list->Append(traces_[i].GetAsList().release());
197 return list.Pass();
200 int GetTraceIndex(int touch_id) const {
201 return touch_id_to_trace_index_.at(touch_id);
204 const TouchTrace* traces() const {
205 return traces_;
208 private:
209 void StartTrace(const ui::TouchEvent& touch) {
210 // Find the first inactive spot; otherwise, overwrite the one
211 // |next_trace_index_| is pointing to.
212 int old_trace_index = next_trace_index_;
213 do {
214 if (!traces_[next_trace_index_].active())
215 break;
216 next_trace_index_ = (next_trace_index_ + 1) % kMaxPaths;
217 } while (next_trace_index_ != old_trace_index);
218 int touch_id = touch.touch_id();
219 traces_[next_trace_index_].Reset();
220 touch_id_to_trace_index_[touch_id] = next_trace_index_;
221 next_trace_index_ = (next_trace_index_ + 1) % kMaxPaths;
224 void AddToTrace(const ui::TouchEvent& touch) {
225 int touch_id = touch.touch_id();
226 int trace_index = touch_id_to_trace_index_[touch_id];
227 traces_[trace_index].AddTouchPoint(touch);
230 TouchTrace traces_[kMaxPaths];
231 int next_trace_index_;
233 std::map<int, int> touch_id_to_trace_index_;
235 DISALLOW_COPY_AND_ASSIGN(TouchLog);
238 // TouchHudCanvas draws touch traces in |FULLSCREEN| and |REDUCED_SCALE| modes.
239 class TouchHudCanvas : public views::View {
240 public:
241 explicit TouchHudCanvas(const TouchLog& touch_log)
242 : touch_log_(touch_log),
243 scale_(1) {
244 SetPaintToLayer(true);
245 SetFillsBoundsOpaquely(false);
247 paint_.setStyle(SkPaint::kFill_Style);
250 ~TouchHudCanvas() override {}
252 void SetScale(int scale) {
253 if (scale_ == scale)
254 return;
255 scale_ = scale;
256 gfx::Transform transform;
257 transform.Scale(1. / scale_, 1. / scale_);
258 layer()->SetTransform(transform);
261 int scale() const { return scale_; }
263 void TouchPointAdded(int touch_id) {
264 int trace_index = touch_log_.GetTraceIndex(touch_id);
265 const TouchTrace& trace = touch_log_.traces()[trace_index];
266 const TouchPointLog& point = trace.log().back();
267 if (point.type == ui::ET_TOUCH_PRESSED)
268 StartedTrace(trace_index);
269 if (point.type != ui::ET_TOUCH_CANCELLED)
270 AddedPointToTrace(trace_index);
273 void Clear() {
274 for (int i = 0; i < kMaxPaths; ++i)
275 paths_[i].reset();
277 SchedulePaint();
280 private:
281 void StartedTrace(int trace_index) {
282 paths_[trace_index].reset();
283 colors_[trace_index] = SkColorSetA(kColors[trace_index], kAlpha);
286 void AddedPointToTrace(int trace_index) {
287 const TouchTrace& trace = touch_log_.traces()[trace_index];
288 const TouchPointLog& point = trace.log().back();
289 const gfx::Point& location = point.location;
290 SkScalar x = SkIntToScalar(location.x());
291 SkScalar y = SkIntToScalar(location.y());
292 SkPoint last;
293 if (!paths_[trace_index].getLastPt(&last) || x != last.x() ||
294 y != last.y()) {
295 paths_[trace_index].addCircle(x, y, SkIntToScalar(kPointRadius));
296 SchedulePaint();
300 // Overridden from views::View.
301 void OnPaint(gfx::Canvas* canvas) override {
302 for (int i = 0; i < kMaxPaths; ++i) {
303 if (paths_[i].countPoints() == 0)
304 continue;
305 paint_.setColor(colors_[i]);
306 canvas->DrawPath(paths_[i], paint_);
310 SkPaint paint_;
312 const TouchLog& touch_log_;
313 SkPath paths_[kMaxPaths];
314 SkColor colors_[kMaxPaths];
316 int scale_;
318 DISALLOW_COPY_AND_ASSIGN(TouchHudCanvas);
321 TouchHudDebug::TouchHudDebug(aura::Window* initial_root)
322 : TouchObserverHUD(initial_root),
323 mode_(FULLSCREEN),
324 touch_log_(new TouchLog()),
325 canvas_(NULL),
326 label_container_(NULL) {
327 const gfx::Display& display =
328 Shell::GetInstance()->display_manager()->GetDisplayForId(display_id());
330 views::View* content = widget()->GetContentsView();
332 canvas_ = new TouchHudCanvas(*touch_log_);
333 content->AddChildView(canvas_);
335 const gfx::Size& display_size = display.size();
336 canvas_->SetSize(display_size);
338 label_container_ = new views::View;
339 label_container_->SetLayoutManager(new views::BoxLayout(
340 views::BoxLayout::kVertical, 0, 0, 0));
342 for (int i = 0; i < kMaxTouchPoints; ++i) {
343 touch_labels_[i] = new views::Label;
344 touch_labels_[i]->SetBackgroundColor(SkColorSetARGB(0, 255, 255, 255));
345 touch_labels_[i]->SetShadows(gfx::ShadowValues(
346 1, gfx::ShadowValue(gfx::Vector2d(1, 1), 0, SK_ColorWHITE)));
347 label_container_->AddChildView(touch_labels_[i]);
349 label_container_->SetX(0);
350 label_container_->SetY(display_size.height() / kReducedScale);
351 label_container_->SetSize(label_container_->GetPreferredSize());
352 label_container_->SetVisible(false);
353 content->AddChildView(label_container_);
356 TouchHudDebug::~TouchHudDebug() {
359 // static
360 scoped_ptr<base::DictionaryValue> TouchHudDebug::GetAllAsDictionary() {
361 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
362 aura::Window::Windows roots = Shell::GetInstance()->GetAllRootWindows();
363 for (aura::Window::Windows::iterator iter = roots.begin();
364 iter != roots.end(); ++iter) {
365 RootWindowController* controller = GetRootWindowController(*iter);
366 TouchHudDebug* hud = controller->touch_hud_debug();
367 if (hud) {
368 scoped_ptr<base::ListValue> list = hud->GetLogAsList();
369 if (!list->empty())
370 value->Set(base::Int64ToString(hud->display_id()), list.release());
373 return value.Pass();
376 void TouchHudDebug::ChangeToNextMode() {
377 switch (mode_) {
378 case FULLSCREEN:
379 SetMode(REDUCED_SCALE);
380 break;
381 case REDUCED_SCALE:
382 SetMode(INVISIBLE);
383 break;
384 case INVISIBLE:
385 SetMode(FULLSCREEN);
386 break;
390 scoped_ptr<base::ListValue> TouchHudDebug::GetLogAsList() const {
391 return touch_log_->GetAsList();
394 void TouchHudDebug::Clear() {
395 if (widget()->IsVisible()) {
396 canvas_->Clear();
397 for (int i = 0; i < kMaxTouchPoints; ++i)
398 touch_labels_[i]->SetText(base::string16());
399 label_container_->SetSize(label_container_->GetPreferredSize());
403 void TouchHudDebug::SetMode(Mode mode) {
404 if (mode_ == mode)
405 return;
406 mode_ = mode;
407 switch (mode) {
408 case FULLSCREEN:
409 label_container_->SetVisible(false);
410 canvas_->SetVisible(true);
411 canvas_->SetScale(1);
412 canvas_->SchedulePaint();
413 widget()->Show();
414 break;
415 case REDUCED_SCALE:
416 label_container_->SetVisible(true);
417 canvas_->SetVisible(true);
418 canvas_->SetScale(kReducedScale);
419 canvas_->SchedulePaint();
420 widget()->Show();
421 break;
422 case INVISIBLE:
423 widget()->Hide();
424 break;
428 void TouchHudDebug::UpdateTouchPointLabel(int index) {
429 int trace_index = touch_log_->GetTraceIndex(index);
430 const TouchTrace& trace = touch_log_->traces()[trace_index];
431 TouchTrace::const_reverse_iterator point = trace.log().rbegin();
432 ui::EventType touch_status = point->type;
433 float touch_radius = std::max(point->radius_x, point->radius_y);
434 while (point != trace.log().rend() && point->type == ui::ET_TOUCH_CANCELLED)
435 point++;
436 DCHECK(point != trace.log().rend());
437 gfx::Point touch_position = point->location;
439 std::string string = base::StringPrintf("%2d: %s %s (%.4f)",
440 index,
441 GetTouchEventLabel(touch_status),
442 touch_position.ToString().c_str(),
443 touch_radius);
444 touch_labels_[index]->SetText(base::UTF8ToUTF16(string));
447 void TouchHudDebug::OnTouchEvent(ui::TouchEvent* event) {
448 if (event->touch_id() >= kMaxTouchPoints)
449 return;
451 touch_log_->AddTouchPoint(*event);
452 canvas_->TouchPointAdded(event->touch_id());
453 UpdateTouchPointLabel(event->touch_id());
454 label_container_->SetSize(label_container_->GetPreferredSize());
457 void TouchHudDebug::OnDisplayMetricsChanged(const gfx::Display& display,
458 uint32_t metrics) {
459 TouchObserverHUD::OnDisplayMetricsChanged(display, metrics);
461 if (display.id() != display_id() || !(metrics & DISPLAY_METRIC_BOUNDS))
462 return;
463 const gfx::Size& size = display.size();
464 canvas_->SetSize(size);
465 label_container_->SetY(size.height() / kReducedScale);
468 void TouchHudDebug::SetHudForRootWindowController(
469 RootWindowController* controller) {
470 controller->set_touch_hud_debug(this);
473 void TouchHudDebug::UnsetHudForRootWindowController(
474 RootWindowController* controller) {
475 controller->set_touch_hud_debug(NULL);
478 } // namespace ash