shell.cc revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
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/accelerator_controller.h" 11#include "ash/accelerators/accelerator_delegate.h" 12#include "ash/accelerators/focus_manager_factory.h" 13#include "ash/accelerators/nested_accelerator_delegate.h" 14#include "ash/accelerometer/accelerometer_controller.h" 15#include "ash/ash_switches.h" 16#include "ash/autoclick/autoclick_controller.h" 17#include "ash/desktop_background/desktop_background_controller.h" 18#include "ash/desktop_background/desktop_background_view.h" 19#include "ash/desktop_background/user_wallpaper_delegate.h" 20#include "ash/display/cursor_window_controller.h" 21#include "ash/display/display_controller.h" 22#include "ash/display/display_manager.h" 23#include "ash/display/event_transformation_handler.h" 24#include "ash/display/mouse_cursor_event_filter.h" 25#include "ash/display/screen_position_controller.h" 26#include "ash/display/virtual_keyboard_window_controller.h" 27#include "ash/drag_drop/drag_drop_controller.h" 28#include "ash/first_run/first_run_helper_impl.h" 29#include "ash/focus_cycler.h" 30#include "ash/frame/custom_frame_view_ash.h" 31#include "ash/gpu_support.h" 32#include "ash/high_contrast/high_contrast_controller.h" 33#include "ash/host/ash_window_tree_host_init_params.h" 34#include "ash/keyboard_uma_event_filter.h" 35#include "ash/magnifier/magnification_controller.h" 36#include "ash/magnifier/partial_magnification_controller.h" 37#include "ash/media_delegate.h" 38#include "ash/new_window_delegate.h" 39#include "ash/root_window_controller.h" 40#include "ash/session/session_state_delegate.h" 41#include "ash/shelf/app_list_shelf_item_delegate.h" 42#include "ash/shelf/shelf_delegate.h" 43#include "ash/shelf/shelf_item_delegate.h" 44#include "ash/shelf/shelf_item_delegate_manager.h" 45#include "ash/shelf/shelf_layout_manager.h" 46#include "ash/shelf/shelf_model.h" 47#include "ash/shelf/shelf_widget.h" 48#include "ash/shelf/shelf_window_watcher.h" 49#include "ash/shell_delegate.h" 50#include "ash/shell_factory.h" 51#include "ash/shell_init_params.h" 52#include "ash/shell_window_ids.h" 53#include "ash/system/locale/locale_notification_controller.h" 54#include "ash/system/status_area_widget.h" 55#include "ash/system/tray/system_tray_delegate.h" 56#include "ash/system/tray/system_tray_notifier.h" 57#include "ash/wm/app_list_controller.h" 58#include "ash/wm/ash_focus_rules.h" 59#include "ash/wm/ash_native_cursor_manager.h" 60#include "ash/wm/coordinate_conversion.h" 61#include "ash/wm/event_client_impl.h" 62#include "ash/wm/lock_state_controller.h" 63#include "ash/wm/maximize_mode/maximize_mode_controller.h" 64#include "ash/wm/maximize_mode/maximize_mode_window_manager.h" 65#include "ash/wm/mru_window_tracker.h" 66#include "ash/wm/overlay_event_filter.h" 67#include "ash/wm/overview/window_selector_controller.h" 68#include "ash/wm/power_button_controller.h" 69#include "ash/wm/resize_shadow_controller.h" 70#include "ash/wm/root_window_layout_manager.h" 71#include "ash/wm/screen_dimmer.h" 72#include "ash/wm/system_gesture_event_filter.h" 73#include "ash/wm/system_modal_container_event_filter.h" 74#include "ash/wm/system_modal_container_layout_manager.h" 75#include "ash/wm/toplevel_window_event_handler.h" 76#include "ash/wm/video_detector.h" 77#include "ash/wm/window_animations.h" 78#include "ash/wm/window_cycle_controller.h" 79#include "ash/wm/window_positioner.h" 80#include "ash/wm/window_properties.h" 81#include "ash/wm/window_util.h" 82#include "ash/wm/workspace_controller.h" 83#include "base/bind.h" 84#include "base/debug/trace_event.h" 85#include "ui/aura/client/aura_constants.h" 86#include "ui/aura/env.h" 87#include "ui/aura/layout_manager.h" 88#include "ui/aura/window.h" 89#include "ui/aura/window_event_dispatcher.h" 90#include "ui/base/ui_base_switches.h" 91#include "ui/compositor/layer.h" 92#include "ui/compositor/layer_animator.h" 93#include "ui/events/event_target_iterator.h" 94#include "ui/gfx/display.h" 95#include "ui/gfx/image/image_skia.h" 96#include "ui/gfx/screen.h" 97#include "ui/gfx/size.h" 98#include "ui/keyboard/keyboard.h" 99#include "ui/keyboard/keyboard_controller.h" 100#include "ui/keyboard/keyboard_switches.h" 101#include "ui/keyboard/keyboard_util.h" 102#include "ui/message_center/message_center.h" 103#include "ui/views/corewm/tooltip_aura.h" 104#include "ui/views/corewm/tooltip_controller.h" 105#include "ui/views/focus/focus_manager_factory.h" 106#include "ui/views/widget/native_widget_aura.h" 107#include "ui/views/widget/widget.h" 108#include "ui/wm/core/accelerator_filter.h" 109#include "ui/wm/core/compound_event_filter.h" 110#include "ui/wm/core/focus_controller.h" 111#include "ui/wm/core/input_method_event_filter.h" 112#include "ui/wm/core/nested_accelerator_controller.h" 113#include "ui/wm/core/shadow_controller.h" 114#include "ui/wm/core/user_activity_detector.h" 115#include "ui/wm/core/visibility_controller.h" 116#include "ui/wm/core/window_modality_controller.h" 117 118#if defined(OS_CHROMEOS) 119#if defined(USE_X11) 120#include "ash/accelerators/magnifier_key_scroller.h" 121#include "ash/accelerators/spoken_feedback_toggler.h" 122#include "ash/touch/touch_transformer_controller.h" 123#include "ui/gfx/x/x11_types.h" 124#endif // defined(USE_X11) 125#include "ash/ash_constants.h" 126#include "ash/display/display_change_observer_chromeos.h" 127#include "ash/display/display_configurator_animation.h" 128#include "ash/display/display_error_observer_chromeos.h" 129#include "ash/display/projecting_observer_chromeos.h" 130#include "ash/display/resolution_notification_controller.h" 131#include "ash/sticky_keys/sticky_keys_controller.h" 132#include "ash/system/chromeos/bluetooth/bluetooth_notification_controller.h" 133#include "ash/system/chromeos/brightness/brightness_controller_chromeos.h" 134#include "ash/system/chromeos/power/power_event_observer.h" 135#include "ash/system/chromeos/power/power_status.h" 136#include "ash/system/chromeos/power/video_activity_notifier.h" 137#include "ash/system/chromeos/session/last_window_closed_logout_reminder.h" 138#include "ash/system/chromeos/session/logout_confirmation_controller.h" 139#include "base/bind_helpers.h" 140#include "base/sys_info.h" 141#include "ui/chromeos/user_activity_power_manager_notifier.h" 142#include "ui/display/chromeos/display_configurator.h" 143#endif // defined(OS_CHROMEOS) 144 145namespace ash { 146 147namespace { 148 149using aura::Window; 150using views::Widget; 151 152// A Corewm VisibilityController subclass that calls the Ash animation routine 153// so we can pick up our extended animations. See ash/wm/window_animations.h. 154class AshVisibilityController : public ::wm::VisibilityController { 155 public: 156 AshVisibilityController() {} 157 virtual ~AshVisibilityController() {} 158 159 private: 160 // Overridden from ::wm::VisibilityController: 161 virtual bool CallAnimateOnChildWindowVisibilityChanged( 162 aura::Window* window, 163 bool visible) OVERRIDE { 164 return AnimateOnChildWindowVisibilityChanged(window, visible); 165 } 166 167 DISALLOW_COPY_AND_ASSIGN(AshVisibilityController); 168}; 169 170AshWindowTreeHostInitParams ShellInitParamsToAshWindowTreeHostInitParams( 171 const ShellInitParams& shell_init_params) { 172 AshWindowTreeHostInitParams ash_init_params; 173#if defined(OS_WIN) 174 ash_init_params.remote_hwnd = shell_init_params.remote_hwnd; 175#endif 176 return ash_init_params; 177} 178 179} // namespace 180 181// static 182Shell* Shell::instance_ = NULL; 183// static 184bool Shell::initially_hide_cursor_ = false; 185 186//////////////////////////////////////////////////////////////////////////////// 187// Shell, public: 188 189// static 190Shell* Shell::CreateInstance(const ShellInitParams& init_params) { 191 CHECK(!instance_); 192 instance_ = new Shell(init_params.delegate); 193 instance_->Init(init_params); 194 return instance_; 195} 196 197// static 198Shell* Shell::GetInstance() { 199 DCHECK(instance_); 200 return instance_; 201} 202 203// static 204bool Shell::HasInstance() { 205 return !!instance_; 206} 207 208// static 209void Shell::DeleteInstance() { 210 delete instance_; 211 instance_ = NULL; 212} 213 214// static 215RootWindowController* Shell::GetPrimaryRootWindowController() { 216 return GetRootWindowController(GetPrimaryRootWindow()); 217} 218 219// static 220Shell::RootWindowControllerList Shell::GetAllRootWindowControllers() { 221 return Shell::GetInstance()->display_controller()-> 222 GetAllRootWindowControllers(); 223} 224 225// static 226aura::Window* Shell::GetPrimaryRootWindow() { 227 return GetInstance()->display_controller()->GetPrimaryRootWindow(); 228} 229 230// static 231aura::Window* Shell::GetTargetRootWindow() { 232 Shell* shell = GetInstance(); 233 if (shell->scoped_target_root_window_) 234 return shell->scoped_target_root_window_; 235 return shell->target_root_window_; 236} 237 238// static 239gfx::Screen* Shell::GetScreen() { 240 return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE); 241} 242 243// static 244aura::Window::Windows Shell::GetAllRootWindows() { 245 return Shell::GetInstance()->display_controller()-> 246 GetAllRootWindows(); 247} 248 249// static 250aura::Window* Shell::GetContainer(aura::Window* root_window, 251 int container_id) { 252 return root_window->GetChildById(container_id); 253} 254 255// static 256const aura::Window* Shell::GetContainer(const aura::Window* root_window, 257 int container_id) { 258 return root_window->GetChildById(container_id); 259} 260 261// static 262std::vector<aura::Window*> Shell::GetContainersFromAllRootWindows( 263 int container_id, 264 aura::Window* priority_root) { 265 std::vector<aura::Window*> containers; 266 aura::Window::Windows root_windows = GetAllRootWindows(); 267 for (aura::Window::Windows::const_iterator it = root_windows.begin(); 268 it != root_windows.end(); ++it) { 269 aura::Window* container = (*it)->GetChildById(container_id); 270 if (container) { 271 if (priority_root && priority_root->Contains(container)) 272 containers.insert(containers.begin(), container); 273 else 274 containers.push_back(container); 275 } 276 } 277 return containers; 278} 279 280void Shell::ShowContextMenu(const gfx::Point& location_in_screen, 281 ui::MenuSourceType source_type) { 282 // No context menus if there is no session with an active user. 283 if (!session_state_delegate_->NumberOfLoggedInUsers()) 284 return; 285 // No context menus when screen is locked. 286 if (session_state_delegate_->IsScreenLocked()) 287 return; 288 289 aura::Window* root = 290 wm::GetRootWindowMatching(gfx::Rect(location_in_screen, gfx::Size())); 291 GetRootWindowController(root) 292 ->ShowContextMenu(location_in_screen, source_type); 293} 294 295void Shell::ShowAppList(aura::Window* window) { 296 // If the context window is not given, show it on the target root window. 297 if (!window) 298 window = GetTargetRootWindow(); 299 if (!app_list_controller_) 300 app_list_controller_.reset(new AppListController); 301 app_list_controller_->SetVisible(true, window); 302} 303 304void Shell::DismissAppList() { 305 if (!app_list_controller_) 306 return; 307 app_list_controller_->SetVisible(false, GetTargetRootWindow()); 308} 309 310void Shell::ToggleAppList(aura::Window* window) { 311 if (app_list_controller_ && app_list_controller_->IsVisible()) { 312 DismissAppList(); 313 return; 314 } 315 316 ShowAppList(window); 317} 318 319bool Shell::GetAppListTargetVisibility() const { 320 return app_list_controller_.get() && 321 app_list_controller_->GetTargetVisibility(); 322} 323 324aura::Window* Shell::GetAppListWindow() { 325 return app_list_controller_.get() ? app_list_controller_->GetWindow() : NULL; 326} 327 328app_list::AppListView* Shell::GetAppListView() { 329 return app_list_controller_.get() ? app_list_controller_->GetView() : NULL; 330} 331 332bool Shell::IsSystemModalWindowOpen() const { 333 if (simulate_modal_window_open_for_testing_) 334 return true; 335 const std::vector<aura::Window*> containers = GetContainersFromAllRootWindows( 336 kShellWindowId_SystemModalContainer, NULL); 337 for (std::vector<aura::Window*>::const_iterator cit = containers.begin(); 338 cit != containers.end(); ++cit) { 339 for (aura::Window::Windows::const_iterator wit = (*cit)->children().begin(); 340 wit != (*cit)->children().end(); ++wit) { 341 if ((*wit)->GetProperty(aura::client::kModalKey) == 342 ui::MODAL_TYPE_SYSTEM && (*wit)->TargetVisibility()) { 343 return true; 344 } 345 } 346 } 347 return false; 348} 349 350views::NonClientFrameView* Shell::CreateDefaultNonClientFrameView( 351 views::Widget* widget) { 352 // Use translucent-style window frames for dialogs. 353 return new CustomFrameViewAsh(widget); 354} 355 356void Shell::RotateFocus(Direction direction) { 357 focus_cycler_->RotateFocus(direction == FORWARD ? FocusCycler::FORWARD 358 : FocusCycler::BACKWARD); 359} 360 361void Shell::SetDisplayWorkAreaInsets(Window* contains, 362 const gfx::Insets& insets) { 363 if (!display_controller_->UpdateWorkAreaOfDisplayNearestWindow( 364 contains, insets)) { 365 return; 366 } 367 FOR_EACH_OBSERVER(ShellObserver, observers_, 368 OnDisplayWorkAreaInsetsChanged()); 369} 370 371void Shell::OnLoginStateChanged(user::LoginStatus status) { 372 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLoginStateChanged(status)); 373} 374 375void Shell::OnLoginUserProfilePrepared() { 376 CreateShelf(); 377 CreateKeyboard(); 378} 379 380void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status) { 381 RootWindowControllerList controllers = GetAllRootWindowControllers(); 382 for (RootWindowControllerList::iterator iter = controllers.begin(); 383 iter != controllers.end(); ++iter) 384 (*iter)->UpdateAfterLoginStatusChange(status); 385} 386 387void Shell::OnAppTerminating() { 388 FOR_EACH_OBSERVER(ShellObserver, observers_, OnAppTerminating()); 389} 390 391void Shell::OnLockStateChanged(bool locked) { 392 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLockStateChanged(locked)); 393#ifndef NDEBUG 394 // Make sure that there is no system modal in Lock layer when unlocked. 395 if (!locked) { 396 std::vector<aura::Window*> containers = GetContainersFromAllRootWindows( 397 kShellWindowId_LockSystemModalContainer, GetPrimaryRootWindow()); 398 for (std::vector<aura::Window*>::const_iterator iter = containers.begin(); 399 iter != containers.end(); ++iter) { 400 DCHECK_EQ(0u, (*iter)->children().size()); 401 } 402 } 403#endif 404} 405 406void Shell::OnCastingSessionStartedOrStopped(bool started) { 407#if defined(OS_CHROMEOS) && defined(USE_X11) 408 if (projecting_observer_) 409 projecting_observer_->OnCastingSessionStartedOrStopped(started); 410#endif 411} 412 413void Shell::OnOverviewModeStarting() { 414 FOR_EACH_OBSERVER(ShellObserver, observers_, OnOverviewModeStarting()); 415} 416 417void Shell::OnOverviewModeEnding() { 418 FOR_EACH_OBSERVER(ShellObserver, observers_, OnOverviewModeEnding()); 419} 420 421void Shell::OnMaximizeModeStarted() { 422 FOR_EACH_OBSERVER(ShellObserver, observers_, OnMaximizeModeStarted()); 423} 424 425void Shell::OnMaximizeModeEnded() { 426 FOR_EACH_OBSERVER(ShellObserver, observers_, OnMaximizeModeEnded()); 427} 428 429void Shell::OnRootWindowAdded(aura::Window* root_window) { 430 FOR_EACH_OBSERVER(ShellObserver, observers_, OnRootWindowAdded(root_window)); 431} 432 433void Shell::CreateShelf() { 434 RootWindowControllerList controllers = GetAllRootWindowControllers(); 435 for (RootWindowControllerList::iterator iter = controllers.begin(); 436 iter != controllers.end(); ++iter) 437 (*iter)->shelf()->CreateShelf(); 438} 439 440void Shell::OnShelfCreatedForRootWindow(aura::Window* root_window) { 441 FOR_EACH_OBSERVER(ShellObserver, 442 observers_, 443 OnShelfCreatedForRootWindow(root_window)); 444} 445 446void Shell::CreateKeyboard() { 447 // TODO(bshe): Primary root window controller may not be the controller to 448 // attach virtual keyboard. See http://crbug.com/303429 449 InitKeyboard(); 450 if (keyboard::IsKeyboardUsabilityExperimentEnabled()) { 451 display_controller()->virtual_keyboard_window_controller()-> 452 ActivateKeyboard(keyboard::KeyboardController::GetInstance()); 453 } else { 454 GetPrimaryRootWindowController()-> 455 ActivateKeyboard(keyboard::KeyboardController::GetInstance()); 456 } 457} 458 459void Shell::DeactivateKeyboard() { 460 if (keyboard::KeyboardController::GetInstance()) { 461 RootWindowControllerList controllers = GetAllRootWindowControllers(); 462 for (RootWindowControllerList::iterator iter = controllers.begin(); 463 iter != controllers.end(); ++iter) { 464 (*iter)->DeactivateKeyboard(keyboard::KeyboardController::GetInstance()); 465 } 466 } 467 keyboard::KeyboardController::ResetInstance(NULL); 468} 469 470void Shell::ShowShelf() { 471 RootWindowControllerList controllers = GetAllRootWindowControllers(); 472 for (RootWindowControllerList::iterator iter = controllers.begin(); 473 iter != controllers.end(); ++iter) 474 (*iter)->ShowShelf(); 475} 476 477void Shell::AddShellObserver(ShellObserver* observer) { 478 observers_.AddObserver(observer); 479} 480 481void Shell::RemoveShellObserver(ShellObserver* observer) { 482 observers_.RemoveObserver(observer); 483} 484 485#if defined(OS_CHROMEOS) 486bool Shell::ShouldSaveDisplaySettings() { 487 return !((maximize_mode_controller_->IsMaximizeModeWindowManagerEnabled() && 488 maximize_mode_controller_-> 489 ignore_display_configuration_updates()) || 490 resolution_notification_controller_->DoesNotificationTimeout()); 491} 492#endif 493 494void Shell::UpdateShelfVisibility() { 495 RootWindowControllerList controllers = GetAllRootWindowControllers(); 496 for (RootWindowControllerList::iterator iter = controllers.begin(); 497 iter != controllers.end(); ++iter) 498 if ((*iter)->shelf()) 499 (*iter)->UpdateShelfVisibility(); 500} 501 502void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior, 503 aura::Window* root_window) { 504 ash::ShelfLayoutManager::ForShelf(root_window)->SetAutoHideBehavior(behavior); 505} 506 507ShelfAutoHideBehavior Shell::GetShelfAutoHideBehavior( 508 aura::Window* root_window) const { 509 return ash::ShelfLayoutManager::ForShelf(root_window)->auto_hide_behavior(); 510} 511 512void Shell::SetShelfAlignment(ShelfAlignment alignment, 513 aura::Window* root_window) { 514 if (ash::ShelfLayoutManager::ForShelf(root_window)->SetAlignment(alignment)) { 515 FOR_EACH_OBSERVER( 516 ShellObserver, observers_, OnShelfAlignmentChanged(root_window)); 517 } 518} 519 520ShelfAlignment Shell::GetShelfAlignment(const aura::Window* root_window) { 521 return GetRootWindowController(root_window) 522 ->GetShelfLayoutManager() 523 ->GetAlignment(); 524} 525 526void Shell::SetDimming(bool should_dim) { 527 RootWindowControllerList controllers = GetAllRootWindowControllers(); 528 for (RootWindowControllerList::iterator iter = controllers.begin(); 529 iter != controllers.end(); ++iter) 530 (*iter)->screen_dimmer()->SetDimming(should_dim); 531} 532 533void Shell::NotifyFullscreenStateChange(bool is_fullscreen, 534 aura::Window* root_window) { 535 FOR_EACH_OBSERVER(ShellObserver, observers_, OnFullscreenStateChanged( 536 is_fullscreen, root_window)); 537} 538 539void Shell::CreateModalBackground(aura::Window* window) { 540 if (!modality_filter_) { 541 modality_filter_.reset(new SystemModalContainerEventFilter(this)); 542 AddPreTargetHandler(modality_filter_.get()); 543 } 544 RootWindowControllerList controllers = GetAllRootWindowControllers(); 545 for (RootWindowControllerList::iterator iter = controllers.begin(); 546 iter != controllers.end(); ++iter) 547 (*iter)->GetSystemModalLayoutManager(window)->CreateModalBackground(); 548} 549 550void Shell::OnModalWindowRemoved(aura::Window* removed) { 551 RootWindowControllerList controllers = GetAllRootWindowControllers(); 552 bool activated = false; 553 for (RootWindowControllerList::iterator iter = controllers.begin(); 554 iter != controllers.end() && !activated; ++iter) { 555 activated = (*iter)->GetSystemModalLayoutManager(removed)-> 556 ActivateNextModalWindow(); 557 } 558 if (!activated) { 559 RemovePreTargetHandler(modality_filter_.get()); 560 modality_filter_.reset(); 561 for (RootWindowControllerList::iterator iter = controllers.begin(); 562 iter != controllers.end(); ++iter) 563 (*iter)->GetSystemModalLayoutManager(removed)->DestroyModalBackground(); 564 } 565} 566 567WebNotificationTray* Shell::GetWebNotificationTray() { 568 return GetPrimaryRootWindowController()->shelf()-> 569 status_area_widget()->web_notification_tray(); 570} 571 572bool Shell::HasPrimaryStatusArea() { 573 ShelfWidget* shelf = GetPrimaryRootWindowController()->shelf(); 574 return shelf && shelf->status_area_widget(); 575} 576 577SystemTray* Shell::GetPrimarySystemTray() { 578 return GetPrimaryRootWindowController()->GetSystemTray(); 579} 580 581ShelfDelegate* Shell::GetShelfDelegate() { 582 if (!shelf_delegate_) { 583 shelf_model_.reset(new ShelfModel); 584 // Creates ShelfItemDelegateManager before ShelfDelegate. 585 shelf_item_delegate_manager_.reset( 586 new ShelfItemDelegateManager(shelf_model_.get())); 587 588 shelf_delegate_.reset(delegate_->CreateShelfDelegate(shelf_model_.get())); 589 scoped_ptr<ShelfItemDelegate> controller(new AppListShelfItemDelegate); 590 591 // Finding the shelf model's location of the app list and setting its 592 // ShelfItemDelegate. 593 int app_list_index = shelf_model_->GetItemIndexForType(TYPE_APP_LIST); 594 DCHECK_GE(app_list_index, 0); 595 ShelfID app_list_id = shelf_model_->items()[app_list_index].id; 596 DCHECK(app_list_id); 597 shelf_item_delegate_manager_->SetShelfItemDelegate(app_list_id, 598 controller.Pass()); 599 shelf_window_watcher_.reset(new ShelfWindowWatcher( 600 shelf_model_.get(), shelf_item_delegate_manager_.get())); 601 } 602 return shelf_delegate_.get(); 603} 604 605void Shell::SetTouchHudProjectionEnabled(bool enabled) { 606 if (is_touch_hud_projection_enabled_ == enabled) 607 return; 608 609 is_touch_hud_projection_enabled_ = enabled; 610 FOR_EACH_OBSERVER(ShellObserver, observers_, 611 OnTouchHudProjectionToggled(enabled)); 612} 613 614#if defined(OS_CHROMEOS) 615ash::FirstRunHelper* Shell::CreateFirstRunHelper() { 616 return new ash::FirstRunHelperImpl; 617} 618 619void Shell::SetCursorCompositingEnabled(bool enabled) { 620 display_controller_->cursor_window_controller()->SetCursorCompositingEnabled( 621 enabled); 622 native_cursor_manager_->SetNativeCursorEnabled(!enabled); 623} 624#endif // defined(OS_CHROMEOS) 625 626void Shell::DoInitialWorkspaceAnimation() { 627 return GetPrimaryRootWindowController()->workspace_controller()-> 628 DoInitialAnimation(); 629} 630 631//////////////////////////////////////////////////////////////////////////////// 632// Shell, private: 633 634Shell::Shell(ShellDelegate* delegate) 635 : target_root_window_(NULL), 636 scoped_target_root_window_(NULL), 637 delegate_(delegate), 638 window_positioner_(new WindowPositioner), 639 activation_client_(NULL), 640 accelerometer_controller_(new AccelerometerController()), 641#if defined(OS_CHROMEOS) 642 display_configurator_(new ui::DisplayConfigurator()), 643#endif // defined(OS_CHROMEOS) 644 native_cursor_manager_(new AshNativeCursorManager), 645 cursor_manager_( 646 scoped_ptr< ::wm::NativeCursorManager>(native_cursor_manager_)), 647 simulate_modal_window_open_for_testing_(false), 648 is_touch_hud_projection_enabled_(false) { 649 DCHECK(delegate_.get()); 650 gpu_support_.reset(delegate_->CreateGPUSupport()); 651 display_manager_.reset(new DisplayManager); 652 display_controller_.reset(new DisplayController); 653#if defined(OS_CHROMEOS) && defined(USE_X11) 654 user_metrics_recorder_.reset(new UserMetricsRecorder); 655#endif // defined(OS_CHROMEOS) 656 657#if defined(OS_CHROMEOS) 658 PowerStatus::Initialize(); 659#endif 660} 661 662Shell::~Shell() { 663 TRACE_EVENT0("shutdown", "ash::Shell::Destructor"); 664 665 delegate_->PreShutdown(); 666 667 views::FocusManagerFactory::Install(NULL); 668 669 // Remove the focus from any window. This will prevent overhead and side 670 // effects (e.g. crashes) from changing focus during shutdown. 671 // See bug crbug.com/134502. 672 aura::client::GetFocusClient(GetPrimaryRootWindow())->FocusWindow(NULL); 673 674 // Please keep in same order as in Init() because it's easy to miss one. 675 if (window_modality_controller_) 676 window_modality_controller_.reset(); 677#if defined(OS_CHROMEOS) && defined(USE_X11) 678 RemovePreTargetHandler(magnifier_key_scroll_handler_.get()); 679 magnifier_key_scroll_handler_.reset(); 680 681 RemovePreTargetHandler(speech_feedback_handler_.get()); 682 speech_feedback_handler_.reset(); 683#endif 684 RemovePreTargetHandler(user_activity_detector_.get()); 685 RemovePreTargetHandler(overlay_filter_.get()); 686 RemovePreTargetHandler(input_method_filter_.get()); 687 RemovePreTargetHandler(accelerator_filter_.get()); 688 RemovePreTargetHandler(event_transformation_handler_.get()); 689 RemovePreTargetHandler(toplevel_window_event_handler_.get()); 690 RemovePostTargetHandler(toplevel_window_event_handler_.get()); 691 RemovePreTargetHandler(system_gesture_filter_.get()); 692 RemovePreTargetHandler(keyboard_metrics_filter_.get()); 693 RemovePreTargetHandler(mouse_cursor_filter_.get()); 694 695 // TooltipController is deleted with the Shell so removing its references. 696 RemovePreTargetHandler(tooltip_controller_.get()); 697 698 // Destroy maximize mode controller early on since it has some observers which 699 // need to be removed. 700 maximize_mode_controller_->Shutdown(); 701 maximize_mode_controller_.reset(); 702 703 // AppList needs to be released before shelf layout manager, which is 704 // destroyed with shelf container in the loop below. However, app list 705 // container is now on top of shelf container and released after it. 706 // TODO(xiyuan): Move it back when app list container is no longer needed. 707 app_list_controller_.reset(); 708 709#if defined(OS_CHROMEOS) 710 // Destroy the LastWindowClosedLogoutReminder before the 711 // LogoutConfirmationController. 712 last_window_closed_logout_reminder_.reset(); 713 714 // Destroy the LogoutConfirmationController before the SystemTrayDelegate. 715 logout_confirmation_controller_.reset(); 716#endif 717 718 // Destroy SystemTrayDelegate before destroying the status area(s). 719 system_tray_delegate_->Shutdown(); 720 system_tray_delegate_.reset(); 721 722 locale_notification_controller_.reset(); 723 724 // Drag-and-drop must be canceled prior to close all windows. 725 drag_drop_controller_.reset(); 726 727 // Controllers who have WindowObserver added must be deleted 728 // before |display_controller_| is deleted. 729 730#if defined(OS_CHROMEOS) 731 // VideoActivityNotifier must be deleted before |video_detector_| is 732 // deleted because it's observing video activity through 733 // VideoDetectorObserver interface. 734 video_activity_notifier_.reset(); 735#endif // defined(OS_CHROMEOS) 736 video_detector_.reset(); 737 high_contrast_controller_.reset(); 738 739 shadow_controller_.reset(); 740 resize_shadow_controller_.reset(); 741 742 window_cycle_controller_.reset(); 743 window_selector_controller_.reset(); 744 mru_window_tracker_.reset(); 745 746 // |shelf_window_watcher_| has a weak pointer to |shelf_Model_| 747 // and has window observers. 748 shelf_window_watcher_.reset(); 749 750 // Destroy all child windows including widgets. 751 display_controller_->CloseChildWindows(); 752 display_controller_->CloseNonDesktopDisplay(); 753 754 // Chrome implementation of shelf delegate depends on FocusClient, 755 // so must be deleted before |focus_client_|. 756 shelf_delegate_.reset(); 757 focus_client_.reset(); 758 759 // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems 760 // needs to remove observers from it. 761 system_tray_notifier_.reset(); 762 763 // These need a valid Shell instance to clean up properly, so explicitly 764 // delete them before invalidating the instance. 765 // Alphabetical. TODO(oshima): sort. 766 magnification_controller_.reset(); 767 partial_magnification_controller_.reset(); 768 tooltip_controller_.reset(); 769 event_client_.reset(); 770 nested_accelerator_controller_.reset(); 771 toplevel_window_event_handler_.reset(); 772 visibility_controller_.reset(); 773 // |shelf_item_delegate_manager_| observes |shelf_model_|. It must be 774 // destroyed before |shelf_model_| is destroyed. 775 shelf_item_delegate_manager_.reset(); 776 shelf_model_.reset(); 777 778 power_button_controller_.reset(); 779 lock_state_controller_.reset(); 780 781#if defined(OS_CHROMEOS) 782 resolution_notification_controller_.reset(); 783#endif 784 desktop_background_controller_.reset(); 785 mouse_cursor_filter_.reset(); 786 787#if defined(OS_CHROMEOS) && defined(USE_X11) 788 touch_transformer_controller_.reset(); 789#endif // defined(OS_CHROMEOS) && defined(USE_X11) 790 791 // This also deletes all RootWindows. Note that we invoke Shutdown() on 792 // DisplayController before resetting |display_controller_|, since destruction 793 // of its owned RootWindowControllers relies on the value. 794 display_manager_->CreateScreenForShutdown(); 795 display_controller_->Shutdown(); 796 display_controller_.reset(); 797 screen_position_controller_.reset(); 798 accessibility_delegate_.reset(); 799 new_window_delegate_.reset(); 800 media_delegate_.reset(); 801 802 keyboard::KeyboardController::ResetInstance(NULL); 803 804#if defined(OS_CHROMEOS) 805 if (display_change_observer_) 806 display_configurator_->RemoveObserver(display_change_observer_.get()); 807 if (display_configurator_animation_) 808 display_configurator_->RemoveObserver( 809 display_configurator_animation_.get()); 810 if (display_error_observer_) 811 display_configurator_->RemoveObserver(display_error_observer_.get()); 812 if (projecting_observer_) 813 display_configurator_->RemoveObserver(projecting_observer_.get()); 814 display_change_observer_.reset(); 815#endif // defined(OS_CHROMEOS) 816 817#if defined(OS_CHROMEOS) 818 PowerStatus::Shutdown(); 819#endif 820 821 DCHECK(instance_ == this); 822 instance_ = NULL; 823} 824 825void Shell::Init(const ShellInitParams& init_params) { 826 delegate_->PreInit(); 827 if (keyboard::IsKeyboardUsabilityExperimentEnabled()) { 828 display_manager_->SetSecondDisplayMode(DisplayManager::VIRTUAL_KEYBOARD); 829 } 830 bool display_initialized = display_manager_->InitFromCommandLine(); 831#if defined(OS_CHROMEOS) 832 display_configurator_->Init(!gpu_support_->IsPanelFittingDisabled()); 833 display_configurator_animation_.reset(new DisplayConfiguratorAnimation()); 834 display_configurator_->AddObserver(display_configurator_animation_.get()); 835 836 projecting_observer_.reset(new ProjectingObserver()); 837 display_configurator_->AddObserver(projecting_observer_.get()); 838 839 if (!display_initialized && base::SysInfo::IsRunningOnChromeOS()) { 840 display_change_observer_.reset(new DisplayChangeObserver); 841 // Register |display_change_observer_| first so that the rest of 842 // observer gets invoked after the root windows are configured. 843 display_configurator_->AddObserver(display_change_observer_.get()); 844 display_error_observer_.reset(new DisplayErrorObserver()); 845 display_configurator_->AddObserver(display_error_observer_.get()); 846 display_configurator_->set_state_controller(display_change_observer_.get()); 847 display_configurator_->set_mirroring_controller(display_manager_.get()); 848 display_configurator_->ForceInitialConfigure( 849 delegate_->IsFirstRunAfterBoot() ? kChromeOsBootColor : 0); 850 display_initialized = true; 851 } 852#endif // defined(OS_CHROMEOS) 853 if (!display_initialized) 854 display_manager_->InitDefaultDisplay(); 855 856 display_manager_->InitFontParams(); 857 858 // Install the custom factory first so that views::FocusManagers for Tray, 859 // Shelf, and WallPaper could be created by the factory. 860 views::FocusManagerFactory::Install(new AshFocusManagerFactory); 861 862 aura::Env::CreateInstance(true); 863 aura::Env::GetInstance()->set_context_factory(init_params.context_factory); 864 865 // The WindowModalityController needs to be at the front of the input event 866 // pretarget handler list to ensure that it processes input events when modal 867 // windows are active. 868 window_modality_controller_.reset( 869 new ::wm::WindowModalityController(this)); 870 871 env_filter_.reset(new ::wm::CompoundEventFilter); 872 AddPreTargetHandler(env_filter_.get()); 873 874 ::wm::FocusController* focus_controller = 875 new ::wm::FocusController(new wm::AshFocusRules); 876 focus_client_.reset(focus_controller); 877 activation_client_ = focus_controller; 878 activation_client_->AddObserver(this); 879 focus_cycler_.reset(new FocusCycler()); 880 881 screen_position_controller_.reset(new ScreenPositionController); 882 883 display_controller_->Start(); 884 display_controller_->CreatePrimaryHost( 885 ShellInitParamsToAshWindowTreeHostInitParams(init_params)); 886 aura::Window* root_window = display_controller_->GetPrimaryRootWindow(); 887 target_root_window_ = root_window; 888 889#if defined(OS_CHROMEOS) 890 resolution_notification_controller_.reset( 891 new ResolutionNotificationController); 892#endif 893 894 cursor_manager_.SetDisplay(GetScreen()->GetPrimaryDisplay()); 895 896 nested_accelerator_controller_.reset( 897 new ::wm::NestedAcceleratorController(new NestedAcceleratorDelegate)); 898 accelerator_controller_.reset(new AcceleratorController); 899 maximize_mode_controller_.reset(new MaximizeModeController()); 900 901#if defined(OS_CHROMEOS) && defined(USE_X11) 902 magnifier_key_scroll_handler_ = MagnifierKeyScroller::CreateHandler().Pass(); 903 AddPreTargetHandler(magnifier_key_scroll_handler_.get()); 904 speech_feedback_handler_ = SpokenFeedbackToggler::CreateHandler().Pass(); 905 AddPreTargetHandler(speech_feedback_handler_.get()); 906#endif 907 908 // The order in which event filters are added is significant. 909 910 // wm::UserActivityDetector passes events to observers, so let them get 911 // rewritten first. 912 user_activity_detector_.reset(new ::wm::UserActivityDetector); 913 AddPreTargetHandler(user_activity_detector_.get()); 914 915 overlay_filter_.reset(new OverlayEventFilter); 916 AddPreTargetHandler(overlay_filter_.get()); 917 AddShellObserver(overlay_filter_.get()); 918 919 input_method_filter_.reset(new ::wm::InputMethodEventFilter( 920 root_window->GetHost()->GetAcceleratedWidget())); 921 AddPreTargetHandler(input_method_filter_.get()); 922 923 accelerator_filter_.reset(new ::wm::AcceleratorFilter( 924 scoped_ptr< ::wm::AcceleratorDelegate>(new AcceleratorDelegate).Pass())); 925 AddPreTargetHandler(accelerator_filter_.get()); 926 927 event_transformation_handler_.reset(new EventTransformationHandler); 928 AddPreTargetHandler(event_transformation_handler_.get()); 929 930 toplevel_window_event_handler_.reset(new ToplevelWindowEventHandler); 931 932 system_gesture_filter_.reset(new SystemGestureEventFilter); 933 AddPreTargetHandler(system_gesture_filter_.get()); 934 935 keyboard_metrics_filter_.reset(new KeyboardUMAEventFilter); 936 AddPreTargetHandler(keyboard_metrics_filter_.get()); 937 938 // The keyboard system must be initialized before the RootWindowController is 939 // created. 940#if defined(OS_CHROMEOS) 941 keyboard::InitializeKeyboard(); 942#endif 943 944#if defined(OS_CHROMEOS) 945 sticky_keys_controller_.reset(new StickyKeysController); 946#endif 947 948 lock_state_controller_.reset(new LockStateController); 949 power_button_controller_.reset(new PowerButtonController( 950 lock_state_controller_.get())); 951#if defined(OS_CHROMEOS) 952 // Pass the initial display state to PowerButtonController. 953 power_button_controller_->OnDisplayModeChanged( 954 display_configurator_->cached_displays()); 955#endif 956 AddShellObserver(lock_state_controller_.get()); 957 958 drag_drop_controller_.reset(new DragDropController); 959 mouse_cursor_filter_.reset(new MouseCursorEventFilter()); 960 PrependPreTargetHandler(mouse_cursor_filter_.get()); 961 962 // Create Controllers that may need root window. 963 // TODO(oshima): Move as many controllers before creating 964 // RootWindowController as possible. 965 visibility_controller_.reset(new AshVisibilityController); 966 967 magnification_controller_.reset( 968 MagnificationController::CreateInstance()); 969 mru_window_tracker_.reset(new MruWindowTracker(activation_client_)); 970 971 partial_magnification_controller_.reset( 972 new PartialMagnificationController()); 973 974 autoclick_controller_.reset(AutoclickController::CreateInstance()); 975 976 high_contrast_controller_.reset(new HighContrastController); 977 video_detector_.reset(new VideoDetector); 978 window_selector_controller_.reset(new WindowSelectorController()); 979 window_cycle_controller_.reset(new WindowCycleController()); 980 981 tooltip_controller_.reset( 982 new views::corewm::TooltipController( 983 scoped_ptr<views::corewm::Tooltip>( 984 new views::corewm::TooltipAura(gfx::SCREEN_TYPE_ALTERNATE)))); 985 AddPreTargetHandler(tooltip_controller_.get()); 986 987 event_client_.reset(new EventClientImpl); 988 989 // This controller needs to be set before SetupManagedWindowMode. 990 desktop_background_controller_.reset(new DesktopBackgroundController()); 991 user_wallpaper_delegate_.reset(delegate_->CreateUserWallpaperDelegate()); 992 993 session_state_delegate_.reset(delegate_->CreateSessionStateDelegate()); 994 accessibility_delegate_.reset(delegate_->CreateAccessibilityDelegate()); 995 new_window_delegate_.reset(delegate_->CreateNewWindowDelegate()); 996 media_delegate_.reset(delegate_->CreateMediaDelegate()); 997 998 resize_shadow_controller_.reset(new ResizeShadowController()); 999 shadow_controller_.reset( 1000 new ::wm::ShadowController(activation_client_)); 1001 1002 // Create system_tray_notifier_ before the delegate. 1003 system_tray_notifier_.reset(new ash::SystemTrayNotifier()); 1004 1005 // Initialize system_tray_delegate_ before initializing StatusAreaWidget. 1006 system_tray_delegate_.reset(delegate()->CreateSystemTrayDelegate()); 1007 DCHECK(system_tray_delegate_.get()); 1008 1009 locale_notification_controller_.reset(new LocaleNotificationController); 1010 1011 // Initialize system_tray_delegate_ after StatusAreaWidget is created. 1012 system_tray_delegate_->Initialize(); 1013 1014#if defined(OS_CHROMEOS) 1015 // Create the LogoutConfirmationController after the SystemTrayDelegate. 1016 logout_confirmation_controller_.reset(new LogoutConfirmationController( 1017 base::Bind(&SystemTrayDelegate::SignOut, 1018 base::Unretained(system_tray_delegate_.get())))); 1019#endif 1020 1021#if defined(OS_CHROMEOS) && defined(USE_X11) 1022 // Create TouchTransformerController before DisplayController::InitDisplays() 1023 // since TouchTransformerController listens on 1024 // DisplayController::Observer::OnDisplaysInitialized(). 1025 touch_transformer_controller_.reset(new TouchTransformerController()); 1026#endif // defined(OS_CHROMEOS) && defined(USE_X11) 1027 1028 display_controller_->InitDisplays(); 1029 1030 // It needs to be created after RootWindowController has been created 1031 // (which calls OnWindowResized has been called, otherwise the 1032 // widget will not paint when restoring after a browser crash. Also it needs 1033 // to be created after InitSecondaryDisplays() to initialize the wallpapers in 1034 // the correct size. 1035 user_wallpaper_delegate_->InitializeWallpaper(); 1036 1037 if (initially_hide_cursor_) 1038 cursor_manager_.HideCursor(); 1039 cursor_manager_.SetCursor(ui::kCursorPointer); 1040 1041#if defined(OS_CHROMEOS) 1042 // Set accelerator controller delegates. 1043 accelerator_controller_->SetBrightnessControlDelegate( 1044 scoped_ptr<ash::BrightnessControlDelegate>( 1045 new ash::system::BrightnessControllerChromeos).Pass()); 1046 1047 power_event_observer_.reset(new PowerEventObserver()); 1048 user_activity_notifier_.reset( 1049 new ui::UserActivityPowerManagerNotifier(user_activity_detector_.get())); 1050 video_activity_notifier_.reset( 1051 new VideoActivityNotifier(video_detector_.get())); 1052 bluetooth_notification_controller_.reset(new BluetoothNotificationController); 1053 last_window_closed_logout_reminder_.reset(new LastWindowClosedLogoutReminder); 1054#endif 1055 1056 weak_display_manager_factory_.reset( 1057 new base::WeakPtrFactory<DisplayManager>(display_manager_.get())); 1058 // The compositor thread and main message loop have to be running in 1059 // order to create mirror window. Run it after the main message loop 1060 // is started. 1061 base::MessageLoopForUI::current()->PostTask( 1062 FROM_HERE, 1063 base::Bind(&DisplayManager::CreateMirrorWindowIfAny, 1064 weak_display_manager_factory_->GetWeakPtr())); 1065} 1066 1067void Shell::InitKeyboard() { 1068 if (keyboard::IsKeyboardEnabled()) { 1069 if (keyboard::KeyboardController::GetInstance()) { 1070 RootWindowControllerList controllers = GetAllRootWindowControllers(); 1071 for (RootWindowControllerList::iterator iter = controllers.begin(); 1072 iter != controllers.end(); ++iter) { 1073 (*iter)->DeactivateKeyboard( 1074 keyboard::KeyboardController::GetInstance()); 1075 } 1076 } 1077 keyboard::KeyboardControllerProxy* proxy = 1078 delegate_->CreateKeyboardControllerProxy(); 1079 keyboard::KeyboardController::ResetInstance( 1080 new keyboard::KeyboardController(proxy)); 1081 } 1082} 1083 1084void Shell::InitRootWindow(aura::Window* root_window) { 1085 DCHECK(activation_client_); 1086 DCHECK(visibility_controller_.get()); 1087 DCHECK(drag_drop_controller_.get()); 1088 1089 aura::client::SetFocusClient(root_window, focus_client_.get()); 1090 input_method_filter_->SetInputMethodPropertyInRootWindow(root_window); 1091 aura::client::SetActivationClient(root_window, activation_client_); 1092 ::wm::FocusController* focus_controller = 1093 static_cast< ::wm::FocusController*>(activation_client_); 1094 root_window->AddPreTargetHandler(focus_controller); 1095 aura::client::SetVisibilityClient(root_window, visibility_controller_.get()); 1096 aura::client::SetDragDropClient(root_window, drag_drop_controller_.get()); 1097 aura::client::SetScreenPositionClient(root_window, 1098 screen_position_controller_.get()); 1099 aura::client::SetCursorClient(root_window, &cursor_manager_); 1100 aura::client::SetTooltipClient(root_window, tooltip_controller_.get()); 1101 aura::client::SetEventClient(root_window, event_client_.get()); 1102 1103 aura::client::SetWindowMoveClient(root_window, 1104 toplevel_window_event_handler_.get()); 1105 root_window->AddPreTargetHandler(toplevel_window_event_handler_.get()); 1106 root_window->AddPostTargetHandler(toplevel_window_event_handler_.get()); 1107 1108 if (nested_accelerator_controller_) { 1109 aura::client::SetDispatcherClient(root_window, 1110 nested_accelerator_controller_.get()); 1111 } 1112} 1113 1114bool Shell::CanWindowReceiveEvents(aura::Window* window) { 1115 RootWindowControllerList controllers = GetAllRootWindowControllers(); 1116 for (RootWindowControllerList::iterator iter = controllers.begin(); 1117 iter != controllers.end(); ++iter) { 1118 SystemModalContainerLayoutManager* layout_manager = 1119 (*iter)->GetSystemModalLayoutManager(window); 1120 if (layout_manager && layout_manager->CanWindowReceiveEvents(window)) 1121 return true; 1122 // Allow events to fall through to the virtual keyboard even if displaying 1123 // a system modal dialog. 1124 if ((*iter)->IsVirtualKeyboardWindow(window)) 1125 return true; 1126 } 1127 1128 return false; 1129} 1130 1131//////////////////////////////////////////////////////////////////////////////// 1132// Shell, ui::EventTarget overrides: 1133 1134bool Shell::CanAcceptEvent(const ui::Event& event) { 1135 return true; 1136} 1137 1138ui::EventTarget* Shell::GetParentTarget() { 1139 return aura::Env::GetInstance(); 1140} 1141 1142scoped_ptr<ui::EventTargetIterator> Shell::GetChildIterator() const { 1143 return scoped_ptr<ui::EventTargetIterator>(); 1144} 1145 1146ui::EventTargeter* Shell::GetEventTargeter() { 1147 NOTREACHED(); 1148 return NULL; 1149} 1150 1151void Shell::OnEvent(ui::Event* event) { 1152} 1153 1154//////////////////////////////////////////////////////////////////////////////// 1155// Shell, aura::client::ActivationChangeObserver implementation: 1156 1157void Shell::OnWindowActivated(aura::Window* gained_active, 1158 aura::Window* lost_active) { 1159 if (gained_active) 1160 target_root_window_ = gained_active->GetRootWindow(); 1161} 1162 1163} // namespace ash 1164