1 // Copyright (c) 2012 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 #ifndef ASH_SHELL_H_ 6 #define ASH_SHELL_H_ 7 8 #include <utility> 9 #include <vector> 10 11 #include "ash/ash_export.h" 12 #include "ash/shelf/shelf_types.h" 13 #include "ash/system/user/login_status.h" 14 #include "ash/wm/system_modal_container_event_filter_delegate.h" 15 #include "base/basictypes.h" 16 #include "base/compiler_specific.h" 17 #include "base/gtest_prod_util.h" 18 #include "base/memory/scoped_ptr.h" 19 #include "base/observer_list.h" 20 #include "ui/aura/client/activation_change_observer.h" 21 #include "ui/base/events/event_target.h" 22 #include "ui/base/ui_base_types.h" 23 #include "ui/gfx/insets.h" 24 #include "ui/gfx/screen.h" 25 #include "ui/gfx/size.h" 26 #include "ui/views/corewm/cursor_manager.h" 27 28 class CommandLine; 29 30 namespace aura { 31 class EventFilter; 32 class RootWindow; 33 class Window; 34 namespace client { 35 class ActivationClient; 36 class FocusClient; 37 class UserActionClient; 38 } 39 } 40 namespace chromeos { 41 class OutputConfigurator; 42 } 43 namespace content { 44 class BrowserContext; 45 } 46 47 namespace gfx { 48 class ImageSkia; 49 class Point; 50 class Rect; 51 } 52 namespace ui { 53 class Layer; 54 } 55 namespace views { 56 class NonClientFrameView; 57 class Widget; 58 namespace corewm { 59 class CompoundEventFilter; 60 class InputMethodEventFilter; 61 class ShadowController; 62 class TooltipController; 63 class VisibilityController; 64 class WindowModalityController; 65 } 66 } 67 68 namespace ash { 69 70 class AcceleratorController; 71 class AshNativeCursorManager; 72 class CapsLockDelegate; 73 class DesktopBackgroundController; 74 class DisplayController; 75 class HighContrastController; 76 class Launcher; 77 class LauncherDelegate; 78 class LauncherModel; 79 class MagnificationController; 80 class MruWindowTracker; 81 class NestedDispatcherController; 82 class PartialMagnificationController; 83 class PowerButtonController; 84 class RootWindowHostFactory; 85 class ScreenAsh; 86 class LockStateController; 87 class SessionStateDelegate; 88 class ShellDelegate; 89 class ShellObserver; 90 class SystemTray; 91 class SystemTrayDelegate; 92 class SystemTrayNotifier; 93 class UserActivityDetector; 94 class UserWallpaperDelegate; 95 class VideoDetector; 96 class WebNotificationTray; 97 class WindowCycleController; 98 class WindowSelectorController; 99 100 namespace internal { 101 class AcceleratorFilter; 102 class ActivationController; 103 class AppListController; 104 class CaptureController; 105 class DisplayChangeObserverX11; 106 class DisplayErrorObserver; 107 class DisplayManager; 108 class DragDropController; 109 class EventClientImpl; 110 class EventRewriterEventFilter; 111 class EventTransformationHandler; 112 class FocusCycler; 113 class LocaleNotificationController; 114 class MouseCursorEventFilter; 115 class OutputConfiguratorAnimation; 116 class OverlayEventFilter; 117 class ResizeShadowController; 118 class ResolutionNotificationController; 119 class RootWindowController; 120 class RootWindowLayoutManager; 121 class ScopedTargetRootWindow; 122 class ScreenPositionController; 123 class SlowAnimationEventFilter; 124 class StatusAreaWidget; 125 class SystemGestureEventFilter; 126 class SystemModalContainerEventFilter; 127 class TouchObserverHUD; 128 } 129 130 namespace shell { 131 class WindowWatcher; 132 } 133 134 namespace test { 135 class ShellTestApi; 136 } 137 138 // Shell is a singleton object that presents the Shell API and implements the 139 // RootWindow's delegate interface. 140 // 141 // Upon creation, the Shell sets itself as the RootWindow's delegate, which 142 // takes ownership of the Shell. 143 class ASH_EXPORT Shell 144 : public internal::SystemModalContainerEventFilterDelegate, 145 public ui::EventTarget, 146 public aura::client::ActivationChangeObserver { 147 public: 148 typedef std::vector<aura::RootWindow*> RootWindowList; 149 typedef std::vector<internal::RootWindowController*> RootWindowControllerList; 150 151 enum Direction { 152 FORWARD, 153 BACKWARD 154 }; 155 156 // A shell must be explicitly created so that it can call |Init()| with the 157 // delegate set. |delegate| can be NULL (if not required for initialization). 158 // Takes ownership of |delegate|. 159 static Shell* CreateInstance(ShellDelegate* delegate); 160 161 // Should never be called before |CreateInstance()|. 162 static Shell* GetInstance(); 163 164 // Returns true if the ash shell has been instantiated. 165 static bool HasInstance(); 166 167 static void DeleteInstance(); 168 169 // Returns the root window controller for the primary root window. 170 // TODO(oshima): move this to |RootWindowController| 171 static internal::RootWindowController* GetPrimaryRootWindowController(); 172 173 // Returns all root window controllers. 174 // TODO(oshima): move this to |RootWindowController| 175 static RootWindowControllerList GetAllRootWindowControllers(); 176 177 // Returns the primary RootWindow. The primary RootWindow is the one 178 // that has a launcher. 179 static aura::RootWindow* GetPrimaryRootWindow(); 180 181 // Returns a RootWindow when used as a target when creating a new window. 182 // The root window of the active window is used in most cases, but can 183 // be overridden by using ScopedTargetRootWindow(). 184 // If you want to get a RootWindow of the active window, just use 185 // |wm::GetActiveWindow()->GetRootWindow()|. 186 // TODO(oshima): Rename to GetTargetRootWindow() crbug.com/266378. 187 static aura::RootWindow* GetActiveRootWindow(); 188 189 // Returns the global Screen object that's always active in ash. 190 static gfx::Screen* GetScreen(); 191 192 // Returns all root windows. 193 static RootWindowList GetAllRootWindows(); 194 195 static aura::Window* GetContainer(aura::RootWindow* root_window, 196 int container_id); 197 static const aura::Window* GetContainer(const aura::RootWindow* root_window, 198 int container_id); 199 200 // Returns the list of containers that match |container_id| in 201 // all root windows. If |priority_root| is given, the container 202 // in the |priority_root| will be inserted at the top of the list. 203 static std::vector<aura::Window*> GetContainersFromAllRootWindows( 204 int container_id, 205 aura::RootWindow* priority_root); 206 207 // True if an experimental maximize mode is enabled which forces browser and 208 // application windows to be maximized only. 209 static bool IsForcedMaximizeMode(); 210 211 void set_active_root_window(aura::RootWindow* target_root_window) { 212 target_root_window_ = target_root_window; 213 } 214 215 // Shows the context menu for the background and launcher at 216 // |location_in_screen| (in screen coordinates). 217 void ShowContextMenu(const gfx::Point& location_in_screen, 218 ui::MenuSourceType source_type); 219 220 // Toggles the app list. |window| specifies in which display the app 221 // list should be shown. If this is NULL, the active root window 222 // will be used. 223 void ToggleAppList(aura::Window* anchor); 224 225 // Returns app list target visibility. 226 bool GetAppListTargetVisibility() const; 227 228 // Returns app list window or NULL if it is not visible. 229 aura::Window* GetAppListWindow(); 230 231 // Returns true if a system-modal dialog window is currently open. 232 bool IsSystemModalWindowOpen() const; 233 234 // For testing only: set simulation that a modal window is open 235 void SimulateModalWindowOpenForTesting(bool modal_window_open) { 236 simulate_modal_window_open_for_testing_ = modal_window_open; 237 } 238 239 // Creates a default views::NonClientFrameView for use by windows in the 240 // Ash environment. 241 views::NonClientFrameView* CreateDefaultNonClientFrameView( 242 views::Widget* widget); 243 244 // Rotates focus through containers that can receive focus. 245 void RotateFocus(Direction direction); 246 247 // Sets the work area insets of the display that contains |window|, 248 // this notifies observers too. 249 // TODO(sky): this no longer really replicates what happens and is unreliable. 250 // Remove this. 251 void SetDisplayWorkAreaInsets(aura::Window* window, 252 const gfx::Insets& insets); 253 254 // Called when the user logs in. 255 void OnLoginStateChanged(user::LoginStatus status); 256 257 // Called when the login status changes. 258 // TODO(oshima): Investigate if we can merge this and |OnLoginStateChanged|. 259 void UpdateAfterLoginStatusChange(user::LoginStatus status); 260 261 // Called when the application is exiting. 262 void OnAppTerminating(); 263 264 // Called when the screen is locked (after the lock window is visible) or 265 // unlocked. 266 void OnLockStateChanged(bool locked); 267 268 // Initializes |launcher_|. Does nothing if it's already initialized. 269 void CreateLauncher(); 270 271 // Show launcher view if it was created hidden (before session has started). 272 void ShowLauncher(); 273 274 // Adds/removes observer. 275 void AddShellObserver(ShellObserver* observer); 276 void RemoveShellObserver(ShellObserver* observer); 277 278 #if !defined(OS_MACOSX) 279 AcceleratorController* accelerator_controller() { 280 return accelerator_controller_.get(); 281 } 282 #endif // !defined(OS_MACOSX) 283 284 internal::DisplayManager* display_manager() { 285 return display_manager_.get(); 286 } 287 views::corewm::InputMethodEventFilter* input_method_filter() { 288 return input_method_filter_.get(); 289 } 290 views::corewm::CompoundEventFilter* env_filter() { 291 return env_filter_.get(); 292 } 293 views::corewm::TooltipController* tooltip_controller() { 294 return tooltip_controller_.get(); 295 } 296 internal::EventRewriterEventFilter* event_rewriter_filter() { 297 return event_rewriter_filter_.get(); 298 } 299 internal::OverlayEventFilter* overlay_filter() { 300 return overlay_filter_.get(); 301 } 302 DesktopBackgroundController* desktop_background_controller() { 303 return desktop_background_controller_.get(); 304 } 305 PowerButtonController* power_button_controller() { 306 return power_button_controller_.get(); 307 } 308 LockStateController* lock_state_controller() { 309 return lock_state_controller_.get(); 310 } 311 MruWindowTracker* mru_window_tracker() { 312 return mru_window_tracker_.get(); 313 } 314 UserActivityDetector* user_activity_detector() { 315 return user_activity_detector_.get(); 316 } 317 VideoDetector* video_detector() { 318 return video_detector_.get(); 319 } 320 WindowCycleController* window_cycle_controller() { 321 return window_cycle_controller_.get(); 322 } 323 WindowSelectorController* window_selector_controller() { 324 return window_selector_controller_.get(); 325 } 326 internal::FocusCycler* focus_cycler() { 327 return focus_cycler_.get(); 328 } 329 DisplayController* display_controller() { 330 return display_controller_.get(); 331 } 332 internal::MouseCursorEventFilter* mouse_cursor_filter() { 333 return mouse_cursor_filter_.get(); 334 } 335 internal::EventTransformationHandler* event_transformation_handler() { 336 return event_transformation_handler_.get(); 337 } 338 views::corewm::CursorManager* cursor_manager() { return &cursor_manager_; } 339 340 ShellDelegate* delegate() { return delegate_.get(); } 341 342 UserWallpaperDelegate* user_wallpaper_delegate() { 343 return user_wallpaper_delegate_.get(); 344 } 345 346 CapsLockDelegate* caps_lock_delegate() { 347 return caps_lock_delegate_.get(); 348 } 349 350 SessionStateDelegate* session_state_delegate() { 351 return session_state_delegate_.get(); 352 } 353 354 HighContrastController* high_contrast_controller() { 355 return high_contrast_controller_.get(); 356 } 357 358 MagnificationController* magnification_controller() { 359 return magnification_controller_.get(); 360 } 361 362 PartialMagnificationController* partial_magnification_controller() { 363 return partial_magnification_controller_.get(); 364 } 365 aura::client::ActivationClient* activation_client() { 366 return activation_client_; 367 } 368 369 ScreenAsh* screen() { return screen_; } 370 371 // Force the shelf to query for it's current visibility state. 372 void UpdateShelfVisibility(); 373 374 // TODO(oshima): Define an interface to access shelf/launcher 375 // state, or just use Launcher. 376 377 // Sets/gets the shelf auto-hide behavior on |root_window|. 378 void SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior, 379 aura::RootWindow* root_window); 380 ShelfAutoHideBehavior GetShelfAutoHideBehavior( 381 aura::RootWindow* root_window) const; 382 383 // Sets/gets shelf's alignment on |root_window|. 384 void SetShelfAlignment(ShelfAlignment alignment, 385 aura::RootWindow* root_window); 386 ShelfAlignment GetShelfAlignment(aura::RootWindow* root_window); 387 388 // Dims or undims the screen. 389 void SetDimming(bool should_dim); 390 391 // Creates a modal background (a partially-opaque fullscreen window) 392 // on all displays for |window|. 393 void CreateModalBackground(aura::Window* window); 394 395 // Called when a modal window is removed. It will activate 396 // another modal window if any, or remove modal screens 397 // on all displays. 398 void OnModalWindowRemoved(aura::Window* removed); 399 400 // Returns WebNotificationTray on the primary root window. 401 WebNotificationTray* GetWebNotificationTray(); 402 403 // Does the primary display have status area? 404 bool HasPrimaryStatusArea(); 405 406 // Returns the system tray on primary display. 407 SystemTray* GetPrimarySystemTray(); 408 409 SystemTrayDelegate* system_tray_delegate() { 410 return system_tray_delegate_.get(); 411 } 412 413 SystemTrayNotifier* system_tray_notifier() { 414 return system_tray_notifier_.get(); 415 } 416 417 static void set_initially_hide_cursor(bool hide) { 418 initially_hide_cursor_ = hide; 419 } 420 421 internal::ResizeShadowController* resize_shadow_controller() { 422 return resize_shadow_controller_.get(); 423 } 424 425 // Made available for tests. 426 views::corewm::ShadowController* shadow_controller() { 427 return shadow_controller_.get(); 428 } 429 430 content::BrowserContext* browser_context() { return browser_context_; } 431 void set_browser_context(content::BrowserContext* browser_context) { 432 browser_context_ = browser_context; 433 } 434 435 // Initializes the root window to be used for a secondary display. 436 void InitRootWindowForSecondaryDisplay(aura::RootWindow* root); 437 438 // Starts the animation that occurs on first login. 439 void DoInitialWorkspaceAnimation(); 440 441 #if defined(OS_CHROMEOS) && defined(USE_X11) 442 // TODO(oshima): Move these objects to DisplayController. 443 chromeos::OutputConfigurator* output_configurator() { 444 return output_configurator_.get(); 445 } 446 internal::OutputConfiguratorAnimation* output_configurator_animation() { 447 return output_configurator_animation_.get(); 448 } 449 internal::DisplayErrorObserver* display_error_observer() { 450 return display_error_observer_.get(); 451 } 452 #endif // defined(OS_CHROMEOS) && defined(USE_X11) 453 454 internal::ResolutionNotificationController* 455 resolution_notification_controller() { 456 return resolution_notification_controller_.get(); 457 } 458 459 RootWindowHostFactory* root_window_host_factory() { 460 return root_window_host_factory_.get(); 461 } 462 463 LauncherModel* launcher_model() { 464 return launcher_model_.get(); 465 } 466 467 // Returns the launcher delegate, creating if necesary. 468 LauncherDelegate* GetLauncherDelegate(); 469 470 void SetTouchHudProjectionEnabled(bool enabled); 471 472 bool is_touch_hud_projection_enabled() const { 473 return is_touch_hud_projection_enabled_; 474 } 475 476 private: 477 FRIEND_TEST_ALL_PREFIXES(ExtendedDesktopTest, TestCursor); 478 FRIEND_TEST_ALL_PREFIXES(WindowManagerTest, MouseEventCursors); 479 FRIEND_TEST_ALL_PREFIXES(WindowManagerTest, TransformActivate); 480 friend class internal::RootWindowController; 481 friend class internal::ScopedTargetRootWindow; 482 friend class test::ShellTestApi; 483 friend class shell::WindowWatcher; 484 485 typedef std::pair<aura::Window*, gfx::Rect> WindowAndBoundsPair; 486 487 // Takes ownership of |delegate|. 488 explicit Shell(ShellDelegate* delegate); 489 virtual ~Shell(); 490 491 void Init(); 492 493 // Initializes the root window and root window controller so that it 494 // can host browser windows. |first_run_after_boot| is true for the 495 // primary display only first time after boot. 496 void InitRootWindowController(internal::RootWindowController* root, 497 bool first_run_after_boot); 498 499 // ash::internal::SystemModalContainerEventFilterDelegate overrides: 500 virtual bool CanWindowReceiveEvents(aura::Window* window) OVERRIDE; 501 502 // Overridden from ui::EventTarget: 503 virtual bool CanAcceptEvent(const ui::Event& event) OVERRIDE; 504 virtual EventTarget* GetParentTarget() OVERRIDE; 505 virtual void OnEvent(ui::Event* event) OVERRIDE; 506 507 // Overridden from aura::client::ActivationChangeObserver: 508 virtual void OnWindowActivated(aura::Window* gained_active, 509 aura::Window* lost_active) OVERRIDE; 510 511 static Shell* instance_; 512 513 // If set before the Shell is initialized, the mouse cursor will be hidden 514 // when the screen is initially created. 515 static bool initially_hide_cursor_; 516 517 ScreenAsh* screen_; 518 519 // When no explicit target display/RootWindow is given, new windows are 520 // created on |scoped_target_root_window_| , unless NULL in 521 // which case they are created on |target_root_window_|. 522 // |target_root_window_| never becomes NULL during the session. 523 aura::RootWindow* target_root_window_; 524 aura::RootWindow* scoped_target_root_window_; 525 526 // The CompoundEventFilter owned by aura::Env object. 527 scoped_ptr<views::corewm::CompoundEventFilter> env_filter_; 528 529 std::vector<WindowAndBoundsPair> to_restore_; 530 531 #if !defined(OS_MACOSX) 532 scoped_ptr<NestedDispatcherController> nested_dispatcher_controller_; 533 534 scoped_ptr<AcceleratorController> accelerator_controller_; 535 #endif // !defined(OS_MACOSX) 536 537 scoped_ptr<ShellDelegate> delegate_; 538 scoped_ptr<SystemTrayDelegate> system_tray_delegate_; 539 scoped_ptr<SystemTrayNotifier> system_tray_notifier_; 540 scoped_ptr<UserWallpaperDelegate> user_wallpaper_delegate_; 541 scoped_ptr<CapsLockDelegate> caps_lock_delegate_; 542 scoped_ptr<SessionStateDelegate> session_state_delegate_; 543 scoped_ptr<LauncherDelegate> launcher_delegate_; 544 545 scoped_ptr<LauncherModel> launcher_model_; 546 547 scoped_ptr<internal::AppListController> app_list_controller_; 548 549 scoped_ptr<internal::ActivationController> activation_controller_; 550 scoped_ptr<internal::CaptureController> capture_controller_; 551 scoped_ptr<internal::DragDropController> drag_drop_controller_; 552 scoped_ptr<internal::ResizeShadowController> resize_shadow_controller_; 553 scoped_ptr<views::corewm::ShadowController> shadow_controller_; 554 scoped_ptr<views::corewm::VisibilityController> visibility_controller_; 555 scoped_ptr<views::corewm::WindowModalityController> 556 window_modality_controller_; 557 scoped_ptr<views::corewm::TooltipController> tooltip_controller_; 558 scoped_ptr<DesktopBackgroundController> desktop_background_controller_; 559 scoped_ptr<PowerButtonController> power_button_controller_; 560 scoped_ptr<LockStateController> lock_state_controller_; 561 scoped_ptr<MruWindowTracker> mru_window_tracker_; 562 scoped_ptr<UserActivityDetector> user_activity_detector_; 563 scoped_ptr<VideoDetector> video_detector_; 564 scoped_ptr<WindowCycleController> window_cycle_controller_; 565 scoped_ptr<WindowSelectorController> window_selector_controller_; 566 scoped_ptr<internal::FocusCycler> focus_cycler_; 567 scoped_ptr<DisplayController> display_controller_; 568 scoped_ptr<HighContrastController> high_contrast_controller_; 569 scoped_ptr<MagnificationController> magnification_controller_; 570 scoped_ptr<PartialMagnificationController> partial_magnification_controller_; 571 scoped_ptr<aura::client::FocusClient> focus_client_; 572 scoped_ptr<aura::client::UserActionClient> user_action_client_; 573 aura::client::ActivationClient* activation_client_; 574 scoped_ptr<internal::MouseCursorEventFilter> mouse_cursor_filter_; 575 scoped_ptr<internal::ScreenPositionController> screen_position_controller_; 576 scoped_ptr<internal::SystemModalContainerEventFilter> modality_filter_; 577 scoped_ptr<internal::EventClientImpl> event_client_; 578 scoped_ptr<internal::EventTransformationHandler> 579 event_transformation_handler_; 580 scoped_ptr<RootWindowHostFactory> root_window_host_factory_; 581 582 // An event filter that rewrites or drops an event. 583 scoped_ptr<internal::EventRewriterEventFilter> event_rewriter_filter_; 584 585 // An event filter that pre-handles key events while the partial 586 // screenshot UI or the keyboard overlay is active. 587 scoped_ptr<internal::OverlayEventFilter> overlay_filter_; 588 589 // An event filter which handles system level gestures 590 scoped_ptr<internal::SystemGestureEventFilter> system_gesture_filter_; 591 592 #if !defined(OS_MACOSX) 593 // An event filter that pre-handles global accelerators. 594 scoped_ptr<internal::AcceleratorFilter> accelerator_filter_; 595 #endif 596 597 // An event filter that pre-handles all key events to send them to an IME. 598 scoped_ptr<views::corewm::InputMethodEventFilter> input_method_filter_; 599 600 scoped_ptr<internal::DisplayManager> display_manager_; 601 602 scoped_ptr<internal::LocaleNotificationController> 603 locale_notification_controller_; 604 605 #if defined(OS_CHROMEOS) && defined(USE_X11) 606 // Controls video output device state. 607 scoped_ptr<chromeos::OutputConfigurator> output_configurator_; 608 scoped_ptr<internal::OutputConfiguratorAnimation> 609 output_configurator_animation_; 610 scoped_ptr<internal::DisplayErrorObserver> display_error_observer_; 611 612 // Receives output change events and udpates the display manager. 613 scoped_ptr<internal::DisplayChangeObserverX11> display_change_observer_; 614 #endif // defined(OS_CHROMEOS) && defined(USE_X11) 615 616 scoped_ptr<internal::ResolutionNotificationController> 617 resolution_notification_controller_; 618 619 // |native_cursor_manager_| is owned by |cursor_manager_|, but we keep a 620 // pointer to vend to test code. 621 AshNativeCursorManager* native_cursor_manager_; 622 views::corewm::CursorManager cursor_manager_; 623 624 ObserverList<ShellObserver> observers_; 625 626 // Used by ash/shell. 627 content::BrowserContext* browser_context_; 628 629 // For testing only: simulate that a modal window is open 630 bool simulate_modal_window_open_for_testing_; 631 632 bool is_touch_hud_projection_enabled_; 633 634 DISALLOW_COPY_AND_ASSIGN(Shell); 635 }; 636 637 } // namespace ash 638 639 #endif // ASH_SHELL_H_ 640