shell.cc revision b2df76ea8fec9e32f6f3718986dba0d95315b29c
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/shell.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/accelerators/focus_manager_factory.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/ash_switches.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/caps_lock_delegate.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/desktop_background/desktop_background_controller.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/desktop_background/desktop_background_view.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/desktop_background/user_wallpaper_delegate.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/display/display_controller.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/display/display_manager.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/display/event_transformation_handler.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/display/mouse_cursor_event_filter.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ash/display/screen_position_controller.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ash/drag_drop/drag_drop_controller.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/focus_cycler.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/high_contrast/high_contrast_controller.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/host/root_window_host_factory.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/launcher/launcher_delegate.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/launcher/launcher_model.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/magnifier/magnification_controller.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/magnifier/partial_magnification_controller.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/root_window_controller.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/screen_ash.h"
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/session_state_delegate.h"
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/shelf/shelf_layout_manager.h"
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/shelf/shelf_widget.h"
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/shell_delegate.h"
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/shell_factory.h"
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/shell_window_ids.h"
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/system/status_area_widget.h"
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/system/tray/system_tray_delegate.h"
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/system/tray/system_tray_notifier.h"
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/activation_controller.h"
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/always_on_top_controller.h"
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/app_list_controller.h"
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/ash_activation_controller.h"
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/ash_focus_rules.h"
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/ash_native_cursor_manager.h"
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/base_layout_manager.h"
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/capture_controller.h"
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/coordinate_conversion.h"
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/custom_frame_view_ash.h"
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/event_client_impl.h"
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/event_rewriter_event_filter.h"
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/overlay_event_filter.h"
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/power_button_controller.h"
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/property_util.h"
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/resize_shadow_controller.h"
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/root_window_layout_manager.h"
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/screen_dimmer.h"
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/session_state_controller.h"
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/session_state_controller_impl.h"
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/session_state_controller_impl2.h"
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/system_gesture_event_filter.h"
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/system_modal_container_event_filter.h"
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/system_modal_container_layout_manager.h"
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/user_activity_detector.h"
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/video_detector.h"
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/window_animations.h"
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/window_cycle_controller.h"
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/window_properties.h"
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/window_util.h"
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/wm/workspace_controller.h"
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/debug/leak_annotations.h"
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/client/aura_constants.h"
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/client/user_action_client.h"
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/env.h"
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/focus_manager.h"
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/layout_manager.h"
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/root_window.h"
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/window.h"
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/ui_base_switches.h"
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/compositor/layer.h"
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/compositor/layer_animator.h"
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/display.h"
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/image/image_skia.h"
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/screen.h"
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/size.h"
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/keyboard/keyboard.h"
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/keyboard/keyboard_util.h"
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/message_center/message_center.h"
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/corewm/compound_event_filter.h"
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/corewm/corewm_switches.h"
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#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::IsForcedMaximizeMode() {
418  CommandLine* command_line = CommandLine::ForCurrentProcess();
419  return command_line->HasSwitch(switches::kForcedMaximizeMode);
420}
421
422void Shell::Init() {
423  delegate_->PreInit();
424#if defined(OS_CHROMEOS) && defined(USE_X11)
425  output_configurator_animation_.reset(
426      new internal::OutputConfiguratorAnimation());
427  output_configurator_->AddObserver(output_configurator_animation_.get());
428  if (base::chromeos::IsRunningOnChromeOS()) {
429    display_change_observer_.reset(new internal::DisplayChangeObserverX11);
430    // Register |display_change_observer_| first so that the rest of
431    // observer gets invoked after the root windows are configured.
432    output_configurator_->AddObserver(display_change_observer_.get());
433    display_error_observer_.reset(new internal::DisplayErrorObserver());
434    output_configurator_->AddObserver(display_error_observer_.get());
435    output_configurator_->set_state_controller(display_change_observer_.get());
436    output_configurator_->Start();
437    display_change_observer_->OnDisplayModeChanged();
438  }
439#endif
440
441  // Install the custom factory first so that views::FocusManagers for Tray,
442  // Launcher, and WallPaper could be created by the factory.
443  views::FocusManagerFactory::Install(new AshFocusManagerFactory);
444
445  env_filter_.reset(new views::corewm::CompoundEventFilter);
446  AddPreTargetHandler(env_filter_.get());
447
448  // Env creates the compositor. Historically it seems to have been implicitly
449  // initialized first by the ActivationController, but now that FocusController
450  // no longer does this we need to do it explicitly.
451  aura::Env::GetInstance();
452  if (views::corewm::UseFocusController()) {
453    views::corewm::FocusController* focus_controller =
454        new views::corewm::FocusController(new wm::AshFocusRules);
455    focus_client_.reset(focus_controller);
456    activation_client_ = focus_controller;
457    activation_client_->AddObserver(this);
458  } else {
459    focus_client_.reset(new aura::FocusManager);
460    activation_controller_.reset(
461        new internal::ActivationController(
462            focus_client_.get(),
463            new internal::AshActivationController));
464    activation_client_ = activation_controller_.get();
465    AddPreTargetHandler(activation_controller_.get());
466  }
467
468  focus_cycler_.reset(new internal::FocusCycler());
469
470  screen_position_controller_.reset(new internal::ScreenPositionController);
471  root_window_host_factory_.reset(delegate_->CreateRootWindowHostFactory());
472  display_controller_->Start();
473  display_controller_->InitPrimaryDisplay();
474  aura::RootWindow* root_window = display_controller_->GetPrimaryRootWindow();
475  active_root_window_ = root_window;
476
477  cursor_manager_.SetDisplay(DisplayController::GetPrimaryDisplay());
478
479#if !defined(OS_MACOSX)
480  nested_dispatcher_controller_.reset(new NestedDispatcherController);
481  accelerator_controller_.reset(new AcceleratorController);
482#endif
483
484  // The order in which event filters are added is significant.
485  user_activity_detector_.reset(new UserActivityDetector);
486  AddPreTargetHandler(user_activity_detector_.get());
487
488  event_rewriter_filter_.reset(new internal::EventRewriterEventFilter);
489  AddPreTargetHandler(event_rewriter_filter_.get());
490
491  overlay_filter_.reset(new internal::OverlayEventFilter);
492  AddPreTargetHandler(overlay_filter_.get());
493  AddShellObserver(overlay_filter_.get());
494
495  input_method_filter_.reset(new views::corewm::InputMethodEventFilter(
496                                 root_window->GetAcceleratedWidget()));
497  AddPreTargetHandler(input_method_filter_.get());
498
499#if !defined(OS_MACOSX)
500  accelerator_filter_.reset(new internal::AcceleratorFilter);
501  AddPreTargetHandler(accelerator_filter_.get());
502#endif
503
504  event_transformation_handler_.reset(new internal::EventTransformationHandler);
505  AddPreTargetHandler(event_transformation_handler_.get());
506
507  system_gesture_filter_.reset(new internal::SystemGestureEventFilter);
508  AddPreTargetHandler(system_gesture_filter_.get());
509
510  capture_controller_.reset(new internal::CaptureController);
511
512  // The keyboard system must be initialized before the RootWindowController is
513  // created.
514  if (keyboard::IsKeyboardEnabled())
515    keyboard::InitializeKeyboard();
516
517  internal::RootWindowController* root_window_controller =
518      new internal::RootWindowController(root_window);
519  root_window_controller->CreateContainers();
520  root_window_controller->CreateSystemBackground(
521      delegate_->IsFirstRunAfterBoot());
522
523  CommandLine* command_line = CommandLine::ForCurrentProcess();
524
525  if (command_line->HasSwitch(ash::switches::kAshDisableNewLockAnimations))
526    session_state_controller_.reset(new SessionStateControllerImpl);
527  else
528    session_state_controller_.reset(new SessionStateControllerImpl2);
529  power_button_controller_.reset(new PowerButtonController(
530      session_state_controller_.get()));
531  AddShellObserver(session_state_controller_.get());
532
533  mouse_cursor_filter_.reset(new internal::MouseCursorEventFilter());
534  AddPreTargetHandler(mouse_cursor_filter_.get());
535
536  // Create Controllers that may need root window.
537  // TODO(oshima): Move as many controllers before creating
538  // RootWindowController as possible.
539  visibility_controller_.reset(new AshVisibilityController);
540  drag_drop_controller_.reset(new internal::DragDropController);
541  user_action_client_.reset(delegate_->CreateUserActionClient());
542  window_modality_controller_.reset(
543      new views::corewm::WindowModalityController);
544  AddPreTargetHandler(window_modality_controller_.get());
545
546  magnification_controller_.reset(
547      MagnificationController::CreateInstance());
548
549  partial_magnification_controller_.reset(
550      new PartialMagnificationController());
551
552  high_contrast_controller_.reset(new HighContrastController);
553  video_detector_.reset(new VideoDetector);
554  window_cycle_controller_.reset(new WindowCycleController(activation_client_));
555
556  tooltip_controller_.reset(new views::corewm::TooltipController(
557                                gfx::SCREEN_TYPE_ALTERNATE));
558  AddPreTargetHandler(tooltip_controller_.get());
559
560  event_client_.reset(new internal::EventClientImpl);
561
562  InitRootWindowController(root_window_controller);
563
564  // This controller needs to be set before SetupManagedWindowMode.
565  desktop_background_controller_.reset(new DesktopBackgroundController());
566  user_wallpaper_delegate_.reset(delegate_->CreateUserWallpaperDelegate());
567  if (!user_wallpaper_delegate_)
568    user_wallpaper_delegate_.reset(new DummyUserWallpaperDelegate());
569
570  // StatusAreaWidget uses Shell's CapsLockDelegate.
571  caps_lock_delegate_.reset(delegate_->CreateCapsLockDelegate());
572
573  session_state_delegate_.reset(delegate_->CreateSessionStateDelegate());
574
575  if (!command_line->HasSwitch(views::corewm::switches::kNoDropShadows)) {
576    resize_shadow_controller_.reset(new internal::ResizeShadowController());
577    shadow_controller_.reset(
578        new views::corewm::ShadowController(activation_client_));
579  }
580
581  // Create system_tray_notifier_ before the delegate.
582  system_tray_notifier_.reset(new ash::SystemTrayNotifier());
583
584  // Initialize system_tray_delegate_ before initializing StatusAreaWidget.
585  system_tray_delegate_.reset(delegate()->CreateSystemTrayDelegate());
586  if (!system_tray_delegate_)
587    system_tray_delegate_.reset(SystemTrayDelegate::CreateDummyDelegate());
588
589  // Creates StatusAreaWidget.
590  root_window_controller->InitForPrimaryDisplay();
591
592  // Initialize system_tray_delegate_ after StatusAreaWidget is created.
593  system_tray_delegate_->Initialize();
594
595  display_controller_->InitSecondaryDisplays();
596
597  // Force Layout
598  root_window_controller->root_window_layout()->OnWindowResized();
599
600  // It needs to be created after OnWindowResized has been called, otherwise the
601  // widget will not paint when restoring after a browser crash.  Also it needs
602  // to be created after InitSecondaryDisplays() to initialize the wallpapers in
603  // the correct size.
604  user_wallpaper_delegate_->InitializeWallpaper();
605
606  if (initially_hide_cursor_)
607    cursor_manager_.HideCursor();
608  cursor_manager_.SetCursor(ui::kCursorPointer);
609
610  if (!cursor_manager_.IsCursorVisible()) {
611    // Cursor might have been hidden by something other than chrome.
612    // Let the first mouse event show the cursor.
613    env_filter_->set_cursor_hidden_by_filter(true);
614  }
615}
616
617void Shell::ShowContextMenu(const gfx::Point& location_in_screen) {
618  // No context menus if there is no session with an active user.
619  if (!session_state_delegate_->HasActiveUser())
620    return;
621  // No context menus when screen is locked.
622  if (session_state_delegate_->IsScreenLocked())
623    return;
624
625  aura::RootWindow* root =
626      wm::GetRootWindowMatching(gfx::Rect(location_in_screen, gfx::Size()));
627  // TODO(oshima): The root and root window controller shouldn't be
628  // NULL even for the out-of-bounds |location_in_screen| (It should
629  // return the primary root). Investigate why/how this is
630  // happening. crbug.com/165214.
631  internal::RootWindowController* rwc = GetRootWindowController(root);
632  CHECK(rwc) << "root=" << root
633             << ", location:" << location_in_screen.ToString();
634  if (rwc)
635    rwc->ShowContextMenu(location_in_screen);
636}
637
638void Shell::ToggleAppList(aura::Window* window) {
639  // If the context window is not given, show it on the active root window.
640  if (!window)
641    window = GetActiveRootWindow();
642  if (!app_list_controller_)
643    app_list_controller_.reset(new internal::AppListController);
644  app_list_controller_->SetVisible(!app_list_controller_->IsVisible(), window);
645}
646
647void Shell::SetDragAndDropHostOfCurrentAppList(
648    app_list::ApplicationDragAndDropHost* drag_and_drop_host) {
649  if (app_list_controller_.get())
650    app_list_controller_->SetDragAndDropHostOfCurrentAppList(
651        drag_and_drop_host);
652}
653
654bool Shell::GetAppListTargetVisibility() const {
655  return app_list_controller_.get() &&
656      app_list_controller_->GetTargetVisibility();
657}
658
659aura::Window* Shell::GetAppListWindow() {
660  return app_list_controller_.get() ? app_list_controller_->GetWindow() : NULL;
661}
662
663bool Shell::IsSystemModalWindowOpen() const {
664  if (simulate_modal_window_open_for_testing_)
665    return true;
666  const std::vector<aura::Window*> containers = GetContainersFromAllRootWindows(
667      internal::kShellWindowId_SystemModalContainer, NULL);
668  for (std::vector<aura::Window*>::const_iterator cit = containers.begin();
669       cit != containers.end(); ++cit) {
670    for (aura::Window::Windows::const_iterator wit = (*cit)->children().begin();
671         wit != (*cit)->children().end(); ++wit) {
672      if ((*wit)->GetProperty(aura::client::kModalKey) ==
673          ui::MODAL_TYPE_SYSTEM && (*wit)->TargetVisibility()) {
674        return true;
675      }
676    }
677  }
678  return false;
679}
680
681views::NonClientFrameView* Shell::CreateDefaultNonClientFrameView(
682    views::Widget* widget) {
683  if (views::DialogDelegate::UseNewStyle())
684    return views::DialogDelegate::CreateNewStyleFrameView(widget);
685  // Use translucent-style window frames for dialogs.
686  CustomFrameViewAsh* frame_view = new CustomFrameViewAsh;
687  frame_view->Init(widget);
688  return frame_view;
689}
690
691void Shell::RotateFocus(Direction direction) {
692  focus_cycler_->RotateFocus(
693      direction == FORWARD ? internal::FocusCycler::FORWARD :
694                             internal::FocusCycler::BACKWARD);
695}
696
697void Shell::SetDisplayWorkAreaInsets(Window* contains,
698                                     const gfx::Insets& insets) {
699  if (!display_manager_->UpdateWorkAreaOfDisplayNearestWindow(contains, insets))
700    return;
701  FOR_EACH_OBSERVER(ShellObserver, observers_,
702                    OnDisplayWorkAreaInsetsChanged());
703}
704
705void Shell::OnLoginStateChanged(user::LoginStatus status) {
706  FOR_EACH_OBSERVER(ShellObserver, observers_, OnLoginStateChanged(status));
707  RootWindowControllerList controllers = GetAllRootWindowControllers();
708  for (RootWindowControllerList::iterator iter = controllers.begin();
709       iter != controllers.end(); ++iter)
710    (*iter)->OnLoginStateChanged(status);
711}
712
713void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status) {
714  RootWindowControllerList controllers = GetAllRootWindowControllers();
715  for (RootWindowControllerList::iterator iter = controllers.begin();
716       iter != controllers.end(); ++iter)
717    (*iter)->UpdateAfterLoginStatusChange(status);
718}
719
720void Shell::OnAppTerminating() {
721  FOR_EACH_OBSERVER(ShellObserver, observers_, OnAppTerminating());
722}
723
724void Shell::OnLockStateChanged(bool locked) {
725  FOR_EACH_OBSERVER(ShellObserver, observers_, OnLockStateChanged(locked));
726}
727
728void Shell::CreateLauncher() {
729  RootWindowControllerList controllers = GetAllRootWindowControllers();
730  for (RootWindowControllerList::iterator iter = controllers.begin();
731       iter != controllers.end(); ++iter)
732    (*iter)->shelf()->CreateLauncher();
733}
734
735void Shell::ShowLauncher() {
736  RootWindowControllerList controllers = GetAllRootWindowControllers();
737  for (RootWindowControllerList::iterator iter = controllers.begin();
738       iter != controllers.end(); ++iter)
739    (*iter)->ShowLauncher();
740}
741
742void Shell::AddShellObserver(ShellObserver* observer) {
743  observers_.AddObserver(observer);
744}
745
746void Shell::RemoveShellObserver(ShellObserver* observer) {
747  observers_.RemoveObserver(observer);
748}
749
750void Shell::UpdateShelfVisibility() {
751  RootWindowControllerList controllers = GetAllRootWindowControllers();
752  for (RootWindowControllerList::iterator iter = controllers.begin();
753       iter != controllers.end(); ++iter)
754    if ((*iter)->shelf())
755      (*iter)->UpdateShelfVisibility();
756}
757
758void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior,
759                                     aura::RootWindow* root_window) {
760  ash::internal::ShelfLayoutManager::ForLauncher(root_window)->
761      SetAutoHideBehavior(behavior);
762}
763
764ShelfAutoHideBehavior Shell::GetShelfAutoHideBehavior(
765    aura::RootWindow* root_window) const {
766  return ash::internal::ShelfLayoutManager::ForLauncher(root_window)->
767      auto_hide_behavior();
768}
769
770void Shell::SetShelfAlignment(ShelfAlignment alignment,
771                              aura::RootWindow* root_window) {
772  if (ash::internal::ShelfLayoutManager::ForLauncher(root_window)->
773      SetAlignment(alignment)) {
774    FOR_EACH_OBSERVER(
775        ShellObserver, observers_, OnShelfAlignmentChanged(root_window));
776  }
777}
778
779ShelfAlignment Shell::GetShelfAlignment(aura::RootWindow* root_window) {
780  return GetRootWindowController(root_window)->
781      GetShelfLayoutManager()->GetAlignment();
782}
783
784void Shell::SetDimming(bool should_dim) {
785  RootWindowControllerList controllers = GetAllRootWindowControllers();
786  for (RootWindowControllerList::iterator iter = controllers.begin();
787       iter != controllers.end(); ++iter)
788    (*iter)->screen_dimmer()->SetDimming(should_dim);
789}
790
791void Shell::CreateModalBackground(aura::Window* window) {
792  if (!modality_filter_) {
793    modality_filter_.reset(new internal::SystemModalContainerEventFilter(this));
794    AddPreTargetHandler(modality_filter_.get());
795  }
796  RootWindowControllerList controllers = GetAllRootWindowControllers();
797  for (RootWindowControllerList::iterator iter = controllers.begin();
798       iter != controllers.end(); ++iter)
799    (*iter)->GetSystemModalLayoutManager(window)->CreateModalBackground();
800}
801
802void Shell::OnModalWindowRemoved(aura::Window* removed) {
803  RootWindowControllerList controllers = GetAllRootWindowControllers();
804  bool activated = false;
805  for (RootWindowControllerList::iterator iter = controllers.begin();
806       iter != controllers.end() && !activated; ++iter) {
807    activated = (*iter)->GetSystemModalLayoutManager(removed)->
808        ActivateNextModalWindow();
809  }
810  if (!activated) {
811    RemovePreTargetHandler(modality_filter_.get());
812    modality_filter_.reset();
813    for (RootWindowControllerList::iterator iter = controllers.begin();
814         iter != controllers.end(); ++iter)
815      (*iter)->GetSystemModalLayoutManager(removed)->DestroyModalBackground();
816  }
817}
818
819WebNotificationTray* Shell::GetWebNotificationTray() {
820  return GetPrimaryRootWindowController()->shelf()->
821      status_area_widget()->web_notification_tray();
822}
823
824bool Shell::HasPrimaryStatusArea() {
825  ShelfWidget* shelf = GetPrimaryRootWindowController()->shelf();
826  return shelf && shelf->status_area_widget();
827}
828
829SystemTray* Shell::GetPrimarySystemTray() {
830  return GetPrimaryRootWindowController()->GetSystemTray();
831}
832
833LauncherDelegate* Shell::GetLauncherDelegate() {
834  if (!launcher_delegate_) {
835    launcher_model_.reset(new LauncherModel);
836    launcher_delegate_.reset(
837        delegate_->CreateLauncherDelegate(launcher_model_.get()));
838  }
839  return launcher_delegate_.get();
840}
841
842void Shell::InitRootWindowForSecondaryDisplay(aura::RootWindow* root) {
843  aura::client::SetFocusClient(root, focus_client_.get());
844  internal::RootWindowController* controller =
845      new internal::RootWindowController(root);
846  controller->CreateContainers();
847  // Pass false for the |is_first_run_after_boot| parameter so we'll show a
848  // black background on this display instead of trying to mimic the boot splash
849  // screen.
850  controller->CreateSystemBackground(false);
851  InitRootWindowController(controller);
852  controller->InitForPrimaryDisplay();
853  controller->root_window_layout()->OnWindowResized();
854  desktop_background_controller_->OnRootWindowAdded(root);
855  high_contrast_controller_->OnRootWindowAdded(root);
856  root->ShowRootWindow();
857  // Activate new root for testing.
858  active_root_window_ = root;
859}
860
861void Shell::DoInitialWorkspaceAnimation() {
862  return GetPrimaryRootWindowController()->workspace_controller()->
863      DoInitialAnimation();
864}
865
866void Shell::InitRootWindowController(
867    internal::RootWindowController* controller) {
868  aura::RootWindow* root_window = controller->root_window();
869  DCHECK(activation_client_);
870  DCHECK(visibility_controller_.get());
871  DCHECK(drag_drop_controller_.get());
872  DCHECK(capture_controller_.get());
873  DCHECK(window_cycle_controller_.get());
874
875  aura::client::SetFocusClient(root_window, focus_client_.get());
876  input_method_filter_->SetInputMethodPropertyInRootWindow(root_window);
877  aura::client::SetActivationClient(root_window, activation_client_);
878  if (views::corewm::UseFocusController()) {
879    views::corewm::FocusController* controller =
880        static_cast<views::corewm::FocusController*>(activation_client_);
881    root_window->AddPreTargetHandler(controller);
882  }
883  aura::client::SetVisibilityClient(root_window, visibility_controller_.get());
884  aura::client::SetDragDropClient(root_window, drag_drop_controller_.get());
885  aura::client::SetCaptureClient(root_window, capture_controller_.get());
886  aura::client::SetScreenPositionClient(root_window,
887                                        screen_position_controller_.get());
888  aura::client::SetCursorClient(root_window, &cursor_manager_);
889  aura::client::SetTooltipClient(root_window, tooltip_controller_.get());
890  aura::client::SetEventClient(root_window, event_client_.get());
891
892  if (nested_dispatcher_controller_) {
893    aura::client::SetDispatcherClient(root_window,
894                                      nested_dispatcher_controller_.get());
895  }
896  if (user_action_client_)
897    aura::client::SetUserActionClient(root_window, user_action_client_.get());
898
899  root_window->SetCursor(ui::kCursorPointer);
900  controller->InitLayoutManagers();
901
902  // TODO(oshima): Move the instance to RootWindowController when
903  // the extended desktop is enabled by default.
904  internal::AlwaysOnTopController* always_on_top_controller =
905      new internal::AlwaysOnTopController;
906  always_on_top_controller->SetAlwaysOnTopContainer(
907      root_window->GetChildById(internal::kShellWindowId_AlwaysOnTopContainer));
908  root_window->SetProperty(internal::kAlwaysOnTopControllerKey,
909                           always_on_top_controller);
910  if (GetPrimaryRootWindowController()->GetSystemModalLayoutManager(NULL)->
911          has_modal_background()) {
912    controller->GetSystemModalLayoutManager(NULL)->CreateModalBackground();
913  }
914
915  window_cycle_controller_->OnRootWindowAdded(root_window);
916}
917
918////////////////////////////////////////////////////////////////////////////////
919// Shell, private:
920
921bool Shell::CanWindowReceiveEvents(aura::Window* window) {
922  RootWindowControllerList controllers = GetAllRootWindowControllers();
923  for (RootWindowControllerList::iterator iter = controllers.begin();
924       iter != controllers.end(); ++iter) {
925    if ((*iter)->GetSystemModalLayoutManager(window)->
926            CanWindowReceiveEvents(window)) {
927      return true;
928    }
929  }
930  return false;
931}
932
933////////////////////////////////////////////////////////////////////////////////
934// Shell, ui::EventTarget overrides:
935
936bool Shell::CanAcceptEvent(const ui::Event& event) {
937  return true;
938}
939
940ui::EventTarget* Shell::GetParentTarget() {
941  return NULL;
942}
943
944void Shell::OnEvent(ui::Event* event) {
945}
946
947////////////////////////////////////////////////////////////////////////////////
948// Shell, aura::client::ActivationChangeObserver implementation:
949
950void Shell::OnWindowActivated(aura::Window* gained_active,
951                              aura::Window* lost_active) {
952  if (gained_active)
953    active_root_window_ = gained_active->GetRootWindow();
954}
955
956}  // namespace ash
957