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/drag_drop/drag_drop_controller.h"
27 #include "ash/first_run/first_run_helper_impl.h"
28 #include "ash/focus_cycler.h"
29 #include "ash/frame/custom_frame_view_ash.h"
30 #include "ash/gpu_support.h"
31 #include "ash/high_contrast/high_contrast_controller.h"
32 #include "ash/host/ash_window_tree_host_init_params.h"
33 #include "ash/keyboard_uma_event_filter.h"
34 #include "ash/magnifier/magnification_controller.h"
35 #include "ash/magnifier/partial_magnification_controller.h"
36 #include "ash/media_delegate.h"
37 #include "ash/new_window_delegate.h"
38 #include "ash/root_window_controller.h"
39 #include "ash/session/session_state_delegate.h"
40 #include "ash/shelf/app_list_shelf_item_delegate.h"
41 #include "ash/shelf/shelf_delegate.h"
42 #include "ash/shelf/shelf_item_delegate.h"
43 #include "ash/shelf/shelf_item_delegate_manager.h"
44 #include "ash/shelf/shelf_layout_manager.h"
45 #include "ash/shelf/shelf_model.h"
46 #include "ash/shelf/shelf_widget.h"
47 #include "ash/shelf/shelf_window_watcher.h"
48 #include "ash/shell_delegate.h"
49 #include "ash/shell_factory.h"
50 #include "ash/shell_init_params.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_cycle_controller.h"
78 #include "ash/wm/window_positioner.h"
79 #include "ash/wm/window_properties.h"
80 #include "ash/wm/window_util.h"
81 #include "ash/wm/workspace_controller.h"
82 #include "base/bind.h"
83 #include "base/debug/trace_event.h"
84 #include "ui/aura/client/aura_constants.h"
85 #include "ui/aura/env.h"
86 #include "ui/aura/layout_manager.h"
87 #include "ui/aura/window.h"
88 #include "ui/aura/window_event_dispatcher.h"
89 #include "ui/base/ui_base_switches.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/image/image_skia.h"
95 #include "ui/gfx/screen.h"
96 #include "ui/gfx/size.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/user_activity_detector.h"
114 #include "ui/wm/core/visibility_controller.h"
115 #include "ui/wm/core/window_modality_controller.h"
117 #if defined(OS_CHROMEOS)
119 #include "ash/accelerators/magnifier_key_scroller.h"
120 #include "ash/accelerators/spoken_feedback_toggler.h"
121 #include "ash/touch/touch_transformer_controller.h"
122 #include "ui/gfx/x/x11_types.h"
123 #endif // defined(USE_X11)
124 #include "ash/ash_constants.h"
125 #include "ash/display/display_change_observer_chromeos.h"
126 #include "ash/display/display_configurator_animation.h"
127 #include "ash/display/display_error_observer_chromeos.h"
128 #include "ash/display/projecting_observer_chromeos.h"
129 #include "ash/display/resolution_notification_controller.h"
130 #include "ash/sticky_keys/sticky_keys_controller.h"
131 #include "ash/system/chromeos/bluetooth/bluetooth_notification_controller.h"
132 #include "ash/system/chromeos/brightness/brightness_controller_chromeos.h"
133 #include "ash/system/chromeos/power/power_event_observer.h"
134 #include "ash/system/chromeos/power/power_status.h"
135 #include "ash/system/chromeos/power/video_activity_notifier.h"
136 #include "ash/system/chromeos/session/last_window_closed_logout_reminder.h"
137 #include "ash/system/chromeos/session/logout_confirmation_controller.h"
138 #include "base/bind_helpers.h"
139 #include "base/sys_info.h"
140 #include "ui/chromeos/user_activity_power_manager_notifier.h"
141 #include "ui/display/chromeos/display_configurator.h"
142 #endif // defined(OS_CHROMEOS)
151 // A Corewm VisibilityController subclass that calls the Ash animation routine
152 // so we can pick up our extended animations. See ash/wm/window_animations.h.
153 class AshVisibilityController
: public ::wm::VisibilityController
{
155 AshVisibilityController() {}
156 virtual ~AshVisibilityController() {}
159 // Overridden from ::wm::VisibilityController:
160 virtual bool CallAnimateOnChildWindowVisibilityChanged(
161 aura::Window
* window
,
162 bool visible
) OVERRIDE
{
163 return AnimateOnChildWindowVisibilityChanged(window
, visible
);
166 DISALLOW_COPY_AND_ASSIGN(AshVisibilityController
);
169 AshWindowTreeHostInitParams
ShellInitParamsToAshWindowTreeHostInitParams(
170 const ShellInitParams
& shell_init_params
) {
171 AshWindowTreeHostInitParams ash_init_params
;
173 ash_init_params
.remote_hwnd
= shell_init_params
.remote_hwnd
;
175 return ash_init_params
;
181 Shell
* Shell::instance_
= NULL
;
183 bool Shell::initially_hide_cursor_
= false;
185 ////////////////////////////////////////////////////////////////////////////////
189 Shell
* Shell::CreateInstance(const ShellInitParams
& init_params
) {
191 instance_
= new Shell(init_params
.delegate
);
192 instance_
->Init(init_params
);
197 Shell
* Shell::GetInstance() {
203 bool Shell::HasInstance() {
208 void Shell::DeleteInstance() {
214 RootWindowController
* Shell::GetPrimaryRootWindowController() {
215 CHECK(HasInstance());
216 return GetRootWindowController(GetPrimaryRootWindow());
220 Shell::RootWindowControllerList
Shell::GetAllRootWindowControllers() {
221 CHECK(HasInstance());
222 return Shell::GetInstance()->display_controller()->
223 GetAllRootWindowControllers();
227 aura::Window
* Shell::GetPrimaryRootWindow() {
228 CHECK(HasInstance());
229 return GetInstance()->display_controller()->GetPrimaryRootWindow();
233 aura::Window
* Shell::GetTargetRootWindow() {
234 CHECK(HasInstance());
235 Shell
* shell
= GetInstance();
236 if (shell
->scoped_target_root_window_
)
237 return shell
->scoped_target_root_window_
;
238 return shell
->target_root_window_
;
242 gfx::Screen
* Shell::GetScreen() {
243 return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE
);
247 aura::Window::Windows
Shell::GetAllRootWindows() {
248 CHECK(HasInstance());
249 return Shell::GetInstance()->display_controller()->
254 aura::Window
* Shell::GetContainer(aura::Window
* root_window
,
256 return root_window
->GetChildById(container_id
);
260 const aura::Window
* Shell::GetContainer(const aura::Window
* root_window
,
262 return root_window
->GetChildById(container_id
);
266 std::vector
<aura::Window
*> Shell::GetContainersFromAllRootWindows(
268 aura::Window
* priority_root
) {
269 std::vector
<aura::Window
*> containers
;
270 aura::Window::Windows root_windows
= GetAllRootWindows();
271 for (aura::Window::Windows::const_iterator it
= root_windows
.begin();
272 it
!= root_windows
.end(); ++it
) {
273 aura::Window
* container
= (*it
)->GetChildById(container_id
);
275 if (priority_root
&& priority_root
->Contains(container
))
276 containers
.insert(containers
.begin(), container
);
278 containers
.push_back(container
);
284 void Shell::ShowContextMenu(const gfx::Point
& location_in_screen
,
285 ui::MenuSourceType source_type
) {
286 // No context menus if there is no session with an active user.
287 if (!session_state_delegate_
->NumberOfLoggedInUsers())
289 // No context menus when screen is locked.
290 if (session_state_delegate_
->IsScreenLocked())
294 wm::GetRootWindowMatching(gfx::Rect(location_in_screen
, gfx::Size()));
295 GetRootWindowController(root
)
296 ->ShowContextMenu(location_in_screen
, source_type
);
299 void Shell::ShowAppList(aura::Window
* window
) {
300 // If the context window is not given, show it on the target root window.
302 window
= GetTargetRootWindow();
303 if (!app_list_controller_
)
304 app_list_controller_
.reset(new AppListController
);
305 app_list_controller_
->Show(window
);
308 void Shell::DismissAppList() {
309 if (!app_list_controller_
)
311 app_list_controller_
->Dismiss();
314 void Shell::ToggleAppList(aura::Window
* window
) {
315 if (app_list_controller_
&& app_list_controller_
->IsVisible()) {
323 bool Shell::GetAppListTargetVisibility() const {
324 return app_list_controller_
.get() &&
325 app_list_controller_
->GetTargetVisibility();
328 aura::Window
* Shell::GetAppListWindow() {
329 return app_list_controller_
.get() ? app_list_controller_
->GetWindow() : NULL
;
332 app_list::AppListView
* Shell::GetAppListView() {
333 return app_list_controller_
.get() ? app_list_controller_
->GetView() : NULL
;
336 bool Shell::IsSystemModalWindowOpen() const {
337 if (simulate_modal_window_open_for_testing_
)
339 const std::vector
<aura::Window
*> containers
= GetContainersFromAllRootWindows(
340 kShellWindowId_SystemModalContainer
, NULL
);
341 for (std::vector
<aura::Window
*>::const_iterator cit
= containers
.begin();
342 cit
!= containers
.end(); ++cit
) {
343 for (aura::Window::Windows::const_iterator wit
= (*cit
)->children().begin();
344 wit
!= (*cit
)->children().end(); ++wit
) {
345 if ((*wit
)->GetProperty(aura::client::kModalKey
) ==
346 ui::MODAL_TYPE_SYSTEM
&& (*wit
)->TargetVisibility()) {
354 views::NonClientFrameView
* Shell::CreateDefaultNonClientFrameView(
355 views::Widget
* widget
) {
356 // Use translucent-style window frames for dialogs.
357 return new CustomFrameViewAsh(widget
);
360 void Shell::RotateFocus(Direction direction
) {
361 focus_cycler_
->RotateFocus(direction
== FORWARD
? FocusCycler::FORWARD
362 : FocusCycler::BACKWARD
);
365 void Shell::SetDisplayWorkAreaInsets(Window
* contains
,
366 const gfx::Insets
& insets
) {
367 if (!display_controller_
->UpdateWorkAreaOfDisplayNearestWindow(
371 FOR_EACH_OBSERVER(ShellObserver
, observers_
,
372 OnDisplayWorkAreaInsetsChanged());
375 void Shell::OnLoginStateChanged(user::LoginStatus status
) {
376 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnLoginStateChanged(status
));
379 void Shell::OnLoginUserProfilePrepared() {
384 void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status
) {
385 RootWindowControllerList controllers
= GetAllRootWindowControllers();
386 for (RootWindowControllerList::iterator iter
= controllers
.begin();
387 iter
!= controllers
.end(); ++iter
)
388 (*iter
)->UpdateAfterLoginStatusChange(status
);
391 void Shell::OnAppTerminating() {
392 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnAppTerminating());
395 void Shell::OnLockStateChanged(bool locked
) {
396 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnLockStateChanged(locked
));
398 // Make sure that there is no system modal in Lock layer when unlocked.
400 std::vector
<aura::Window
*> containers
= GetContainersFromAllRootWindows(
401 kShellWindowId_LockSystemModalContainer
, GetPrimaryRootWindow());
402 for (std::vector
<aura::Window
*>::const_iterator iter
= containers
.begin();
403 iter
!= containers
.end(); ++iter
) {
404 DCHECK_EQ(0u, (*iter
)->children().size());
410 void Shell::OnCastingSessionStartedOrStopped(bool started
) {
411 #if defined(OS_CHROMEOS) && defined(USE_X11)
412 if (projecting_observer_
)
413 projecting_observer_
->OnCastingSessionStartedOrStopped(started
);
417 void Shell::OnOverviewModeStarting() {
418 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnOverviewModeStarting());
421 void Shell::OnOverviewModeEnding() {
422 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnOverviewModeEnding());
425 void Shell::OnMaximizeModeStarted() {
426 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnMaximizeModeStarted());
429 void Shell::OnMaximizeModeEnded() {
430 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnMaximizeModeEnded());
433 void Shell::OnRootWindowAdded(aura::Window
* root_window
) {
434 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnRootWindowAdded(root_window
));
437 void Shell::CreateShelf() {
438 RootWindowControllerList controllers
= GetAllRootWindowControllers();
439 for (RootWindowControllerList::iterator iter
= controllers
.begin();
440 iter
!= controllers
.end(); ++iter
)
441 (*iter
)->shelf()->CreateShelf();
444 void Shell::OnShelfCreatedForRootWindow(aura::Window
* root_window
) {
445 FOR_EACH_OBSERVER(ShellObserver
,
447 OnShelfCreatedForRootWindow(root_window
));
450 void Shell::CreateKeyboard() {
451 // TODO(bshe): Primary root window controller may not be the controller to
452 // attach virtual keyboard. See http://crbug.com/303429
454 GetPrimaryRootWindowController()->
455 ActivateKeyboard(keyboard::KeyboardController::GetInstance());
458 void Shell::DeactivateKeyboard() {
459 if (keyboard::KeyboardController::GetInstance()) {
460 RootWindowControllerList controllers
= GetAllRootWindowControllers();
461 for (RootWindowControllerList::iterator iter
= controllers
.begin();
462 iter
!= controllers
.end(); ++iter
) {
463 (*iter
)->DeactivateKeyboard(keyboard::KeyboardController::GetInstance());
466 keyboard::KeyboardController::ResetInstance(NULL
);
469 void Shell::ShowShelf() {
470 RootWindowControllerList controllers
= GetAllRootWindowControllers();
471 for (RootWindowControllerList::iterator iter
= controllers
.begin();
472 iter
!= controllers
.end(); ++iter
)
473 (*iter
)->ShowShelf();
476 void Shell::AddShellObserver(ShellObserver
* observer
) {
477 observers_
.AddObserver(observer
);
480 void Shell::RemoveShellObserver(ShellObserver
* observer
) {
481 observers_
.RemoveObserver(observer
);
484 #if defined(OS_CHROMEOS)
485 bool Shell::ShouldSaveDisplaySettings() {
486 return !((maximize_mode_controller_
->IsMaximizeModeWindowManagerEnabled() &&
487 maximize_mode_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 accelerometer_controller_(new AccelerometerController()),
640 #if defined(OS_CHROMEOS)
641 display_configurator_(new ui::DisplayConfigurator()),
642 #endif // defined(OS_CHROMEOS)
643 native_cursor_manager_(new AshNativeCursorManager
),
645 scoped_ptr
< ::wm::NativeCursorManager
>(native_cursor_manager_
)),
646 simulate_modal_window_open_for_testing_(false),
647 is_touch_hud_projection_enabled_(false) {
648 DCHECK(delegate_
.get());
649 gpu_support_
.reset(delegate_
->CreateGPUSupport());
650 display_manager_
.reset(new DisplayManager
);
651 display_controller_
.reset(new DisplayController
);
652 #if defined(OS_CHROMEOS) && defined(USE_X11)
653 user_metrics_recorder_
.reset(new UserMetricsRecorder
);
654 #endif // defined(OS_CHROMEOS)
656 #if defined(OS_CHROMEOS)
657 PowerStatus::Initialize();
662 TRACE_EVENT0("shutdown", "ash::Shell::Destructor");
664 delegate_
->PreShutdown();
666 views::FocusManagerFactory::Install(NULL
);
668 // Remove the focus from any window. This will prevent overhead and side
669 // effects (e.g. crashes) from changing focus during shutdown.
670 // See bug crbug.com/134502.
671 aura::client::GetFocusClient(GetPrimaryRootWindow())->FocusWindow(NULL
);
673 // Please keep in same order as in Init() because it's easy to miss one.
674 if (window_modality_controller_
)
675 window_modality_controller_
.reset();
676 #if defined(OS_CHROMEOS) && defined(USE_X11)
677 RemovePreTargetHandler(magnifier_key_scroll_handler_
.get());
678 magnifier_key_scroll_handler_
.reset();
680 RemovePreTargetHandler(speech_feedback_handler_
.get());
681 speech_feedback_handler_
.reset();
683 RemovePreTargetHandler(user_activity_detector_
.get());
684 RemovePreTargetHandler(overlay_filter_
.get());
685 RemovePreTargetHandler(input_method_filter_
.get());
686 RemovePreTargetHandler(accelerator_filter_
.get());
687 RemovePreTargetHandler(event_transformation_handler_
.get());
688 RemovePreTargetHandler(toplevel_window_event_handler_
.get());
689 RemovePostTargetHandler(toplevel_window_event_handler_
.get());
690 RemovePreTargetHandler(system_gesture_filter_
.get());
691 RemovePreTargetHandler(keyboard_metrics_filter_
.get());
692 RemovePreTargetHandler(mouse_cursor_filter_
.get());
694 // TooltipController is deleted with the Shell so removing its references.
695 RemovePreTargetHandler(tooltip_controller_
.get());
697 // Destroy maximize mode controller early on since it has some observers which
698 // need to be removed.
699 maximize_mode_controller_
->Shutdown();
700 maximize_mode_controller_
.reset();
702 // AppList needs to be released before shelf layout manager, which is
703 // destroyed with shelf container in the loop below. However, app list
704 // container is now on top of shelf container and released after it.
705 // TODO(xiyuan): Move it back when app list container is no longer needed.
706 app_list_controller_
.reset();
708 #if defined(OS_CHROMEOS)
709 // Destroy the LastWindowClosedLogoutReminder before the
710 // LogoutConfirmationController.
711 last_window_closed_logout_reminder_
.reset();
713 // Destroy the LogoutConfirmationController before the SystemTrayDelegate.
714 logout_confirmation_controller_
.reset();
717 // Destroy SystemTrayDelegate before destroying the status area(s).
718 system_tray_delegate_
->Shutdown();
719 system_tray_delegate_
.reset();
721 locale_notification_controller_
.reset();
723 // Drag-and-drop must be canceled prior to close all windows.
724 drag_drop_controller_
.reset();
726 // Controllers who have WindowObserver added must be deleted
727 // before |display_controller_| is deleted.
729 #if defined(OS_CHROMEOS)
730 // VideoActivityNotifier must be deleted before |video_detector_| is
731 // deleted because it's observing video activity through
732 // VideoDetectorObserver interface.
733 video_activity_notifier_
.reset();
734 #endif // defined(OS_CHROMEOS)
735 video_detector_
.reset();
736 high_contrast_controller_
.reset();
738 shadow_controller_
.reset();
739 resize_shadow_controller_
.reset();
741 window_cycle_controller_
.reset();
742 window_selector_controller_
.reset();
743 mru_window_tracker_
.reset();
745 // |shelf_window_watcher_| has a weak pointer to |shelf_Model_|
746 // and has window observers.
747 shelf_window_watcher_
.reset();
749 // Destroy all child windows including widgets.
750 display_controller_
->CloseChildWindows();
751 display_controller_
->CloseNonDesktopDisplay();
753 // Chrome implementation of shelf delegate depends on FocusClient,
754 // so must be deleted before |focus_client_|.
755 shelf_delegate_
.reset();
756 focus_client_
.reset();
758 // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems
759 // needs to remove observers from it.
760 system_tray_notifier_
.reset();
762 // These need a valid Shell instance to clean up properly, so explicitly
763 // delete them before invalidating the instance.
764 // Alphabetical. TODO(oshima): sort.
765 magnification_controller_
.reset();
766 partial_magnification_controller_
.reset();
767 tooltip_controller_
.reset();
768 event_client_
.reset();
769 nested_accelerator_controller_
.reset();
770 toplevel_window_event_handler_
.reset();
771 visibility_controller_
.reset();
772 // |shelf_item_delegate_manager_| observes |shelf_model_|. It must be
773 // destroyed before |shelf_model_| is destroyed.
774 shelf_item_delegate_manager_
.reset();
775 shelf_model_
.reset();
777 power_button_controller_
.reset();
778 lock_state_controller_
.reset();
780 #if defined(OS_CHROMEOS)
781 resolution_notification_controller_
.reset();
783 desktop_background_controller_
.reset();
784 mouse_cursor_filter_
.reset();
786 #if defined(OS_CHROMEOS) && defined(USE_X11)
787 touch_transformer_controller_
.reset();
788 #endif // defined(OS_CHROMEOS) && defined(USE_X11)
790 // This also deletes all RootWindows. Note that we invoke Shutdown() on
791 // DisplayController before resetting |display_controller_|, since destruction
792 // of its owned RootWindowControllers relies on the value.
793 display_manager_
->CreateScreenForShutdown();
794 display_controller_
->Shutdown();
795 display_controller_
.reset();
796 screen_position_controller_
.reset();
797 accessibility_delegate_
.reset();
798 new_window_delegate_
.reset();
799 media_delegate_
.reset();
801 keyboard::KeyboardController::ResetInstance(NULL
);
803 #if defined(OS_CHROMEOS)
804 if (display_change_observer_
)
805 display_configurator_
->RemoveObserver(display_change_observer_
.get());
806 if (display_configurator_animation_
)
807 display_configurator_
->RemoveObserver(
808 display_configurator_animation_
.get());
809 if (display_error_observer_
)
810 display_configurator_
->RemoveObserver(display_error_observer_
.get());
811 if (projecting_observer_
)
812 display_configurator_
->RemoveObserver(projecting_observer_
.get());
813 display_change_observer_
.reset();
814 #endif // defined(OS_CHROMEOS)
816 #if defined(OS_CHROMEOS)
817 PowerStatus::Shutdown();
820 DCHECK(instance_
== this);
824 void Shell::Init(const ShellInitParams
& init_params
) {
825 delegate_
->PreInit();
826 bool display_initialized
= display_manager_
->InitFromCommandLine();
827 #if defined(OS_CHROMEOS)
828 display_configurator_
->Init(!gpu_support_
->IsPanelFittingDisabled());
829 display_configurator_animation_
.reset(new DisplayConfiguratorAnimation());
830 display_configurator_
->AddObserver(display_configurator_animation_
.get());
832 projecting_observer_
.reset(new ProjectingObserver());
833 display_configurator_
->AddObserver(projecting_observer_
.get());
835 if (!display_initialized
&& base::SysInfo::IsRunningOnChromeOS()) {
836 display_change_observer_
.reset(new DisplayChangeObserver
);
837 // Register |display_change_observer_| first so that the rest of
838 // observer gets invoked after the root windows are configured.
839 display_configurator_
->AddObserver(display_change_observer_
.get());
840 display_error_observer_
.reset(new DisplayErrorObserver());
841 display_configurator_
->AddObserver(display_error_observer_
.get());
842 display_configurator_
->set_state_controller(display_change_observer_
.get());
843 display_configurator_
->set_mirroring_controller(display_manager_
.get());
844 display_configurator_
->ForceInitialConfigure(
845 delegate_
->IsFirstRunAfterBoot() ? kChromeOsBootColor
: 0);
846 display_initialized
= true;
848 #endif // defined(OS_CHROMEOS)
849 if (!display_initialized
)
850 display_manager_
->InitDefaultDisplay();
852 display_manager_
->InitFontParams();
854 // Install the custom factory first so that views::FocusManagers for Tray,
855 // Shelf, and WallPaper could be created by the factory.
856 views::FocusManagerFactory::Install(new AshFocusManagerFactory
);
858 aura::Env::CreateInstance(true);
859 aura::Env::GetInstance()->set_context_factory(init_params
.context_factory
);
861 // The WindowModalityController needs to be at the front of the input event
862 // pretarget handler list to ensure that it processes input events when modal
863 // windows are active.
864 window_modality_controller_
.reset(
865 new ::wm::WindowModalityController(this));
867 env_filter_
.reset(new ::wm::CompoundEventFilter
);
868 AddPreTargetHandler(env_filter_
.get());
870 ::wm::FocusController
* focus_controller
=
871 new ::wm::FocusController(new wm::AshFocusRules
);
872 focus_client_
.reset(focus_controller
);
873 activation_client_
= focus_controller
;
874 activation_client_
->AddObserver(this);
875 focus_cycler_
.reset(new FocusCycler());
877 screen_position_controller_
.reset(new ScreenPositionController
);
879 display_controller_
->Start();
880 display_controller_
->CreatePrimaryHost(
881 ShellInitParamsToAshWindowTreeHostInitParams(init_params
));
882 aura::Window
* root_window
= display_controller_
->GetPrimaryRootWindow();
883 target_root_window_
= root_window
;
885 #if defined(OS_CHROMEOS)
886 resolution_notification_controller_
.reset(
887 new ResolutionNotificationController
);
890 cursor_manager_
.SetDisplay(GetScreen()->GetPrimaryDisplay());
892 nested_accelerator_controller_
.reset(
893 new ::wm::NestedAcceleratorController(new NestedAcceleratorDelegate
));
894 accelerator_controller_
.reset(new AcceleratorController
);
895 maximize_mode_controller_
.reset(new MaximizeModeController());
897 #if defined(OS_CHROMEOS) && defined(USE_X11)
898 magnifier_key_scroll_handler_
= MagnifierKeyScroller::CreateHandler().Pass();
899 AddPreTargetHandler(magnifier_key_scroll_handler_
.get());
900 speech_feedback_handler_
= SpokenFeedbackToggler::CreateHandler().Pass();
901 AddPreTargetHandler(speech_feedback_handler_
.get());
904 // The order in which event filters are added is significant.
906 // wm::UserActivityDetector passes events to observers, so let them get
908 user_activity_detector_
.reset(new ::wm::UserActivityDetector
);
909 AddPreTargetHandler(user_activity_detector_
.get());
911 overlay_filter_
.reset(new OverlayEventFilter
);
912 AddPreTargetHandler(overlay_filter_
.get());
913 AddShellObserver(overlay_filter_
.get());
915 input_method_filter_
.reset(new ::wm::InputMethodEventFilter(
916 root_window
->GetHost()->GetAcceleratedWidget()));
917 AddPreTargetHandler(input_method_filter_
.get());
919 accelerator_filter_
.reset(new ::wm::AcceleratorFilter(
920 scoped_ptr
< ::wm::AcceleratorDelegate
>(new AcceleratorDelegate
).Pass()));
921 AddPreTargetHandler(accelerator_filter_
.get());
923 event_transformation_handler_
.reset(new EventTransformationHandler
);
924 AddPreTargetHandler(event_transformation_handler_
.get());
926 toplevel_window_event_handler_
.reset(new ToplevelWindowEventHandler
);
928 system_gesture_filter_
.reset(new SystemGestureEventFilter
);
929 AddPreTargetHandler(system_gesture_filter_
.get());
931 keyboard_metrics_filter_
.reset(new KeyboardUMAEventFilter
);
932 AddPreTargetHandler(keyboard_metrics_filter_
.get());
934 // The keyboard system must be initialized before the RootWindowController is
936 #if defined(OS_CHROMEOS)
937 keyboard::InitializeKeyboard();
940 #if defined(OS_CHROMEOS)
941 sticky_keys_controller_
.reset(new StickyKeysController
);
944 lock_state_controller_
.reset(new LockStateController
);
945 power_button_controller_
.reset(new PowerButtonController(
946 lock_state_controller_
.get()));
947 #if defined(OS_CHROMEOS)
948 // Pass the initial display state to PowerButtonController.
949 power_button_controller_
->OnDisplayModeChanged(
950 display_configurator_
->cached_displays());
952 AddShellObserver(lock_state_controller_
.get());
954 drag_drop_controller_
.reset(new DragDropController
);
955 mouse_cursor_filter_
.reset(new MouseCursorEventFilter());
956 PrependPreTargetHandler(mouse_cursor_filter_
.get());
958 // Create Controllers that may need root window.
959 // TODO(oshima): Move as many controllers before creating
960 // RootWindowController as possible.
961 visibility_controller_
.reset(new AshVisibilityController
);
963 magnification_controller_
.reset(
964 MagnificationController::CreateInstance());
965 mru_window_tracker_
.reset(new MruWindowTracker(activation_client_
));
967 partial_magnification_controller_
.reset(
968 new PartialMagnificationController());
970 autoclick_controller_
.reset(AutoclickController::CreateInstance());
972 high_contrast_controller_
.reset(new HighContrastController
);
973 video_detector_
.reset(new VideoDetector
);
974 window_selector_controller_
.reset(new WindowSelectorController());
975 window_cycle_controller_
.reset(new WindowCycleController());
977 tooltip_controller_
.reset(
978 new views::corewm::TooltipController(
979 scoped_ptr
<views::corewm::Tooltip
>(
980 new views::corewm::TooltipAura(gfx::SCREEN_TYPE_ALTERNATE
))));
981 AddPreTargetHandler(tooltip_controller_
.get());
983 event_client_
.reset(new EventClientImpl
);
985 // This controller needs to be set before SetupManagedWindowMode.
986 desktop_background_controller_
.reset(new DesktopBackgroundController());
987 user_wallpaper_delegate_
.reset(delegate_
->CreateUserWallpaperDelegate());
989 session_state_delegate_
.reset(delegate_
->CreateSessionStateDelegate());
990 accessibility_delegate_
.reset(delegate_
->CreateAccessibilityDelegate());
991 new_window_delegate_
.reset(delegate_
->CreateNewWindowDelegate());
992 media_delegate_
.reset(delegate_
->CreateMediaDelegate());
994 resize_shadow_controller_
.reset(new ResizeShadowController());
995 shadow_controller_
.reset(
996 new ::wm::ShadowController(activation_client_
));
998 // Create system_tray_notifier_ before the delegate.
999 system_tray_notifier_
.reset(new ash::SystemTrayNotifier());
1001 // Initialize system_tray_delegate_ before initializing StatusAreaWidget.
1002 system_tray_delegate_
.reset(delegate()->CreateSystemTrayDelegate());
1003 DCHECK(system_tray_delegate_
.get());
1005 locale_notification_controller_
.reset(new LocaleNotificationController
);
1007 // Initialize system_tray_delegate_ after StatusAreaWidget is created.
1008 system_tray_delegate_
->Initialize();
1010 #if defined(OS_CHROMEOS)
1011 // Create the LogoutConfirmationController after the SystemTrayDelegate.
1012 logout_confirmation_controller_
.reset(new LogoutConfirmationController(
1013 base::Bind(&SystemTrayDelegate::SignOut
,
1014 base::Unretained(system_tray_delegate_
.get()))));
1017 #if defined(OS_CHROMEOS) && defined(USE_X11)
1018 // Create TouchTransformerController before DisplayController::InitDisplays()
1019 // since TouchTransformerController listens on
1020 // DisplayController::Observer::OnDisplaysInitialized().
1021 touch_transformer_controller_
.reset(new TouchTransformerController());
1022 #endif // defined(OS_CHROMEOS) && defined(USE_X11)
1024 display_controller_
->InitDisplays();
1026 // It needs to be created after RootWindowController has been created
1027 // (which calls OnWindowResized has been called, otherwise the
1028 // widget will not paint when restoring after a browser crash. Also it needs
1029 // to be created after InitSecondaryDisplays() to initialize the wallpapers in
1030 // the correct size.
1031 user_wallpaper_delegate_
->InitializeWallpaper();
1033 if (initially_hide_cursor_
)
1034 cursor_manager_
.HideCursor();
1035 cursor_manager_
.SetCursor(ui::kCursorPointer
);
1037 #if defined(OS_CHROMEOS)
1038 // Set accelerator controller delegates.
1039 accelerator_controller_
->SetBrightnessControlDelegate(
1040 scoped_ptr
<ash::BrightnessControlDelegate
>(
1041 new ash::system::BrightnessControllerChromeos
).Pass());
1043 power_event_observer_
.reset(new PowerEventObserver());
1044 user_activity_notifier_
.reset(
1045 new ui::UserActivityPowerManagerNotifier(user_activity_detector_
.get()));
1046 video_activity_notifier_
.reset(
1047 new VideoActivityNotifier(video_detector_
.get()));
1048 bluetooth_notification_controller_
.reset(new BluetoothNotificationController
);
1049 last_window_closed_logout_reminder_
.reset(new LastWindowClosedLogoutReminder
);
1052 weak_display_manager_factory_
.reset(
1053 new base::WeakPtrFactory
<DisplayManager
>(display_manager_
.get()));
1054 // The compositor thread and main message loop have to be running in
1055 // order to create mirror window. Run it after the main message loop
1057 base::MessageLoopForUI::current()->PostTask(
1059 base::Bind(&DisplayManager::CreateMirrorWindowIfAny
,
1060 weak_display_manager_factory_
->GetWeakPtr()));
1063 void Shell::InitKeyboard() {
1064 if (keyboard::IsKeyboardEnabled()) {
1065 if (keyboard::KeyboardController::GetInstance()) {
1066 RootWindowControllerList controllers
= GetAllRootWindowControllers();
1067 for (RootWindowControllerList::iterator iter
= controllers
.begin();
1068 iter
!= controllers
.end(); ++iter
) {
1069 (*iter
)->DeactivateKeyboard(
1070 keyboard::KeyboardController::GetInstance());
1073 keyboard::KeyboardControllerProxy
* proxy
=
1074 delegate_
->CreateKeyboardControllerProxy();
1075 keyboard::KeyboardController::ResetInstance(
1076 new keyboard::KeyboardController(proxy
));
1080 void Shell::InitRootWindow(aura::Window
* root_window
) {
1081 DCHECK(activation_client_
);
1082 DCHECK(visibility_controller_
.get());
1083 DCHECK(drag_drop_controller_
.get());
1085 aura::client::SetFocusClient(root_window
, focus_client_
.get());
1086 input_method_filter_
->SetInputMethodPropertyInRootWindow(root_window
);
1087 aura::client::SetActivationClient(root_window
, activation_client_
);
1088 ::wm::FocusController
* focus_controller
=
1089 static_cast< ::wm::FocusController
*>(activation_client_
);
1090 root_window
->AddPreTargetHandler(focus_controller
);
1091 aura::client::SetVisibilityClient(root_window
, visibility_controller_
.get());
1092 aura::client::SetDragDropClient(root_window
, drag_drop_controller_
.get());
1093 aura::client::SetScreenPositionClient(root_window
,
1094 screen_position_controller_
.get());
1095 aura::client::SetCursorClient(root_window
, &cursor_manager_
);
1096 aura::client::SetTooltipClient(root_window
, tooltip_controller_
.get());
1097 aura::client::SetEventClient(root_window
, event_client_
.get());
1099 aura::client::SetWindowMoveClient(root_window
,
1100 toplevel_window_event_handler_
.get());
1101 root_window
->AddPreTargetHandler(toplevel_window_event_handler_
.get());
1102 root_window
->AddPostTargetHandler(toplevel_window_event_handler_
.get());
1104 if (nested_accelerator_controller_
) {
1105 aura::client::SetDispatcherClient(root_window
,
1106 nested_accelerator_controller_
.get());
1110 bool Shell::CanWindowReceiveEvents(aura::Window
* window
) {
1111 RootWindowControllerList controllers
= GetAllRootWindowControllers();
1112 for (RootWindowControllerList::iterator iter
= controllers
.begin();
1113 iter
!= controllers
.end(); ++iter
) {
1114 SystemModalContainerLayoutManager
* layout_manager
=
1115 (*iter
)->GetSystemModalLayoutManager(window
);
1116 if (layout_manager
&& layout_manager
->CanWindowReceiveEvents(window
))
1118 // Allow events to fall through to the virtual keyboard even if displaying
1119 // a system modal dialog.
1120 if ((*iter
)->IsVirtualKeyboardWindow(window
))
1127 ////////////////////////////////////////////////////////////////////////////////
1128 // Shell, ui::EventTarget overrides:
1130 bool Shell::CanAcceptEvent(const ui::Event
& event
) {
1134 ui::EventTarget
* Shell::GetParentTarget() {
1135 return aura::Env::GetInstance();
1138 scoped_ptr
<ui::EventTargetIterator
> Shell::GetChildIterator() const {
1139 return scoped_ptr
<ui::EventTargetIterator
>();
1142 ui::EventTargeter
* Shell::GetEventTargeter() {
1147 void Shell::OnEvent(ui::Event
* event
) {
1150 ////////////////////////////////////////////////////////////////////////////////
1151 // Shell, aura::client::ActivationChangeObserver implementation:
1153 void Shell::OnWindowActivated(aura::Window
* gained_active
,
1154 aura::Window
* lost_active
) {
1156 target_root_window_
= gained_active
->GetRootWindow();