[email protected] | e607e65 | 2013-09-25 01:08:39 | [diff] [blame] | 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. |
| 4 | |
James Cook | 643b718 | 2017-03-05 22:02:58 | [diff] [blame^] | 5 | #include "ash/common/accelerators/debug_commands.h" |
[email protected] | e607e65 | 2013-09-25 01:08:39 | [diff] [blame] | 6 | |
James Cook | 643b718 | 2017-03-05 22:02:58 | [diff] [blame^] | 7 | #include "ash/common/accelerators/accelerator_commands.h" |
James Cook | 6316a55 | 2017-03-05 21:46:21 | [diff] [blame] | 8 | #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 Cook | 643b718 | 2017-03-05 22:02:58 | [diff] [blame^] | 12 | #include "ash/common/wm/maximize_mode/maximize_mode_controller.h" |
James Cook | 6316a55 | 2017-03-05 21:46:21 | [diff] [blame] | 13 | #include "ash/common/wm_shell.h" |
| 14 | #include "ash/common/wm_window.h" |
sky | b664383 | 2017-01-11 22:08:45 | [diff] [blame] | 15 | #include "ash/root_window_controller.h" |
jamescook | a0c4422 | 2017-03-05 02:09:28 | [diff] [blame] | 16 | #include "ash/system/toast/toast_data.h" |
| 17 | #include "ash/system/toast/toast_manager.h" |
yiyix | a12aab1 | 2017-03-03 21:04:34 | [diff] [blame] | 18 | #include "ash/wm/window_properties.h" |
pkotwicz | 01abb76a | 2014-10-23 02:48:31 | [diff] [blame] | 19 | #include "base/command_line.h" |
afakhry | e8eac60 | 2015-11-10 02:31:22 | [diff] [blame] | 20 | #include "base/metrics/user_metrics.h" |
| 21 | #include "base/metrics/user_metrics_action.h" |
jdufault | 8c170d5 | 2016-08-25 21:03:40 | [diff] [blame] | 22 | #include "base/strings/utf_string_conversions.h" |
pkotwicz | 01abb76a | 2014-10-23 02:48:31 | [diff] [blame] | 23 | #include "ui/compositor/debug_utils.h" |
oshima | 37ae087 | 2017-01-20 00:45:45 | [diff] [blame] | 24 | #include "ui/compositor/layer.h" |
msw | 3f439af | 2016-09-08 22:35:26 | [diff] [blame] | 25 | #include "ui/gfx/canvas.h" |
| 26 | #include "ui/gfx/image/image_skia.h" |
pkotwicz | 01abb76a | 2014-10-23 02:48:31 | [diff] [blame] | 27 | #include "ui/views/debug_utils.h" |
| 28 | #include "ui/views/widget/widget.h" |
[email protected] | e607e65 | 2013-09-25 01:08:39 | [diff] [blame] | 29 | |
| 30 | namespace ash { |
| 31 | namespace debug { |
| 32 | namespace { |
| 33 | |
pkotwicz | 01abb76a | 2014-10-23 02:48:31 | [diff] [blame] | 34 | void HandlePrintLayerHierarchy() { |
sky | 3018a07 | 2016-07-21 03:08:07 | [diff] [blame] | 35 | for (WmWindow* root : WmShell::Get()->GetAllRootWindows()) { |
| 36 | ui::Layer* layer = root->GetLayer(); |
| 37 | if (layer) |
| 38 | ui::PrintLayerHierarchy( |
| 39 | layer, root->GetRootWindowController()->GetLastMouseLocationInRoot()); |
pkotwicz | 01abb76a | 2014-10-23 02:48:31 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
| 43 | void HandlePrintViewHierarchy() { |
sky | 3018a07 | 2016-07-21 03:08:07 | [diff] [blame] | 44 | WmWindow* active_window = WmShell::Get()->GetActiveWindow(); |
pkotwicz | 01abb76a | 2014-10-23 02:48:31 | [diff] [blame] | 45 | if (!active_window) |
| 46 | return; |
sky | 3018a07 | 2016-07-21 03:08:07 | [diff] [blame] | 47 | views::Widget* widget = active_window->GetInternalWidget(); |
| 48 | if (!widget) |
pkotwicz | 01abb76a | 2014-10-23 02:48:31 | [diff] [blame] | 49 | return; |
sky | 3018a07 | 2016-07-21 03:08:07 | [diff] [blame] | 50 | views::PrintViewHierarchy(widget->GetRootView()); |
pkotwicz | 01abb76a | 2014-10-23 02:48:31 | [diff] [blame] | 51 | } |
| 52 | |
sky | 3018a07 | 2016-07-21 03:08:07 | [diff] [blame] | 53 | void PrintWindowHierarchy(const WmWindow* active_window, |
| 54 | WmWindow* window, |
pkotwicz | 01abb76a | 2014-10-23 02:48:31 | [diff] [blame] | 55 | int indent, |
| 56 | std::ostringstream* out) { |
| 57 | std::string indent_str(indent, ' '); |
sky | 3018a07 | 2016-07-21 03:08:07 | [diff] [blame] | 58 | std::string name(window->GetName()); |
pkotwicz | 01abb76a | 2014-10-23 02:48:31 | [diff] [blame] | 59 | if (name.empty()) |
| 60 | name = "\"\""; |
| 61 | *out << indent_str << name << " (" << window << ")" |
sky | 3018a07 | 2016-07-21 03:08:07 | [diff] [blame] | 62 | << " type=" << window->GetType() |
| 63 | << ((window == active_window) ? " [active] " : " ") |
pkotwicz | 01abb76a | 2014-10-23 02:48:31 | [diff] [blame] | 64 | << (window->IsVisible() ? " visible " : " ") |
oshima | 37ae087 | 2017-01-20 00:45:45 | [diff] [blame] | 65 | << window->GetBounds().ToString() |
yiyix | a12aab1 | 2017-03-03 21:04:34 | [diff] [blame] | 66 | << (window->aura_window()->GetProperty(kSnapChildrenToPixelBoundary) |
oshima | 37ae087 | 2017-01-20 00:45:45 | [diff] [blame] | 67 | ? " [snapped] " |
| 68 | : "") |
| 69 | << ", subpixel offset=" |
| 70 | << window->GetLayer()->subpixel_position_offset().ToString() << '\n'; |
pkotwicz | 01abb76a | 2014-10-23 02:48:31 | [diff] [blame] | 71 | |
sky | 3018a07 | 2016-07-21 03:08:07 | [diff] [blame] | 72 | for (WmWindow* child : window->GetChildren()) |
| 73 | PrintWindowHierarchy(active_window, child, indent + 3, out); |
pkotwicz | 01abb76a | 2014-10-23 02:48:31 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void HandlePrintWindowHierarchy() { |
sky | 3018a07 | 2016-07-21 03:08:07 | [diff] [blame] | 77 | WmWindow* active_window = WmShell::Get()->GetActiveWindow(); |
| 78 | WmWindow::Windows roots = WmShell::Get()->GetAllRootWindows(); |
| 79 | for (size_t i = 0; i < roots.size(); ++i) { |
pkotwicz | 01abb76a | 2014-10-23 02:48:31 | [diff] [blame] | 80 | std::ostringstream out; |
| 81 | out << "RootWindow " << i << ":\n"; |
sky | 3018a07 | 2016-07-21 03:08:07 | [diff] [blame] | 82 | PrintWindowHierarchy(active_window, roots[i], 0, &out); |
pkotwicz | 01abb76a | 2014-10-23 02:48:31 | [diff] [blame] | 83 | // Error so logs can be collected from end-users. |
| 84 | LOG(ERROR) << out.str(); |
| 85 | } |
| 86 | } |
| 87 | |
msw | 3f439af | 2016-09-08 22:35:26 | [diff] [blame] | 88 | gfx::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); |
enne | 00bd4b7 | 2017-02-08 01:38:14 | [diff] [blame] | 94 | 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); |
msw | 3f439af | 2016-09-08 22:35:26 | [diff] [blame] | 100 | return gfx::ImageSkia(canvas.ExtractImageRep()); |
| 101 | } |
| 102 | |
| 103 | void 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 | |
afakhry | e8eac60 | 2015-11-10 02:31:22 | [diff] [blame] | 129 | void HandleToggleTouchpad() { |
| 130 | base::RecordAction(base::UserMetricsAction("Accel_Toggle_Touchpad")); |
msw | 0414d412 | 2016-07-06 22:58:48 | [diff] [blame] | 131 | ash::WmShell::Get()->delegate()->ToggleTouchpad(); |
afakhry | e8eac60 | 2015-11-10 02:31:22 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void HandleToggleTouchscreen() { |
| 135 | base::RecordAction(base::UserMetricsAction("Accel_Toggle_Touchscreen")); |
warx | 4c8baedb | 2016-12-03 22:38:50 | [diff] [blame] | 136 | ShellDelegate* delegate = WmShell::Get()->delegate(); |
| 137 | delegate->SetTouchscreenEnabledInPrefs( |
| 138 | !delegate->IsTouchscreenEnabledInPrefs(false /* use_local_state */), |
| 139 | false /* use_local_state */); |
| 140 | delegate->UpdateTouchscreenStatusFromPrefs(); |
afakhry | e8eac60 | 2015-11-10 02:31:22 | [diff] [blame] | 141 | } |
| 142 | |
afakhry | cd09f64d | 2016-09-16 22:26:56 | [diff] [blame] | 143 | void HandleToggleTouchView() { |
oshima | 1d55f40 | 2016-06-24 07:13:51 | [diff] [blame] | 144 | MaximizeModeController* controller = |
sky | c68696c | 2016-07-01 21:06:02 | [diff] [blame] | 145 | WmShell::Get()->maximize_mode_controller(); |
oshima | 1d55f40 | 2016-06-24 07:13:51 | [diff] [blame] | 146 | controller->EnableMaximizeModeWindowManager( |
| 147 | !controller->IsMaximizeModeWindowManagerEnabled()); |
| 148 | } |
| 149 | |
jamescook | 192db0bd | 2017-01-09 19:31:29 | [diff] [blame] | 150 | void HandleTriggerCrash() { |
| 151 | CHECK(false) << "Intentional crash via debug accelerator."; |
| 152 | } |
| 153 | |
pkotwicz | 01abb76a | 2014-10-23 02:48:31 | [diff] [blame] | 154 | } // namespace |
| 155 | |
| 156 | void 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 | |
| 165 | bool DebugAcceleratorsEnabled() { |
pgal.u-szeged | d84534d3 | 2014-10-29 12:34:30 | [diff] [blame] | 166 | return base::CommandLine::ForCurrentProcess()->HasSwitch( |
pkotwicz | 01abb76a | 2014-10-23 02:48:31 | [diff] [blame] | 167 | switches::kAshDebugShortcuts); |
| 168 | } |
| 169 | |
afakhry | cd09f64d | 2016-09-16 22:26:56 | [diff] [blame] | 170 | bool DeveloperAcceleratorsEnabled() { |
| 171 | return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 172 | switches::kAshDeveloperShortcuts); |
| 173 | } |
| 174 | |
pkotwicz | 8723017c | 2014-12-09 00:51:24 | [diff] [blame] | 175 | void PerformDebugActionIfEnabled(AcceleratorAction action) { |
pkotwicz | 01abb76a | 2014-10-23 02:48:31 | [diff] [blame] | 176 | if (!DebugAcceleratorsEnabled()) |
pkotwicz | 8723017c | 2014-12-09 00:51:24 | [diff] [blame] | 177 | return; |
pkotwicz | 01abb76a | 2014-10-23 02:48:31 | [diff] [blame] | 178 | |
| 179 | switch (action) { |
jamescook | 6ed406b7 | 2017-02-13 18:31:13 | [diff] [blame] | 180 | 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; |
jamescook | 9fb7aa1 | 2016-06-24 22:22:28 | [diff] [blame] | 189 | case DEBUG_SHOW_TOAST: |
sky | 004dce53 | 2016-07-20 21:28:27 | [diff] [blame] | 190 | WmShell::Get()->toast_manager()->Show( |
jdufault | 8c170d5 | 2016-08-25 21:03:40 | [diff] [blame] | 191 | ToastData("id", base::ASCIIToUTF16("Toast"), 5000 /* duration_ms */, |
| 192 | base::ASCIIToUTF16("Dismiss"))); |
jamescook | 9fb7aa1 | 2016-06-24 22:22:28 | [diff] [blame] | 193 | break; |
afakhry | e8eac60 | 2015-11-10 02:31:22 | [diff] [blame] | 194 | case DEBUG_TOGGLE_TOUCH_PAD: |
| 195 | HandleToggleTouchpad(); |
| 196 | break; |
| 197 | case DEBUG_TOGGLE_TOUCH_SCREEN: |
| 198 | HandleToggleTouchscreen(); |
| 199 | break; |
oshima | 1d55f40 | 2016-06-24 07:13:51 | [diff] [blame] | 200 | case DEBUG_TOGGLE_TOUCH_VIEW: |
afakhry | cd09f64d | 2016-09-16 22:26:56 | [diff] [blame] | 201 | HandleToggleTouchView(); |
oshima | 1d55f40 | 2016-06-24 07:13:51 | [diff] [blame] | 202 | break; |
msw | 3f439af | 2016-09-08 22:35:26 | [diff] [blame] | 203 | case DEBUG_TOGGLE_WALLPAPER_MODE: |
| 204 | HandleToggleWallpaperMode(); |
| 205 | break; |
jamescook | 192db0bd | 2017-01-09 19:31:29 | [diff] [blame] | 206 | case DEBUG_TRIGGER_CRASH: |
| 207 | HandleTriggerCrash(); |
| 208 | break; |
pkotwicz | 01abb76a | 2014-10-23 02:48:31 | [diff] [blame] | 209 | default: |
pkotwicz | 8723017c | 2014-12-09 00:51:24 | [diff] [blame] | 210 | break; |
pkotwicz | 01abb76a | 2014-10-23 02:48:31 | [diff] [blame] | 211 | } |
[email protected] | e607e65 | 2013-09-25 01:08:39 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | } // namespace debug |
| 215 | } // namespace ash |