shell.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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. 4 5#include "ash/shell.h" 6 7#include <algorithm> 8#include <string> 9 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" 103 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 109 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) 122 123namespace ash { 124 125namespace { 126 127using aura::Window; 128using views::Widget; 129 130// This dummy class is used for shell unit tests. We dont have chrome delegate 131// in these tests. 132class DummyUserWallpaperDelegate : public UserWallpaperDelegate { 133 public: 134 DummyUserWallpaperDelegate() {} 135 136 virtual ~DummyUserWallpaperDelegate() {} 137 138 virtual int GetAnimationType() OVERRIDE { 139 return views::corewm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE; 140 } 141 142 virtual bool ShouldShowInitialAnimation() OVERRIDE { 143 return false; 144 } 145 146 virtual void UpdateWallpaper() OVERRIDE { 147 } 148 149 virtual void InitializeWallpaper() OVERRIDE { 150 ash::Shell::GetInstance()->desktop_background_controller()-> 151 CreateEmptyWallpaper(); 152 } 153 154 virtual void OpenSetWallpaperPage() OVERRIDE { 155 } 156 157 virtual bool CanOpenSetWallpaperPage() OVERRIDE { 158 return false; 159 } 160 161 virtual void OnWallpaperAnimationFinished() OVERRIDE { 162 } 163 164 virtual void OnWallpaperBootAnimationFinished() OVERRIDE { 165 } 166 167 private: 168 DISALLOW_COPY_AND_ASSIGN(DummyUserWallpaperDelegate); 169}; 170 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. 173class AshVisibilityController : public views::corewm::VisibilityController { 174 public: 175 AshVisibilityController() {} 176 virtual ~AshVisibilityController() {} 177 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); 184 } 185 186 DISALLOW_COPY_AND_ASSIGN(AshVisibilityController); 187}; 188 189} // namespace 190 191// static 192Shell* Shell::instance_ = NULL; 193// static 194bool Shell::initially_hide_cursor_ = false; 195 196//////////////////////////////////////////////////////////////////////////////// 197// Shell, public: 198 199Shell::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); 215 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); 227 228 output_configurator_->Init( 229 !is_panel_fitting_disabled, 230 delegate_->IsFirstRunAfterBoot() ? kChromeOsBootColor : 0); 231 232 base::MessagePumpAuraX11::Current()->AddDispatcherForRootWindow( 233 output_configurator()); 234#endif // defined(OS_CHROMEOS) 235 AddPreTargetHandler(this); 236} 237 238Shell::~Shell() { 239 views::FocusManagerFactory::Install(NULL); 240 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); 246 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 260 261 // TooltipController is deleted with the Shell so removing its references. 262 RemovePreTargetHandler(tooltip_controller_.get()); 263 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(); 269 270 // Destroy SystemTrayDelegate before destroying the status area(s). 271 system_tray_delegate_->Shutdown(); 272 system_tray_delegate_.reset(); 273 274 // Destroy all child windows including widgets. 275 display_controller_->CloseChildWindows(); 276 277 // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems 278 // needs to remove observers from it. 279 system_tray_notifier_.reset(); 280 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(); 299 300 power_button_controller_.reset(); 301 lock_state_controller_.reset(); 302 303 mirror_window_controller_.reset(); 304 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(); 311 312 // Delete the activation controller after other controllers and launcher 313 // because they might have registered ActivationChangeObserver. 314 activation_controller_.reset(); 315 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) 327 328 DCHECK(instance_ == this); 329 instance_ = NULL; 330} 331 332// static 333Shell* Shell::CreateInstance(ShellDelegate* delegate) { 334 CHECK(!instance_); 335 instance_ = new Shell(delegate); 336 instance_->Init(); 337 return instance_; 338} 339 340// static 341Shell* Shell::GetInstance() { 342 DCHECK(instance_); 343 return instance_; 344} 345 346// static 347bool Shell::HasInstance() { 348 return !!instance_; 349} 350 351// static 352void Shell::DeleteInstance() { 353 delete instance_; 354 instance_ = NULL; 355} 356 357// static 358internal::RootWindowController* Shell::GetPrimaryRootWindowController() { 359 return GetRootWindowController(GetPrimaryRootWindow()); 360} 361 362// static 363Shell::RootWindowControllerList Shell::GetAllRootWindowControllers() { 364 return Shell::GetInstance()->display_controller()-> 365 GetAllRootWindowControllers(); 366} 367 368// static 369aura::RootWindow* Shell::GetPrimaryRootWindow() { 370 return GetInstance()->display_controller()->GetPrimaryRootWindow(); 371} 372 373// static 374aura::RootWindow* Shell::GetActiveRootWindow() { 375 return GetInstance()->active_root_window_; 376} 377 378// static 379gfx::Screen* Shell::GetScreen() { 380 return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE); 381} 382 383// static 384Shell::RootWindowList Shell::GetAllRootWindows() { 385 return Shell::GetInstance()->display_controller()-> 386 GetAllRootWindows(); 387} 388 389// static 390aura::Window* Shell::GetContainer(aura::RootWindow* root_window, 391 int container_id) { 392 return root_window->GetChildById(container_id); 393} 394 395// static 396const aura::Window* Shell::GetContainer(const aura::RootWindow* root_window, 397 int container_id) { 398 return root_window->GetChildById(container_id); 399} 400 401// static 402std::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); 415 } 416 } 417 return containers; 418} 419 420// static 421bool Shell::IsForcedMaximizeMode() { 422 CommandLine* command_line = CommandLine::ForCurrentProcess(); 423 return command_line->HasSwitch(switches::kForcedMaximizeMode); 424} 425 426void Shell::Init() { 427 CommandLine* command_line = CommandLine::ForCurrentProcess(); 428 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::kAshDisableSoftwareMirroring)) 443 output_configurator_->set_mirroring_controller(display_manager_.get()); 444 output_configurator_->Start(); 445 display_change_observer_->OnDisplayModeChanged(); 446 } 447#endif 448 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); 452 453 env_filter_.reset(new views::corewm::CompoundEventFilter); 454 AddPreTargetHandler(env_filter_.get()); 455 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()); 474 } 475 476 focus_cycler_.reset(new internal::FocusCycler()); 477 478 screen_position_controller_.reset(new internal::ScreenPositionController); 479 root_window_host_factory_.reset(delegate_->CreateRootWindowHostFactory()); 480 481 display_controller_->Start(); 482 display_controller_->InitPrimaryDisplay(); 483 aura::RootWindow* root_window = display_controller_->GetPrimaryRootWindow(); 484 active_root_window_ = root_window; 485 486 cursor_manager_.SetDisplay(DisplayController::GetPrimaryDisplay()); 487 488#if !defined(OS_MACOSX) 489 nested_dispatcher_controller_.reset(new NestedDispatcherController); 490 accelerator_controller_.reset(new AcceleratorController); 491#endif 492 493 // The order in which event filters are added is significant. 494 user_activity_detector_.reset(new UserActivityDetector); 495 AddPreTargetHandler(user_activity_detector_.get()); 496 497 event_rewriter_filter_.reset(new internal::EventRewriterEventFilter); 498 AddPreTargetHandler(event_rewriter_filter_.get()); 499 500 overlay_filter_.reset(new internal::OverlayEventFilter); 501 AddPreTargetHandler(overlay_filter_.get()); 502 AddShellObserver(overlay_filter_.get()); 503 504 input_method_filter_.reset(new views::corewm::InputMethodEventFilter( 505 root_window->GetAcceleratedWidget())); 506 AddPreTargetHandler(input_method_filter_.get()); 507 508#if !defined(OS_MACOSX) 509 accelerator_filter_.reset(new internal::AcceleratorFilter); 510 AddPreTargetHandler(accelerator_filter_.get()); 511#endif 512 513 event_transformation_handler_.reset(new internal::EventTransformationHandler); 514 AddPreTargetHandler(event_transformation_handler_.get()); 515 516 system_gesture_filter_.reset(new internal::SystemGestureEventFilter); 517 AddPreTargetHandler(system_gesture_filter_.get()); 518 519 capture_controller_.reset(new internal::CaptureController); 520 521 // The keyboard system must be initialized before the RootWindowController is 522 // created. 523 if (keyboard::IsKeyboardEnabled()) 524 keyboard::InitializeKeyboard(); 525 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()); 531 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()); 539 540 drag_drop_controller_.reset(new internal::DragDropController); 541 mouse_cursor_filter_.reset(new internal::MouseCursorEventFilter()); 542 PrependPreTargetHandler(mouse_cursor_filter_.get()); 543 544 // Create Controllers that may need root window. 545 // TODO(oshima): Move as many controllers before creating 546 // RootWindowController as possible. 547 visibility_controller_.reset(new AshVisibilityController); 548 user_action_client_.reset(delegate_->CreateUserActionClient()); 549 window_modality_controller_.reset( 550 new views::corewm::WindowModalityController); 551 AddPreTargetHandler(window_modality_controller_.get()); 552 553 magnification_controller_.reset( 554 MagnificationController::CreateInstance()); 555 556 partial_magnification_controller_.reset( 557 new PartialMagnificationController()); 558 559 high_contrast_controller_.reset(new HighContrastController); 560 video_detector_.reset(new VideoDetector); 561 window_cycle_controller_.reset(new WindowCycleController(activation_client_)); 562 563 tooltip_controller_.reset(new views::corewm::TooltipController( 564 gfx::SCREEN_TYPE_ALTERNATE)); 565 AddPreTargetHandler(tooltip_controller_.get()); 566 567 event_client_.reset(new internal::EventClientImpl); 568 569 InitRootWindowController(root_window_controller); 570 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()); 576 577 // StatusAreaWidget uses Shell's CapsLockDelegate. 578 caps_lock_delegate_.reset(delegate_->CreateCapsLockDelegate()); 579 580 session_state_delegate_.reset(delegate_->CreateSessionStateDelegate()); 581 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_)); 586 } 587 588 // Create system_tray_notifier_ before the delegate. 589 system_tray_notifier_.reset(new ash::SystemTrayNotifier()); 590 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()); 595 596 // Creates StatusAreaWidget. 597 root_window_controller->InitForPrimaryDisplay(); 598 599 // Initialize system_tray_delegate_ after StatusAreaWidget is created. 600 system_tray_delegate_->Initialize(); 601 602 display_controller_->InitSecondaryDisplays(); 603 604 // Force Layout 605 root_window_controller->root_window_layout()->OnWindowResized(); 606 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(); 612 613 if (initially_hide_cursor_) 614 cursor_manager_.HideCursor(); 615 cursor_manager_.SetCursor(ui::kCursorPointer); 616 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); 621 } 622} 623 624void 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; 631 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); 643} 644 645void 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); 652} 653 654void 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); 659} 660 661bool Shell::GetAppListTargetVisibility() const { 662 return app_list_controller_.get() && 663 app_list_controller_->GetTargetVisibility(); 664} 665 666aura::Window* Shell::GetAppListWindow() { 667 return app_list_controller_.get() ? app_list_controller_->GetWindow() : NULL; 668} 669 670bool 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; 682 } 683 } 684 } 685 return false; 686} 687 688views::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; 694} 695 696void Shell::RotateFocus(Direction direction) { 697 focus_cycler_->RotateFocus( 698 direction == FORWARD ? internal::FocusCycler::FORWARD : 699 internal::FocusCycler::BACKWARD); 700} 701 702void 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()); 708} 709 710void 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); 716} 717 718void 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); 723} 724 725void Shell::OnAppTerminating() { 726 FOR_EACH_OBSERVER(ShellObserver, observers_, OnAppTerminating()); 727} 728 729void Shell::OnLockStateChanged(bool locked) { 730 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLockStateChanged(locked)); 731} 732 733void Shell::CreateLauncher() { 734 RootWindowControllerList controllers = GetAllRootWindowControllers(); 735 for (RootWindowControllerList::iterator iter = controllers.begin(); 736 iter != controllers.end(); ++iter) 737 (*iter)->shelf()->CreateLauncher(); 738} 739 740void Shell::ShowLauncher() { 741 RootWindowControllerList controllers = GetAllRootWindowControllers(); 742 for (RootWindowControllerList::iterator iter = controllers.begin(); 743 iter != controllers.end(); ++iter) 744 (*iter)->ShowLauncher(); 745} 746 747void Shell::AddShellObserver(ShellObserver* observer) { 748 observers_.AddObserver(observer); 749} 750 751void Shell::RemoveShellObserver(ShellObserver* observer) { 752 observers_.RemoveObserver(observer); 753} 754 755void 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(); 761} 762 763void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior, 764 aura::RootWindow* root_window) { 765 ash::internal::ShelfLayoutManager::ForLauncher(root_window)-> 766 SetAutoHideBehavior(behavior); 767} 768 769ShelfAutoHideBehavior Shell::GetShelfAutoHideBehavior( 770 aura::RootWindow* root_window) const { 771 return ash::internal::ShelfLayoutManager::ForLauncher(root_window)-> 772 auto_hide_behavior(); 773} 774 775void 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)); 781 } 782} 783 784ShelfAlignment Shell::GetShelfAlignment(aura::RootWindow* root_window) { 785 return GetRootWindowController(root_window)-> 786 GetShelfLayoutManager()->GetAlignment(); 787} 788 789void 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); 794} 795 796void Shell::CreateModalBackground(aura::Window* window) { 797 if (!modality_filter_) { 798 modality_filter_.reset(new internal::SystemModalContainerEventFilter(this)); 799 AddPreTargetHandler(modality_filter_.get()); 800 } 801 RootWindowControllerList controllers = GetAllRootWindowControllers(); 802 for (RootWindowControllerList::iterator iter = controllers.begin(); 803 iter != controllers.end(); ++iter) 804 (*iter)->GetSystemModalLayoutManager(window)->CreateModalBackground(); 805} 806 807void 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(); 814 } 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(); 821 } 822} 823 824WebNotificationTray* Shell::GetWebNotificationTray() { 825 return GetPrimaryRootWindowController()->shelf()-> 826 status_area_widget()->web_notification_tray(); 827} 828 829bool Shell::HasPrimaryStatusArea() { 830 ShelfWidget* shelf = GetPrimaryRootWindowController()->shelf(); 831 return shelf && shelf->status_area_widget(); 832} 833 834SystemTray* Shell::GetPrimarySystemTray() { 835 return GetPrimaryRootWindowController()->GetSystemTray(); 836} 837 838LauncherDelegate* 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())); 847 } 848 return launcher_delegate_.get(); 849} 850 851void 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; 868} 869 870void Shell::DoInitialWorkspaceAnimation() { 871 return GetPrimaryRootWindowController()->workspace_controller()-> 872 DoInitialAnimation(); 873} 874 875void 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()); 883 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); 891 } 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()); 900 901 if (nested_dispatcher_controller_) { 902 aura::client::SetDispatcherClient(root_window, 903 nested_dispatcher_controller_.get()); 904 } 905 if (user_action_client_) 906 aura::client::SetUserActionClient(root_window, user_action_client_.get()); 907 908 root_window->SetCursor(ui::kCursorPointer); 909 controller->InitLayoutManagers(); 910 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(); 922 } 923 924 window_cycle_controller_->OnRootWindowAdded(root_window); 925} 926 927//////////////////////////////////////////////////////////////////////////////// 928// Shell, private: 929 930bool 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; 938 } 939 return false; 940} 941 942//////////////////////////////////////////////////////////////////////////////// 943// Shell, ui::EventTarget overrides: 944 945bool Shell::CanAcceptEvent(const ui::Event& event) { 946 return true; 947} 948 949ui::EventTarget* Shell::GetParentTarget() { 950 return NULL; 951} 952 953void Shell::OnEvent(ui::Event* event) { 954} 955 956//////////////////////////////////////////////////////////////////////////////// 957// Shell, aura::client::ActivationChangeObserver implementation: 958 959void Shell::OnWindowActivated(aura::Window* gained_active, 960 aura::Window* lost_active) { 961 if (gained_active) 962 active_root_window_ = gained_active->GetRootWindow(); 963} 964 965} // namespace ash 966