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/screen_position_controller.h"
21 #include "ash/drag_drop/drag_drop_controller.h"
22 #include "ash/focus_cycler.h"
23 #include "ash/high_contrast/high_contrast_controller.h"
24 #include "ash/host/root_window_host_factory.h"
25 #include "ash/launcher/launcher_delegate.h"
26 #include "ash/launcher/launcher_model.h"
27 #include "ash/magnifier/magnification_controller.h"
28 #include "ash/magnifier/partial_magnification_controller.h"
29 #include "ash/root_window_controller.h"
30 #include "ash/screen_ash.h"
31 #include "ash/session_state_delegate.h"
32 #include "ash/shelf/shelf_layout_manager.h"
33 #include "ash/shelf/shelf_widget.h"
34 #include "ash/shell_delegate.h"
35 #include "ash/shell_factory.h"
36 #include "ash/shell_window_ids.h"
37 #include "ash/system/status_area_widget.h"
38 #include "ash/system/tray/system_tray_delegate.h"
39 #include "ash/system/tray/system_tray_notifier.h"
40 #include "ash/wm/activation_controller.h"
41 #include "ash/wm/app_list_controller.h"
42 #include "ash/wm/ash_activation_controller.h"
43 #include "ash/wm/ash_focus_rules.h"
44 #include "ash/wm/ash_native_cursor_manager.h"
45 #include "ash/wm/base_layout_manager.h"
46 #include "ash/wm/capture_controller.h"
47 #include "ash/wm/coordinate_conversion.h"
48 #include "ash/wm/custom_frame_view_ash.h"
49 #include "ash/wm/event_client_impl.h"
50 #include "ash/wm/event_rewriter_event_filter.h"
51 #include "ash/wm/lock_state_controller.h"
52 #include "ash/wm/lock_state_controller_impl2.h"
53 #include "ash/wm/overlay_event_filter.h"
54 #include "ash/wm/power_button_controller.h"
55 #include "ash/wm/property_util.h"
56 #include "ash/wm/resize_shadow_controller.h"
57 #include "ash/wm/root_window_layout_manager.h"
58 #include "ash/wm/screen_dimmer.h"
59 #include "ash/wm/session_state_controller_impl.h"
60 #include "ash/wm/system_gesture_event_filter.h"
61 #include "ash/wm/system_modal_container_event_filter.h"
62 #include "ash/wm/system_modal_container_layout_manager.h"
63 #include "ash/wm/user_activity_detector.h"
64 #include "ash/wm/video_detector.h"
65 #include "ash/wm/window_animations.h"
66 #include "ash/wm/window_cycle_controller.h"
67 #include "ash/wm/window_properties.h"
68 #include "ash/wm/window_util.h"
69 #include "ash/wm/workspace_controller.h"
70 #include "base/bind.h"
71 #include "base/command_line.h"
72 #include "base/debug/leak_annotations.h"
73 #include "ui/aura/client/aura_constants.h"
74 #include "ui/aura/client/user_action_client.h"
75 #include "ui/aura/env.h"
76 #include "ui/aura/focus_manager.h"
77 #include "ui/aura/layout_manager.h"
78 #include "ui/aura/root_window.h"
79 #include "ui/aura/window.h"
80 #include "ui/base/ui_base_switches.h"
81 #include "ui/compositor/layer.h"
82 #include "ui/compositor/layer_animator.h"
83 #include "ui/gfx/display.h"
84 #include "ui/gfx/image/image_skia.h"
85 #include "ui/gfx/screen.h"
86 #include "ui/gfx/size.h"
87 #include "ui/keyboard/keyboard.h"
88 #include "ui/keyboard/keyboard_util.h"
89 #include "ui/message_center/message_center.h"
90 #include "ui/views/corewm/compound_event_filter.h"
91 #include "ui/views/corewm/corewm_switches.h"
92 #include "ui/views/corewm/focus_controller.h"
93 #include "ui/views/corewm/input_method_event_filter.h"
94 #include "ui/views/corewm/shadow_controller.h"
95 #include "ui/views/corewm/tooltip_controller.h"
96 #include "ui/views/corewm/visibility_controller.h"
97 #include "ui/views/corewm/window_modality_controller.h"
98 #include "ui/views/focus/focus_manager_factory.h"
99 #include "ui/views/widget/native_widget_aura.h"
100 #include "ui/views/widget/widget.h"
102 #if !defined(OS_MACOSX)
103 #include "ash/accelerators/accelerator_controller.h"
104 #include "ash/accelerators/accelerator_filter.h"
105 #include "ash/accelerators/nested_dispatcher_controller.h"
108 #if defined(OS_CHROMEOS)
110 #include "ash/ash_constants.h"
111 #include "ash/display/display_change_observer_x11.h"
112 #include "ash/display/display_error_observer.h"
113 #include "ash/display/output_configurator_animation.h"
114 #include "base/chromeos/chromeos_version.h"
115 #include "base/message_loop/message_pump_aurax11.h"
116 #include "chromeos/display/output_configurator.h"
117 #include "content/public/browser/gpu_data_manager.h"
118 #include "content/public/common/content_switches.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 active_root_window_(NULL
),
204 activation_client_(NULL
),
205 #if defined(OS_CHROMEOS) && defined(USE_X11)
206 output_configurator_(new chromeos::OutputConfigurator()),
207 #endif // defined(OS_CHROMEOS)
208 native_cursor_manager_(new AshNativeCursorManager
),
209 cursor_manager_(scoped_ptr
<views::corewm::NativeCursorManager
>(
210 native_cursor_manager_
)),
211 browser_context_(NULL
),
212 simulate_modal_window_open_for_testing_(false),
213 is_touch_hud_projection_enabled_(false) {
214 DCHECK(delegate_
.get());
215 display_manager_
.reset(new internal::DisplayManager
);
217 ANNOTATE_LEAKING_OBJECT_PTR(screen_
); // see crbug.com/156466
218 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_ALTERNATE
, screen_
);
219 if (!gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE
))
220 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE
, screen_
);
221 display_controller_
.reset(new DisplayController
);
222 #if defined(OS_CHROMEOS) && defined(USE_X11)
223 bool is_panel_fitting_disabled
=
224 content::GpuDataManager::GetInstance()->IsFeatureBlacklisted(
225 gpu::GPU_FEATURE_TYPE_PANEL_FITTING
) ||
226 CommandLine::ForCurrentProcess()->HasSwitch(
227 ::switches::kDisablePanelFitting
);
229 output_configurator_
->Init(
230 !is_panel_fitting_disabled
,
231 delegate_
->IsFirstRunAfterBoot() ? kChromeOsBootColor
: 0);
233 base::MessagePumpAuraX11::Current()->AddDispatcherForRootWindow(
234 output_configurator());
235 // We can't do this with a root window listener because XI_HierarchyChanged
236 // messages don't have a target window.
237 base::MessagePumpAuraX11::Current()->AddObserver(output_configurator());
238 #endif // defined(OS_CHROMEOS)
239 AddPreTargetHandler(this);
241 #if defined(OS_CHROMEOS)
242 internal::PowerStatus::Initialize();
247 views::FocusManagerFactory::Install(NULL
);
249 // Remove the focus from any window. This will prevent overhead and side
250 // effects (e.g. crashes) from changing focus during shutdown.
251 // See bug crbug.com/134502.
252 if (active_root_window_
)
253 aura::client::GetFocusClient(active_root_window_
)->FocusWindow(NULL
);
255 // Please keep in same order as in Init() because it's easy to miss one.
256 RemovePreTargetHandler(event_rewriter_filter_
.get());
257 RemovePreTargetHandler(user_activity_detector_
.get());
258 RemovePreTargetHandler(overlay_filter_
.get());
259 RemovePreTargetHandler(input_method_filter_
.get());
260 RemovePreTargetHandler(window_modality_controller_
.get());
261 if (mouse_cursor_filter_
)
262 RemovePreTargetHandler(mouse_cursor_filter_
.get());
263 RemovePreTargetHandler(system_gesture_filter_
.get());
264 RemovePreTargetHandler(event_transformation_handler_
.get());
265 #if !defined(OS_MACOSX)
266 RemovePreTargetHandler(accelerator_filter_
.get());
269 // TooltipController is deleted with the Shell so removing its references.
270 RemovePreTargetHandler(tooltip_controller_
.get());
272 // AppList needs to be released before shelf layout manager, which is
273 // destroyed with launcher container in the loop below. However, app list
274 // container is now on top of launcher container and released after it.
275 // TODO(xiyuan): Move it back when app list container is no longer needed.
276 app_list_controller_
.reset();
278 // Destroy SystemTrayDelegate before destroying the status area(s).
279 system_tray_delegate_
->Shutdown();
280 system_tray_delegate_
.reset();
282 // Destroy all child windows including widgets.
283 display_controller_
->CloseChildWindows();
285 // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems
286 // needs to remove observers from it.
287 system_tray_notifier_
.reset();
289 // These need a valid Shell instance to clean up properly, so explicitly
290 // delete them before invalidating the instance.
291 // Alphabetical. TODO(oshima): sort.
292 drag_drop_controller_
.reset();
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 capture_controller_
.reset();
301 nested_dispatcher_controller_
.reset();
302 user_action_client_
.reset();
303 visibility_controller_
.reset();
304 launcher_delegate_
.reset();
305 launcher_model_
.reset();
306 video_detector_
.reset();
308 power_button_controller_
.reset();
309 lock_state_controller_
.reset();
311 // This also deletes all RootWindows. Note that we invoke Shutdown() on
312 // DisplayController before resetting |display_controller_|, since destruction
313 // of its owned RootWindowControllers relies on the value.
314 display_controller_
->Shutdown();
315 display_controller_
.reset();
316 screen_position_controller_
.reset();
318 // Delete the activation controller after other controllers and launcher
319 // because they might have registered ActivationChangeObserver.
320 activation_controller_
.reset();
322 #if defined(OS_CHROMEOS) && defined(USE_X11)
323 if (display_change_observer_
)
324 output_configurator_
->RemoveObserver(display_change_observer_
.get());
325 if (output_configurator_animation_
)
326 output_configurator_
->RemoveObserver(output_configurator_animation_
.get());
327 if (display_error_observer_
)
328 output_configurator_
->RemoveObserver(display_error_observer_
.get());
329 base::MessagePumpAuraX11::Current()->RemoveDispatcherForRootWindow(
330 output_configurator());
331 base::MessagePumpAuraX11::Current()->RemoveObserver(output_configurator());
332 display_change_observer_
.reset();
333 #endif // defined(OS_CHROMEOS)
335 #if defined(OS_CHROMEOS)
336 internal::PowerStatus::Shutdown();
339 DCHECK(instance_
== this);
344 Shell
* Shell::CreateInstance(ShellDelegate
* delegate
) {
346 instance_
= new Shell(delegate
);
352 Shell
* Shell::GetInstance() {
358 bool Shell::HasInstance() {
363 void Shell::DeleteInstance() {
369 internal::RootWindowController
* Shell::GetPrimaryRootWindowController() {
370 return GetRootWindowController(GetPrimaryRootWindow());
374 Shell::RootWindowControllerList
Shell::GetAllRootWindowControllers() {
375 return Shell::GetInstance()->display_controller()->
376 GetAllRootWindowControllers();
380 aura::RootWindow
* Shell::GetPrimaryRootWindow() {
381 return GetInstance()->display_controller()->GetPrimaryRootWindow();
385 aura::RootWindow
* Shell::GetActiveRootWindow() {
386 return GetInstance()->active_root_window_
;
390 gfx::Screen
* Shell::GetScreen() {
391 return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE
);
395 Shell::RootWindowList
Shell::GetAllRootWindows() {
396 return Shell::GetInstance()->display_controller()->
401 aura::Window
* Shell::GetContainer(aura::RootWindow
* root_window
,
403 return root_window
->GetChildById(container_id
);
407 const aura::Window
* Shell::GetContainer(const aura::RootWindow
* root_window
,
409 return root_window
->GetChildById(container_id
);
413 std::vector
<aura::Window
*> Shell::GetContainersFromAllRootWindows(
415 aura::RootWindow
* priority_root
) {
416 std::vector
<aura::Window
*> containers
;
417 RootWindowList root_windows
= GetAllRootWindows();
418 for (RootWindowList::const_iterator it
= root_windows
.begin();
419 it
!= root_windows
.end(); ++it
) {
420 aura::Window
* container
= (*it
)->GetChildById(container_id
);
422 if (priority_root
&& priority_root
->Contains(container
))
423 containers
.insert(containers
.begin(), container
);
425 containers
.push_back(container
);
432 bool Shell::IsForcedMaximizeMode() {
433 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
434 return command_line
->HasSwitch(switches::kForcedMaximizeMode
);
438 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
440 delegate_
->PreInit();
441 bool display_initialized
= false;
442 #if defined(OS_CHROMEOS) && defined(USE_X11)
443 output_configurator_animation_
.reset(
444 new internal::OutputConfiguratorAnimation());
445 output_configurator_
->AddObserver(output_configurator_animation_
.get());
446 if (base::chromeos::IsRunningOnChromeOS()) {
447 display_change_observer_
.reset(new internal::DisplayChangeObserverX11
);
448 // Register |display_change_observer_| first so that the rest of
449 // observer gets invoked after the root windows are configured.
450 output_configurator_
->AddObserver(display_change_observer_
.get());
451 display_error_observer_
.reset(new internal::DisplayErrorObserver());
452 output_configurator_
->AddObserver(display_error_observer_
.get());
453 output_configurator_
->set_state_controller(display_change_observer_
.get());
454 if (!command_line
->HasSwitch(ash::switches::kAshDisableSoftwareMirroring
))
455 output_configurator_
->set_mirroring_controller(display_manager_
.get());
456 output_configurator_
->Start();
457 display_change_observer_
->OnDisplayModeChanged();
458 display_initialized
= true;
461 if (!display_initialized
)
462 display_manager_
->InitFromCommandLine();
464 // Install the custom factory first so that views::FocusManagers for Tray,
465 // Launcher, and WallPaper could be created by the factory.
466 views::FocusManagerFactory::Install(new AshFocusManagerFactory
);
468 env_filter_
.reset(new views::corewm::CompoundEventFilter
);
469 AddPreTargetHandler(env_filter_
.get());
471 // Env creates the compositor. Historically it seems to have been implicitly
472 // initialized first by the ActivationController, but now that FocusController
473 // no longer does this we need to do it explicitly.
474 aura::Env::GetInstance();
475 if (views::corewm::UseFocusController()) {
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);
482 focus_client_
.reset(new aura::FocusManager
);
483 activation_controller_
.reset(
484 new internal::ActivationController(
486 new internal::AshActivationController
));
487 activation_client_
= activation_controller_
.get();
488 AddPreTargetHandler(activation_controller_
.get());
491 focus_cycler_
.reset(new internal::FocusCycler());
493 screen_position_controller_
.reset(new internal::ScreenPositionController
);
494 root_window_host_factory_
.reset(delegate_
->CreateRootWindowHostFactory());
496 display_controller_
->Start();
497 display_controller_
->InitPrimaryDisplay();
498 aura::RootWindow
* root_window
= display_controller_
->GetPrimaryRootWindow();
499 active_root_window_
= root_window
;
501 cursor_manager_
.SetDisplay(DisplayController::GetPrimaryDisplay());
503 #if !defined(OS_MACOSX)
504 nested_dispatcher_controller_
.reset(new NestedDispatcherController
);
505 accelerator_controller_
.reset(new AcceleratorController
);
508 // The order in which event filters are added is significant.
509 event_rewriter_filter_
.reset(new internal::EventRewriterEventFilter
);
510 AddPreTargetHandler(event_rewriter_filter_
.get());
512 // UserActivityDetector passes events to observers, so let them get
514 user_activity_detector_
.reset(new UserActivityDetector
);
515 AddPreTargetHandler(user_activity_detector_
.get());
517 overlay_filter_
.reset(new internal::OverlayEventFilter
);
518 AddPreTargetHandler(overlay_filter_
.get());
519 AddShellObserver(overlay_filter_
.get());
521 input_method_filter_
.reset(new views::corewm::InputMethodEventFilter(
522 root_window
->GetAcceleratedWidget()));
523 AddPreTargetHandler(input_method_filter_
.get());
525 #if !defined(OS_MACOSX)
526 accelerator_filter_
.reset(new internal::AcceleratorFilter
);
527 AddPreTargetHandler(accelerator_filter_
.get());
530 event_transformation_handler_
.reset(new internal::EventTransformationHandler
);
531 AddPreTargetHandler(event_transformation_handler_
.get());
533 system_gesture_filter_
.reset(new internal::SystemGestureEventFilter
);
534 AddPreTargetHandler(system_gesture_filter_
.get());
536 capture_controller_
.reset(new internal::CaptureController
);
538 // The keyboard system must be initialized before the RootWindowController is
540 if (keyboard::IsKeyboardEnabled())
541 keyboard::InitializeKeyboard();
543 if (command_line
->HasSwitch(ash::switches::kAshDisableNewLockAnimations
))
544 lock_state_controller_
.reset(new SessionStateControllerImpl
);
546 lock_state_controller_
.reset(new LockStateControllerImpl2
);
547 power_button_controller_
.reset(new PowerButtonController(
548 lock_state_controller_
.get()));
549 AddShellObserver(lock_state_controller_
.get());
551 drag_drop_controller_
.reset(new internal::DragDropController
);
552 mouse_cursor_filter_
.reset(new internal::MouseCursorEventFilter());
553 PrependPreTargetHandler(mouse_cursor_filter_
.get());
555 // Create Controllers that may need root window.
556 // TODO(oshima): Move as many controllers before creating
557 // RootWindowController as possible.
558 visibility_controller_
.reset(new AshVisibilityController
);
559 user_action_client_
.reset(delegate_
->CreateUserActionClient());
560 window_modality_controller_
.reset(
561 new views::corewm::WindowModalityController
);
562 AddPreTargetHandler(window_modality_controller_
.get());
564 magnification_controller_
.reset(
565 MagnificationController::CreateInstance());
567 partial_magnification_controller_
.reset(
568 new PartialMagnificationController());
570 high_contrast_controller_
.reset(new HighContrastController
);
571 video_detector_
.reset(new VideoDetector
);
572 window_cycle_controller_
.reset(new WindowCycleController(activation_client_
));
574 tooltip_controller_
.reset(new views::corewm::TooltipController(
575 gfx::SCREEN_TYPE_ALTERNATE
));
576 AddPreTargetHandler(tooltip_controller_
.get());
578 event_client_
.reset(new internal::EventClientImpl
);
580 // This controller needs to be set before SetupManagedWindowMode.
581 desktop_background_controller_
.reset(new DesktopBackgroundController());
582 user_wallpaper_delegate_
.reset(delegate_
->CreateUserWallpaperDelegate());
583 if (!user_wallpaper_delegate_
)
584 user_wallpaper_delegate_
.reset(new DummyUserWallpaperDelegate());
586 // StatusAreaWidget uses Shell's CapsLockDelegate.
587 caps_lock_delegate_
.reset(delegate_
->CreateCapsLockDelegate());
589 session_state_delegate_
.reset(delegate_
->CreateSessionStateDelegate());
591 if (!command_line
->HasSwitch(views::corewm::switches::kNoDropShadows
)) {
592 resize_shadow_controller_
.reset(new internal::ResizeShadowController());
593 shadow_controller_
.reset(
594 new views::corewm::ShadowController(activation_client_
));
597 // Create system_tray_notifier_ before the delegate.
598 system_tray_notifier_
.reset(new ash::SystemTrayNotifier());
600 // Initialize system_tray_delegate_ before initializing StatusAreaWidget.
601 system_tray_delegate_
.reset(delegate()->CreateSystemTrayDelegate());
602 if (!system_tray_delegate_
)
603 system_tray_delegate_
.reset(SystemTrayDelegate::CreateDummyDelegate());
605 internal::RootWindowController
* root_window_controller
=
606 new internal::RootWindowController(root_window
);
607 InitRootWindowController(root_window_controller
,
608 delegate_
->IsFirstRunAfterBoot());
610 // Initialize system_tray_delegate_ after StatusAreaWidget is created.
611 system_tray_delegate_
->Initialize();
613 display_controller_
->InitSecondaryDisplays();
616 root_window_controller
->root_window_layout()->OnWindowResized();
618 // It needs to be created after OnWindowResized has been called, otherwise the
619 // widget will not paint when restoring after a browser crash. Also it needs
620 // to be created after InitSecondaryDisplays() to initialize the wallpapers in
622 user_wallpaper_delegate_
->InitializeWallpaper();
624 if (initially_hide_cursor_
)
625 cursor_manager_
.HideCursor();
626 cursor_manager_
.SetCursor(ui::kCursorPointer
);
628 if (!cursor_manager_
.IsCursorVisible()) {
629 // Cursor might have been hidden by something other than chrome.
630 // Let the first mouse event show the cursor.
631 env_filter_
->set_cursor_hidden_by_filter(true);
635 void Shell::ShowContextMenu(const gfx::Point
& location_in_screen
,
636 ui::MenuSourceType source_type
) {
637 // No context menus if there is no session with an active user.
638 if (!session_state_delegate_
->NumberOfLoggedInUsers())
640 // No context menus when screen is locked.
641 if (session_state_delegate_
->IsScreenLocked())
644 aura::RootWindow
* root
=
645 wm::GetRootWindowMatching(gfx::Rect(location_in_screen
, gfx::Size()));
646 // TODO(oshima): The root and root window controller shouldn't be
647 // NULL even for the out-of-bounds |location_in_screen| (It should
648 // return the primary root). Investigate why/how this is
649 // happening. crbug.com/165214.
650 internal::RootWindowController
* rwc
= GetRootWindowController(root
);
651 CHECK(rwc
) << "root=" << root
652 << ", location:" << location_in_screen
.ToString();
654 rwc
->ShowContextMenu(location_in_screen
, source_type
);
657 void Shell::ToggleAppList(aura::Window
* window
) {
658 // If the context window is not given, show it on the active root window.
660 window
= GetActiveRootWindow();
661 if (!app_list_controller_
)
662 app_list_controller_
.reset(new internal::AppListController
);
663 app_list_controller_
->SetVisible(!app_list_controller_
->IsVisible(), window
);
666 void Shell::SetDragAndDropHostOfCurrentAppList(
667 app_list::ApplicationDragAndDropHost
* drag_and_drop_host
) {
668 if (app_list_controller_
.get())
669 app_list_controller_
->SetDragAndDropHostOfCurrentAppList(
673 bool Shell::GetAppListTargetVisibility() const {
674 return app_list_controller_
.get() &&
675 app_list_controller_
->GetTargetVisibility();
678 aura::Window
* Shell::GetAppListWindow() {
679 return app_list_controller_
.get() ? app_list_controller_
->GetWindow() : NULL
;
682 bool Shell::IsSystemModalWindowOpen() const {
683 if (simulate_modal_window_open_for_testing_
)
685 const std::vector
<aura::Window
*> containers
= GetContainersFromAllRootWindows(
686 internal::kShellWindowId_SystemModalContainer
, NULL
);
687 for (std::vector
<aura::Window
*>::const_iterator cit
= containers
.begin();
688 cit
!= containers
.end(); ++cit
) {
689 for (aura::Window::Windows::const_iterator wit
= (*cit
)->children().begin();
690 wit
!= (*cit
)->children().end(); ++wit
) {
691 if ((*wit
)->GetProperty(aura::client::kModalKey
) ==
692 ui::MODAL_TYPE_SYSTEM
&& (*wit
)->TargetVisibility()) {
700 views::NonClientFrameView
* Shell::CreateDefaultNonClientFrameView(
701 views::Widget
* widget
) {
702 // Use translucent-style window frames for dialogs.
703 CustomFrameViewAsh
* frame_view
= new CustomFrameViewAsh
;
704 frame_view
->Init(widget
);
708 void Shell::RotateFocus(Direction direction
) {
709 focus_cycler_
->RotateFocus(
710 direction
== FORWARD
? internal::FocusCycler::FORWARD
:
711 internal::FocusCycler::BACKWARD
);
714 void Shell::SetDisplayWorkAreaInsets(Window
* contains
,
715 const gfx::Insets
& insets
) {
716 if (!display_controller_
->UpdateWorkAreaOfDisplayNearestWindow(
720 FOR_EACH_OBSERVER(ShellObserver
, observers_
,
721 OnDisplayWorkAreaInsetsChanged());
724 void Shell::OnLoginStateChanged(user::LoginStatus status
) {
725 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnLoginStateChanged(status
));
728 void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status
) {
729 RootWindowControllerList controllers
= GetAllRootWindowControllers();
730 for (RootWindowControllerList::iterator iter
= controllers
.begin();
731 iter
!= controllers
.end(); ++iter
)
732 (*iter
)->UpdateAfterLoginStatusChange(status
);
735 void Shell::OnAppTerminating() {
736 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnAppTerminating());
739 void Shell::OnLockStateChanged(bool locked
) {
740 FOR_EACH_OBSERVER(ShellObserver
, observers_
, OnLockStateChanged(locked
));
743 void Shell::CreateLauncher() {
744 RootWindowControllerList controllers
= GetAllRootWindowControllers();
745 for (RootWindowControllerList::iterator iter
= controllers
.begin();
746 iter
!= controllers
.end(); ++iter
)
747 (*iter
)->shelf()->CreateLauncher();
750 void Shell::ShowLauncher() {
751 RootWindowControllerList controllers
= GetAllRootWindowControllers();
752 for (RootWindowControllerList::iterator iter
= controllers
.begin();
753 iter
!= controllers
.end(); ++iter
)
754 (*iter
)->ShowLauncher();
757 void Shell::AddShellObserver(ShellObserver
* observer
) {
758 observers_
.AddObserver(observer
);
761 void Shell::RemoveShellObserver(ShellObserver
* observer
) {
762 observers_
.RemoveObserver(observer
);
765 void Shell::UpdateShelfVisibility() {
766 RootWindowControllerList controllers
= GetAllRootWindowControllers();
767 for (RootWindowControllerList::iterator iter
= controllers
.begin();
768 iter
!= controllers
.end(); ++iter
)
769 if ((*iter
)->shelf())
770 (*iter
)->UpdateShelfVisibility();
773 void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior
,
774 aura::RootWindow
* root_window
) {
775 ash::internal::ShelfLayoutManager::ForLauncher(root_window
)->
776 SetAutoHideBehavior(behavior
);
779 ShelfAutoHideBehavior
Shell::GetShelfAutoHideBehavior(
780 aura::RootWindow
* root_window
) const {
781 return ash::internal::ShelfLayoutManager::ForLauncher(root_window
)->
782 auto_hide_behavior();
785 void Shell::SetShelfAlignment(ShelfAlignment alignment
,
786 aura::RootWindow
* root_window
) {
787 if (ash::internal::ShelfLayoutManager::ForLauncher(root_window
)->
788 SetAlignment(alignment
)) {
790 ShellObserver
, observers_
, OnShelfAlignmentChanged(root_window
));
794 ShelfAlignment
Shell::GetShelfAlignment(aura::RootWindow
* root_window
) {
795 return GetRootWindowController(root_window
)->
796 GetShelfLayoutManager()->GetAlignment();
799 void Shell::SetDimming(bool should_dim
) {
800 RootWindowControllerList controllers
= GetAllRootWindowControllers();
801 for (RootWindowControllerList::iterator iter
= controllers
.begin();
802 iter
!= controllers
.end(); ++iter
)
803 (*iter
)->screen_dimmer()->SetDimming(should_dim
);
806 void Shell::CreateModalBackground(aura::Window
* window
) {
807 if (!modality_filter_
) {
808 modality_filter_
.reset(new internal::SystemModalContainerEventFilter(this));
809 AddPreTargetHandler(modality_filter_
.get());
811 RootWindowControllerList controllers
= GetAllRootWindowControllers();
812 for (RootWindowControllerList::iterator iter
= controllers
.begin();
813 iter
!= controllers
.end(); ++iter
)
814 (*iter
)->GetSystemModalLayoutManager(window
)->CreateModalBackground();
817 void Shell::OnModalWindowRemoved(aura::Window
* removed
) {
818 RootWindowControllerList controllers
= GetAllRootWindowControllers();
819 bool activated
= false;
820 for (RootWindowControllerList::iterator iter
= controllers
.begin();
821 iter
!= controllers
.end() && !activated
; ++iter
) {
822 activated
= (*iter
)->GetSystemModalLayoutManager(removed
)->
823 ActivateNextModalWindow();
826 RemovePreTargetHandler(modality_filter_
.get());
827 modality_filter_
.reset();
828 for (RootWindowControllerList::iterator iter
= controllers
.begin();
829 iter
!= controllers
.end(); ++iter
)
830 (*iter
)->GetSystemModalLayoutManager(removed
)->DestroyModalBackground();
834 WebNotificationTray
* Shell::GetWebNotificationTray() {
835 return GetPrimaryRootWindowController()->shelf()->
836 status_area_widget()->web_notification_tray();
839 bool Shell::HasPrimaryStatusArea() {
840 ShelfWidget
* shelf
= GetPrimaryRootWindowController()->shelf();
841 return shelf
&& shelf
->status_area_widget();
844 SystemTray
* Shell::GetPrimarySystemTray() {
845 return GetPrimaryRootWindowController()->GetSystemTray();
848 LauncherDelegate
* Shell::GetLauncherDelegate() {
849 if (!launcher_delegate_
) {
850 launcher_model_
.reset(new LauncherModel
);
851 launcher_delegate_
.reset(
852 delegate_
->CreateLauncherDelegate(launcher_model_
.get()));
854 return launcher_delegate_
.get();
857 void Shell::SetTouchHudProjectionEnabled(bool enabled
) {
858 if (is_touch_hud_projection_enabled_
== enabled
)
861 is_touch_hud_projection_enabled_
= enabled
;
862 FOR_EACH_OBSERVER(ShellObserver
, observers_
,
863 OnTouchHudProjectionToggled(enabled
));
866 void Shell::InitRootWindowForSecondaryDisplay(aura::RootWindow
* root
) {
867 internal::RootWindowController
* controller
=
868 new internal::RootWindowController(root
);
869 // Pass false for the |is_first_run_after_boot| parameter so we'll show a
870 // black background on this display instead of trying to mimic the boot splash
872 InitRootWindowController(controller
, false);
874 controller
->root_window_layout()->OnWindowResized();
875 desktop_background_controller_
->OnRootWindowAdded(root
);
876 high_contrast_controller_
->OnRootWindowAdded(root
);
877 root
->ShowRootWindow();
878 // Activate new root for testing.
879 active_root_window_
= root
;
881 // Create a launcher if a user is already logged.
882 if (Shell::GetInstance()->session_state_delegate()->NumberOfLoggedInUsers())
883 controller
->shelf()->CreateLauncher();
886 void Shell::DoInitialWorkspaceAnimation() {
887 return GetPrimaryRootWindowController()->workspace_controller()->
888 DoInitialAnimation();
891 void Shell::InitRootWindowController(
892 internal::RootWindowController
* controller
,
893 bool first_run_after_boot
) {
895 aura::RootWindow
* root_window
= controller
->root_window();
896 DCHECK(activation_client_
);
897 DCHECK(visibility_controller_
.get());
898 DCHECK(drag_drop_controller_
.get());
899 DCHECK(capture_controller_
.get());
900 DCHECK(window_cycle_controller_
.get());
902 aura::client::SetFocusClient(root_window
, focus_client_
.get());
903 input_method_filter_
->SetInputMethodPropertyInRootWindow(root_window
);
904 aura::client::SetActivationClient(root_window
, activation_client_
);
905 if (views::corewm::UseFocusController()) {
906 views::corewm::FocusController
* controller
=
907 static_cast<views::corewm::FocusController
*>(activation_client_
);
908 root_window
->AddPreTargetHandler(controller
);
910 aura::client::SetVisibilityClient(root_window
, visibility_controller_
.get());
911 aura::client::SetDragDropClient(root_window
, drag_drop_controller_
.get());
912 aura::client::SetCaptureClient(root_window
, capture_controller_
.get());
913 aura::client::SetScreenPositionClient(root_window
,
914 screen_position_controller_
.get());
915 aura::client::SetCursorClient(root_window
, &cursor_manager_
);
916 aura::client::SetTooltipClient(root_window
, tooltip_controller_
.get());
917 aura::client::SetEventClient(root_window
, event_client_
.get());
919 if (nested_dispatcher_controller_
) {
920 aura::client::SetDispatcherClient(root_window
,
921 nested_dispatcher_controller_
.get());
923 if (user_action_client_
)
924 aura::client::SetUserActionClient(root_window
, user_action_client_
.get());
926 controller
->Init(first_run_after_boot
);
928 window_cycle_controller_
->OnRootWindowAdded(root_window
);
931 ////////////////////////////////////////////////////////////////////////////////
934 bool Shell::CanWindowReceiveEvents(aura::Window
* window
) {
935 RootWindowControllerList controllers
= GetAllRootWindowControllers();
936 for (RootWindowControllerList::iterator iter
= controllers
.begin();
937 iter
!= controllers
.end(); ++iter
) {
938 internal::SystemModalContainerLayoutManager
* layout_manager
=
939 (*iter
)->GetSystemModalLayoutManager(window
);
940 if (layout_manager
&& layout_manager
->CanWindowReceiveEvents(window
))
946 ////////////////////////////////////////////////////////////////////////////////
947 // Shell, ui::EventTarget overrides:
949 bool Shell::CanAcceptEvent(const ui::Event
& event
) {
953 ui::EventTarget
* Shell::GetParentTarget() {
957 void Shell::OnEvent(ui::Event
* event
) {
960 ////////////////////////////////////////////////////////////////////////////////
961 // Shell, aura::client::ActivationChangeObserver implementation:
963 void Shell::OnWindowActivated(aura::Window
* gained_active
,
964 aura::Window
* lost_active
) {
966 active_root_window_
= gained_active
->GetRootWindow();