shell.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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/screen_position_controller.h"
21#include "ash/drag_drop/drag_drop_controller.h"
22#include "ash/focus_cycler.h"
23#include "ash/high_contrast/high_contrast_controller.h"
24#include "ash/host/root_window_host_factory.h"
25#include "ash/launcher/launcher_delegate.h"
26#include "ash/launcher/launcher_model.h"
27#include "ash/magnifier/magnification_controller.h"
28#include "ash/magnifier/partial_magnification_controller.h"
29#include "ash/root_window_controller.h"
30#include "ash/screen_ash.h"
31#include "ash/session_state_delegate.h"
32#include "ash/shelf/shelf_layout_manager.h"
33#include "ash/shelf/shelf_widget.h"
34#include "ash/shell_delegate.h"
35#include "ash/shell_factory.h"
36#include "ash/shell_window_ids.h"
37#include "ash/system/status_area_widget.h"
38#include "ash/system/tray/system_tray_delegate.h"
39#include "ash/system/tray/system_tray_notifier.h"
40#include "ash/wm/activation_controller.h"
41#include "ash/wm/always_on_top_controller.h"
42#include "ash/wm/app_list_controller.h"
43#include "ash/wm/ash_activation_controller.h"
44#include "ash/wm/ash_focus_rules.h"
45#include "ash/wm/ash_native_cursor_manager.h"
46#include "ash/wm/base_layout_manager.h"
47#include "ash/wm/capture_controller.h"
48#include "ash/wm/coordinate_conversion.h"
49#include "ash/wm/custom_frame_view_ash.h"
50#include "ash/wm/event_client_impl.h"
51#include "ash/wm/event_rewriter_event_filter.h"
52#include "ash/wm/overlay_event_filter.h"
53#include "ash/wm/power_button_controller.h"
54#include "ash/wm/property_util.h"
55#include "ash/wm/resize_shadow_controller.h"
56#include "ash/wm/root_window_layout_manager.h"
57#include "ash/wm/screen_dimmer.h"
58#include "ash/wm/session_state_controller.h"
59#include "ash/wm/session_state_controller_impl.h"
60#include "ash/wm/session_state_controller_impl2.h"
61#include "ash/wm/system_gesture_event_filter.h"
62#include "ash/wm/system_modal_container_event_filter.h"
63#include "ash/wm/system_modal_container_layout_manager.h"
64#include "ash/wm/user_activity_detector.h"
65#include "ash/wm/video_detector.h"
66#include "ash/wm/window_animations.h"
67#include "ash/wm/window_cycle_controller.h"
68#include "ash/wm/window_properties.h"
69#include "ash/wm/window_util.h"
70#include "ash/wm/workspace_controller.h"
71#include "base/bind.h"
72#include "base/command_line.h"
73#include "base/debug/leak_annotations.h"
74#include "ui/aura/client/aura_constants.h"
75#include "ui/aura/client/user_action_client.h"
76#include "ui/aura/env.h"
77#include "ui/aura/focus_manager.h"
78#include "ui/aura/layout_manager.h"
79#include "ui/aura/root_window.h"
80#include "ui/aura/window.h"
81#include "ui/base/ui_base_switches.h"
82#include "ui/compositor/layer.h"
83#include "ui/compositor/layer_animator.h"
84#include "ui/gfx/display.h"
85#include "ui/gfx/image/image_skia.h"
86#include "ui/gfx/screen.h"
87#include "ui/gfx/size.h"
88#include "ui/keyboard/keyboard.h"
89#include "ui/keyboard/keyboard_util.h"
90#include "ui/message_center/message_center.h"
91#include "ui/views/corewm/compound_event_filter.h"
92#include "ui/views/corewm/corewm_switches.h"
93#include "ui/views/corewm/focus_controller.h"
94#include "ui/views/corewm/input_method_event_filter.h"
95#include "ui/views/corewm/shadow_controller.h"
96#include "ui/views/corewm/tooltip_controller.h"
97#include "ui/views/corewm/visibility_controller.h"
98#include "ui/views/corewm/window_modality_controller.h"
99#include "ui/views/focus/focus_manager_factory.h"
100#include "ui/views/widget/native_widget_aura.h"
101#include "ui/views/widget/widget.h"
102#include "ui/views/window/dialog_delegate.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 "content/public/common/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  ANNOTATE_LEAKING_OBJECT_PTR(screen_);  // see crbug.com/156466
215  gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_ALTERNATE, screen_);
216  if (!gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE))
217    gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_);
218  display_controller_.reset(new DisplayController);
219#if defined(OS_CHROMEOS) && defined(USE_X11)
220  bool is_panel_fitting_disabled =
221      content::GpuDataManager::GetInstance()->IsFeatureBlacklisted(
222          content::GPU_FEATURE_TYPE_PANEL_FITTING) ||
223      CommandLine::ForCurrentProcess()->HasSwitch(
224          ::switches::kDisablePanelFitting);
225
226  output_configurator_->Init(
227      !is_panel_fitting_disabled,
228      delegate_->IsFirstRunAfterBoot() ? kChromeOsBootColor : 0);
229
230  base::MessagePumpAuraX11::Current()->AddDispatcherForRootWindow(
231      output_configurator());
232#endif  // defined(OS_CHROMEOS)
233  AddPreTargetHandler(this);
234}
235
236Shell::~Shell() {
237  views::FocusManagerFactory::Install(NULL);
238
239  // Remove the focus from any window. This will prevent overhead and side
240  // effects (e.g. crashes) from changing focus during shutdown.
241  // See bug crbug.com/134502.
242  if (active_root_window_)
243    aura::client::GetFocusClient(active_root_window_)->FocusWindow(NULL);
244
245  // Please keep in same order as in Init() because it's easy to miss one.
246  RemovePreTargetHandler(user_activity_detector_.get());
247  RemovePreTargetHandler(event_rewriter_filter_.get());
248  RemovePreTargetHandler(overlay_filter_.get());
249  RemovePreTargetHandler(input_method_filter_.get());
250  RemovePreTargetHandler(window_modality_controller_.get());
251  if (mouse_cursor_filter_)
252    RemovePreTargetHandler(mouse_cursor_filter_.get());
253  RemovePreTargetHandler(system_gesture_filter_.get());
254  RemovePreTargetHandler(event_transformation_handler_.get());
255#if !defined(OS_MACOSX)
256  RemovePreTargetHandler(accelerator_filter_.get());
257#endif
258
259  // TooltipController is deleted with the Shell so removing its references.
260  RemovePreTargetHandler(tooltip_controller_.get());
261
262  // AppList needs to be released before shelf layout manager, which is
263  // destroyed with launcher container in the loop below. However, app list
264  // container is now on top of launcher container and released after it.
265  // TODO(xiyuan): Move it back when app list container is no longer needed.
266  app_list_controller_.reset();
267
268  // Destroy SystemTrayDelegate before destroying the status area(s).
269  system_tray_delegate_->Shutdown();
270  system_tray_delegate_.reset();
271
272  // Destroy all child windows including widgets.
273  display_controller_->CloseChildWindows();
274
275  // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems
276  // needs to remove observers from it.
277  system_tray_notifier_.reset();
278
279  // These need a valid Shell instance to clean up properly, so explicitly
280  // delete them before invalidating the instance.
281  // Alphabetical. TODO(oshima): sort.
282  drag_drop_controller_.reset();
283  magnification_controller_.reset();
284  partial_magnification_controller_.reset();
285  resize_shadow_controller_.reset();
286  shadow_controller_.reset();
287  tooltip_controller_.reset();
288  event_client_.reset();
289  window_cycle_controller_.reset();
290  capture_controller_.reset();
291  nested_dispatcher_controller_.reset();
292  user_action_client_.reset();
293  visibility_controller_.reset();
294  launcher_delegate_.reset();
295  launcher_model_.reset();
296  video_detector_.reset();
297
298  power_button_controller_.reset();
299  session_state_controller_.reset();
300
301  // This also deletes all RootWindows. Note that we invoke Shutdown() on
302  // DisplayController before resetting |display_controller_|, since destruction
303  // of its owned RootWindowControllers relies on the value.
304  display_controller_->Shutdown();
305  display_controller_.reset();
306  screen_position_controller_.reset();
307
308  // Delete the activation controller after other controllers and launcher
309  // because they might have registered ActivationChangeObserver.
310  activation_controller_.reset();
311
312#if defined(OS_CHROMEOS) && defined(USE_X11)
313   if (display_change_observer_)
314    output_configurator_->RemoveObserver(display_change_observer_.get());
315  if (output_configurator_animation_)
316    output_configurator_->RemoveObserver(output_configurator_animation_.get());
317  if (display_error_observer_)
318    output_configurator_->RemoveObserver(display_error_observer_.get());
319  base::MessagePumpAuraX11::Current()->RemoveDispatcherForRootWindow(
320      output_configurator());
321  display_change_observer_.reset();
322#endif  // defined(OS_CHROMEOS)
323
324  DCHECK(instance_ == this);
325  instance_ = NULL;
326}
327
328// static
329Shell* Shell::CreateInstance(ShellDelegate* delegate) {
330  CHECK(!instance_);
331  instance_ = new Shell(delegate);
332  instance_->Init();
333  return instance_;
334}
335
336// static
337Shell* Shell::GetInstance() {
338  DCHECK(instance_);
339  return instance_;
340}
341
342// static
343bool Shell::HasInstance() {
344  return !!instance_;
345}
346
347// static
348void Shell::DeleteInstance() {
349  delete instance_;
350  instance_ = NULL;
351}
352
353// static
354internal::RootWindowController* Shell::GetPrimaryRootWindowController() {
355  return GetRootWindowController(GetPrimaryRootWindow());
356}
357
358// static
359Shell::RootWindowControllerList Shell::GetAllRootWindowControllers() {
360  return Shell::GetInstance()->display_controller()->
361      GetAllRootWindowControllers();
362}
363
364// static
365aura::RootWindow* Shell::GetPrimaryRootWindow() {
366  return GetInstance()->display_controller()->GetPrimaryRootWindow();
367}
368
369// static
370aura::RootWindow* Shell::GetActiveRootWindow() {
371  return GetInstance()->active_root_window_;
372}
373
374// static
375gfx::Screen* Shell::GetScreen() {
376  return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE);
377}
378
379// static
380Shell::RootWindowList Shell::GetAllRootWindows() {
381  return Shell::GetInstance()->display_controller()->
382      GetAllRootWindows();
383}
384
385// static
386aura::Window* Shell::GetContainer(aura::RootWindow* root_window,
387                                  int container_id) {
388  return root_window->GetChildById(container_id);
389}
390
391// static
392const aura::Window* Shell::GetContainer(const aura::RootWindow* root_window,
393                                        int container_id) {
394  return root_window->GetChildById(container_id);
395}
396
397// static
398std::vector<aura::Window*> Shell::GetContainersFromAllRootWindows(
399    int container_id,
400    aura::RootWindow* priority_root) {
401  std::vector<aura::Window*> containers;
402  RootWindowList root_windows = GetAllRootWindows();
403  for (RootWindowList::const_iterator it = root_windows.begin();
404       it != root_windows.end(); ++it) {
405    aura::Window* container = (*it)->GetChildById(container_id);
406    if (container) {
407      if (priority_root && priority_root->Contains(container))
408        containers.insert(containers.begin(), container);
409      else
410        containers.push_back(container);
411    }
412  }
413  return containers;
414}
415
416// static
417bool Shell::IsLauncherPerDisplayEnabled() {
418  CommandLine* command_line = CommandLine::ForCurrentProcess();
419  return !command_line->HasSwitch(switches::kAshDisableLauncherPerDisplay);
420}
421
422// static
423bool Shell::IsForcedMaximizeMode() {
424  CommandLine* command_line = CommandLine::ForCurrentProcess();
425  return command_line->HasSwitch(switches::kForcedMaximizeMode);
426}
427
428void Shell::Init() {
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    output_configurator_->Start();
443    display_change_observer_->OnDisplayModeChanged();
444  }
445#endif
446
447  // Install the custom factory first so that views::FocusManagers for Tray,
448  // Launcher, and WallPaper could be created by the factory.
449  views::FocusManagerFactory::Install(new AshFocusManagerFactory);
450
451  env_filter_.reset(new views::corewm::CompoundEventFilter);
452  AddPreTargetHandler(env_filter_.get());
453
454  // Env creates the compositor. Historically it seems to have been implicitly
455  // initialized first by the ActivationController, but now that FocusController
456  // no longer does this we need to do it explicitly.
457  aura::Env::GetInstance();
458  if (views::corewm::UseFocusController()) {
459    views::corewm::FocusController* focus_controller =
460        new views::corewm::FocusController(new wm::AshFocusRules);
461    focus_client_.reset(focus_controller);
462    activation_client_ = focus_controller;
463    activation_client_->AddObserver(this);
464  } else {
465    focus_client_.reset(new aura::FocusManager);
466    activation_controller_.reset(
467        new internal::ActivationController(
468            focus_client_.get(),
469            new internal::AshActivationController));
470    activation_client_ = activation_controller_.get();
471    AddPreTargetHandler(activation_controller_.get());
472  }
473
474  focus_cycler_.reset(new internal::FocusCycler());
475
476  screen_position_controller_.reset(new internal::ScreenPositionController);
477  root_window_host_factory_.reset(delegate_->CreateRootWindowHostFactory());
478  display_controller_->Start();
479  display_controller_->InitPrimaryDisplay();
480  aura::RootWindow* root_window = display_controller_->GetPrimaryRootWindow();
481  active_root_window_ = root_window;
482
483  cursor_manager_.SetDisplay(DisplayController::GetPrimaryDisplay());
484
485#if !defined(OS_MACOSX)
486  nested_dispatcher_controller_.reset(new NestedDispatcherController);
487  accelerator_controller_.reset(new AcceleratorController);
488#endif
489
490  // The order in which event filters are added is significant.
491  user_activity_detector_.reset(new UserActivityDetector);
492  AddPreTargetHandler(user_activity_detector_.get());
493
494  event_rewriter_filter_.reset(new internal::EventRewriterEventFilter);
495  AddPreTargetHandler(event_rewriter_filter_.get());
496
497  overlay_filter_.reset(new internal::OverlayEventFilter);
498  AddPreTargetHandler(overlay_filter_.get());
499  AddShellObserver(overlay_filter_.get());
500
501  input_method_filter_.reset(new views::corewm::InputMethodEventFilter(
502                                 root_window->GetAcceleratedWidget()));
503  AddPreTargetHandler(input_method_filter_.get());
504
505#if !defined(OS_MACOSX)
506  accelerator_filter_.reset(new internal::AcceleratorFilter);
507  AddPreTargetHandler(accelerator_filter_.get());
508#endif
509
510  event_transformation_handler_.reset(new internal::EventTransformationHandler);
511  AddPreTargetHandler(event_transformation_handler_.get());
512
513  system_gesture_filter_.reset(new internal::SystemGestureEventFilter);
514  AddPreTargetHandler(system_gesture_filter_.get());
515
516  capture_controller_.reset(new internal::CaptureController);
517
518  // The keyboard system must be initialized before the RootWindowController is
519  // created.
520  if (keyboard::IsKeyboardEnabled())
521    keyboard::InitializeKeyboard();
522
523  internal::RootWindowController* root_window_controller =
524      new internal::RootWindowController(root_window);
525  root_window_controller->CreateContainers();
526  root_window_controller->CreateSystemBackground(
527      delegate_->IsFirstRunAfterBoot());
528
529  CommandLine* command_line = CommandLine::ForCurrentProcess();
530
531  if (command_line->HasSwitch(ash::switches::kAshDisableNewLockAnimations))
532    session_state_controller_.reset(new SessionStateControllerImpl);
533  else
534    session_state_controller_.reset(new SessionStateControllerImpl2);
535  power_button_controller_.reset(new PowerButtonController(
536      session_state_controller_.get()));
537  AddShellObserver(session_state_controller_.get());
538
539  mouse_cursor_filter_.reset(new internal::MouseCursorEventFilter());
540  AddPreTargetHandler(mouse_cursor_filter_.get());
541
542  // Create Controllers that may need root window.
543  // TODO(oshima): Move as many controllers before creating
544  // RootWindowController as possible.
545  visibility_controller_.reset(new AshVisibilityController);
546  drag_drop_controller_.reset(new internal::DragDropController);
547  user_action_client_.reset(delegate_->CreateUserActionClient());
548  window_modality_controller_.reset(
549      new views::corewm::WindowModalityController);
550  AddPreTargetHandler(window_modality_controller_.get());
551
552  magnification_controller_.reset(
553      MagnificationController::CreateInstance());
554
555  partial_magnification_controller_.reset(
556      new PartialMagnificationController());
557
558  high_contrast_controller_.reset(new HighContrastController);
559  video_detector_.reset(new VideoDetector);
560  window_cycle_controller_.reset(new WindowCycleController(activation_client_));
561
562  tooltip_controller_.reset(new views::corewm::TooltipController(
563                                gfx::SCREEN_TYPE_ALTERNATE));
564  AddPreTargetHandler(tooltip_controller_.get());
565
566  event_client_.reset(new internal::EventClientImpl);
567
568  InitRootWindowController(root_window_controller);
569
570  // This controller needs to be set before SetupManagedWindowMode.
571  desktop_background_controller_.reset(new DesktopBackgroundController());
572  user_wallpaper_delegate_.reset(delegate_->CreateUserWallpaperDelegate());
573  if (!user_wallpaper_delegate_)
574    user_wallpaper_delegate_.reset(new DummyUserWallpaperDelegate());
575
576  // StatusAreaWidget uses Shell's CapsLockDelegate.
577  caps_lock_delegate_.reset(delegate_->CreateCapsLockDelegate());
578
579  session_state_delegate_.reset(delegate_->CreateSessionStateDelegate());
580
581  if (!command_line->HasSwitch(views::corewm::switches::kNoDropShadows)) {
582    resize_shadow_controller_.reset(new internal::ResizeShadowController());
583    shadow_controller_.reset(
584        new views::corewm::ShadowController(activation_client_));
585  }
586
587  // Create system_tray_notifier_ before the delegate.
588  system_tray_notifier_.reset(new ash::SystemTrayNotifier());
589
590  // Initialize system_tray_delegate_ before initializing StatusAreaWidget.
591  system_tray_delegate_.reset(delegate()->CreateSystemTrayDelegate());
592  if (!system_tray_delegate_)
593    system_tray_delegate_.reset(SystemTrayDelegate::CreateDummyDelegate());
594
595  // Creates StatusAreaWidget.
596  root_window_controller->InitForPrimaryDisplay();
597
598  // Initialize system_tray_delegate_ after StatusAreaWidget is created.
599  system_tray_delegate_->Initialize();
600
601  display_controller_->InitSecondaryDisplays();
602
603  // Force Layout
604  root_window_controller->root_window_layout()->OnWindowResized();
605
606  // It needs to be created after OnWindowResized has been called, otherwise the
607  // widget will not paint when restoring after a browser crash.  Also it needs
608  // to be created after InitSecondaryDisplays() to initialize the wallpapers in
609  // the correct size.
610  user_wallpaper_delegate_->InitializeWallpaper();
611
612  if (initially_hide_cursor_)
613    cursor_manager_.HideCursor();
614  cursor_manager_.SetCursor(ui::kCursorPointer);
615
616  if (!cursor_manager_.IsCursorVisible()) {
617    // Cursor might have been hidden by something other than chrome.
618    // Let the first mouse event show the cursor.
619    env_filter_->set_cursor_hidden_by_filter(true);
620  }
621}
622
623void Shell::ShowContextMenu(const gfx::Point& location_in_screen) {
624  // No context menus if there is no session with an active user.
625  if (!session_state_delegate_->HasActiveUser())
626    return;
627  // No context menus when screen is locked.
628  if (session_state_delegate_->IsScreenLocked())
629    return;
630
631  aura::RootWindow* root =
632      wm::GetRootWindowMatching(gfx::Rect(location_in_screen, gfx::Size()));
633  // TODO(oshima): The root and root window controller shouldn't be
634  // NULL even for the out-of-bounds |location_in_screen| (It should
635  // return the primary root). Investigate why/how this is
636  // happening. crbug.com/165214.
637  internal::RootWindowController* rwc = GetRootWindowController(root);
638  CHECK(rwc) << "root=" << root
639             << ", location:" << location_in_screen.ToString();
640  if (rwc)
641    rwc->ShowContextMenu(location_in_screen);
642}
643
644void Shell::ToggleAppList(aura::Window* window) {
645  // If the context window is not given, show it on the active root window.
646  if (!window)
647    window = GetActiveRootWindow();
648  if (!app_list_controller_)
649    app_list_controller_.reset(new internal::AppListController);
650  app_list_controller_->SetVisible(!app_list_controller_->IsVisible(), window);
651}
652
653bool Shell::GetAppListTargetVisibility() const {
654  return app_list_controller_.get() &&
655      app_list_controller_->GetTargetVisibility();
656}
657
658aura::Window* Shell::GetAppListWindow() {
659  return app_list_controller_.get() ? app_list_controller_->GetWindow() : NULL;
660}
661
662bool Shell::IsSystemModalWindowOpen() const {
663  if (simulate_modal_window_open_for_testing_)
664    return true;
665  const std::vector<aura::Window*> containers = GetContainersFromAllRootWindows(
666      internal::kShellWindowId_SystemModalContainer, NULL);
667  for (std::vector<aura::Window*>::const_iterator cit = containers.begin();
668       cit != containers.end(); ++cit) {
669    for (aura::Window::Windows::const_iterator wit = (*cit)->children().begin();
670         wit != (*cit)->children().end(); ++wit) {
671      if ((*wit)->GetProperty(aura::client::kModalKey) ==
672          ui::MODAL_TYPE_SYSTEM && (*wit)->TargetVisibility()) {
673        return true;
674      }
675    }
676  }
677  return false;
678}
679
680views::NonClientFrameView* Shell::CreateDefaultNonClientFrameView(
681    views::Widget* widget) {
682  if (views::DialogDelegate::UseNewStyle())
683    return views::DialogDelegate::CreateNewStyleFrameView(widget);
684  // Use translucent-style window frames for dialogs.
685  CustomFrameViewAsh* frame_view = new CustomFrameViewAsh;
686  frame_view->Init(widget);
687  return frame_view;
688}
689
690void Shell::RotateFocus(Direction direction) {
691  focus_cycler_->RotateFocus(
692      direction == FORWARD ? internal::FocusCycler::FORWARD :
693                             internal::FocusCycler::BACKWARD);
694}
695
696void Shell::SetDisplayWorkAreaInsets(Window* contains,
697                                     const gfx::Insets& insets) {
698  if (!display_manager_->UpdateWorkAreaOfDisplayNearestWindow(contains, insets))
699    return;
700  FOR_EACH_OBSERVER(ShellObserver, observers_,
701                    OnDisplayWorkAreaInsetsChanged());
702}
703
704void Shell::OnLoginStateChanged(user::LoginStatus status) {
705  FOR_EACH_OBSERVER(ShellObserver, observers_, OnLoginStateChanged(status));
706  RootWindowControllerList controllers = GetAllRootWindowControllers();
707  for (RootWindowControllerList::iterator iter = controllers.begin();
708       iter != controllers.end(); ++iter)
709    (*iter)->OnLoginStateChanged(status);
710}
711
712void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status) {
713  RootWindowControllerList controllers = GetAllRootWindowControllers();
714  for (RootWindowControllerList::iterator iter = controllers.begin();
715       iter != controllers.end(); ++iter)
716    (*iter)->UpdateAfterLoginStatusChange(status);
717}
718
719void Shell::OnAppTerminating() {
720  FOR_EACH_OBSERVER(ShellObserver, observers_, OnAppTerminating());
721}
722
723void Shell::OnLockStateChanged(bool locked) {
724  FOR_EACH_OBSERVER(ShellObserver, observers_, OnLockStateChanged(locked));
725}
726
727void Shell::CreateLauncher() {
728  if (IsLauncherPerDisplayEnabled()) {
729    RootWindowControllerList controllers = GetAllRootWindowControllers();
730    for (RootWindowControllerList::iterator iter = controllers.begin();
731         iter != controllers.end(); ++iter)
732      (*iter)->shelf()->CreateLauncher();
733  } else {
734    GetPrimaryRootWindowController()->shelf()->CreateLauncher();
735  }
736}
737
738void Shell::ShowLauncher() {
739  if (IsLauncherPerDisplayEnabled()) {
740    RootWindowControllerList controllers = GetAllRootWindowControllers();
741    for (RootWindowControllerList::iterator iter = controllers.begin();
742         iter != controllers.end(); ++iter)
743      (*iter)->ShowLauncher();
744  } else {
745    GetPrimaryRootWindowController()->ShowLauncher();
746  }
747}
748
749void Shell::AddShellObserver(ShellObserver* observer) {
750  observers_.AddObserver(observer);
751}
752
753void Shell::RemoveShellObserver(ShellObserver* observer) {
754  observers_.RemoveObserver(observer);
755}
756
757void Shell::UpdateShelfVisibility() {
758  RootWindowControllerList controllers = GetAllRootWindowControllers();
759  for (RootWindowControllerList::iterator iter = controllers.begin();
760       iter != controllers.end(); ++iter)
761    if ((*iter)->shelf())
762      (*iter)->UpdateShelfVisibility();
763}
764
765void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior,
766                                     aura::RootWindow* root_window) {
767  ash::internal::ShelfLayoutManager::ForLauncher(root_window)->
768      SetAutoHideBehavior(behavior);
769}
770
771ShelfAutoHideBehavior Shell::GetShelfAutoHideBehavior(
772    aura::RootWindow* root_window) const {
773  return ash::internal::ShelfLayoutManager::ForLauncher(root_window)->
774      auto_hide_behavior();
775}
776
777void Shell::SetShelfAlignment(ShelfAlignment alignment,
778                              aura::RootWindow* root_window) {
779  if (ash::internal::ShelfLayoutManager::ForLauncher(root_window)->
780      SetAlignment(alignment)) {
781    FOR_EACH_OBSERVER(
782        ShellObserver, observers_, OnShelfAlignmentChanged(root_window));
783  }
784}
785
786ShelfAlignment Shell::GetShelfAlignment(aura::RootWindow* root_window) {
787  return GetRootWindowController(root_window)->
788      GetShelfLayoutManager()->GetAlignment();
789}
790
791void Shell::SetDimming(bool should_dim) {
792  RootWindowControllerList controllers = GetAllRootWindowControllers();
793  for (RootWindowControllerList::iterator iter = controllers.begin();
794       iter != controllers.end(); ++iter)
795    (*iter)->screen_dimmer()->SetDimming(should_dim);
796}
797
798void Shell::CreateModalBackground(aura::Window* window) {
799  if (!modality_filter_) {
800    modality_filter_.reset(new internal::SystemModalContainerEventFilter(this));
801    AddPreTargetHandler(modality_filter_.get());
802  }
803  RootWindowControllerList controllers = GetAllRootWindowControllers();
804  for (RootWindowControllerList::iterator iter = controllers.begin();
805       iter != controllers.end(); ++iter)
806    (*iter)->GetSystemModalLayoutManager(window)->CreateModalBackground();
807}
808
809void Shell::OnModalWindowRemoved(aura::Window* removed) {
810  RootWindowControllerList controllers = GetAllRootWindowControllers();
811  bool activated = false;
812  for (RootWindowControllerList::iterator iter = controllers.begin();
813       iter != controllers.end() && !activated; ++iter) {
814    activated = (*iter)->GetSystemModalLayoutManager(removed)->
815        ActivateNextModalWindow();
816  }
817  if (!activated) {
818    RemovePreTargetHandler(modality_filter_.get());
819    modality_filter_.reset();
820    for (RootWindowControllerList::iterator iter = controllers.begin();
821         iter != controllers.end(); ++iter)
822      (*iter)->GetSystemModalLayoutManager(removed)->DestroyModalBackground();
823  }
824}
825
826WebNotificationTray* Shell::GetWebNotificationTray() {
827  return GetPrimaryRootWindowController()->shelf()->
828      status_area_widget()->web_notification_tray();
829}
830
831bool Shell::HasPrimaryStatusArea() {
832  ShelfWidget* shelf = GetPrimaryRootWindowController()->shelf();
833  return shelf && shelf->status_area_widget();
834}
835
836SystemTray* Shell::GetPrimarySystemTray() {
837  return GetPrimaryRootWindowController()->GetSystemTray();
838}
839
840LauncherDelegate* Shell::GetLauncherDelegate() {
841  if (!launcher_delegate_) {
842    launcher_model_.reset(new LauncherModel);
843    launcher_delegate_.reset(
844        delegate_->CreateLauncherDelegate(launcher_model_.get()));
845  }
846  return launcher_delegate_.get();
847}
848
849void Shell::InitRootWindowForSecondaryDisplay(aura::RootWindow* root) {
850  aura::client::SetFocusClient(root, focus_client_.get());
851  internal::RootWindowController* controller =
852      new internal::RootWindowController(root);
853  controller->CreateContainers();
854  // Pass false for the |is_first_run_after_boot| parameter so we'll show a
855  // black background on this display instead of trying to mimic the boot splash
856  // screen.
857  controller->CreateSystemBackground(false);
858  InitRootWindowController(controller);
859  if (IsLauncherPerDisplayEnabled())
860    controller->InitForPrimaryDisplay();
861  controller->root_window_layout()->OnWindowResized();
862  desktop_background_controller_->OnRootWindowAdded(root);
863  high_contrast_controller_->OnRootWindowAdded(root);
864  root->ShowRootWindow();
865  // Activate new root for testing.
866  active_root_window_ = root;
867}
868
869void Shell::DoInitialWorkspaceAnimation() {
870  return GetPrimaryRootWindowController()->workspace_controller()->
871      DoInitialAnimation();
872}
873
874void Shell::InitRootWindowController(
875    internal::RootWindowController* controller) {
876  aura::RootWindow* root_window = controller->root_window();
877  DCHECK(activation_client_);
878  DCHECK(visibility_controller_.get());
879  DCHECK(drag_drop_controller_.get());
880  DCHECK(capture_controller_.get());
881  DCHECK(window_cycle_controller_.get());
882
883  aura::client::SetFocusClient(root_window, focus_client_.get());
884  input_method_filter_->SetInputMethodPropertyInRootWindow(root_window);
885  aura::client::SetActivationClient(root_window, activation_client_);
886  if (views::corewm::UseFocusController()) {
887    views::corewm::FocusController* controller =
888        static_cast<views::corewm::FocusController*>(activation_client_);
889    root_window->AddPreTargetHandler(controller);
890  }
891  aura::client::SetVisibilityClient(root_window, visibility_controller_.get());
892  aura::client::SetDragDropClient(root_window, drag_drop_controller_.get());
893  aura::client::SetCaptureClient(root_window, capture_controller_.get());
894  aura::client::SetScreenPositionClient(root_window,
895                                        screen_position_controller_.get());
896  aura::client::SetCursorClient(root_window, &cursor_manager_);
897  aura::client::SetTooltipClient(root_window, tooltip_controller_.get());
898  aura::client::SetEventClient(root_window, event_client_.get());
899
900  if (nested_dispatcher_controller_) {
901    aura::client::SetDispatcherClient(root_window,
902                                      nested_dispatcher_controller_.get());
903  }
904  if (user_action_client_)
905    aura::client::SetUserActionClient(root_window, user_action_client_.get());
906
907  root_window->SetCursor(ui::kCursorPointer);
908  controller->InitLayoutManagers();
909
910  // TODO(oshima): Move the instance to RootWindowController when
911  // the extended desktop is enabled by default.
912  internal::AlwaysOnTopController* always_on_top_controller =
913      new internal::AlwaysOnTopController;
914  always_on_top_controller->SetAlwaysOnTopContainer(
915      root_window->GetChildById(internal::kShellWindowId_AlwaysOnTopContainer));
916  root_window->SetProperty(internal::kAlwaysOnTopControllerKey,
917                           always_on_top_controller);
918  if (GetPrimaryRootWindowController()->GetSystemModalLayoutManager(NULL)->
919          has_modal_background()) {
920    controller->GetSystemModalLayoutManager(NULL)->CreateModalBackground();
921  }
922
923  window_cycle_controller_->OnRootWindowAdded(root_window);
924}
925
926////////////////////////////////////////////////////////////////////////////////
927// Shell, private:
928
929bool Shell::CanWindowReceiveEvents(aura::Window* window) {
930  RootWindowControllerList controllers = GetAllRootWindowControllers();
931  for (RootWindowControllerList::iterator iter = controllers.begin();
932       iter != controllers.end(); ++iter) {
933    if ((*iter)->GetSystemModalLayoutManager(window)->
934            CanWindowReceiveEvents(window)) {
935      return true;
936    }
937  }
938  return false;
939}
940
941////////////////////////////////////////////////////////////////////////////////
942// Shell, ui::EventTarget overrides:
943
944bool Shell::CanAcceptEvent(const ui::Event& event) {
945  return true;
946}
947
948ui::EventTarget* Shell::GetParentTarget() {
949  return NULL;
950}
951
952void Shell::OnEvent(ui::Event* event) {
953}
954
955////////////////////////////////////////////////////////////////////////////////
956// Shell, aura::client::ActivationChangeObserver implementation:
957
958void Shell::OnWindowActivated(aura::Window* gained_active,
959                              aura::Window* lost_active) {
960  if (gained_active)
961    active_root_window_ = gained_active->GetRootWindow();
962}
963
964}  // namespace ash
965