Revert 203708 "Disable ico_image_decoder_unittest until the bad ..."
[chromium-blink-merge.git] / ash / shell.cc
blobb9aa55e4a581196a2da4d17ea321c53204157c5e
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/always_on_top_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/capture_controller.h"
49 #include "ash/wm/coordinate_conversion.h"
50 #include "ash/wm/custom_frame_view_ash.h"
51 #include "ash/wm/event_client_impl.h"
52 #include "ash/wm/event_rewriter_event_filter.h"
53 #include "ash/wm/lock_state_controller.h"
54 #include "ash/wm/lock_state_controller_impl2.h"
55 #include "ash/wm/overlay_event_filter.h"
56 #include "ash/wm/power_button_controller.h"
57 #include "ash/wm/property_util.h"
58 #include "ash/wm/resize_shadow_controller.h"
59 #include "ash/wm/root_window_layout_manager.h"
60 #include "ash/wm/screen_dimmer.h"
61 #include "ash/wm/session_state_controller_impl.h"
62 #include "ash/wm/system_gesture_event_filter.h"
63 #include "ash/wm/system_modal_container_event_filter.h"
64 #include "ash/wm/system_modal_container_layout_manager.h"
65 #include "ash/wm/user_activity_detector.h"
66 #include "ash/wm/video_detector.h"
67 #include "ash/wm/window_animations.h"
68 #include "ash/wm/window_cycle_controller.h"
69 #include "ash/wm/window_properties.h"
70 #include "ash/wm/window_util.h"
71 #include "ash/wm/workspace_controller.h"
72 #include "base/bind.h"
73 #include "base/command_line.h"
74 #include "base/debug/leak_annotations.h"
75 #include "ui/aura/client/aura_constants.h"
76 #include "ui/aura/client/user_action_client.h"
77 #include "ui/aura/env.h"
78 #include "ui/aura/focus_manager.h"
79 #include "ui/aura/layout_manager.h"
80 #include "ui/aura/root_window.h"
81 #include "ui/aura/window.h"
82 #include "ui/base/ui_base_switches.h"
83 #include "ui/compositor/layer.h"
84 #include "ui/compositor/layer_animator.h"
85 #include "ui/gfx/display.h"
86 #include "ui/gfx/image/image_skia.h"
87 #include "ui/gfx/screen.h"
88 #include "ui/gfx/size.h"
89 #include "ui/keyboard/keyboard.h"
90 #include "ui/keyboard/keyboard_util.h"
91 #include "ui/message_center/message_center.h"
92 #include "ui/views/corewm/compound_event_filter.h"
93 #include "ui/views/corewm/corewm_switches.h"
94 #include "ui/views/corewm/focus_controller.h"
95 #include "ui/views/corewm/input_method_event_filter.h"
96 #include "ui/views/corewm/shadow_controller.h"
97 #include "ui/views/corewm/tooltip_controller.h"
98 #include "ui/views/corewm/visibility_controller.h"
99 #include "ui/views/corewm/window_modality_controller.h"
100 #include "ui/views/focus/focus_manager_factory.h"
101 #include "ui/views/widget/native_widget_aura.h"
102 #include "ui/views/widget/widget.h"
104 #if !defined(OS_MACOSX)
105 #include "ash/accelerators/accelerator_controller.h"
106 #include "ash/accelerators/accelerator_filter.h"
107 #include "ash/accelerators/nested_dispatcher_controller.h"
108 #endif
110 #if defined(OS_CHROMEOS) && 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_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(OS_CHROMEOS)
123 namespace ash {
125 namespace {
127 using aura::Window;
128 using views::Widget;
130 // This dummy class is used for shell unit tests. We dont have chrome delegate
131 // in these tests.
132 class DummyUserWallpaperDelegate : public UserWallpaperDelegate {
133 public:
134 DummyUserWallpaperDelegate() {}
136 virtual ~DummyUserWallpaperDelegate() {}
138 virtual int GetAnimationType() OVERRIDE {
139 return views::corewm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE;
142 virtual bool ShouldShowInitialAnimation() OVERRIDE {
143 return false;
146 virtual void UpdateWallpaper() OVERRIDE {
149 virtual void InitializeWallpaper() OVERRIDE {
150 ash::Shell::GetInstance()->desktop_background_controller()->
151 CreateEmptyWallpaper();
154 virtual void OpenSetWallpaperPage() OVERRIDE {
157 virtual bool CanOpenSetWallpaperPage() OVERRIDE {
158 return false;
161 virtual void OnWallpaperAnimationFinished() OVERRIDE {
164 virtual void OnWallpaperBootAnimationFinished() OVERRIDE {
167 private:
168 DISALLOW_COPY_AND_ASSIGN(DummyUserWallpaperDelegate);
171 // A Corewm VisibilityController subclass that calls the Ash animation routine
172 // so we can pick up our extended animations. See ash/wm/window_animations.h.
173 class AshVisibilityController : public views::corewm::VisibilityController {
174 public:
175 AshVisibilityController() {}
176 virtual ~AshVisibilityController() {}
178 private:
179 // Overridden from views::corewm::VisibilityController:
180 virtual bool CallAnimateOnChildWindowVisibilityChanged(
181 aura::Window* window,
182 bool visible) OVERRIDE {
183 return AnimateOnChildWindowVisibilityChanged(window, visible);
186 DISALLOW_COPY_AND_ASSIGN(AshVisibilityController);
189 } // namespace
191 // static
192 Shell* Shell::instance_ = NULL;
193 // static
194 bool Shell::initially_hide_cursor_ = false;
196 ////////////////////////////////////////////////////////////////////////////////
197 // Shell, public:
199 Shell::Shell(ShellDelegate* delegate)
200 : screen_(new ScreenAsh),
201 active_root_window_(NULL),
202 delegate_(delegate),
203 activation_client_(NULL),
204 #if defined(OS_CHROMEOS) && defined(USE_X11)
205 output_configurator_(new chromeos::OutputConfigurator()),
206 #endif // defined(OS_CHROMEOS)
207 native_cursor_manager_(new AshNativeCursorManager),
208 cursor_manager_(scoped_ptr<views::corewm::NativeCursorManager>(
209 native_cursor_manager_)),
210 browser_context_(NULL),
211 simulate_modal_window_open_for_testing_(false) {
212 DCHECK(delegate_.get());
213 display_manager_.reset(new internal::DisplayManager);
214 mirror_window_controller_.reset(new internal::MirrorWindowController);
216 ANNOTATE_LEAKING_OBJECT_PTR(screen_); // see crbug.com/156466
217 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_ALTERNATE, screen_);
218 if (!gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE))
219 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_);
220 display_controller_.reset(new DisplayController);
221 #if defined(OS_CHROMEOS) && defined(USE_X11)
222 bool is_panel_fitting_disabled =
223 content::GpuDataManager::GetInstance()->IsFeatureBlacklisted(
224 gpu::GPU_FEATURE_TYPE_PANEL_FITTING) ||
225 CommandLine::ForCurrentProcess()->HasSwitch(
226 ::switches::kDisablePanelFitting);
228 output_configurator_->Init(
229 !is_panel_fitting_disabled,
230 delegate_->IsFirstRunAfterBoot() ? kChromeOsBootColor : 0);
232 base::MessagePumpAuraX11::Current()->AddDispatcherForRootWindow(
233 output_configurator());
234 #endif // defined(OS_CHROMEOS)
235 AddPreTargetHandler(this);
238 Shell::~Shell() {
239 views::FocusManagerFactory::Install(NULL);
241 // Remove the focus from any window. This will prevent overhead and side
242 // effects (e.g. crashes) from changing focus during shutdown.
243 // See bug crbug.com/134502.
244 if (active_root_window_)
245 aura::client::GetFocusClient(active_root_window_)->FocusWindow(NULL);
247 // Please keep in same order as in Init() because it's easy to miss one.
248 RemovePreTargetHandler(user_activity_detector_.get());
249 RemovePreTargetHandler(event_rewriter_filter_.get());
250 RemovePreTargetHandler(overlay_filter_.get());
251 RemovePreTargetHandler(input_method_filter_.get());
252 RemovePreTargetHandler(window_modality_controller_.get());
253 if (mouse_cursor_filter_)
254 RemovePreTargetHandler(mouse_cursor_filter_.get());
255 RemovePreTargetHandler(system_gesture_filter_.get());
256 RemovePreTargetHandler(event_transformation_handler_.get());
257 #if !defined(OS_MACOSX)
258 RemovePreTargetHandler(accelerator_filter_.get());
259 #endif
261 // TooltipController is deleted with the Shell so removing its references.
262 RemovePreTargetHandler(tooltip_controller_.get());
264 // AppList needs to be released before shelf layout manager, which is
265 // destroyed with launcher container in the loop below. However, app list
266 // container is now on top of launcher container and released after it.
267 // TODO(xiyuan): Move it back when app list container is no longer needed.
268 app_list_controller_.reset();
270 // Destroy SystemTrayDelegate before destroying the status area(s).
271 system_tray_delegate_->Shutdown();
272 system_tray_delegate_.reset();
274 // Destroy all child windows including widgets.
275 display_controller_->CloseChildWindows();
277 // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems
278 // needs to remove observers from it.
279 system_tray_notifier_.reset();
281 // These need a valid Shell instance to clean up properly, so explicitly
282 // delete them before invalidating the instance.
283 // Alphabetical. TODO(oshima): sort.
284 drag_drop_controller_.reset();
285 magnification_controller_.reset();
286 partial_magnification_controller_.reset();
287 resize_shadow_controller_.reset();
288 shadow_controller_.reset();
289 tooltip_controller_.reset();
290 event_client_.reset();
291 window_cycle_controller_.reset();
292 capture_controller_.reset();
293 nested_dispatcher_controller_.reset();
294 user_action_client_.reset();
295 visibility_controller_.reset();
296 launcher_delegate_.reset();
297 launcher_model_.reset();
298 video_detector_.reset();
300 power_button_controller_.reset();
301 lock_state_controller_.reset();
303 mirror_window_controller_.reset();
305 // This also deletes all RootWindows. Note that we invoke Shutdown() on
306 // DisplayController before resetting |display_controller_|, since destruction
307 // of its owned RootWindowControllers relies on the value.
308 display_controller_->Shutdown();
309 display_controller_.reset();
310 screen_position_controller_.reset();
312 // Delete the activation controller after other controllers and launcher
313 // because they might have registered ActivationChangeObserver.
314 activation_controller_.reset();
316 #if defined(OS_CHROMEOS) && defined(USE_X11)
317 if (display_change_observer_)
318 output_configurator_->RemoveObserver(display_change_observer_.get());
319 if (output_configurator_animation_)
320 output_configurator_->RemoveObserver(output_configurator_animation_.get());
321 if (display_error_observer_)
322 output_configurator_->RemoveObserver(display_error_observer_.get());
323 base::MessagePumpAuraX11::Current()->RemoveDispatcherForRootWindow(
324 output_configurator());
325 display_change_observer_.reset();
326 #endif // defined(OS_CHROMEOS)
328 DCHECK(instance_ == this);
329 instance_ = NULL;
332 // static
333 Shell* Shell::CreateInstance(ShellDelegate* delegate) {
334 CHECK(!instance_);
335 instance_ = new Shell(delegate);
336 instance_->Init();
337 return instance_;
340 // static
341 Shell* Shell::GetInstance() {
342 DCHECK(instance_);
343 return instance_;
346 // static
347 bool Shell::HasInstance() {
348 return !!instance_;
351 // static
352 void Shell::DeleteInstance() {
353 delete instance_;
354 instance_ = NULL;
357 // static
358 internal::RootWindowController* Shell::GetPrimaryRootWindowController() {
359 return GetRootWindowController(GetPrimaryRootWindow());
362 // static
363 Shell::RootWindowControllerList Shell::GetAllRootWindowControllers() {
364 return Shell::GetInstance()->display_controller()->
365 GetAllRootWindowControllers();
368 // static
369 aura::RootWindow* Shell::GetPrimaryRootWindow() {
370 return GetInstance()->display_controller()->GetPrimaryRootWindow();
373 // static
374 aura::RootWindow* Shell::GetActiveRootWindow() {
375 return GetInstance()->active_root_window_;
378 // static
379 gfx::Screen* Shell::GetScreen() {
380 return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE);
383 // static
384 Shell::RootWindowList Shell::GetAllRootWindows() {
385 return Shell::GetInstance()->display_controller()->
386 GetAllRootWindows();
389 // static
390 aura::Window* Shell::GetContainer(aura::RootWindow* root_window,
391 int container_id) {
392 return root_window->GetChildById(container_id);
395 // static
396 const aura::Window* Shell::GetContainer(const aura::RootWindow* root_window,
397 int container_id) {
398 return root_window->GetChildById(container_id);
401 // static
402 std::vector<aura::Window*> Shell::GetContainersFromAllRootWindows(
403 int container_id,
404 aura::RootWindow* priority_root) {
405 std::vector<aura::Window*> containers;
406 RootWindowList root_windows = GetAllRootWindows();
407 for (RootWindowList::const_iterator it = root_windows.begin();
408 it != root_windows.end(); ++it) {
409 aura::Window* container = (*it)->GetChildById(container_id);
410 if (container) {
411 if (priority_root && priority_root->Contains(container))
412 containers.insert(containers.begin(), container);
413 else
414 containers.push_back(container);
417 return containers;
420 // static
421 bool Shell::IsForcedMaximizeMode() {
422 CommandLine* command_line = CommandLine::ForCurrentProcess();
423 return command_line->HasSwitch(switches::kForcedMaximizeMode);
426 void Shell::Init() {
427 CommandLine* command_line = CommandLine::ForCurrentProcess();
429 delegate_->PreInit();
430 #if defined(OS_CHROMEOS) && defined(USE_X11)
431 output_configurator_animation_.reset(
432 new internal::OutputConfiguratorAnimation());
433 output_configurator_->AddObserver(output_configurator_animation_.get());
434 if (base::chromeos::IsRunningOnChromeOS()) {
435 display_change_observer_.reset(new internal::DisplayChangeObserverX11);
436 // Register |display_change_observer_| first so that the rest of
437 // observer gets invoked after the root windows are configured.
438 output_configurator_->AddObserver(display_change_observer_.get());
439 display_error_observer_.reset(new internal::DisplayErrorObserver());
440 output_configurator_->AddObserver(display_error_observer_.get());
441 output_configurator_->set_state_controller(display_change_observer_.get());
442 if (command_line->HasSwitch(ash::switches::kAshEnableSoftwareMirroring))
443 output_configurator_->set_mirroring_controller(display_manager_.get());
444 output_configurator_->Start();
445 display_change_observer_->OnDisplayModeChanged();
447 #endif
449 // Install the custom factory first so that views::FocusManagers for Tray,
450 // Launcher, and WallPaper could be created by the factory.
451 views::FocusManagerFactory::Install(new AshFocusManagerFactory);
453 env_filter_.reset(new views::corewm::CompoundEventFilter);
454 AddPreTargetHandler(env_filter_.get());
456 // Env creates the compositor. Historically it seems to have been implicitly
457 // initialized first by the ActivationController, but now that FocusController
458 // no longer does this we need to do it explicitly.
459 aura::Env::GetInstance();
460 if (views::corewm::UseFocusController()) {
461 views::corewm::FocusController* focus_controller =
462 new views::corewm::FocusController(new wm::AshFocusRules);
463 focus_client_.reset(focus_controller);
464 activation_client_ = focus_controller;
465 activation_client_->AddObserver(this);
466 } else {
467 focus_client_.reset(new aura::FocusManager);
468 activation_controller_.reset(
469 new internal::ActivationController(
470 focus_client_.get(),
471 new internal::AshActivationController));
472 activation_client_ = activation_controller_.get();
473 AddPreTargetHandler(activation_controller_.get());
476 focus_cycler_.reset(new internal::FocusCycler());
478 screen_position_controller_.reset(new internal::ScreenPositionController);
479 root_window_host_factory_.reset(delegate_->CreateRootWindowHostFactory());
481 display_controller_->Start();
482 display_controller_->InitPrimaryDisplay();
483 aura::RootWindow* root_window = display_controller_->GetPrimaryRootWindow();
484 active_root_window_ = root_window;
486 cursor_manager_.SetDisplay(DisplayController::GetPrimaryDisplay());
488 #if !defined(OS_MACOSX)
489 nested_dispatcher_controller_.reset(new NestedDispatcherController);
490 accelerator_controller_.reset(new AcceleratorController);
491 #endif
493 // The order in which event filters are added is significant.
494 user_activity_detector_.reset(new UserActivityDetector);
495 AddPreTargetHandler(user_activity_detector_.get());
497 event_rewriter_filter_.reset(new internal::EventRewriterEventFilter);
498 AddPreTargetHandler(event_rewriter_filter_.get());
500 overlay_filter_.reset(new internal::OverlayEventFilter);
501 AddPreTargetHandler(overlay_filter_.get());
502 AddShellObserver(overlay_filter_.get());
504 input_method_filter_.reset(new views::corewm::InputMethodEventFilter(
505 root_window->GetAcceleratedWidget()));
506 AddPreTargetHandler(input_method_filter_.get());
508 #if !defined(OS_MACOSX)
509 accelerator_filter_.reset(new internal::AcceleratorFilter);
510 AddPreTargetHandler(accelerator_filter_.get());
511 #endif
513 event_transformation_handler_.reset(new internal::EventTransformationHandler);
514 AddPreTargetHandler(event_transformation_handler_.get());
516 system_gesture_filter_.reset(new internal::SystemGestureEventFilter);
517 AddPreTargetHandler(system_gesture_filter_.get());
519 capture_controller_.reset(new internal::CaptureController);
521 // The keyboard system must be initialized before the RootWindowController is
522 // created.
523 if (keyboard::IsKeyboardEnabled())
524 keyboard::InitializeKeyboard();
526 internal::RootWindowController* root_window_controller =
527 new internal::RootWindowController(root_window);
528 root_window_controller->CreateContainers();
529 root_window_controller->CreateSystemBackground(
530 delegate_->IsFirstRunAfterBoot());
532 if (command_line->HasSwitch(ash::switches::kAshDisableNewLockAnimations))
533 lock_state_controller_.reset(new SessionStateControllerImpl);
534 else
535 lock_state_controller_.reset(new LockStateControllerImpl2);
536 power_button_controller_.reset(new PowerButtonController(
537 lock_state_controller_.get()));
538 AddShellObserver(lock_state_controller_.get());
540 mouse_cursor_filter_.reset(new internal::MouseCursorEventFilter());
541 AddPreTargetHandler(mouse_cursor_filter_.get());
543 // Create Controllers that may need root window.
544 // TODO(oshima): Move as many controllers before creating
545 // RootWindowController as possible.
546 visibility_controller_.reset(new AshVisibilityController);
547 drag_drop_controller_.reset(new internal::DragDropController);
548 user_action_client_.reset(delegate_->CreateUserActionClient());
549 window_modality_controller_.reset(
550 new views::corewm::WindowModalityController);
551 AddPreTargetHandler(window_modality_controller_.get());
553 magnification_controller_.reset(
554 MagnificationController::CreateInstance());
556 partial_magnification_controller_.reset(
557 new PartialMagnificationController());
559 high_contrast_controller_.reset(new HighContrastController);
560 video_detector_.reset(new VideoDetector);
561 window_cycle_controller_.reset(new WindowCycleController(activation_client_));
563 tooltip_controller_.reset(new views::corewm::TooltipController(
564 gfx::SCREEN_TYPE_ALTERNATE));
565 AddPreTargetHandler(tooltip_controller_.get());
567 event_client_.reset(new internal::EventClientImpl);
569 InitRootWindowController(root_window_controller);
571 // This controller needs to be set before SetupManagedWindowMode.
572 desktop_background_controller_.reset(new DesktopBackgroundController());
573 user_wallpaper_delegate_.reset(delegate_->CreateUserWallpaperDelegate());
574 if (!user_wallpaper_delegate_)
575 user_wallpaper_delegate_.reset(new DummyUserWallpaperDelegate());
577 // StatusAreaWidget uses Shell's CapsLockDelegate.
578 caps_lock_delegate_.reset(delegate_->CreateCapsLockDelegate());
580 session_state_delegate_.reset(delegate_->CreateSessionStateDelegate());
582 if (!command_line->HasSwitch(views::corewm::switches::kNoDropShadows)) {
583 resize_shadow_controller_.reset(new internal::ResizeShadowController());
584 shadow_controller_.reset(
585 new views::corewm::ShadowController(activation_client_));
588 // Create system_tray_notifier_ before the delegate.
589 system_tray_notifier_.reset(new ash::SystemTrayNotifier());
591 // Initialize system_tray_delegate_ before initializing StatusAreaWidget.
592 system_tray_delegate_.reset(delegate()->CreateSystemTrayDelegate());
593 if (!system_tray_delegate_)
594 system_tray_delegate_.reset(SystemTrayDelegate::CreateDummyDelegate());
596 // Creates StatusAreaWidget.
597 root_window_controller->InitForPrimaryDisplay();
599 // Initialize system_tray_delegate_ after StatusAreaWidget is created.
600 system_tray_delegate_->Initialize();
602 display_controller_->InitSecondaryDisplays();
604 // Force Layout
605 root_window_controller->root_window_layout()->OnWindowResized();
607 // It needs to be created after OnWindowResized has been called, otherwise the
608 // widget will not paint when restoring after a browser crash. Also it needs
609 // to be created after InitSecondaryDisplays() to initialize the wallpapers in
610 // the correct size.
611 user_wallpaper_delegate_->InitializeWallpaper();
613 if (initially_hide_cursor_)
614 cursor_manager_.HideCursor();
615 cursor_manager_.SetCursor(ui::kCursorPointer);
617 if (!cursor_manager_.IsCursorVisible()) {
618 // Cursor might have been hidden by something other than chrome.
619 // Let the first mouse event show the cursor.
620 env_filter_->set_cursor_hidden_by_filter(true);
624 void Shell::ShowContextMenu(const gfx::Point& location_in_screen) {
625 // No context menus if there is no session with an active user.
626 if (!session_state_delegate_->NumberOfLoggedInUsers())
627 return;
628 // No context menus when screen is locked.
629 if (session_state_delegate_->IsScreenLocked())
630 return;
632 aura::RootWindow* root =
633 wm::GetRootWindowMatching(gfx::Rect(location_in_screen, gfx::Size()));
634 // TODO(oshima): The root and root window controller shouldn't be
635 // NULL even for the out-of-bounds |location_in_screen| (It should
636 // return the primary root). Investigate why/how this is
637 // happening. crbug.com/165214.
638 internal::RootWindowController* rwc = GetRootWindowController(root);
639 CHECK(rwc) << "root=" << root
640 << ", location:" << location_in_screen.ToString();
641 if (rwc)
642 rwc->ShowContextMenu(location_in_screen);
645 void Shell::ToggleAppList(aura::Window* window) {
646 // If the context window is not given, show it on the active root window.
647 if (!window)
648 window = GetActiveRootWindow();
649 if (!app_list_controller_)
650 app_list_controller_.reset(new internal::AppListController);
651 app_list_controller_->SetVisible(!app_list_controller_->IsVisible(), window);
654 void Shell::SetDragAndDropHostOfCurrentAppList(
655 app_list::ApplicationDragAndDropHost* drag_and_drop_host) {
656 if (app_list_controller_.get())
657 app_list_controller_->SetDragAndDropHostOfCurrentAppList(
658 drag_and_drop_host);
661 bool Shell::GetAppListTargetVisibility() const {
662 return app_list_controller_.get() &&
663 app_list_controller_->GetTargetVisibility();
666 aura::Window* Shell::GetAppListWindow() {
667 return app_list_controller_.get() ? app_list_controller_->GetWindow() : NULL;
670 bool Shell::IsSystemModalWindowOpen() const {
671 if (simulate_modal_window_open_for_testing_)
672 return true;
673 const std::vector<aura::Window*> containers = GetContainersFromAllRootWindows(
674 internal::kShellWindowId_SystemModalContainer, NULL);
675 for (std::vector<aura::Window*>::const_iterator cit = containers.begin();
676 cit != containers.end(); ++cit) {
677 for (aura::Window::Windows::const_iterator wit = (*cit)->children().begin();
678 wit != (*cit)->children().end(); ++wit) {
679 if ((*wit)->GetProperty(aura::client::kModalKey) ==
680 ui::MODAL_TYPE_SYSTEM && (*wit)->TargetVisibility()) {
681 return true;
685 return false;
688 views::NonClientFrameView* Shell::CreateDefaultNonClientFrameView(
689 views::Widget* widget) {
690 // Use translucent-style window frames for dialogs.
691 CustomFrameViewAsh* frame_view = new CustomFrameViewAsh;
692 frame_view->Init(widget);
693 return frame_view;
696 void Shell::RotateFocus(Direction direction) {
697 focus_cycler_->RotateFocus(
698 direction == FORWARD ? internal::FocusCycler::FORWARD :
699 internal::FocusCycler::BACKWARD);
702 void Shell::SetDisplayWorkAreaInsets(Window* contains,
703 const gfx::Insets& insets) {
704 if (!display_manager_->UpdateWorkAreaOfDisplayNearestWindow(contains, insets))
705 return;
706 FOR_EACH_OBSERVER(ShellObserver, observers_,
707 OnDisplayWorkAreaInsetsChanged());
710 void Shell::OnLoginStateChanged(user::LoginStatus status) {
711 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLoginStateChanged(status));
712 RootWindowControllerList controllers = GetAllRootWindowControllers();
713 for (RootWindowControllerList::iterator iter = controllers.begin();
714 iter != controllers.end(); ++iter)
715 (*iter)->OnLoginStateChanged(status);
718 void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status) {
719 RootWindowControllerList controllers = GetAllRootWindowControllers();
720 for (RootWindowControllerList::iterator iter = controllers.begin();
721 iter != controllers.end(); ++iter)
722 (*iter)->UpdateAfterLoginStatusChange(status);
725 void Shell::OnAppTerminating() {
726 FOR_EACH_OBSERVER(ShellObserver, observers_, OnAppTerminating());
729 void Shell::OnLockStateChanged(bool locked) {
730 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLockStateChanged(locked));
733 void Shell::CreateLauncher() {
734 RootWindowControllerList controllers = GetAllRootWindowControllers();
735 for (RootWindowControllerList::iterator iter = controllers.begin();
736 iter != controllers.end(); ++iter)
737 (*iter)->shelf()->CreateLauncher();
740 void Shell::ShowLauncher() {
741 RootWindowControllerList controllers = GetAllRootWindowControllers();
742 for (RootWindowControllerList::iterator iter = controllers.begin();
743 iter != controllers.end(); ++iter)
744 (*iter)->ShowLauncher();
747 void Shell::AddShellObserver(ShellObserver* observer) {
748 observers_.AddObserver(observer);
751 void Shell::RemoveShellObserver(ShellObserver* observer) {
752 observers_.RemoveObserver(observer);
755 void Shell::UpdateShelfVisibility() {
756 RootWindowControllerList controllers = GetAllRootWindowControllers();
757 for (RootWindowControllerList::iterator iter = controllers.begin();
758 iter != controllers.end(); ++iter)
759 if ((*iter)->shelf())
760 (*iter)->UpdateShelfVisibility();
763 void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior,
764 aura::RootWindow* root_window) {
765 ash::internal::ShelfLayoutManager::ForLauncher(root_window)->
766 SetAutoHideBehavior(behavior);
769 ShelfAutoHideBehavior Shell::GetShelfAutoHideBehavior(
770 aura::RootWindow* root_window) const {
771 return ash::internal::ShelfLayoutManager::ForLauncher(root_window)->
772 auto_hide_behavior();
775 void Shell::SetShelfAlignment(ShelfAlignment alignment,
776 aura::RootWindow* root_window) {
777 if (ash::internal::ShelfLayoutManager::ForLauncher(root_window)->
778 SetAlignment(alignment)) {
779 FOR_EACH_OBSERVER(
780 ShellObserver, observers_, OnShelfAlignmentChanged(root_window));
784 ShelfAlignment Shell::GetShelfAlignment(aura::RootWindow* root_window) {
785 return GetRootWindowController(root_window)->
786 GetShelfLayoutManager()->GetAlignment();
789 void Shell::SetDimming(bool should_dim) {
790 RootWindowControllerList controllers = GetAllRootWindowControllers();
791 for (RootWindowControllerList::iterator iter = controllers.begin();
792 iter != controllers.end(); ++iter)
793 (*iter)->screen_dimmer()->SetDimming(should_dim);
796 void Shell::CreateModalBackground(aura::Window* window) {
797 if (!modality_filter_) {
798 modality_filter_.reset(new internal::SystemModalContainerEventFilter(this));
799 AddPreTargetHandler(modality_filter_.get());
801 RootWindowControllerList controllers = GetAllRootWindowControllers();
802 for (RootWindowControllerList::iterator iter = controllers.begin();
803 iter != controllers.end(); ++iter)
804 (*iter)->GetSystemModalLayoutManager(window)->CreateModalBackground();
807 void Shell::OnModalWindowRemoved(aura::Window* removed) {
808 RootWindowControllerList controllers = GetAllRootWindowControllers();
809 bool activated = false;
810 for (RootWindowControllerList::iterator iter = controllers.begin();
811 iter != controllers.end() && !activated; ++iter) {
812 activated = (*iter)->GetSystemModalLayoutManager(removed)->
813 ActivateNextModalWindow();
815 if (!activated) {
816 RemovePreTargetHandler(modality_filter_.get());
817 modality_filter_.reset();
818 for (RootWindowControllerList::iterator iter = controllers.begin();
819 iter != controllers.end(); ++iter)
820 (*iter)->GetSystemModalLayoutManager(removed)->DestroyModalBackground();
824 WebNotificationTray* Shell::GetWebNotificationTray() {
825 return GetPrimaryRootWindowController()->shelf()->
826 status_area_widget()->web_notification_tray();
829 bool Shell::HasPrimaryStatusArea() {
830 ShelfWidget* shelf = GetPrimaryRootWindowController()->shelf();
831 return shelf && shelf->status_area_widget();
834 SystemTray* Shell::GetPrimarySystemTray() {
835 return GetPrimaryRootWindowController()->GetSystemTray();
838 LauncherDelegate* Shell::GetLauncherDelegate() {
839 if (!launcher_delegate_) {
840 if (!launcher_model_)
841 launcher_model_.reset(new LauncherModel);
842 // Attempt to create the Launcher. This may fail if the application is not
843 // ready to create it yet, in which case the app is responsible for calling
844 // ash::Shell::CreateLauncher() when ready.
845 launcher_delegate_.reset(
846 delegate_->CreateLauncherDelegate(launcher_model_.get()));
848 return launcher_delegate_.get();
851 void Shell::InitRootWindowForSecondaryDisplay(aura::RootWindow* root) {
852 aura::client::SetFocusClient(root, focus_client_.get());
853 internal::RootWindowController* controller =
854 new internal::RootWindowController(root);
855 controller->CreateContainers();
856 // Pass false for the |is_first_run_after_boot| parameter so we'll show a
857 // black background on this display instead of trying to mimic the boot splash
858 // screen.
859 controller->CreateSystemBackground(false);
860 InitRootWindowController(controller);
861 controller->InitForPrimaryDisplay();
862 controller->root_window_layout()->OnWindowResized();
863 desktop_background_controller_->OnRootWindowAdded(root);
864 high_contrast_controller_->OnRootWindowAdded(root);
865 root->ShowRootWindow();
866 // Activate new root for testing.
867 active_root_window_ = root;
870 void Shell::DoInitialWorkspaceAnimation() {
871 return GetPrimaryRootWindowController()->workspace_controller()->
872 DoInitialAnimation();
875 void Shell::InitRootWindowController(
876 internal::RootWindowController* controller) {
877 aura::RootWindow* root_window = controller->root_window();
878 DCHECK(activation_client_);
879 DCHECK(visibility_controller_.get());
880 DCHECK(drag_drop_controller_.get());
881 DCHECK(capture_controller_.get());
882 DCHECK(window_cycle_controller_.get());
884 aura::client::SetFocusClient(root_window, focus_client_.get());
885 input_method_filter_->SetInputMethodPropertyInRootWindow(root_window);
886 aura::client::SetActivationClient(root_window, activation_client_);
887 if (views::corewm::UseFocusController()) {
888 views::corewm::FocusController* controller =
889 static_cast<views::corewm::FocusController*>(activation_client_);
890 root_window->AddPreTargetHandler(controller);
892 aura::client::SetVisibilityClient(root_window, visibility_controller_.get());
893 aura::client::SetDragDropClient(root_window, drag_drop_controller_.get());
894 aura::client::SetCaptureClient(root_window, capture_controller_.get());
895 aura::client::SetScreenPositionClient(root_window,
896 screen_position_controller_.get());
897 aura::client::SetCursorClient(root_window, &cursor_manager_);
898 aura::client::SetTooltipClient(root_window, tooltip_controller_.get());
899 aura::client::SetEventClient(root_window, event_client_.get());
901 if (nested_dispatcher_controller_) {
902 aura::client::SetDispatcherClient(root_window,
903 nested_dispatcher_controller_.get());
905 if (user_action_client_)
906 aura::client::SetUserActionClient(root_window, user_action_client_.get());
908 root_window->SetCursor(ui::kCursorPointer);
909 controller->InitLayoutManagers();
911 // TODO(oshima): Move the instance to RootWindowController when
912 // the extended desktop is enabled by default.
913 internal::AlwaysOnTopController* always_on_top_controller =
914 new internal::AlwaysOnTopController;
915 always_on_top_controller->SetAlwaysOnTopContainer(
916 root_window->GetChildById(internal::kShellWindowId_AlwaysOnTopContainer));
917 root_window->SetProperty(internal::kAlwaysOnTopControllerKey,
918 always_on_top_controller);
919 if (GetPrimaryRootWindowController()->GetSystemModalLayoutManager(NULL)->
920 has_modal_background()) {
921 controller->GetSystemModalLayoutManager(NULL)->CreateModalBackground();
924 window_cycle_controller_->OnRootWindowAdded(root_window);
927 ////////////////////////////////////////////////////////////////////////////////
928 // Shell, private:
930 bool Shell::CanWindowReceiveEvents(aura::Window* window) {
931 RootWindowControllerList controllers = GetAllRootWindowControllers();
932 for (RootWindowControllerList::iterator iter = controllers.begin();
933 iter != controllers.end(); ++iter) {
934 internal::SystemModalContainerLayoutManager* layout_manager =
935 (*iter)->GetSystemModalLayoutManager(window);
936 if (layout_manager && layout_manager->CanWindowReceiveEvents(window))
937 return true;
939 return false;
942 ////////////////////////////////////////////////////////////////////////////////
943 // Shell, ui::EventTarget overrides:
945 bool Shell::CanAcceptEvent(const ui::Event& event) {
946 return true;
949 ui::EventTarget* Shell::GetParentTarget() {
950 return NULL;
953 void Shell::OnEvent(ui::Event* event) {
956 ////////////////////////////////////////////////////////////////////////////////
957 // Shell, aura::client::ActivationChangeObserver implementation:
959 void Shell::OnWindowActivated(aura::Window* gained_active,
960 aura::Window* lost_active) {
961 if (gained_active)
962 active_root_window_ = gained_active->GetRootWindow();
965 } // namespace ash