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/focus_manager_factory.h"
11 #include "ash/ash_switches.h"
12 #include "ash/caps_lock_delegate.h"
13 #include "ash/desktop_background/desktop_background_controller.h"
14 #include "ash/desktop_background/desktop_background_view.h"
15 #include "ash/desktop_background/user_wallpaper_delegate.h"
16 #include "ash/display/display_controller.h"
17 #include "ash/display/display_manager.h"
18 #include "ash/display/event_transformation_handler.h"
19 #include "ash/display/mouse_cursor_event_filter.h"
20 #include "ash/display/resolution_notification_controller.h"
21 #include "ash/display/screen_position_controller.h"
22 #include "ash/drag_drop/drag_drop_controller.h"
23 #include "ash/focus_cycler.h"
24 #include "ash/high_contrast/high_contrast_controller.h"
25 #include "ash/host/root_window_host_factory.h"
26 #include "ash/launcher/launcher_delegate.h"
27 #include "ash/launcher/launcher_model.h"
28 #include "ash/magnifier/magnification_controller.h"
29 #include "ash/magnifier/partial_magnification_controller.h"
30 #include "ash/root_window_controller.h"
31 #include "ash/screen_ash.h"
32 #include "ash/session_state_delegate.h"
33 #include "ash/shelf/shelf_layout_manager.h"
34 #include "ash/shelf/shelf_widget.h"
35 #include "ash/shell_delegate.h"
36 #include "ash/shell_factory.h"
37 #include "ash/shell_window_ids.h"
38 #include "ash/system/locale/locale_notification_controller.h"
39 #include "ash/system/status_area_widget.h"
40 #include "ash/system/tray/system_tray_delegate.h"
41 #include "ash/system/tray/system_tray_notifier.h"
42 #include "ash/wm/activation_controller.h"
43 #include "ash/wm/app_list_controller.h"
44 #include "ash/wm/ash_activation_controller.h"
45 #include "ash/wm/ash_focus_rules.h"
46 #include "ash/wm/ash_native_cursor_manager.h"
47 #include "ash/wm/base_layout_manager.h"
48 #include "ash/wm/coordinate_conversion.h"
49 #include "ash/wm/custom_frame_view_ash.h"
50 #include "ash/wm/event_client_impl.h"
51 #include "ash/wm/event_rewriter_event_filter.h"
52 #include "ash/wm/lock_state_controller.h"
53 #include "ash/wm/lock_state_controller_impl2.h"
54 #include "ash/wm/mru_window_tracker.h"
55 #include "ash/wm/overlay_event_filter.h"
56 #include "ash/wm/overview/window_selector_controller.h"
57 #include "ash/wm/power_button_controller.h"
58 #include "ash/wm/property_util.h"
59 #include "ash/wm/resize_shadow_controller.h"
60 #include "ash/wm/root_window_layout_manager.h"
61 #include "ash/wm/screen_dimmer.h"
62 #include "ash/wm/session_state_controller_impl.h"
63 #include "ash/wm/system_gesture_event_filter.h"
64 #include "ash/wm/system_modal_container_event_filter.h"
65 #include "ash/wm/system_modal_container_layout_manager.h"
66 #include "ash/wm/user_activity_detector.h"
67 #include "ash/wm/video_detector.h"
68 #include "ash/wm/window_animations.h"
69 #include "ash/wm/window_cycle_controller.h"
70 #include "ash/wm/window_properties.h"
71 #include "ash/wm/window_util.h"
72 #include "ash/wm/workspace_controller.h"
73 #include "base/bind.h"
74 #include "base/command_line.h"
75 #include "base/debug/leak_annotations.h"
76 #include "ui/aura/client/aura_constants.h"
77 #include "ui/aura/client/user_action_client.h"
78 #include "ui/aura/env.h"
79 #include "ui/aura/focus_manager.h"
80 #include "ui/aura/layout_manager.h"
81 #include "ui/aura/root_window.h"
82 #include "ui/aura/window.h"
83 #include "ui/base/ui_base_switches.h"
84 #include "ui/compositor/layer.h"
85 #include "ui/compositor/layer_animator.h"
86 #include "ui/gfx/display.h"
87 #include "ui/gfx/image/image_skia.h"
88 #include "ui/gfx/screen.h"
89 #include "ui/gfx/size.h"
90 #include "ui/keyboard/keyboard.h"
91 #include "ui/keyboard/keyboard_util.h"
92 #include "ui/message_center/message_center.h"
93 #include "ui/views/corewm/compound_event_filter.h"
94 #include "ui/views/corewm/corewm_switches.h"
95 #include "ui/views/corewm/focus_controller.h"
96 #include "ui/views/corewm/input_method_event_filter.h"
97 #include "ui/views/corewm/shadow_controller.h"
98 #include "ui/views/corewm/tooltip_controller.h"
99 #include "ui/views/corewm/visibility_controller.h"
100 #include "ui/views/corewm/window_modality_controller.h"
101 #include "ui/views/focus/focus_manager_factory.h"
102 #include "ui/views/widget/native_widget_aura.h"
103 #include "ui/views/widget/widget.h"
105 #if !defined(OS_MACOSX)
106 #include "ash/accelerators/accelerator_controller.h"
107 #include "ash/accelerators/accelerator_filter.h"
108 #include "ash/accelerators/nested_dispatcher_controller.h"
111 #if defined(OS_CHROMEOS)
113 #include "ash/ash_constants.h"
114 #include "ash/display/display_change_observer_chromeos.h"
115 #include "ash/display/display_error_observer_chromeos.h"
116 #include "ash/display/output_configurator_animation.h"
117 #include "base/chromeos/chromeos_version.h"
118 #include "base/message_loop/message_pump_aurax11.h"
119 #include "chromeos/display/output_configurator.h"
120 #include "content/public/browser/gpu_data_manager.h"
121 #include "gpu/config/gpu_feature_type.h"
122 #endif // defined(USE_X11)
123 #include "ash/system/chromeos/power/power_status.h"
124 #endif // defined(OS_CHROMEOS)
133 // This dummy class is used for shell unit tests. We dont have chrome delegate
135 class DummyUserWallpaperDelegate
: public UserWallpaperDelegate
{
137 DummyUserWallpaperDelegate() {}
139 virtual ~DummyUserWallpaperDelegate() {}
141 virtual int GetAnimationType() OVERRIDE
{
142 return views::corewm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE
;
145 virtual bool ShouldShowInitialAnimation() OVERRIDE
{
149 virtual void UpdateWallpaper() OVERRIDE
{
152 virtual void InitializeWallpaper() OVERRIDE
{
153 ash::Shell::GetInstance()->desktop_background_controller()->
154 CreateEmptyWallpaper();
157 virtual void OpenSetWallpaperPage() OVERRIDE
{
160 virtual bool CanOpenSetWallpaperPage() OVERRIDE
{
164 virtual void OnWallpaperAnimationFinished() OVERRIDE
{
167 virtual void OnWallpaperBootAnimationFinished() OVERRIDE
{
171 DISALLOW_COPY_AND_ASSIGN(DummyUserWallpaperDelegate
);
174 // A Corewm VisibilityController subclass that calls the Ash animation routine
175 // so we can pick up our extended animations. See ash/wm/window_animations.h.
176 class AshVisibilityController
: public views::corewm::VisibilityController
{
178 AshVisibilityController() {}
179 virtual ~AshVisibilityController() {}
182 // Overridden from views::corewm::VisibilityController:
183 virtual bool CallAnimateOnChildWindowVisibilityChanged(
184 aura::Window
* window
,
185 bool visible
) OVERRIDE
{
186 return AnimateOnChildWindowVisibilityChanged(window
, visible
);
189 DISALLOW_COPY_AND_ASSIGN(AshVisibilityController
);
195 Shell
* Shell::instance_
= NULL
;
197 bool Shell::initially_hide_cursor_
= false;
199 ////////////////////////////////////////////////////////////////////////////////
202 Shell::Shell(ShellDelegate
* delegate
)
203 : screen_(new ScreenAsh
),
204 target_root_window_(NULL
),
205 scoped_target_root_window_(NULL
),
207 activation_client_(NULL
),
208 #if defined(OS_CHROMEOS) && defined(USE_X11)
209 output_configurator_(new chromeos::OutputConfigurator()),
210 #endif // defined(OS_CHROMEOS)
211 native_cursor_manager_(new AshNativeCursorManager
),
212 cursor_manager_(scoped_ptr
<views::corewm::NativeCursorManager
>(
213 native_cursor_manager_
)),
214 browser_context_(NULL
),
215 simulate_modal_window_open_for_testing_(false),
216 is_touch_hud_projection_enabled_(false) {
217 DCHECK(delegate_
.get());
218 display_manager_
.reset(new internal::DisplayManager
);
220 ANNOTATE_LEAKING_OBJECT_PTR(screen_
); // see crbug.com/156466
221 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_ALTERNATE
, screen_
);
222 if (!gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE
))
223 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE
, screen_
);
224 display_controller_
.reset(new DisplayController
);
225 #if defined(OS_CHROMEOS) && defined(USE_X11)
226 bool is_panel_fitting_disabled
=
227 content::GpuDataManager::GetInstance()->IsFeatureBlacklisted(
228 gpu::GPU_FEATURE_TYPE_PANEL_FITTING
);
230 output_configurator_
->Init(!is_panel_fitting_disabled
);
232 base::MessagePumpAuraX11::Current()->AddDispatcherForRootWindow(
233 output_configurator());
234 // We can't do this with a root window listener because XI_HierarchyChanged
235 // messages don't have a target window.
236 base::MessagePumpAuraX11::Current()->AddObserver(output_configurator());
237 #endif // defined(OS_CHROMEOS)
238 AddPreTargetHandler(this);
240 #if defined(OS_CHROMEOS)
241 internal::PowerStatus::Initialize();
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 #if !defined(OS_MACOSX)
264 RemovePreTargetHandler(accelerator_filter_
.get());
267 // TooltipController is deleted with the Shell so removing its references.
268 RemovePreTargetHandler(tooltip_controller_
.get());
270 // AppList needs to be released before shelf layout manager, which is
271 // destroyed with launcher container in the loop below. However, app list
272 // container is now on top of launcher container and released after it.
273 // TODO(xiyuan): Move it back when app list container is no longer needed.
274 app_list_controller_
.reset();
276 // Destroy SystemTrayDelegate before destroying the status area(s).
277 system_tray_delegate_
->Shutdown();
278 system_tray_delegate_
.reset();
280 locale_notification_controller_
.reset();
282 // Drag-and-drop must be canceled prior to close all windows.
283 drag_drop_controller_
.reset();
285 // Destroy all child windows including widgets.
286 display_controller_
->CloseChildWindows();
288 // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems
289 // needs to remove observers from it.
290 system_tray_notifier_
.reset();
292 // These need a valid Shell instance to clean up properly, so explicitly
293 // delete them before invalidating the instance.
294 // Alphabetical. TODO(oshima): sort.
295 magnification_controller_
.reset();
296 partial_magnification_controller_
.reset();
297 resize_shadow_controller_
.reset();
298 shadow_controller_
.reset();
299 tooltip_controller_
.reset();
300 event_client_
.reset();
301 window_cycle_controller_
.reset();
302 nested_dispatcher_controller_
.reset();
303 user_action_client_
.reset();
304 visibility_controller_
.reset();
305 launcher_delegate_
.reset();
306 launcher_model_
.reset();
307 video_detector_
.reset();
309 power_button_controller_
.reset();
310 lock_state_controller_
.reset();
311 mru_window_tracker_
.reset();
313 resolution_notification_controller_
.reset();
315 // This also deletes all RootWindows. Note that we invoke Shutdown() on
316 // DisplayController before resetting |display_controller_|, since destruction
317 // of its owned RootWindowControllers relies on the value.
318 display_controller_
->Shutdown();
319 display_controller_
.reset();
320 screen_position_controller_
.reset();
322 // Delete the activation controller after other controllers and launcher
323 // because they might have registered ActivationChangeObserver.
324 activation_controller_
.reset();
326 #if defined(OS_CHROMEOS) && defined(USE_X11)
327 if (display_change_observer_
)
328 output_configurator_
->RemoveObserver(display_change_observer_
.get());
329 if (output_configurator_animation_
)
330 output_configurator_
->RemoveObserver(output_configurator_animation_
.get());
331 if (display_error_observer_
)
332 output_configurator_
->RemoveObserver(display_error_observer_
.get());
333 base::MessagePumpAuraX11::Current()->RemoveDispatcherForRootWindow(
334 output_configurator());
335 base::MessagePumpAuraX11::Current()->RemoveObserver(output_configurator());
336 display_change_observer_
.reset();
337 #endif // defined(OS_CHROMEOS)
339 #if defined(OS_CHROMEOS)
340 internal::PowerStatus::Shutdown();
343 DCHECK(instance_
== this);
348 Shell
* Shell::CreateInstance(ShellDelegate
* delegate
) {
350 instance_
= new Shell(delegate
);
356 Shell
* Shell::GetInstance() {
362 bool Shell::HasInstance() {
367 void Shell::DeleteInstance() {
373 internal::RootWindowController
* Shell::GetPrimaryRootWindowController() {
374 return GetRootWindowController(GetPrimaryRootWindow());
378 Shell::RootWindowControllerList
Shell::GetAllRootWindowControllers() {
379 return Shell::GetInstance()->display_controller()->
380 GetAllRootWindowControllers();
384 aura::RootWindow
* Shell::GetPrimaryRootWindow() {
385 return GetInstance()->display_controller()->GetPrimaryRootWindow();
389 aura::RootWindow
* Shell::GetActiveRootWindow() {
390 Shell
* shell
= GetInstance();
391 if (shell
->scoped_target_root_window_
)
392 return shell
->scoped_target_root_window_
;
393 return shell
->target_root_window_
;
397 gfx::Screen
* Shell::GetScreen() {
398 return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE
);
402 Shell::RootWindowList
Shell::GetAllRootWindows() {
403 return Shell::GetInstance()->display_controller()->
408 aura::Window
* Shell::GetContainer(aura::RootWindow
* root_window
,
410 return root_window
->GetChildById(container_id
);
414 const aura::Window
* Shell::GetContainer(const aura::RootWindow
* root_window
,
416 return root_window
->GetChildById(container_id
);
420 std::vector
<aura::Window
*> Shell::GetContainersFromAllRootWindows(
422 aura::RootWindow
* priority_root
) {
423 std::vector
<aura::Window
*> containers
;
424 RootWindowList root_windows
= GetAllRootWindows();
425 for (RootWindowList::const_iterator it
= root_windows
.begin();
426 it
!= root_windows
.end(); ++it
) {
427 aura::Window
* container
= (*it
)->GetChildById(container_id
);
429 if (priority_root
&& priority_root
->Contains(container
))
430 containers
.insert(containers
.begin(), container
);
432 containers
.push_back(container
);
439 bool Shell::IsForcedMaximizeMode() {
440 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
441 return command_line
->HasSwitch(switches::kForcedMaximizeMode
);
445 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
447 delegate_
->PreInit();
448 bool display_initialized
= false;
449 #if defined(OS_CHROMEOS) && defined(USE_X11)
450 output_configurator_animation_
.reset(
451 new internal::OutputConfiguratorAnimation());
452 output_configurator_
->AddObserver(output_configurator_animation_
.get());
453 if (base::chromeos::IsRunningOnChromeOS()) {
454 display_change_observer_
.reset(new internal::DisplayChangeObserver
);
455 // Register |display_change_observer_| first so that the rest of
456 // observer gets invoked after the root windows are configured.
457 output_configurator_
->AddObserver(display_change_observer_
.get());
458 display_error_observer_
.reset(new internal::DisplayErrorObserver());
459 output_configurator_
->AddObserver(display_error_observer_
.get());
460 output_configurator_
->set_state_controller(display_change_observer_
.get());
461 if (!command_line
->HasSwitch(ash::switches::kAshDisableSoftwareMirroring
))
462 output_configurator_
->set_mirroring_controller(display_manager_
.get());
463 output_configurator_
->Start(
464 delegate_
->IsFirstRunAfterBoot() ? kChromeOsBootColor
: 0);
465 display_initialized
= true;
467 #endif // defined(OS_CHROMEOS) && defined(USE_X11)
468 if (!display_initialized
)
469 display_manager_
->InitFromCommandLine();
471 // Install the custom factory first so that views::FocusManagers for Tray,
472 // Launcher, and WallPaper could be created by the factory.
473 views::FocusManagerFactory::Install(new AshFocusManagerFactory
);
475 env_filter_
.reset(new views::corewm::CompoundEventFilter
);
476 AddPreTargetHandler(env_filter_
.get());
478 // Env creates the compositor. Historically it seems to have been implicitly
479 // initialized first by the ActivationController, but now that FocusController
480 // no longer does this we need to do it explicitly.
481 aura::Env::GetInstance();
482 if (views::corewm::UseFocusController()) {
483 views::corewm::FocusController
* focus_controller
=
484 new views::corewm::FocusController(new wm::AshFocusRules
);
485 focus_client_
.reset(focus_controller
);
486 activation_client_
= focus_controller
;
487 activation_client_
->AddObserver(this);
489 focus_client_
.reset(new aura::FocusManager
);
490 activation_controller_
.reset(
491 new internal::ActivationController(
493 new internal::AshActivationController
));
494 activation_client_
= activation_controller_
.get();
495 AddPreTargetHandler(activation_controller_
.get());
498 focus_cycler_
.reset(new internal::FocusCycler());
500 screen_position_controller_
.reset(new internal::ScreenPositionController
);
501 root_window_host_factory_
.reset(delegate_
->CreateRootWindowHostFactory());
503 display_controller_
->Start();
504 display_controller_
->InitPrimaryDisplay();
505 aura::RootWindow
* root_window
= display_controller_
->GetPrimaryRootWindow();
506 target_root_window_
= root_window
;
508 resolution_notification_controller_
.reset(
509 new internal::ResolutionNotificationController
);
511 cursor_manager_
.SetDisplay(DisplayController::GetPrimaryDisplay());
513 #if !defined(OS_MACOSX)
514 nested_dispatcher_controller_
.reset(new NestedDispatcherController
);
515 accelerator_controller_
.reset(new AcceleratorController
);
518 // The order in which event filters are added is significant.
519 event_rewriter_filter_
.reset(new internal::EventRewriterEventFilter
);
520 AddPreTargetHandler(event_rewriter_filter_
.get());
522 // UserActivityDetector passes events to observers, so let them get
524 user_activity_detector_
.reset(new UserActivityDetector
);
525 AddPreTargetHandler(user_activity_detector_
.get());
527 overlay_filter_
.reset(new internal::OverlayEventFilter
);
528 AddPreTargetHandler(overlay_filter_
.get());
529 AddShellObserver(overlay_filter_
.get());
531 input_method_filter_
.reset(new views::corewm::InputMethodEventFilter(
532 root_window
->GetAcceleratedWidget()));
533 AddPreTargetHandler(input_method_filter_
.get());
535 #if !defined(OS_MACOSX)
536 accelerator_filter_
.reset(new internal::AcceleratorFilter
);
537 AddPreTargetHandler(accelerator_filter_
.get());
540 event_transformation_handler_
.reset(new internal::EventTransformationHandler
);
541 AddPreTargetHandler(event_transformation_handler_
.get());
543 system_gesture_filter_
.reset(new internal::SystemGestureEventFilter
);
544 AddPreTargetHandler(system_gesture_filter_
.get());
546 // The keyboard system must be initialized before the RootWindowController is
548 if (keyboard::IsKeyboardEnabled())
549 keyboard::InitializeKeyboard();
551 if (command_line
->HasSwitch(ash::switches::kAshDisableNewLockAnimations
))
552 lock_state_controller_
.reset(new SessionStateControllerImpl
);
554 lock_state_controller_
.reset(new LockStateControllerImpl2
);
555 power_button_controller_
.reset(new PowerButtonController(
556 lock_state_controller_
.get()));
557 AddShellObserver(lock_state_controller_
.get());
559 drag_drop_controller_
.reset(new internal::DragDropController
);
560 mouse_cursor_filter_
.reset(new internal::MouseCursorEventFilter());
561 PrependPreTargetHandler(mouse_cursor_filter_
.get());
563 // Create Controllers that may need root window.
564 // TODO(oshima): Move as many controllers before creating
565 // RootWindowController as possible.
566 visibility_controller_
.reset(new AshVisibilityController
);
567 user_action_client_
.reset(delegate_
->CreateUserActionClient());
568 window_modality_controller_
.reset(
569 new views::corewm::WindowModalityController
);
570 AddPreTargetHandler(window_modality_controller_
.get());
572 magnification_controller_
.reset(
573 MagnificationController::CreateInstance());
574 mru_window_tracker_
.reset(new MruWindowTracker(activation_client_
));
576 partial_magnification_controller_
.reset(
577 new PartialMagnificationController());
579 high_contrast_controller_
.reset(new HighContrastController
);
580 video_detector_
.reset(new VideoDetector
);
581 window_cycle_controller_
.reset(new WindowCycleController());
582 window_selector_controller_
.reset(new WindowSelectorController());
584 tooltip_controller_
.reset(new views::corewm::TooltipController(
585 gfx::SCREEN_TYPE_ALTERNATE
));
586 AddPreTargetHandler(tooltip_controller_
.get());
588 event_client_
.reset(new internal::EventClientImpl
);
590 // This controller needs to be set before SetupManagedWindowMode.
591 desktop_background_controller_
.reset(new DesktopBackgroundController());
592 user_wallpaper_delegate_
.reset(delegate_
->CreateUserWallpaperDelegate());
593 if (!user_wallpaper_delegate_
)
594 user_wallpaper_delegate_
.reset(new DummyUserWallpaperDelegate());
596 // StatusAreaWidget uses Shell's CapsLockDelegate.
597 caps_lock_delegate_
.reset(delegate_
->CreateCapsLockDelegate());
599 session_state_delegate_
.reset(delegate_
->CreateSessionStateDelegate());
601 if (!command_line
->HasSwitch(views::corewm::switches::kNoDropShadows
)) {
602 resize_shadow_controller_
.reset(new internal::ResizeShadowController());
603 shadow_controller_
.reset(
604 new views::corewm::ShadowController(activation_client_
));
607 // Create system_tray_notifier_ before the delegate.
608 system_tray_notifier_
.reset(new ash::SystemTrayNotifier());
610 // Initialize system_tray_delegate_ before initializing StatusAreaWidget.
611 system_tray_delegate_
.reset(delegate()->CreateSystemTrayDelegate());
612 if (!system_tray_delegate_
)
613 system_tray_delegate_
.reset(SystemTrayDelegate::CreateDummyDelegate());
615 internal::RootWindowController
* root_window_controller
=
616 new internal::RootWindowController(root_window
);
617 InitRootWindowController(root_window_controller
,
618 delegate_
->IsFirstRunAfterBoot());
620 locale_notification_controller_
.reset(
621 new internal::LocaleNotificationController
);
623 // Initialize system_tray_delegate_ after StatusAreaWidget is created.
624 system_tray_delegate_
->Initialize();
626 display_controller_
->InitSecondaryDisplays();
629 root_window_controller
->root_window_layout()->OnWindowResized();
631 // It needs to be created after OnWindowResized has been called, otherwise the
632 // widget will not paint when restoring after a browser crash. Also it needs
633 // to be created after InitSecondaryDisplays() to initialize the wallpapers in
635 user_wallpaper_delegate_
->InitializeWallpaper();
637 if (initially_hide_cursor_
)
638 cursor_manager_
.HideCursor();
639 cursor_manager_
.SetCursor(ui::kCursorPointer
);
641 if (!cursor_manager_
.IsCursorVisible()) {
642 // Cursor might have been hidden by something other than chrome.
643 // Let the first mouse event show the cursor.
644 env_filter_
->set_cursor_hidden_by_filter(true);
648 void Shell::ShowContextMenu(const gfx::Point
& location_in_screen
,
649 ui::MenuSourceType source_type
) {
650 // No context menus if there is no session with an active user.
651 if (!session_state_delegate_
->NumberOfLoggedInUsers())
653 // No context menus when screen is locked.
654 if (session_state_delegate_
->IsScreenLocked())
657 aura::RootWindow
* root
=
658 wm::GetRootWindowMatching(gfx::Rect(location_in_screen
, gfx::Size()));
659 // TODO(oshima): The root and root window controller shouldn't be
660 // NULL even for the out-of-bounds |location_in_screen| (It should
661 // return the primary root). Investigate why/how this is
662 // happening. crbug.com/165214.
663 internal::RootWindowController
* rwc
= GetRootWindowController(root
);
664 CHECK(rwc
) << "root=" << root
665 << ", location:" << location_in_screen
.ToString();
667 rwc
->ShowContextMenu(location_in_screen
, source_type
);
670 void Shell::ToggleAppList(aura::Window
* window
) {
671 // If the context window is not given, show it on the active root window.
673 window
= GetActiveRootWindow();
674 if (!app_list_controller_
)
675 app_list_controller_
.reset(new internal::AppListController
);
676 app_list_controller_
->SetVisible(!app_list_controller_
->IsVisible(), window
);
679 bool Shell::GetAppListTargetVisibility() const {
680 return app_list_controller_
.get() &&
681 app_list_controller_
->GetTargetVisibility();
684 aura::Window
* Shell::GetAppListWindow() {
685 return app_list_controller_
.get() ? app_list_controller_
->GetWindow() : NULL
;
688 bool Shell::IsSystemModalWindowOpen() const {
689 if (simulate_modal_window_open_for_testing_
)
691 const std::vector
<aura::Window
*> containers
= GetContainersFromAllRootWindows(
692 internal::kShellWindowId_SystemModalContainer
, NULL
);
693 for (std::vector
<aura::Window
*>::const_iterator cit
= containers
.begin();
694 cit
!= containers
.end(); ++cit
) {
695 for (aura::Window::Windows::const_iterator wit
= (*cit
)->children().begin();
696 wit
!= (*cit
)->children().end(); ++wit
) {
697 if ((*wit
)->GetProperty(aura::client::kModalKey
) ==
698 ui::MODAL_TYPE_SYSTEM
&& (*wit
)->TargetVisibility()) {
706 views::NonClientFrameView
* Shell::CreateDefaultNonClientFrameView(
707 views::Widget
* widget
) {
708 // Use translucent-style window frames for dialogs.
709 CustomFrameViewAsh
* frame_view
= new CustomFrameViewAsh
;
710 frame_view
->Init(widget
);
714 void Shell::RotateFocus(Direction direction
) {
715 focus_cycler_
->RotateFocus(
716 direction
== FORWARD
? internal::FocusCycler::FORWARD
:
717 internal::FocusCycler::BACKWARD
);
720 void Shell::SetDisplayWorkAreaInsets(Window
* contains
,
721 const gfx::Insets
& insets
) {
722 if (!display_controller_
->UpdateWorkAreaOfDisplayNearestWindow(
726 FOR_EACH_OBSERVER(ShellObserver
, observers_
,
727 OnDisplayWorkAreaInsetsChanged());
730 void Shell::OnLoginStateChanged(user::LoginStatus status
) {
731 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnLoginStateChanged(status
));
734 void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status
) {
735 RootWindowControllerList controllers
= GetAllRootWindowControllers();
736 for (RootWindowControllerList::iterator iter
= controllers
.begin();
737 iter
!= controllers
.end(); ++iter
)
738 (*iter
)->UpdateAfterLoginStatusChange(status
);
741 void Shell::OnAppTerminating() {
742 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnAppTerminating());
745 void Shell::OnLockStateChanged(bool locked
) {
746 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnLockStateChanged(locked
));
748 // Make sure that there is no system modal in Lock layer when unlocked.
750 std::vector
<aura::Window
*> containers
= GetContainersFromAllRootWindows(
751 internal::kShellWindowId_LockSystemModalContainer
,
752 GetPrimaryRootWindow());
753 for (std::vector
<aura::Window
*>::const_iterator iter
= containers
.begin();
754 iter
!= containers
.end(); ++iter
) {
755 DCHECK_EQ(0u, (*iter
)->children().size());
761 void Shell::CreateLauncher() {
762 RootWindowControllerList controllers
= GetAllRootWindowControllers();
763 for (RootWindowControllerList::iterator iter
= controllers
.begin();
764 iter
!= controllers
.end(); ++iter
)
765 (*iter
)->shelf()->CreateLauncher();
768 void Shell::ShowLauncher() {
769 RootWindowControllerList controllers
= GetAllRootWindowControllers();
770 for (RootWindowControllerList::iterator iter
= controllers
.begin();
771 iter
!= controllers
.end(); ++iter
)
772 (*iter
)->ShowLauncher();
775 void Shell::AddShellObserver(ShellObserver
* observer
) {
776 observers_
.AddObserver(observer
);
779 void Shell::RemoveShellObserver(ShellObserver
* observer
) {
780 observers_
.RemoveObserver(observer
);
783 void Shell::UpdateShelfVisibility() {
784 RootWindowControllerList controllers
= GetAllRootWindowControllers();
785 for (RootWindowControllerList::iterator iter
= controllers
.begin();
786 iter
!= controllers
.end(); ++iter
)
787 if ((*iter
)->shelf())
788 (*iter
)->UpdateShelfVisibility();
791 void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior
,
792 aura::RootWindow
* root_window
) {
793 ash::internal::ShelfLayoutManager::ForLauncher(root_window
)->
794 SetAutoHideBehavior(behavior
);
797 ShelfAutoHideBehavior
Shell::GetShelfAutoHideBehavior(
798 aura::RootWindow
* root_window
) const {
799 return ash::internal::ShelfLayoutManager::ForLauncher(root_window
)->
800 auto_hide_behavior();
803 void Shell::SetShelfAlignment(ShelfAlignment alignment
,
804 aura::RootWindow
* root_window
) {
805 if (ash::internal::ShelfLayoutManager::ForLauncher(root_window
)->
806 SetAlignment(alignment
)) {
808 ShellObserver
, observers_
, OnShelfAlignmentChanged(root_window
));
812 ShelfAlignment
Shell::GetShelfAlignment(aura::RootWindow
* root_window
) {
813 return GetRootWindowController(root_window
)->
814 GetShelfLayoutManager()->GetAlignment();
817 void Shell::SetDimming(bool should_dim
) {
818 RootWindowControllerList controllers
= GetAllRootWindowControllers();
819 for (RootWindowControllerList::iterator iter
= controllers
.begin();
820 iter
!= controllers
.end(); ++iter
)
821 (*iter
)->screen_dimmer()->SetDimming(should_dim
);
824 void Shell::CreateModalBackground(aura::Window
* window
) {
825 if (!modality_filter_
) {
826 modality_filter_
.reset(new internal::SystemModalContainerEventFilter(this));
827 AddPreTargetHandler(modality_filter_
.get());
829 RootWindowControllerList controllers
= GetAllRootWindowControllers();
830 for (RootWindowControllerList::iterator iter
= controllers
.begin();
831 iter
!= controllers
.end(); ++iter
)
832 (*iter
)->GetSystemModalLayoutManager(window
)->CreateModalBackground();
835 void Shell::OnModalWindowRemoved(aura::Window
* removed
) {
836 RootWindowControllerList controllers
= GetAllRootWindowControllers();
837 bool activated
= false;
838 for (RootWindowControllerList::iterator iter
= controllers
.begin();
839 iter
!= controllers
.end() && !activated
; ++iter
) {
840 activated
= (*iter
)->GetSystemModalLayoutManager(removed
)->
841 ActivateNextModalWindow();
844 RemovePreTargetHandler(modality_filter_
.get());
845 modality_filter_
.reset();
846 for (RootWindowControllerList::iterator iter
= controllers
.begin();
847 iter
!= controllers
.end(); ++iter
)
848 (*iter
)->GetSystemModalLayoutManager(removed
)->DestroyModalBackground();
852 WebNotificationTray
* Shell::GetWebNotificationTray() {
853 return GetPrimaryRootWindowController()->shelf()->
854 status_area_widget()->web_notification_tray();
857 bool Shell::HasPrimaryStatusArea() {
858 ShelfWidget
* shelf
= GetPrimaryRootWindowController()->shelf();
859 return shelf
&& shelf
->status_area_widget();
862 SystemTray
* Shell::GetPrimarySystemTray() {
863 return GetPrimaryRootWindowController()->GetSystemTray();
866 LauncherDelegate
* Shell::GetLauncherDelegate() {
867 if (!launcher_delegate_
) {
868 launcher_model_
.reset(new LauncherModel
);
869 launcher_delegate_
.reset(
870 delegate_
->CreateLauncherDelegate(launcher_model_
.get()));
872 return launcher_delegate_
.get();
875 void Shell::SetTouchHudProjectionEnabled(bool enabled
) {
876 if (is_touch_hud_projection_enabled_
== enabled
)
879 is_touch_hud_projection_enabled_
= enabled
;
880 FOR_EACH_OBSERVER(ShellObserver
, observers_
,
881 OnTouchHudProjectionToggled(enabled
));
884 void Shell::InitRootWindowForSecondaryDisplay(aura::RootWindow
* root
) {
885 internal::RootWindowController
* controller
=
886 new internal::RootWindowController(root
);
887 // Pass false for the |is_first_run_after_boot| parameter so we'll show a
888 // black background on this display instead of trying to mimic the boot splash
890 InitRootWindowController(controller
, false);
892 controller
->root_window_layout()->OnWindowResized();
893 desktop_background_controller_
->OnRootWindowAdded(root
);
894 high_contrast_controller_
->OnRootWindowAdded(root
);
895 root
->ShowRootWindow();
896 // Activate new root for testing.
897 // TODO(oshima): remove this.
898 target_root_window_
= root
;
900 // Create a launcher if a user is already logged.
901 if (Shell::GetInstance()->session_state_delegate()->NumberOfLoggedInUsers())
902 controller
->shelf()->CreateLauncher();
905 void Shell::DoInitialWorkspaceAnimation() {
906 return GetPrimaryRootWindowController()->workspace_controller()->
907 DoInitialAnimation();
910 void Shell::InitRootWindowController(
911 internal::RootWindowController
* controller
,
912 bool first_run_after_boot
) {
914 aura::RootWindow
* root_window
= controller
->root_window();
915 DCHECK(activation_client_
);
916 DCHECK(visibility_controller_
.get());
917 DCHECK(drag_drop_controller_
.get());
918 DCHECK(window_cycle_controller_
.get());
920 aura::client::SetFocusClient(root_window
, focus_client_
.get());
921 input_method_filter_
->SetInputMethodPropertyInRootWindow(root_window
);
922 aura::client::SetActivationClient(root_window
, activation_client_
);
923 if (views::corewm::UseFocusController()) {
924 views::corewm::FocusController
* controller
=
925 static_cast<views::corewm::FocusController
*>(activation_client_
);
926 root_window
->AddPreTargetHandler(controller
);
928 aura::client::SetVisibilityClient(root_window
, visibility_controller_
.get());
929 aura::client::SetDragDropClient(root_window
, drag_drop_controller_
.get());
930 aura::client::SetScreenPositionClient(root_window
,
931 screen_position_controller_
.get());
932 aura::client::SetCursorClient(root_window
, &cursor_manager_
);
933 aura::client::SetTooltipClient(root_window
, tooltip_controller_
.get());
934 aura::client::SetEventClient(root_window
, event_client_
.get());
936 if (nested_dispatcher_controller_
) {
937 aura::client::SetDispatcherClient(root_window
,
938 nested_dispatcher_controller_
.get());
940 if (user_action_client_
)
941 aura::client::SetUserActionClient(root_window
, user_action_client_
.get());
943 controller
->Init(first_run_after_boot
);
945 mru_window_tracker_
->OnRootWindowAdded(root_window
);
948 ////////////////////////////////////////////////////////////////////////////////
951 bool Shell::CanWindowReceiveEvents(aura::Window
* window
) {
952 RootWindowControllerList controllers
= GetAllRootWindowControllers();
953 for (RootWindowControllerList::iterator iter
= controllers
.begin();
954 iter
!= controllers
.end(); ++iter
) {
955 internal::SystemModalContainerLayoutManager
* layout_manager
=
956 (*iter
)->GetSystemModalLayoutManager(window
);
957 if (layout_manager
&& layout_manager
->CanWindowReceiveEvents(window
))
963 ////////////////////////////////////////////////////////////////////////////////
964 // Shell, ui::EventTarget overrides:
966 bool Shell::CanAcceptEvent(const ui::Event
& event
) {
970 ui::EventTarget
* Shell::GetParentTarget() {
974 void Shell::OnEvent(ui::Event
* event
) {
977 ////////////////////////////////////////////////////////////////////////////////
978 // Shell, aura::client::ActivationChangeObserver implementation:
980 void Shell::OnWindowActivated(aura::Window
* gained_active
,
981 aura::Window
* lost_active
) {
983 target_root_window_
= gained_active
->GetRootWindow();