Cleanup: move AlwaysOnTopController to RootWindowController
[chromium-blink-merge.git] / ash / shell.cc
blob3f451dfb129e148c9e2e2e09faf3e5aa836bdadb
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.
5 #include "ash/shell.h"
7 #include <algorithm>
8 #include <string>
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/mirror_window_controller.h"
20 #include "ash/display/mouse_cursor_event_filter.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/status_area_widget.h"
39 #include "ash/system/tray/system_tray_delegate.h"
40 #include "ash/system/tray/system_tray_notifier.h"
41 #include "ash/wm/activation_controller.h"
42 #include "ash/wm/app_list_controller.h"
43 #include "ash/wm/ash_activation_controller.h"
44 #include "ash/wm/ash_focus_rules.h"
45 #include "ash/wm/ash_native_cursor_manager.h"
46 #include "ash/wm/base_layout_manager.h"
47 #include "ash/wm/capture_controller.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/overlay_event_filter.h"
55 #include "ash/wm/power_button_controller.h"
56 #include "ash/wm/property_util.h"
57 #include "ash/wm/resize_shadow_controller.h"
58 #include "ash/wm/root_window_layout_manager.h"
59 #include "ash/wm/screen_dimmer.h"
60 #include "ash/wm/session_state_controller_impl.h"
61 #include "ash/wm/system_gesture_event_filter.h"
62 #include "ash/wm/system_modal_container_event_filter.h"
63 #include "ash/wm/system_modal_container_layout_manager.h"
64 #include "ash/wm/user_activity_detector.h"
65 #include "ash/wm/video_detector.h"
66 #include "ash/wm/window_animations.h"
67 #include "ash/wm/window_cycle_controller.h"
68 #include "ash/wm/window_properties.h"
69 #include "ash/wm/window_util.h"
70 #include "ash/wm/workspace_controller.h"
71 #include "base/bind.h"
72 #include "base/command_line.h"
73 #include "base/debug/leak_annotations.h"
74 #include "ui/aura/client/aura_constants.h"
75 #include "ui/aura/client/user_action_client.h"
76 #include "ui/aura/env.h"
77 #include "ui/aura/focus_manager.h"
78 #include "ui/aura/layout_manager.h"
79 #include "ui/aura/root_window.h"
80 #include "ui/aura/window.h"
81 #include "ui/base/ui_base_switches.h"
82 #include "ui/compositor/layer.h"
83 #include "ui/compositor/layer_animator.h"
84 #include "ui/gfx/display.h"
85 #include "ui/gfx/image/image_skia.h"
86 #include "ui/gfx/screen.h"
87 #include "ui/gfx/size.h"
88 #include "ui/keyboard/keyboard.h"
89 #include "ui/keyboard/keyboard_util.h"
90 #include "ui/message_center/message_center.h"
91 #include "ui/views/corewm/compound_event_filter.h"
92 #include "ui/views/corewm/corewm_switches.h"
93 #include "ui/views/corewm/focus_controller.h"
94 #include "ui/views/corewm/input_method_event_filter.h"
95 #include "ui/views/corewm/shadow_controller.h"
96 #include "ui/views/corewm/tooltip_controller.h"
97 #include "ui/views/corewm/visibility_controller.h"
98 #include "ui/views/corewm/window_modality_controller.h"
99 #include "ui/views/focus/focus_manager_factory.h"
100 #include "ui/views/widget/native_widget_aura.h"
101 #include "ui/views/widget/widget.h"
103 #if !defined(OS_MACOSX)
104 #include "ash/accelerators/accelerator_controller.h"
105 #include "ash/accelerators/accelerator_filter.h"
106 #include "ash/accelerators/nested_dispatcher_controller.h"
107 #endif
109 #if defined(OS_CHROMEOS)
110 #if defined(USE_X11)
111 #include "ash/ash_constants.h"
112 #include "ash/display/display_change_observer_x11.h"
113 #include "ash/display/display_error_dialog.h"
114 #include "ash/display/output_configurator_animation.h"
115 #include "base/chromeos/chromeos_version.h"
116 #include "base/message_loop/message_pump_aurax11.h"
117 #include "chromeos/display/output_configurator.h"
118 #include "content/public/browser/gpu_data_manager.h"
119 #include "content/public/common/content_switches.h"
120 #include "gpu/config/gpu_feature_type.h"
121 #endif // defined(USE_X11)
122 #include "ash/system/chromeos/power/power_status.h"
123 #endif // defined(OS_CHROMEOS)
125 namespace ash {
127 namespace {
129 using aura::Window;
130 using views::Widget;
132 // This dummy class is used for shell unit tests. We dont have chrome delegate
133 // in these tests.
134 class DummyUserWallpaperDelegate : public UserWallpaperDelegate {
135 public:
136 DummyUserWallpaperDelegate() {}
138 virtual ~DummyUserWallpaperDelegate() {}
140 virtual int GetAnimationType() OVERRIDE {
141 return views::corewm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE;
144 virtual bool ShouldShowInitialAnimation() OVERRIDE {
145 return false;
148 virtual void UpdateWallpaper() OVERRIDE {
151 virtual void InitializeWallpaper() OVERRIDE {
152 ash::Shell::GetInstance()->desktop_background_controller()->
153 CreateEmptyWallpaper();
156 virtual void OpenSetWallpaperPage() OVERRIDE {
159 virtual bool CanOpenSetWallpaperPage() OVERRIDE {
160 return false;
163 virtual void OnWallpaperAnimationFinished() OVERRIDE {
166 virtual void OnWallpaperBootAnimationFinished() OVERRIDE {
169 private:
170 DISALLOW_COPY_AND_ASSIGN(DummyUserWallpaperDelegate);
173 // A Corewm VisibilityController subclass that calls the Ash animation routine
174 // so we can pick up our extended animations. See ash/wm/window_animations.h.
175 class AshVisibilityController : public views::corewm::VisibilityController {
176 public:
177 AshVisibilityController() {}
178 virtual ~AshVisibilityController() {}
180 private:
181 // Overridden from views::corewm::VisibilityController:
182 virtual bool CallAnimateOnChildWindowVisibilityChanged(
183 aura::Window* window,
184 bool visible) OVERRIDE {
185 return AnimateOnChildWindowVisibilityChanged(window, visible);
188 DISALLOW_COPY_AND_ASSIGN(AshVisibilityController);
191 } // namespace
193 // static
194 Shell* Shell::instance_ = NULL;
195 // static
196 bool Shell::initially_hide_cursor_ = false;
198 ////////////////////////////////////////////////////////////////////////////////
199 // Shell, public:
201 Shell::Shell(ShellDelegate* delegate)
202 : screen_(new ScreenAsh),
203 active_root_window_(NULL),
204 delegate_(delegate),
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);
217 mirror_window_controller_.reset(new internal::MirrorWindowController);
219 ANNOTATE_LEAKING_OBJECT_PTR(screen_); // see crbug.com/156466
220 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_ALTERNATE, screen_);
221 if (!gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE))
222 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_);
223 display_controller_.reset(new DisplayController);
224 #if defined(OS_CHROMEOS) && defined(USE_X11)
225 bool is_panel_fitting_disabled =
226 content::GpuDataManager::GetInstance()->IsFeatureBlacklisted(
227 gpu::GPU_FEATURE_TYPE_PANEL_FITTING) ||
228 CommandLine::ForCurrentProcess()->HasSwitch(
229 ::switches::kDisablePanelFitting);
231 output_configurator_->Init(
232 !is_panel_fitting_disabled,
233 delegate_->IsFirstRunAfterBoot() ? kChromeOsBootColor : 0);
235 base::MessagePumpAuraX11::Current()->AddDispatcherForRootWindow(
236 output_configurator());
237 #endif // defined(OS_CHROMEOS)
238 AddPreTargetHandler(this);
240 #if defined(OS_CHROMEOS)
241 internal::PowerStatus::Initialize();
242 #endif
245 Shell::~Shell() {
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 if (active_root_window_)
252 aura::client::GetFocusClient(active_root_window_)->FocusWindow(NULL);
254 // Please keep in same order as in Init() because it's easy to miss one.
255 RemovePreTargetHandler(user_activity_detector_.get());
256 RemovePreTargetHandler(event_rewriter_filter_.get());
257 RemovePreTargetHandler(overlay_filter_.get());
258 RemovePreTargetHandler(input_method_filter_.get());
259 RemovePreTargetHandler(window_modality_controller_.get());
260 if (mouse_cursor_filter_)
261 RemovePreTargetHandler(mouse_cursor_filter_.get());
262 RemovePreTargetHandler(system_gesture_filter_.get());
263 RemovePreTargetHandler(event_transformation_handler_.get());
264 #if !defined(OS_MACOSX)
265 RemovePreTargetHandler(accelerator_filter_.get());
266 #endif
268 // TooltipController is deleted with the Shell so removing its references.
269 RemovePreTargetHandler(tooltip_controller_.get());
271 // AppList needs to be released before shelf layout manager, which is
272 // destroyed with launcher container in the loop below. However, app list
273 // container is now on top of launcher container and released after it.
274 // TODO(xiyuan): Move it back when app list container is no longer needed.
275 app_list_controller_.reset();
277 // Destroy SystemTrayDelegate before destroying the status area(s).
278 system_tray_delegate_->Shutdown();
279 system_tray_delegate_.reset();
281 // Destroy all child windows including widgets.
282 display_controller_->CloseChildWindows();
284 // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems
285 // needs to remove observers from it.
286 system_tray_notifier_.reset();
288 // These need a valid Shell instance to clean up properly, so explicitly
289 // delete them before invalidating the instance.
290 // Alphabetical. TODO(oshima): sort.
291 drag_drop_controller_.reset();
292 magnification_controller_.reset();
293 partial_magnification_controller_.reset();
294 resize_shadow_controller_.reset();
295 shadow_controller_.reset();
296 tooltip_controller_.reset();
297 event_client_.reset();
298 window_cycle_controller_.reset();
299 capture_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();
310 mirror_window_controller_.reset();
312 // This also deletes all RootWindows. Note that we invoke Shutdown() on
313 // DisplayController before resetting |display_controller_|, since destruction
314 // of its owned RootWindowControllers relies on the value.
315 display_controller_->Shutdown();
316 display_controller_.reset();
317 screen_position_controller_.reset();
319 // Delete the activation controller after other controllers and launcher
320 // because they might have registered ActivationChangeObserver.
321 activation_controller_.reset();
323 #if defined(OS_CHROMEOS) && defined(USE_X11)
324 if (display_change_observer_)
325 output_configurator_->RemoveObserver(display_change_observer_.get());
326 if (output_configurator_animation_)
327 output_configurator_->RemoveObserver(output_configurator_animation_.get());
328 if (display_error_observer_)
329 output_configurator_->RemoveObserver(display_error_observer_.get());
330 base::MessagePumpAuraX11::Current()->RemoveDispatcherForRootWindow(
331 output_configurator());
332 display_change_observer_.reset();
333 #endif // defined(OS_CHROMEOS)
335 #if defined(OS_CHROMEOS)
336 internal::PowerStatus::Shutdown();
337 #endif
339 DCHECK(instance_ == this);
340 instance_ = NULL;
343 // static
344 Shell* Shell::CreateInstance(ShellDelegate* delegate) {
345 CHECK(!instance_);
346 instance_ = new Shell(delegate);
347 instance_->Init();
348 return instance_;
351 // static
352 Shell* Shell::GetInstance() {
353 DCHECK(instance_);
354 return instance_;
357 // static
358 bool Shell::HasInstance() {
359 return !!instance_;
362 // static
363 void Shell::DeleteInstance() {
364 delete instance_;
365 instance_ = NULL;
368 // static
369 internal::RootWindowController* Shell::GetPrimaryRootWindowController() {
370 return GetRootWindowController(GetPrimaryRootWindow());
373 // static
374 Shell::RootWindowControllerList Shell::GetAllRootWindowControllers() {
375 return Shell::GetInstance()->display_controller()->
376 GetAllRootWindowControllers();
379 // static
380 aura::RootWindow* Shell::GetPrimaryRootWindow() {
381 return GetInstance()->display_controller()->GetPrimaryRootWindow();
384 // static
385 aura::RootWindow* Shell::GetActiveRootWindow() {
386 return GetInstance()->active_root_window_;
389 // static
390 gfx::Screen* Shell::GetScreen() {
391 return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE);
394 // static
395 Shell::RootWindowList Shell::GetAllRootWindows() {
396 return Shell::GetInstance()->display_controller()->
397 GetAllRootWindows();
400 // static
401 aura::Window* Shell::GetContainer(aura::RootWindow* root_window,
402 int container_id) {
403 return root_window->GetChildById(container_id);
406 // static
407 const aura::Window* Shell::GetContainer(const aura::RootWindow* root_window,
408 int container_id) {
409 return root_window->GetChildById(container_id);
412 // static
413 std::vector<aura::Window*> Shell::GetContainersFromAllRootWindows(
414 int container_id,
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);
421 if (container) {
422 if (priority_root && priority_root->Contains(container))
423 containers.insert(containers.begin(), container);
424 else
425 containers.push_back(container);
428 return containers;
431 // static
432 bool Shell::IsForcedMaximizeMode() {
433 CommandLine* command_line = CommandLine::ForCurrentProcess();
434 return command_line->HasSwitch(switches::kForcedMaximizeMode);
437 void Shell::Init() {
438 CommandLine* command_line = CommandLine::ForCurrentProcess();
440 delegate_->PreInit();
441 #if defined(OS_CHROMEOS) && defined(USE_X11)
442 output_configurator_animation_.reset(
443 new internal::OutputConfiguratorAnimation());
444 output_configurator_->AddObserver(output_configurator_animation_.get());
445 if (base::chromeos::IsRunningOnChromeOS()) {
446 display_change_observer_.reset(new internal::DisplayChangeObserverX11);
447 // Register |display_change_observer_| first so that the rest of
448 // observer gets invoked after the root windows are configured.
449 output_configurator_->AddObserver(display_change_observer_.get());
450 display_error_observer_.reset(new internal::DisplayErrorObserver());
451 output_configurator_->AddObserver(display_error_observer_.get());
452 output_configurator_->set_state_controller(display_change_observer_.get());
453 if (!command_line->HasSwitch(ash::switches::kAshDisableSoftwareMirroring))
454 output_configurator_->set_mirroring_controller(display_manager_.get());
455 output_configurator_->Start();
456 display_change_observer_->OnDisplayModeChanged();
458 #endif
460 // Install the custom factory first so that views::FocusManagers for Tray,
461 // Launcher, and WallPaper could be created by the factory.
462 views::FocusManagerFactory::Install(new AshFocusManagerFactory);
464 env_filter_.reset(new views::corewm::CompoundEventFilter);
465 AddPreTargetHandler(env_filter_.get());
467 // Env creates the compositor. Historically it seems to have been implicitly
468 // initialized first by the ActivationController, but now that FocusController
469 // no longer does this we need to do it explicitly.
470 aura::Env::GetInstance();
471 if (views::corewm::UseFocusController()) {
472 views::corewm::FocusController* focus_controller =
473 new views::corewm::FocusController(new wm::AshFocusRules);
474 focus_client_.reset(focus_controller);
475 activation_client_ = focus_controller;
476 activation_client_->AddObserver(this);
477 } else {
478 focus_client_.reset(new aura::FocusManager);
479 activation_controller_.reset(
480 new internal::ActivationController(
481 focus_client_.get(),
482 new internal::AshActivationController));
483 activation_client_ = activation_controller_.get();
484 AddPreTargetHandler(activation_controller_.get());
487 focus_cycler_.reset(new internal::FocusCycler());
489 screen_position_controller_.reset(new internal::ScreenPositionController);
490 root_window_host_factory_.reset(delegate_->CreateRootWindowHostFactory());
492 display_controller_->Start();
493 display_controller_->InitPrimaryDisplay();
494 aura::RootWindow* root_window = display_controller_->GetPrimaryRootWindow();
495 active_root_window_ = root_window;
497 cursor_manager_.SetDisplay(DisplayController::GetPrimaryDisplay());
499 #if !defined(OS_MACOSX)
500 nested_dispatcher_controller_.reset(new NestedDispatcherController);
501 accelerator_controller_.reset(new AcceleratorController);
502 #endif
504 // The order in which event filters are added is significant.
505 user_activity_detector_.reset(new UserActivityDetector);
506 AddPreTargetHandler(user_activity_detector_.get());
508 event_rewriter_filter_.reset(new internal::EventRewriterEventFilter);
509 AddPreTargetHandler(event_rewriter_filter_.get());
511 overlay_filter_.reset(new internal::OverlayEventFilter);
512 AddPreTargetHandler(overlay_filter_.get());
513 AddShellObserver(overlay_filter_.get());
515 input_method_filter_.reset(new views::corewm::InputMethodEventFilter(
516 root_window->GetAcceleratedWidget()));
517 AddPreTargetHandler(input_method_filter_.get());
519 #if !defined(OS_MACOSX)
520 accelerator_filter_.reset(new internal::AcceleratorFilter);
521 AddPreTargetHandler(accelerator_filter_.get());
522 #endif
524 event_transformation_handler_.reset(new internal::EventTransformationHandler);
525 AddPreTargetHandler(event_transformation_handler_.get());
527 system_gesture_filter_.reset(new internal::SystemGestureEventFilter);
528 AddPreTargetHandler(system_gesture_filter_.get());
530 capture_controller_.reset(new internal::CaptureController);
532 // The keyboard system must be initialized before the RootWindowController is
533 // created.
534 if (keyboard::IsKeyboardEnabled())
535 keyboard::InitializeKeyboard();
537 internal::RootWindowController* root_window_controller =
538 new internal::RootWindowController(root_window);
539 root_window_controller->CreateContainers();
540 root_window_controller->CreateSystemBackground(
541 delegate_->IsFirstRunAfterBoot());
543 if (command_line->HasSwitch(ash::switches::kAshDisableNewLockAnimations))
544 lock_state_controller_.reset(new SessionStateControllerImpl);
545 else
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 InitRootWindowController(root_window_controller);
582 // This controller needs to be set before SetupManagedWindowMode.
583 desktop_background_controller_.reset(new DesktopBackgroundController());
584 user_wallpaper_delegate_.reset(delegate_->CreateUserWallpaperDelegate());
585 if (!user_wallpaper_delegate_)
586 user_wallpaper_delegate_.reset(new DummyUserWallpaperDelegate());
588 // StatusAreaWidget uses Shell's CapsLockDelegate.
589 caps_lock_delegate_.reset(delegate_->CreateCapsLockDelegate());
591 session_state_delegate_.reset(delegate_->CreateSessionStateDelegate());
593 if (!command_line->HasSwitch(views::corewm::switches::kNoDropShadows)) {
594 resize_shadow_controller_.reset(new internal::ResizeShadowController());
595 shadow_controller_.reset(
596 new views::corewm::ShadowController(activation_client_));
599 // Create system_tray_notifier_ before the delegate.
600 system_tray_notifier_.reset(new ash::SystemTrayNotifier());
602 // Initialize system_tray_delegate_ before initializing StatusAreaWidget.
603 system_tray_delegate_.reset(delegate()->CreateSystemTrayDelegate());
604 if (!system_tray_delegate_)
605 system_tray_delegate_.reset(SystemTrayDelegate::CreateDummyDelegate());
607 // Creates StatusAreaWidget.
608 root_window_controller->InitForPrimaryDisplay();
610 // Initialize system_tray_delegate_ after StatusAreaWidget is created.
611 system_tray_delegate_->Initialize();
613 display_controller_->InitSecondaryDisplays();
615 // Force Layout
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
621 // the correct size.
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())
639 return;
640 // No context menus when screen is locked.
641 if (session_state_delegate_->IsScreenLocked())
642 return;
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();
653 if (rwc)
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.
659 if (!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(
670 drag_and_drop_host);
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_)
684 return true;
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()) {
693 return true;
697 return false;
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);
705 return frame_view;
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_manager_->UpdateWorkAreaOfDisplayNearestWindow(contains, insets))
717 return;
718 FOR_EACH_OBSERVER(ShellObserver, observers_,
719 OnDisplayWorkAreaInsetsChanged());
722 void Shell::OnLoginStateChanged(user::LoginStatus status) {
723 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLoginStateChanged(status));
724 RootWindowControllerList controllers = GetAllRootWindowControllers();
725 for (RootWindowControllerList::iterator iter = controllers.begin();
726 iter != controllers.end(); ++iter)
727 (*iter)->OnLoginStateChanged(status);
730 void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status) {
731 RootWindowControllerList controllers = GetAllRootWindowControllers();
732 for (RootWindowControllerList::iterator iter = controllers.begin();
733 iter != controllers.end(); ++iter)
734 (*iter)->UpdateAfterLoginStatusChange(status);
737 void Shell::OnAppTerminating() {
738 FOR_EACH_OBSERVER(ShellObserver, observers_, OnAppTerminating());
741 void Shell::OnLockStateChanged(bool locked) {
742 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLockStateChanged(locked));
745 void Shell::CreateLauncher() {
746 RootWindowControllerList controllers = GetAllRootWindowControllers();
747 for (RootWindowControllerList::iterator iter = controllers.begin();
748 iter != controllers.end(); ++iter)
749 (*iter)->shelf()->CreateLauncher();
752 void Shell::ShowLauncher() {
753 RootWindowControllerList controllers = GetAllRootWindowControllers();
754 for (RootWindowControllerList::iterator iter = controllers.begin();
755 iter != controllers.end(); ++iter)
756 (*iter)->ShowLauncher();
759 void Shell::AddShellObserver(ShellObserver* observer) {
760 observers_.AddObserver(observer);
763 void Shell::RemoveShellObserver(ShellObserver* observer) {
764 observers_.RemoveObserver(observer);
767 void Shell::UpdateShelfVisibility() {
768 RootWindowControllerList controllers = GetAllRootWindowControllers();
769 for (RootWindowControllerList::iterator iter = controllers.begin();
770 iter != controllers.end(); ++iter)
771 if ((*iter)->shelf())
772 (*iter)->UpdateShelfVisibility();
775 void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior,
776 aura::RootWindow* root_window) {
777 ash::internal::ShelfLayoutManager::ForLauncher(root_window)->
778 SetAutoHideBehavior(behavior);
781 ShelfAutoHideBehavior Shell::GetShelfAutoHideBehavior(
782 aura::RootWindow* root_window) const {
783 return ash::internal::ShelfLayoutManager::ForLauncher(root_window)->
784 auto_hide_behavior();
787 void Shell::SetShelfAlignment(ShelfAlignment alignment,
788 aura::RootWindow* root_window) {
789 if (ash::internal::ShelfLayoutManager::ForLauncher(root_window)->
790 SetAlignment(alignment)) {
791 FOR_EACH_OBSERVER(
792 ShellObserver, observers_, OnShelfAlignmentChanged(root_window));
796 ShelfAlignment Shell::GetShelfAlignment(aura::RootWindow* root_window) {
797 return GetRootWindowController(root_window)->
798 GetShelfLayoutManager()->GetAlignment();
801 void Shell::SetDimming(bool should_dim) {
802 RootWindowControllerList controllers = GetAllRootWindowControllers();
803 for (RootWindowControllerList::iterator iter = controllers.begin();
804 iter != controllers.end(); ++iter)
805 (*iter)->screen_dimmer()->SetDimming(should_dim);
808 void Shell::CreateModalBackground(aura::Window* window) {
809 if (!modality_filter_) {
810 modality_filter_.reset(new internal::SystemModalContainerEventFilter(this));
811 AddPreTargetHandler(modality_filter_.get());
813 RootWindowControllerList controllers = GetAllRootWindowControllers();
814 for (RootWindowControllerList::iterator iter = controllers.begin();
815 iter != controllers.end(); ++iter)
816 (*iter)->GetSystemModalLayoutManager(window)->CreateModalBackground();
819 void Shell::OnModalWindowRemoved(aura::Window* removed) {
820 RootWindowControllerList controllers = GetAllRootWindowControllers();
821 bool activated = false;
822 for (RootWindowControllerList::iterator iter = controllers.begin();
823 iter != controllers.end() && !activated; ++iter) {
824 activated = (*iter)->GetSystemModalLayoutManager(removed)->
825 ActivateNextModalWindow();
827 if (!activated) {
828 RemovePreTargetHandler(modality_filter_.get());
829 modality_filter_.reset();
830 for (RootWindowControllerList::iterator iter = controllers.begin();
831 iter != controllers.end(); ++iter)
832 (*iter)->GetSystemModalLayoutManager(removed)->DestroyModalBackground();
836 WebNotificationTray* Shell::GetWebNotificationTray() {
837 return GetPrimaryRootWindowController()->shelf()->
838 status_area_widget()->web_notification_tray();
841 bool Shell::HasPrimaryStatusArea() {
842 ShelfWidget* shelf = GetPrimaryRootWindowController()->shelf();
843 return shelf && shelf->status_area_widget();
846 SystemTray* Shell::GetPrimarySystemTray() {
847 return GetPrimaryRootWindowController()->GetSystemTray();
850 LauncherDelegate* Shell::GetLauncherDelegate() {
851 if (!launcher_delegate_) {
852 if (!launcher_model_)
853 launcher_model_.reset(new LauncherModel);
854 // Attempt to create the Launcher. This may fail if the application is not
855 // ready to create it yet, in which case the app is responsible for calling
856 // ash::Shell::CreateLauncher() when ready.
857 launcher_delegate_.reset(
858 delegate_->CreateLauncherDelegate(launcher_model_.get()));
860 return launcher_delegate_.get();
863 void Shell::SetTouchHudProjectionEnabled(bool enabled) {
864 if (is_touch_hud_projection_enabled_ == enabled)
865 return;
867 RootWindowList roots = GetInstance()->GetAllRootWindows();
868 for (RootWindowList::iterator iter = roots.begin(); iter != roots.end();
869 ++iter) {
870 internal::RootWindowController* controller = GetRootWindowController(*iter);
871 if (enabled)
872 controller->EnableTouchHudProjection();
873 else
874 controller->DisableTouchHudProjection();
876 is_touch_hud_projection_enabled_ = enabled;
879 void Shell::InitRootWindowForSecondaryDisplay(aura::RootWindow* root) {
880 aura::client::SetFocusClient(root, focus_client_.get());
881 internal::RootWindowController* controller =
882 new internal::RootWindowController(root);
883 controller->CreateContainers();
884 // Pass false for the |is_first_run_after_boot| parameter so we'll show a
885 // black background on this display instead of trying to mimic the boot splash
886 // screen.
887 controller->CreateSystemBackground(false);
888 InitRootWindowController(controller);
889 controller->InitForPrimaryDisplay();
890 controller->root_window_layout()->OnWindowResized();
891 desktop_background_controller_->OnRootWindowAdded(root);
892 high_contrast_controller_->OnRootWindowAdded(root);
893 root->ShowRootWindow();
894 // Activate new root for testing.
895 active_root_window_ = root;
898 void Shell::DoInitialWorkspaceAnimation() {
899 return GetPrimaryRootWindowController()->workspace_controller()->
900 DoInitialAnimation();
903 void Shell::InitRootWindowController(
904 internal::RootWindowController* controller) {
905 aura::RootWindow* root_window = controller->root_window();
906 DCHECK(activation_client_);
907 DCHECK(visibility_controller_.get());
908 DCHECK(drag_drop_controller_.get());
909 DCHECK(capture_controller_.get());
910 DCHECK(window_cycle_controller_.get());
912 aura::client::SetFocusClient(root_window, focus_client_.get());
913 input_method_filter_->SetInputMethodPropertyInRootWindow(root_window);
914 aura::client::SetActivationClient(root_window, activation_client_);
915 if (views::corewm::UseFocusController()) {
916 views::corewm::FocusController* controller =
917 static_cast<views::corewm::FocusController*>(activation_client_);
918 root_window->AddPreTargetHandler(controller);
920 aura::client::SetVisibilityClient(root_window, visibility_controller_.get());
921 aura::client::SetDragDropClient(root_window, drag_drop_controller_.get());
922 aura::client::SetCaptureClient(root_window, capture_controller_.get());
923 aura::client::SetScreenPositionClient(root_window,
924 screen_position_controller_.get());
925 aura::client::SetCursorClient(root_window, &cursor_manager_);
926 aura::client::SetTooltipClient(root_window, tooltip_controller_.get());
927 aura::client::SetEventClient(root_window, event_client_.get());
929 if (nested_dispatcher_controller_) {
930 aura::client::SetDispatcherClient(root_window,
931 nested_dispatcher_controller_.get());
933 if (user_action_client_)
934 aura::client::SetUserActionClient(root_window, user_action_client_.get());
936 root_window->SetCursor(ui::kCursorPointer);
937 controller->InitLayoutManagers();
938 controller->InitTouchHuds();
940 if (GetPrimaryRootWindowController()->GetSystemModalLayoutManager(NULL)->
941 has_modal_background()) {
942 controller->GetSystemModalLayoutManager(NULL)->CreateModalBackground();
945 window_cycle_controller_->OnRootWindowAdded(root_window);
948 ////////////////////////////////////////////////////////////////////////////////
949 // Shell, private:
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))
958 return true;
960 return false;
963 ////////////////////////////////////////////////////////////////////////////////
964 // Shell, ui::EventTarget overrides:
966 bool Shell::CanAcceptEvent(const ui::Event& event) {
967 return true;
970 ui::EventTarget* Shell::GetParentTarget() {
971 return NULL;
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) {
982 if (gained_active)
983 active_root_window_ = gained_active->GetRootWindow();
986 } // namespace ash