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_delegate.h"
12 #include "ash/accelerators/focus_manager_factory.h"
13 #include "ash/accelerators/nested_accelerator_delegate.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/ash_window_tree_host_init_params.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/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_init_params.h"
52 #include "ash/shell_window_ids.h"
53 #include "ash/system/locale/locale_notification_controller.h"
54 #include "ash/system/status_area_widget.h"
55 #include "ash/system/tray/system_tray_delegate.h"
56 #include "ash/system/tray/system_tray_notifier.h"
57 #include "ash/wm/app_list_controller.h"
58 #include "ash/wm/ash_focus_rules.h"
59 #include "ash/wm/ash_native_cursor_manager.h"
60 #include "ash/wm/coordinate_conversion.h"
61 #include "ash/wm/event_client_impl.h"
62 #include "ash/wm/lock_state_controller.h"
63 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
64 #include "ash/wm/maximize_mode/maximize_mode_window_manager.h"
65 #include "ash/wm/mru_window_tracker.h"
66 #include "ash/wm/overlay_event_filter.h"
67 #include "ash/wm/overview/window_selector_controller.h"
68 #include "ash/wm/power_button_controller.h"
69 #include "ash/wm/resize_shadow_controller.h"
70 #include "ash/wm/root_window_layout_manager.h"
71 #include "ash/wm/screen_dimmer.h"
72 #include "ash/wm/system_gesture_event_filter.h"
73 #include "ash/wm/system_modal_container_event_filter.h"
74 #include "ash/wm/system_modal_container_layout_manager.h"
75 #include "ash/wm/toplevel_window_event_handler.h"
76 #include "ash/wm/video_detector.h"
77 #include "ash/wm/window_animations.h"
78 #include "ash/wm/window_cycle_controller.h"
79 #include "ash/wm/window_positioner.h"
80 #include "ash/wm/window_properties.h"
81 #include "ash/wm/window_util.h"
82 #include "ash/wm/workspace_controller.h"
83 #include "base/bind.h"
84 #include "base/debug/trace_event.h"
85 #include "ui/aura/client/aura_constants.h"
86 #include "ui/aura/env.h"
87 #include "ui/aura/layout_manager.h"
88 #include "ui/aura/window.h"
89 #include "ui/aura/window_event_dispatcher.h"
90 #include "ui/base/ui_base_switches.h"
91 #include "ui/compositor/layer.h"
92 #include "ui/compositor/layer_animator.h"
93 #include "ui/events/event_target_iterator.h"
94 #include "ui/gfx/display.h"
95 #include "ui/gfx/image/image_skia.h"
96 #include "ui/gfx/screen.h"
97 #include "ui/gfx/size.h"
98 #include "ui/keyboard/keyboard.h"
99 #include "ui/keyboard/keyboard_controller.h"
100 #include "ui/keyboard/keyboard_switches.h"
101 #include "ui/keyboard/keyboard_util.h"
102 #include "ui/message_center/message_center.h"
103 #include "ui/views/corewm/tooltip_aura.h"
104 #include "ui/views/corewm/tooltip_controller.h"
105 #include "ui/views/focus/focus_manager_factory.h"
106 #include "ui/views/widget/native_widget_aura.h"
107 #include "ui/views/widget/widget.h"
108 #include "ui/wm/core/accelerator_filter.h"
109 #include "ui/wm/core/compound_event_filter.h"
110 #include "ui/wm/core/focus_controller.h"
111 #include "ui/wm/core/input_method_event_filter.h"
112 #include "ui/wm/core/nested_accelerator_controller.h"
113 #include "ui/wm/core/shadow_controller.h"
114 #include "ui/wm/core/user_activity_detector.h"
115 #include "ui/wm/core/visibility_controller.h"
116 #include "ui/wm/core/window_modality_controller.h"
118 #if defined(OS_CHROMEOS)
120 #include "ash/accelerators/magnifier_key_scroller.h"
121 #include "ash/accelerators/spoken_feedback_toggler.h"
122 #include "ash/touch/touch_transformer_controller.h"
123 #include "ui/gfx/x/x11_types.h"
124 #endif // defined(USE_X11)
125 #include "ash/ash_constants.h"
126 #include "ash/display/display_change_observer_chromeos.h"
127 #include "ash/display/display_configurator_animation.h"
128 #include "ash/display/display_error_observer_chromeos.h"
129 #include "ash/display/projecting_observer_chromeos.h"
130 #include "ash/display/resolution_notification_controller.h"
131 #include "ash/sticky_keys/sticky_keys_controller.h"
132 #include "ash/system/chromeos/bluetooth/bluetooth_notification_controller.h"
133 #include "ash/system/chromeos/brightness/brightness_controller_chromeos.h"
134 #include "ash/system/chromeos/power/power_event_observer.h"
135 #include "ash/system/chromeos/power/power_status.h"
136 #include "ash/system/chromeos/power/video_activity_notifier.h"
137 #include "ash/system/chromeos/session/last_window_closed_logout_reminder.h"
138 #include "ash/system/chromeos/session/logout_confirmation_controller.h"
139 #include "base/bind_helpers.h"
140 #include "base/sys_info.h"
141 #include "ui/chromeos/user_activity_power_manager_notifier.h"
142 #include "ui/display/chromeos/display_configurator.h"
143 #endif // defined(OS_CHROMEOS)
152 // A Corewm VisibilityController subclass that calls the Ash animation routine
153 // so we can pick up our extended animations. See ash/wm/window_animations.h.
154 class AshVisibilityController
: public ::wm::VisibilityController
{
156 AshVisibilityController() {}
157 virtual ~AshVisibilityController() {}
160 // Overridden from ::wm::VisibilityController:
161 virtual bool CallAnimateOnChildWindowVisibilityChanged(
162 aura::Window
* window
,
163 bool visible
) OVERRIDE
{
164 return AnimateOnChildWindowVisibilityChanged(window
, visible
);
167 DISALLOW_COPY_AND_ASSIGN(AshVisibilityController
);
170 AshWindowTreeHostInitParams
ShellInitParamsToAshWindowTreeHostInitParams(
171 const ShellInitParams
& shell_init_params
) {
172 AshWindowTreeHostInitParams ash_init_params
;
174 ash_init_params
.remote_hwnd
= shell_init_params
.remote_hwnd
;
176 return ash_init_params
;
182 Shell
* Shell::instance_
= NULL
;
184 bool Shell::initially_hide_cursor_
= false;
186 ////////////////////////////////////////////////////////////////////////////////
190 Shell
* Shell::CreateInstance(const ShellInitParams
& init_params
) {
192 instance_
= new Shell(init_params
.delegate
);
193 instance_
->Init(init_params
);
198 Shell
* Shell::GetInstance() {
204 bool Shell::HasInstance() {
209 void Shell::DeleteInstance() {
215 RootWindowController
* Shell::GetPrimaryRootWindowController() {
216 return GetRootWindowController(GetPrimaryRootWindow());
220 Shell::RootWindowControllerList
Shell::GetAllRootWindowControllers() {
221 return Shell::GetInstance()->display_controller()->
222 GetAllRootWindowControllers();
226 aura::Window
* Shell::GetPrimaryRootWindow() {
227 return GetInstance()->display_controller()->GetPrimaryRootWindow();
231 aura::Window
* Shell::GetTargetRootWindow() {
232 Shell
* shell
= GetInstance();
233 if (shell
->scoped_target_root_window_
)
234 return shell
->scoped_target_root_window_
;
235 return shell
->target_root_window_
;
239 gfx::Screen
* Shell::GetScreen() {
240 return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE
);
244 aura::Window::Windows
Shell::GetAllRootWindows() {
245 return Shell::GetInstance()->display_controller()->
250 aura::Window
* Shell::GetContainer(aura::Window
* root_window
,
252 return root_window
->GetChildById(container_id
);
256 const aura::Window
* Shell::GetContainer(const aura::Window
* root_window
,
258 return root_window
->GetChildById(container_id
);
262 std::vector
<aura::Window
*> Shell::GetContainersFromAllRootWindows(
264 aura::Window
* priority_root
) {
265 std::vector
<aura::Window
*> containers
;
266 aura::Window::Windows root_windows
= GetAllRootWindows();
267 for (aura::Window::Windows::const_iterator it
= root_windows
.begin();
268 it
!= root_windows
.end(); ++it
) {
269 aura::Window
* container
= (*it
)->GetChildById(container_id
);
271 if (priority_root
&& priority_root
->Contains(container
))
272 containers
.insert(containers
.begin(), container
);
274 containers
.push_back(container
);
280 void Shell::ShowContextMenu(const gfx::Point
& location_in_screen
,
281 ui::MenuSourceType source_type
) {
282 // No context menus if there is no session with an active user.
283 if (!session_state_delegate_
->NumberOfLoggedInUsers())
285 // No context menus when screen is locked.
286 if (session_state_delegate_
->IsScreenLocked())
290 wm::GetRootWindowMatching(gfx::Rect(location_in_screen
, gfx::Size()));
291 GetRootWindowController(root
)
292 ->ShowContextMenu(location_in_screen
, source_type
);
295 void Shell::ToggleAppList(aura::Window
* window
) {
296 // If the context window is not given, show it on the target root window.
298 window
= GetTargetRootWindow();
299 if (!app_list_controller_
)
300 app_list_controller_
.reset(new AppListController
);
301 app_list_controller_
->SetVisible(!app_list_controller_
->IsVisible(), window
);
304 bool Shell::GetAppListTargetVisibility() const {
305 return app_list_controller_
.get() &&
306 app_list_controller_
->GetTargetVisibility();
309 aura::Window
* Shell::GetAppListWindow() {
310 return app_list_controller_
.get() ? app_list_controller_
->GetWindow() : NULL
;
313 app_list::AppListView
* Shell::GetAppListView() {
314 return app_list_controller_
.get() ? app_list_controller_
->GetView() : NULL
;
317 bool Shell::IsSystemModalWindowOpen() const {
318 if (simulate_modal_window_open_for_testing_
)
320 const std::vector
<aura::Window
*> containers
= GetContainersFromAllRootWindows(
321 kShellWindowId_SystemModalContainer
, NULL
);
322 for (std::vector
<aura::Window
*>::const_iterator cit
= containers
.begin();
323 cit
!= containers
.end(); ++cit
) {
324 for (aura::Window::Windows::const_iterator wit
= (*cit
)->children().begin();
325 wit
!= (*cit
)->children().end(); ++wit
) {
326 if ((*wit
)->GetProperty(aura::client::kModalKey
) ==
327 ui::MODAL_TYPE_SYSTEM
&& (*wit
)->TargetVisibility()) {
335 views::NonClientFrameView
* Shell::CreateDefaultNonClientFrameView(
336 views::Widget
* widget
) {
337 // Use translucent-style window frames for dialogs.
338 return new CustomFrameViewAsh(widget
);
341 void Shell::RotateFocus(Direction direction
) {
342 focus_cycler_
->RotateFocus(direction
== FORWARD
? FocusCycler::FORWARD
343 : FocusCycler::BACKWARD
);
346 void Shell::SetDisplayWorkAreaInsets(Window
* contains
,
347 const gfx::Insets
& insets
) {
348 if (!display_controller_
->UpdateWorkAreaOfDisplayNearestWindow(
352 FOR_EACH_OBSERVER(ShellObserver
, observers_
,
353 OnDisplayWorkAreaInsetsChanged());
356 void Shell::OnLoginStateChanged(user::LoginStatus status
) {
357 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnLoginStateChanged(status
));
360 void Shell::OnLoginUserProfilePrepared() {
365 void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status
) {
366 RootWindowControllerList controllers
= GetAllRootWindowControllers();
367 for (RootWindowControllerList::iterator iter
= controllers
.begin();
368 iter
!= controllers
.end(); ++iter
)
369 (*iter
)->UpdateAfterLoginStatusChange(status
);
372 void Shell::OnAppTerminating() {
373 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnAppTerminating());
376 void Shell::OnLockStateChanged(bool locked
) {
377 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnLockStateChanged(locked
));
379 // Make sure that there is no system modal in Lock layer when unlocked.
381 std::vector
<aura::Window
*> containers
= GetContainersFromAllRootWindows(
382 kShellWindowId_LockSystemModalContainer
, GetPrimaryRootWindow());
383 for (std::vector
<aura::Window
*>::const_iterator iter
= containers
.begin();
384 iter
!= containers
.end(); ++iter
) {
385 DCHECK_EQ(0u, (*iter
)->children().size());
391 void Shell::OnCastingSessionStartedOrStopped(bool started
) {
392 #if defined(OS_CHROMEOS) && defined(USE_X11)
393 if (projecting_observer_
)
394 projecting_observer_
->OnCastingSessionStartedOrStopped(started
);
398 void Shell::OnOverviewModeStarting() {
399 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnOverviewModeStarting());
402 void Shell::OnOverviewModeEnding() {
403 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnOverviewModeEnding());
406 void Shell::OnMaximizeModeStarted() {
407 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnMaximizeModeStarted());
410 void Shell::OnMaximizeModeEnded() {
411 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnMaximizeModeEnded());
414 void Shell::OnRootWindowAdded(aura::Window
* root_window
) {
415 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnRootWindowAdded(root_window
));
418 void Shell::CreateShelf() {
419 RootWindowControllerList controllers
= GetAllRootWindowControllers();
420 for (RootWindowControllerList::iterator iter
= controllers
.begin();
421 iter
!= controllers
.end(); ++iter
)
422 (*iter
)->shelf()->CreateShelf();
425 void Shell::OnShelfCreatedForRootWindow(aura::Window
* root_window
) {
426 FOR_EACH_OBSERVER(ShellObserver
,
428 OnShelfCreatedForRootWindow(root_window
));
431 void Shell::CreateKeyboard() {
432 // TODO(bshe): Primary root window controller may not be the controller to
433 // attach virtual keyboard. See http://crbug.com/303429
435 if (keyboard::IsKeyboardUsabilityExperimentEnabled()) {
436 display_controller()->virtual_keyboard_window_controller()->
437 ActivateKeyboard(keyboard::KeyboardController::GetInstance());
439 GetPrimaryRootWindowController()->
440 ActivateKeyboard(keyboard::KeyboardController::GetInstance());
444 void Shell::DeactivateKeyboard() {
445 if (keyboard::KeyboardController::GetInstance()) {
446 RootWindowControllerList controllers
= GetAllRootWindowControllers();
447 for (RootWindowControllerList::iterator iter
= controllers
.begin();
448 iter
!= controllers
.end(); ++iter
) {
449 (*iter
)->DeactivateKeyboard(keyboard::KeyboardController::GetInstance());
452 keyboard::KeyboardController::ResetInstance(NULL
);
455 void Shell::ShowShelf() {
456 RootWindowControllerList controllers
= GetAllRootWindowControllers();
457 for (RootWindowControllerList::iterator iter
= controllers
.begin();
458 iter
!= controllers
.end(); ++iter
)
459 (*iter
)->ShowShelf();
462 void Shell::AddShellObserver(ShellObserver
* observer
) {
463 observers_
.AddObserver(observer
);
466 void Shell::RemoveShellObserver(ShellObserver
* observer
) {
467 observers_
.RemoveObserver(observer
);
470 #if defined(OS_CHROMEOS)
471 bool Shell::ShouldSaveDisplaySettings() {
472 return !((maximize_mode_controller_
->IsMaximizeModeWindowManagerEnabled() &&
473 maximize_mode_controller_
->in_set_screen_rotation()) ||
474 resolution_notification_controller_
->DoesNotificationTimeout());
478 void Shell::UpdateShelfVisibility() {
479 RootWindowControllerList controllers
= GetAllRootWindowControllers();
480 for (RootWindowControllerList::iterator iter
= controllers
.begin();
481 iter
!= controllers
.end(); ++iter
)
482 if ((*iter
)->shelf())
483 (*iter
)->UpdateShelfVisibility();
486 void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior
,
487 aura::Window
* root_window
) {
488 ash::ShelfLayoutManager::ForShelf(root_window
)->SetAutoHideBehavior(behavior
);
491 ShelfAutoHideBehavior
Shell::GetShelfAutoHideBehavior(
492 aura::Window
* root_window
) const {
493 return ash::ShelfLayoutManager::ForShelf(root_window
)->auto_hide_behavior();
496 void Shell::SetShelfAlignment(ShelfAlignment alignment
,
497 aura::Window
* root_window
) {
498 if (ash::ShelfLayoutManager::ForShelf(root_window
)->SetAlignment(alignment
)) {
500 ShellObserver
, observers_
, OnShelfAlignmentChanged(root_window
));
504 ShelfAlignment
Shell::GetShelfAlignment(const aura::Window
* root_window
) {
505 return GetRootWindowController(root_window
)
506 ->GetShelfLayoutManager()
510 void Shell::SetDimming(bool should_dim
) {
511 RootWindowControllerList controllers
= GetAllRootWindowControllers();
512 for (RootWindowControllerList::iterator iter
= controllers
.begin();
513 iter
!= controllers
.end(); ++iter
)
514 (*iter
)->screen_dimmer()->SetDimming(should_dim
);
517 void Shell::NotifyFullscreenStateChange(bool is_fullscreen
,
518 aura::Window
* root_window
) {
519 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnFullscreenStateChanged(
520 is_fullscreen
, root_window
));
523 void Shell::CreateModalBackground(aura::Window
* window
) {
524 if (!modality_filter_
) {
525 modality_filter_
.reset(new SystemModalContainerEventFilter(this));
526 AddPreTargetHandler(modality_filter_
.get());
528 RootWindowControllerList controllers
= GetAllRootWindowControllers();
529 for (RootWindowControllerList::iterator iter
= controllers
.begin();
530 iter
!= controllers
.end(); ++iter
)
531 (*iter
)->GetSystemModalLayoutManager(window
)->CreateModalBackground();
534 void Shell::OnModalWindowRemoved(aura::Window
* removed
) {
535 RootWindowControllerList controllers
= GetAllRootWindowControllers();
536 bool activated
= false;
537 for (RootWindowControllerList::iterator iter
= controllers
.begin();
538 iter
!= controllers
.end() && !activated
; ++iter
) {
539 activated
= (*iter
)->GetSystemModalLayoutManager(removed
)->
540 ActivateNextModalWindow();
543 RemovePreTargetHandler(modality_filter_
.get());
544 modality_filter_
.reset();
545 for (RootWindowControllerList::iterator iter
= controllers
.begin();
546 iter
!= controllers
.end(); ++iter
)
547 (*iter
)->GetSystemModalLayoutManager(removed
)->DestroyModalBackground();
551 WebNotificationTray
* Shell::GetWebNotificationTray() {
552 return GetPrimaryRootWindowController()->shelf()->
553 status_area_widget()->web_notification_tray();
556 bool Shell::HasPrimaryStatusArea() {
557 ShelfWidget
* shelf
= GetPrimaryRootWindowController()->shelf();
558 return shelf
&& shelf
->status_area_widget();
561 SystemTray
* Shell::GetPrimarySystemTray() {
562 return GetPrimaryRootWindowController()->GetSystemTray();
565 ShelfDelegate
* Shell::GetShelfDelegate() {
566 if (!shelf_delegate_
) {
567 shelf_model_
.reset(new ShelfModel
);
568 // Creates ShelfItemDelegateManager before ShelfDelegate.
569 shelf_item_delegate_manager_
.reset(
570 new ShelfItemDelegateManager(shelf_model_
.get()));
572 shelf_delegate_
.reset(delegate_
->CreateShelfDelegate(shelf_model_
.get()));
573 scoped_ptr
<ShelfItemDelegate
> controller(new AppListShelfItemDelegate
);
575 // Finding the shelf model's location of the app list and setting its
576 // ShelfItemDelegate.
577 int app_list_index
= shelf_model_
->GetItemIndexForType(TYPE_APP_LIST
);
578 DCHECK_GE(app_list_index
, 0);
579 ShelfID app_list_id
= shelf_model_
->items()[app_list_index
].id
;
581 shelf_item_delegate_manager_
->SetShelfItemDelegate(app_list_id
,
583 shelf_window_watcher_
.reset(new ShelfWindowWatcher(
584 shelf_model_
.get(), shelf_item_delegate_manager_
.get()));
586 return shelf_delegate_
.get();
589 void Shell::SetTouchHudProjectionEnabled(bool enabled
) {
590 if (is_touch_hud_projection_enabled_
== enabled
)
593 is_touch_hud_projection_enabled_
= enabled
;
594 FOR_EACH_OBSERVER(ShellObserver
, observers_
,
595 OnTouchHudProjectionToggled(enabled
));
598 #if defined(OS_CHROMEOS)
599 ash::FirstRunHelper
* Shell::CreateFirstRunHelper() {
600 return new ash::FirstRunHelperImpl
;
603 void Shell::SetCursorCompositingEnabled(bool enabled
) {
604 display_controller_
->cursor_window_controller()->SetCursorCompositingEnabled(
606 native_cursor_manager_
->SetNativeCursorEnabled(!enabled
);
608 #endif // defined(OS_CHROMEOS)
610 void Shell::DoInitialWorkspaceAnimation() {
611 return GetPrimaryRootWindowController()->workspace_controller()->
612 DoInitialAnimation();
615 ////////////////////////////////////////////////////////////////////////////////
618 Shell::Shell(ShellDelegate
* delegate
)
619 : target_root_window_(NULL
),
620 scoped_target_root_window_(NULL
),
622 window_positioner_(new WindowPositioner
),
623 activation_client_(NULL
),
624 accelerometer_controller_(new AccelerometerController()),
625 #if defined(OS_CHROMEOS)
626 display_configurator_(new ui::DisplayConfigurator()),
627 #endif // defined(OS_CHROMEOS)
628 native_cursor_manager_(new AshNativeCursorManager
),
630 scoped_ptr
< ::wm::NativeCursorManager
>(native_cursor_manager_
)),
631 simulate_modal_window_open_for_testing_(false),
632 is_touch_hud_projection_enabled_(false) {
633 DCHECK(delegate_
.get());
634 gpu_support_
.reset(delegate_
->CreateGPUSupport());
635 display_manager_
.reset(new DisplayManager
);
636 display_controller_
.reset(new DisplayController
);
637 #if defined(OS_CHROMEOS) && defined(USE_X11)
638 user_metrics_recorder_
.reset(new UserMetricsRecorder
);
639 #endif // defined(OS_CHROMEOS)
641 #if defined(OS_CHROMEOS)
642 PowerStatus::Initialize();
647 TRACE_EVENT0("shutdown", "ash::Shell::Destructor");
649 delegate_
->PreShutdown();
651 views::FocusManagerFactory::Install(NULL
);
653 // Remove the focus from any window. This will prevent overhead and side
654 // effects (e.g. crashes) from changing focus during shutdown.
655 // See bug crbug.com/134502.
656 aura::client::GetFocusClient(GetPrimaryRootWindow())->FocusWindow(NULL
);
658 // Please keep in same order as in Init() because it's easy to miss one.
659 if (window_modality_controller_
)
660 window_modality_controller_
.reset();
661 #if defined(OS_CHROMEOS) && defined(USE_X11)
662 RemovePreTargetHandler(magnifier_key_scroll_handler_
.get());
663 magnifier_key_scroll_handler_
.reset();
665 RemovePreTargetHandler(speech_feedback_handler_
.get());
666 speech_feedback_handler_
.reset();
668 RemovePreTargetHandler(user_activity_detector_
.get());
669 RemovePreTargetHandler(overlay_filter_
.get());
670 RemovePreTargetHandler(input_method_filter_
.get());
671 RemovePreTargetHandler(accelerator_filter_
.get());
672 RemovePreTargetHandler(event_transformation_handler_
.get());
673 RemovePreTargetHandler(toplevel_window_event_handler_
.get());
674 RemovePostTargetHandler(toplevel_window_event_handler_
.get());
675 RemovePreTargetHandler(system_gesture_filter_
.get());
676 RemovePreTargetHandler(keyboard_metrics_filter_
.get());
677 RemovePreTargetHandler(mouse_cursor_filter_
.get());
679 // TooltipController is deleted with the Shell so removing its references.
680 RemovePreTargetHandler(tooltip_controller_
.get());
682 // Destroy maximize mode controller early on since it has some observers which
683 // need to be removed.
684 maximize_mode_controller_
->Shutdown();
685 maximize_mode_controller_
.reset();
687 // AppList needs to be released before shelf layout manager, which is
688 // destroyed with shelf container in the loop below. However, app list
689 // container is now on top of shelf container and released after it.
690 // TODO(xiyuan): Move it back when app list container is no longer needed.
691 app_list_controller_
.reset();
693 #if defined(OS_CHROMEOS)
694 // Destroy the LastWindowClosedLogoutReminder before the
695 // LogoutConfirmationController.
696 last_window_closed_logout_reminder_
.reset();
698 // Destroy the LogoutConfirmationController before the SystemTrayDelegate.
699 logout_confirmation_controller_
.reset();
702 // Destroy SystemTrayDelegate before destroying the status area(s).
703 system_tray_delegate_
->Shutdown();
704 system_tray_delegate_
.reset();
706 locale_notification_controller_
.reset();
708 // Drag-and-drop must be canceled prior to close all windows.
709 drag_drop_controller_
.reset();
711 // Controllers who have WindowObserver added must be deleted
712 // before |display_controller_| is deleted.
714 #if defined(OS_CHROMEOS)
715 // VideoActivityNotifier must be deleted before |video_detector_| is
716 // deleted because it's observing video activity through
717 // VideoDetectorObserver interface.
718 video_activity_notifier_
.reset();
719 #endif // defined(OS_CHROMEOS)
720 video_detector_
.reset();
721 high_contrast_controller_
.reset();
723 shadow_controller_
.reset();
724 resize_shadow_controller_
.reset();
726 window_cycle_controller_
.reset();
727 window_selector_controller_
.reset();
728 mru_window_tracker_
.reset();
730 // |shelf_window_watcher_| has a weak pointer to |shelf_Model_|
731 // and has window observers.
732 shelf_window_watcher_
.reset();
734 // Destroy all child windows including widgets.
735 display_controller_
->CloseChildWindows();
736 display_controller_
->CloseNonDesktopDisplay();
738 // Chrome implementation of shelf delegate depends on FocusClient,
739 // so must be deleted before |focus_client_|.
740 shelf_delegate_
.reset();
741 focus_client_
.reset();
743 // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems
744 // needs to remove observers from it.
745 system_tray_notifier_
.reset();
747 // These need a valid Shell instance to clean up properly, so explicitly
748 // delete them before invalidating the instance.
749 // Alphabetical. TODO(oshima): sort.
750 magnification_controller_
.reset();
751 partial_magnification_controller_
.reset();
752 tooltip_controller_
.reset();
753 event_client_
.reset();
754 nested_accelerator_controller_
.reset();
755 toplevel_window_event_handler_
.reset();
756 visibility_controller_
.reset();
757 // |shelf_item_delegate_manager_| observes |shelf_model_|. It must be
758 // destroyed before |shelf_model_| is destroyed.
759 shelf_item_delegate_manager_
.reset();
760 shelf_model_
.reset();
762 power_button_controller_
.reset();
763 lock_state_controller_
.reset();
765 #if defined(OS_CHROMEOS)
766 resolution_notification_controller_
.reset();
768 desktop_background_controller_
.reset();
769 mouse_cursor_filter_
.reset();
771 #if defined(OS_CHROMEOS) && defined(USE_X11)
772 touch_transformer_controller_
.reset();
773 #endif // defined(OS_CHROMEOS) && defined(USE_X11)
775 // This also deletes all RootWindows. Note that we invoke Shutdown() on
776 // DisplayController before resetting |display_controller_|, since destruction
777 // of its owned RootWindowControllers relies on the value.
778 display_manager_
->CreateScreenForShutdown();
779 display_controller_
->Shutdown();
780 display_controller_
.reset();
781 screen_position_controller_
.reset();
782 accessibility_delegate_
.reset();
783 new_window_delegate_
.reset();
784 media_delegate_
.reset();
786 keyboard::KeyboardController::ResetInstance(NULL
);
788 #if defined(OS_CHROMEOS)
789 if (display_change_observer_
)
790 display_configurator_
->RemoveObserver(display_change_observer_
.get());
791 if (display_configurator_animation_
)
792 display_configurator_
->RemoveObserver(
793 display_configurator_animation_
.get());
794 if (display_error_observer_
)
795 display_configurator_
->RemoveObserver(display_error_observer_
.get());
796 if (projecting_observer_
)
797 display_configurator_
->RemoveObserver(projecting_observer_
.get());
798 display_change_observer_
.reset();
799 #endif // defined(OS_CHROMEOS)
801 #if defined(OS_CHROMEOS)
802 PowerStatus::Shutdown();
805 DCHECK(instance_
== this);
809 void Shell::Init(const ShellInitParams
& init_params
) {
810 delegate_
->PreInit();
811 if (keyboard::IsKeyboardUsabilityExperimentEnabled()) {
812 display_manager_
->SetSecondDisplayMode(DisplayManager::VIRTUAL_KEYBOARD
);
814 bool display_initialized
= display_manager_
->InitFromCommandLine();
815 #if defined(OS_CHROMEOS)
816 display_configurator_
->Init(!gpu_support_
->IsPanelFittingDisabled());
817 display_configurator_animation_
.reset(new DisplayConfiguratorAnimation());
818 display_configurator_
->AddObserver(display_configurator_animation_
.get());
820 projecting_observer_
.reset(new ProjectingObserver());
821 display_configurator_
->AddObserver(projecting_observer_
.get());
823 if (!display_initialized
&& base::SysInfo::IsRunningOnChromeOS()) {
824 display_change_observer_
.reset(new DisplayChangeObserver
);
825 // Register |display_change_observer_| first so that the rest of
826 // observer gets invoked after the root windows are configured.
827 display_configurator_
->AddObserver(display_change_observer_
.get());
828 display_error_observer_
.reset(new DisplayErrorObserver());
829 display_configurator_
->AddObserver(display_error_observer_
.get());
830 display_configurator_
->set_state_controller(display_change_observer_
.get());
831 display_configurator_
->set_mirroring_controller(display_manager_
.get());
832 display_configurator_
->ForceInitialConfigure(
833 delegate_
->IsFirstRunAfterBoot() ? kChromeOsBootColor
: 0);
834 display_initialized
= true;
836 #endif // defined(OS_CHROMEOS)
837 if (!display_initialized
)
838 display_manager_
->InitDefaultDisplay();
840 // Install the custom factory first so that views::FocusManagers for Tray,
841 // Shelf, and WallPaper could be created by the factory.
842 views::FocusManagerFactory::Install(new AshFocusManagerFactory
);
844 aura::Env::CreateInstance(true);
845 aura::Env::GetInstance()->set_context_factory(init_params
.context_factory
);
847 // The WindowModalityController needs to be at the front of the input event
848 // pretarget handler list to ensure that it processes input events when modal
849 // windows are active.
850 window_modality_controller_
.reset(
851 new ::wm::WindowModalityController(this));
853 env_filter_
.reset(new ::wm::CompoundEventFilter
);
854 AddPreTargetHandler(env_filter_
.get());
856 ::wm::FocusController
* focus_controller
=
857 new ::wm::FocusController(new wm::AshFocusRules
);
858 focus_client_
.reset(focus_controller
);
859 activation_client_
= focus_controller
;
860 activation_client_
->AddObserver(this);
861 focus_cycler_
.reset(new FocusCycler());
863 screen_position_controller_
.reset(new ScreenPositionController
);
865 display_controller_
->Start();
866 display_controller_
->CreatePrimaryHost(
867 ShellInitParamsToAshWindowTreeHostInitParams(init_params
));
868 aura::Window
* root_window
= display_controller_
->GetPrimaryRootWindow();
869 target_root_window_
= root_window
;
871 #if defined(OS_CHROMEOS)
872 resolution_notification_controller_
.reset(
873 new ResolutionNotificationController
);
876 cursor_manager_
.SetDisplay(GetScreen()->GetPrimaryDisplay());
878 nested_accelerator_controller_
.reset(
879 new ::wm::NestedAcceleratorController(new NestedAcceleratorDelegate
));
880 accelerator_controller_
.reset(new AcceleratorController
);
881 maximize_mode_controller_
.reset(new MaximizeModeController());
883 #if defined(OS_CHROMEOS) && defined(USE_X11)
884 magnifier_key_scroll_handler_
= MagnifierKeyScroller::CreateHandler().Pass();
885 AddPreTargetHandler(magnifier_key_scroll_handler_
.get());
886 speech_feedback_handler_
= SpokenFeedbackToggler::CreateHandler().Pass();
887 AddPreTargetHandler(speech_feedback_handler_
.get());
890 // The order in which event filters are added is significant.
892 // wm::UserActivityDetector passes events to observers, so let them get
894 user_activity_detector_
.reset(new ::wm::UserActivityDetector
);
895 AddPreTargetHandler(user_activity_detector_
.get());
897 overlay_filter_
.reset(new OverlayEventFilter
);
898 AddPreTargetHandler(overlay_filter_
.get());
899 AddShellObserver(overlay_filter_
.get());
901 input_method_filter_
.reset(new ::wm::InputMethodEventFilter(
902 root_window
->GetHost()->GetAcceleratedWidget()));
903 AddPreTargetHandler(input_method_filter_
.get());
905 accelerator_filter_
.reset(new ::wm::AcceleratorFilter(
906 scoped_ptr
< ::wm::AcceleratorDelegate
>(new AcceleratorDelegate
).Pass()));
907 AddPreTargetHandler(accelerator_filter_
.get());
909 event_transformation_handler_
.reset(new EventTransformationHandler
);
910 AddPreTargetHandler(event_transformation_handler_
.get());
912 toplevel_window_event_handler_
.reset(new ToplevelWindowEventHandler
);
914 system_gesture_filter_
.reset(new SystemGestureEventFilter
);
915 AddPreTargetHandler(system_gesture_filter_
.get());
917 keyboard_metrics_filter_
.reset(new KeyboardUMAEventFilter
);
918 AddPreTargetHandler(keyboard_metrics_filter_
.get());
920 // The keyboard system must be initialized before the RootWindowController is
922 #if defined(OS_CHROMEOS)
923 keyboard::InitializeKeyboard();
926 #if defined(OS_CHROMEOS)
927 sticky_keys_controller_
.reset(new StickyKeysController
);
930 lock_state_controller_
.reset(new LockStateController
);
931 power_button_controller_
.reset(new PowerButtonController(
932 lock_state_controller_
.get()));
933 #if defined(OS_CHROMEOS)
934 // Pass the initial display state to PowerButtonController.
935 power_button_controller_
->OnDisplayModeChanged(
936 display_configurator_
->cached_displays());
938 AddShellObserver(lock_state_controller_
.get());
940 drag_drop_controller_
.reset(new DragDropController
);
941 mouse_cursor_filter_
.reset(new MouseCursorEventFilter());
942 PrependPreTargetHandler(mouse_cursor_filter_
.get());
944 // Create Controllers that may need root window.
945 // TODO(oshima): Move as many controllers before creating
946 // RootWindowController as possible.
947 visibility_controller_
.reset(new AshVisibilityController
);
949 magnification_controller_
.reset(
950 MagnificationController::CreateInstance());
951 mru_window_tracker_
.reset(new MruWindowTracker(activation_client_
));
953 partial_magnification_controller_
.reset(
954 new PartialMagnificationController());
956 autoclick_controller_
.reset(AutoclickController::CreateInstance());
958 high_contrast_controller_
.reset(new HighContrastController
);
959 video_detector_
.reset(new VideoDetector
);
960 window_selector_controller_
.reset(new WindowSelectorController());
961 window_cycle_controller_
.reset(new WindowCycleController());
963 tooltip_controller_
.reset(
964 new views::corewm::TooltipController(
965 scoped_ptr
<views::corewm::Tooltip
>(
966 new views::corewm::TooltipAura(gfx::SCREEN_TYPE_ALTERNATE
))));
967 AddPreTargetHandler(tooltip_controller_
.get());
969 event_client_
.reset(new EventClientImpl
);
971 // This controller needs to be set before SetupManagedWindowMode.
972 desktop_background_controller_
.reset(new DesktopBackgroundController());
973 user_wallpaper_delegate_
.reset(delegate_
->CreateUserWallpaperDelegate());
975 session_state_delegate_
.reset(delegate_
->CreateSessionStateDelegate());
976 accessibility_delegate_
.reset(delegate_
->CreateAccessibilityDelegate());
977 new_window_delegate_
.reset(delegate_
->CreateNewWindowDelegate());
978 media_delegate_
.reset(delegate_
->CreateMediaDelegate());
980 resize_shadow_controller_
.reset(new ResizeShadowController());
981 shadow_controller_
.reset(
982 new ::wm::ShadowController(activation_client_
));
984 // Create system_tray_notifier_ before the delegate.
985 system_tray_notifier_
.reset(new ash::SystemTrayNotifier());
987 // Initialize system_tray_delegate_ before initializing StatusAreaWidget.
988 system_tray_delegate_
.reset(delegate()->CreateSystemTrayDelegate());
989 DCHECK(system_tray_delegate_
.get());
991 locale_notification_controller_
.reset(new LocaleNotificationController
);
993 // Initialize system_tray_delegate_ after StatusAreaWidget is created.
994 system_tray_delegate_
->Initialize();
996 #if defined(OS_CHROMEOS)
997 // Create the LogoutConfirmationController after the SystemTrayDelegate.
998 logout_confirmation_controller_
.reset(new LogoutConfirmationController(
999 base::Bind(&SystemTrayDelegate::SignOut
,
1000 base::Unretained(system_tray_delegate_
.get()))));
1003 #if defined(OS_CHROMEOS) && defined(USE_X11)
1004 // Create TouchTransformerController before DisplayController::InitDisplays()
1005 // since TouchTransformerController listens on
1006 // DisplayController::Observer::OnDisplaysInitialized().
1007 touch_transformer_controller_
.reset(new TouchTransformerController());
1008 #endif // defined(OS_CHROMEOS) && defined(USE_X11)
1010 display_controller_
->InitDisplays();
1012 // It needs to be created after RootWindowController has been created
1013 // (which calls OnWindowResized has been called, otherwise the
1014 // widget will not paint when restoring after a browser crash. Also it needs
1015 // to be created after InitSecondaryDisplays() to initialize the wallpapers in
1016 // the correct size.
1017 user_wallpaper_delegate_
->InitializeWallpaper();
1019 if (initially_hide_cursor_
)
1020 cursor_manager_
.HideCursor();
1021 cursor_manager_
.SetCursor(ui::kCursorPointer
);
1023 #if defined(OS_CHROMEOS)
1024 // Set accelerator controller delegates.
1025 accelerator_controller_
->SetBrightnessControlDelegate(
1026 scoped_ptr
<ash::BrightnessControlDelegate
>(
1027 new ash::system::BrightnessControllerChromeos
).Pass());
1029 power_event_observer_
.reset(new PowerEventObserver());
1030 user_activity_notifier_
.reset(
1031 new ui::UserActivityPowerManagerNotifier(user_activity_detector_
.get()));
1032 video_activity_notifier_
.reset(
1033 new VideoActivityNotifier(video_detector_
.get()));
1034 bluetooth_notification_controller_
.reset(new BluetoothNotificationController
);
1035 last_window_closed_logout_reminder_
.reset(new LastWindowClosedLogoutReminder
);
1038 weak_display_manager_factory_
.reset(
1039 new base::WeakPtrFactory
<DisplayManager
>(display_manager_
.get()));
1040 // The compositor thread and main message loop have to be running in
1041 // order to create mirror window. Run it after the main message loop
1043 base::MessageLoopForUI::current()->PostTask(
1045 base::Bind(&DisplayManager::CreateMirrorWindowIfAny
,
1046 weak_display_manager_factory_
->GetWeakPtr()));
1049 void Shell::InitKeyboard() {
1050 if (keyboard::IsKeyboardEnabled()) {
1051 if (keyboard::KeyboardController::GetInstance()) {
1052 RootWindowControllerList controllers
= GetAllRootWindowControllers();
1053 for (RootWindowControllerList::iterator iter
= controllers
.begin();
1054 iter
!= controllers
.end(); ++iter
) {
1055 (*iter
)->DeactivateKeyboard(
1056 keyboard::KeyboardController::GetInstance());
1059 keyboard::KeyboardControllerProxy
* proxy
=
1060 delegate_
->CreateKeyboardControllerProxy();
1061 keyboard::KeyboardController::ResetInstance(
1062 new keyboard::KeyboardController(proxy
));
1066 void Shell::InitRootWindow(aura::Window
* root_window
) {
1067 DCHECK(activation_client_
);
1068 DCHECK(visibility_controller_
.get());
1069 DCHECK(drag_drop_controller_
.get());
1071 aura::client::SetFocusClient(root_window
, focus_client_
.get());
1072 input_method_filter_
->SetInputMethodPropertyInRootWindow(root_window
);
1073 aura::client::SetActivationClient(root_window
, activation_client_
);
1074 ::wm::FocusController
* focus_controller
=
1075 static_cast< ::wm::FocusController
*>(activation_client_
);
1076 root_window
->AddPreTargetHandler(focus_controller
);
1077 aura::client::SetVisibilityClient(root_window
, visibility_controller_
.get());
1078 aura::client::SetDragDropClient(root_window
, drag_drop_controller_
.get());
1079 aura::client::SetScreenPositionClient(root_window
,
1080 screen_position_controller_
.get());
1081 aura::client::SetCursorClient(root_window
, &cursor_manager_
);
1082 aura::client::SetTooltipClient(root_window
, tooltip_controller_
.get());
1083 aura::client::SetEventClient(root_window
, event_client_
.get());
1085 aura::client::SetWindowMoveClient(root_window
,
1086 toplevel_window_event_handler_
.get());
1087 root_window
->AddPreTargetHandler(toplevel_window_event_handler_
.get());
1088 root_window
->AddPostTargetHandler(toplevel_window_event_handler_
.get());
1090 if (nested_accelerator_controller_
) {
1091 aura::client::SetDispatcherClient(root_window
,
1092 nested_accelerator_controller_
.get());
1096 bool Shell::CanWindowReceiveEvents(aura::Window
* window
) {
1097 RootWindowControllerList controllers
= GetAllRootWindowControllers();
1098 for (RootWindowControllerList::iterator iter
= controllers
.begin();
1099 iter
!= controllers
.end(); ++iter
) {
1100 SystemModalContainerLayoutManager
* layout_manager
=
1101 (*iter
)->GetSystemModalLayoutManager(window
);
1102 if (layout_manager
&& layout_manager
->CanWindowReceiveEvents(window
))
1104 // Allow events to fall through to the virtual keyboard even if displaying
1105 // a system modal dialog.
1106 if ((*iter
)->IsVirtualKeyboardWindow(window
))
1113 ////////////////////////////////////////////////////////////////////////////////
1114 // Shell, ui::EventTarget overrides:
1116 bool Shell::CanAcceptEvent(const ui::Event
& event
) {
1120 ui::EventTarget
* Shell::GetParentTarget() {
1121 return aura::Env::GetInstance();
1124 scoped_ptr
<ui::EventTargetIterator
> Shell::GetChildIterator() const {
1125 return scoped_ptr
<ui::EventTargetIterator
>();
1128 ui::EventTargeter
* Shell::GetEventTargeter() {
1133 void Shell::OnEvent(ui::Event
* event
) {
1136 ////////////////////////////////////////////////////////////////////////////////
1137 // Shell, aura::client::ActivationChangeObserver implementation:
1139 void Shell::OnWindowActivated(aura::Window
* gained_active
,
1140 aura::Window
* lost_active
) {
1142 target_root_window_
= gained_active
->GetRootWindow();