By moving the call to Load() up in SearchProvider::Start(), we are giving a chance...
[chromium-blink-merge.git] / ash / shell.cc
blob7c940d4198d00bc29e1a2ac66d01f46963749b06
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/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/shell_delegate.h"
32 #include "ash/shell_factory.h"
33 #include "ash/shell_window_ids.h"
34 #include "ash/system/status_area_widget.h"
35 #include "ash/system/tray/system_tray_delegate.h"
36 #include "ash/system/tray/system_tray_notifier.h"
37 #include "ash/tooltips/tooltip_controller.h"
38 #include "ash/touch/touch_observer_hud.h"
39 #include "ash/wm/activation_controller.h"
40 #include "ash/wm/always_on_top_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/base_layout_manager.h"
45 #include "ash/wm/capture_controller.h"
46 #include "ash/wm/coordinate_conversion.h"
47 #include "ash/wm/custom_frame_view_ash.h"
48 #include "ash/wm/event_client_impl.h"
49 #include "ash/wm/event_rewriter_event_filter.h"
50 #include "ash/wm/overlay_event_filter.h"
51 #include "ash/wm/power_button_controller.h"
52 #include "ash/wm/property_util.h"
53 #include "ash/wm/resize_shadow_controller.h"
54 #include "ash/wm/root_window_layout_manager.h"
55 #include "ash/wm/screen_dimmer.h"
56 #include "ash/wm/session_state_controller.h"
57 #include "ash/wm/session_state_controller_impl.h"
58 #include "ash/wm/session_state_controller_impl2.h"
59 #include "ash/wm/system_gesture_event_filter.h"
60 #include "ash/wm/system_modal_container_event_filter.h"
61 #include "ash/wm/system_modal_container_layout_manager.h"
62 #include "ash/wm/user_activity_detector.h"
63 #include "ash/wm/video_detector.h"
64 #include "ash/wm/window_animations.h"
65 #include "ash/wm/window_cycle_controller.h"
66 #include "ash/wm/window_properties.h"
67 #include "ash/wm/window_util.h"
68 #include "ash/wm/workspace_controller.h"
69 #include "base/bind.h"
70 #include "base/command_line.h"
71 #include "base/debug/leak_annotations.h"
72 #include "ui/aura/client/aura_constants.h"
73 #include "ui/aura/client/user_action_client.h"
74 #include "ui/aura/env.h"
75 #include "ui/aura/focus_manager.h"
76 #include "ui/aura/layout_manager.h"
77 #include "ui/aura/root_window.h"
78 #include "ui/aura/window.h"
79 #include "ui/base/ui_base_switches.h"
80 #include "ui/compositor/layer.h"
81 #include "ui/compositor/layer_animator.h"
82 #include "ui/gfx/display.h"
83 #include "ui/gfx/image/image_skia.h"
84 #include "ui/gfx/screen.h"
85 #include "ui/gfx/size.h"
86 #include "ui/views/corewm/compound_event_filter.h"
87 #include "ui/views/corewm/corewm_switches.h"
88 #include "ui/views/corewm/focus_controller.h"
89 #include "ui/views/corewm/input_method_event_filter.h"
90 #include "ui/views/corewm/shadow_controller.h"
91 #include "ui/views/corewm/visibility_controller.h"
92 #include "ui/views/corewm/window_modality_controller.h"
93 #include "ui/views/focus/focus_manager_factory.h"
94 #include "ui/views/widget/native_widget_aura.h"
95 #include "ui/views/widget/widget.h"
96 #include "ui/views/window/dialog_frame_view.h"
98 #if !defined(OS_MACOSX)
99 #include "ash/accelerators/accelerator_controller.h"
100 #include "ash/accelerators/accelerator_filter.h"
101 #include "ash/accelerators/nested_dispatcher_controller.h"
102 #endif
104 #if defined(OS_CHROMEOS)
105 #include "ash/display/display_change_observer_x11.h"
106 #include "ash/display/output_configurator_animation.h"
107 #include "base/chromeos/chromeos_version.h"
108 #include "base/message_pump_aurax11.h"
109 #include "chromeos/display/output_configurator.h"
110 #include "content/public/browser/gpu_data_manager.h"
111 #include "content/public/common/gpu_feature_type.h"
112 #endif // defined(OS_CHROMEOS)
114 namespace ash {
116 namespace {
118 using aura::Window;
119 using views::Widget;
121 // This dummy class is used for shell unit tests. We dont have chrome delegate
122 // in these tests.
123 class DummyUserWallpaperDelegate : public UserWallpaperDelegate {
124 public:
125 DummyUserWallpaperDelegate() {}
127 virtual ~DummyUserWallpaperDelegate() {}
129 virtual int GetAnimationType() OVERRIDE {
130 return views::corewm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE;
133 virtual bool ShouldShowInitialAnimation() OVERRIDE {
134 return false;
137 virtual void UpdateWallpaper() OVERRIDE {
140 virtual void InitializeWallpaper() OVERRIDE {
141 ash::Shell::GetInstance()->desktop_background_controller()->
142 CreateEmptyWallpaper();
145 virtual void OpenSetWallpaperPage() OVERRIDE {
148 virtual bool CanOpenSetWallpaperPage() OVERRIDE {
149 return false;
152 virtual void OnWallpaperAnimationFinished() OVERRIDE {
155 virtual void OnWallpaperBootAnimationFinished() OVERRIDE {
158 private:
159 DISALLOW_COPY_AND_ASSIGN(DummyUserWallpaperDelegate);
162 // A Corewm VisibilityController subclass that calls the Ash animation routine
163 // so we can pick up our extended animations. See ash/wm/window_animations.h.
164 class AshVisibilityController : public views::corewm::VisibilityController {
165 public:
166 AshVisibilityController() {}
167 virtual ~AshVisibilityController() {}
169 private:
170 // Overridden from views::corewm::VisibilityController:
171 virtual bool CallAnimateOnChildWindowVisibilityChanged(
172 aura::Window* window,
173 bool visible) OVERRIDE {
174 return AnimateOnChildWindowVisibilityChanged(window, visible);
177 DISALLOW_COPY_AND_ASSIGN(AshVisibilityController);
180 } // namespace
182 // static
183 Shell* Shell::instance_ = NULL;
184 // static
185 bool Shell::initially_hide_cursor_ = false;
187 ////////////////////////////////////////////////////////////////////////////////
188 // Shell, public:
190 Shell::Shell(ShellDelegate* delegate)
191 : screen_(new ScreenAsh),
192 active_root_window_(NULL),
193 delegate_(delegate),
194 activation_client_(NULL),
195 #if defined(OS_CHROMEOS)
196 output_configurator_(new chromeos::OutputConfigurator()),
197 output_configurator_animation_(
198 new internal::OutputConfiguratorAnimation()),
199 #endif // defined(OS_CHROMEOS)
200 browser_context_(NULL),
201 simulate_modal_window_open_for_testing_(false) {
202 DCHECK(delegate_.get());
203 display_manager_.reset(new internal::DisplayManager);
204 ANNOTATE_LEAKING_OBJECT_PTR(screen_); // see crbug.com/156466
205 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_ALTERNATE, screen_);
206 if (!gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE))
207 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_);
208 #if defined(OS_CHROMEOS)
209 content::GpuFeatureType blacklisted_features =
210 content::GpuDataManager::GetInstance()->GetBlacklistedFeatures();
211 bool is_panel_fitting_disabled =
212 (blacklisted_features & content::GPU_FEATURE_TYPE_PANEL_FITTING) ||
213 CommandLine::ForCurrentProcess()->HasSwitch(
214 switches::kAshDisablePanelFitting);
215 output_configurator_->Init(!is_panel_fitting_disabled);
217 output_configurator_->AddObserver(output_configurator_animation_.get());
218 base::MessagePumpAuraX11::Current()->AddDispatcherForRootWindow(
219 output_configurator());
220 #endif // defined(OS_CHROMEOS)
221 AddPreTargetHandler(this);
224 Shell::~Shell() {
225 views::FocusManagerFactory::Install(NULL);
227 // Remove the focus from any window. This will prevent overhead and side
228 // effects (e.g. crashes) from changing focus during shutdown.
229 // See bug crbug.com/134502.
230 if (active_root_window_)
231 aura::client::GetFocusClient(active_root_window_)->FocusWindow(NULL);
233 // Please keep in same order as in Init() because it's easy to miss one.
234 RemovePreTargetHandler(user_activity_detector_.get());
235 RemovePreTargetHandler(event_rewriter_filter_.get());
236 RemovePreTargetHandler(overlay_filter_.get());
237 RemovePreTargetHandler(input_method_filter_.get());
238 RemovePreTargetHandler(window_modality_controller_.get());
239 if (mouse_cursor_filter_.get())
240 RemovePreTargetHandler(mouse_cursor_filter_.get());
241 RemovePreTargetHandler(system_gesture_filter_.get());
242 #if !defined(OS_MACOSX)
243 RemovePreTargetHandler(accelerator_filter_.get());
244 #endif
245 if (touch_observer_hud_.get())
246 RemovePreTargetHandler(touch_observer_hud_.get());
248 // TooltipController is deleted with the Shell so removing its references.
249 RemovePreTargetHandler(tooltip_controller_.get());
251 // AppList needs to be released before shelf layout manager, which is
252 // destroyed with launcher container in the loop below. However, app list
253 // container is now on top of launcher container and released after it.
254 // TODO(xiyuan): Move it back when app list container is no longer needed.
255 app_list_controller_.reset();
257 // Destroy SystemTrayDelegate before destroying the status area(s).
258 system_tray_delegate_.reset();
260 // Destroy all child windows including widgets.
261 display_controller_->CloseChildWindows();
263 // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems
264 // needs to remove observers from it.
265 system_tray_notifier_.reset();
267 // These need a valid Shell instance to clean up properly, so explicitly
268 // delete them before invalidating the instance.
269 // Alphabetical. TODO(oshima): sort.
270 drag_drop_controller_.reset();
271 magnification_controller_.reset();
272 partial_magnification_controller_.reset();
273 resize_shadow_controller_.reset();
274 shadow_controller_.reset();
275 tooltip_controller_.reset();
276 event_client_.reset();
277 window_cycle_controller_.reset();
278 capture_controller_.reset();
279 nested_dispatcher_controller_.reset();
280 user_action_client_.reset();
281 visibility_controller_.reset();
282 launcher_delegate_.reset();
283 launcher_model_.reset();
285 power_button_controller_.reset();
286 session_state_controller_.reset();
288 // This also deletes all RootWindows.
289 display_controller_.reset();
290 screen_position_controller_.reset();
292 // Delete the activation controller after other controllers and launcher
293 // because they might have registered ActivationChangeObserver.
294 activation_controller_.reset();
296 DCHECK(instance_ == this);
297 instance_ = NULL;
299 #if defined(OS_CHROMEOS)
300 output_configurator_->RemoveObserver(output_configurator_animation_.get());
301 base::MessagePumpAuraX11::Current()->RemoveDispatcherForRootWindow(
302 output_configurator());
303 #endif // defined(OS_CHROMEOS)
306 // static
307 Shell* Shell::CreateInstance(ShellDelegate* delegate) {
308 CHECK(!instance_);
309 instance_ = new Shell(delegate);
310 instance_->Init();
311 return instance_;
314 // static
315 Shell* Shell::GetInstance() {
316 DCHECK(instance_);
317 return instance_;
320 // static
321 bool Shell::HasInstance() {
322 return !!instance_;
325 // static
326 void Shell::DeleteInstance() {
327 delete instance_;
328 instance_ = NULL;
331 // static
332 internal::RootWindowController* Shell::GetPrimaryRootWindowController() {
333 return GetRootWindowController(GetPrimaryRootWindow());
336 // static
337 Shell::RootWindowControllerList Shell::GetAllRootWindowControllers() {
338 return Shell::GetInstance()->display_controller()->
339 GetAllRootWindowControllers();
342 // static
343 aura::RootWindow* Shell::GetPrimaryRootWindow() {
344 return GetInstance()->display_controller()->GetPrimaryRootWindow();
347 // static
348 aura::RootWindow* Shell::GetActiveRootWindow() {
349 return GetInstance()->active_root_window_;
352 // static
353 gfx::Screen* Shell::GetScreen() {
354 return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE);
357 // static
358 Shell::RootWindowList Shell::GetAllRootWindows() {
359 return Shell::GetInstance()->display_controller()->
360 GetAllRootWindows();
363 // static
364 aura::Window* Shell::GetContainer(aura::RootWindow* root_window,
365 int container_id) {
366 return root_window->GetChildById(container_id);
369 // static
370 const aura::Window* Shell::GetContainer(const aura::RootWindow* root_window,
371 int container_id) {
372 return root_window->GetChildById(container_id);
375 // static
376 std::vector<aura::Window*> Shell::GetContainersFromAllRootWindows(
377 int container_id,
378 aura::RootWindow* priority_root) {
379 std::vector<aura::Window*> containers;
380 RootWindowList root_windows = GetAllRootWindows();
381 for (RootWindowList::const_iterator it = root_windows.begin();
382 it != root_windows.end(); ++it) {
383 aura::Window* container = (*it)->GetChildById(container_id);
384 if (container) {
385 if (priority_root && priority_root->Contains(container))
386 containers.insert(containers.begin(), container);
387 else
388 containers.push_back(container);
391 return containers;
394 // static
395 bool Shell::IsLauncherPerDisplayEnabled() {
396 CommandLine* command_line = CommandLine::ForCurrentProcess();
397 return !command_line->HasSwitch(switches::kAshDisableLauncherPerDisplay);
400 void Shell::Init() {
401 #if defined(OS_CHROMEOS)
402 if (base::chromeos::IsRunningOnChromeOS()) {
403 display_change_observer_.reset(new internal::DisplayChangeObserverX11);
404 display_change_observer_->NotifyDisplayChange();
406 #endif
408 // Install the custom factory first so that views::FocusManagers for Tray,
409 // Launcher, and WallPaper could be created by the factory.
410 views::FocusManagerFactory::Install(new AshFocusManagerFactory);
412 env_filter_.reset(new views::corewm::CompoundEventFilter);
413 AddPreTargetHandler(env_filter_.get());
415 if (views::corewm::UseFocusController()) {
416 views::corewm::FocusController* focus_controller =
417 new views::corewm::FocusController(new wm::AshFocusRules);
418 focus_client_.reset(focus_controller);
419 activation_client_ = focus_controller;
420 activation_client_->AddObserver(this);
421 } else {
422 focus_client_.reset(new aura::FocusManager);
423 activation_controller_.reset(
424 new internal::ActivationController(
425 focus_client_.get(),
426 new internal::AshActivationController));
427 activation_client_ = activation_controller_.get();
428 AddPreTargetHandler(activation_controller_.get());
431 focus_cycler_.reset(new internal::FocusCycler());
433 screen_position_controller_.reset(new internal::ScreenPositionController);
434 root_window_host_factory_.reset(delegate_->CreateRootWindowHostFactory());
435 display_controller_.reset(new DisplayController);
436 display_controller_->InitPrimaryDisplay();
437 aura::RootWindow* root_window = display_controller_->GetPrimaryRootWindow();
438 active_root_window_ = root_window;
440 cursor_manager_.SetDeviceScaleFactor(
441 root_window->AsRootWindowHostDelegate()->GetDeviceScaleFactor());
443 #if !defined(OS_MACOSX)
444 nested_dispatcher_controller_.reset(new NestedDispatcherController);
445 accelerator_controller_.reset(new AcceleratorController);
446 #endif
448 // The order in which event filters are added is significant.
449 user_activity_detector_.reset(new UserActivityDetector);
450 AddPreTargetHandler(user_activity_detector_.get());
452 event_rewriter_filter_.reset(new internal::EventRewriterEventFilter);
453 AddPreTargetHandler(event_rewriter_filter_.get());
455 overlay_filter_.reset(new internal::OverlayEventFilter);
456 AddPreTargetHandler(overlay_filter_.get());
457 AddShellObserver(overlay_filter_.get());
459 input_method_filter_.reset(new views::corewm::InputMethodEventFilter(
460 root_window->GetAcceleratedWidget()));
461 AddPreTargetHandler(input_method_filter_.get());
463 #if !defined(OS_MACOSX)
464 accelerator_filter_.reset(new internal::AcceleratorFilter);
465 AddPreTargetHandler(accelerator_filter_.get());
466 #endif
468 event_transformation_handler_.reset(new internal::EventTransformationHandler);
469 AddPreTargetHandler(event_transformation_handler_.get());
471 system_gesture_filter_.reset(new internal::SystemGestureEventFilter);
472 AddPreTargetHandler(system_gesture_filter_.get());
474 capture_controller_.reset(new internal::CaptureController);
476 internal::RootWindowController* root_window_controller =
477 new internal::RootWindowController(root_window);
478 root_window_controller->CreateContainers();
479 root_window_controller->CreateSystemBackground(
480 delegate_->IsFirstRunAfterBoot());
482 CommandLine* command_line = CommandLine::ForCurrentProcess();
484 if (command_line->HasSwitch(ash::switches::kAshDisableNewLockAnimations))
485 session_state_controller_.reset(new SessionStateControllerImpl);
486 else
487 session_state_controller_.reset(new SessionStateControllerImpl2);
488 power_button_controller_.reset(new PowerButtonController(
489 session_state_controller_.get()));
490 AddShellObserver(session_state_controller_.get());
492 if (command_line->HasSwitch(switches::kAshTouchHud)) {
493 touch_observer_hud_.reset(new internal::TouchObserverHUD);
494 AddPreTargetHandler(touch_observer_hud_.get());
497 mouse_cursor_filter_.reset(new internal::MouseCursorEventFilter());
498 AddPreTargetHandler(mouse_cursor_filter_.get());
500 // Create Controllers that may need root window.
501 // TODO(oshima): Move as many controllers before creating
502 // RootWindowController as possible.
503 visibility_controller_.reset(new AshVisibilityController);
504 drag_drop_controller_.reset(new internal::DragDropController);
505 user_action_client_.reset(delegate_->CreateUserActionClient());
506 window_modality_controller_.reset(
507 new views::corewm::WindowModalityController);
508 AddPreTargetHandler(window_modality_controller_.get());
510 magnification_controller_.reset(
511 MagnificationController::CreateInstance());
513 partial_magnification_controller_.reset(
514 new PartialMagnificationController());
516 high_contrast_controller_.reset(new HighContrastController);
517 video_detector_.reset(new VideoDetector);
518 window_cycle_controller_.reset(new WindowCycleController(activation_client_));
520 tooltip_controller_.reset(new internal::TooltipController(
521 drag_drop_controller_.get()));
522 AddPreTargetHandler(tooltip_controller_.get());
524 event_client_.reset(new internal::EventClientImpl);
526 InitRootWindowController(root_window_controller);
528 // This controller needs to be set before SetupManagedWindowMode.
529 desktop_background_controller_.reset(new DesktopBackgroundController());
530 user_wallpaper_delegate_.reset(delegate_->CreateUserWallpaperDelegate());
531 if (!user_wallpaper_delegate_.get())
532 user_wallpaper_delegate_.reset(new DummyUserWallpaperDelegate());
534 // StatusAreaWidget uses Shell's CapsLockDelegate.
535 caps_lock_delegate_.reset(delegate_->CreateCapsLockDelegate());
537 if (!command_line->HasSwitch(switches::kAuraNoShadows)) {
538 resize_shadow_controller_.reset(new internal::ResizeShadowController());
539 shadow_controller_.reset(
540 new views::corewm::ShadowController(GetPrimaryRootWindow()));
543 // Create system_tray_notifier_ before the delegate.
544 system_tray_notifier_.reset(new ash::SystemTrayNotifier());
546 // Initialize system_tray_delegate_ before initializing StatusAreaWidget.
547 system_tray_delegate_.reset(delegate()->CreateSystemTrayDelegate());
548 if (!system_tray_delegate_.get())
549 system_tray_delegate_.reset(SystemTrayDelegate::CreateDummyDelegate());
551 // Creates StatusAreaWidget.
552 root_window_controller->InitForPrimaryDisplay();
554 // Initialize system_tray_delegate_ after StatusAreaWidget is created.
555 system_tray_delegate_->Initialize();
557 display_controller_->InitSecondaryDisplays();
559 // Force Layout
560 root_window_controller->root_window_layout()->OnWindowResized();
562 // It needs to be created after OnWindowResized has been called, otherwise the
563 // widget will not paint when restoring after a browser crash. Also it needs
564 // to be created after InitSecondaryDisplays() to initialize the wallpapers in
565 // the correct size.
566 user_wallpaper_delegate_->InitializeWallpaper();
568 if (initially_hide_cursor_)
569 cursor_manager_.DisableMouseEvents();
570 cursor_manager_.SetCursor(ui::kCursorPointer);
572 // Cursor might have been hidden by somethign other than chrome.
573 // Let the first mouse event show the cursor.
574 env_filter_->set_cursor_hidden_by_filter(true);
577 void Shell::ShowContextMenu(const gfx::Point& location_in_screen) {
578 // No context menus if user have not logged in.
579 if (!delegate_->IsUserLoggedIn())
580 return;
581 // No context menus when screen is locked.
582 if (IsScreenLocked())
583 return;
585 aura::RootWindow* root =
586 wm::GetRootWindowMatching(gfx::Rect(location_in_screen, gfx::Size()));
587 // TODO(oshima): The root and root window controller shouldn't be
588 // NULL even for the out-of-bounds |location_in_screen| (It should
589 // return the primary root). Investigate why/how this is
590 // happening. crbug.com/165214.
591 internal::RootWindowController* rwc = GetRootWindowController(root);
592 CHECK(rwc) << "root=" << root
593 << ", location:" << location_in_screen.ToString();
594 if (rwc)
595 rwc->ShowContextMenu(location_in_screen);
598 void Shell::ToggleAppList(aura::Window* window) {
599 // If the context window is not given, show it on the active root window.
600 if (!window)
601 window = GetActiveRootWindow();
602 if (!app_list_controller_.get())
603 app_list_controller_.reset(new internal::AppListController);
604 app_list_controller_->SetVisible(!app_list_controller_->IsVisible(), window);
607 bool Shell::GetAppListTargetVisibility() const {
608 return app_list_controller_.get() &&
609 app_list_controller_->GetTargetVisibility();
612 aura::Window* Shell::GetAppListWindow() {
613 return app_list_controller_.get() ? app_list_controller_->GetWindow() : NULL;
616 bool Shell::CanLockScreen() {
617 return delegate_->CanLockScreen();
620 bool Shell::IsScreenLocked() const {
621 return delegate_->IsScreenLocked();
624 bool Shell::IsSystemModalWindowOpen() const {
625 if (simulate_modal_window_open_for_testing_)
626 return true;
627 const std::vector<aura::Window*> containers = GetContainersFromAllRootWindows(
628 internal::kShellWindowId_SystemModalContainer, NULL);
629 for (std::vector<aura::Window*>::const_iterator cit = containers.begin();
630 cit != containers.end(); ++cit) {
631 for (aura::Window::Windows::const_iterator wit = (*cit)->children().begin();
632 wit != (*cit)->children().end(); ++wit) {
633 if ((*wit)->GetProperty(aura::client::kModalKey) ==
634 ui::MODAL_TYPE_SYSTEM && (*wit)->TargetVisibility()) {
635 return true;
639 return false;
642 views::NonClientFrameView* Shell::CreateDefaultNonClientFrameView(
643 views::Widget* widget) {
644 if (CommandLine::ForCurrentProcess()->HasSwitch(
645 ::switches::kEnableNewDialogStyle)) {
646 return new views::DialogFrameView(string16());
648 // Use translucent-style window frames for dialogs.
649 CustomFrameViewAsh* frame_view = new CustomFrameViewAsh;
650 frame_view->Init(widget);
651 return frame_view;
654 void Shell::RotateFocus(Direction direction) {
655 focus_cycler_->RotateFocus(
656 direction == FORWARD ? internal::FocusCycler::FORWARD :
657 internal::FocusCycler::BACKWARD);
660 void Shell::SetDisplayWorkAreaInsets(Window* contains,
661 const gfx::Insets& insets) {
662 if (!display_manager_->UpdateWorkAreaOfDisplayNearestWindow(contains, insets))
663 return;
664 FOR_EACH_OBSERVER(ShellObserver, observers_,
665 OnDisplayWorkAreaInsetsChanged());
668 void Shell::OnLoginStateChanged(user::LoginStatus status) {
669 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLoginStateChanged(status));
670 RootWindowControllerList controllers = GetAllRootWindowControllers();
671 for (RootWindowControllerList::iterator iter = controllers.begin();
672 iter != controllers.end(); ++iter)
673 (*iter)->OnLoginStateChanged(status);
676 void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status) {
677 RootWindowControllerList controllers = GetAllRootWindowControllers();
678 for (RootWindowControllerList::iterator iter = controllers.begin();
679 iter != controllers.end(); ++iter)
680 (*iter)->UpdateAfterLoginStatusChange(status);
683 void Shell::OnAppTerminating() {
684 FOR_EACH_OBSERVER(ShellObserver, observers_, OnAppTerminating());
687 void Shell::OnLockStateChanged(bool locked) {
688 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLockStateChanged(locked));
691 void Shell::CreateLauncher() {
692 if (IsLauncherPerDisplayEnabled()) {
693 RootWindowControllerList controllers = GetAllRootWindowControllers();
694 for (RootWindowControllerList::iterator iter = controllers.begin();
695 iter != controllers.end(); ++iter)
696 (*iter)->CreateLauncher();
697 } else {
698 GetPrimaryRootWindowController()->CreateLauncher();
702 void Shell::ShowLauncher() {
703 if (IsLauncherPerDisplayEnabled()) {
704 RootWindowControllerList controllers = GetAllRootWindowControllers();
705 for (RootWindowControllerList::iterator iter = controllers.begin();
706 iter != controllers.end(); ++iter)
707 (*iter)->ShowLauncher();
708 } else {
709 GetPrimaryRootWindowController()->ShowLauncher();
713 void Shell::AddShellObserver(ShellObserver* observer) {
714 observers_.AddObserver(observer);
717 void Shell::RemoveShellObserver(ShellObserver* observer) {
718 observers_.RemoveObserver(observer);
721 void Shell::UpdateShelfVisibility() {
722 RootWindowControllerList controllers = GetAllRootWindowControllers();
723 for (RootWindowControllerList::iterator iter = controllers.begin();
724 iter != controllers.end(); ++iter)
725 if ((*iter)->shelf())
726 (*iter)->UpdateShelfVisibility();
729 void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior,
730 aura::RootWindow* root_window) {
731 GetRootWindowController(root_window)->SetShelfAutoHideBehavior(behavior);
734 ShelfAutoHideBehavior Shell::GetShelfAutoHideBehavior(
735 aura::RootWindow* root_window) const {
736 return GetRootWindowController(root_window)->GetShelfAutoHideBehavior();
739 void Shell::SetShelfAlignment(ShelfAlignment alignment,
740 aura::RootWindow* root_window) {
741 if (GetRootWindowController(root_window)->SetShelfAlignment(alignment)) {
742 FOR_EACH_OBSERVER(
743 ShellObserver, observers_, OnShelfAlignmentChanged(root_window));
747 ShelfAlignment Shell::GetShelfAlignment(aura::RootWindow* root_window) {
748 return GetRootWindowController(root_window)->GetShelfAlignment();
751 void Shell::SetDimming(bool should_dim) {
752 RootWindowControllerList controllers = GetAllRootWindowControllers();
753 for (RootWindowControllerList::iterator iter = controllers.begin();
754 iter != controllers.end(); ++iter)
755 (*iter)->screen_dimmer()->SetDimming(should_dim);
758 void Shell::CreateModalBackground(aura::Window* window) {
759 if (!modality_filter_.get()) {
760 modality_filter_.reset(new internal::SystemModalContainerEventFilter(this));
761 AddPreTargetHandler(modality_filter_.get());
763 RootWindowControllerList controllers = GetAllRootWindowControllers();
764 for (RootWindowControllerList::iterator iter = controllers.begin();
765 iter != controllers.end(); ++iter)
766 (*iter)->GetSystemModalLayoutManager(window)->CreateModalBackground();
769 void Shell::OnModalWindowRemoved(aura::Window* removed) {
770 RootWindowControllerList controllers = GetAllRootWindowControllers();
771 bool activated = false;
772 for (RootWindowControllerList::iterator iter = controllers.begin();
773 iter != controllers.end() && !activated; ++iter) {
774 activated = (*iter)->GetSystemModalLayoutManager(removed)->
775 ActivateNextModalWindow();
777 if (!activated) {
778 RemovePreTargetHandler(modality_filter_.get());
779 modality_filter_.reset();
780 for (RootWindowControllerList::iterator iter = controllers.begin();
781 iter != controllers.end(); ++iter)
782 (*iter)->GetSystemModalLayoutManager(removed)->DestroyModalBackground();
786 WebNotificationTray* Shell::GetWebNotificationTray() {
787 return GetPrimaryRootWindowController()->status_area_widget()->
788 web_notification_tray();
791 bool Shell::HasPrimaryStatusArea() {
792 return !!GetPrimaryRootWindowController()->status_area_widget();
795 SystemTray* Shell::GetPrimarySystemTray() {
796 return GetPrimaryRootWindowController()->GetSystemTray();
799 LauncherDelegate* Shell::GetLauncherDelegate() {
800 if (!launcher_delegate_.get()) {
801 launcher_model_.reset(new LauncherModel);
802 launcher_delegate_.reset(
803 delegate_->CreateLauncherDelegate(launcher_model_.get()));
805 return launcher_delegate_.get();
808 void Shell::InitRootWindowForSecondaryDisplay(aura::RootWindow* root) {
809 aura::client::SetFocusClient(root, focus_client_.get());
810 internal::RootWindowController* controller =
811 new internal::RootWindowController(root);
812 controller->CreateContainers();
813 // Pass false for the |is_first_run_after_boot| parameter so we'll show a
814 // black background on this display instead of trying to mimic the boot splash
815 // screen.
816 controller->CreateSystemBackground(false);
817 InitRootWindowController(controller);
818 if (IsLauncherPerDisplayEnabled())
819 controller->InitForPrimaryDisplay();
820 controller->root_window_layout()->OnWindowResized();
821 desktop_background_controller_->OnRootWindowAdded(root);
822 high_contrast_controller_->OnRootWindowAdded(root);
823 root->ShowRootWindow();
824 // Activate new root for testing.
825 active_root_window_ = root;
828 void Shell::DoInitialWorkspaceAnimation() {
829 return GetPrimaryRootWindowController()->workspace_controller()->
830 DoInitialAnimation();
833 void Shell::InitRootWindowController(
834 internal::RootWindowController* controller) {
835 aura::RootWindow* root_window = controller->root_window();
836 DCHECK(activation_client_);
837 DCHECK(visibility_controller_.get());
838 DCHECK(drag_drop_controller_.get());
839 DCHECK(capture_controller_.get());
840 DCHECK(window_cycle_controller_.get());
842 aura::client::SetFocusClient(root_window, focus_client_.get());
843 input_method_filter_->SetInputMethodPropertyInRootWindow(root_window);
844 aura::client::SetActivationClient(root_window, activation_client_);
845 if (views::corewm::UseFocusController()) {
846 views::corewm::FocusController* controller =
847 static_cast<views::corewm::FocusController*>(activation_client_);
848 root_window->AddPreTargetHandler(controller);
850 aura::client::SetVisibilityClient(root_window, visibility_controller_.get());
851 aura::client::SetDragDropClient(root_window, drag_drop_controller_.get());
852 aura::client::SetCaptureClient(root_window, capture_controller_.get());
853 aura::client::SetScreenPositionClient(root_window,
854 screen_position_controller_.get());
855 aura::client::SetCursorClient(root_window, &cursor_manager_);
856 aura::client::SetTooltipClient(root_window, tooltip_controller_.get());
857 aura::client::SetEventClient(root_window, event_client_.get());
859 if (nested_dispatcher_controller_.get()) {
860 aura::client::SetDispatcherClient(root_window,
861 nested_dispatcher_controller_.get());
863 if (user_action_client_.get())
864 aura::client::SetUserActionClient(root_window, user_action_client_.get());
866 root_window->SetCursor(ui::kCursorPointer);
867 controller->InitLayoutManagers();
869 // TODO(oshima): Move the instance to RootWindowController when
870 // the extended desktop is enabled by default.
871 internal::AlwaysOnTopController* always_on_top_controller =
872 new internal::AlwaysOnTopController;
873 always_on_top_controller->SetAlwaysOnTopContainer(
874 root_window->GetChildById(internal::kShellWindowId_AlwaysOnTopContainer));
875 root_window->SetProperty(internal::kAlwaysOnTopControllerKey,
876 always_on_top_controller);
877 if (GetPrimaryRootWindowController()->GetSystemModalLayoutManager(NULL)->
878 has_modal_background()) {
879 controller->GetSystemModalLayoutManager(NULL)->CreateModalBackground();
882 window_cycle_controller_->OnRootWindowAdded(root_window);
885 ////////////////////////////////////////////////////////////////////////////////
886 // Shell, private:
888 bool Shell::CanWindowReceiveEvents(aura::Window* window) {
889 RootWindowControllerList controllers = GetAllRootWindowControllers();
890 for (RootWindowControllerList::iterator iter = controllers.begin();
891 iter != controllers.end(); ++iter) {
892 if ((*iter)->GetSystemModalLayoutManager(window)->
893 CanWindowReceiveEvents(window)) {
894 return true;
897 return false;
900 ////////////////////////////////////////////////////////////////////////////////
901 // Shell, ui::EventTarget overrides:
903 bool Shell::CanAcceptEvent(const ui::Event& event) {
904 return true;
907 ui::EventTarget* Shell::GetParentTarget() {
908 return NULL;
911 void Shell::OnEvent(ui::Event* event) {
914 ////////////////////////////////////////////////////////////////////////////////
915 // Shell, aura::client::ActivationChangeObserver implementation:
917 void Shell::OnWindowActivated(aura::Window* gained_active,
918 aura::Window* lost_active) {
919 if (gained_active)
920 active_root_window_ = gained_active->GetRootWindow();
923 } // namespace ash