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.
11 #include "ash/ash_export.h"
12 #include "ash/metrics/user_metrics_recorder.h"
13 #include "ash/shelf/shelf_types.h"
14 #include "ash/system/user/login_status.h"
15 #include "ash/wm/system_modal_container_event_filter_delegate.h"
16 #include "base/basictypes.h"
17 #include "base/compiler_specific.h"
18 #include "base/gtest_prod_util.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "base/memory/weak_ptr.h"
21 #include "base/observer_list.h"
22 #include "ui/aura/client/activation_change_observer.h"
23 #include "ui/aura/window.h"
24 #include "ui/base/ui_base_types.h"
25 #include "ui/events/event_target.h"
26 #include "ui/gfx/insets.h"
27 #include "ui/gfx/screen.h"
28 #include "ui/gfx/size.h"
29 #include "ui/views/corewm/cursor_manager.h"
41 class ActivationClient;
43 class UserActionClient;
47 class OutputConfigurator;
57 class KeyboardController;
64 class NonClientFrameView;
67 class CompoundEventFilter;
68 class InputMethodEventFilter;
69 class ShadowController;
70 class TooltipController;
71 class VisibilityController;
72 class WindowModalityController;
78 class AcceleratorController;
79 class AccessibilityDelegate;
80 class AshNativeCursorManager;
81 class AutoclickController;
82 class CapsLockDelegate;
83 class DesktopBackgroundController;
84 class DisplayController;
87 class HighContrastController;
88 class LockStateController;
89 class MagnificationController;
91 class MruWindowTracker;
92 class NestedDispatcherController;
93 class NewWindowDelegate;
94 class PartialMagnificationController;
95 class PowerButtonController;
96 class WindowTreeHostFactory;
98 class SessionStateDelegate;
101 class ShelfItemDelegateManager;
105 class StickyKeysController;
107 class SystemTrayDelegate;
108 class SystemTrayNotifier;
109 class ToplevelWindowEventHandler;
110 class UserActivityDetector;
111 class UserWallpaperDelegate;
113 class WebNotificationTray;
114 class WindowCycleController;
115 class WindowPositioner;
116 class WindowSelectorController;
119 class AcceleratorFilter;
120 class AppListController;
121 class CaptureController;
122 class DisplayChangeObserver;
123 class DisplayErrorObserver;
124 class DisplayManager;
125 class DragDropController;
126 class EventClientImpl;
127 class EventRewriterEventFilter;
128 class EventTransformationHandler;
130 class KeyboardUMAEventFilter;
131 class LocaleNotificationController;
132 class MouseCursorEventFilter;
133 class OutputConfiguratorAnimation;
134 class OverlayEventFilter;
135 class PowerEventObserver;
136 class ProjectingObserver;
137 class ResizeShadowController;
138 class ResolutionNotificationController;
139 class RootWindowController;
140 class ScopedTargetRootWindow;
141 class ScreenPositionController;
142 class ShelfWindowWatcher;
143 class SlowAnimationEventFilter;
144 class StatusAreaWidget;
145 class SystemGestureEventFilter;
146 class SystemModalContainerEventFilter;
147 class TouchObserverHUD;
148 class UserActivityNotifier;
149 class VideoActivityNotifier;
160 // Shell is a singleton object that presents the Shell API and implements the
161 // RootWindow's delegate interface.
163 // Upon creation, the Shell sets itself as the RootWindow's delegate, which
164 // takes ownership of the Shell.
165 class ASH_EXPORT Shell
166 : public internal::SystemModalContainerEventFilterDelegate,
167 public ui::EventTarget,
168 public aura::client::ActivationChangeObserver {
170 typedef std::vector<internal::RootWindowController*> RootWindowControllerList;
177 // A shell must be explicitly created so that it can call |Init()| with the
178 // delegate set. |delegate| can be NULL (if not required for initialization).
179 // Takes ownership of |delegate|.
180 static Shell* CreateInstance(ShellDelegate* delegate);
182 // Should never be called before |CreateInstance()|.
183 static Shell* GetInstance();
185 // Returns true if the ash shell has been instantiated.
186 static bool HasInstance();
188 static void DeleteInstance();
190 // Returns the root window controller for the primary root window.
191 // TODO(oshima): move this to |RootWindowController|
192 static internal::RootWindowController* GetPrimaryRootWindowController();
194 // Returns all root window controllers.
195 // TODO(oshima): move this to |RootWindowController|
196 static RootWindowControllerList GetAllRootWindowControllers();
198 // Returns the primary root Window. The primary root Window is the one that
200 static aura::Window* GetPrimaryRootWindow();
202 // Returns a root Window when used as a target when creating a new window.
203 // The root window of the active window is used in most cases, but can
204 // be overridden by using ScopedTargetRootWindow().
205 // If you want to get the root Window of the active window, just use
206 // |wm::GetActiveWindow()->GetRootWindow()|.
207 static aura::Window* GetTargetRootWindow();
209 // Returns the global Screen object that's always active in ash.
210 static gfx::Screen* GetScreen();
212 // Returns all root windows.
213 static aura::Window::Windows GetAllRootWindows();
215 static aura::Window* GetContainer(aura::Window* root_window,
217 static const aura::Window* GetContainer(const aura::Window* root_window,
220 // Returns the list of containers that match |container_id| in
221 // all root windows. If |priority_root| is given, the container
222 // in the |priority_root| will be inserted at the top of the list.
223 static std::vector<aura::Window*> GetContainersFromAllRootWindows(
225 aura::Window* priority_root);
227 void set_target_root_window(aura::Window* target_root_window) {
228 target_root_window_ = target_root_window;
231 // Shows the context menu for the background and launcher at
232 // |location_in_screen| (in screen coordinates).
233 void ShowContextMenu(const gfx::Point& location_in_screen,
234 ui::MenuSourceType source_type);
236 // Toggles the app list. |window| specifies in which display the app
237 // list should be shown. If this is NULL, the active root window
239 void ToggleAppList(aura::Window* anchor);
241 // Returns app list target visibility.
242 bool GetAppListTargetVisibility() const;
244 // Returns app list window or NULL if it is not visible.
245 aura::Window* GetAppListWindow();
247 // Returns app list view or NULL if it is not visible.
248 app_list::AppListView* GetAppListView();
250 // Returns true if a system-modal dialog window is currently open.
251 bool IsSystemModalWindowOpen() const;
253 // For testing only: set simulation that a modal window is open
254 void SimulateModalWindowOpenForTesting(bool modal_window_open) {
255 simulate_modal_window_open_for_testing_ = modal_window_open;
258 // Creates a default views::NonClientFrameView for use by windows in the
260 views::NonClientFrameView* CreateDefaultNonClientFrameView(
261 views::Widget* widget);
263 // Rotates focus through containers that can receive focus.
264 void RotateFocus(Direction direction);
266 // Sets the work area insets of the display that contains |window|,
267 // this notifies observers too.
268 // TODO(sky): this no longer really replicates what happens and is unreliable.
270 void SetDisplayWorkAreaInsets(aura::Window* window,
271 const gfx::Insets& insets);
273 // Called when the user logs in.
274 void OnLoginStateChanged(user::LoginStatus status);
276 // Called after the logged-in user's profile is ready.
277 void OnLoginUserProfilePrepared();
279 // Called when the login status changes.
280 // TODO(oshima): Investigate if we can merge this and |OnLoginStateChanged|.
281 void UpdateAfterLoginStatusChange(user::LoginStatus status);
283 // Called when the application is exiting.
284 void OnAppTerminating();
286 // Called when the screen is locked (after the lock window is visible) or
288 void OnLockStateChanged(bool locked);
290 // Called when a casting session is started or stopped.
291 void OnCastingSessionStartedOrStopped(bool started);
293 // Initializes |shelf_|. Does nothing if it's already initialized.
296 // Creates a virtual keyboard. Deletes the old virtual keyboard if it already
298 void CreateKeyboard();
300 // Deactivates the virtual keyboard.
301 void DeactivateKeyboard();
303 // Show shelf view if it was created hidden (before session has started).
306 // Adds/removes observer.
307 void AddShellObserver(ShellObserver* observer);
308 void RemoveShellObserver(ShellObserver* observer);
310 keyboard::KeyboardController* keyboard_controller() {
311 return keyboard_controller_.get();
314 AcceleratorController* accelerator_controller() {
315 return accelerator_controller_.get();
318 internal::DisplayManager* display_manager() {
319 return display_manager_.get();
321 views::corewm::InputMethodEventFilter* input_method_filter() {
322 return input_method_filter_.get();
324 views::corewm::CompoundEventFilter* env_filter() {
325 return env_filter_.get();
327 views::corewm::TooltipController* tooltip_controller() {
328 return tooltip_controller_.get();
330 internal::OverlayEventFilter* overlay_filter() {
331 return overlay_filter_.get();
333 DesktopBackgroundController* desktop_background_controller() {
334 return desktop_background_controller_.get();
336 PowerButtonController* power_button_controller() {
337 return power_button_controller_.get();
339 LockStateController* lock_state_controller() {
340 return lock_state_controller_.get();
342 MruWindowTracker* mru_window_tracker() {
343 return mru_window_tracker_.get();
345 UserActivityDetector* user_activity_detector() {
346 return user_activity_detector_.get();
348 VideoDetector* video_detector() {
349 return video_detector_.get();
351 WindowCycleController* window_cycle_controller() {
352 return window_cycle_controller_.get();
354 WindowSelectorController* window_selector_controller() {
355 return window_selector_controller_.get();
357 internal::FocusCycler* focus_cycler() {
358 return focus_cycler_.get();
360 DisplayController* display_controller() {
361 return display_controller_.get();
363 internal::MouseCursorEventFilter* mouse_cursor_filter() {
364 return mouse_cursor_filter_.get();
366 internal::EventTransformationHandler* event_transformation_handler() {
367 return event_transformation_handler_.get();
369 views::corewm::CursorManager* cursor_manager() { return &cursor_manager_; }
371 ShellDelegate* delegate() { return delegate_.get(); }
373 UserWallpaperDelegate* user_wallpaper_delegate() {
374 return user_wallpaper_delegate_.get();
377 CapsLockDelegate* caps_lock_delegate() {
378 return caps_lock_delegate_.get();
381 SessionStateDelegate* session_state_delegate() {
382 return session_state_delegate_.get();
385 AccessibilityDelegate* accessibility_delegate() {
386 return accessibility_delegate_.get();
389 NewWindowDelegate* new_window_delegate() {
390 return new_window_delegate_.get();
393 MediaDelegate* media_delegate() {
394 return media_delegate_.get();
397 HighContrastController* high_contrast_controller() {
398 return high_contrast_controller_.get();
401 MagnificationController* magnification_controller() {
402 return magnification_controller_.get();
405 PartialMagnificationController* partial_magnification_controller() {
406 return partial_magnification_controller_.get();
409 AutoclickController* autoclick_controller() {
410 return autoclick_controller_.get();
413 aura::client::ActivationClient* activation_client() {
414 return activation_client_;
417 ShelfItemDelegateManager* shelf_item_delegate_manager() {
418 return shelf_item_delegate_manager_.get();
421 // Force the shelf to query for it's current visibility state.
422 void UpdateShelfVisibility();
424 // TODO(oshima): Define an interface to access shelf/launcher
425 // state, or just use Launcher.
427 // Sets/gets the shelf auto-hide behavior on |root_window|.
428 void SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior,
429 aura::Window* root_window);
430 ShelfAutoHideBehavior GetShelfAutoHideBehavior(
431 aura::Window* root_window) const;
433 // Sets/gets shelf's alignment on |root_window|.
434 void SetShelfAlignment(ShelfAlignment alignment,
435 aura::Window* root_window);
436 ShelfAlignment GetShelfAlignment(aura::Window* root_window);
438 // Dims or undims the screen.
439 void SetDimming(bool should_dim);
441 // Notifies |observers_| when entering or exiting fullscreen mode in
443 void NotifyFullscreenStateChange(bool is_fullscreen,
444 aura::Window* root_window);
446 // Creates a modal background (a partially-opaque fullscreen window)
447 // on all displays for |window|.
448 void CreateModalBackground(aura::Window* window);
450 // Called when a modal window is removed. It will activate
451 // another modal window if any, or remove modal screens
453 void OnModalWindowRemoved(aura::Window* removed);
455 // Returns WebNotificationTray on the primary root window.
456 WebNotificationTray* GetWebNotificationTray();
458 // Does the primary display have status area?
459 bool HasPrimaryStatusArea();
461 // Returns the system tray on primary display.
462 SystemTray* GetPrimarySystemTray();
464 SystemTrayDelegate* system_tray_delegate() {
465 return system_tray_delegate_.get();
468 SystemTrayNotifier* system_tray_notifier() {
469 return system_tray_notifier_.get();
472 static void set_initially_hide_cursor(bool hide) {
473 initially_hide_cursor_ = hide;
476 internal::ResizeShadowController* resize_shadow_controller() {
477 return resize_shadow_controller_.get();
480 // Made available for tests.
481 views::corewm::ShadowController* shadow_controller() {
482 return shadow_controller_.get();
485 // Starts the animation that occurs on first login.
486 void DoInitialWorkspaceAnimation();
488 #if defined(OS_CHROMEOS)
490 // TODO(oshima): Move these objects to DisplayController.
491 chromeos::OutputConfigurator* output_configurator() {
492 return output_configurator_.get();
494 internal::OutputConfiguratorAnimation* output_configurator_animation() {
495 return output_configurator_animation_.get();
497 internal::DisplayErrorObserver* display_error_observer() {
498 return display_error_observer_.get();
500 #endif // defined(USE_X11)
502 internal::ResolutionNotificationController*
503 resolution_notification_controller() {
504 return resolution_notification_controller_.get();
506 #endif // defined(OS_CHROMEOS)
508 WindowTreeHostFactory* window_tree_host_factory() {
509 return window_tree_host_factory_.get();
512 ShelfModel* shelf_model() {
513 return shelf_model_.get();
516 WindowPositioner* window_positioner() {
517 return window_positioner_.get();
520 // Returns the launcher delegate, creating if necesary.
521 ShelfDelegate* GetShelfDelegate();
523 UserMetricsRecorder* metrics() {
524 return user_metrics_recorder_.get();
527 void SetTouchHudProjectionEnabled(bool enabled);
529 bool is_touch_hud_projection_enabled() const {
530 return is_touch_hud_projection_enabled_;
533 #if defined(OS_CHROMEOS)
534 // Creates instance of FirstRunHelper. Caller is responsible for deleting
536 ash::FirstRunHelper* CreateFirstRunHelper();
538 // Toggles cursor compositing on/off. Native cursor is disabled when cursor
539 // compositing is enabled, and vice versa.
540 void SetCursorCompositingEnabled(bool enabled);
542 StickyKeysController* sticky_keys_controller() {
543 return sticky_keys_controller_.get();
545 #endif // defined(OS_CHROMEOS)
547 GPUSupport* gpu_support() { return gpu_support_.get(); }
550 FRIEND_TEST_ALL_PREFIXES(ExtendedDesktopTest, TestCursor);
551 FRIEND_TEST_ALL_PREFIXES(WindowManagerTest, MouseEventCursors);
552 FRIEND_TEST_ALL_PREFIXES(WindowManagerTest, TransformActivate);
553 friend class internal::RootWindowController;
554 friend class internal::ScopedTargetRootWindow;
555 friend class test::ShellTestApi;
556 friend class shell::WindowWatcher;
558 typedef std::pair<aura::Window*, gfx::Rect> WindowAndBoundsPair;
560 // Takes ownership of |delegate|.
561 explicit Shell(ShellDelegate* delegate);
566 // Initializes virtual keyboard controller.
569 // Initializes the root window so that it can host browser windows.
570 void InitRootWindow(aura::Window* root_window);
572 // ash::internal::SystemModalContainerEventFilterDelegate overrides:
573 virtual bool CanWindowReceiveEvents(aura::Window* window) OVERRIDE;
575 // Overridden from ui::EventTarget:
576 virtual bool CanAcceptEvent(const ui::Event& event) OVERRIDE;
577 virtual EventTarget* GetParentTarget() OVERRIDE;
578 virtual scoped_ptr<ui::EventTargetIterator> GetChildIterator() const OVERRIDE;
579 virtual ui::EventTargeter* GetEventTargeter() OVERRIDE;
580 virtual void OnEvent(ui::Event* event) OVERRIDE;
582 // Overridden from aura::client::ActivationChangeObserver:
583 virtual void OnWindowActivated(aura::Window* gained_active,
584 aura::Window* lost_active) OVERRIDE;
586 static Shell* instance_;
588 // If set before the Shell is initialized, the mouse cursor will be hidden
589 // when the screen is initially created.
590 static bool initially_hide_cursor_;
592 // When no explicit target display/RootWindow is given, new windows are
593 // created on |scoped_target_root_window_| , unless NULL in
594 // which case they are created on |target_root_window_|.
595 // |target_root_window_| never becomes NULL during the session.
596 aura::Window* target_root_window_;
597 aura::Window* scoped_target_root_window_;
599 // The CompoundEventFilter owned by aura::Env object.
600 scoped_ptr<views::corewm::CompoundEventFilter> env_filter_;
602 std::vector<WindowAndBoundsPair> to_restore_;
604 scoped_ptr<UserMetricsRecorder> user_metrics_recorder_;
605 scoped_ptr<keyboard::KeyboardController> keyboard_controller_;
606 scoped_ptr<NestedDispatcherController> nested_dispatcher_controller_;
607 scoped_ptr<AcceleratorController> accelerator_controller_;
608 scoped_ptr<ShellDelegate> delegate_;
609 scoped_ptr<SystemTrayDelegate> system_tray_delegate_;
610 scoped_ptr<SystemTrayNotifier> system_tray_notifier_;
611 scoped_ptr<UserWallpaperDelegate> user_wallpaper_delegate_;
612 scoped_ptr<CapsLockDelegate> caps_lock_delegate_;
613 scoped_ptr<SessionStateDelegate> session_state_delegate_;
614 scoped_ptr<AccessibilityDelegate> accessibility_delegate_;
615 scoped_ptr<NewWindowDelegate> new_window_delegate_;
616 scoped_ptr<MediaDelegate> media_delegate_;
617 scoped_ptr<ShelfDelegate> shelf_delegate_;
618 scoped_ptr<ShelfItemDelegateManager> shelf_item_delegate_manager_;
619 scoped_ptr<internal::ShelfWindowWatcher> shelf_window_watcher_;
621 scoped_ptr<ShelfModel> shelf_model_;
622 scoped_ptr<WindowPositioner> window_positioner_;
624 scoped_ptr<internal::AppListController> app_list_controller_;
626 scoped_ptr<internal::DragDropController> drag_drop_controller_;
627 scoped_ptr<internal::ResizeShadowController> resize_shadow_controller_;
628 scoped_ptr<views::corewm::ShadowController> shadow_controller_;
629 scoped_ptr<views::corewm::VisibilityController> visibility_controller_;
630 scoped_ptr<views::corewm::WindowModalityController>
631 window_modality_controller_;
632 scoped_ptr<views::corewm::TooltipController> tooltip_controller_;
633 scoped_ptr<DesktopBackgroundController> desktop_background_controller_;
634 scoped_ptr<PowerButtonController> power_button_controller_;
635 scoped_ptr<LockStateController> lock_state_controller_;
636 scoped_ptr<MruWindowTracker> mru_window_tracker_;
637 scoped_ptr<UserActivityDetector> user_activity_detector_;
638 scoped_ptr<VideoDetector> video_detector_;
639 scoped_ptr<WindowCycleController> window_cycle_controller_;
640 scoped_ptr<WindowSelectorController> window_selector_controller_;
641 scoped_ptr<internal::FocusCycler> focus_cycler_;
642 scoped_ptr<DisplayController> display_controller_;
643 scoped_ptr<HighContrastController> high_contrast_controller_;
644 scoped_ptr<MagnificationController> magnification_controller_;
645 scoped_ptr<PartialMagnificationController> partial_magnification_controller_;
646 scoped_ptr<AutoclickController> autoclick_controller_;
647 scoped_ptr<aura::client::FocusClient> focus_client_;
648 scoped_ptr<aura::client::UserActionClient> user_action_client_;
649 aura::client::ActivationClient* activation_client_;
650 scoped_ptr<internal::MouseCursorEventFilter> mouse_cursor_filter_;
651 scoped_ptr<internal::ScreenPositionController> screen_position_controller_;
652 scoped_ptr<internal::SystemModalContainerEventFilter> modality_filter_;
653 scoped_ptr<internal::EventClientImpl> event_client_;
654 scoped_ptr<internal::EventTransformationHandler>
655 event_transformation_handler_;
656 scoped_ptr<WindowTreeHostFactory> window_tree_host_factory_;
658 // An event filter that pre-handles key events while the partial
659 // screenshot UI or the keyboard overlay is active.
660 scoped_ptr<internal::OverlayEventFilter> overlay_filter_;
662 // An event filter for logging keyboard-related metrics.
663 scoped_ptr<internal::KeyboardUMAEventFilter> keyboard_metrics_filter_;
665 // An event filter which handles moving and resizing windows.
666 scoped_ptr<ToplevelWindowEventHandler> toplevel_window_event_handler_;
668 // An event filter which handles system level gestures
669 scoped_ptr<internal::SystemGestureEventFilter> system_gesture_filter_;
671 // An event filter that pre-handles global accelerators.
672 scoped_ptr<internal::AcceleratorFilter> accelerator_filter_;
674 // An event filter that pre-handles all key events to send them to an IME.
675 scoped_ptr<views::corewm::InputMethodEventFilter> input_method_filter_;
677 scoped_ptr<internal::DisplayManager> display_manager_;
678 scoped_ptr<base::WeakPtrFactory<internal::DisplayManager> >
679 weak_display_manager_factory_;
681 scoped_ptr<internal::LocaleNotificationController>
682 locale_notification_controller_;
684 #if defined(OS_CHROMEOS)
685 scoped_ptr<internal::PowerEventObserver> power_event_observer_;
686 scoped_ptr<internal::UserActivityNotifier> user_activity_notifier_;
687 scoped_ptr<internal::VideoActivityNotifier> video_activity_notifier_;
688 scoped_ptr<StickyKeysController> sticky_keys_controller_;
689 scoped_ptr<internal::ResolutionNotificationController>
690 resolution_notification_controller_;
692 // Controls video output device state.
693 scoped_ptr<chromeos::OutputConfigurator> output_configurator_;
694 scoped_ptr<internal::OutputConfiguratorAnimation>
695 output_configurator_animation_;
696 scoped_ptr<internal::DisplayErrorObserver> display_error_observer_;
697 scoped_ptr<internal::ProjectingObserver> projecting_observer_;
699 // Listens for output changes and updates the display manager.
700 scoped_ptr<internal::DisplayChangeObserver> display_change_observer_;
702 scoped_ptr<ui::EventHandler> magnifier_key_scroll_handler_;
703 scoped_ptr<ui::EventHandler> speech_feedback_handler_;
704 #endif // defined(USE_X11)
705 #endif // defined(OS_CHROMEOS)
707 // |native_cursor_manager_| is owned by |cursor_manager_|, but we keep a
708 // pointer to vend to test code.
709 AshNativeCursorManager* native_cursor_manager_;
710 views::corewm::CursorManager cursor_manager_;
712 ObserverList<ShellObserver> observers_;
714 // For testing only: simulate that a modal window is open
715 bool simulate_modal_window_open_for_testing_;
717 bool is_touch_hud_projection_enabled_;
719 // Injected content::GPUDataManager support.
720 scoped_ptr<GPUSupport> gpu_support_;
722 DISALLOW_COPY_AND_ASSIGN(Shell);
727 #endif // ASH_SHELL_H_