blob: 1610ae3579cd2a545e336f812baf6691253ff58b [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)90dce4d2013-05-29 14:40:03 +010019#include "ash/display/mirror_window_controller.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000020#include "ash/display/mouse_cursor_event_filter.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000021#include "ash/display/screen_position_controller.h"
22#include "ash/drag_drop/drag_drop_controller.h"
23#include "ash/focus_cycler.h"
24#include "ash/high_contrast/high_contrast_controller.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000025#include "ash/host/root_window_host_factory.h"
26#include "ash/launcher/launcher_delegate.h"
27#include "ash/launcher/launcher_model.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000028#include "ash/magnifier/magnification_controller.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000029#include "ash/magnifier/partial_magnification_controller.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000030#include "ash/root_window_controller.h"
31#include "ash/screen_ash.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010032#include "ash/session_state_delegate.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000033#include "ash/shelf/shelf_layout_manager.h"
34#include "ash/shelf/shelf_widget.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000035#include "ash/shell_delegate.h"
36#include "ash/shell_factory.h"
37#include "ash/shell_window_ids.h"
38#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)58218062012-11-14 11:43:16 +000054#include "ash/wm/overlay_event_filter.h"
55#include "ash/wm/power_button_controller.h"
56#include "ash/wm/property_util.h"
57#include "ash/wm/resize_shadow_controller.h"
58#include "ash/wm/root_window_layout_manager.h"
59#include "ash/wm/screen_dimmer.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000060#include "ash/wm/session_state_controller_impl.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000061#include "ash/wm/system_gesture_event_filter.h"
62#include "ash/wm/system_modal_container_event_filter.h"
63#include "ash/wm/system_modal_container_layout_manager.h"
64#include "ash/wm/user_activity_detector.h"
65#include "ash/wm/video_detector.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000066#include "ash/wm/window_animations.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000067#include "ash/wm/window_cycle_controller.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000068#include "ash/wm/window_properties.h"
69#include "ash/wm/window_util.h"
70#include "ash/wm/workspace_controller.h"
71#include "base/bind.h"
72#include "base/command_line.h"
73#include "base/debug/leak_annotations.h"
74#include "ui/aura/client/aura_constants.h"
75#include "ui/aura/client/user_action_client.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000076#include "ui/aura/env.h"
77#include "ui/aura/focus_manager.h"
78#include "ui/aura/layout_manager.h"
79#include "ui/aura/root_window.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000080#include "ui/aura/window.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000081#include "ui/base/ui_base_switches.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000082#include "ui/compositor/layer.h"
83#include "ui/compositor/layer_animator.h"
84#include "ui/gfx/display.h"
85#include "ui/gfx/image/image_skia.h"
86#include "ui/gfx/screen.h"
87#include "ui/gfx/size.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010088#include "ui/keyboard/keyboard.h"
89#include "ui/keyboard/keyboard_util.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000090#include "ui/message_center/message_center.h"
91#include "ui/views/corewm/compound_event_filter.h"
92#include "ui/views/corewm/corewm_switches.h"
93#include "ui/views/corewm/focus_controller.h"
94#include "ui/views/corewm/input_method_event_filter.h"
95#include "ui/views/corewm/shadow_controller.h"
96#include "ui/views/corewm/tooltip_controller.h"
97#include "ui/views/corewm/visibility_controller.h"
98#include "ui/views/corewm/window_modality_controller.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000099#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
103#if !defined(OS_MACOSX)
104#include "ash/accelerators/accelerator_controller.h"
105#include "ash/accelerators/accelerator_filter.h"
106#include "ash/accelerators/nested_dispatcher_controller.h"
107#endif
108
Ben Murdocheb525c52013-07-10 11:40:50 +0100109#if defined(OS_CHROMEOS)
110#if defined(USE_X11)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000111#include "ash/ash_constants.h"
112#include "ash/display/display_change_observer_x11.h"
113#include "ash/display/display_error_dialog.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000114#include "ash/display/output_configurator_animation.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000115#include "base/chromeos/chromeos_version.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100116#include "base/message_loop/message_pump_aurax11.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000117#include "chromeos/display/output_configurator.h"
118#include "content/public/browser/gpu_data_manager.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000119#include "content/public/common/content_switches.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100120#include "gpu/config/gpu_feature_type.h"
Ben Murdocheb525c52013-07-10 11:40:50 +0100121#endif // defined(USE_X11)
122#include "ash/system/chromeos/power/power_status.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000123#endif // defined(OS_CHROMEOS)
124
125namespace ash {
126
127namespace {
128
129using aura::Window;
130using views::Widget;
131
132// This dummy class is used for shell unit tests. We dont have chrome delegate
133// in these tests.
134class DummyUserWallpaperDelegate : public UserWallpaperDelegate {
135 public:
136 DummyUserWallpaperDelegate() {}
137
138 virtual ~DummyUserWallpaperDelegate() {}
139
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000140 virtual int GetAnimationType() OVERRIDE {
141 return views::corewm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000142 }
143
144 virtual bool ShouldShowInitialAnimation() OVERRIDE {
145 return false;
146 }
147
148 virtual void UpdateWallpaper() OVERRIDE {
149 }
150
151 virtual void InitializeWallpaper() OVERRIDE {
152 ash::Shell::GetInstance()->desktop_background_controller()->
153 CreateEmptyWallpaper();
154 }
155
156 virtual void OpenSetWallpaperPage() OVERRIDE {
157 }
158
159 virtual bool CanOpenSetWallpaperPage() OVERRIDE {
160 return false;
161 }
162
163 virtual void OnWallpaperAnimationFinished() OVERRIDE {
164 }
165
166 virtual void OnWallpaperBootAnimationFinished() OVERRIDE {
167 }
168
169 private:
170 DISALLOW_COPY_AND_ASSIGN(DummyUserWallpaperDelegate);
171};
172
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000173// A Corewm VisibilityController subclass that calls the Ash animation routine
174// so we can pick up our extended animations. See ash/wm/window_animations.h.
175class AshVisibilityController : public views::corewm::VisibilityController {
176 public:
177 AshVisibilityController() {}
178 virtual ~AshVisibilityController() {}
179
180 private:
181 // Overridden from views::corewm::VisibilityController:
182 virtual bool CallAnimateOnChildWindowVisibilityChanged(
183 aura::Window* window,
184 bool visible) OVERRIDE {
185 return AnimateOnChildWindowVisibilityChanged(window, visible);
186 }
187
188 DISALLOW_COPY_AND_ASSIGN(AshVisibilityController);
189};
190
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000191} // namespace
192
193// static
194Shell* Shell::instance_ = NULL;
195// static
196bool Shell::initially_hide_cursor_ = false;
197
198////////////////////////////////////////////////////////////////////////////////
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000199// Shell, public:
200
201Shell::Shell(ShellDelegate* delegate)
202 : screen_(new ScreenAsh),
203 active_root_window_(NULL),
204 delegate_(delegate),
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000205 activation_client_(NULL),
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100206#if defined(OS_CHROMEOS) && defined(USE_X11)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000207 output_configurator_(new chromeos::OutputConfigurator()),
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000208#endif // defined(OS_CHROMEOS)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000209 native_cursor_manager_(new AshNativeCursorManager),
210 cursor_manager_(scoped_ptr<views::corewm::NativeCursorManager>(
211 native_cursor_manager_)),
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000212 browser_context_(NULL),
Ben Murdocheb525c52013-07-10 11:40:50 +0100213 simulate_modal_window_open_for_testing_(false),
214 is_touch_hud_projection_enabled_(false) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000215 DCHECK(delegate_.get());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000216 display_manager_.reset(new internal::DisplayManager);
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100217 mirror_window_controller_.reset(new internal::MirrorWindowController);
218
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000219 ANNOTATE_LEAKING_OBJECT_PTR(screen_); // see crbug.com/156466
220 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_ALTERNATE, screen_);
221 if (!gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE))
222 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000223 display_controller_.reset(new DisplayController);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100224#if defined(OS_CHROMEOS) && defined(USE_X11)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000225 bool is_panel_fitting_disabled =
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100226 content::GpuDataManager::GetInstance()->IsFeatureBlacklisted(
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100227 gpu::GPU_FEATURE_TYPE_PANEL_FITTING) ||
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000228 CommandLine::ForCurrentProcess()->HasSwitch(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000229 ::switches::kDisablePanelFitting);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000230
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000231 output_configurator_->Init(
232 !is_panel_fitting_disabled,
233 delegate_->IsFirstRunAfterBoot() ? kChromeOsBootColor : 0);
234
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000235 base::MessagePumpAuraX11::Current()->AddDispatcherForRootWindow(
236 output_configurator());
237#endif // defined(OS_CHROMEOS)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000238 AddPreTargetHandler(this);
Ben Murdocheb525c52013-07-10 11:40:50 +0100239
240#if defined(OS_CHROMEOS)
241 internal::PowerStatus::Initialize();
242#endif
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000243}
244
245Shell::~Shell() {
246 views::FocusManagerFactory::Install(NULL);
247
248 // Remove the focus from any window. This will prevent overhead and side
249 // effects (e.g. crashes) from changing focus during shutdown.
250 // See bug crbug.com/134502.
251 if (active_root_window_)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000252 aura::client::GetFocusClient(active_root_window_)->FocusWindow(NULL);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000253
254 // Please keep in same order as in Init() because it's easy to miss one.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000255 RemovePreTargetHandler(user_activity_detector_.get());
256 RemovePreTargetHandler(event_rewriter_filter_.get());
257 RemovePreTargetHandler(overlay_filter_.get());
258 RemovePreTargetHandler(input_method_filter_.get());
259 RemovePreTargetHandler(window_modality_controller_.get());
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100260 if (mouse_cursor_filter_)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000261 RemovePreTargetHandler(mouse_cursor_filter_.get());
262 RemovePreTargetHandler(system_gesture_filter_.get());
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100263 RemovePreTargetHandler(event_transformation_handler_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000264#if !defined(OS_MACOSX)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000265 RemovePreTargetHandler(accelerator_filter_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000266#endif
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000267
268 // TooltipController is deleted with the Shell so removing its references.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000269 RemovePreTargetHandler(tooltip_controller_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000270
271 // AppList needs to be released before shelf layout manager, which is
272 // destroyed with launcher container in the loop below. However, app list
273 // container is now on top of launcher container and released after it.
274 // TODO(xiyuan): Move it back when app list container is no longer needed.
275 app_list_controller_.reset();
276
277 // Destroy SystemTrayDelegate before destroying the status area(s).
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100278 system_tray_delegate_->Shutdown();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000279 system_tray_delegate_.reset();
280
281 // Destroy all child windows including widgets.
282 display_controller_->CloseChildWindows();
283
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000284 // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems
285 // needs to remove observers from it.
286 system_tray_notifier_.reset();
287
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000288 // These need a valid Shell instance to clean up properly, so explicitly
289 // delete them before invalidating the instance.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000290 // Alphabetical. TODO(oshima): sort.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000291 drag_drop_controller_.reset();
292 magnification_controller_.reset();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000293 partial_magnification_controller_.reset();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000294 resize_shadow_controller_.reset();
295 shadow_controller_.reset();
296 tooltip_controller_.reset();
297 event_client_.reset();
298 window_cycle_controller_.reset();
299 capture_controller_.reset();
300 nested_dispatcher_controller_.reset();
301 user_action_client_.reset();
302 visibility_controller_.reset();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000303 launcher_delegate_.reset();
304 launcher_model_.reset();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100305 video_detector_.reset();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000306
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000307 power_button_controller_.reset();
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100308 lock_state_controller_.reset();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000309
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100310 mirror_window_controller_.reset();
311
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000312 // This also deletes all RootWindows. Note that we invoke Shutdown() on
313 // DisplayController before resetting |display_controller_|, since destruction
314 // of its owned RootWindowControllers relies on the value.
315 display_controller_->Shutdown();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000316 display_controller_.reset();
317 screen_position_controller_.reset();
318
319 // Delete the activation controller after other controllers and launcher
320 // because they might have registered ActivationChangeObserver.
321 activation_controller_.reset();
322
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100323#if defined(OS_CHROMEOS) && defined(USE_X11)
324 if (display_change_observer_)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000325 output_configurator_->RemoveObserver(display_change_observer_.get());
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100326 if (output_configurator_animation_)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000327 output_configurator_->RemoveObserver(output_configurator_animation_.get());
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100328 if (display_error_observer_)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000329 output_configurator_->RemoveObserver(display_error_observer_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000330 base::MessagePumpAuraX11::Current()->RemoveDispatcherForRootWindow(
331 output_configurator());
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100332 display_change_observer_.reset();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000333#endif // defined(OS_CHROMEOS)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000334
Ben Murdocheb525c52013-07-10 11:40:50 +0100335#if defined(OS_CHROMEOS)
336 internal::PowerStatus::Shutdown();
337#endif
338
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000339 DCHECK(instance_ == this);
340 instance_ = NULL;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000341}
342
343// static
344Shell* Shell::CreateInstance(ShellDelegate* delegate) {
345 CHECK(!instance_);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000346 instance_ = new Shell(delegate);
347 instance_->Init();
348 return instance_;
349}
350
351// static
352Shell* Shell::GetInstance() {
353 DCHECK(instance_);
354 return instance_;
355}
356
357// static
358bool Shell::HasInstance() {
359 return !!instance_;
360}
361
362// static
363void Shell::DeleteInstance() {
364 delete instance_;
365 instance_ = NULL;
366}
367
368// static
369internal::RootWindowController* Shell::GetPrimaryRootWindowController() {
370 return GetRootWindowController(GetPrimaryRootWindow());
371}
372
373// static
374Shell::RootWindowControllerList Shell::GetAllRootWindowControllers() {
375 return Shell::GetInstance()->display_controller()->
376 GetAllRootWindowControllers();
377}
378
379// static
380aura::RootWindow* Shell::GetPrimaryRootWindow() {
381 return GetInstance()->display_controller()->GetPrimaryRootWindow();
382}
383
384// static
385aura::RootWindow* Shell::GetActiveRootWindow() {
386 return GetInstance()->active_root_window_;
387}
388
389// static
390gfx::Screen* Shell::GetScreen() {
391 return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE);
392}
393
394// static
395Shell::RootWindowList Shell::GetAllRootWindows() {
396 return Shell::GetInstance()->display_controller()->
397 GetAllRootWindows();
398}
399
400// static
401aura::Window* Shell::GetContainer(aura::RootWindow* root_window,
402 int container_id) {
403 return root_window->GetChildById(container_id);
404}
405
406// static
407const aura::Window* Shell::GetContainer(const aura::RootWindow* root_window,
408 int container_id) {
409 return root_window->GetChildById(container_id);
410}
411
412// static
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000413std::vector<aura::Window*> Shell::GetContainersFromAllRootWindows(
414 int container_id,
415 aura::RootWindow* priority_root) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000416 std::vector<aura::Window*> containers;
417 RootWindowList root_windows = GetAllRootWindows();
418 for (RootWindowList::const_iterator it = root_windows.begin();
419 it != root_windows.end(); ++it) {
420 aura::Window* container = (*it)->GetChildById(container_id);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000421 if (container) {
422 if (priority_root && priority_root->Contains(container))
423 containers.insert(containers.begin(), container);
424 else
425 containers.push_back(container);
426 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000427 }
428 return containers;
429}
430
431// static
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100432bool Shell::IsForcedMaximizeMode() {
433 CommandLine* command_line = CommandLine::ForCurrentProcess();
434 return command_line->HasSwitch(switches::kForcedMaximizeMode);
435}
436
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000437void Shell::Init() {
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100438 CommandLine* command_line = CommandLine::ForCurrentProcess();
439
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000440 delegate_->PreInit();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100441#if defined(OS_CHROMEOS) && defined(USE_X11)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000442 output_configurator_animation_.reset(
443 new internal::OutputConfiguratorAnimation());
444 output_configurator_->AddObserver(output_configurator_animation_.get());
445 if (base::chromeos::IsRunningOnChromeOS()) {
446 display_change_observer_.reset(new internal::DisplayChangeObserverX11);
447 // Register |display_change_observer_| first so that the rest of
448 // observer gets invoked after the root windows are configured.
449 output_configurator_->AddObserver(display_change_observer_.get());
450 display_error_observer_.reset(new internal::DisplayErrorObserver());
451 output_configurator_->AddObserver(display_error_observer_.get());
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100452 output_configurator_->set_state_controller(display_change_observer_.get());
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100453 if (!command_line->HasSwitch(ash::switches::kAshDisableSoftwareMirroring))
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100454 output_configurator_->set_mirroring_controller(display_manager_.get());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000455 output_configurator_->Start();
456 display_change_observer_->OnDisplayModeChanged();
457 }
458#endif
459
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000460 // Install the custom factory first so that views::FocusManagers for Tray,
461 // Launcher, and WallPaper could be created by the factory.
462 views::FocusManagerFactory::Install(new AshFocusManagerFactory);
463
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000464 env_filter_.reset(new views::corewm::CompoundEventFilter);
465 AddPreTargetHandler(env_filter_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000466
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000467 // Env creates the compositor. Historically it seems to have been implicitly
468 // initialized first by the ActivationController, but now that FocusController
469 // no longer does this we need to do it explicitly.
470 aura::Env::GetInstance();
471 if (views::corewm::UseFocusController()) {
472 views::corewm::FocusController* focus_controller =
473 new views::corewm::FocusController(new wm::AshFocusRules);
474 focus_client_.reset(focus_controller);
475 activation_client_ = focus_controller;
476 activation_client_->AddObserver(this);
477 } else {
478 focus_client_.reset(new aura::FocusManager);
479 activation_controller_.reset(
480 new internal::ActivationController(
481 focus_client_.get(),
482 new internal::AshActivationController));
483 activation_client_ = activation_controller_.get();
484 AddPreTargetHandler(activation_controller_.get());
485 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000486
487 focus_cycler_.reset(new internal::FocusCycler());
488
489 screen_position_controller_.reset(new internal::ScreenPositionController);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000490 root_window_host_factory_.reset(delegate_->CreateRootWindowHostFactory());
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100491
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000492 display_controller_->Start();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000493 display_controller_->InitPrimaryDisplay();
494 aura::RootWindow* root_window = display_controller_->GetPrimaryRootWindow();
495 active_root_window_ = root_window;
496
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000497 cursor_manager_.SetDisplay(DisplayController::GetPrimaryDisplay());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000498
499#if !defined(OS_MACOSX)
500 nested_dispatcher_controller_.reset(new NestedDispatcherController);
501 accelerator_controller_.reset(new AcceleratorController);
502#endif
503
504 // The order in which event filters are added is significant.
505 user_activity_detector_.reset(new UserActivityDetector);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000506 AddPreTargetHandler(user_activity_detector_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000507
508 event_rewriter_filter_.reset(new internal::EventRewriterEventFilter);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000509 AddPreTargetHandler(event_rewriter_filter_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000510
511 overlay_filter_.reset(new internal::OverlayEventFilter);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000512 AddPreTargetHandler(overlay_filter_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000513 AddShellObserver(overlay_filter_.get());
514
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000515 input_method_filter_.reset(new views::corewm::InputMethodEventFilter(
516 root_window->GetAcceleratedWidget()));
517 AddPreTargetHandler(input_method_filter_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000518
519#if !defined(OS_MACOSX)
520 accelerator_filter_.reset(new internal::AcceleratorFilter);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000521 AddPreTargetHandler(accelerator_filter_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000522#endif
523
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000524 event_transformation_handler_.reset(new internal::EventTransformationHandler);
525 AddPreTargetHandler(event_transformation_handler_.get());
526
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000527 system_gesture_filter_.reset(new internal::SystemGestureEventFilter);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000528 AddPreTargetHandler(system_gesture_filter_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000529
530 capture_controller_.reset(new internal::CaptureController);
531
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100532 // The keyboard system must be initialized before the RootWindowController is
533 // created.
534 if (keyboard::IsKeyboardEnabled())
535 keyboard::InitializeKeyboard();
536
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000537 if (command_line->HasSwitch(ash::switches::kAshDisableNewLockAnimations))
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100538 lock_state_controller_.reset(new SessionStateControllerImpl);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000539 else
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100540 lock_state_controller_.reset(new LockStateControllerImpl2);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000541 power_button_controller_.reset(new PowerButtonController(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100542 lock_state_controller_.get()));
543 AddShellObserver(lock_state_controller_.get());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000544
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100545 drag_drop_controller_.reset(new internal::DragDropController);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000546 mouse_cursor_filter_.reset(new internal::MouseCursorEventFilter());
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100547 PrependPreTargetHandler(mouse_cursor_filter_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000548
549 // Create Controllers that may need root window.
550 // TODO(oshima): Move as many controllers before creating
551 // RootWindowController as possible.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000552 visibility_controller_.reset(new AshVisibilityController);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000553 user_action_client_.reset(delegate_->CreateUserActionClient());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000554 window_modality_controller_.reset(
555 new views::corewm::WindowModalityController);
556 AddPreTargetHandler(window_modality_controller_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000557
558 magnification_controller_.reset(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000559 MagnificationController::CreateInstance());
560
561 partial_magnification_controller_.reset(
562 new PartialMagnificationController());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000563
564 high_contrast_controller_.reset(new HighContrastController);
565 video_detector_.reset(new VideoDetector);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000566 window_cycle_controller_.reset(new WindowCycleController(activation_client_));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000567
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000568 tooltip_controller_.reset(new views::corewm::TooltipController(
569 gfx::SCREEN_TYPE_ALTERNATE));
570 AddPreTargetHandler(tooltip_controller_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000571
572 event_client_.reset(new internal::EventClientImpl);
573
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000574 // This controller needs to be set before SetupManagedWindowMode.
575 desktop_background_controller_.reset(new DesktopBackgroundController());
576 user_wallpaper_delegate_.reset(delegate_->CreateUserWallpaperDelegate());
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100577 if (!user_wallpaper_delegate_)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000578 user_wallpaper_delegate_.reset(new DummyUserWallpaperDelegate());
579
580 // StatusAreaWidget uses Shell's CapsLockDelegate.
581 caps_lock_delegate_.reset(delegate_->CreateCapsLockDelegate());
582
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100583 session_state_delegate_.reset(delegate_->CreateSessionStateDelegate());
584
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000585 if (!command_line->HasSwitch(views::corewm::switches::kNoDropShadows)) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000586 resize_shadow_controller_.reset(new internal::ResizeShadowController());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000587 shadow_controller_.reset(
588 new views::corewm::ShadowController(activation_client_));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000589 }
590
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000591 // Create system_tray_notifier_ before the delegate.
592 system_tray_notifier_.reset(new ash::SystemTrayNotifier());
593
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000594 // Initialize system_tray_delegate_ before initializing StatusAreaWidget.
595 system_tray_delegate_.reset(delegate()->CreateSystemTrayDelegate());
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100596 if (!system_tray_delegate_)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000597 system_tray_delegate_.reset(SystemTrayDelegate::CreateDummyDelegate());
598
Ben Murdocheb525c52013-07-10 11:40:50 +0100599 internal::RootWindowController* root_window_controller =
600 new internal::RootWindowController(root_window);
601 InitRootWindowController(root_window_controller,
602 delegate_->IsFirstRunAfterBoot());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000603
604 // Initialize system_tray_delegate_ after StatusAreaWidget is created.
605 system_tray_delegate_->Initialize();
606
607 display_controller_->InitSecondaryDisplays();
608
609 // Force Layout
610 root_window_controller->root_window_layout()->OnWindowResized();
611
612 // It needs to be created after OnWindowResized has been called, otherwise the
613 // widget will not paint when restoring after a browser crash. Also it needs
614 // to be created after InitSecondaryDisplays() to initialize the wallpapers in
615 // the correct size.
616 user_wallpaper_delegate_->InitializeWallpaper();
617
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000618 if (initially_hide_cursor_)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000619 cursor_manager_.HideCursor();
620 cursor_manager_.SetCursor(ui::kCursorPointer);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000621
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000622 if (!cursor_manager_.IsCursorVisible()) {
623 // Cursor might have been hidden by something other than chrome.
624 // Let the first mouse event show the cursor.
625 env_filter_->set_cursor_hidden_by_filter(true);
626 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000627}
628
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100629void Shell::ShowContextMenu(const gfx::Point& location_in_screen,
630 ui::MenuSourceType source_type) {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100631 // No context menus if there is no session with an active user.
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100632 if (!session_state_delegate_->NumberOfLoggedInUsers())
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000633 return;
634 // No context menus when screen is locked.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100635 if (session_state_delegate_->IsScreenLocked())
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000636 return;
637
638 aura::RootWindow* root =
639 wm::GetRootWindowMatching(gfx::Rect(location_in_screen, gfx::Size()));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000640 // TODO(oshima): The root and root window controller shouldn't be
641 // NULL even for the out-of-bounds |location_in_screen| (It should
642 // return the primary root). Investigate why/how this is
643 // happening. crbug.com/165214.
644 internal::RootWindowController* rwc = GetRootWindowController(root);
645 CHECK(rwc) << "root=" << root
646 << ", location:" << location_in_screen.ToString();
647 if (rwc)
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100648 rwc->ShowContextMenu(location_in_screen, source_type);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000649}
650
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000651void Shell::ToggleAppList(aura::Window* window) {
652 // If the context window is not given, show it on the active root window.
653 if (!window)
654 window = GetActiveRootWindow();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100655 if (!app_list_controller_)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000656 app_list_controller_.reset(new internal::AppListController);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000657 app_list_controller_->SetVisible(!app_list_controller_->IsVisible(), window);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000658}
659
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100660void Shell::SetDragAndDropHostOfCurrentAppList(
661 app_list::ApplicationDragAndDropHost* drag_and_drop_host) {
662 if (app_list_controller_.get())
663 app_list_controller_->SetDragAndDropHostOfCurrentAppList(
664 drag_and_drop_host);
665}
666
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000667bool Shell::GetAppListTargetVisibility() const {
668 return app_list_controller_.get() &&
669 app_list_controller_->GetTargetVisibility();
670}
671
672aura::Window* Shell::GetAppListWindow() {
673 return app_list_controller_.get() ? app_list_controller_->GetWindow() : NULL;
674}
675
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000676bool Shell::IsSystemModalWindowOpen() const {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000677 if (simulate_modal_window_open_for_testing_)
678 return true;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000679 const std::vector<aura::Window*> containers = GetContainersFromAllRootWindows(
680 internal::kShellWindowId_SystemModalContainer, NULL);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000681 for (std::vector<aura::Window*>::const_iterator cit = containers.begin();
682 cit != containers.end(); ++cit) {
683 for (aura::Window::Windows::const_iterator wit = (*cit)->children().begin();
684 wit != (*cit)->children().end(); ++wit) {
685 if ((*wit)->GetProperty(aura::client::kModalKey) ==
686 ui::MODAL_TYPE_SYSTEM && (*wit)->TargetVisibility()) {
687 return true;
688 }
689 }
690 }
691 return false;
692}
693
694views::NonClientFrameView* Shell::CreateDefaultNonClientFrameView(
695 views::Widget* widget) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000696 // Use translucent-style window frames for dialogs.
697 CustomFrameViewAsh* frame_view = new CustomFrameViewAsh;
698 frame_view->Init(widget);
699 return frame_view;
700}
701
702void Shell::RotateFocus(Direction direction) {
703 focus_cycler_->RotateFocus(
704 direction == FORWARD ? internal::FocusCycler::FORWARD :
705 internal::FocusCycler::BACKWARD);
706}
707
708void Shell::SetDisplayWorkAreaInsets(Window* contains,
709 const gfx::Insets& insets) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000710 if (!display_manager_->UpdateWorkAreaOfDisplayNearestWindow(contains, insets))
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000711 return;
712 FOR_EACH_OBSERVER(ShellObserver, observers_,
713 OnDisplayWorkAreaInsetsChanged());
714}
715
716void Shell::OnLoginStateChanged(user::LoginStatus status) {
717 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLoginStateChanged(status));
718 RootWindowControllerList controllers = GetAllRootWindowControllers();
719 for (RootWindowControllerList::iterator iter = controllers.begin();
720 iter != controllers.end(); ++iter)
721 (*iter)->OnLoginStateChanged(status);
722}
723
724void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status) {
725 RootWindowControllerList controllers = GetAllRootWindowControllers();
726 for (RootWindowControllerList::iterator iter = controllers.begin();
727 iter != controllers.end(); ++iter)
728 (*iter)->UpdateAfterLoginStatusChange(status);
729}
730
731void Shell::OnAppTerminating() {
732 FOR_EACH_OBSERVER(ShellObserver, observers_, OnAppTerminating());
733}
734
735void Shell::OnLockStateChanged(bool locked) {
736 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLockStateChanged(locked));
737}
738
739void Shell::CreateLauncher() {
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100740 RootWindowControllerList controllers = GetAllRootWindowControllers();
741 for (RootWindowControllerList::iterator iter = controllers.begin();
742 iter != controllers.end(); ++iter)
743 (*iter)->shelf()->CreateLauncher();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000744}
745
746void Shell::ShowLauncher() {
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100747 RootWindowControllerList controllers = GetAllRootWindowControllers();
748 for (RootWindowControllerList::iterator iter = controllers.begin();
749 iter != controllers.end(); ++iter)
750 (*iter)->ShowLauncher();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000751}
752
753void Shell::AddShellObserver(ShellObserver* observer) {
754 observers_.AddObserver(observer);
755}
756
757void Shell::RemoveShellObserver(ShellObserver* observer) {
758 observers_.RemoveObserver(observer);
759}
760
761void Shell::UpdateShelfVisibility() {
762 RootWindowControllerList controllers = GetAllRootWindowControllers();
763 for (RootWindowControllerList::iterator iter = controllers.begin();
764 iter != controllers.end(); ++iter)
765 if ((*iter)->shelf())
766 (*iter)->UpdateShelfVisibility();
767}
768
769void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior,
770 aura::RootWindow* root_window) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000771 ash::internal::ShelfLayoutManager::ForLauncher(root_window)->
772 SetAutoHideBehavior(behavior);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000773}
774
775ShelfAutoHideBehavior Shell::GetShelfAutoHideBehavior(
776 aura::RootWindow* root_window) const {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000777 return ash::internal::ShelfLayoutManager::ForLauncher(root_window)->
778 auto_hide_behavior();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000779}
780
781void Shell::SetShelfAlignment(ShelfAlignment alignment,
782 aura::RootWindow* root_window) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000783 if (ash::internal::ShelfLayoutManager::ForLauncher(root_window)->
784 SetAlignment(alignment)) {
785 FOR_EACH_OBSERVER(
786 ShellObserver, observers_, OnShelfAlignmentChanged(root_window));
787 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000788}
789
790ShelfAlignment Shell::GetShelfAlignment(aura::RootWindow* root_window) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000791 return GetRootWindowController(root_window)->
792 GetShelfLayoutManager()->GetAlignment();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000793}
794
795void Shell::SetDimming(bool should_dim) {
796 RootWindowControllerList controllers = GetAllRootWindowControllers();
797 for (RootWindowControllerList::iterator iter = controllers.begin();
798 iter != controllers.end(); ++iter)
799 (*iter)->screen_dimmer()->SetDimming(should_dim);
800}
801
802void Shell::CreateModalBackground(aura::Window* window) {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100803 if (!modality_filter_) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000804 modality_filter_.reset(new internal::SystemModalContainerEventFilter(this));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000805 AddPreTargetHandler(modality_filter_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000806 }
807 RootWindowControllerList controllers = GetAllRootWindowControllers();
808 for (RootWindowControllerList::iterator iter = controllers.begin();
809 iter != controllers.end(); ++iter)
810 (*iter)->GetSystemModalLayoutManager(window)->CreateModalBackground();
811}
812
813void Shell::OnModalWindowRemoved(aura::Window* removed) {
814 RootWindowControllerList controllers = GetAllRootWindowControllers();
815 bool activated = false;
816 for (RootWindowControllerList::iterator iter = controllers.begin();
817 iter != controllers.end() && !activated; ++iter) {
818 activated = (*iter)->GetSystemModalLayoutManager(removed)->
819 ActivateNextModalWindow();
820 }
821 if (!activated) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000822 RemovePreTargetHandler(modality_filter_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000823 modality_filter_.reset();
824 for (RootWindowControllerList::iterator iter = controllers.begin();
825 iter != controllers.end(); ++iter)
826 (*iter)->GetSystemModalLayoutManager(removed)->DestroyModalBackground();
827 }
828}
829
830WebNotificationTray* Shell::GetWebNotificationTray() {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000831 return GetPrimaryRootWindowController()->shelf()->
832 status_area_widget()->web_notification_tray();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000833}
834
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000835bool Shell::HasPrimaryStatusArea() {
836 ShelfWidget* shelf = GetPrimaryRootWindowController()->shelf();
837 return shelf && shelf->status_area_widget();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000838}
839
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000840SystemTray* Shell::GetPrimarySystemTray() {
841 return GetPrimaryRootWindowController()->GetSystemTray();
842}
843
844LauncherDelegate* Shell::GetLauncherDelegate() {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100845 if (!launcher_delegate_) {
Ben Murdocheb525c52013-07-10 11:40:50 +0100846 launcher_model_.reset(new LauncherModel);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000847 launcher_delegate_.reset(
848 delegate_->CreateLauncherDelegate(launcher_model_.get()));
849 }
850 return launcher_delegate_.get();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000851}
852
Ben Murdocheb525c52013-07-10 11:40:50 +0100853void Shell::SetTouchHudProjectionEnabled(bool enabled) {
854 if (is_touch_hud_projection_enabled_ == enabled)
855 return;
856
857 RootWindowList roots = GetInstance()->GetAllRootWindows();
858 for (RootWindowList::iterator iter = roots.begin(); iter != roots.end();
859 ++iter) {
860 internal::RootWindowController* controller = GetRootWindowController(*iter);
861 if (enabled)
862 controller->EnableTouchHudProjection();
863 else
864 controller->DisableTouchHudProjection();
865 }
866 is_touch_hud_projection_enabled_ = enabled;
867}
868
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000869void Shell::InitRootWindowForSecondaryDisplay(aura::RootWindow* root) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000870 internal::RootWindowController* controller =
871 new internal::RootWindowController(root);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000872 // Pass false for the |is_first_run_after_boot| parameter so we'll show a
873 // black background on this display instead of trying to mimic the boot splash
874 // screen.
Ben Murdocheb525c52013-07-10 11:40:50 +0100875 InitRootWindowController(controller, false);
876
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000877 controller->root_window_layout()->OnWindowResized();
878 desktop_background_controller_->OnRootWindowAdded(root);
879 high_contrast_controller_->OnRootWindowAdded(root);
880 root->ShowRootWindow();
881 // Activate new root for testing.
882 active_root_window_ = root;
Ben Murdocheb525c52013-07-10 11:40:50 +0100883
884 // Create a launcher if a user is already logged.
885 if (Shell::GetInstance()->session_state_delegate()->NumberOfLoggedInUsers())
886 controller->shelf()->CreateLauncher();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000887}
888
889void Shell::DoInitialWorkspaceAnimation() {
890 return GetPrimaryRootWindowController()->workspace_controller()->
891 DoInitialAnimation();
892}
893
894void Shell::InitRootWindowController(
Ben Murdocheb525c52013-07-10 11:40:50 +0100895 internal::RootWindowController* controller,
896 bool first_run_after_boot) {
897
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000898 aura::RootWindow* root_window = controller->root_window();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000899 DCHECK(activation_client_);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000900 DCHECK(visibility_controller_.get());
901 DCHECK(drag_drop_controller_.get());
902 DCHECK(capture_controller_.get());
903 DCHECK(window_cycle_controller_.get());
904
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000905 aura::client::SetFocusClient(root_window, focus_client_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000906 input_method_filter_->SetInputMethodPropertyInRootWindow(root_window);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000907 aura::client::SetActivationClient(root_window, activation_client_);
908 if (views::corewm::UseFocusController()) {
909 views::corewm::FocusController* controller =
910 static_cast<views::corewm::FocusController*>(activation_client_);
911 root_window->AddPreTargetHandler(controller);
912 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000913 aura::client::SetVisibilityClient(root_window, visibility_controller_.get());
914 aura::client::SetDragDropClient(root_window, drag_drop_controller_.get());
915 aura::client::SetCaptureClient(root_window, capture_controller_.get());
916 aura::client::SetScreenPositionClient(root_window,
917 screen_position_controller_.get());
918 aura::client::SetCursorClient(root_window, &cursor_manager_);
919 aura::client::SetTooltipClient(root_window, tooltip_controller_.get());
920 aura::client::SetEventClient(root_window, event_client_.get());
921
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100922 if (nested_dispatcher_controller_) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000923 aura::client::SetDispatcherClient(root_window,
924 nested_dispatcher_controller_.get());
925 }
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100926 if (user_action_client_)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000927 aura::client::SetUserActionClient(root_window, user_action_client_.get());
928
Ben Murdocheb525c52013-07-10 11:40:50 +0100929 controller->Init(first_run_after_boot);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000930
931 window_cycle_controller_->OnRootWindowAdded(root_window);
932}
933
934////////////////////////////////////////////////////////////////////////////////
935// Shell, private:
936
937bool Shell::CanWindowReceiveEvents(aura::Window* window) {
938 RootWindowControllerList controllers = GetAllRootWindowControllers();
939 for (RootWindowControllerList::iterator iter = controllers.begin();
940 iter != controllers.end(); ++iter) {
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100941 internal::SystemModalContainerLayoutManager* layout_manager =
942 (*iter)->GetSystemModalLayoutManager(window);
943 if (layout_manager && layout_manager->CanWindowReceiveEvents(window))
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000944 return true;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000945 }
946 return false;
947}
948
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000949////////////////////////////////////////////////////////////////////////////////
950// Shell, ui::EventTarget overrides:
951
952bool Shell::CanAcceptEvent(const ui::Event& event) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000953 return true;
954}
955
956ui::EventTarget* Shell::GetParentTarget() {
957 return NULL;
958}
959
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000960void Shell::OnEvent(ui::Event* event) {
961}
962
963////////////////////////////////////////////////////////////////////////////////
964// Shell, aura::client::ActivationChangeObserver implementation:
965
966void Shell::OnWindowActivated(aura::Window* gained_active,
967 aura::Window* lost_active) {
968 if (gained_active)
969 active_root_window_ = gained_active->GetRootWindow();
970}
971
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000972} // namespace ash