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/ash_switches.h"
15 #include "ash/autoclick/autoclick_controller.h"
16 #include "ash/desktop_background/desktop_background_controller.h"
17 #include "ash/desktop_background/desktop_background_view.h"
18 #include "ash/desktop_background/user_wallpaper_delegate.h"
19 #include "ash/display/cursor_window_controller.h"
20 #include "ash/display/display_controller.h"
21 #include "ash/display/display_manager.h"
22 #include "ash/display/event_transformation_handler.h"
23 #include "ash/display/mouse_cursor_event_filter.h"
24 #include "ash/display/screen_position_controller.h"
25 #include "ash/drag_drop/drag_drop_controller.h"
26 #include "ash/first_run/first_run_helper_impl.h"
27 #include "ash/focus_cycler.h"
28 #include "ash/frame/custom_frame_view_ash.h"
29 #include "ash/gpu_support.h"
30 #include "ash/high_contrast/high_contrast_controller.h"
31 #include "ash/host/ash_window_tree_host_init_params.h"
32 #include "ash/keyboard_uma_event_filter.h"
33 #include "ash/magnifier/magnification_controller.h"
34 #include "ash/magnifier/partial_magnification_controller.h"
35 #include "ash/media_delegate.h"
36 #include "ash/new_window_delegate.h"
37 #include "ash/root_window_controller.h"
38 #include "ash/session/session_state_delegate.h"
39 #include "ash/shelf/app_list_shelf_item_delegate.h"
40 #include "ash/shelf/shelf_delegate.h"
41 #include "ash/shelf/shelf_item_delegate.h"
42 #include "ash/shelf/shelf_item_delegate_manager.h"
43 #include "ash/shelf/shelf_layout_manager.h"
44 #include "ash/shelf/shelf_model.h"
45 #include "ash/shelf/shelf_widget.h"
46 #include "ash/shelf/shelf_window_watcher.h"
47 #include "ash/shell_delegate.h"
48 #include "ash/shell_factory.h"
49 #include "ash/shell_init_params.h"
50 #include "ash/shell_window_ids.h"
51 #include "ash/system/locale/locale_notification_controller.h"
52 #include "ash/system/status_area_widget.h"
53 #include "ash/system/tray/system_tray_delegate.h"
54 #include "ash/system/tray/system_tray_notifier.h"
55 #include "ash/wm/app_list_controller.h"
56 #include "ash/wm/ash_focus_rules.h"
57 #include "ash/wm/ash_native_cursor_manager.h"
58 #include "ash/wm/coordinate_conversion.h"
59 #include "ash/wm/event_client_impl.h"
60 #include "ash/wm/lock_state_controller.h"
61 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
62 #include "ash/wm/maximize_mode/maximize_mode_window_manager.h"
63 #include "ash/wm/mru_window_tracker.h"
64 #include "ash/wm/overlay_event_filter.h"
65 #include "ash/wm/overview/window_selector_controller.h"
66 #include "ash/wm/power_button_controller.h"
67 #include "ash/wm/resize_shadow_controller.h"
68 #include "ash/wm/root_window_layout_manager.h"
69 #include "ash/wm/screen_dimmer.h"
70 #include "ash/wm/system_gesture_event_filter.h"
71 #include "ash/wm/system_modal_container_event_filter.h"
72 #include "ash/wm/system_modal_container_layout_manager.h"
73 #include "ash/wm/toplevel_window_event_handler.h"
74 #include "ash/wm/video_detector.h"
75 #include "ash/wm/window_animations.h"
76 #include "ash/wm/window_cycle_controller.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/base/user_activity/user_activity_detector.h"
90 #include "ui/compositor/layer.h"
91 #include "ui/compositor/layer_animator.h"
92 #include "ui/events/event_target_iterator.h"
93 #include "ui/gfx/display.h"
94 #include "ui/gfx/geometry/size.h"
95 #include "ui/gfx/image/image_skia.h"
96 #include "ui/gfx/screen.h"
97 #include "ui/keyboard/keyboard.h"
98 #include "ui/keyboard/keyboard_controller.h"
99 #include "ui/keyboard/keyboard_switches.h"
100 #include "ui/keyboard/keyboard_util.h"
101 #include "ui/message_center/message_center.h"
102 #include "ui/views/corewm/tooltip_aura.h"
103 #include "ui/views/corewm/tooltip_controller.h"
104 #include "ui/views/focus/focus_manager_factory.h"
105 #include "ui/views/widget/native_widget_aura.h"
106 #include "ui/views/widget/widget.h"
107 #include "ui/wm/core/accelerator_filter.h"
108 #include "ui/wm/core/compound_event_filter.h"
109 #include "ui/wm/core/focus_controller.h"
110 #include "ui/wm/core/input_method_event_filter.h"
111 #include "ui/wm/core/nested_accelerator_controller.h"
112 #include "ui/wm/core/shadow_controller.h"
113 #include "ui/wm/core/visibility_controller.h"
114 #include "ui/wm/core/window_modality_controller.h"
116 #if defined(OS_CHROMEOS)
118 #include "ui/gfx/x/x11_types.h"
119 #endif // defined(USE_X11)
120 #include "ash/accelerators/magnifier_key_scroller.h"
121 #include "ash/accelerators/spoken_feedback_toggler.h"
122 #include "ash/ash_constants.h"
123 #include "ash/content/display/screen_orientation_controller_chromeos.h"
124 #include "ash/display/display_change_observer_chromeos.h"
125 #include "ash/display/display_configurator_animation.h"
126 #include "ash/display/display_error_observer_chromeos.h"
127 #include "ash/display/projecting_observer_chromeos.h"
128 #include "ash/display/resolution_notification_controller.h"
129 #include "ash/sticky_keys/sticky_keys_controller.h"
130 #include "ash/system/chromeos/bluetooth/bluetooth_notification_controller.h"
131 #include "ash/system/chromeos/brightness/brightness_controller_chromeos.h"
132 #include "ash/system/chromeos/power/power_event_observer.h"
133 #include "ash/system/chromeos/power/power_status.h"
134 #include "ash/system/chromeos/power/video_activity_notifier.h"
135 #include "ash/system/chromeos/session/last_window_closed_logout_reminder.h"
136 #include "ash/system/chromeos/session/logout_confirmation_controller.h"
137 #include "ash/touch/touch_transformer_controller.h"
138 #include "ash/virtual_keyboard_controller.h"
139 #include "base/bind_helpers.h"
140 #include "base/sys_info.h"
141 #include "chromeos/dbus/dbus_thread_manager.h"
142 #include "ui/chromeos/user_activity_power_manager_notifier.h"
143 #include "ui/display/chromeos/display_configurator.h"
144 #endif // defined(OS_CHROMEOS)
153 // A Corewm VisibilityController subclass that calls the Ash animation routine
154 // so we can pick up our extended animations. See ash/wm/window_animations.h.
155 class AshVisibilityController
: public ::wm::VisibilityController
{
157 AshVisibilityController() {}
158 ~AshVisibilityController() override
{}
161 // Overridden from ::wm::VisibilityController:
162 bool CallAnimateOnChildWindowVisibilityChanged(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 CHECK(HasInstance());
217 return GetRootWindowController(GetPrimaryRootWindow());
221 Shell::RootWindowControllerList
Shell::GetAllRootWindowControllers() {
222 CHECK(HasInstance());
223 return Shell::GetInstance()->display_controller()->
224 GetAllRootWindowControllers();
228 aura::Window
* Shell::GetPrimaryRootWindow() {
229 CHECK(HasInstance());
230 return GetInstance()->display_controller()->GetPrimaryRootWindow();
234 aura::Window
* Shell::GetTargetRootWindow() {
235 CHECK(HasInstance());
236 Shell
* shell
= GetInstance();
237 if (shell
->scoped_target_root_window_
)
238 return shell
->scoped_target_root_window_
;
239 return shell
->target_root_window_
;
243 gfx::Screen
* Shell::GetScreen() {
244 return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE
);
248 aura::Window::Windows
Shell::GetAllRootWindows() {
249 CHECK(HasInstance());
250 return Shell::GetInstance()->display_controller()->
255 aura::Window
* Shell::GetContainer(aura::Window
* root_window
,
257 return root_window
->GetChildById(container_id
);
261 const aura::Window
* Shell::GetContainer(const aura::Window
* root_window
,
263 return root_window
->GetChildById(container_id
);
267 std::vector
<aura::Window
*> Shell::GetContainersFromAllRootWindows(
269 aura::Window
* priority_root
) {
270 std::vector
<aura::Window
*> containers
;
271 aura::Window::Windows root_windows
= GetAllRootWindows();
272 for (aura::Window::Windows::const_iterator it
= root_windows
.begin();
273 it
!= root_windows
.end(); ++it
) {
274 aura::Window
* container
= (*it
)->GetChildById(container_id
);
276 if (priority_root
&& priority_root
->Contains(container
))
277 containers
.insert(containers
.begin(), container
);
279 containers
.push_back(container
);
285 void Shell::ShowContextMenu(const gfx::Point
& location_in_screen
,
286 ui::MenuSourceType source_type
) {
287 // No context menus if there is no session with an active user.
288 if (!session_state_delegate_
->NumberOfLoggedInUsers())
290 // No context menus when screen is locked.
291 if (session_state_delegate_
->IsScreenLocked())
295 wm::GetRootWindowMatching(gfx::Rect(location_in_screen
, gfx::Size()));
296 GetRootWindowController(root
)
297 ->ShowContextMenu(location_in_screen
, source_type
);
300 void Shell::ShowAppList(aura::Window
* window
) {
301 // If the context window is not given, show it on the target root window.
303 window
= GetTargetRootWindow();
304 if (!app_list_controller_
)
305 app_list_controller_
.reset(new AppListController
);
306 app_list_controller_
->Show(window
);
309 void Shell::DismissAppList() {
310 if (!app_list_controller_
)
312 app_list_controller_
->Dismiss();
315 void Shell::ToggleAppList(aura::Window
* window
) {
316 if (app_list_controller_
&& app_list_controller_
->IsVisible()) {
324 bool Shell::GetAppListTargetVisibility() const {
325 return app_list_controller_
.get() &&
326 app_list_controller_
->GetTargetVisibility();
329 aura::Window
* Shell::GetAppListWindow() {
330 return app_list_controller_
.get() ? app_list_controller_
->GetWindow() : NULL
;
333 app_list::AppListView
* Shell::GetAppListView() {
334 return app_list_controller_
.get() ? app_list_controller_
->GetView() : NULL
;
337 bool Shell::IsSystemModalWindowOpen() const {
338 if (simulate_modal_window_open_for_testing_
)
340 const std::vector
<aura::Window
*> containers
= GetContainersFromAllRootWindows(
341 kShellWindowId_SystemModalContainer
, NULL
);
342 for (std::vector
<aura::Window
*>::const_iterator cit
= containers
.begin();
343 cit
!= containers
.end(); ++cit
) {
344 for (aura::Window::Windows::const_iterator wit
= (*cit
)->children().begin();
345 wit
!= (*cit
)->children().end(); ++wit
) {
346 if ((*wit
)->GetProperty(aura::client::kModalKey
) ==
347 ui::MODAL_TYPE_SYSTEM
&& (*wit
)->TargetVisibility()) {
355 views::NonClientFrameView
* Shell::CreateDefaultNonClientFrameView(
356 views::Widget
* widget
) {
357 // Use translucent-style window frames for dialogs.
358 return new CustomFrameViewAsh(widget
);
361 void Shell::RotateFocus(Direction direction
) {
362 focus_cycler_
->RotateFocus(direction
== FORWARD
? FocusCycler::FORWARD
363 : FocusCycler::BACKWARD
);
366 void Shell::SetDisplayWorkAreaInsets(Window
* contains
,
367 const gfx::Insets
& insets
) {
368 if (!display_controller_
->UpdateWorkAreaOfDisplayNearestWindow(
372 FOR_EACH_OBSERVER(ShellObserver
, observers_
,
373 OnDisplayWorkAreaInsetsChanged());
376 void Shell::OnLoginStateChanged(user::LoginStatus status
) {
377 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnLoginStateChanged(status
));
380 void Shell::OnLoginUserProfilePrepared() {
385 void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status
) {
386 RootWindowControllerList controllers
= GetAllRootWindowControllers();
387 for (RootWindowControllerList::iterator iter
= controllers
.begin();
388 iter
!= controllers
.end(); ++iter
)
389 (*iter
)->UpdateAfterLoginStatusChange(status
);
392 void Shell::OnAppTerminating() {
393 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnAppTerminating());
396 void Shell::OnLockStateChanged(bool locked
) {
397 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnLockStateChanged(locked
));
399 // Make sure that there is no system modal in Lock layer when unlocked.
401 std::vector
<aura::Window
*> containers
= GetContainersFromAllRootWindows(
402 kShellWindowId_LockSystemModalContainer
, GetPrimaryRootWindow());
403 for (std::vector
<aura::Window
*>::const_iterator iter
= containers
.begin();
404 iter
!= containers
.end(); ++iter
) {
405 DCHECK_EQ(0u, (*iter
)->children().size());
411 void Shell::OnCastingSessionStartedOrStopped(bool started
) {
412 #if defined(OS_CHROMEOS)
413 if (projecting_observer_
)
414 projecting_observer_
->OnCastingSessionStartedOrStopped(started
);
418 void Shell::OnOverviewModeStarting() {
419 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnOverviewModeStarting());
422 void Shell::OnOverviewModeEnding() {
423 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnOverviewModeEnding());
426 void Shell::OnMaximizeModeStarted() {
427 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnMaximizeModeStarted());
430 void Shell::OnMaximizeModeEnded() {
431 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnMaximizeModeEnded());
434 void Shell::OnRootWindowAdded(aura::Window
* root_window
) {
435 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnRootWindowAdded(root_window
));
438 void Shell::CreateShelf() {
439 RootWindowControllerList controllers
= GetAllRootWindowControllers();
440 for (RootWindowControllerList::iterator iter
= controllers
.begin();
441 iter
!= controllers
.end(); ++iter
)
442 (*iter
)->shelf()->CreateShelf();
445 void Shell::OnShelfCreatedForRootWindow(aura::Window
* root_window
) {
446 FOR_EACH_OBSERVER(ShellObserver
,
448 OnShelfCreatedForRootWindow(root_window
));
451 void Shell::CreateKeyboard() {
452 // TODO(bshe): Primary root window controller may not be the controller to
453 // attach virtual keyboard. See http://crbug.com/303429
455 GetPrimaryRootWindowController()->
456 ActivateKeyboard(keyboard::KeyboardController::GetInstance());
459 void Shell::DeactivateKeyboard() {
460 if (keyboard::KeyboardController::GetInstance()) {
461 RootWindowControllerList controllers
= GetAllRootWindowControllers();
462 for (RootWindowControllerList::iterator iter
= controllers
.begin();
463 iter
!= controllers
.end(); ++iter
) {
464 (*iter
)->DeactivateKeyboard(keyboard::KeyboardController::GetInstance());
467 keyboard::KeyboardController::ResetInstance(NULL
);
470 void Shell::ShowShelf() {
471 RootWindowControllerList controllers
= GetAllRootWindowControllers();
472 for (RootWindowControllerList::iterator iter
= controllers
.begin();
473 iter
!= controllers
.end(); ++iter
)
474 (*iter
)->ShowShelf();
477 void Shell::AddShellObserver(ShellObserver
* observer
) {
478 observers_
.AddObserver(observer
);
481 void Shell::RemoveShellObserver(ShellObserver
* observer
) {
482 observers_
.RemoveObserver(observer
);
485 #if defined(OS_CHROMEOS)
486 bool Shell::ShouldSaveDisplaySettings() {
487 return !(screen_orientation_controller_
488 ->ignore_display_configuration_updates() ||
489 resolution_notification_controller_
->DoesNotificationTimeout());
493 void Shell::UpdateShelfVisibility() {
494 RootWindowControllerList controllers
= GetAllRootWindowControllers();
495 for (RootWindowControllerList::iterator iter
= controllers
.begin();
496 iter
!= controllers
.end(); ++iter
)
497 if ((*iter
)->shelf())
498 (*iter
)->UpdateShelfVisibility();
501 void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior
,
502 aura::Window
* root_window
) {
503 ash::ShelfLayoutManager::ForShelf(root_window
)->SetAutoHideBehavior(behavior
);
506 ShelfAutoHideBehavior
Shell::GetShelfAutoHideBehavior(
507 aura::Window
* root_window
) const {
508 return ash::ShelfLayoutManager::ForShelf(root_window
)->auto_hide_behavior();
511 void Shell::SetShelfAlignment(ShelfAlignment alignment
,
512 aura::Window
* root_window
) {
513 if (ash::ShelfLayoutManager::ForShelf(root_window
)->SetAlignment(alignment
)) {
515 ShellObserver
, observers_
, OnShelfAlignmentChanged(root_window
));
519 ShelfAlignment
Shell::GetShelfAlignment(const aura::Window
* root_window
) {
520 return GetRootWindowController(root_window
)
521 ->GetShelfLayoutManager()
525 void Shell::SetDimming(bool should_dim
) {
526 RootWindowControllerList controllers
= GetAllRootWindowControllers();
527 for (RootWindowControllerList::iterator iter
= controllers
.begin();
528 iter
!= controllers
.end(); ++iter
)
529 (*iter
)->screen_dimmer()->SetDimming(should_dim
);
532 void Shell::NotifyFullscreenStateChange(bool is_fullscreen
,
533 aura::Window
* root_window
) {
534 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnFullscreenStateChanged(
535 is_fullscreen
, root_window
));
538 void Shell::CreateModalBackground(aura::Window
* window
) {
539 if (!modality_filter_
) {
540 modality_filter_
.reset(new SystemModalContainerEventFilter(this));
541 AddPreTargetHandler(modality_filter_
.get());
543 RootWindowControllerList controllers
= GetAllRootWindowControllers();
544 for (RootWindowControllerList::iterator iter
= controllers
.begin();
545 iter
!= controllers
.end(); ++iter
)
546 (*iter
)->GetSystemModalLayoutManager(window
)->CreateModalBackground();
549 void Shell::OnModalWindowRemoved(aura::Window
* removed
) {
550 RootWindowControllerList controllers
= GetAllRootWindowControllers();
551 bool activated
= false;
552 for (RootWindowControllerList::iterator iter
= controllers
.begin();
553 iter
!= controllers
.end() && !activated
; ++iter
) {
554 activated
= (*iter
)->GetSystemModalLayoutManager(removed
)->
555 ActivateNextModalWindow();
558 RemovePreTargetHandler(modality_filter_
.get());
559 modality_filter_
.reset();
560 for (RootWindowControllerList::iterator iter
= controllers
.begin();
561 iter
!= controllers
.end(); ++iter
)
562 (*iter
)->GetSystemModalLayoutManager(removed
)->DestroyModalBackground();
566 WebNotificationTray
* Shell::GetWebNotificationTray() {
567 return GetPrimaryRootWindowController()->shelf()->
568 status_area_widget()->web_notification_tray();
571 bool Shell::HasPrimaryStatusArea() {
572 ShelfWidget
* shelf
= GetPrimaryRootWindowController()->shelf();
573 return shelf
&& shelf
->status_area_widget();
576 SystemTray
* Shell::GetPrimarySystemTray() {
577 return GetPrimaryRootWindowController()->GetSystemTray();
580 ShelfDelegate
* Shell::GetShelfDelegate() {
581 if (!shelf_delegate_
) {
582 shelf_model_
.reset(new ShelfModel
);
583 // Creates ShelfItemDelegateManager before ShelfDelegate.
584 shelf_item_delegate_manager_
.reset(
585 new ShelfItemDelegateManager(shelf_model_
.get()));
587 shelf_delegate_
.reset(delegate_
->CreateShelfDelegate(shelf_model_
.get()));
588 scoped_ptr
<ShelfItemDelegate
> controller(new AppListShelfItemDelegate
);
590 // Finding the shelf model's location of the app list and setting its
591 // ShelfItemDelegate.
592 int app_list_index
= shelf_model_
->GetItemIndexForType(TYPE_APP_LIST
);
593 DCHECK_GE(app_list_index
, 0);
594 ShelfID app_list_id
= shelf_model_
->items()[app_list_index
].id
;
596 shelf_item_delegate_manager_
->SetShelfItemDelegate(app_list_id
,
598 shelf_window_watcher_
.reset(new ShelfWindowWatcher(
599 shelf_model_
.get(), shelf_item_delegate_manager_
.get()));
601 return shelf_delegate_
.get();
604 void Shell::SetTouchHudProjectionEnabled(bool enabled
) {
605 if (is_touch_hud_projection_enabled_
== enabled
)
608 is_touch_hud_projection_enabled_
= enabled
;
609 FOR_EACH_OBSERVER(ShellObserver
, observers_
,
610 OnTouchHudProjectionToggled(enabled
));
613 #if defined(OS_CHROMEOS)
614 ash::FirstRunHelper
* Shell::CreateFirstRunHelper() {
615 return new ash::FirstRunHelperImpl
;
618 void Shell::SetCursorCompositingEnabled(bool enabled
) {
619 display_controller_
->cursor_window_controller()->SetCursorCompositingEnabled(
621 native_cursor_manager_
->SetNativeCursorEnabled(!enabled
);
623 #endif // defined(OS_CHROMEOS)
625 void Shell::DoInitialWorkspaceAnimation() {
626 return GetPrimaryRootWindowController()->workspace_controller()->
627 DoInitialAnimation();
630 ////////////////////////////////////////////////////////////////////////////////
633 Shell::Shell(ShellDelegate
* delegate
)
634 : target_root_window_(NULL
),
635 scoped_target_root_window_(NULL
),
637 window_positioner_(new WindowPositioner
),
638 activation_client_(NULL
),
639 #if defined(OS_CHROMEOS)
640 display_configurator_(new ui::DisplayConfigurator()),
641 #endif // defined(OS_CHROMEOS)
642 native_cursor_manager_(new AshNativeCursorManager
),
644 scoped_ptr
<::wm::NativeCursorManager
>(native_cursor_manager_
)),
645 simulate_modal_window_open_for_testing_(false),
646 is_touch_hud_projection_enabled_(false) {
647 DCHECK(delegate_
.get());
648 gpu_support_
.reset(delegate_
->CreateGPUSupport());
649 display_manager_
.reset(new DisplayManager
);
650 display_controller_
.reset(new DisplayController
);
651 user_metrics_recorder_
.reset(new UserMetricsRecorder
);
653 #if defined(OS_CHROMEOS)
654 PowerStatus::Initialize();
659 TRACE_EVENT0("shutdown", "ash::Shell::Destructor");
661 delegate_
->PreShutdown();
663 views::FocusManagerFactory::Install(NULL
);
665 // Remove the focus from any window. This will prevent overhead and side
666 // effects (e.g. crashes) from changing focus during shutdown.
667 // See bug crbug.com/134502.
668 aura::client::GetFocusClient(GetPrimaryRootWindow())->FocusWindow(NULL
);
670 // Please keep in same order as in Init() because it's easy to miss one.
671 if (window_modality_controller_
)
672 window_modality_controller_
.reset();
673 #if defined(OS_CHROMEOS)
674 RemovePreTargetHandler(magnifier_key_scroll_handler_
.get());
675 magnifier_key_scroll_handler_
.reset();
677 RemovePreTargetHandler(speech_feedback_handler_
.get());
678 speech_feedback_handler_
.reset();
680 RemovePreTargetHandler(user_activity_detector_
.get());
681 RemovePreTargetHandler(overlay_filter_
.get());
682 RemovePreTargetHandler(input_method_filter_
.get());
683 RemovePreTargetHandler(accelerator_filter_
.get());
684 RemovePreTargetHandler(event_transformation_handler_
.get());
685 RemovePreTargetHandler(toplevel_window_event_handler_
.get());
686 RemovePostTargetHandler(toplevel_window_event_handler_
.get());
687 RemovePreTargetHandler(system_gesture_filter_
.get());
688 RemovePreTargetHandler(keyboard_metrics_filter_
.get());
689 RemovePreTargetHandler(mouse_cursor_filter_
.get());
691 // TooltipController is deleted with the Shell so removing its references.
692 RemovePreTargetHandler(tooltip_controller_
.get());
694 #if defined(OS_CHROMEOS)
695 screen_orientation_controller_
.reset();
698 // Destroy the virtual keyboard controller before the maximize mode controller
699 // since the latters destructor triggers events that the former is listening
700 // to but no longer cares about.
701 #if defined(OS_CHROMEOS)
702 virtual_keyboard_controller_
.reset();
705 // Destroy maximize mode controller early on since it has some observers which
706 // need to be removed.
707 maximize_mode_controller_
.reset();
709 // AppList needs to be released before shelf layout manager, which is
710 // destroyed with shelf container in the loop below. However, app list
711 // container is now on top of shelf container and released after it.
712 // TODO(xiyuan): Move it back when app list container is no longer needed.
713 app_list_controller_
.reset();
715 #if defined(OS_CHROMEOS)
716 // Destroy the LastWindowClosedLogoutReminder before the
717 // LogoutConfirmationController.
718 last_window_closed_logout_reminder_
.reset();
720 // Destroy the LogoutConfirmationController before the SystemTrayDelegate.
721 logout_confirmation_controller_
.reset();
724 // Destroy SystemTrayDelegate before destroying the status area(s).
725 system_tray_delegate_
->Shutdown();
726 system_tray_delegate_
.reset();
728 locale_notification_controller_
.reset();
730 // Drag-and-drop must be canceled prior to close all windows.
731 drag_drop_controller_
.reset();
733 // Controllers who have WindowObserver added must be deleted
734 // before |display_controller_| is deleted.
736 #if defined(OS_CHROMEOS)
737 // VideoActivityNotifier must be deleted before |video_detector_| is
738 // deleted because it's observing video activity through
739 // VideoDetectorObserver interface.
740 video_activity_notifier_
.reset();
741 #endif // defined(OS_CHROMEOS)
742 video_detector_
.reset();
743 high_contrast_controller_
.reset();
745 shadow_controller_
.reset();
746 resize_shadow_controller_
.reset();
748 window_cycle_controller_
.reset();
749 window_selector_controller_
.reset();
750 mru_window_tracker_
.reset();
752 // |shelf_window_watcher_| has a weak pointer to |shelf_Model_|
753 // and has window observers.
754 shelf_window_watcher_
.reset();
756 // Destroy all child windows including widgets.
757 display_controller_
->CloseChildWindows();
758 display_controller_
->CloseMirroringDisplay();
760 // Chrome implementation of shelf delegate depends on FocusClient,
761 // so must be deleted before |focus_client_|.
762 shelf_delegate_
.reset();
763 focus_client_
.reset();
765 // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems
766 // needs to remove observers from it.
767 system_tray_notifier_
.reset();
769 // These need a valid Shell instance to clean up properly, so explicitly
770 // delete them before invalidating the instance.
771 // Alphabetical. TODO(oshima): sort.
772 magnification_controller_
.reset();
773 partial_magnification_controller_
.reset();
774 tooltip_controller_
.reset();
775 event_client_
.reset();
776 nested_accelerator_controller_
.reset();
777 toplevel_window_event_handler_
.reset();
778 visibility_controller_
.reset();
779 // |shelf_item_delegate_manager_| observes |shelf_model_|. It must be
780 // destroyed before |shelf_model_| is destroyed.
781 shelf_item_delegate_manager_
.reset();
782 shelf_model_
.reset();
784 power_button_controller_
.reset();
785 lock_state_controller_
.reset();
787 #if defined(OS_CHROMEOS)
788 resolution_notification_controller_
.reset();
790 desktop_background_controller_
.reset();
791 mouse_cursor_filter_
.reset();
793 #if defined(OS_CHROMEOS)
794 touch_transformer_controller_
.reset();
795 #endif // defined(OS_CHROMEOS)
797 // This also deletes all RootWindows. Note that we invoke Shutdown() on
798 // DisplayController before resetting |display_controller_|, since destruction
799 // of its owned RootWindowControllers relies on the value.
800 display_manager_
->CreateScreenForShutdown();
801 display_controller_
->Shutdown();
802 display_controller_
.reset();
803 screen_position_controller_
.reset();
804 accessibility_delegate_
.reset();
805 new_window_delegate_
.reset();
806 media_delegate_
.reset();
808 keyboard::KeyboardController::ResetInstance(NULL
);
810 #if defined(OS_CHROMEOS)
811 if (display_change_observer_
)
812 display_configurator_
->RemoveObserver(display_change_observer_
.get());
813 if (display_configurator_animation_
)
814 display_configurator_
->RemoveObserver(
815 display_configurator_animation_
.get());
816 if (display_error_observer_
)
817 display_configurator_
->RemoveObserver(display_error_observer_
.get());
818 if (projecting_observer_
)
819 display_configurator_
->RemoveObserver(projecting_observer_
.get());
820 display_change_observer_
.reset();
822 PowerStatus::Shutdown();
824 // Ensure that DBusThreadManager outlives this Shell.
825 DCHECK(chromeos::DBusThreadManager::IsInitialized());
828 DCHECK(instance_
== this);
832 void Shell::Init(const ShellInitParams
& init_params
) {
833 delegate_
->PreInit();
834 bool display_initialized
= display_manager_
->InitFromCommandLine();
835 #if defined(OS_CHROMEOS)
836 display_configurator_
->Init(!gpu_support_
->IsPanelFittingDisabled());
837 display_configurator_animation_
.reset(new DisplayConfiguratorAnimation());
838 display_configurator_
->AddObserver(display_configurator_animation_
.get());
840 // The DBusThreadManager must outlive this Shell. See the DCHECK in ~Shell.
841 chromeos::DBusThreadManager
* dbus_thread_manager
=
842 chromeos::DBusThreadManager::Get();
843 projecting_observer_
.reset(
844 new ProjectingObserver(dbus_thread_manager
->GetPowerManagerClient()));
845 display_configurator_
->AddObserver(projecting_observer_
.get());
847 if (!display_initialized
&& base::SysInfo::IsRunningOnChromeOS()) {
848 display_change_observer_
.reset(new DisplayChangeObserver
);
849 // Register |display_change_observer_| first so that the rest of
850 // observer gets invoked after the root windows are configured.
851 display_configurator_
->AddObserver(display_change_observer_
.get());
852 display_error_observer_
.reset(new DisplayErrorObserver());
853 display_configurator_
->AddObserver(display_error_observer_
.get());
854 display_configurator_
->set_state_controller(display_change_observer_
.get());
855 display_configurator_
->set_mirroring_controller(display_manager_
.get());
856 display_configurator_
->ForceInitialConfigure(
857 delegate_
->IsFirstRunAfterBoot() ? kChromeOsBootColor
: 0);
858 display_initialized
= true;
860 #endif // defined(OS_CHROMEOS)
861 if (!display_initialized
)
862 display_manager_
->InitDefaultDisplay();
864 display_manager_
->RefreshFontParams();
866 // Install the custom factory first so that views::FocusManagers for Tray,
867 // Shelf, and WallPaper could be created by the factory.
868 views::FocusManagerFactory::Install(new AshFocusManagerFactory
);
870 aura::Env::CreateInstance(true);
871 aura::Env::GetInstance()->set_context_factory(init_params
.context_factory
);
873 // The WindowModalityController needs to be at the front of the input event
874 // pretarget handler list to ensure that it processes input events when modal
875 // windows are active.
876 window_modality_controller_
.reset(
877 new ::wm::WindowModalityController(this));
879 env_filter_
.reset(new ::wm::CompoundEventFilter
);
880 AddPreTargetHandler(env_filter_
.get());
882 wm::AshFocusRules
* focus_rules
= new wm::AshFocusRules();
884 ::wm::FocusController
* focus_controller
=
885 new ::wm::FocusController(focus_rules
);
886 focus_client_
.reset(focus_controller
);
887 activation_client_
= focus_controller
;
888 activation_client_
->AddObserver(this);
889 focus_cycler_
.reset(new FocusCycler());
891 screen_position_controller_
.reset(new ScreenPositionController
);
893 display_controller_
->Start();
894 display_controller_
->CreatePrimaryHost(
895 ShellInitParamsToAshWindowTreeHostInitParams(init_params
));
896 aura::Window
* root_window
= display_controller_
->GetPrimaryRootWindow();
897 target_root_window_
= root_window
;
899 #if defined(OS_CHROMEOS)
900 resolution_notification_controller_
.reset(
901 new ResolutionNotificationController
);
904 cursor_manager_
.SetDisplay(GetScreen()->GetPrimaryDisplay());
906 nested_accelerator_controller_
.reset(
907 new ::wm::NestedAcceleratorController(new NestedAcceleratorDelegate
));
908 accelerator_controller_
.reset(new AcceleratorController
);
909 maximize_mode_controller_
.reset(new MaximizeModeController());
911 #if defined(OS_CHROMEOS)
912 magnifier_key_scroll_handler_
= MagnifierKeyScroller::CreateHandler();
913 AddPreTargetHandler(magnifier_key_scroll_handler_
.get());
914 speech_feedback_handler_
= SpokenFeedbackToggler::CreateHandler();
915 AddPreTargetHandler(speech_feedback_handler_
.get());
918 // The order in which event filters are added is significant.
920 // ui::UserActivityDetector passes events to observers, so let them get
922 user_activity_detector_
.reset(new ui::UserActivityDetector
);
923 AddPreTargetHandler(user_activity_detector_
.get());
925 overlay_filter_
.reset(new OverlayEventFilter
);
926 AddPreTargetHandler(overlay_filter_
.get());
927 AddShellObserver(overlay_filter_
.get());
929 input_method_filter_
.reset(new ::wm::InputMethodEventFilter(
930 root_window
->GetHost()->GetAcceleratedWidget()));
931 AddPreTargetHandler(input_method_filter_
.get());
933 accelerator_filter_
.reset(new ::wm::AcceleratorFilter(
934 scoped_ptr
< ::wm::AcceleratorDelegate
>(new AcceleratorDelegate
).Pass(),
935 accelerator_controller_
->accelerator_history()));
936 AddPreTargetHandler(accelerator_filter_
.get());
938 event_transformation_handler_
.reset(new EventTransformationHandler
);
939 AddPreTargetHandler(event_transformation_handler_
.get());
941 toplevel_window_event_handler_
.reset(new ToplevelWindowEventHandler
);
943 system_gesture_filter_
.reset(new SystemGestureEventFilter
);
944 AddPreTargetHandler(system_gesture_filter_
.get());
946 keyboard_metrics_filter_
.reset(new KeyboardUMAEventFilter
);
947 AddPreTargetHandler(keyboard_metrics_filter_
.get());
949 // The keyboard system must be initialized before the RootWindowController is
951 #if defined(OS_CHROMEOS)
952 keyboard::InitializeKeyboard();
955 #if defined(OS_CHROMEOS)
956 sticky_keys_controller_
.reset(new StickyKeysController
);
959 lock_state_controller_
.reset(new LockStateController
);
960 power_button_controller_
.reset(new PowerButtonController(
961 lock_state_controller_
.get()));
962 #if defined(OS_CHROMEOS)
963 // Pass the initial display state to PowerButtonController.
964 power_button_controller_
->OnDisplayModeChanged(
965 display_configurator_
->cached_displays());
967 AddShellObserver(lock_state_controller_
.get());
969 drag_drop_controller_
.reset(new DragDropController
);
970 mouse_cursor_filter_
.reset(new MouseCursorEventFilter());
971 PrependPreTargetHandler(mouse_cursor_filter_
.get());
973 // Create Controllers that may need root window.
974 // TODO(oshima): Move as many controllers before creating
975 // RootWindowController as possible.
976 visibility_controller_
.reset(new AshVisibilityController
);
978 magnification_controller_
.reset(
979 MagnificationController::CreateInstance());
980 mru_window_tracker_
.reset(new MruWindowTracker(activation_client_
,
983 partial_magnification_controller_
.reset(
984 new PartialMagnificationController());
986 autoclick_controller_
.reset(AutoclickController::CreateInstance());
988 high_contrast_controller_
.reset(new HighContrastController
);
989 video_detector_
.reset(new VideoDetector
);
990 window_selector_controller_
.reset(new WindowSelectorController());
991 window_cycle_controller_
.reset(new WindowCycleController());
993 tooltip_controller_
.reset(
994 new views::corewm::TooltipController(
995 scoped_ptr
<views::corewm::Tooltip
>(
996 new views::corewm::TooltipAura(gfx::SCREEN_TYPE_ALTERNATE
))));
997 AddPreTargetHandler(tooltip_controller_
.get());
999 event_client_
.reset(new EventClientImpl
);
1001 // This controller needs to be set before SetupManagedWindowMode.
1002 desktop_background_controller_
.reset(new DesktopBackgroundController());
1003 user_wallpaper_delegate_
.reset(delegate_
->CreateUserWallpaperDelegate());
1005 session_state_delegate_
.reset(delegate_
->CreateSessionStateDelegate());
1006 accessibility_delegate_
.reset(delegate_
->CreateAccessibilityDelegate());
1007 new_window_delegate_
.reset(delegate_
->CreateNewWindowDelegate());
1008 media_delegate_
.reset(delegate_
->CreateMediaDelegate());
1010 resize_shadow_controller_
.reset(new ResizeShadowController());
1011 shadow_controller_
.reset(
1012 new ::wm::ShadowController(activation_client_
));
1014 // Create system_tray_notifier_ before the delegate.
1015 system_tray_notifier_
.reset(new ash::SystemTrayNotifier());
1017 // Initialize system_tray_delegate_ before initializing StatusAreaWidget.
1018 system_tray_delegate_
.reset(delegate()->CreateSystemTrayDelegate());
1019 DCHECK(system_tray_delegate_
.get());
1021 locale_notification_controller_
.reset(new LocaleNotificationController
);
1023 // Initialize system_tray_delegate_ after StatusAreaWidget is created.
1024 system_tray_delegate_
->Initialize();
1026 #if defined(OS_CHROMEOS)
1027 // Create the LogoutConfirmationController after the SystemTrayDelegate.
1028 logout_confirmation_controller_
.reset(new LogoutConfirmationController(
1029 base::Bind(&SystemTrayDelegate::SignOut
,
1030 base::Unretained(system_tray_delegate_
.get()))));
1032 // Create TouchTransformerController before DisplayController::InitDisplays()
1033 // since TouchTransformerController listens on
1034 // DisplayController::Observer::OnDisplaysInitialized().
1035 touch_transformer_controller_
.reset(new TouchTransformerController());
1036 #endif // defined(OS_CHROMEOS)
1038 display_controller_
->InitDisplays();
1040 #if defined(OS_CHROMEOS)
1041 // Needs to be created after InitDisplays() since it may cause the virtual
1042 // keyboard to be deployed.
1043 virtual_keyboard_controller_
.reset(new VirtualKeyboardController
);
1044 #endif // defined(OS_CHROMEOS)
1046 // It needs to be created after RootWindowController has been created
1047 // (which calls OnWindowResized has been called, otherwise the
1048 // widget will not paint when restoring after a browser crash. Also it needs
1049 // to be created after InitSecondaryDisplays() to initialize the wallpapers in
1050 // the correct size.
1051 user_wallpaper_delegate_
->InitializeWallpaper();
1053 if (initially_hide_cursor_
)
1054 cursor_manager_
.HideCursor();
1055 cursor_manager_
.SetCursor(ui::kCursorPointer
);
1057 #if defined(OS_CHROMEOS)
1058 // Set accelerator controller delegates.
1059 accelerator_controller_
->SetBrightnessControlDelegate(
1060 scoped_ptr
<ash::BrightnessControlDelegate
>(
1061 new ash::system::BrightnessControllerChromeos
).Pass());
1063 power_event_observer_
.reset(new PowerEventObserver());
1064 user_activity_notifier_
.reset(
1065 new ui::UserActivityPowerManagerNotifier(user_activity_detector_
.get()));
1066 video_activity_notifier_
.reset(
1067 new VideoActivityNotifier(video_detector_
.get()));
1068 bluetooth_notification_controller_
.reset(new BluetoothNotificationController
);
1069 last_window_closed_logout_reminder_
.reset(new LastWindowClosedLogoutReminder
);
1070 screen_orientation_controller_
.reset(new ScreenOrientationController());
1072 // The compositor thread and main message loop have to be running in
1073 // order to create mirror window. Run it after the main message loop
1075 display_manager_
->CreateMirrorWindowAsyncIfAny();
1078 void Shell::InitKeyboard() {
1079 if (keyboard::IsKeyboardEnabled()) {
1080 if (keyboard::KeyboardController::GetInstance()) {
1081 RootWindowControllerList controllers
= GetAllRootWindowControllers();
1082 for (RootWindowControllerList::iterator iter
= controllers
.begin();
1083 iter
!= controllers
.end(); ++iter
) {
1084 (*iter
)->DeactivateKeyboard(
1085 keyboard::KeyboardController::GetInstance());
1088 keyboard::KeyboardControllerProxy
* proxy
=
1089 delegate_
->CreateKeyboardControllerProxy();
1090 keyboard::KeyboardController::ResetInstance(
1091 new keyboard::KeyboardController(proxy
));
1095 void Shell::InitRootWindow(aura::Window
* root_window
) {
1096 DCHECK(activation_client_
);
1097 DCHECK(visibility_controller_
.get());
1098 DCHECK(drag_drop_controller_
.get());
1100 aura::client::SetFocusClient(root_window
, focus_client_
.get());
1101 input_method_filter_
->SetInputMethodPropertyInRootWindow(root_window
);
1102 aura::client::SetActivationClient(root_window
, activation_client_
);
1103 ::wm::FocusController
* focus_controller
=
1104 static_cast< ::wm::FocusController
*>(activation_client_
);
1105 root_window
->AddPreTargetHandler(focus_controller
);
1106 aura::client::SetVisibilityClient(root_window
, visibility_controller_
.get());
1107 aura::client::SetDragDropClient(root_window
, drag_drop_controller_
.get());
1108 aura::client::SetScreenPositionClient(root_window
,
1109 screen_position_controller_
.get());
1110 aura::client::SetCursorClient(root_window
, &cursor_manager_
);
1111 aura::client::SetTooltipClient(root_window
, tooltip_controller_
.get());
1112 aura::client::SetEventClient(root_window
, event_client_
.get());
1114 aura::client::SetWindowMoveClient(root_window
,
1115 toplevel_window_event_handler_
.get());
1116 root_window
->AddPreTargetHandler(toplevel_window_event_handler_
.get());
1117 root_window
->AddPostTargetHandler(toplevel_window_event_handler_
.get());
1119 if (nested_accelerator_controller_
) {
1120 aura::client::SetDispatcherClient(root_window
,
1121 nested_accelerator_controller_
.get());
1125 bool Shell::CanWindowReceiveEvents(aura::Window
* window
) {
1126 RootWindowControllerList controllers
= GetAllRootWindowControllers();
1127 for (RootWindowControllerList::iterator iter
= controllers
.begin();
1128 iter
!= controllers
.end(); ++iter
) {
1129 SystemModalContainerLayoutManager
* layout_manager
=
1130 (*iter
)->GetSystemModalLayoutManager(window
);
1131 if (layout_manager
&& layout_manager
->CanWindowReceiveEvents(window
))
1133 // Allow events to fall through to the virtual keyboard even if displaying
1134 // a system modal dialog.
1135 if ((*iter
)->IsVirtualKeyboardWindow(window
))
1142 ////////////////////////////////////////////////////////////////////////////////
1143 // Shell, ui::EventTarget overrides:
1145 bool Shell::CanAcceptEvent(const ui::Event
& event
) {
1149 ui::EventTarget
* Shell::GetParentTarget() {
1150 return aura::Env::GetInstance();
1153 scoped_ptr
<ui::EventTargetIterator
> Shell::GetChildIterator() const {
1154 return scoped_ptr
<ui::EventTargetIterator
>();
1157 ui::EventTargeter
* Shell::GetEventTargeter() {
1162 void Shell::OnEvent(ui::Event
* event
) {
1165 ////////////////////////////////////////////////////////////////////////////////
1166 // Shell, aura::client::ActivationChangeObserver implementation:
1168 void Shell::OnWindowActivated(aura::Window
* gained_active
,
1169 aura::Window
* lost_active
) {
1171 target_root_window_
= gained_active
->GetRootWindow();