Home | History | Annotate | Download | only in ash
      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/mouse_cursor_event_filter.h"
     20 #include "ash/display/resolution_notification_controller.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/locale/locale_notification_controller.h"
     39 #include "ash/system/status_area_widget.h"
     40 #include "ash/system/tray/system_tray_delegate.h"
     41 #include "ash/system/tray/system_tray_notifier.h"
     42 #include "ash/wm/activation_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/mru_window_tracker.h"
     56 #include "ash/wm/overlay_event_filter.h"
     57 #include "ash/wm/power_button_controller.h"
     58 #include "ash/wm/property_util.h"
     59 #include "ash/wm/resize_shadow_controller.h"
     60 #include "ash/wm/root_window_layout_manager.h"
     61 #include "ash/wm/screen_dimmer.h"
     62 #include "ash/wm/session_state_controller_impl.h"
     63 #include "ash/wm/system_gesture_event_filter.h"
     64 #include "ash/wm/system_modal_container_event_filter.h"
     65 #include "ash/wm/system_modal_container_layout_manager.h"
     66 #include "ash/wm/user_activity_detector.h"
     67 #include "ash/wm/video_detector.h"
     68 #include "ash/wm/window_animations.h"
     69 #include "ash/wm/window_cycle_controller.h"
     70 #include "ash/wm/window_properties.h"
     71 #include "ash/wm/window_selector_controller.h"
     72 #include "ash/wm/window_util.h"
     73 #include "ash/wm/workspace_controller.h"
     74 #include "base/bind.h"
     75 #include "base/command_line.h"
     76 #include "base/debug/leak_annotations.h"
     77 #include "ui/aura/client/aura_constants.h"
     78 #include "ui/aura/client/user_action_client.h"
     79 #include "ui/aura/env.h"
     80 #include "ui/aura/focus_manager.h"
     81 #include "ui/aura/layout_manager.h"
     82 #include "ui/aura/root_window.h"
     83 #include "ui/aura/window.h"
     84 #include "ui/base/ui_base_switches.h"
     85 #include "ui/compositor/layer.h"
     86 #include "ui/compositor/layer_animator.h"
     87 #include "ui/gfx/display.h"
     88 #include "ui/gfx/image/image_skia.h"
     89 #include "ui/gfx/screen.h"
     90 #include "ui/gfx/size.h"
     91 #include "ui/keyboard/keyboard.h"
     92 #include "ui/keyboard/keyboard_util.h"
     93 #include "ui/message_center/message_center.h"
     94 #include "ui/views/corewm/compound_event_filter.h"
     95 #include "ui/views/corewm/corewm_switches.h"
     96 #include "ui/views/corewm/focus_controller.h"
     97 #include "ui/views/corewm/input_method_event_filter.h"
     98 #include "ui/views/corewm/shadow_controller.h"
     99 #include "ui/views/corewm/tooltip_controller.h"
    100 #include "ui/views/corewm/visibility_controller.h"
    101 #include "ui/views/corewm/window_modality_controller.h"
    102 #include "ui/views/focus/focus_manager_factory.h"
    103 #include "ui/views/widget/native_widget_aura.h"
    104 #include "ui/views/widget/widget.h"
    105 
    106 #if !defined(OS_MACOSX)
    107 #include "ash/accelerators/accelerator_controller.h"
    108 #include "ash/accelerators/accelerator_filter.h"
    109 #include "ash/accelerators/nested_dispatcher_controller.h"
    110 #endif
    111 
    112 #if defined(OS_CHROMEOS)
    113 #if defined(USE_X11)
    114 #include "ash/ash_constants.h"
    115 #include "ash/display/display_change_observer_x11.h"
    116 #include "ash/display/display_error_observer.h"
    117 #include "ash/display/output_configurator_animation.h"
    118 #include "base/chromeos/chromeos_version.h"
    119 #include "base/message_loop/message_pump_aurax11.h"
    120 #include "chromeos/display/output_configurator.h"
    121 #include "content/public/browser/gpu_data_manager.h"
    122 #include "content/public/common/content_switches.h"
    123 #include "gpu/config/gpu_feature_type.h"
    124 #endif  // defined(USE_X11)
    125 #include "ash/system/chromeos/power/power_status.h"
    126 #endif  // defined(OS_CHROMEOS)
    127 
    128 namespace ash {
    129 
    130 namespace {
    131 
    132 using aura::Window;
    133 using views::Widget;
    134 
    135 // This dummy class is used for shell unit tests. We dont have chrome delegate
    136 // in these tests.
    137 class DummyUserWallpaperDelegate : public UserWallpaperDelegate {
    138  public:
    139   DummyUserWallpaperDelegate() {}
    140 
    141   virtual ~DummyUserWallpaperDelegate() {}
    142 
    143   virtual int GetAnimationType() OVERRIDE {
    144     return views::corewm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE;
    145   }
    146 
    147   virtual bool ShouldShowInitialAnimation() OVERRIDE {
    148     return false;
    149   }
    150 
    151   virtual void UpdateWallpaper() OVERRIDE {
    152   }
    153 
    154   virtual void InitializeWallpaper() OVERRIDE {
    155     ash::Shell::GetInstance()->desktop_background_controller()->
    156         CreateEmptyWallpaper();
    157   }
    158 
    159   virtual void OpenSetWallpaperPage() OVERRIDE {
    160   }
    161 
    162   virtual bool CanOpenSetWallpaperPage() OVERRIDE {
    163     return false;
    164   }
    165 
    166   virtual void OnWallpaperAnimationFinished() OVERRIDE {
    167   }
    168 
    169   virtual void OnWallpaperBootAnimationFinished() OVERRIDE {
    170   }
    171 
    172  private:
    173   DISALLOW_COPY_AND_ASSIGN(DummyUserWallpaperDelegate);
    174 };
    175 
    176 // A Corewm VisibilityController subclass that calls the Ash animation routine
    177 // so we can pick up our extended animations. See ash/wm/window_animations.h.
    178 class AshVisibilityController : public views::corewm::VisibilityController {
    179  public:
    180   AshVisibilityController() {}
    181   virtual ~AshVisibilityController() {}
    182 
    183  private:
    184   // Overridden from views::corewm::VisibilityController:
    185   virtual bool CallAnimateOnChildWindowVisibilityChanged(
    186       aura::Window* window,
    187       bool visible) OVERRIDE {
    188     return AnimateOnChildWindowVisibilityChanged(window, visible);
    189   }
    190 
    191   DISALLOW_COPY_AND_ASSIGN(AshVisibilityController);
    192 };
    193 
    194 }  // namespace
    195 
    196 // static
    197 Shell* Shell::instance_ = NULL;
    198 // static
    199 bool Shell::initially_hide_cursor_ = false;
    200 
    201 ////////////////////////////////////////////////////////////////////////////////
    202 // Shell, public:
    203 
    204 Shell::Shell(ShellDelegate* delegate)
    205     : screen_(new ScreenAsh),
    206       target_root_window_(NULL),
    207       scoped_target_root_window_(NULL),
    208       delegate_(delegate),
    209       activation_client_(NULL),
    210 #if defined(OS_CHROMEOS) && defined(USE_X11)
    211       output_configurator_(new chromeos::OutputConfigurator()),
    212 #endif  // defined(OS_CHROMEOS)
    213       native_cursor_manager_(new AshNativeCursorManager),
    214       cursor_manager_(scoped_ptr<views::corewm::NativeCursorManager>(
    215           native_cursor_manager_)),
    216       browser_context_(NULL),
    217       simulate_modal_window_open_for_testing_(false),
    218       is_touch_hud_projection_enabled_(false) {
    219   DCHECK(delegate_.get());
    220   display_manager_.reset(new internal::DisplayManager);
    221 
    222   ANNOTATE_LEAKING_OBJECT_PTR(screen_);  // see crbug.com/156466
    223   gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_ALTERNATE, screen_);
    224   if (!gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE))
    225     gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_);
    226   display_controller_.reset(new DisplayController);
    227 #if defined(OS_CHROMEOS) && defined(USE_X11)
    228   bool is_panel_fitting_disabled =
    229       content::GpuDataManager::GetInstance()->IsFeatureBlacklisted(
    230           gpu::GPU_FEATURE_TYPE_PANEL_FITTING) ||
    231       CommandLine::ForCurrentProcess()->HasSwitch(
    232           ::switches::kDisablePanelFitting);
    233 
    234   output_configurator_->Init(!is_panel_fitting_disabled);
    235 
    236   base::MessagePumpAuraX11::Current()->AddDispatcherForRootWindow(
    237       output_configurator());
    238   // We can't do this with a root window listener because XI_HierarchyChanged
    239   // messages don't have a target window.
    240   base::MessagePumpAuraX11::Current()->AddObserver(output_configurator());
    241 #endif  // defined(OS_CHROMEOS)
    242   AddPreTargetHandler(this);
    243 
    244 #if defined(OS_CHROMEOS)
    245   internal::PowerStatus::Initialize();
    246 #endif
    247 }
    248 
    249 Shell::~Shell() {
    250   views::FocusManagerFactory::Install(NULL);
    251 
    252   // Remove the focus from any window. This will prevent overhead and side
    253   // effects (e.g. crashes) from changing focus during shutdown.
    254   // See bug crbug.com/134502.
    255   aura::client::GetFocusClient(GetPrimaryRootWindow())->FocusWindow(NULL);
    256 
    257   // Please keep in same order as in Init() because it's easy to miss one.
    258   RemovePreTargetHandler(event_rewriter_filter_.get());
    259   RemovePreTargetHandler(user_activity_detector_.get());
    260   RemovePreTargetHandler(overlay_filter_.get());
    261   RemovePreTargetHandler(input_method_filter_.get());
    262   RemovePreTargetHandler(window_modality_controller_.get());
    263   if (mouse_cursor_filter_)
    264     RemovePreTargetHandler(mouse_cursor_filter_.get());
    265   RemovePreTargetHandler(system_gesture_filter_.get());
    266   RemovePreTargetHandler(event_transformation_handler_.get());
    267 #if !defined(OS_MACOSX)
    268   RemovePreTargetHandler(accelerator_filter_.get());
    269 #endif
    270 
    271   // TooltipController is deleted with the Shell so removing its references.
    272   RemovePreTargetHandler(tooltip_controller_.get());
    273 
    274   // AppList needs to be released before shelf layout manager, which is
    275   // destroyed with launcher container in the loop below. However, app list
    276   // container is now on top of launcher container and released after it.
    277   // TODO(xiyuan): Move it back when app list container is no longer needed.
    278   app_list_controller_.reset();
    279 
    280   // Destroy SystemTrayDelegate before destroying the status area(s).
    281   system_tray_delegate_->Shutdown();
    282   system_tray_delegate_.reset();
    283 
    284   locale_notification_controller_.reset();
    285 
    286   // Drag-and-drop must be canceled prior to close all windows.
    287   drag_drop_controller_.reset();
    288 
    289   // Destroy all child windows including widgets.
    290   display_controller_->CloseChildWindows();
    291 
    292   // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems
    293   // needs to remove observers from it.
    294   system_tray_notifier_.reset();
    295 
    296   // These need a valid Shell instance to clean up properly, so explicitly
    297   // delete them before invalidating the instance.
    298   // Alphabetical. TODO(oshima): sort.
    299   magnification_controller_.reset();
    300   partial_magnification_controller_.reset();
    301   resize_shadow_controller_.reset();
    302   shadow_controller_.reset();
    303   tooltip_controller_.reset();
    304   event_client_.reset();
    305   window_cycle_controller_.reset();
    306   capture_controller_.reset();
    307   nested_dispatcher_controller_.reset();
    308   user_action_client_.reset();
    309   visibility_controller_.reset();
    310   launcher_delegate_.reset();
    311   launcher_model_.reset();
    312   video_detector_.reset();
    313 
    314   power_button_controller_.reset();
    315   lock_state_controller_.reset();
    316   mru_window_tracker_.reset();
    317 
    318   resolution_notification_controller_.reset();
    319 
    320   // This also deletes all RootWindows. Note that we invoke Shutdown() on
    321   // DisplayController before resetting |display_controller_|, since destruction
    322   // of its owned RootWindowControllers relies on the value.
    323   display_controller_->Shutdown();
    324   display_controller_.reset();
    325   screen_position_controller_.reset();
    326 
    327   // Delete the activation controller after other controllers and launcher
    328   // because they might have registered ActivationChangeObserver.
    329   activation_controller_.reset();
    330 
    331 #if defined(OS_CHROMEOS) && defined(USE_X11)
    332    if (display_change_observer_)
    333     output_configurator_->RemoveObserver(display_change_observer_.get());
    334   if (output_configurator_animation_)
    335     output_configurator_->RemoveObserver(output_configurator_animation_.get());
    336   if (display_error_observer_)
    337     output_configurator_->RemoveObserver(display_error_observer_.get());
    338   base::MessagePumpAuraX11::Current()->RemoveDispatcherForRootWindow(
    339       output_configurator());
    340   base::MessagePumpAuraX11::Current()->RemoveObserver(output_configurator());
    341   display_change_observer_.reset();
    342 #endif  // defined(OS_CHROMEOS)
    343 
    344 #if defined(OS_CHROMEOS)
    345   internal::PowerStatus::Shutdown();
    346 #endif
    347 
    348   DCHECK(instance_ == this);
    349   instance_ = NULL;
    350 }
    351 
    352 // static
    353 Shell* Shell::CreateInstance(ShellDelegate* delegate) {
    354   CHECK(!instance_);
    355   instance_ = new Shell(delegate);
    356   instance_->Init();
    357   return instance_;
    358 }
    359 
    360 // static
    361 Shell* Shell::GetInstance() {
    362   DCHECK(instance_);
    363   return instance_;
    364 }
    365 
    366 // static
    367 bool Shell::HasInstance() {
    368   return !!instance_;
    369 }
    370 
    371 // static
    372 void Shell::DeleteInstance() {
    373   delete instance_;
    374   instance_ = NULL;
    375 }
    376 
    377 // static
    378 internal::RootWindowController* Shell::GetPrimaryRootWindowController() {
    379   return GetRootWindowController(GetPrimaryRootWindow());
    380 }
    381 
    382 // static
    383 Shell::RootWindowControllerList Shell::GetAllRootWindowControllers() {
    384   return Shell::GetInstance()->display_controller()->
    385       GetAllRootWindowControllers();
    386 }
    387 
    388 // static
    389 aura::RootWindow* Shell::GetPrimaryRootWindow() {
    390   return GetInstance()->display_controller()->GetPrimaryRootWindow();
    391 }
    392 
    393 // static
    394 aura::RootWindow* Shell::GetActiveRootWindow() {
    395   Shell* shell = GetInstance();
    396   if (shell->scoped_target_root_window_)
    397     return shell->scoped_target_root_window_;
    398   return shell->target_root_window_;
    399 }
    400 
    401 // static
    402 gfx::Screen* Shell::GetScreen() {
    403   return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE);
    404 }
    405 
    406 // static
    407 Shell::RootWindowList Shell::GetAllRootWindows() {
    408   return Shell::GetInstance()->display_controller()->
    409       GetAllRootWindows();
    410 }
    411 
    412 // static
    413 aura::Window* Shell::GetContainer(aura::RootWindow* root_window,
    414                                   int container_id) {
    415   return root_window->GetChildById(container_id);
    416 }
    417 
    418 // static
    419 const aura::Window* Shell::GetContainer(const aura::RootWindow* root_window,
    420                                         int container_id) {
    421   return root_window->GetChildById(container_id);
    422 }
    423 
    424 // static
    425 std::vector<aura::Window*> Shell::GetContainersFromAllRootWindows(
    426     int container_id,
    427     aura::RootWindow* priority_root) {
    428   std::vector<aura::Window*> containers;
    429   RootWindowList root_windows = GetAllRootWindows();
    430   for (RootWindowList::const_iterator it = root_windows.begin();
    431        it != root_windows.end(); ++it) {
    432     aura::Window* container = (*it)->GetChildById(container_id);
    433     if (container) {
    434       if (priority_root && priority_root->Contains(container))
    435         containers.insert(containers.begin(), container);
    436       else
    437         containers.push_back(container);
    438     }
    439   }
    440   return containers;
    441 }
    442 
    443 // static
    444 bool Shell::IsForcedMaximizeMode() {
    445   CommandLine* command_line = CommandLine::ForCurrentProcess();
    446   return command_line->HasSwitch(switches::kForcedMaximizeMode);
    447 }
    448 
    449 void Shell::Init() {
    450   CommandLine* command_line = CommandLine::ForCurrentProcess();
    451 
    452   delegate_->PreInit();
    453   bool display_initialized = false;
    454 #if defined(OS_CHROMEOS) && defined(USE_X11)
    455   output_configurator_animation_.reset(
    456       new internal::OutputConfiguratorAnimation());
    457   output_configurator_->AddObserver(output_configurator_animation_.get());
    458   if (base::chromeos::IsRunningOnChromeOS()) {
    459     display_change_observer_.reset(new internal::DisplayChangeObserverX11);
    460     // Register |display_change_observer_| first so that the rest of
    461     // observer gets invoked after the root windows are configured.
    462     output_configurator_->AddObserver(display_change_observer_.get());
    463     display_error_observer_.reset(new internal::DisplayErrorObserver());
    464     output_configurator_->AddObserver(display_error_observer_.get());
    465     output_configurator_->set_state_controller(display_change_observer_.get());
    466     if (!command_line->HasSwitch(ash::switches::kAshDisableSoftwareMirroring))
    467       output_configurator_->set_mirroring_controller(display_manager_.get());
    468     output_configurator_->Start(
    469         delegate_->IsFirstRunAfterBoot() ? kChromeOsBootColor : 0);
    470     display_initialized = true;
    471   }
    472 #endif
    473   if (!display_initialized)
    474     display_manager_->InitFromCommandLine();
    475 
    476   // Install the custom factory first so that views::FocusManagers for Tray,
    477   // Launcher, and WallPaper could be created by the factory.
    478   views::FocusManagerFactory::Install(new AshFocusManagerFactory);
    479 
    480   env_filter_.reset(new views::corewm::CompoundEventFilter);
    481   AddPreTargetHandler(env_filter_.get());
    482 
    483   // Env creates the compositor. Historically it seems to have been implicitly
    484   // initialized first by the ActivationController, but now that FocusController
    485   // no longer does this we need to do it explicitly.
    486   aura::Env::GetInstance();
    487   if (views::corewm::UseFocusController()) {
    488     views::corewm::FocusController* focus_controller =
    489         new views::corewm::FocusController(new wm::AshFocusRules);
    490     focus_client_.reset(focus_controller);
    491     activation_client_ = focus_controller;
    492     activation_client_->AddObserver(this);
    493   } else {
    494     focus_client_.reset(new aura::FocusManager);
    495     activation_controller_.reset(
    496         new internal::ActivationController(
    497             focus_client_.get(),
    498             new internal::AshActivationController));
    499     activation_client_ = activation_controller_.get();
    500     AddPreTargetHandler(activation_controller_.get());
    501   }
    502 
    503   focus_cycler_.reset(new internal::FocusCycler());
    504 
    505   screen_position_controller_.reset(new internal::ScreenPositionController);
    506   root_window_host_factory_.reset(delegate_->CreateRootWindowHostFactory());
    507 
    508   display_controller_->Start();
    509   display_controller_->InitPrimaryDisplay();
    510   aura::RootWindow* root_window = display_controller_->GetPrimaryRootWindow();
    511   target_root_window_ = root_window;
    512 
    513   resolution_notification_controller_.reset(
    514       new internal::ResolutionNotificationController);
    515 
    516   cursor_manager_.SetDisplay(DisplayController::GetPrimaryDisplay());
    517 
    518 #if !defined(OS_MACOSX)
    519   nested_dispatcher_controller_.reset(new NestedDispatcherController);
    520   accelerator_controller_.reset(new AcceleratorController);
    521 #endif
    522 
    523   // The order in which event filters are added is significant.
    524   event_rewriter_filter_.reset(new internal::EventRewriterEventFilter);
    525   AddPreTargetHandler(event_rewriter_filter_.get());
    526 
    527   // UserActivityDetector passes events to observers, so let them get
    528   // rewritten first.
    529   user_activity_detector_.reset(new UserActivityDetector);
    530   AddPreTargetHandler(user_activity_detector_.get());
    531 
    532   overlay_filter_.reset(new internal::OverlayEventFilter);
    533   AddPreTargetHandler(overlay_filter_.get());
    534   AddShellObserver(overlay_filter_.get());
    535 
    536   input_method_filter_.reset(new views::corewm::InputMethodEventFilter(
    537                                  root_window->GetAcceleratedWidget()));
    538   AddPreTargetHandler(input_method_filter_.get());
    539 
    540 #if !defined(OS_MACOSX)
    541   accelerator_filter_.reset(new internal::AcceleratorFilter);
    542   AddPreTargetHandler(accelerator_filter_.get());
    543 #endif
    544 
    545   event_transformation_handler_.reset(new internal::EventTransformationHandler);
    546   AddPreTargetHandler(event_transformation_handler_.get());
    547 
    548   system_gesture_filter_.reset(new internal::SystemGestureEventFilter);
    549   AddPreTargetHandler(system_gesture_filter_.get());
    550 
    551   capture_controller_.reset(new internal::CaptureController);
    552 
    553   // The keyboard system must be initialized before the RootWindowController is
    554   // created.
    555   if (keyboard::IsKeyboardEnabled())
    556     keyboard::InitializeKeyboard();
    557 
    558   if (command_line->HasSwitch(ash::switches::kAshDisableNewLockAnimations))
    559     lock_state_controller_.reset(new SessionStateControllerImpl);
    560   else
    561     lock_state_controller_.reset(new LockStateControllerImpl2);
    562   power_button_controller_.reset(new PowerButtonController(
    563       lock_state_controller_.get()));
    564   AddShellObserver(lock_state_controller_.get());
    565 
    566   drag_drop_controller_.reset(new internal::DragDropController);
    567   mouse_cursor_filter_.reset(new internal::MouseCursorEventFilter());
    568   PrependPreTargetHandler(mouse_cursor_filter_.get());
    569 
    570   // Create Controllers that may need root window.
    571   // TODO(oshima): Move as many controllers before creating
    572   // RootWindowController as possible.
    573   visibility_controller_.reset(new AshVisibilityController);
    574   user_action_client_.reset(delegate_->CreateUserActionClient());
    575   window_modality_controller_.reset(
    576       new views::corewm::WindowModalityController);
    577   AddPreTargetHandler(window_modality_controller_.get());
    578 
    579   magnification_controller_.reset(
    580       MagnificationController::CreateInstance());
    581   mru_window_tracker_.reset(new MruWindowTracker(activation_client_));
    582 
    583   partial_magnification_controller_.reset(
    584       new PartialMagnificationController());
    585 
    586   high_contrast_controller_.reset(new HighContrastController);
    587   video_detector_.reset(new VideoDetector);
    588   window_cycle_controller_.reset(new WindowCycleController());
    589   window_selector_controller_.reset(new WindowSelectorController());
    590 
    591   tooltip_controller_.reset(new views::corewm::TooltipController(
    592                                 gfx::SCREEN_TYPE_ALTERNATE));
    593   AddPreTargetHandler(tooltip_controller_.get());
    594 
    595   event_client_.reset(new internal::EventClientImpl);
    596 
    597   // This controller needs to be set before SetupManagedWindowMode.
    598   desktop_background_controller_.reset(new DesktopBackgroundController());
    599   user_wallpaper_delegate_.reset(delegate_->CreateUserWallpaperDelegate());
    600   if (!user_wallpaper_delegate_)
    601     user_wallpaper_delegate_.reset(new DummyUserWallpaperDelegate());
    602 
    603   // StatusAreaWidget uses Shell's CapsLockDelegate.
    604   caps_lock_delegate_.reset(delegate_->CreateCapsLockDelegate());
    605 
    606   session_state_delegate_.reset(delegate_->CreateSessionStateDelegate());
    607 
    608   if (!command_line->HasSwitch(views::corewm::switches::kNoDropShadows)) {
    609     resize_shadow_controller_.reset(new internal::ResizeShadowController());
    610     shadow_controller_.reset(
    611         new views::corewm::ShadowController(activation_client_));
    612   }
    613 
    614   // Create system_tray_notifier_ before the delegate.
    615   system_tray_notifier_.reset(new ash::SystemTrayNotifier());
    616 
    617   // Initialize system_tray_delegate_ before initializing StatusAreaWidget.
    618   system_tray_delegate_.reset(delegate()->CreateSystemTrayDelegate());
    619   if (!system_tray_delegate_)
    620     system_tray_delegate_.reset(SystemTrayDelegate::CreateDummyDelegate());
    621 
    622   internal::RootWindowController* root_window_controller =
    623       new internal::RootWindowController(root_window);
    624   InitRootWindowController(root_window_controller,
    625                            delegate_->IsFirstRunAfterBoot());
    626 
    627   locale_notification_controller_.reset(
    628       new internal::LocaleNotificationController);
    629 
    630   // Initialize system_tray_delegate_ after StatusAreaWidget is created.
    631   system_tray_delegate_->Initialize();
    632 
    633   display_controller_->InitSecondaryDisplays();
    634 
    635   // Force Layout
    636   root_window_controller->root_window_layout()->OnWindowResized();
    637 
    638   // It needs to be created after OnWindowResized has been called, otherwise the
    639   // widget will not paint when restoring after a browser crash.  Also it needs
    640   // to be created after InitSecondaryDisplays() to initialize the wallpapers in
    641   // the correct size.
    642   user_wallpaper_delegate_->InitializeWallpaper();
    643 
    644   if (initially_hide_cursor_)
    645     cursor_manager_.HideCursor();
    646   cursor_manager_.SetCursor(ui::kCursorPointer);
    647 
    648   if (!cursor_manager_.IsCursorVisible()) {
    649     // Cursor might have been hidden by something other than chrome.
    650     // Let the first mouse event show the cursor.
    651     env_filter_->set_cursor_hidden_by_filter(true);
    652   }
    653 }
    654 
    655 void Shell::ShowContextMenu(const gfx::Point& location_in_screen,
    656                             ui::MenuSourceType source_type) {
    657   // No context menus if there is no session with an active user.
    658   if (!session_state_delegate_->NumberOfLoggedInUsers())
    659     return;
    660   // No context menus when screen is locked.
    661   if (session_state_delegate_->IsScreenLocked())
    662     return;
    663 
    664   aura::RootWindow* root =
    665       wm::GetRootWindowMatching(gfx::Rect(location_in_screen, gfx::Size()));
    666   // TODO(oshima): The root and root window controller shouldn't be
    667   // NULL even for the out-of-bounds |location_in_screen| (It should
    668   // return the primary root). Investigate why/how this is
    669   // happening. crbug.com/165214.
    670   internal::RootWindowController* rwc = GetRootWindowController(root);
    671   CHECK(rwc) << "root=" << root
    672              << ", location:" << location_in_screen.ToString();
    673   if (rwc)
    674     rwc->ShowContextMenu(location_in_screen, source_type);
    675 }
    676 
    677 void Shell::ToggleAppList(aura::Window* window) {
    678   // If the context window is not given, show it on the active root window.
    679   if (!window)
    680     window = GetActiveRootWindow();
    681   if (!app_list_controller_)
    682     app_list_controller_.reset(new internal::AppListController);
    683   app_list_controller_->SetVisible(!app_list_controller_->IsVisible(), window);
    684 }
    685 
    686 bool Shell::GetAppListTargetVisibility() const {
    687   return app_list_controller_.get() &&
    688       app_list_controller_->GetTargetVisibility();
    689 }
    690 
    691 aura::Window* Shell::GetAppListWindow() {
    692   return app_list_controller_.get() ? app_list_controller_->GetWindow() : NULL;
    693 }
    694 
    695 bool Shell::IsSystemModalWindowOpen() const {
    696   if (simulate_modal_window_open_for_testing_)
    697     return true;
    698   const std::vector<aura::Window*> containers = GetContainersFromAllRootWindows(
    699       internal::kShellWindowId_SystemModalContainer, NULL);
    700   for (std::vector<aura::Window*>::const_iterator cit = containers.begin();
    701        cit != containers.end(); ++cit) {
    702     for (aura::Window::Windows::const_iterator wit = (*cit)->children().begin();
    703          wit != (*cit)->children().end(); ++wit) {
    704       if ((*wit)->GetProperty(aura::client::kModalKey) ==
    705           ui::MODAL_TYPE_SYSTEM && (*wit)->TargetVisibility()) {
    706         return true;
    707       }
    708     }
    709   }
    710   return false;
    711 }
    712 
    713 views::NonClientFrameView* Shell::CreateDefaultNonClientFrameView(
    714     views::Widget* widget) {
    715   // Use translucent-style window frames for dialogs.
    716   CustomFrameViewAsh* frame_view = new CustomFrameViewAsh;
    717   frame_view->Init(widget);
    718   return frame_view;
    719 }
    720 
    721 void Shell::RotateFocus(Direction direction) {
    722   focus_cycler_->RotateFocus(
    723       direction == FORWARD ? internal::FocusCycler::FORWARD :
    724                              internal::FocusCycler::BACKWARD);
    725 }
    726 
    727 void Shell::SetDisplayWorkAreaInsets(Window* contains,
    728                                      const gfx::Insets& insets) {
    729   if (!display_controller_->UpdateWorkAreaOfDisplayNearestWindow(
    730           contains, insets)) {
    731     return;
    732   }
    733   FOR_EACH_OBSERVER(ShellObserver, observers_,
    734                     OnDisplayWorkAreaInsetsChanged());
    735 }
    736 
    737 void Shell::OnLoginStateChanged(user::LoginStatus status) {
    738   FOR_EACH_OBSERVER(ShellObserver, observers_, OnLoginStateChanged(status));
    739 }
    740 
    741 void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status) {
    742   RootWindowControllerList controllers = GetAllRootWindowControllers();
    743   for (RootWindowControllerList::iterator iter = controllers.begin();
    744        iter != controllers.end(); ++iter)
    745     (*iter)->UpdateAfterLoginStatusChange(status);
    746 }
    747 
    748 void Shell::OnAppTerminating() {
    749   FOR_EACH_OBSERVER(ShellObserver, observers_, OnAppTerminating());
    750 }
    751 
    752 void Shell::OnLockStateChanged(bool locked) {
    753   FOR_EACH_OBSERVER(ShellObserver, observers_, OnLockStateChanged(locked));
    754 #ifndef NDEBUG
    755   // Make sure that there is no system modal in Lock layer when unlocked.
    756   if (!locked) {
    757     std::vector<aura::Window*> containers = GetContainersFromAllRootWindows(
    758         internal::kShellWindowId_LockSystemModalContainer,
    759         GetPrimaryRootWindow());
    760     for (std::vector<aura::Window*>::const_iterator iter = containers.begin();
    761          iter != containers.end(); ++iter) {
    762       DCHECK_EQ(0u, (*iter)->children().size());
    763     }
    764   }
    765 #endif
    766 }
    767 
    768 void Shell::CreateLauncher() {
    769   RootWindowControllerList controllers = GetAllRootWindowControllers();
    770   for (RootWindowControllerList::iterator iter = controllers.begin();
    771        iter != controllers.end(); ++iter)
    772     (*iter)->shelf()->CreateLauncher();
    773 }
    774 
    775 void Shell::ShowLauncher() {
    776   RootWindowControllerList controllers = GetAllRootWindowControllers();
    777   for (RootWindowControllerList::iterator iter = controllers.begin();
    778        iter != controllers.end(); ++iter)
    779     (*iter)->ShowLauncher();
    780 }
    781 
    782 void Shell::AddShellObserver(ShellObserver* observer) {
    783   observers_.AddObserver(observer);
    784 }
    785 
    786 void Shell::RemoveShellObserver(ShellObserver* observer) {
    787   observers_.RemoveObserver(observer);
    788 }
    789 
    790 void Shell::UpdateShelfVisibility() {
    791   RootWindowControllerList controllers = GetAllRootWindowControllers();
    792   for (RootWindowControllerList::iterator iter = controllers.begin();
    793        iter != controllers.end(); ++iter)
    794     if ((*iter)->shelf())
    795       (*iter)->UpdateShelfVisibility();
    796 }
    797 
    798 void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior,
    799                                      aura::RootWindow* root_window) {
    800   ash::internal::ShelfLayoutManager::ForLauncher(root_window)->
    801       SetAutoHideBehavior(behavior);
    802 }
    803 
    804 ShelfAutoHideBehavior Shell::GetShelfAutoHideBehavior(
    805     aura::RootWindow* root_window) const {
    806   return ash::internal::ShelfLayoutManager::ForLauncher(root_window)->
    807       auto_hide_behavior();
    808 }
    809 
    810 void Shell::SetShelfAlignment(ShelfAlignment alignment,
    811                               aura::RootWindow* root_window) {
    812   if (ash::internal::ShelfLayoutManager::ForLauncher(root_window)->
    813       SetAlignment(alignment)) {
    814     FOR_EACH_OBSERVER(
    815         ShellObserver, observers_, OnShelfAlignmentChanged(root_window));
    816   }
    817 }
    818 
    819 ShelfAlignment Shell::GetShelfAlignment(aura::RootWindow* root_window) {
    820   return GetRootWindowController(root_window)->
    821       GetShelfLayoutManager()->GetAlignment();
    822 }
    823 
    824 void Shell::SetDimming(bool should_dim) {
    825   RootWindowControllerList controllers = GetAllRootWindowControllers();
    826   for (RootWindowControllerList::iterator iter = controllers.begin();
    827        iter != controllers.end(); ++iter)
    828     (*iter)->screen_dimmer()->SetDimming(should_dim);
    829 }
    830 
    831 void Shell::CreateModalBackground(aura::Window* window) {
    832   if (!modality_filter_) {
    833     modality_filter_.reset(new internal::SystemModalContainerEventFilter(this));
    834     AddPreTargetHandler(modality_filter_.get());
    835   }
    836   RootWindowControllerList controllers = GetAllRootWindowControllers();
    837   for (RootWindowControllerList::iterator iter = controllers.begin();
    838        iter != controllers.end(); ++iter)
    839     (*iter)->GetSystemModalLayoutManager(window)->CreateModalBackground();
    840 }
    841 
    842 void Shell::OnModalWindowRemoved(aura::Window* removed) {
    843   RootWindowControllerList controllers = GetAllRootWindowControllers();
    844   bool activated = false;
    845   for (RootWindowControllerList::iterator iter = controllers.begin();
    846        iter != controllers.end() && !activated; ++iter) {
    847     activated = (*iter)->GetSystemModalLayoutManager(removed)->
    848         ActivateNextModalWindow();
    849   }
    850   if (!activated) {
    851     RemovePreTargetHandler(modality_filter_.get());
    852     modality_filter_.reset();
    853     for (RootWindowControllerList::iterator iter = controllers.begin();
    854          iter != controllers.end(); ++iter)
    855       (*iter)->GetSystemModalLayoutManager(removed)->DestroyModalBackground();
    856   }
    857 }
    858 
    859 WebNotificationTray* Shell::GetWebNotificationTray() {
    860   return GetPrimaryRootWindowController()->shelf()->
    861       status_area_widget()->web_notification_tray();
    862 }
    863 
    864 bool Shell::HasPrimaryStatusArea() {
    865   ShelfWidget* shelf = GetPrimaryRootWindowController()->shelf();
    866   return shelf && shelf->status_area_widget();
    867 }
    868 
    869 SystemTray* Shell::GetPrimarySystemTray() {
    870   return GetPrimaryRootWindowController()->GetSystemTray();
    871 }
    872 
    873 LauncherDelegate* Shell::GetLauncherDelegate() {
    874   if (!launcher_delegate_) {
    875     launcher_model_.reset(new LauncherModel);
    876     launcher_delegate_.reset(
    877         delegate_->CreateLauncherDelegate(launcher_model_.get()));
    878   }
    879   return launcher_delegate_.get();
    880 }
    881 
    882 void Shell::SetTouchHudProjectionEnabled(bool enabled) {
    883   if (is_touch_hud_projection_enabled_ == enabled)
    884     return;
    885 
    886   is_touch_hud_projection_enabled_ = enabled;
    887   FOR_EACH_OBSERVER(ShellObserver, observers_,
    888                     OnTouchHudProjectionToggled(enabled));
    889 }
    890 
    891 void Shell::InitRootWindowForSecondaryDisplay(aura::RootWindow* root) {
    892   internal::RootWindowController* controller =
    893       new internal::RootWindowController(root);
    894   // Pass false for the |is_first_run_after_boot| parameter so we'll show a
    895   // black background on this display instead of trying to mimic the boot splash
    896   // screen.
    897   InitRootWindowController(controller, false);
    898 
    899   controller->root_window_layout()->OnWindowResized();
    900   desktop_background_controller_->OnRootWindowAdded(root);
    901   high_contrast_controller_->OnRootWindowAdded(root);
    902   root->ShowRootWindow();
    903   // Activate new root for testing.
    904   // TODO(oshima): remove this.
    905   target_root_window_ = root;
    906 
    907   // Create a launcher if a user is already logged.
    908   if (Shell::GetInstance()->session_state_delegate()->NumberOfLoggedInUsers())
    909     controller->shelf()->CreateLauncher();
    910 }
    911 
    912 void Shell::DoInitialWorkspaceAnimation() {
    913   return GetPrimaryRootWindowController()->workspace_controller()->
    914       DoInitialAnimation();
    915 }
    916 
    917 void Shell::InitRootWindowController(
    918     internal::RootWindowController* controller,
    919     bool first_run_after_boot) {
    920 
    921   aura::RootWindow* root_window = controller->root_window();
    922   DCHECK(activation_client_);
    923   DCHECK(visibility_controller_.get());
    924   DCHECK(drag_drop_controller_.get());
    925   DCHECK(capture_controller_.get());
    926   DCHECK(window_cycle_controller_.get());
    927 
    928   aura::client::SetFocusClient(root_window, focus_client_.get());
    929   input_method_filter_->SetInputMethodPropertyInRootWindow(root_window);
    930   aura::client::SetActivationClient(root_window, activation_client_);
    931   if (views::corewm::UseFocusController()) {
    932     views::corewm::FocusController* controller =
    933         static_cast<views::corewm::FocusController*>(activation_client_);
    934     root_window->AddPreTargetHandler(controller);
    935   }
    936   aura::client::SetVisibilityClient(root_window, visibility_controller_.get());
    937   aura::client::SetDragDropClient(root_window, drag_drop_controller_.get());
    938   aura::client::SetCaptureClient(root_window, capture_controller_.get());
    939   aura::client::SetScreenPositionClient(root_window,
    940                                         screen_position_controller_.get());
    941   aura::client::SetCursorClient(root_window, &cursor_manager_);
    942   aura::client::SetTooltipClient(root_window, tooltip_controller_.get());
    943   aura::client::SetEventClient(root_window, event_client_.get());
    944 
    945   if (nested_dispatcher_controller_) {
    946     aura::client::SetDispatcherClient(root_window,
    947                                       nested_dispatcher_controller_.get());
    948   }
    949   if (user_action_client_)
    950     aura::client::SetUserActionClient(root_window, user_action_client_.get());
    951 
    952   controller->Init(first_run_after_boot);
    953 
    954   mru_window_tracker_->OnRootWindowAdded(root_window);
    955 }
    956 
    957 ////////////////////////////////////////////////////////////////////////////////
    958 // Shell, private:
    959 
    960 bool Shell::CanWindowReceiveEvents(aura::Window* window) {
    961   RootWindowControllerList controllers = GetAllRootWindowControllers();
    962   for (RootWindowControllerList::iterator iter = controllers.begin();
    963        iter != controllers.end(); ++iter) {
    964     internal::SystemModalContainerLayoutManager* layout_manager =
    965         (*iter)->GetSystemModalLayoutManager(window);
    966     if (layout_manager && layout_manager->CanWindowReceiveEvents(window))
    967       return true;
    968   }
    969   return false;
    970 }
    971 
    972 ////////////////////////////////////////////////////////////////////////////////
    973 // Shell, ui::EventTarget overrides:
    974 
    975 bool Shell::CanAcceptEvent(const ui::Event& event) {
    976   return true;
    977 }
    978 
    979 ui::EventTarget* Shell::GetParentTarget() {
    980   return NULL;
    981 }
    982 
    983 void Shell::OnEvent(ui::Event* event) {
    984 }
    985 
    986 ////////////////////////////////////////////////////////////////////////////////
    987 // Shell, aura::client::ActivationChangeObserver implementation:
    988 
    989 void Shell::OnWindowActivated(aura::Window* gained_active,
    990                               aura::Window* lost_active) {
    991   if (gained_active)
    992     target_root_window_ = gained_active->GetRootWindow();
    993 }
    994 
    995 }  // namespace ash
    996