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.
10 #include "ash/accelerators/accelerator_controller.h"
11 #include "ash/accelerators/accelerator_filter.h"
12 #include "ash/accelerators/focus_manager_factory.h"
13 #include "ash/accelerators/nested_dispatcher_controller.h"
14 #include "ash/accelerometer/accelerometer_controller.h"
15 #include "ash/ash_switches.h"
16 #include "ash/autoclick/autoclick_controller.h"
17 #include "ash/desktop_background/desktop_background_controller.h"
18 #include "ash/desktop_background/desktop_background_view.h"
19 #include "ash/desktop_background/user_wallpaper_delegate.h"
20 #include "ash/display/cursor_window_controller.h"
21 #include "ash/display/display_controller.h"
22 #include "ash/display/display_manager.h"
23 #include "ash/display/event_transformation_handler.h"
24 #include "ash/display/mouse_cursor_event_filter.h"
25 #include "ash/display/screen_position_controller.h"
26 #include "ash/display/virtual_keyboard_window_controller.h"
27 #include "ash/drag_drop/drag_drop_controller.h"
28 #include "ash/first_run/first_run_helper_impl.h"
29 #include "ash/focus_cycler.h"
30 #include "ash/frame/custom_frame_view_ash.h"
31 #include "ash/gpu_support.h"
32 #include "ash/high_contrast/high_contrast_controller.h"
33 #include "ash/host/window_tree_host_factory.h"
34 #include "ash/keyboard_uma_event_filter.h"
35 #include "ash/magnifier/magnification_controller.h"
36 #include "ash/magnifier/partial_magnification_controller.h"
37 #include "ash/media_delegate.h"
38 #include "ash/new_window_delegate.h"
39 #include "ash/root_window_controller.h"
40 #include "ash/session_state_delegate.h"
41 #include "ash/shelf/app_list_shelf_item_delegate.h"
42 #include "ash/shelf/shelf_delegate.h"
43 #include "ash/shelf/shelf_item_delegate.h"
44 #include "ash/shelf/shelf_item_delegate_manager.h"
45 #include "ash/shelf/shelf_layout_manager.h"
46 #include "ash/shelf/shelf_model.h"
47 #include "ash/shelf/shelf_widget.h"
48 #include "ash/shelf/shelf_window_watcher.h"
49 #include "ash/shell_delegate.h"
50 #include "ash/shell_factory.h"
51 #include "ash/shell_window_ids.h"
52 #include "ash/system/locale/locale_notification_controller.h"
53 #include "ash/system/status_area_widget.h"
54 #include "ash/system/tray/system_tray_delegate.h"
55 #include "ash/system/tray/system_tray_notifier.h"
56 #include "ash/wm/app_list_controller.h"
57 #include "ash/wm/ash_focus_rules.h"
58 #include "ash/wm/ash_native_cursor_manager.h"
59 #include "ash/wm/coordinate_conversion.h"
60 #include "ash/wm/event_client_impl.h"
61 #include "ash/wm/lock_state_controller.h"
62 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
63 #include "ash/wm/maximize_mode/maximize_mode_window_manager.h"
64 #include "ash/wm/mru_window_tracker.h"
65 #include "ash/wm/overlay_event_filter.h"
66 #include "ash/wm/overview/window_selector_controller.h"
67 #include "ash/wm/power_button_controller.h"
68 #include "ash/wm/resize_shadow_controller.h"
69 #include "ash/wm/root_window_layout_manager.h"
70 #include "ash/wm/screen_dimmer.h"
71 #include "ash/wm/system_gesture_event_filter.h"
72 #include "ash/wm/system_modal_container_event_filter.h"
73 #include "ash/wm/system_modal_container_layout_manager.h"
74 #include "ash/wm/toplevel_window_event_handler.h"
75 #include "ash/wm/video_detector.h"
76 #include "ash/wm/window_animations.h"
77 #include "ash/wm/window_positioner.h"
78 #include "ash/wm/window_properties.h"
79 #include "ash/wm/window_util.h"
80 #include "ash/wm/workspace_controller.h"
81 #include "base/bind.h"
82 #include "base/debug/trace_event.h"
83 #include "ui/aura/client/aura_constants.h"
84 #include "ui/aura/env.h"
85 #include "ui/aura/layout_manager.h"
86 #include "ui/aura/window.h"
87 #include "ui/aura/window_event_dispatcher.h"
88 #include "ui/base/ui_base_switches.h"
89 #include "ui/compositor/layer.h"
90 #include "ui/compositor/layer_animator.h"
91 #include "ui/events/event_target_iterator.h"
92 #include "ui/gfx/display.h"
93 #include "ui/gfx/image/image_skia.h"
94 #include "ui/gfx/screen.h"
95 #include "ui/gfx/size.h"
96 #include "ui/keyboard/keyboard.h"
97 #include "ui/keyboard/keyboard_controller.h"
98 #include "ui/keyboard/keyboard_switches.h"
99 #include "ui/keyboard/keyboard_util.h"
100 #include "ui/message_center/message_center.h"
101 #include "ui/views/corewm/tooltip_aura.h"
102 #include "ui/views/corewm/tooltip_controller.h"
103 #include "ui/views/focus/focus_manager_factory.h"
104 #include "ui/views/widget/native_widget_aura.h"
105 #include "ui/views/widget/widget.h"
106 #include "ui/wm/core/compound_event_filter.h"
107 #include "ui/wm/core/focus_controller.h"
108 #include "ui/wm/core/input_method_event_filter.h"
109 #include "ui/wm/core/shadow_controller.h"
110 #include "ui/wm/core/user_activity_detector.h"
111 #include "ui/wm/core/visibility_controller.h"
112 #include "ui/wm/core/window_modality_controller.h"
114 #if defined(OS_CHROMEOS)
116 #include "ash/accelerators/magnifier_key_scroller.h"
117 #include "ash/accelerators/spoken_feedback_toggler.h"
118 #include "base/message_loop/message_pump_x11.h"
119 #endif // defined(USE_X11)
120 #include "ash/ash_constants.h"
121 #include "ash/display/display_change_observer_chromeos.h"
122 #include "ash/display/display_error_observer_chromeos.h"
123 #include "ash/display/output_configurator_animation.h"
124 #include "ash/display/projecting_observer_chromeos.h"
125 #include "ash/display/resolution_notification_controller.h"
126 #include "ash/sticky_keys/sticky_keys_controller.h"
127 #include "ash/system/chromeos/bluetooth/bluetooth_notification_controller.h"
128 #include "ash/system/chromeos/brightness/brightness_controller_chromeos.h"
129 #include "ash/system/chromeos/power/power_event_observer.h"
130 #include "ash/system/chromeos/power/power_status.h"
131 #include "ash/system/chromeos/power/user_activity_notifier.h"
132 #include "ash/system/chromeos/power/video_activity_notifier.h"
133 #include "ash/system/chromeos/session/last_window_closed_logout_reminder.h"
134 #include "ash/system/chromeos/session/logout_confirmation_controller.h"
135 #include "base/bind_helpers.h"
136 #include "base/sys_info.h"
137 #include "ui/display/chromeos/display_configurator.h"
138 #endif // defined(OS_CHROMEOS)
147 // A Corewm VisibilityController subclass that calls the Ash animation routine
148 // so we can pick up our extended animations. See ash/wm/window_animations.h.
149 class AshVisibilityController
: public ::wm::VisibilityController
{
151 AshVisibilityController() {}
152 virtual ~AshVisibilityController() {}
155 // Overridden from ::wm::VisibilityController:
156 virtual bool CallAnimateOnChildWindowVisibilityChanged(
157 aura::Window
* window
,
158 bool visible
) OVERRIDE
{
159 return AnimateOnChildWindowVisibilityChanged(window
, visible
);
162 DISALLOW_COPY_AND_ASSIGN(AshVisibilityController
);
168 Shell
* Shell::instance_
= NULL
;
170 bool Shell::initially_hide_cursor_
= false;
172 ////////////////////////////////////////////////////////////////////////////////
176 Shell
* Shell::CreateInstance(ShellDelegate
* delegate
) {
178 instance_
= new Shell(delegate
);
184 Shell
* Shell::GetInstance() {
190 bool Shell::HasInstance() {
195 void Shell::DeleteInstance() {
201 RootWindowController
* Shell::GetPrimaryRootWindowController() {
202 return GetRootWindowController(GetPrimaryRootWindow());
206 Shell::RootWindowControllerList
Shell::GetAllRootWindowControllers() {
207 return Shell::GetInstance()->display_controller()->
208 GetAllRootWindowControllers();
212 aura::Window
* Shell::GetPrimaryRootWindow() {
213 return GetInstance()->display_controller()->GetPrimaryRootWindow();
217 aura::Window
* Shell::GetTargetRootWindow() {
218 Shell
* shell
= GetInstance();
219 if (shell
->scoped_target_root_window_
)
220 return shell
->scoped_target_root_window_
;
221 return shell
->target_root_window_
;
225 gfx::Screen
* Shell::GetScreen() {
226 return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE
);
230 aura::Window::Windows
Shell::GetAllRootWindows() {
231 return Shell::GetInstance()->display_controller()->
236 aura::Window
* Shell::GetContainer(aura::Window
* root_window
,
238 return root_window
->GetChildById(container_id
);
242 const aura::Window
* Shell::GetContainer(const aura::Window
* root_window
,
244 return root_window
->GetChildById(container_id
);
248 std::vector
<aura::Window
*> Shell::GetContainersFromAllRootWindows(
250 aura::Window
* priority_root
) {
251 std::vector
<aura::Window
*> containers
;
252 aura::Window::Windows root_windows
= GetAllRootWindows();
253 for (aura::Window::Windows::const_iterator it
= root_windows
.begin();
254 it
!= root_windows
.end(); ++it
) {
255 aura::Window
* container
= (*it
)->GetChildById(container_id
);
257 if (priority_root
&& priority_root
->Contains(container
))
258 containers
.insert(containers
.begin(), container
);
260 containers
.push_back(container
);
266 void Shell::ShowContextMenu(const gfx::Point
& location_in_screen
,
267 ui::MenuSourceType source_type
) {
268 // No context menus if there is no session with an active user.
269 if (!session_state_delegate_
->NumberOfLoggedInUsers())
271 // No context menus when screen is locked.
272 if (session_state_delegate_
->IsScreenLocked())
276 wm::GetRootWindowMatching(gfx::Rect(location_in_screen
, gfx::Size()));
277 GetRootWindowController(root
)
278 ->ShowContextMenu(location_in_screen
, source_type
);
281 void Shell::ToggleAppList(aura::Window
* window
) {
282 // If the context window is not given, show it on the target root window.
284 window
= GetTargetRootWindow();
285 if (!app_list_controller_
)
286 app_list_controller_
.reset(new AppListController
);
287 app_list_controller_
->SetVisible(!app_list_controller_
->IsVisible(), window
);
290 bool Shell::GetAppListTargetVisibility() const {
291 return app_list_controller_
.get() &&
292 app_list_controller_
->GetTargetVisibility();
295 aura::Window
* Shell::GetAppListWindow() {
296 return app_list_controller_
.get() ? app_list_controller_
->GetWindow() : NULL
;
299 app_list::AppListView
* Shell::GetAppListView() {
300 return app_list_controller_
.get() ? app_list_controller_
->GetView() : NULL
;
303 bool Shell::IsSystemModalWindowOpen() const {
304 if (simulate_modal_window_open_for_testing_
)
306 const std::vector
<aura::Window
*> containers
= GetContainersFromAllRootWindows(
307 kShellWindowId_SystemModalContainer
, NULL
);
308 for (std::vector
<aura::Window
*>::const_iterator cit
= containers
.begin();
309 cit
!= containers
.end(); ++cit
) {
310 for (aura::Window::Windows::const_iterator wit
= (*cit
)->children().begin();
311 wit
!= (*cit
)->children().end(); ++wit
) {
312 if ((*wit
)->GetProperty(aura::client::kModalKey
) ==
313 ui::MODAL_TYPE_SYSTEM
&& (*wit
)->TargetVisibility()) {
321 views::NonClientFrameView
* Shell::CreateDefaultNonClientFrameView(
322 views::Widget
* widget
) {
323 // Use translucent-style window frames for dialogs.
324 return new CustomFrameViewAsh(widget
);
327 void Shell::RotateFocus(Direction direction
) {
328 focus_cycler_
->RotateFocus(direction
== FORWARD
? FocusCycler::FORWARD
329 : FocusCycler::BACKWARD
);
332 void Shell::SetDisplayWorkAreaInsets(Window
* contains
,
333 const gfx::Insets
& insets
) {
334 if (!display_controller_
->UpdateWorkAreaOfDisplayNearestWindow(
338 FOR_EACH_OBSERVER(ShellObserver
, observers_
,
339 OnDisplayWorkAreaInsetsChanged());
342 void Shell::OnLoginStateChanged(user::LoginStatus status
) {
343 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnLoginStateChanged(status
));
346 void Shell::OnLoginUserProfilePrepared() {
351 void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status
) {
352 RootWindowControllerList controllers
= GetAllRootWindowControllers();
353 for (RootWindowControllerList::iterator iter
= controllers
.begin();
354 iter
!= controllers
.end(); ++iter
)
355 (*iter
)->UpdateAfterLoginStatusChange(status
);
358 void Shell::OnAppTerminating() {
359 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnAppTerminating());
362 void Shell::OnLockStateChanged(bool locked
) {
363 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnLockStateChanged(locked
));
365 // Make sure that there is no system modal in Lock layer when unlocked.
367 std::vector
<aura::Window
*> containers
= GetContainersFromAllRootWindows(
368 kShellWindowId_LockSystemModalContainer
, GetPrimaryRootWindow());
369 for (std::vector
<aura::Window
*>::const_iterator iter
= containers
.begin();
370 iter
!= containers
.end(); ++iter
) {
371 DCHECK_EQ(0u, (*iter
)->children().size());
377 void Shell::OnCastingSessionStartedOrStopped(bool started
) {
378 #if defined(OS_CHROMEOS) && defined(USE_X11)
379 if (projecting_observer_
)
380 projecting_observer_
->OnCastingSessionStartedOrStopped(started
);
384 void Shell::OnOverviewModeStarting() {
385 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnOverviewModeStarting());
388 void Shell::OnOverviewModeEnding() {
389 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnOverviewModeEnding());
392 void Shell::OnMaximizeModeStarted() {
393 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnMaximizeModeStarted());
396 void Shell::OnMaximizeModeEnded() {
397 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnMaximizeModeEnded());
400 void Shell::CreateShelf() {
401 RootWindowControllerList controllers
= GetAllRootWindowControllers();
402 for (RootWindowControllerList::iterator iter
= controllers
.begin();
403 iter
!= controllers
.end(); ++iter
)
404 (*iter
)->shelf()->CreateShelf();
407 void Shell::CreateKeyboard() {
408 // TODO(bshe): Primary root window controller may not be the controller to
409 // attach virtual keyboard. See http://crbug.com/303429
411 if (keyboard::IsKeyboardUsabilityExperimentEnabled()) {
412 display_controller()->virtual_keyboard_window_controller()->
413 ActivateKeyboard(keyboard::KeyboardController::GetInstance());
415 GetPrimaryRootWindowController()->
416 ActivateKeyboard(keyboard::KeyboardController::GetInstance());
420 void Shell::DeactivateKeyboard() {
421 if (keyboard::KeyboardController::GetInstance()) {
422 RootWindowControllerList controllers
= GetAllRootWindowControllers();
423 for (RootWindowControllerList::iterator iter
= controllers
.begin();
424 iter
!= controllers
.end(); ++iter
) {
425 (*iter
)->DeactivateKeyboard(keyboard::KeyboardController::GetInstance());
428 keyboard::KeyboardController::ResetInstance(NULL
);
431 void Shell::ShowShelf() {
432 RootWindowControllerList controllers
= GetAllRootWindowControllers();
433 for (RootWindowControllerList::iterator iter
= controllers
.begin();
434 iter
!= controllers
.end(); ++iter
)
435 (*iter
)->ShowShelf();
438 void Shell::AddShellObserver(ShellObserver
* observer
) {
439 observers_
.AddObserver(observer
);
442 void Shell::RemoveShellObserver(ShellObserver
* observer
) {
443 observers_
.RemoveObserver(observer
);
446 void Shell::EnableMaximizeModeWindowManager(bool enable
) {
447 if (enable
&& !maximize_mode_window_manager_
.get()) {
448 maximize_mode_window_manager_
.reset(new MaximizeModeWindowManager());
449 } else if (!enable
&& maximize_mode_window_manager_
.get()) {
450 maximize_mode_window_manager_
.reset();
454 bool Shell::IsMaximizeModeWindowManagerEnabled() {
455 return maximize_mode_window_manager_
.get() != NULL
;
458 void Shell::UpdateShelfVisibility() {
459 RootWindowControllerList controllers
= GetAllRootWindowControllers();
460 for (RootWindowControllerList::iterator iter
= controllers
.begin();
461 iter
!= controllers
.end(); ++iter
)
462 if ((*iter
)->shelf())
463 (*iter
)->UpdateShelfVisibility();
466 void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior
,
467 aura::Window
* root_window
) {
468 ash::ShelfLayoutManager::ForShelf(root_window
)->SetAutoHideBehavior(behavior
);
471 ShelfAutoHideBehavior
Shell::GetShelfAutoHideBehavior(
472 aura::Window
* root_window
) const {
473 return ash::ShelfLayoutManager::ForShelf(root_window
)->auto_hide_behavior();
476 void Shell::SetShelfAlignment(ShelfAlignment alignment
,
477 aura::Window
* root_window
) {
478 if (ash::ShelfLayoutManager::ForShelf(root_window
)->SetAlignment(alignment
)) {
480 ShellObserver
, observers_
, OnShelfAlignmentChanged(root_window
));
484 ShelfAlignment
Shell::GetShelfAlignment(aura::Window
* root_window
) {
485 return GetRootWindowController(root_window
)
486 ->GetShelfLayoutManager()
490 void Shell::SetDimming(bool should_dim
) {
491 RootWindowControllerList controllers
= GetAllRootWindowControllers();
492 for (RootWindowControllerList::iterator iter
= controllers
.begin();
493 iter
!= controllers
.end(); ++iter
)
494 (*iter
)->screen_dimmer()->SetDimming(should_dim
);
497 void Shell::NotifyFullscreenStateChange(bool is_fullscreen
,
498 aura::Window
* root_window
) {
499 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnFullscreenStateChanged(
500 is_fullscreen
, root_window
));
503 void Shell::CreateModalBackground(aura::Window
* window
) {
504 if (!modality_filter_
) {
505 modality_filter_
.reset(new SystemModalContainerEventFilter(this));
506 AddPreTargetHandler(modality_filter_
.get());
508 RootWindowControllerList controllers
= GetAllRootWindowControllers();
509 for (RootWindowControllerList::iterator iter
= controllers
.begin();
510 iter
!= controllers
.end(); ++iter
)
511 (*iter
)->GetSystemModalLayoutManager(window
)->CreateModalBackground();
514 void Shell::OnModalWindowRemoved(aura::Window
* removed
) {
515 RootWindowControllerList controllers
= GetAllRootWindowControllers();
516 bool activated
= false;
517 for (RootWindowControllerList::iterator iter
= controllers
.begin();
518 iter
!= controllers
.end() && !activated
; ++iter
) {
519 activated
= (*iter
)->GetSystemModalLayoutManager(removed
)->
520 ActivateNextModalWindow();
523 RemovePreTargetHandler(modality_filter_
.get());
524 modality_filter_
.reset();
525 for (RootWindowControllerList::iterator iter
= controllers
.begin();
526 iter
!= controllers
.end(); ++iter
)
527 (*iter
)->GetSystemModalLayoutManager(removed
)->DestroyModalBackground();
531 WebNotificationTray
* Shell::GetWebNotificationTray() {
532 return GetPrimaryRootWindowController()->shelf()->
533 status_area_widget()->web_notification_tray();
536 bool Shell::HasPrimaryStatusArea() {
537 ShelfWidget
* shelf
= GetPrimaryRootWindowController()->shelf();
538 return shelf
&& shelf
->status_area_widget();
541 SystemTray
* Shell::GetPrimarySystemTray() {
542 return GetPrimaryRootWindowController()->GetSystemTray();
545 ShelfDelegate
* Shell::GetShelfDelegate() {
546 if (!shelf_delegate_
) {
547 shelf_model_
.reset(new ShelfModel
);
548 // Creates ShelfItemDelegateManager before ShelfDelegate.
549 shelf_item_delegate_manager_
.reset(
550 new ShelfItemDelegateManager(shelf_model_
.get()));
552 shelf_delegate_
.reset(delegate_
->CreateShelfDelegate(shelf_model_
.get()));
553 scoped_ptr
<ShelfItemDelegate
> controller(new AppListShelfItemDelegate
);
555 // Finding the shelf model's location of the app list and setting its
556 // ShelfItemDelegate.
557 int app_list_index
= shelf_model_
->GetItemIndexForType(TYPE_APP_LIST
);
558 DCHECK_GE(app_list_index
, 0);
559 ShelfID app_list_id
= shelf_model_
->items()[app_list_index
].id
;
561 shelf_item_delegate_manager_
->SetShelfItemDelegate(app_list_id
,
563 shelf_window_watcher_
.reset(new ShelfWindowWatcher(
564 shelf_model_
.get(), shelf_item_delegate_manager_
.get()));
566 return shelf_delegate_
.get();
569 void Shell::SetTouchHudProjectionEnabled(bool enabled
) {
570 if (is_touch_hud_projection_enabled_
== enabled
)
573 is_touch_hud_projection_enabled_
= enabled
;
574 FOR_EACH_OBSERVER(ShellObserver
, observers_
,
575 OnTouchHudProjectionToggled(enabled
));
578 #if defined(OS_CHROMEOS)
579 ash::FirstRunHelper
* Shell::CreateFirstRunHelper() {
580 return new ash::FirstRunHelperImpl
;
583 void Shell::SetCursorCompositingEnabled(bool enabled
) {
584 display_controller_
->cursor_window_controller()->SetCursorCompositingEnabled(
586 native_cursor_manager_
->SetNativeCursorEnabled(!enabled
);
588 #endif // defined(OS_CHROMEOS)
590 void Shell::DoInitialWorkspaceAnimation() {
591 return GetPrimaryRootWindowController()->workspace_controller()->
592 DoInitialAnimation();
595 ////////////////////////////////////////////////////////////////////////////////
598 Shell::Shell(ShellDelegate
* delegate
)
599 : target_root_window_(NULL
),
600 scoped_target_root_window_(NULL
),
602 window_positioner_(new WindowPositioner
),
603 activation_client_(NULL
),
604 accelerometer_controller_(new AccelerometerController()),
605 #if defined(OS_CHROMEOS)
606 display_configurator_(new ui::DisplayConfigurator()),
607 #endif // defined(OS_CHROMEOS)
608 native_cursor_manager_(new AshNativeCursorManager
),
610 scoped_ptr
< ::wm::NativeCursorManager
>(native_cursor_manager_
)),
611 simulate_modal_window_open_for_testing_(false),
612 is_touch_hud_projection_enabled_(false) {
613 DCHECK(delegate_
.get());
614 gpu_support_
.reset(delegate_
->CreateGPUSupport());
615 display_manager_
.reset(new DisplayManager
);
616 display_controller_
.reset(new DisplayController
);
617 #if defined(OS_CHROMEOS) && defined(USE_X11)
618 user_metrics_recorder_
.reset(new UserMetricsRecorder
);
619 #endif // defined(OS_CHROMEOS)
621 #if defined(OS_CHROMEOS)
622 PowerStatus::Initialize();
627 TRACE_EVENT0("shutdown", "ash::Shell::Destructor");
629 delegate_
->PreShutdown();
631 views::FocusManagerFactory::Install(NULL
);
633 // Remove the focus from any window. This will prevent overhead and side
634 // effects (e.g. crashes) from changing focus during shutdown.
635 // See bug crbug.com/134502.
636 aura::client::GetFocusClient(GetPrimaryRootWindow())->FocusWindow(NULL
);
638 // Please keep in same order as in Init() because it's easy to miss one.
639 if (window_modality_controller_
)
640 window_modality_controller_
.reset();
641 #if defined(OS_CHROMEOS) && defined(USE_X11)
642 RemovePreTargetHandler(magnifier_key_scroll_handler_
.get());
643 magnifier_key_scroll_handler_
.reset();
645 RemovePreTargetHandler(speech_feedback_handler_
.get());
646 speech_feedback_handler_
.reset();
648 RemovePreTargetHandler(user_activity_detector_
.get());
649 RemovePreTargetHandler(overlay_filter_
.get());
650 RemovePreTargetHandler(input_method_filter_
.get());
651 RemovePreTargetHandler(accelerator_filter_
.get());
652 RemovePreTargetHandler(event_transformation_handler_
.get());
653 RemovePreTargetHandler(toplevel_window_event_handler_
.get());
654 RemovePostTargetHandler(toplevel_window_event_handler_
.get());
655 RemovePreTargetHandler(system_gesture_filter_
.get());
656 RemovePreTargetHandler(keyboard_metrics_filter_
.get());
657 if (mouse_cursor_filter_
)
658 RemovePreTargetHandler(mouse_cursor_filter_
.get());
660 // TooltipController is deleted with the Shell so removing its references.
661 RemovePreTargetHandler(tooltip_controller_
.get());
663 // Destroy maximize window manager early on since it has some observers which
664 // need to be removed.
665 maximize_mode_controller_
.reset();
666 maximize_mode_window_manager_
.reset();
668 // AppList needs to be released before shelf layout manager, which is
669 // destroyed with shelf container in the loop below. However, app list
670 // container is now on top of shelf container and released after it.
671 // TODO(xiyuan): Move it back when app list container is no longer needed.
672 app_list_controller_
.reset();
674 #if defined(OS_CHROMEOS)
675 // Destroy the LastWindowClosedLogoutReminder before the
676 // LogoutConfirmationController.
677 last_window_closed_logout_reminder_
.reset();
679 // Destroy the LogoutConfirmationController before the SystemTrayDelegate.
680 logout_confirmation_controller_
.reset();
683 // Destroy SystemTrayDelegate before destroying the status area(s).
684 system_tray_delegate_
->Shutdown();
685 system_tray_delegate_
.reset();
687 locale_notification_controller_
.reset();
689 // Drag-and-drop must be canceled prior to close all windows.
690 drag_drop_controller_
.reset();
692 // Controllers who have WindowObserver added must be deleted
693 // before |display_controller_| is deleted.
695 #if defined(OS_CHROMEOS)
696 // VideoActivityNotifier must be deleted before |video_detector_| is
697 // deleted because it's observing video activity through
698 // VideoDetectorObserver interface.
699 video_activity_notifier_
.reset();
700 #endif // defined(OS_CHROMEOS)
701 video_detector_
.reset();
703 shadow_controller_
.reset();
704 resize_shadow_controller_
.reset();
706 window_selector_controller_
.reset();
707 mru_window_tracker_
.reset();
709 // |shelf_window_watcher_| has a weak pointer to |shelf_Model_|
710 // and has window observers.
711 shelf_window_watcher_
.reset();
713 // Destroy all child windows including widgets.
714 display_controller_
->CloseChildWindows();
715 display_controller_
->CloseNonDesktopDisplay();
717 // Chrome implementation of shelf delegate depends on FocusClient,
718 // so must be deleted before |focus_client_|.
719 shelf_delegate_
.reset();
720 focus_client_
.reset();
722 // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems
723 // needs to remove observers from it.
724 system_tray_notifier_
.reset();
726 // These need a valid Shell instance to clean up properly, so explicitly
727 // delete them before invalidating the instance.
728 // Alphabetical. TODO(oshima): sort.
729 magnification_controller_
.reset();
730 partial_magnification_controller_
.reset();
731 tooltip_controller_
.reset();
732 event_client_
.reset();
733 nested_dispatcher_controller_
.reset();
734 toplevel_window_event_handler_
.reset();
735 visibility_controller_
.reset();
736 // |shelf_item_delegate_manager_| observes |shelf_model_|. It must be
737 // destroyed before |shelf_model_| is destroyed.
738 shelf_item_delegate_manager_
.reset();
739 shelf_model_
.reset();
741 power_button_controller_
.reset();
742 lock_state_controller_
.reset();
744 #if defined(OS_CHROMEOS)
745 resolution_notification_controller_
.reset();
747 desktop_background_controller_
.reset();
749 // This also deletes all RootWindows. Note that we invoke Shutdown() on
750 // DisplayController before resetting |display_controller_|, since destruction
751 // of its owned RootWindowControllers relies on the value.
752 display_manager_
->CreateScreenForShutdown();
753 display_controller_
->Shutdown();
754 display_controller_
.reset();
755 screen_position_controller_
.reset();
756 accessibility_delegate_
.reset();
757 new_window_delegate_
.reset();
758 media_delegate_
.reset();
760 keyboard::KeyboardController::ResetInstance(NULL
);
762 #if defined(OS_CHROMEOS)
763 if (display_change_observer_
)
764 display_configurator_
->RemoveObserver(display_change_observer_
.get());
765 if (output_configurator_animation_
)
766 display_configurator_
->RemoveObserver(output_configurator_animation_
.get());
767 if (display_error_observer_
)
768 display_configurator_
->RemoveObserver(display_error_observer_
.get());
769 if (projecting_observer_
)
770 display_configurator_
->RemoveObserver(projecting_observer_
.get());
771 display_change_observer_
.reset();
772 #endif // defined(OS_CHROMEOS)
774 #if defined(OS_CHROMEOS)
775 PowerStatus::Shutdown();
778 DCHECK(instance_
== this);
783 delegate_
->PreInit();
784 if (keyboard::IsKeyboardUsabilityExperimentEnabled()) {
785 display_manager_
->SetSecondDisplayMode(DisplayManager::VIRTUAL_KEYBOARD
);
787 bool display_initialized
= display_manager_
->InitFromCommandLine();
788 #if defined(OS_CHROMEOS)
789 display_configurator_
->Init(!gpu_support_
->IsPanelFittingDisabled());
790 output_configurator_animation_
.reset(new OutputConfiguratorAnimation());
791 display_configurator_
->AddObserver(output_configurator_animation_
.get());
793 projecting_observer_
.reset(new ProjectingObserver());
794 display_configurator_
->AddObserver(projecting_observer_
.get());
796 if (!display_initialized
&& base::SysInfo::IsRunningOnChromeOS()) {
797 display_change_observer_
.reset(new DisplayChangeObserver
);
798 // Register |display_change_observer_| first so that the rest of
799 // observer gets invoked after the root windows are configured.
800 display_configurator_
->AddObserver(display_change_observer_
.get());
801 display_error_observer_
.reset(new DisplayErrorObserver());
802 display_configurator_
->AddObserver(display_error_observer_
.get());
803 display_configurator_
->set_state_controller(display_change_observer_
.get());
804 display_configurator_
->set_mirroring_controller(display_manager_
.get());
805 display_configurator_
->ForceInitialConfigure(
806 delegate_
->IsFirstRunAfterBoot() ? kChromeOsBootColor
: 0);
807 display_initialized
= true;
809 #endif // defined(OS_CHROMEOS)
810 if (!display_initialized
)
811 display_manager_
->InitDefaultDisplay();
813 // Install the custom factory first so that views::FocusManagers for Tray,
814 // Shelf, and WallPaper could be created by the factory.
815 views::FocusManagerFactory::Install(new AshFocusManagerFactory
);
817 // Env creates the compositor. Historically it seems to have been implicitly
818 // initialized first by the ActivationController, but now that FocusController
819 // no longer does this we need to do it explicitly.
820 aura::Env::CreateInstance();
822 // The WindowModalityController needs to be at the front of the input event
823 // pretarget handler list to ensure that it processes input events when modal
824 // windows are active.
825 window_modality_controller_
.reset(
826 new ::wm::WindowModalityController(this));
828 AddPreTargetHandler(this);
830 env_filter_
.reset(new ::wm::CompoundEventFilter
);
831 AddPreTargetHandler(env_filter_
.get());
833 ::wm::FocusController
* focus_controller
=
834 new ::wm::FocusController(new wm::AshFocusRules
);
835 focus_client_
.reset(focus_controller
);
836 activation_client_
= focus_controller
;
837 activation_client_
->AddObserver(this);
838 focus_cycler_
.reset(new FocusCycler());
840 screen_position_controller_
.reset(new ScreenPositionController
);
841 window_tree_host_factory_
.reset(delegate_
->CreateWindowTreeHostFactory());
843 display_controller_
->Start();
844 display_controller_
->InitPrimaryDisplay();
845 aura::Window
* root_window
= display_controller_
->GetPrimaryRootWindow();
846 target_root_window_
= root_window
;
848 #if defined(OS_CHROMEOS)
849 resolution_notification_controller_
.reset(
850 new ResolutionNotificationController
);
853 cursor_manager_
.SetDisplay(GetScreen()->GetPrimaryDisplay());
855 nested_dispatcher_controller_
.reset(new NestedDispatcherController
);
856 accelerator_controller_
.reset(new AcceleratorController
);
857 maximize_mode_controller_
.reset(new MaximizeModeController());
859 #if defined(OS_CHROMEOS) && defined(USE_X11)
860 magnifier_key_scroll_handler_
= MagnifierKeyScroller::CreateHandler().Pass();
861 AddPreTargetHandler(magnifier_key_scroll_handler_
.get());
862 speech_feedback_handler_
= SpokenFeedbackToggler::CreateHandler().Pass();
863 AddPreTargetHandler(speech_feedback_handler_
.get());
866 // The order in which event filters are added is significant.
868 #if defined(OS_CHROMEOS)
869 // The StickyKeysController also rewrites events and must be added
870 // before observers, but after the EventRewriterEventFilter.
871 sticky_keys_controller_
.reset(new StickyKeysController
);
872 AddPreTargetHandler(sticky_keys_controller_
.get());
875 // wm::UserActivityDetector passes events to observers, so let them get
877 user_activity_detector_
.reset(new ::wm::UserActivityDetector
);
878 AddPreTargetHandler(user_activity_detector_
.get());
880 overlay_filter_
.reset(new OverlayEventFilter
);
881 AddPreTargetHandler(overlay_filter_
.get());
882 AddShellObserver(overlay_filter_
.get());
884 input_method_filter_
.reset(new ::wm::InputMethodEventFilter(
885 root_window
->GetHost()->GetAcceleratedWidget()));
886 AddPreTargetHandler(input_method_filter_
.get());
888 accelerator_filter_
.reset(new AcceleratorFilter
);
889 AddPreTargetHandler(accelerator_filter_
.get());
891 event_transformation_handler_
.reset(new EventTransformationHandler
);
892 AddPreTargetHandler(event_transformation_handler_
.get());
894 toplevel_window_event_handler_
.reset(new ToplevelWindowEventHandler
);
896 system_gesture_filter_
.reset(new SystemGestureEventFilter
);
897 AddPreTargetHandler(system_gesture_filter_
.get());
899 keyboard_metrics_filter_
.reset(new KeyboardUMAEventFilter
);
900 AddPreTargetHandler(keyboard_metrics_filter_
.get());
902 // The keyboard system must be initialized before the RootWindowController is
904 #if defined(OS_CHROMEOS)
905 keyboard::InitializeKeyboard();
908 lock_state_controller_
.reset(new LockStateController
);
909 power_button_controller_
.reset(new PowerButtonController(
910 lock_state_controller_
.get()));
911 #if defined(OS_CHROMEOS)
912 // Pass the initial display state to PowerButtonController.
913 power_button_controller_
->OnDisplayModeChanged(
914 display_configurator_
->cached_outputs());
916 AddShellObserver(lock_state_controller_
.get());
918 drag_drop_controller_
.reset(new DragDropController
);
919 mouse_cursor_filter_
.reset(new MouseCursorEventFilter());
920 PrependPreTargetHandler(mouse_cursor_filter_
.get());
922 // Create Controllers that may need root window.
923 // TODO(oshima): Move as many controllers before creating
924 // RootWindowController as possible.
925 visibility_controller_
.reset(new AshVisibilityController
);
927 magnification_controller_
.reset(
928 MagnificationController::CreateInstance());
929 mru_window_tracker_
.reset(new MruWindowTracker(activation_client_
));
931 partial_magnification_controller_
.reset(
932 new PartialMagnificationController());
934 autoclick_controller_
.reset(AutoclickController::CreateInstance());
936 high_contrast_controller_
.reset(new HighContrastController
);
937 video_detector_
.reset(new VideoDetector
);
938 window_selector_controller_
.reset(new WindowSelectorController());
940 tooltip_controller_
.reset(
941 new views::corewm::TooltipController(
942 scoped_ptr
<views::corewm::Tooltip
>(
943 new views::corewm::TooltipAura(gfx::SCREEN_TYPE_ALTERNATE
))));
944 AddPreTargetHandler(tooltip_controller_
.get());
946 event_client_
.reset(new EventClientImpl
);
948 // This controller needs to be set before SetupManagedWindowMode.
949 desktop_background_controller_
.reset(new DesktopBackgroundController());
950 user_wallpaper_delegate_
.reset(delegate_
->CreateUserWallpaperDelegate());
952 session_state_delegate_
.reset(delegate_
->CreateSessionStateDelegate());
953 accessibility_delegate_
.reset(delegate_
->CreateAccessibilityDelegate());
954 new_window_delegate_
.reset(delegate_
->CreateNewWindowDelegate());
955 media_delegate_
.reset(delegate_
->CreateMediaDelegate());
957 resize_shadow_controller_
.reset(new ResizeShadowController());
958 shadow_controller_
.reset(
959 new ::wm::ShadowController(activation_client_
));
961 // Create system_tray_notifier_ before the delegate.
962 system_tray_notifier_
.reset(new ash::SystemTrayNotifier());
964 // Initialize system_tray_delegate_ before initializing StatusAreaWidget.
965 system_tray_delegate_
.reset(delegate()->CreateSystemTrayDelegate());
966 DCHECK(system_tray_delegate_
.get());
968 locale_notification_controller_
.reset(new LocaleNotificationController
);
970 // Initialize system_tray_delegate_ after StatusAreaWidget is created.
971 system_tray_delegate_
->Initialize();
973 #if defined(OS_CHROMEOS)
974 // Create the LogoutConfirmationController after the SystemTrayDelegate.
975 logout_confirmation_controller_
.reset(new LogoutConfirmationController(
976 base::Bind(&SystemTrayDelegate::SignOut
,
977 base::Unretained(system_tray_delegate_
.get()))));
980 // TODO(oshima): Initialize all RootWindowControllers once, and
981 // initialize controller/delegates above when initializing the
982 // primary root window controller.
983 RootWindowController::CreateForPrimaryDisplay(root_window
->GetHost());
985 display_controller_
->InitSecondaryDisplays();
987 // It needs to be created after RootWindowController has been created
988 // (which calls OnWindowResized has been called, otherwise the
989 // widget will not paint when restoring after a browser crash. Also it needs
990 // to be created after InitSecondaryDisplays() to initialize the wallpapers in
992 user_wallpaper_delegate_
->InitializeWallpaper();
994 if (initially_hide_cursor_
)
995 cursor_manager_
.HideCursor();
996 cursor_manager_
.SetCursor(ui::kCursorPointer
);
998 #if defined(OS_CHROMEOS)
999 // Set accelerator controller delegates.
1000 accelerator_controller_
->SetBrightnessControlDelegate(
1001 scoped_ptr
<ash::BrightnessControlDelegate
>(
1002 new ash::system::BrightnessControllerChromeos
).Pass());
1004 power_event_observer_
.reset(new PowerEventObserver());
1005 user_activity_notifier_
.reset(
1006 new UserActivityNotifier(user_activity_detector_
.get()));
1007 video_activity_notifier_
.reset(
1008 new VideoActivityNotifier(video_detector_
.get()));
1009 bluetooth_notification_controller_
.reset(new BluetoothNotificationController
);
1010 last_window_closed_logout_reminder_
.reset(new LastWindowClosedLogoutReminder
);
1013 weak_display_manager_factory_
.reset(
1014 new base::WeakPtrFactory
<DisplayManager
>(display_manager_
.get()));
1015 // The compositor thread and main message loop have to be running in
1016 // order to create mirror window. Run it after the main message loop
1018 base::MessageLoopForUI::current()->PostTask(
1020 base::Bind(&DisplayManager::CreateMirrorWindowIfAny
,
1021 weak_display_manager_factory_
->GetWeakPtr()));
1024 void Shell::InitKeyboard() {
1025 if (keyboard::IsKeyboardEnabled()) {
1026 if (keyboard::KeyboardController::GetInstance()) {
1027 RootWindowControllerList controllers
= GetAllRootWindowControllers();
1028 for (RootWindowControllerList::iterator iter
= controllers
.begin();
1029 iter
!= controllers
.end(); ++iter
) {
1030 (*iter
)->DeactivateKeyboard(
1031 keyboard::KeyboardController::GetInstance());
1034 keyboard::KeyboardControllerProxy
* proxy
=
1035 delegate_
->CreateKeyboardControllerProxy();
1036 keyboard::KeyboardController::ResetInstance(
1037 new keyboard::KeyboardController(proxy
));
1041 void Shell::InitRootWindow(aura::Window
* root_window
) {
1042 DCHECK(activation_client_
);
1043 DCHECK(visibility_controller_
.get());
1044 DCHECK(drag_drop_controller_
.get());
1046 aura::client::SetFocusClient(root_window
, focus_client_
.get());
1047 input_method_filter_
->SetInputMethodPropertyInRootWindow(root_window
);
1048 aura::client::SetActivationClient(root_window
, activation_client_
);
1049 ::wm::FocusController
* focus_controller
=
1050 static_cast< ::wm::FocusController
*>(activation_client_
);
1051 root_window
->AddPreTargetHandler(focus_controller
);
1052 aura::client::SetVisibilityClient(root_window
, visibility_controller_
.get());
1053 aura::client::SetDragDropClient(root_window
, drag_drop_controller_
.get());
1054 aura::client::SetScreenPositionClient(root_window
,
1055 screen_position_controller_
.get());
1056 aura::client::SetCursorClient(root_window
, &cursor_manager_
);
1057 aura::client::SetTooltipClient(root_window
, tooltip_controller_
.get());
1058 aura::client::SetEventClient(root_window
, event_client_
.get());
1060 aura::client::SetWindowMoveClient(root_window
,
1061 toplevel_window_event_handler_
.get());
1062 root_window
->AddPreTargetHandler(toplevel_window_event_handler_
.get());
1063 root_window
->AddPostTargetHandler(toplevel_window_event_handler_
.get());
1065 if (nested_dispatcher_controller_
) {
1066 aura::client::SetDispatcherClient(root_window
,
1067 nested_dispatcher_controller_
.get());
1071 bool Shell::CanWindowReceiveEvents(aura::Window
* window
) {
1072 RootWindowControllerList controllers
= GetAllRootWindowControllers();
1073 for (RootWindowControllerList::iterator iter
= controllers
.begin();
1074 iter
!= controllers
.end(); ++iter
) {
1075 SystemModalContainerLayoutManager
* layout_manager
=
1076 (*iter
)->GetSystemModalLayoutManager(window
);
1077 if (layout_manager
&& layout_manager
->CanWindowReceiveEvents(window
))
1079 // Allow events to fall through to the virtual keyboard even if displaying
1080 // a system modal dialog.
1081 if ((*iter
)->IsVirtualKeyboardWindow(window
))
1088 ////////////////////////////////////////////////////////////////////////////////
1089 // Shell, ui::EventTarget overrides:
1091 bool Shell::CanAcceptEvent(const ui::Event
& event
) {
1095 ui::EventTarget
* Shell::GetParentTarget() {
1096 return aura::Env::GetInstance();
1099 scoped_ptr
<ui::EventTargetIterator
> Shell::GetChildIterator() const {
1100 return scoped_ptr
<ui::EventTargetIterator
>();
1103 ui::EventTargeter
* Shell::GetEventTargeter() {
1108 void Shell::OnEvent(ui::Event
* event
) {
1111 ////////////////////////////////////////////////////////////////////////////////
1112 // Shell, aura::client::ActivationChangeObserver implementation:
1114 void Shell::OnWindowActivated(aura::Window
* gained_active
,
1115 aura::Window
* lost_active
) {
1117 target_root_window_
= gained_active
->GetRootWindow();