debug_commands.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #include "ash/accelerators/debug_commands.h"
  5. #include <string>
  6. #include <utility>
  7. #include "ash/accelerators/accelerator_commands.h"
  8. #include "ash/constants/ash_features.h"
  9. #include "ash/constants/ash_switches.h"
  10. #include "ash/constants/notifier_catalogs.h"
  11. #include "ash/hud_display/hud_display.h"
  12. #include "ash/public/cpp/accelerators.h"
  13. #include "ash/public/cpp/debug_utils.h"
  14. #include "ash/public/cpp/system/toast_data.h"
  15. #include "ash/shell.h"
  16. #include "ash/system/toast/toast_manager_impl.h"
  17. #include "ash/touch/touch_devices_controller.h"
  18. #include "ash/wallpaper/wallpaper_controller_impl.h"
  19. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  20. #include "base/command_line.h"
  21. #include "base/metrics/user_metrics.h"
  22. #include "base/metrics/user_metrics_action.h"
  23. #include "base/strings/utf_string_conversions.h"
  24. #include "ui/accessibility/ax_tree_id.h"
  25. #include "ui/aura/client/aura_constants.h"
  26. #include "ui/display/manager/display_manager.h"
  27. #include "ui/gfx/canvas.h"
  28. #include "ui/gfx/geometry/skia_conversions.h"
  29. #include "ui/gfx/image/image_skia.h"
  30. #include "ui/gfx/image/image_skia_rep.h"
  31. #include "ui/views/widget/widget.h"
  32. namespace ash {
  33. namespace debug {
  34. namespace {
  35. void HandlePrintLayerHierarchy() {
  36. std::ostringstream out;
  37. PrintLayerHierarchy(&out);
  38. LOG(ERROR) << out.str();
  39. }
  40. void HandlePrintViewHierarchy() {
  41. std::ostringstream out;
  42. PrintViewHierarchy(&out);
  43. LOG(ERROR) << out.str();
  44. }
  45. void HandlePrintWindowHierarchy() {
  46. std::ostringstream out;
  47. PrintWindowHierarchy(&out, /*scrub_data=*/false);
  48. LOG(ERROR) << out.str();
  49. }
  50. gfx::ImageSkia CreateWallpaperImage(SkColor fill, SkColor rect) {
  51. // TODO(oshima): Consider adding a command line option to control wallpaper
  52. // images for testing. The size is randomly picked.
  53. gfx::Size image_size(1366, 768);
  54. SkBitmap bitmap;
  55. bitmap.allocN32Pixels(image_size.width(), image_size.height(), true);
  56. SkCanvas canvas(bitmap);
  57. canvas.drawColor(fill);
  58. SkPaint paint;
  59. paint.setColor(rect);
  60. paint.setStrokeWidth(10);
  61. paint.setStyle(SkPaint::kStroke_Style);
  62. paint.setBlendMode(SkBlendMode::kSrcOver);
  63. canvas.drawRoundRect(gfx::RectToSkRect(gfx::Rect(image_size)), 100.f, 100.f,
  64. paint);
  65. return gfx::ImageSkia::CreateFromBitmap(std::move(bitmap), 1.f);
  66. }
  67. void HandleToggleWallpaperMode() {
  68. static int index = 0;
  69. auto* wallpaper_controller = Shell::Get()->wallpaper_controller();
  70. WallpaperInfo info("", WALLPAPER_LAYOUT_STRETCH, WallpaperType::kDefault,
  71. base::Time::Now().LocalMidnight());
  72. switch (++index % 4) {
  73. case 0:
  74. wallpaper_controller->ShowDefaultWallpaperForTesting();
  75. break;
  76. case 1:
  77. wallpaper_controller->ShowWallpaperImage(
  78. CreateWallpaperImage(SK_ColorRED, SK_ColorBLUE), info,
  79. /*preview_mode=*/false, /*always_on_top=*/false);
  80. break;
  81. case 2:
  82. info.layout = WALLPAPER_LAYOUT_CENTER;
  83. wallpaper_controller->ShowWallpaperImage(
  84. CreateWallpaperImage(SK_ColorBLUE, SK_ColorGREEN), info,
  85. /*preview_mode=*/false, /*always_on_top=*/false);
  86. break;
  87. case 3:
  88. info.layout = WALLPAPER_LAYOUT_CENTER_CROPPED;
  89. wallpaper_controller->ShowWallpaperImage(
  90. CreateWallpaperImage(SK_ColorGREEN, SK_ColorRED), info,
  91. /*preview_mode=*/false, /*always_on_top=*/false);
  92. break;
  93. }
  94. }
  95. void HandleDumpCalendarModel() {
  96. accelerators::DumpCalendarModel();
  97. }
  98. void HandleToggleKeyboardBacklight() {
  99. if (ash::features::IsKeyboardBacklightToggleEnabled()) {
  100. base::RecordAction(base::UserMetricsAction("Accel_Keyboard_Backlight"));
  101. accelerators::ToggleKeyboardBacklight();
  102. }
  103. }
  104. void HandleToggleMicrophoneMute() {
  105. base::RecordAction(base::UserMetricsAction("Accel_Microphone_Mute"));
  106. accelerators::MicrophoneMuteToggle();
  107. }
  108. void HandleToggleTouchpad() {
  109. base::RecordAction(base::UserMetricsAction("Accel_Toggle_Touchpad"));
  110. Shell::Get()->touch_devices_controller()->ToggleTouchpad();
  111. }
  112. void HandleToggleTouchscreen() {
  113. base::RecordAction(base::UserMetricsAction("Accel_Toggle_Touchscreen"));
  114. TouchDevicesController* controller = Shell::Get()->touch_devices_controller();
  115. controller->SetTouchscreenEnabled(
  116. !controller->GetTouchscreenEnabled(TouchDeviceEnabledSource::USER_PREF),
  117. TouchDeviceEnabledSource::USER_PREF);
  118. }
  119. void HandleToggleTabletMode() {
  120. TabletModeController* controller = Shell::Get()->tablet_mode_controller();
  121. controller->SetEnabledForDev(!controller->InTabletMode());
  122. }
  123. void HandleTriggerCrash() {
  124. LOG(FATAL) << "Intentional crash via debug accelerator.";
  125. }
  126. void HandleTriggerHUDDisplay() {
  127. hud_display::HUDDisplayView::Toggle();
  128. }
  129. } // namespace
  130. void PrintUIHierarchies() {
  131. // This is a separate command so the user only has to hit one key to generate
  132. // all the logs. Developers use the individual dumps repeatedly, so keep
  133. // those as separate commands to avoid spamming their logs.
  134. HandlePrintLayerHierarchy();
  135. HandlePrintWindowHierarchy();
  136. HandlePrintViewHierarchy();
  137. }
  138. bool DebugAcceleratorsEnabled() {
  139. return base::CommandLine::ForCurrentProcess()->HasSwitch(
  140. switches::kAshDebugShortcuts);
  141. }
  142. bool DeveloperAcceleratorsEnabled() {
  143. return base::CommandLine::ForCurrentProcess()->HasSwitch(
  144. switches::kAshDeveloperShortcuts);
  145. }
  146. void PerformDebugActionIfEnabled(AcceleratorAction action) {
  147. if (!DebugAcceleratorsEnabled())
  148. return;
  149. switch (action) {
  150. case DEBUG_DUMP_CALENDAR_MODEL:
  151. HandleDumpCalendarModel();
  152. break;
  153. case DEBUG_KEYBOARD_BACKLIGHT_TOGGLE:
  154. HandleToggleKeyboardBacklight();
  155. break;
  156. case DEBUG_MICROPHONE_MUTE_TOGGLE:
  157. HandleToggleMicrophoneMute();
  158. break;
  159. case DEBUG_PRINT_LAYER_HIERARCHY:
  160. HandlePrintLayerHierarchy();
  161. break;
  162. case DEBUG_PRINT_VIEW_HIERARCHY:
  163. HandlePrintViewHierarchy();
  164. break;
  165. case DEBUG_PRINT_WINDOW_HIERARCHY:
  166. HandlePrintWindowHierarchy();
  167. break;
  168. case DEBUG_SHOW_TOAST:
  169. Shell::Get()->toast_manager()->Show(ToastData(
  170. /*id=*/"id", ToastCatalogName::kDebugCommand, /*text=*/u"Toast",
  171. ToastData::kDefaultToastDuration,
  172. /*visible_on_lock_screen=*/false, /*has_dismiss_button=*/true,
  173. /*custom_dismiss_text=*/u"Dismiss"));
  174. break;
  175. case DEBUG_TOGGLE_TOUCH_PAD:
  176. HandleToggleTouchpad();
  177. break;
  178. case DEBUG_TOGGLE_TOUCH_SCREEN:
  179. HandleToggleTouchscreen();
  180. break;
  181. case DEBUG_TOGGLE_TABLET_MODE:
  182. HandleToggleTabletMode();
  183. break;
  184. case DEBUG_TOGGLE_WALLPAPER_MODE:
  185. HandleToggleWallpaperMode();
  186. break;
  187. case DEBUG_TRIGGER_CRASH:
  188. HandleTriggerCrash();
  189. break;
  190. case DEBUG_TOGGLE_HUD_DISPLAY:
  191. HandleTriggerHUDDisplay();
  192. break;
  193. default:
  194. break;
  195. }
  196. }
  197. } // namespace debug
  198. } // namespace ash