1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
10 #include "ash/accelerators/accelerator_controller.h"
11 #include "ash/accelerators/accelerator_filter.h"
12 #include "ash/accelerators/focus_manager_factory.h"
13 #include "ash/accelerators/nested_dispatcher_controller.h"
14 #include "ash/ash_switches.h"
15 #include "ash/caps_lock_delegate.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/display_controller.h"
20 #include "ash/display/display_manager.h"
21 #include "ash/display/event_transformation_handler.h"
22 #include "ash/display/mouse_cursor_event_filter.h"
23 #include "ash/display/resolution_notification_controller.h"
24 #include "ash/display/screen_position_controller.h"
25 #include "ash/drag_drop/drag_drop_controller.h"
26 #include "ash/focus_cycler.h"
27 #include "ash/high_contrast/high_contrast_controller.h"
28 #include "ash/host/root_window_host_factory.h"
29 #include "ash/launcher/launcher_delegate.h"
30 #include "ash/launcher/launcher_item_delegate.h"
31 #include "ash/launcher/launcher_item_delegate_manager.h"
32 #include "ash/launcher/launcher_model.h"
33 #include "ash/magnifier/magnification_controller.h"
34 #include "ash/magnifier/partial_magnification_controller.h"
35 #include "ash/root_window_controller.h"
36 #include "ash/screen_ash.h"
37 #include "ash/session_state_delegate.h"
38 #include "ash/shelf/app_list_shelf_item_delegate.h"
39 #include "ash/shelf/shelf_layout_manager.h"
40 #include "ash/shelf/shelf_widget.h"
41 #include "ash/shell_delegate.h"
42 #include "ash/shell_factory.h"
43 #include "ash/shell_window_ids.h"
44 #include "ash/system/locale/locale_notification_controller.h"
45 #include "ash/system/status_area_widget.h"
46 #include "ash/system/tray/system_tray_delegate.h"
47 #include "ash/system/tray/system_tray_notifier.h"
48 #include "ash/wm/app_list_controller.h"
49 #include "ash/wm/ash_focus_rules.h"
50 #include "ash/wm/ash_native_cursor_manager.h"
51 #include "ash/wm/base_layout_manager.h"
52 #include "ash/wm/coordinate_conversion.h"
53 #include "ash/wm/custom_frame_view_ash.h"
54 #include "ash/wm/event_client_impl.h"
55 #include "ash/wm/event_rewriter_event_filter.h"
56 #include "ash/wm/lock_state_controller.h"
57 #include "ash/wm/lock_state_controller_impl2.h"
58 #include "ash/wm/mru_window_tracker.h"
59 #include "ash/wm/overlay_event_filter.h"
60 #include "ash/wm/overview/window_selector_controller.h"
61 #include "ash/wm/power_button_controller.h"
62 #include "ash/wm/resize_shadow_controller.h"
63 #include "ash/wm/root_window_layout_manager.h"
64 #include "ash/wm/screen_dimmer.h"
65 #include "ash/wm/session_state_controller_impl.h"
66 #include "ash/wm/system_gesture_event_filter.h"
67 #include "ash/wm/system_modal_container_event_filter.h"
68 #include "ash/wm/system_modal_container_layout_manager.h"
69 #include "ash/wm/user_activity_detector.h"
70 #include "ash/wm/video_detector.h"
71 #include "ash/wm/window_animations.h"
72 #include "ash/wm/window_cycle_controller.h"
73 #include "ash/wm/window_properties.h"
74 #include "ash/wm/window_util.h"
75 #include "ash/wm/workspace_controller.h"
76 #include "base/bind.h"
77 #include "base/command_line.h"
78 #include "base/debug/leak_annotations.h"
79 #include "base/debug/trace_event.h"
80 #include "ui/aura/client/aura_constants.h"
81 #include "ui/aura/client/user_action_client.h"
82 #include "ui/aura/env.h"
83 #include "ui/aura/focus_manager.h"
84 #include "ui/aura/layout_manager.h"
85 #include "ui/aura/root_window.h"
86 #include "ui/aura/window.h"
87 #include "ui/base/ui_base_switches.h"
88 #include "ui/compositor/layer.h"
89 #include "ui/compositor/layer_animator.h"
90 #include "ui/gfx/display.h"
91 #include "ui/gfx/image/image_skia.h"
92 #include "ui/gfx/screen.h"
93 #include "ui/gfx/size.h"
94 #include "ui/keyboard/keyboard.h"
95 #include "ui/keyboard/keyboard_util.h"
96 #include "ui/message_center/message_center.h"
97 #include "ui/views/corewm/compound_event_filter.h"
98 #include "ui/views/corewm/corewm_switches.h"
99 #include "ui/views/corewm/focus_controller.h"
100 #include "ui/views/corewm/input_method_event_filter.h"
101 #include "ui/views/corewm/shadow_controller.h"
102 #include "ui/views/corewm/tooltip_controller.h"
103 #include "ui/views/corewm/visibility_controller.h"
104 #include "ui/views/corewm/window_modality_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"
109 #if defined(OS_CHROMEOS)
111 #include "ash/ash_constants.h"
112 #include "ash/display/display_change_observer_chromeos.h"
113 #include "ash/display/display_error_observer_chromeos.h"
114 #include "ash/display/output_configurator_animation.h"
115 #include "base/chromeos/chromeos_version.h"
116 #include "base/message_loop/message_pump_x11.h"
117 #include "chromeos/display/output_configurator.h"
118 #include "content/public/browser/gpu_data_manager.h"
119 #include "gpu/config/gpu_feature_type.h"
120 #endif // defined(USE_X11)
121 #include "ash/system/chromeos/power/power_status.h"
122 #endif // defined(OS_CHROMEOS)
131 // This dummy class is used for shell unit tests. We dont have chrome delegate
133 class DummyUserWallpaperDelegate
: public UserWallpaperDelegate
{
135 DummyUserWallpaperDelegate() {}
137 virtual ~DummyUserWallpaperDelegate() {}
139 virtual int GetAnimationType() OVERRIDE
{
140 return views::corewm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE
;
143 virtual bool ShouldShowInitialAnimation() OVERRIDE
{
147 virtual void UpdateWallpaper() OVERRIDE
{
150 virtual void InitializeWallpaper() OVERRIDE
{
151 ash::Shell::GetInstance()->desktop_background_controller()->
152 CreateEmptyWallpaper();
155 virtual void OpenSetWallpaperPage() OVERRIDE
{
158 virtual bool CanOpenSetWallpaperPage() OVERRIDE
{
162 virtual void OnWallpaperAnimationFinished() OVERRIDE
{
165 virtual void OnWallpaperBootAnimationFinished() OVERRIDE
{
169 DISALLOW_COPY_AND_ASSIGN(DummyUserWallpaperDelegate
);
172 // A Corewm VisibilityController subclass that calls the Ash animation routine
173 // so we can pick up our extended animations. See ash/wm/window_animations.h.
174 class AshVisibilityController
: public views::corewm::VisibilityController
{
176 AshVisibilityController() {}
177 virtual ~AshVisibilityController() {}
180 // Overridden from views::corewm::VisibilityController:
181 virtual bool CallAnimateOnChildWindowVisibilityChanged(
182 aura::Window
* window
,
183 bool visible
) OVERRIDE
{
184 return AnimateOnChildWindowVisibilityChanged(window
, visible
);
187 DISALLOW_COPY_AND_ASSIGN(AshVisibilityController
);
193 Shell
* Shell::instance_
= NULL
;
195 bool Shell::initially_hide_cursor_
= false;
197 ////////////////////////////////////////////////////////////////////////////////
200 Shell::Shell(ShellDelegate
* delegate
)
201 : screen_(new ScreenAsh
),
202 target_root_window_(NULL
),
203 scoped_target_root_window_(NULL
),
205 activation_client_(NULL
),
206 #if defined(OS_CHROMEOS) && defined(USE_X11)
207 output_configurator_(new chromeos::OutputConfigurator()),
208 #endif // defined(OS_CHROMEOS)
209 native_cursor_manager_(new AshNativeCursorManager
),
210 cursor_manager_(scoped_ptr
<views::corewm::NativeCursorManager
>(
211 native_cursor_manager_
)),
212 browser_context_(NULL
),
213 simulate_modal_window_open_for_testing_(false),
214 is_touch_hud_projection_enabled_(false) {
215 DCHECK(delegate_
.get());
216 display_manager_
.reset(new internal::DisplayManager
);
218 ANNOTATE_LEAKING_OBJECT_PTR(screen_
); // see crbug.com/156466
219 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_ALTERNATE
, screen_
);
220 if (!gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE
))
221 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE
, screen_
);
222 display_controller_
.reset(new DisplayController
);
223 #if defined(OS_CHROMEOS) && defined(USE_X11)
224 bool is_panel_fitting_disabled
=
225 content::GpuDataManager::GetInstance()->IsFeatureBlacklisted(
226 gpu::GPU_FEATURE_TYPE_PANEL_FITTING
);
228 output_configurator_
->Init(!is_panel_fitting_disabled
);
230 base::MessagePumpX11::Current()->AddDispatcherForRootWindow(
231 output_configurator());
232 // We can't do this with a root window listener because XI_HierarchyChanged
233 // messages don't have a target window.
234 base::MessagePumpX11::Current()->AddObserver(output_configurator());
235 #endif // defined(OS_CHROMEOS)
236 AddPreTargetHandler(this);
238 #if defined(OS_CHROMEOS)
239 internal::PowerStatus::Initialize();
244 TRACE_EVENT0("shutdown", "ash::Shell::Destructor");
246 views::FocusManagerFactory::Install(NULL
);
248 // Remove the focus from any window. This will prevent overhead and side
249 // effects (e.g. crashes) from changing focus during shutdown.
250 // See bug crbug.com/134502.
251 aura::client::GetFocusClient(GetPrimaryRootWindow())->FocusWindow(NULL
);
253 // Please keep in same order as in Init() because it's easy to miss one.
254 RemovePreTargetHandler(event_rewriter_filter_
.get());
255 RemovePreTargetHandler(user_activity_detector_
.get());
256 RemovePreTargetHandler(overlay_filter_
.get());
257 RemovePreTargetHandler(input_method_filter_
.get());
258 RemovePreTargetHandler(window_modality_controller_
.get());
259 if (mouse_cursor_filter_
)
260 RemovePreTargetHandler(mouse_cursor_filter_
.get());
261 RemovePreTargetHandler(system_gesture_filter_
.get());
262 RemovePreTargetHandler(event_transformation_handler_
.get());
263 RemovePreTargetHandler(accelerator_filter_
.get());
265 // TooltipController is deleted with the Shell so removing its references.
266 RemovePreTargetHandler(tooltip_controller_
.get());
268 // AppList needs to be released before shelf layout manager, which is
269 // destroyed with launcher container in the loop below. However, app list
270 // container is now on top of launcher container and released after it.
271 // TODO(xiyuan): Move it back when app list container is no longer needed.
272 app_list_controller_
.reset();
274 // Destroy SystemTrayDelegate before destroying the status area(s).
275 system_tray_delegate_
->Shutdown();
276 system_tray_delegate_
.reset();
278 locale_notification_controller_
.reset();
280 // Drag-and-drop must be canceled prior to close all windows.
281 drag_drop_controller_
.reset();
283 // Destroy all child windows including widgets.
284 display_controller_
->CloseChildWindows();
286 // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems
287 // needs to remove observers from it.
288 system_tray_notifier_
.reset();
290 // These need a valid Shell instance to clean up properly, so explicitly
291 // delete them before invalidating the instance.
292 // Alphabetical. TODO(oshima): sort.
293 magnification_controller_
.reset();
294 partial_magnification_controller_
.reset();
295 resize_shadow_controller_
.reset();
296 shadow_controller_
.reset();
297 tooltip_controller_
.reset();
298 event_client_
.reset();
299 window_cycle_controller_
.reset();
300 nested_dispatcher_controller_
.reset();
301 user_action_client_
.reset();
302 visibility_controller_
.reset();
303 launcher_delegate_
.reset();
304 launcher_model_
.reset();
305 video_detector_
.reset();
307 power_button_controller_
.reset();
308 lock_state_controller_
.reset();
309 mru_window_tracker_
.reset();
311 resolution_notification_controller_
.reset();
313 // This also deletes all RootWindows. Note that we invoke Shutdown() on
314 // DisplayController before resetting |display_controller_|, since destruction
315 // of its owned RootWindowControllers relies on the value.
316 display_controller_
->Shutdown();
317 display_controller_
.reset();
318 screen_position_controller_
.reset();
320 #if defined(OS_CHROMEOS) && defined(USE_X11)
321 if (display_change_observer_
)
322 output_configurator_
->RemoveObserver(display_change_observer_
.get());
323 if (output_configurator_animation_
)
324 output_configurator_
->RemoveObserver(output_configurator_animation_
.get());
325 if (display_error_observer_
)
326 output_configurator_
->RemoveObserver(display_error_observer_
.get());
327 base::MessagePumpX11::Current()->RemoveDispatcherForRootWindow(
328 output_configurator());
329 base::MessagePumpX11::Current()->RemoveObserver(output_configurator());
330 display_change_observer_
.reset();
331 #endif // defined(OS_CHROMEOS)
333 #if defined(OS_CHROMEOS)
334 internal::PowerStatus::Shutdown();
337 DCHECK(instance_
== this);
342 Shell
* Shell::CreateInstance(ShellDelegate
* delegate
) {
344 instance_
= new Shell(delegate
);
350 Shell
* Shell::GetInstance() {
356 bool Shell::HasInstance() {
361 void Shell::DeleteInstance() {
367 internal::RootWindowController
* Shell::GetPrimaryRootWindowController() {
368 return internal::GetRootWindowController(GetPrimaryRootWindow());
372 Shell::RootWindowControllerList
Shell::GetAllRootWindowControllers() {
373 return Shell::GetInstance()->display_controller()->
374 GetAllRootWindowControllers();
378 aura::RootWindow
* Shell::GetPrimaryRootWindow() {
379 return GetInstance()->display_controller()->GetPrimaryRootWindow();
383 aura::RootWindow
* Shell::GetTargetRootWindow() {
384 Shell
* shell
= GetInstance();
385 if (shell
->scoped_target_root_window_
)
386 return shell
->scoped_target_root_window_
;
387 return shell
->target_root_window_
;
391 gfx::Screen
* Shell::GetScreen() {
392 return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE
);
396 Shell::RootWindowList
Shell::GetAllRootWindows() {
397 return Shell::GetInstance()->display_controller()->
402 aura::Window
* Shell::GetContainer(aura::RootWindow
* root_window
,
404 return root_window
->GetChildById(container_id
);
408 const aura::Window
* Shell::GetContainer(const aura::RootWindow
* root_window
,
410 return root_window
->GetChildById(container_id
);
414 std::vector
<aura::Window
*> Shell::GetContainersFromAllRootWindows(
416 aura::RootWindow
* priority_root
) {
417 std::vector
<aura::Window
*> containers
;
418 RootWindowList root_windows
= GetAllRootWindows();
419 for (RootWindowList::const_iterator it
= root_windows
.begin();
420 it
!= root_windows
.end(); ++it
) {
421 aura::Window
* container
= (*it
)->GetChildById(container_id
);
423 if (priority_root
&& priority_root
->Contains(container
))
424 containers
.insert(containers
.begin(), container
);
426 containers
.push_back(container
);
433 bool Shell::IsForcedMaximizeMode() {
434 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
435 return command_line
->HasSwitch(switches::kForcedMaximizeMode
);
439 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
441 delegate_
->PreInit();
442 bool display_initialized
= false;
443 #if defined(OS_CHROMEOS) && defined(USE_X11)
444 output_configurator_animation_
.reset(
445 new internal::OutputConfiguratorAnimation());
446 output_configurator_
->AddObserver(output_configurator_animation_
.get());
447 if (base::chromeos::IsRunningOnChromeOS()) {
448 display_change_observer_
.reset(new internal::DisplayChangeObserver
);
449 // Register |display_change_observer_| first so that the rest of
450 // observer gets invoked after the root windows are configured.
451 output_configurator_
->AddObserver(display_change_observer_
.get());
452 display_error_observer_
.reset(new internal::DisplayErrorObserver());
453 output_configurator_
->AddObserver(display_error_observer_
.get());
454 output_configurator_
->set_state_controller(display_change_observer_
.get());
455 if (!command_line
->HasSwitch(ash::switches::kAshDisableSoftwareMirroring
))
456 output_configurator_
->set_mirroring_controller(display_manager_
.get());
457 output_configurator_
->Start(
458 delegate_
->IsFirstRunAfterBoot() ? kChromeOsBootColor
: 0);
459 display_initialized
= true;
461 #endif // defined(OS_CHROMEOS) && defined(USE_X11)
462 if (!display_initialized
)
463 display_manager_
->InitFromCommandLine();
465 // Install the custom factory first so that views::FocusManagers for Tray,
466 // Launcher, and WallPaper could be created by the factory.
467 views::FocusManagerFactory::Install(new AshFocusManagerFactory
);
469 env_filter_
.reset(new views::corewm::CompoundEventFilter
);
470 AddPreTargetHandler(env_filter_
.get());
472 // Env creates the compositor. Historically it seems to have been implicitly
473 // initialized first by the ActivationController, but now that FocusController
474 // no longer does this we need to do it explicitly.
475 aura::Env::GetInstance();
476 views::corewm::FocusController
* focus_controller
=
477 new views::corewm::FocusController(new wm::AshFocusRules
);
478 focus_client_
.reset(focus_controller
);
479 activation_client_
= focus_controller
;
480 activation_client_
->AddObserver(this);
481 focus_cycler_
.reset(new internal::FocusCycler());
483 screen_position_controller_
.reset(new internal::ScreenPositionController
);
484 root_window_host_factory_
.reset(delegate_
->CreateRootWindowHostFactory());
486 display_controller_
->Start();
487 display_controller_
->InitPrimaryDisplay();
488 aura::RootWindow
* root_window
= display_controller_
->GetPrimaryRootWindow();
489 target_root_window_
= root_window
;
491 resolution_notification_controller_
.reset(
492 new internal::ResolutionNotificationController
);
494 cursor_manager_
.SetDisplay(DisplayController::GetPrimaryDisplay());
496 nested_dispatcher_controller_
.reset(new NestedDispatcherController
);
497 accelerator_controller_
.reset(new AcceleratorController
);
499 // The order in which event filters are added is significant.
500 event_rewriter_filter_
.reset(new internal::EventRewriterEventFilter
);
501 AddPreTargetHandler(event_rewriter_filter_
.get());
503 // UserActivityDetector passes events to observers, so let them get
505 user_activity_detector_
.reset(new UserActivityDetector
);
506 AddPreTargetHandler(user_activity_detector_
.get());
508 overlay_filter_
.reset(new internal::OverlayEventFilter
);
509 AddPreTargetHandler(overlay_filter_
.get());
510 AddShellObserver(overlay_filter_
.get());
512 input_method_filter_
.reset(new views::corewm::InputMethodEventFilter(
513 root_window
->GetAcceleratedWidget()));
514 AddPreTargetHandler(input_method_filter_
.get());
516 accelerator_filter_
.reset(new internal::AcceleratorFilter
);
517 AddPreTargetHandler(accelerator_filter_
.get());
519 event_transformation_handler_
.reset(new internal::EventTransformationHandler
);
520 AddPreTargetHandler(event_transformation_handler_
.get());
522 system_gesture_filter_
.reset(new internal::SystemGestureEventFilter
);
523 AddPreTargetHandler(system_gesture_filter_
.get());
525 // The keyboard system must be initialized before the RootWindowController is
527 if (keyboard::IsKeyboardEnabled())
528 keyboard::InitializeKeyboard();
530 if (command_line
->HasSwitch(ash::switches::kAshDisableNewLockAnimations
))
531 lock_state_controller_
.reset(new SessionStateControllerImpl
);
533 lock_state_controller_
.reset(new LockStateControllerImpl2
);
534 power_button_controller_
.reset(new PowerButtonController(
535 lock_state_controller_
.get()));
536 AddShellObserver(lock_state_controller_
.get());
538 drag_drop_controller_
.reset(new internal::DragDropController
);
539 mouse_cursor_filter_
.reset(new internal::MouseCursorEventFilter());
540 PrependPreTargetHandler(mouse_cursor_filter_
.get());
542 // Create Controllers that may need root window.
543 // TODO(oshima): Move as many controllers before creating
544 // RootWindowController as possible.
545 visibility_controller_
.reset(new AshVisibilityController
);
546 user_action_client_
.reset(delegate_
->CreateUserActionClient());
547 window_modality_controller_
.reset(
548 new views::corewm::WindowModalityController
);
549 AddPreTargetHandler(window_modality_controller_
.get());
551 magnification_controller_
.reset(
552 MagnificationController::CreateInstance());
553 mru_window_tracker_
.reset(new MruWindowTracker(activation_client_
));
555 partial_magnification_controller_
.reset(
556 new PartialMagnificationController());
558 high_contrast_controller_
.reset(new HighContrastController
);
559 video_detector_
.reset(new VideoDetector
);
560 window_cycle_controller_
.reset(new WindowCycleController());
561 window_selector_controller_
.reset(new WindowSelectorController());
563 tooltip_controller_
.reset(new views::corewm::TooltipController(
564 gfx::SCREEN_TYPE_ALTERNATE
));
565 AddPreTargetHandler(tooltip_controller_
.get());
567 event_client_
.reset(new internal::EventClientImpl
);
569 // This controller needs to be set before SetupManagedWindowMode.
570 desktop_background_controller_
.reset(new DesktopBackgroundController());
571 user_wallpaper_delegate_
.reset(delegate_
->CreateUserWallpaperDelegate());
572 if (!user_wallpaper_delegate_
)
573 user_wallpaper_delegate_
.reset(new DummyUserWallpaperDelegate());
575 // StatusAreaWidget uses Shell's CapsLockDelegate.
576 caps_lock_delegate_
.reset(delegate_
->CreateCapsLockDelegate());
578 session_state_delegate_
.reset(delegate_
->CreateSessionStateDelegate());
580 if (!command_line
->HasSwitch(views::corewm::switches::kNoDropShadows
)) {
581 resize_shadow_controller_
.reset(new internal::ResizeShadowController());
582 shadow_controller_
.reset(
583 new views::corewm::ShadowController(activation_client_
));
586 // Create system_tray_notifier_ before the delegate.
587 system_tray_notifier_
.reset(new ash::SystemTrayNotifier());
589 // Initialize system_tray_delegate_ before initializing StatusAreaWidget.
590 system_tray_delegate_
.reset(delegate()->CreateSystemTrayDelegate());
591 DCHECK(system_tray_delegate_
.get());
593 internal::RootWindowController
* root_window_controller
=
594 new internal::RootWindowController(root_window
);
595 InitRootWindowController(root_window_controller
,
596 delegate_
->IsFirstRunAfterBoot());
598 locale_notification_controller_
.reset(
599 new internal::LocaleNotificationController
);
601 // Initialize system_tray_delegate_ after StatusAreaWidget is created.
602 system_tray_delegate_
->Initialize();
604 display_controller_
->InitSecondaryDisplays();
607 root_window_controller
->root_window_layout()->OnWindowResized();
609 // It needs to be created after OnWindowResized has been called, otherwise the
610 // widget will not paint when restoring after a browser crash. Also it needs
611 // to be created after InitSecondaryDisplays() to initialize the wallpapers in
613 user_wallpaper_delegate_
->InitializeWallpaper();
615 if (initially_hide_cursor_
)
616 cursor_manager_
.HideCursor();
617 cursor_manager_
.SetCursor(ui::kCursorPointer
);
619 if (!cursor_manager_
.IsCursorVisible()) {
620 // Cursor might have been hidden by something other than chrome.
621 // Let the first mouse event show the cursor.
622 env_filter_
->set_cursor_hidden_by_filter(true);
625 // The compositor thread and main message loop have to be running in
626 // order to create mirror window. Run it after the main message loop
628 base::MessageLoopForUI::current()->PostTask(
630 base::Bind(&internal::DisplayManager::CreateMirrorWindowIfAny
,
631 base::Unretained(display_manager_
.get())));
634 void Shell::ShowContextMenu(const gfx::Point
& location_in_screen
,
635 ui::MenuSourceType source_type
) {
636 // No context menus if there is no session with an active user.
637 if (!session_state_delegate_
->NumberOfLoggedInUsers())
639 // No context menus when screen is locked.
640 if (session_state_delegate_
->IsScreenLocked())
643 aura::RootWindow
* root
=
644 wm::GetRootWindowMatching(gfx::Rect(location_in_screen
, gfx::Size()));
645 // TODO(oshima): The root and root window controller shouldn't be
646 // NULL even for the out-of-bounds |location_in_screen| (It should
647 // return the primary root). Investigate why/how this is
648 // happening. crbug.com/165214.
649 internal::RootWindowController
* rwc
= internal::GetRootWindowController(root
);
650 CHECK(rwc
) << "root=" << root
651 << ", location:" << location_in_screen
.ToString();
653 rwc
->ShowContextMenu(location_in_screen
, source_type
);
656 void Shell::ToggleAppList(aura::Window
* window
) {
657 // If the context window is not given, show it on the target root window.
659 window
= GetTargetRootWindow();
660 if (!app_list_controller_
)
661 app_list_controller_
.reset(new internal::AppListController
);
662 app_list_controller_
->SetVisible(!app_list_controller_
->IsVisible(), window
);
665 bool Shell::GetAppListTargetVisibility() const {
666 return app_list_controller_
.get() &&
667 app_list_controller_
->GetTargetVisibility();
670 aura::Window
* Shell::GetAppListWindow() {
671 return app_list_controller_
.get() ? app_list_controller_
->GetWindow() : NULL
;
674 bool Shell::IsSystemModalWindowOpen() const {
675 if (simulate_modal_window_open_for_testing_
)
677 const std::vector
<aura::Window
*> containers
= GetContainersFromAllRootWindows(
678 internal::kShellWindowId_SystemModalContainer
, NULL
);
679 for (std::vector
<aura::Window
*>::const_iterator cit
= containers
.begin();
680 cit
!= containers
.end(); ++cit
) {
681 for (aura::Window::Windows::const_iterator wit
= (*cit
)->children().begin();
682 wit
!= (*cit
)->children().end(); ++wit
) {
683 if ((*wit
)->GetProperty(aura::client::kModalKey
) ==
684 ui::MODAL_TYPE_SYSTEM
&& (*wit
)->TargetVisibility()) {
692 views::NonClientFrameView
* Shell::CreateDefaultNonClientFrameView(
693 views::Widget
* widget
) {
694 // Use translucent-style window frames for dialogs.
695 CustomFrameViewAsh
* frame_view
= new CustomFrameViewAsh
;
696 frame_view
->Init(widget
);
700 void Shell::RotateFocus(Direction direction
) {
701 focus_cycler_
->RotateFocus(
702 direction
== FORWARD
? internal::FocusCycler::FORWARD
:
703 internal::FocusCycler::BACKWARD
);
706 void Shell::SetDisplayWorkAreaInsets(Window
* contains
,
707 const gfx::Insets
& insets
) {
708 if (!display_controller_
->UpdateWorkAreaOfDisplayNearestWindow(
712 FOR_EACH_OBSERVER(ShellObserver
, observers_
,
713 OnDisplayWorkAreaInsetsChanged());
716 void Shell::OnLoginStateChanged(user::LoginStatus status
) {
717 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnLoginStateChanged(status
));
720 void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status
) {
721 RootWindowControllerList controllers
= GetAllRootWindowControllers();
722 for (RootWindowControllerList::iterator iter
= controllers
.begin();
723 iter
!= controllers
.end(); ++iter
)
724 (*iter
)->UpdateAfterLoginStatusChange(status
);
727 void Shell::OnAppTerminating() {
728 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnAppTerminating());
731 void Shell::OnLockStateChanged(bool locked
) {
732 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnLockStateChanged(locked
));
734 // Make sure that there is no system modal in Lock layer when unlocked.
736 std::vector
<aura::Window
*> containers
= GetContainersFromAllRootWindows(
737 internal::kShellWindowId_LockSystemModalContainer
,
738 GetPrimaryRootWindow());
739 for (std::vector
<aura::Window
*>::const_iterator iter
= containers
.begin();
740 iter
!= containers
.end(); ++iter
) {
741 DCHECK_EQ(0u, (*iter
)->children().size());
747 void Shell::CreateLauncher() {
748 RootWindowControllerList controllers
= GetAllRootWindowControllers();
749 for (RootWindowControllerList::iterator iter
= controllers
.begin();
750 iter
!= controllers
.end(); ++iter
)
751 (*iter
)->shelf()->CreateLauncher();
754 void Shell::ShowLauncher() {
755 RootWindowControllerList controllers
= GetAllRootWindowControllers();
756 for (RootWindowControllerList::iterator iter
= controllers
.begin();
757 iter
!= controllers
.end(); ++iter
)
758 (*iter
)->ShowLauncher();
761 void Shell::AddShellObserver(ShellObserver
* observer
) {
762 observers_
.AddObserver(observer
);
765 void Shell::RemoveShellObserver(ShellObserver
* observer
) {
766 observers_
.RemoveObserver(observer
);
769 void Shell::UpdateShelfVisibility() {
770 RootWindowControllerList controllers
= GetAllRootWindowControllers();
771 for (RootWindowControllerList::iterator iter
= controllers
.begin();
772 iter
!= controllers
.end(); ++iter
)
773 if ((*iter
)->shelf())
774 (*iter
)->UpdateShelfVisibility();
777 void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior
,
778 aura::RootWindow
* root_window
) {
779 ash::internal::ShelfLayoutManager::ForLauncher(root_window
)->
780 SetAutoHideBehavior(behavior
);
783 ShelfAutoHideBehavior
Shell::GetShelfAutoHideBehavior(
784 aura::RootWindow
* root_window
) const {
785 return ash::internal::ShelfLayoutManager::ForLauncher(root_window
)->
786 auto_hide_behavior();
789 void Shell::SetShelfAlignment(ShelfAlignment alignment
,
790 aura::RootWindow
* root_window
) {
791 if (ash::internal::ShelfLayoutManager::ForLauncher(root_window
)->
792 SetAlignment(alignment
)) {
794 ShellObserver
, observers_
, OnShelfAlignmentChanged(root_window
));
798 ShelfAlignment
Shell::GetShelfAlignment(aura::RootWindow
* root_window
) {
799 return internal::GetRootWindowController(root_window
)->
800 GetShelfLayoutManager()->GetAlignment();
803 void Shell::SetDimming(bool should_dim
) {
804 RootWindowControllerList controllers
= GetAllRootWindowControllers();
805 for (RootWindowControllerList::iterator iter
= controllers
.begin();
806 iter
!= controllers
.end(); ++iter
)
807 (*iter
)->screen_dimmer()->SetDimming(should_dim
);
810 void Shell::CreateModalBackground(aura::Window
* window
) {
811 if (!modality_filter_
) {
812 modality_filter_
.reset(new internal::SystemModalContainerEventFilter(this));
813 AddPreTargetHandler(modality_filter_
.get());
815 RootWindowControllerList controllers
= GetAllRootWindowControllers();
816 for (RootWindowControllerList::iterator iter
= controllers
.begin();
817 iter
!= controllers
.end(); ++iter
)
818 (*iter
)->GetSystemModalLayoutManager(window
)->CreateModalBackground();
821 void Shell::OnModalWindowRemoved(aura::Window
* removed
) {
822 RootWindowControllerList controllers
= GetAllRootWindowControllers();
823 bool activated
= false;
824 for (RootWindowControllerList::iterator iter
= controllers
.begin();
825 iter
!= controllers
.end() && !activated
; ++iter
) {
826 activated
= (*iter
)->GetSystemModalLayoutManager(removed
)->
827 ActivateNextModalWindow();
830 RemovePreTargetHandler(modality_filter_
.get());
831 modality_filter_
.reset();
832 for (RootWindowControllerList::iterator iter
= controllers
.begin();
833 iter
!= controllers
.end(); ++iter
)
834 (*iter
)->GetSystemModalLayoutManager(removed
)->DestroyModalBackground();
838 WebNotificationTray
* Shell::GetWebNotificationTray() {
839 return GetPrimaryRootWindowController()->shelf()->
840 status_area_widget()->web_notification_tray();
843 bool Shell::HasPrimaryStatusArea() {
844 ShelfWidget
* shelf
= GetPrimaryRootWindowController()->shelf();
845 return shelf
&& shelf
->status_area_widget();
848 SystemTray
* Shell::GetPrimarySystemTray() {
849 return GetPrimaryRootWindowController()->GetSystemTray();
852 LauncherDelegate
* Shell::GetLauncherDelegate() {
853 if (!launcher_delegate_
) {
854 // Creates LauncherItemDelegateManager before LauncherDelegate.
855 launcher_item_delegate_manager_
.reset(new LauncherItemDelegateManager
);
856 launcher_model_
.reset(new LauncherModel
);
857 launcher_delegate_
.reset(
858 delegate_
->CreateLauncherDelegate(launcher_model_
.get()));
859 app_list_shelf_item_delegate_
.reset(
860 new internal::AppListShelfItemDelegate
);
862 return launcher_delegate_
.get();
865 void Shell::SetTouchHudProjectionEnabled(bool enabled
) {
866 if (is_touch_hud_projection_enabled_
== enabled
)
869 is_touch_hud_projection_enabled_
= enabled
;
870 FOR_EACH_OBSERVER(ShellObserver
, observers_
,
871 OnTouchHudProjectionToggled(enabled
));
874 void Shell::InitRootWindowForSecondaryDisplay(aura::RootWindow
* root
) {
875 internal::RootWindowController
* controller
=
876 new internal::RootWindowController(root
);
877 // Pass false for the |is_first_run_after_boot| parameter so we'll show a
878 // black background on this display instead of trying to mimic the boot splash
880 InitRootWindowController(controller
, false);
882 controller
->root_window_layout()->OnWindowResized();
883 desktop_background_controller_
->OnRootWindowAdded(root
);
884 high_contrast_controller_
->OnRootWindowAdded(root
);
885 root
->ShowRootWindow();
886 // Activate new root for testing.
887 // TODO(oshima): remove this.
888 target_root_window_
= root
;
890 // Create a launcher if a user is already logged.
891 if (Shell::GetInstance()->session_state_delegate()->NumberOfLoggedInUsers())
892 controller
->shelf()->CreateLauncher();
895 void Shell::DoInitialWorkspaceAnimation() {
896 return GetPrimaryRootWindowController()->workspace_controller()->
897 DoInitialAnimation();
900 void Shell::InitRootWindowController(
901 internal::RootWindowController
* controller
,
902 bool first_run_after_boot
) {
904 aura::RootWindow
* root_window
= controller
->root_window();
905 DCHECK(activation_client_
);
906 DCHECK(visibility_controller_
.get());
907 DCHECK(drag_drop_controller_
.get());
908 DCHECK(window_cycle_controller_
.get());
910 aura::client::SetFocusClient(root_window
, focus_client_
.get());
911 input_method_filter_
->SetInputMethodPropertyInRootWindow(root_window
);
912 aura::client::SetActivationClient(root_window
, activation_client_
);
913 views::corewm::FocusController
* focus_controller
=
914 static_cast<views::corewm::FocusController
*>(activation_client_
);
915 root_window
->AddPreTargetHandler(focus_controller
);
916 aura::client::SetVisibilityClient(root_window
, visibility_controller_
.get());
917 aura::client::SetDragDropClient(root_window
, drag_drop_controller_
.get());
918 aura::client::SetScreenPositionClient(root_window
,
919 screen_position_controller_
.get());
920 aura::client::SetCursorClient(root_window
, &cursor_manager_
);
921 aura::client::SetTooltipClient(root_window
, tooltip_controller_
.get());
922 aura::client::SetEventClient(root_window
, event_client_
.get());
924 if (nested_dispatcher_controller_
) {
925 aura::client::SetDispatcherClient(root_window
,
926 nested_dispatcher_controller_
.get());
928 if (user_action_client_
)
929 aura::client::SetUserActionClient(root_window
, user_action_client_
.get());
931 controller
->Init(first_run_after_boot
);
934 ////////////////////////////////////////////////////////////////////////////////
937 bool Shell::CanWindowReceiveEvents(aura::Window
* window
) {
938 RootWindowControllerList controllers
= GetAllRootWindowControllers();
939 for (RootWindowControllerList::iterator iter
= controllers
.begin();
940 iter
!= controllers
.end(); ++iter
) {
941 internal::SystemModalContainerLayoutManager
* layout_manager
=
942 (*iter
)->GetSystemModalLayoutManager(window
);
943 if (layout_manager
&& layout_manager
->CanWindowReceiveEvents(window
))
949 ////////////////////////////////////////////////////////////////////////////////
950 // Shell, ui::EventTarget overrides:
952 bool Shell::CanAcceptEvent(const ui::Event
& event
) {
956 ui::EventTarget
* Shell::GetParentTarget() {
960 void Shell::OnEvent(ui::Event
* event
) {
963 ////////////////////////////////////////////////////////////////////////////////
964 // Shell, aura::client::ActivationChangeObserver implementation:
966 void Shell::OnWindowActivated(aura::Window
* gained_active
,
967 aura::Window
* lost_active
) {
969 target_root_window_
= gained_active
->GetRootWindow();