blob: 1bade434dd44a35b45a7547c5bd7dd0329067b33 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000014#include "ash/desktop_background/desktop_background_view.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000015#include "ash/desktop_background/user_wallpaper_delegate.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000016#include "ash/display/display_controller.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000017#include "ash/display/display_manager.h"
18#include "ash/display/event_transformation_handler.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000019#include "ash/display/mouse_cursor_event_filter.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000020#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"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000024#include "ash/host/root_window_host_factory.h"
25#include "ash/launcher/launcher_delegate.h"
26#include "ash/launcher/launcher_model.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000027#include "ash/magnifier/magnification_controller.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000028#include "ash/magnifier/partial_magnification_controller.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000029#include "ash/root_window_controller.h"
30#include "ash/screen_ash.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010031#include "ash/session_state_delegate.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000032#include "ash/shelf/shelf_layout_manager.h"
33#include "ash/shelf/shelf_widget.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000034#include "ash/shell_delegate.h"
35#include "ash/shell_factory.h"
36#include "ash/shell_window_ids.h"
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010037#include "ash/system/locale/locale_notification_controller.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000038#include "ash/system/status_area_widget.h"
39#include "ash/system/tray/system_tray_delegate.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000040#include "ash/system/tray/system_tray_notifier.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000041#include "ash/wm/activation_controller.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000042#include "ash/wm/app_list_controller.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000043#include "ash/wm/ash_activation_controller.h"
44#include "ash/wm/ash_focus_rules.h"
45#include "ash/wm/ash_native_cursor_manager.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000046#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"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000050#include "ash/wm/event_client_impl.h"
51#include "ash/wm/event_rewriter_event_filter.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010052#include "ash/wm/lock_state_controller.h"
53#include "ash/wm/lock_state_controller_impl2.h"
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010054#include "ash/wm/mru_window_tracker.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000055#include "ash/wm/overlay_event_filter.h"
56#include "ash/wm/power_button_controller.h"
57#include "ash/wm/property_util.h"
58#include "ash/wm/resize_shadow_controller.h"
59#include "ash/wm/root_window_layout_manager.h"
60#include "ash/wm/screen_dimmer.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000061#include "ash/wm/session_state_controller_impl.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000062#include "ash/wm/system_gesture_event_filter.h"
63#include "ash/wm/system_modal_container_event_filter.h"
64#include "ash/wm/system_modal_container_layout_manager.h"
65#include "ash/wm/user_activity_detector.h"
66#include "ash/wm/video_detector.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000067#include "ash/wm/window_animations.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000068#include "ash/wm/window_cycle_controller.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000069#include "ash/wm/window_properties.h"
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010070#include "ash/wm/window_selector_controller.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000071#include "ash/wm/window_util.h"
72#include "ash/wm/workspace_controller.h"
73#include "base/bind.h"
74#include "base/command_line.h"
75#include "base/debug/leak_annotations.h"
76#include "ui/aura/client/aura_constants.h"
77#include "ui/aura/client/user_action_client.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000078#include "ui/aura/env.h"
79#include "ui/aura/focus_manager.h"
80#include "ui/aura/layout_manager.h"
81#include "ui/aura/root_window.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000082#include "ui/aura/window.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000083#include "ui/base/ui_base_switches.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000084#include "ui/compositor/layer.h"
85#include "ui/compositor/layer_animator.h"
86#include "ui/gfx/display.h"
87#include "ui/gfx/image/image_skia.h"
88#include "ui/gfx/screen.h"
89#include "ui/gfx/size.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010090#include "ui/keyboard/keyboard.h"
91#include "ui/keyboard/keyboard_util.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000092#include "ui/message_center/message_center.h"
93#include "ui/views/corewm/compound_event_filter.h"
94#include "ui/views/corewm/corewm_switches.h"
95#include "ui/views/corewm/focus_controller.h"
96#include "ui/views/corewm/input_method_event_filter.h"
97#include "ui/views/corewm/shadow_controller.h"
98#include "ui/views/corewm/tooltip_controller.h"
99#include "ui/views/corewm/visibility_controller.h"
100#include "ui/views/corewm/window_modality_controller.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000101#include "ui/views/focus/focus_manager_factory.h"
102#include "ui/views/widget/native_widget_aura.h"
103#include "ui/views/widget/widget.h"
104
105#if !defined(OS_MACOSX)
106#include "ash/accelerators/accelerator_controller.h"
107#include "ash/accelerators/accelerator_filter.h"
108#include "ash/accelerators/nested_dispatcher_controller.h"
109#endif
110
Ben Murdocheb525c52013-07-10 11:40:50 +0100111#if defined(OS_CHROMEOS)
112#if defined(USE_X11)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000113#include "ash/ash_constants.h"
114#include "ash/display/display_change_observer_x11.h"
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100115#include "ash/display/display_error_observer.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000116#include "ash/display/output_configurator_animation.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000117#include "base/chromeos/chromeos_version.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100118#include "base/message_loop/message_pump_aurax11.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000119#include "chromeos/display/output_configurator.h"
120#include "content/public/browser/gpu_data_manager.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000121#include "content/public/common/content_switches.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100122#include "gpu/config/gpu_feature_type.h"
Ben Murdocheb525c52013-07-10 11:40:50 +0100123#endif // defined(USE_X11)
124#include "ash/system/chromeos/power/power_status.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000125#endif // defined(OS_CHROMEOS)
126
127namespace ash {
128
129namespace {
130
131using aura::Window;
132using views::Widget;
133
134// This dummy class is used for shell unit tests. We dont have chrome delegate
135// in these tests.
136class DummyUserWallpaperDelegate : public UserWallpaperDelegate {
137 public:
138 DummyUserWallpaperDelegate() {}
139
140 virtual ~DummyUserWallpaperDelegate() {}
141
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000142 virtual int GetAnimationType() OVERRIDE {
143 return views::corewm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000144 }
145
146 virtual bool ShouldShowInitialAnimation() OVERRIDE {
147 return false;
148 }
149
150 virtual void UpdateWallpaper() OVERRIDE {
151 }
152
153 virtual void InitializeWallpaper() OVERRIDE {
154 ash::Shell::GetInstance()->desktop_background_controller()->
155 CreateEmptyWallpaper();
156 }
157
158 virtual void OpenSetWallpaperPage() OVERRIDE {
159 }
160
161 virtual bool CanOpenSetWallpaperPage() OVERRIDE {
162 return false;
163 }
164
165 virtual void OnWallpaperAnimationFinished() OVERRIDE {
166 }
167
168 virtual void OnWallpaperBootAnimationFinished() OVERRIDE {
169 }
170
171 private:
172 DISALLOW_COPY_AND_ASSIGN(DummyUserWallpaperDelegate);
173};
174
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000175// A Corewm VisibilityController subclass that calls the Ash animation routine
176// so we can pick up our extended animations. See ash/wm/window_animations.h.
177class AshVisibilityController : public views::corewm::VisibilityController {
178 public:
179 AshVisibilityController() {}
180 virtual ~AshVisibilityController() {}
181
182 private:
183 // Overridden from views::corewm::VisibilityController:
184 virtual bool CallAnimateOnChildWindowVisibilityChanged(
185 aura::Window* window,
186 bool visible) OVERRIDE {
187 return AnimateOnChildWindowVisibilityChanged(window, visible);
188 }
189
190 DISALLOW_COPY_AND_ASSIGN(AshVisibilityController);
191};
192
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000193} // namespace
194
195// static
196Shell* Shell::instance_ = NULL;
197// static
198bool Shell::initially_hide_cursor_ = false;
199
200////////////////////////////////////////////////////////////////////////////////
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000201// Shell, public:
202
203Shell::Shell(ShellDelegate* delegate)
204 : screen_(new ScreenAsh),
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100205 target_root_window_(NULL),
206 scoped_target_root_window_(NULL),
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000207 delegate_(delegate),
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000208 activation_client_(NULL),
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100209#if defined(OS_CHROMEOS) && defined(USE_X11)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000210 output_configurator_(new chromeos::OutputConfigurator()),
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000211#endif // defined(OS_CHROMEOS)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000212 native_cursor_manager_(new AshNativeCursorManager),
213 cursor_manager_(scoped_ptr<views::corewm::NativeCursorManager>(
214 native_cursor_manager_)),
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000215 browser_context_(NULL),
Ben Murdocheb525c52013-07-10 11:40:50 +0100216 simulate_modal_window_open_for_testing_(false),
217 is_touch_hud_projection_enabled_(false) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000218 DCHECK(delegate_.get());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000219 display_manager_.reset(new internal::DisplayManager);
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100220
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000221 ANNOTATE_LEAKING_OBJECT_PTR(screen_); // see crbug.com/156466
222 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_ALTERNATE, screen_);
223 if (!gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE))
224 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000225 display_controller_.reset(new DisplayController);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100226#if defined(OS_CHROMEOS) && defined(USE_X11)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000227 bool is_panel_fitting_disabled =
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100228 content::GpuDataManager::GetInstance()->IsFeatureBlacklisted(
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100229 gpu::GPU_FEATURE_TYPE_PANEL_FITTING) ||
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000230 CommandLine::ForCurrentProcess()->HasSwitch(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000231 ::switches::kDisablePanelFitting);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000232
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100233 output_configurator_->Init(!is_panel_fitting_disabled);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000234
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000235 base::MessagePumpAuraX11::Current()->AddDispatcherForRootWindow(
236 output_configurator());
Ben Murdoch558790d2013-07-30 15:19:42 +0100237 // We can't do this with a root window listener because XI_HierarchyChanged
238 // messages don't have a target window.
239 base::MessagePumpAuraX11::Current()->AddObserver(output_configurator());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000240#endif // defined(OS_CHROMEOS)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000241 AddPreTargetHandler(this);
Ben Murdocheb525c52013-07-10 11:40:50 +0100242
243#if defined(OS_CHROMEOS)
244 internal::PowerStatus::Initialize();
245#endif
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000246}
247
248Shell::~Shell() {
249 views::FocusManagerFactory::Install(NULL);
250
251 // Remove the focus from any window. This will prevent overhead and side
252 // effects (e.g. crashes) from changing focus during shutdown.
253 // See bug crbug.com/134502.
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100254 aura::client::GetFocusClient(GetPrimaryRootWindow())->FocusWindow(NULL);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000255
256 // Please keep in same order as in Init() because it's easy to miss one.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000257 RemovePreTargetHandler(event_rewriter_filter_.get());
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100258 RemovePreTargetHandler(user_activity_detector_.get());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000259 RemovePreTargetHandler(overlay_filter_.get());
260 RemovePreTargetHandler(input_method_filter_.get());
261 RemovePreTargetHandler(window_modality_controller_.get());
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100262 if (mouse_cursor_filter_)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000263 RemovePreTargetHandler(mouse_cursor_filter_.get());
264 RemovePreTargetHandler(system_gesture_filter_.get());
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100265 RemovePreTargetHandler(event_transformation_handler_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000266#if !defined(OS_MACOSX)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000267 RemovePreTargetHandler(accelerator_filter_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000268#endif
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000269
270 // TooltipController is deleted with the Shell so removing its references.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000271 RemovePreTargetHandler(tooltip_controller_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000272
273 // AppList needs to be released before shelf layout manager, which is
274 // destroyed with launcher container in the loop below. However, app list
275 // container is now on top of launcher container and released after it.
276 // TODO(xiyuan): Move it back when app list container is no longer needed.
277 app_list_controller_.reset();
278
279 // Destroy SystemTrayDelegate before destroying the status area(s).
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100280 system_tray_delegate_->Shutdown();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000281 system_tray_delegate_.reset();
282
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100283 locale_notification_controller_.reset();
284
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000285 // Destroy all child windows including widgets.
286 display_controller_->CloseChildWindows();
287
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000288 // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems
289 // needs to remove observers from it.
290 system_tray_notifier_.reset();
291
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000292 // These need a valid Shell instance to clean up properly, so explicitly
293 // delete them before invalidating the instance.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000294 // Alphabetical. TODO(oshima): sort.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000295 drag_drop_controller_.reset();
296 magnification_controller_.reset();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000297 partial_magnification_controller_.reset();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000298 resize_shadow_controller_.reset();
299 shadow_controller_.reset();
300 tooltip_controller_.reset();
301 event_client_.reset();
302 window_cycle_controller_.reset();
303 capture_controller_.reset();
304 nested_dispatcher_controller_.reset();
305 user_action_client_.reset();
306 visibility_controller_.reset();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000307 launcher_delegate_.reset();
308 launcher_model_.reset();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100309 video_detector_.reset();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000310
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000311 power_button_controller_.reset();
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100312 lock_state_controller_.reset();
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100313 mru_window_tracker_.reset();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000314
315 // This also deletes all RootWindows. Note that we invoke Shutdown() on
316 // DisplayController before resetting |display_controller_|, since destruction
317 // of its owned RootWindowControllers relies on the value.
318 display_controller_->Shutdown();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000319 display_controller_.reset();
320 screen_position_controller_.reset();
321
322 // Delete the activation controller after other controllers and launcher
323 // because they might have registered ActivationChangeObserver.
324 activation_controller_.reset();
325
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100326#if defined(OS_CHROMEOS) && defined(USE_X11)
327 if (display_change_observer_)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000328 output_configurator_->RemoveObserver(display_change_observer_.get());
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100329 if (output_configurator_animation_)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000330 output_configurator_->RemoveObserver(output_configurator_animation_.get());
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100331 if (display_error_observer_)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000332 output_configurator_->RemoveObserver(display_error_observer_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000333 base::MessagePumpAuraX11::Current()->RemoveDispatcherForRootWindow(
334 output_configurator());
Ben Murdoch558790d2013-07-30 15:19:42 +0100335 base::MessagePumpAuraX11::Current()->RemoveObserver(output_configurator());
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100336 display_change_observer_.reset();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000337#endif // defined(OS_CHROMEOS)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000338
Ben Murdocheb525c52013-07-10 11:40:50 +0100339#if defined(OS_CHROMEOS)
340 internal::PowerStatus::Shutdown();
341#endif
342
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000343 DCHECK(instance_ == this);
344 instance_ = NULL;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000345}
346
347// static
348Shell* Shell::CreateInstance(ShellDelegate* delegate) {
349 CHECK(!instance_);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000350 instance_ = new Shell(delegate);
351 instance_->Init();
352 return instance_;
353}
354
355// static
356Shell* Shell::GetInstance() {
357 DCHECK(instance_);
358 return instance_;
359}
360
361// static
362bool Shell::HasInstance() {
363 return !!instance_;
364}
365
366// static
367void Shell::DeleteInstance() {
368 delete instance_;
369 instance_ = NULL;
370}
371
372// static
373internal::RootWindowController* Shell::GetPrimaryRootWindowController() {
374 return GetRootWindowController(GetPrimaryRootWindow());
375}
376
377// static
378Shell::RootWindowControllerList Shell::GetAllRootWindowControllers() {
379 return Shell::GetInstance()->display_controller()->
380 GetAllRootWindowControllers();
381}
382
383// static
384aura::RootWindow* Shell::GetPrimaryRootWindow() {
385 return GetInstance()->display_controller()->GetPrimaryRootWindow();
386}
387
388// static
389aura::RootWindow* Shell::GetActiveRootWindow() {
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100390 Shell* shell = GetInstance();
391 if (shell->scoped_target_root_window_)
392 return shell->scoped_target_root_window_;
393 return shell->target_root_window_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000394}
395
396// static
397gfx::Screen* Shell::GetScreen() {
398 return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE);
399}
400
401// static
402Shell::RootWindowList Shell::GetAllRootWindows() {
403 return Shell::GetInstance()->display_controller()->
404 GetAllRootWindows();
405}
406
407// static
408aura::Window* Shell::GetContainer(aura::RootWindow* root_window,
409 int container_id) {
410 return root_window->GetChildById(container_id);
411}
412
413// static
414const aura::Window* Shell::GetContainer(const aura::RootWindow* root_window,
415 int container_id) {
416 return root_window->GetChildById(container_id);
417}
418
419// static
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000420std::vector<aura::Window*> Shell::GetContainersFromAllRootWindows(
421 int container_id,
422 aura::RootWindow* priority_root) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000423 std::vector<aura::Window*> containers;
424 RootWindowList root_windows = GetAllRootWindows();
425 for (RootWindowList::const_iterator it = root_windows.begin();
426 it != root_windows.end(); ++it) {
427 aura::Window* container = (*it)->GetChildById(container_id);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000428 if (container) {
429 if (priority_root && priority_root->Contains(container))
430 containers.insert(containers.begin(), container);
431 else
432 containers.push_back(container);
433 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000434 }
435 return containers;
436}
437
438// static
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100439bool Shell::IsForcedMaximizeMode() {
440 CommandLine* command_line = CommandLine::ForCurrentProcess();
441 return command_line->HasSwitch(switches::kForcedMaximizeMode);
442}
443
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000444void Shell::Init() {
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100445 CommandLine* command_line = CommandLine::ForCurrentProcess();
446
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000447 delegate_->PreInit();
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100448 bool display_initialized = false;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100449#if defined(OS_CHROMEOS) && defined(USE_X11)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000450 output_configurator_animation_.reset(
451 new internal::OutputConfiguratorAnimation());
452 output_configurator_->AddObserver(output_configurator_animation_.get());
453 if (base::chromeos::IsRunningOnChromeOS()) {
454 display_change_observer_.reset(new internal::DisplayChangeObserverX11);
455 // Register |display_change_observer_| first so that the rest of
456 // observer gets invoked after the root windows are configured.
457 output_configurator_->AddObserver(display_change_observer_.get());
458 display_error_observer_.reset(new internal::DisplayErrorObserver());
459 output_configurator_->AddObserver(display_error_observer_.get());
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100460 output_configurator_->set_state_controller(display_change_observer_.get());
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100461 if (!command_line->HasSwitch(ash::switches::kAshDisableSoftwareMirroring))
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100462 output_configurator_->set_mirroring_controller(display_manager_.get());
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100463 output_configurator_->Start(
464 delegate_->IsFirstRunAfterBoot() ? kChromeOsBootColor : 0);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100465 display_initialized = true;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000466 }
467#endif
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100468 if (!display_initialized)
469 display_manager_->InitFromCommandLine();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000470
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000471 // Install the custom factory first so that views::FocusManagers for Tray,
472 // Launcher, and WallPaper could be created by the factory.
473 views::FocusManagerFactory::Install(new AshFocusManagerFactory);
474
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000475 env_filter_.reset(new views::corewm::CompoundEventFilter);
476 AddPreTargetHandler(env_filter_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000477
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000478 // Env creates the compositor. Historically it seems to have been implicitly
479 // initialized first by the ActivationController, but now that FocusController
480 // no longer does this we need to do it explicitly.
481 aura::Env::GetInstance();
482 if (views::corewm::UseFocusController()) {
483 views::corewm::FocusController* focus_controller =
484 new views::corewm::FocusController(new wm::AshFocusRules);
485 focus_client_.reset(focus_controller);
486 activation_client_ = focus_controller;
487 activation_client_->AddObserver(this);
488 } else {
489 focus_client_.reset(new aura::FocusManager);
490 activation_controller_.reset(
491 new internal::ActivationController(
492 focus_client_.get(),
493 new internal::AshActivationController));
494 activation_client_ = activation_controller_.get();
495 AddPreTargetHandler(activation_controller_.get());
496 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000497
498 focus_cycler_.reset(new internal::FocusCycler());
499
500 screen_position_controller_.reset(new internal::ScreenPositionController);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000501 root_window_host_factory_.reset(delegate_->CreateRootWindowHostFactory());
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100502
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000503 display_controller_->Start();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000504 display_controller_->InitPrimaryDisplay();
505 aura::RootWindow* root_window = display_controller_->GetPrimaryRootWindow();
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100506 target_root_window_ = root_window;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000507
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000508 cursor_manager_.SetDisplay(DisplayController::GetPrimaryDisplay());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000509
510#if !defined(OS_MACOSX)
511 nested_dispatcher_controller_.reset(new NestedDispatcherController);
512 accelerator_controller_.reset(new AcceleratorController);
513#endif
514
515 // The order in which event filters are added is significant.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000516 event_rewriter_filter_.reset(new internal::EventRewriterEventFilter);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000517 AddPreTargetHandler(event_rewriter_filter_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000518
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100519 // UserActivityDetector passes events to observers, so let them get
520 // rewritten first.
521 user_activity_detector_.reset(new UserActivityDetector);
522 AddPreTargetHandler(user_activity_detector_.get());
523
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000524 overlay_filter_.reset(new internal::OverlayEventFilter);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000525 AddPreTargetHandler(overlay_filter_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000526 AddShellObserver(overlay_filter_.get());
527
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000528 input_method_filter_.reset(new views::corewm::InputMethodEventFilter(
529 root_window->GetAcceleratedWidget()));
530 AddPreTargetHandler(input_method_filter_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000531
532#if !defined(OS_MACOSX)
533 accelerator_filter_.reset(new internal::AcceleratorFilter);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000534 AddPreTargetHandler(accelerator_filter_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000535#endif
536
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000537 event_transformation_handler_.reset(new internal::EventTransformationHandler);
538 AddPreTargetHandler(event_transformation_handler_.get());
539
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000540 system_gesture_filter_.reset(new internal::SystemGestureEventFilter);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000541 AddPreTargetHandler(system_gesture_filter_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000542
543 capture_controller_.reset(new internal::CaptureController);
544
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100545 // The keyboard system must be initialized before the RootWindowController is
546 // created.
547 if (keyboard::IsKeyboardEnabled())
548 keyboard::InitializeKeyboard();
549
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000550 if (command_line->HasSwitch(ash::switches::kAshDisableNewLockAnimations))
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100551 lock_state_controller_.reset(new SessionStateControllerImpl);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000552 else
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100553 lock_state_controller_.reset(new LockStateControllerImpl2);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000554 power_button_controller_.reset(new PowerButtonController(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100555 lock_state_controller_.get()));
556 AddShellObserver(lock_state_controller_.get());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000557
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100558 drag_drop_controller_.reset(new internal::DragDropController);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000559 mouse_cursor_filter_.reset(new internal::MouseCursorEventFilter());
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100560 PrependPreTargetHandler(mouse_cursor_filter_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000561
562 // Create Controllers that may need root window.
563 // TODO(oshima): Move as many controllers before creating
564 // RootWindowController as possible.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000565 visibility_controller_.reset(new AshVisibilityController);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000566 user_action_client_.reset(delegate_->CreateUserActionClient());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000567 window_modality_controller_.reset(
568 new views::corewm::WindowModalityController);
569 AddPreTargetHandler(window_modality_controller_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000570
571 magnification_controller_.reset(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000572 MagnificationController::CreateInstance());
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100573 mru_window_tracker_.reset(new MruWindowTracker(activation_client_));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000574
575 partial_magnification_controller_.reset(
576 new PartialMagnificationController());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000577
578 high_contrast_controller_.reset(new HighContrastController);
579 video_detector_.reset(new VideoDetector);
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100580 window_cycle_controller_.reset(new WindowCycleController());
581 window_selector_controller_.reset(new WindowSelectorController());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000582
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000583 tooltip_controller_.reset(new views::corewm::TooltipController(
584 gfx::SCREEN_TYPE_ALTERNATE));
585 AddPreTargetHandler(tooltip_controller_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000586
587 event_client_.reset(new internal::EventClientImpl);
588
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000589 // This controller needs to be set before SetupManagedWindowMode.
590 desktop_background_controller_.reset(new DesktopBackgroundController());
591 user_wallpaper_delegate_.reset(delegate_->CreateUserWallpaperDelegate());
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100592 if (!user_wallpaper_delegate_)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000593 user_wallpaper_delegate_.reset(new DummyUserWallpaperDelegate());
594
595 // StatusAreaWidget uses Shell's CapsLockDelegate.
596 caps_lock_delegate_.reset(delegate_->CreateCapsLockDelegate());
597
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100598 session_state_delegate_.reset(delegate_->CreateSessionStateDelegate());
599
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000600 if (!command_line->HasSwitch(views::corewm::switches::kNoDropShadows)) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000601 resize_shadow_controller_.reset(new internal::ResizeShadowController());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000602 shadow_controller_.reset(
603 new views::corewm::ShadowController(activation_client_));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000604 }
605
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000606 // Create system_tray_notifier_ before the delegate.
607 system_tray_notifier_.reset(new ash::SystemTrayNotifier());
608
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000609 // Initialize system_tray_delegate_ before initializing StatusAreaWidget.
610 system_tray_delegate_.reset(delegate()->CreateSystemTrayDelegate());
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100611 if (!system_tray_delegate_)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000612 system_tray_delegate_.reset(SystemTrayDelegate::CreateDummyDelegate());
613
Ben Murdocheb525c52013-07-10 11:40:50 +0100614 internal::RootWindowController* root_window_controller =
615 new internal::RootWindowController(root_window);
616 InitRootWindowController(root_window_controller,
617 delegate_->IsFirstRunAfterBoot());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000618
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100619 locale_notification_controller_.reset(
620 new internal::LocaleNotificationController);
621
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000622 // Initialize system_tray_delegate_ after StatusAreaWidget is created.
623 system_tray_delegate_->Initialize();
624
625 display_controller_->InitSecondaryDisplays();
626
627 // Force Layout
628 root_window_controller->root_window_layout()->OnWindowResized();
629
630 // It needs to be created after OnWindowResized has been called, otherwise the
631 // widget will not paint when restoring after a browser crash. Also it needs
632 // to be created after InitSecondaryDisplays() to initialize the wallpapers in
633 // the correct size.
634 user_wallpaper_delegate_->InitializeWallpaper();
635
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000636 if (initially_hide_cursor_)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000637 cursor_manager_.HideCursor();
638 cursor_manager_.SetCursor(ui::kCursorPointer);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000639
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000640 if (!cursor_manager_.IsCursorVisible()) {
641 // Cursor might have been hidden by something other than chrome.
642 // Let the first mouse event show the cursor.
643 env_filter_->set_cursor_hidden_by_filter(true);
644 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000645}
646
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100647void Shell::ShowContextMenu(const gfx::Point& location_in_screen,
648 ui::MenuSourceType source_type) {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100649 // No context menus if there is no session with an active user.
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100650 if (!session_state_delegate_->NumberOfLoggedInUsers())
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000651 return;
652 // No context menus when screen is locked.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100653 if (session_state_delegate_->IsScreenLocked())
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000654 return;
655
656 aura::RootWindow* root =
657 wm::GetRootWindowMatching(gfx::Rect(location_in_screen, gfx::Size()));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000658 // TODO(oshima): The root and root window controller shouldn't be
659 // NULL even for the out-of-bounds |location_in_screen| (It should
660 // return the primary root). Investigate why/how this is
661 // happening. crbug.com/165214.
662 internal::RootWindowController* rwc = GetRootWindowController(root);
663 CHECK(rwc) << "root=" << root
664 << ", location:" << location_in_screen.ToString();
665 if (rwc)
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100666 rwc->ShowContextMenu(location_in_screen, source_type);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000667}
668
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000669void Shell::ToggleAppList(aura::Window* window) {
670 // If the context window is not given, show it on the active root window.
671 if (!window)
672 window = GetActiveRootWindow();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100673 if (!app_list_controller_)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000674 app_list_controller_.reset(new internal::AppListController);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000675 app_list_controller_->SetVisible(!app_list_controller_->IsVisible(), window);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000676}
677
678bool Shell::GetAppListTargetVisibility() const {
679 return app_list_controller_.get() &&
680 app_list_controller_->GetTargetVisibility();
681}
682
683aura::Window* Shell::GetAppListWindow() {
684 return app_list_controller_.get() ? app_list_controller_->GetWindow() : NULL;
685}
686
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000687bool Shell::IsSystemModalWindowOpen() const {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000688 if (simulate_modal_window_open_for_testing_)
689 return true;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000690 const std::vector<aura::Window*> containers = GetContainersFromAllRootWindows(
691 internal::kShellWindowId_SystemModalContainer, NULL);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000692 for (std::vector<aura::Window*>::const_iterator cit = containers.begin();
693 cit != containers.end(); ++cit) {
694 for (aura::Window::Windows::const_iterator wit = (*cit)->children().begin();
695 wit != (*cit)->children().end(); ++wit) {
696 if ((*wit)->GetProperty(aura::client::kModalKey) ==
697 ui::MODAL_TYPE_SYSTEM && (*wit)->TargetVisibility()) {
698 return true;
699 }
700 }
701 }
702 return false;
703}
704
705views::NonClientFrameView* Shell::CreateDefaultNonClientFrameView(
706 views::Widget* widget) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000707 // Use translucent-style window frames for dialogs.
708 CustomFrameViewAsh* frame_view = new CustomFrameViewAsh;
709 frame_view->Init(widget);
710 return frame_view;
711}
712
713void Shell::RotateFocus(Direction direction) {
714 focus_cycler_->RotateFocus(
715 direction == FORWARD ? internal::FocusCycler::FORWARD :
716 internal::FocusCycler::BACKWARD);
717}
718
719void Shell::SetDisplayWorkAreaInsets(Window* contains,
720 const gfx::Insets& insets) {
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100721 if (!display_controller_->UpdateWorkAreaOfDisplayNearestWindow(
722 contains, insets)) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000723 return;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100724 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000725 FOR_EACH_OBSERVER(ShellObserver, observers_,
726 OnDisplayWorkAreaInsetsChanged());
727}
728
729void Shell::OnLoginStateChanged(user::LoginStatus status) {
730 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLoginStateChanged(status));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000731}
732
733void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status) {
734 RootWindowControllerList controllers = GetAllRootWindowControllers();
735 for (RootWindowControllerList::iterator iter = controllers.begin();
736 iter != controllers.end(); ++iter)
737 (*iter)->UpdateAfterLoginStatusChange(status);
738}
739
740void Shell::OnAppTerminating() {
741 FOR_EACH_OBSERVER(ShellObserver, observers_, OnAppTerminating());
742}
743
744void Shell::OnLockStateChanged(bool locked) {
745 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLockStateChanged(locked));
746}
747
748void Shell::CreateLauncher() {
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100749 RootWindowControllerList controllers = GetAllRootWindowControllers();
750 for (RootWindowControllerList::iterator iter = controllers.begin();
751 iter != controllers.end(); ++iter)
752 (*iter)->shelf()->CreateLauncher();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000753}
754
755void Shell::ShowLauncher() {
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100756 RootWindowControllerList controllers = GetAllRootWindowControllers();
757 for (RootWindowControllerList::iterator iter = controllers.begin();
758 iter != controllers.end(); ++iter)
759 (*iter)->ShowLauncher();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000760}
761
762void Shell::AddShellObserver(ShellObserver* observer) {
763 observers_.AddObserver(observer);
764}
765
766void Shell::RemoveShellObserver(ShellObserver* observer) {
767 observers_.RemoveObserver(observer);
768}
769
770void Shell::UpdateShelfVisibility() {
771 RootWindowControllerList controllers = GetAllRootWindowControllers();
772 for (RootWindowControllerList::iterator iter = controllers.begin();
773 iter != controllers.end(); ++iter)
774 if ((*iter)->shelf())
775 (*iter)->UpdateShelfVisibility();
776}
777
778void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior,
779 aura::RootWindow* root_window) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000780 ash::internal::ShelfLayoutManager::ForLauncher(root_window)->
781 SetAutoHideBehavior(behavior);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000782}
783
784ShelfAutoHideBehavior Shell::GetShelfAutoHideBehavior(
785 aura::RootWindow* root_window) const {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000786 return ash::internal::ShelfLayoutManager::ForLauncher(root_window)->
787 auto_hide_behavior();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000788}
789
790void Shell::SetShelfAlignment(ShelfAlignment alignment,
791 aura::RootWindow* root_window) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000792 if (ash::internal::ShelfLayoutManager::ForLauncher(root_window)->
793 SetAlignment(alignment)) {
794 FOR_EACH_OBSERVER(
795 ShellObserver, observers_, OnShelfAlignmentChanged(root_window));
796 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000797}
798
799ShelfAlignment Shell::GetShelfAlignment(aura::RootWindow* root_window) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000800 return GetRootWindowController(root_window)->
801 GetShelfLayoutManager()->GetAlignment();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000802}
803
804void Shell::SetDimming(bool should_dim) {
805 RootWindowControllerList controllers = GetAllRootWindowControllers();
806 for (RootWindowControllerList::iterator iter = controllers.begin();
807 iter != controllers.end(); ++iter)
808 (*iter)->screen_dimmer()->SetDimming(should_dim);
809}
810
811void Shell::CreateModalBackground(aura::Window* window) {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100812 if (!modality_filter_) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000813 modality_filter_.reset(new internal::SystemModalContainerEventFilter(this));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000814 AddPreTargetHandler(modality_filter_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000815 }
816 RootWindowControllerList controllers = GetAllRootWindowControllers();
817 for (RootWindowControllerList::iterator iter = controllers.begin();
818 iter != controllers.end(); ++iter)
819 (*iter)->GetSystemModalLayoutManager(window)->CreateModalBackground();
820}
821
822void Shell::OnModalWindowRemoved(aura::Window* removed) {
823 RootWindowControllerList controllers = GetAllRootWindowControllers();
824 bool activated = false;
825 for (RootWindowControllerList::iterator iter = controllers.begin();
826 iter != controllers.end() && !activated; ++iter) {
827 activated = (*iter)->GetSystemModalLayoutManager(removed)->
828 ActivateNextModalWindow();
829 }
830 if (!activated) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000831 RemovePreTargetHandler(modality_filter_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000832 modality_filter_.reset();
833 for (RootWindowControllerList::iterator iter = controllers.begin();
834 iter != controllers.end(); ++iter)
835 (*iter)->GetSystemModalLayoutManager(removed)->DestroyModalBackground();
836 }
837}
838
839WebNotificationTray* Shell::GetWebNotificationTray() {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000840 return GetPrimaryRootWindowController()->shelf()->
841 status_area_widget()->web_notification_tray();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000842}
843
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000844bool Shell::HasPrimaryStatusArea() {
845 ShelfWidget* shelf = GetPrimaryRootWindowController()->shelf();
846 return shelf && shelf->status_area_widget();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000847}
848
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000849SystemTray* Shell::GetPrimarySystemTray() {
850 return GetPrimaryRootWindowController()->GetSystemTray();
851}
852
853LauncherDelegate* Shell::GetLauncherDelegate() {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100854 if (!launcher_delegate_) {
Ben Murdocheb525c52013-07-10 11:40:50 +0100855 launcher_model_.reset(new LauncherModel);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000856 launcher_delegate_.reset(
857 delegate_->CreateLauncherDelegate(launcher_model_.get()));
858 }
859 return launcher_delegate_.get();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000860}
861
Ben Murdocheb525c52013-07-10 11:40:50 +0100862void Shell::SetTouchHudProjectionEnabled(bool enabled) {
863 if (is_touch_hud_projection_enabled_ == enabled)
864 return;
865
Ben Murdocheb525c52013-07-10 11:40:50 +0100866 is_touch_hud_projection_enabled_ = enabled;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100867 FOR_EACH_OBSERVER(ShellObserver, observers_,
868 OnTouchHudProjectionToggled(enabled));
Ben Murdocheb525c52013-07-10 11:40:50 +0100869}
870
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000871void Shell::InitRootWindowForSecondaryDisplay(aura::RootWindow* root) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000872 internal::RootWindowController* controller =
873 new internal::RootWindowController(root);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000874 // Pass false for the |is_first_run_after_boot| parameter so we'll show a
875 // black background on this display instead of trying to mimic the boot splash
876 // screen.
Ben Murdocheb525c52013-07-10 11:40:50 +0100877 InitRootWindowController(controller, false);
878
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000879 controller->root_window_layout()->OnWindowResized();
880 desktop_background_controller_->OnRootWindowAdded(root);
881 high_contrast_controller_->OnRootWindowAdded(root);
882 root->ShowRootWindow();
883 // Activate new root for testing.
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100884 // TODO(oshima): remove this.
885 target_root_window_ = root;
Ben Murdocheb525c52013-07-10 11:40:50 +0100886
887 // Create a launcher if a user is already logged.
888 if (Shell::GetInstance()->session_state_delegate()->NumberOfLoggedInUsers())
889 controller->shelf()->CreateLauncher();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000890}
891
892void Shell::DoInitialWorkspaceAnimation() {
893 return GetPrimaryRootWindowController()->workspace_controller()->
894 DoInitialAnimation();
895}
896
897void Shell::InitRootWindowController(
Ben Murdocheb525c52013-07-10 11:40:50 +0100898 internal::RootWindowController* controller,
899 bool first_run_after_boot) {
900
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000901 aura::RootWindow* root_window = controller->root_window();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000902 DCHECK(activation_client_);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000903 DCHECK(visibility_controller_.get());
904 DCHECK(drag_drop_controller_.get());
905 DCHECK(capture_controller_.get());
906 DCHECK(window_cycle_controller_.get());
907
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000908 aura::client::SetFocusClient(root_window, focus_client_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000909 input_method_filter_->SetInputMethodPropertyInRootWindow(root_window);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000910 aura::client::SetActivationClient(root_window, activation_client_);
911 if (views::corewm::UseFocusController()) {
912 views::corewm::FocusController* controller =
913 static_cast<views::corewm::FocusController*>(activation_client_);
914 root_window->AddPreTargetHandler(controller);
915 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000916 aura::client::SetVisibilityClient(root_window, visibility_controller_.get());
917 aura::client::SetDragDropClient(root_window, drag_drop_controller_.get());
918 aura::client::SetCaptureClient(root_window, capture_controller_.get());
919 aura::client::SetScreenPositionClient(root_window,
920 screen_position_controller_.get());
921 aura::client::SetCursorClient(root_window, &cursor_manager_);
922 aura::client::SetTooltipClient(root_window, tooltip_controller_.get());
923 aura::client::SetEventClient(root_window, event_client_.get());
924
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100925 if (nested_dispatcher_controller_) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000926 aura::client::SetDispatcherClient(root_window,
927 nested_dispatcher_controller_.get());
928 }
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100929 if (user_action_client_)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000930 aura::client::SetUserActionClient(root_window, user_action_client_.get());
931
Ben Murdocheb525c52013-07-10 11:40:50 +0100932 controller->Init(first_run_after_boot);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000933
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100934 mru_window_tracker_->OnRootWindowAdded(root_window);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000935}
936
937////////////////////////////////////////////////////////////////////////////////
938// Shell, private:
939
940bool Shell::CanWindowReceiveEvents(aura::Window* window) {
941 RootWindowControllerList controllers = GetAllRootWindowControllers();
942 for (RootWindowControllerList::iterator iter = controllers.begin();
943 iter != controllers.end(); ++iter) {
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100944 internal::SystemModalContainerLayoutManager* layout_manager =
945 (*iter)->GetSystemModalLayoutManager(window);
946 if (layout_manager && layout_manager->CanWindowReceiveEvents(window))
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000947 return true;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000948 }
949 return false;
950}
951
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000952////////////////////////////////////////////////////////////////////////////////
953// Shell, ui::EventTarget overrides:
954
955bool Shell::CanAcceptEvent(const ui::Event& event) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000956 return true;
957}
958
959ui::EventTarget* Shell::GetParentTarget() {
960 return NULL;
961}
962
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000963void Shell::OnEvent(ui::Event* event) {
964}
965
966////////////////////////////////////////////////////////////////////////////////
967// Shell, aura::client::ActivationChangeObserver implementation:
968
969void Shell::OnWindowActivated(aura::Window* gained_active,
970 aura::Window* lost_active) {
971 if (gained_active)
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100972 target_root_window_ = gained_active->GetRootWindow();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000973}
974
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000975} // namespace ash