blob: e54ec8923e087eacaf084848d03b07f19e331e31 [file] [log] [blame]
[email protected]e607e652013-09-25 01:08:391// 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.
4
James Cook643b7182017-03-05 22:02:585#include "ash/common/accelerators/debug_commands.h"
[email protected]e607e652013-09-25 01:08:396
James Cook643b7182017-03-05 22:02:587#include "ash/common/accelerators/accelerator_commands.h"
James Cook6316a552017-03-05 21:46:218#include "ash/common/ash_switches.h"
9#include "ash/common/shell_delegate.h"
10#include "ash/common/wallpaper/wallpaper_controller.h"
11#include "ash/common/wallpaper/wallpaper_delegate.h"
James Cook643b7182017-03-05 22:02:5812#include "ash/common/wm/maximize_mode/maximize_mode_controller.h"
James Cook6316a552017-03-05 21:46:2113#include "ash/common/wm_shell.h"
14#include "ash/common/wm_window.h"
skyb6643832017-01-11 22:08:4515#include "ash/root_window_controller.h"
jamescooka0c44222017-03-05 02:09:2816#include "ash/system/toast/toast_data.h"
17#include "ash/system/toast/toast_manager.h"
yiyixa12aab12017-03-03 21:04:3418#include "ash/wm/window_properties.h"
pkotwicz01abb76a2014-10-23 02:48:3119#include "base/command_line.h"
afakhrye8eac602015-11-10 02:31:2220#include "base/metrics/user_metrics.h"
21#include "base/metrics/user_metrics_action.h"
jdufault8c170d52016-08-25 21:03:4022#include "base/strings/utf_string_conversions.h"
pkotwicz01abb76a2014-10-23 02:48:3123#include "ui/compositor/debug_utils.h"
oshima37ae0872017-01-20 00:45:4524#include "ui/compositor/layer.h"
msw3f439af2016-09-08 22:35:2625#include "ui/gfx/canvas.h"
26#include "ui/gfx/image/image_skia.h"
pkotwicz01abb76a2014-10-23 02:48:3127#include "ui/views/debug_utils.h"
28#include "ui/views/widget/widget.h"
[email protected]e607e652013-09-25 01:08:3929
30namespace ash {
31namespace debug {
32namespace {
33
pkotwicz01abb76a2014-10-23 02:48:3134void HandlePrintLayerHierarchy() {
sky3018a072016-07-21 03:08:0735 for (WmWindow* root : WmShell::Get()->GetAllRootWindows()) {
36 ui::Layer* layer = root->GetLayer();
37 if (layer)
38 ui::PrintLayerHierarchy(
39 layer, root->GetRootWindowController()->GetLastMouseLocationInRoot());
pkotwicz01abb76a2014-10-23 02:48:3140 }
41}
42
43void HandlePrintViewHierarchy() {
sky3018a072016-07-21 03:08:0744 WmWindow* active_window = WmShell::Get()->GetActiveWindow();
pkotwicz01abb76a2014-10-23 02:48:3145 if (!active_window)
46 return;
sky3018a072016-07-21 03:08:0747 views::Widget* widget = active_window->GetInternalWidget();
48 if (!widget)
pkotwicz01abb76a2014-10-23 02:48:3149 return;
sky3018a072016-07-21 03:08:0750 views::PrintViewHierarchy(widget->GetRootView());
pkotwicz01abb76a2014-10-23 02:48:3151}
52
sky3018a072016-07-21 03:08:0753void PrintWindowHierarchy(const WmWindow* active_window,
54 WmWindow* window,
pkotwicz01abb76a2014-10-23 02:48:3155 int indent,
56 std::ostringstream* out) {
57 std::string indent_str(indent, ' ');
sky3018a072016-07-21 03:08:0758 std::string name(window->GetName());
pkotwicz01abb76a2014-10-23 02:48:3159 if (name.empty())
60 name = "\"\"";
61 *out << indent_str << name << " (" << window << ")"
sky3018a072016-07-21 03:08:0762 << " type=" << window->GetType()
63 << ((window == active_window) ? " [active] " : " ")
pkotwicz01abb76a2014-10-23 02:48:3164 << (window->IsVisible() ? " visible " : " ")
oshima37ae0872017-01-20 00:45:4565 << window->GetBounds().ToString()
yiyixa12aab12017-03-03 21:04:3466 << (window->aura_window()->GetProperty(kSnapChildrenToPixelBoundary)
oshima37ae0872017-01-20 00:45:4567 ? " [snapped] "
68 : "")
69 << ", subpixel offset="
70 << window->GetLayer()->subpixel_position_offset().ToString() << '\n';
pkotwicz01abb76a2014-10-23 02:48:3171
sky3018a072016-07-21 03:08:0772 for (WmWindow* child : window->GetChildren())
73 PrintWindowHierarchy(active_window, child, indent + 3, out);
pkotwicz01abb76a2014-10-23 02:48:3174}
75
76void HandlePrintWindowHierarchy() {
sky3018a072016-07-21 03:08:0777 WmWindow* active_window = WmShell::Get()->GetActiveWindow();
78 WmWindow::Windows roots = WmShell::Get()->GetAllRootWindows();
79 for (size_t i = 0; i < roots.size(); ++i) {
pkotwicz01abb76a2014-10-23 02:48:3180 std::ostringstream out;
81 out << "RootWindow " << i << ":\n";
sky3018a072016-07-21 03:08:0782 PrintWindowHierarchy(active_window, roots[i], 0, &out);
pkotwicz01abb76a2014-10-23 02:48:3183 // Error so logs can be collected from end-users.
84 LOG(ERROR) << out.str();
85 }
86}
87
msw3f439af2016-09-08 22:35:2688gfx::ImageSkia CreateWallpaperImage(SkColor fill, SkColor rect) {
89 // TODO(oshima): Consider adding a command line option to control wallpaper
90 // images for testing. The size is randomly picked.
91 gfx::Size image_size(1366, 768);
92 gfx::Canvas canvas(image_size, 1.0f, true);
93 canvas.DrawColor(fill);
enne00bd4b72017-02-08 01:38:1494 cc::PaintFlags flags;
95 flags.setColor(rect);
96 flags.setStrokeWidth(10);
97 flags.setStyle(cc::PaintFlags::kStroke_Style);
98 flags.setBlendMode(SkBlendMode::kSrcOver);
99 canvas.DrawRoundRect(gfx::Rect(image_size), 100, flags);
msw3f439af2016-09-08 22:35:26100 return gfx::ImageSkia(canvas.ExtractImageRep());
101}
102
103void HandleToggleWallpaperMode() {
104 static int index = 0;
105 WallpaperController* wallpaper_controller =
106 WmShell::Get()->wallpaper_controller();
107 switch (++index % 4) {
108 case 0:
109 ash::WmShell::Get()->wallpaper_delegate()->InitializeWallpaper();
110 break;
111 case 1:
112 wallpaper_controller->SetWallpaperImage(
113 CreateWallpaperImage(SK_ColorRED, SK_ColorBLUE),
114 wallpaper::WALLPAPER_LAYOUT_STRETCH);
115 break;
116 case 2:
117 wallpaper_controller->SetWallpaperImage(
118 CreateWallpaperImage(SK_ColorBLUE, SK_ColorGREEN),
119 wallpaper::WALLPAPER_LAYOUT_CENTER);
120 break;
121 case 3:
122 wallpaper_controller->SetWallpaperImage(
123 CreateWallpaperImage(SK_ColorGREEN, SK_ColorRED),
124 wallpaper::WALLPAPER_LAYOUT_CENTER_CROPPED);
125 break;
126 }
127}
128
afakhrye8eac602015-11-10 02:31:22129void HandleToggleTouchpad() {
130 base::RecordAction(base::UserMetricsAction("Accel_Toggle_Touchpad"));
msw0414d4122016-07-06 22:58:48131 ash::WmShell::Get()->delegate()->ToggleTouchpad();
afakhrye8eac602015-11-10 02:31:22132}
133
134void HandleToggleTouchscreen() {
135 base::RecordAction(base::UserMetricsAction("Accel_Toggle_Touchscreen"));
warx4c8baedb2016-12-03 22:38:50136 ShellDelegate* delegate = WmShell::Get()->delegate();
137 delegate->SetTouchscreenEnabledInPrefs(
138 !delegate->IsTouchscreenEnabledInPrefs(false /* use_local_state */),
139 false /* use_local_state */);
140 delegate->UpdateTouchscreenStatusFromPrefs();
afakhrye8eac602015-11-10 02:31:22141}
142
afakhrycd09f64d2016-09-16 22:26:56143void HandleToggleTouchView() {
oshima1d55f402016-06-24 07:13:51144 MaximizeModeController* controller =
skyc68696c2016-07-01 21:06:02145 WmShell::Get()->maximize_mode_controller();
oshima1d55f402016-06-24 07:13:51146 controller->EnableMaximizeModeWindowManager(
147 !controller->IsMaximizeModeWindowManagerEnabled());
148}
149
jamescook192db0bd2017-01-09 19:31:29150void HandleTriggerCrash() {
151 CHECK(false) << "Intentional crash via debug accelerator.";
152}
153
pkotwicz01abb76a2014-10-23 02:48:31154} // namespace
155
156void PrintUIHierarchies() {
157 // This is a separate command so the user only has to hit one key to generate
158 // all the logs. Developers use the individual dumps repeatedly, so keep
159 // those as separate commands to avoid spamming their logs.
160 HandlePrintLayerHierarchy();
161 HandlePrintWindowHierarchy();
162 HandlePrintViewHierarchy();
163}
164
165bool DebugAcceleratorsEnabled() {
pgal.u-szegedd84534d32014-10-29 12:34:30166 return base::CommandLine::ForCurrentProcess()->HasSwitch(
pkotwicz01abb76a2014-10-23 02:48:31167 switches::kAshDebugShortcuts);
168}
169
afakhrycd09f64d2016-09-16 22:26:56170bool DeveloperAcceleratorsEnabled() {
171 return base::CommandLine::ForCurrentProcess()->HasSwitch(
172 switches::kAshDeveloperShortcuts);
173}
174
pkotwicz8723017c2014-12-09 00:51:24175void PerformDebugActionIfEnabled(AcceleratorAction action) {
pkotwicz01abb76a2014-10-23 02:48:31176 if (!DebugAcceleratorsEnabled())
pkotwicz8723017c2014-12-09 00:51:24177 return;
pkotwicz01abb76a2014-10-23 02:48:31178
179 switch (action) {
jamescook6ed406b72017-02-13 18:31:13180 case DEBUG_PRINT_LAYER_HIERARCHY:
181 HandlePrintLayerHierarchy();
182 break;
183 case DEBUG_PRINT_VIEW_HIERARCHY:
184 HandlePrintViewHierarchy();
185 break;
186 case DEBUG_PRINT_WINDOW_HIERARCHY:
187 HandlePrintWindowHierarchy();
188 break;
jamescook9fb7aa12016-06-24 22:22:28189 case DEBUG_SHOW_TOAST:
sky004dce532016-07-20 21:28:27190 WmShell::Get()->toast_manager()->Show(
jdufault8c170d52016-08-25 21:03:40191 ToastData("id", base::ASCIIToUTF16("Toast"), 5000 /* duration_ms */,
192 base::ASCIIToUTF16("Dismiss")));
jamescook9fb7aa12016-06-24 22:22:28193 break;
afakhrye8eac602015-11-10 02:31:22194 case DEBUG_TOGGLE_TOUCH_PAD:
195 HandleToggleTouchpad();
196 break;
197 case DEBUG_TOGGLE_TOUCH_SCREEN:
198 HandleToggleTouchscreen();
199 break;
oshima1d55f402016-06-24 07:13:51200 case DEBUG_TOGGLE_TOUCH_VIEW:
afakhrycd09f64d2016-09-16 22:26:56201 HandleToggleTouchView();
oshima1d55f402016-06-24 07:13:51202 break;
msw3f439af2016-09-08 22:35:26203 case DEBUG_TOGGLE_WALLPAPER_MODE:
204 HandleToggleWallpaperMode();
205 break;
jamescook192db0bd2017-01-09 19:31:29206 case DEBUG_TRIGGER_CRASH:
207 HandleTriggerCrash();
208 break;
pkotwicz01abb76a2014-10-23 02:48:31209 default:
pkotwicz8723017c2014-12-09 00:51:24210 break;
pkotwicz01abb76a2014-10-23 02:48:31211 }
[email protected]e607e652013-09-25 01:08:39212}
213
214} // namespace debug
215} // namespace ash