Enable icu_use_data_file_flag on Android
[chromium-blink-merge.git] / ash / shell.cc
blobc70e584ccb1fe530cf363e4a08de49c36bb75ff8
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/shell.h"
7 #include <algorithm>
8 #include <string>
10 #include "ash/accelerators/accelerator_controller.h"
11 #include "ash/accelerators/accelerator_filter.h"
12 #include "ash/accelerators/focus_manager_factory.h"
13 #include "ash/accelerators/nested_dispatcher_controller.h"
14 #include "ash/ash_switches.h"
15 #include "ash/autoclick/autoclick_controller.h"
16 #include "ash/caps_lock_delegate.h"
17 #include "ash/desktop_background/desktop_background_controller.h"
18 #include "ash/desktop_background/desktop_background_view.h"
19 #include "ash/desktop_background/user_wallpaper_delegate.h"
20 #include "ash/display/cursor_window_controller.h"
21 #include "ash/display/display_controller.h"
22 #include "ash/display/display_manager.h"
23 #include "ash/display/event_transformation_handler.h"
24 #include "ash/display/mouse_cursor_event_filter.h"
25 #include "ash/display/screen_position_controller.h"
26 #include "ash/display/virtual_keyboard_window_controller.h"
27 #include "ash/drag_drop/drag_drop_controller.h"
28 #include "ash/first_run/first_run_helper_impl.h"
29 #include "ash/focus_cycler.h"
30 #include "ash/gpu_support.h"
31 #include "ash/high_contrast/high_contrast_controller.h"
32 #include "ash/host/window_tree_host_factory.h"
33 #include "ash/keyboard_uma_event_filter.h"
34 #include "ash/magnifier/magnification_controller.h"
35 #include "ash/magnifier/partial_magnification_controller.h"
36 #include "ash/media_delegate.h"
37 #include "ash/new_window_delegate.h"
38 #include "ash/root_window_controller.h"
39 #include "ash/session_state_delegate.h"
40 #include "ash/shelf/app_list_shelf_item_delegate.h"
41 #include "ash/shelf/shelf_delegate.h"
42 #include "ash/shelf/shelf_item_delegate.h"
43 #include "ash/shelf/shelf_item_delegate_manager.h"
44 #include "ash/shelf/shelf_layout_manager.h"
45 #include "ash/shelf/shelf_model.h"
46 #include "ash/shelf/shelf_widget.h"
47 #include "ash/shelf/shelf_window_watcher.h"
48 #include "ash/shell_delegate.h"
49 #include "ash/shell_factory.h"
50 #include "ash/shell_window_ids.h"
51 #include "ash/system/locale/locale_notification_controller.h"
52 #include "ash/system/status_area_widget.h"
53 #include "ash/system/tray/system_tray_delegate.h"
54 #include "ash/system/tray/system_tray_notifier.h"
55 #include "ash/wm/app_list_controller.h"
56 #include "ash/wm/ash_focus_rules.h"
57 #include "ash/wm/ash_native_cursor_manager.h"
58 #include "ash/wm/coordinate_conversion.h"
59 #include "ash/wm/custom_frame_view_ash.h"
60 #include "ash/wm/event_client_impl.h"
61 #include "ash/wm/lock_state_controller.h"
62 #include "ash/wm/maximize_mode/maximize_mode_window_manager.h"
63 #include "ash/wm/mru_window_tracker.h"
64 #include "ash/wm/overlay_event_filter.h"
65 #include "ash/wm/overview/window_selector_controller.h"
66 #include "ash/wm/power_button_controller.h"
67 #include "ash/wm/resize_shadow_controller.h"
68 #include "ash/wm/root_window_layout_manager.h"
69 #include "ash/wm/screen_dimmer.h"
70 #include "ash/wm/system_gesture_event_filter.h"
71 #include "ash/wm/system_modal_container_event_filter.h"
72 #include "ash/wm/system_modal_container_layout_manager.h"
73 #include "ash/wm/toplevel_window_event_handler.h"
74 #include "ash/wm/user_activity_detector.h"
75 #include "ash/wm/video_detector.h"
76 #include "ash/wm/window_animations.h"
77 #include "ash/wm/window_cycle_controller.h"
78 #include "ash/wm/window_positioner.h"
79 #include "ash/wm/window_properties.h"
80 #include "ash/wm/window_util.h"
81 #include "ash/wm/workspace_controller.h"
82 #include "base/bind.h"
83 #include "base/command_line.h"
84 #include "base/debug/trace_event.h"
85 #include "ui/aura/client/aura_constants.h"
86 #include "ui/aura/client/user_action_client.h"
87 #include "ui/aura/env.h"
88 #include "ui/aura/layout_manager.h"
89 #include "ui/aura/window.h"
90 #include "ui/aura/window_event_dispatcher.h"
91 #include "ui/base/ui_base_switches.h"
92 #include "ui/compositor/layer.h"
93 #include "ui/compositor/layer_animator.h"
94 #include "ui/events/event_target_iterator.h"
95 #include "ui/gfx/display.h"
96 #include "ui/gfx/image/image_skia.h"
97 #include "ui/gfx/screen.h"
98 #include "ui/gfx/size.h"
99 #include "ui/keyboard/keyboard.h"
100 #include "ui/keyboard/keyboard_controller.h"
101 #include "ui/keyboard/keyboard_switches.h"
102 #include "ui/keyboard/keyboard_util.h"
103 #include "ui/message_center/message_center.h"
104 #include "ui/views/corewm/compound_event_filter.h"
105 #include "ui/views/corewm/corewm_switches.h"
106 #include "ui/views/corewm/focus_controller.h"
107 #include "ui/views/corewm/input_method_event_filter.h"
108 #include "ui/views/corewm/shadow_controller.h"
109 #include "ui/views/corewm/tooltip_aura.h"
110 #include "ui/views/corewm/tooltip_controller.h"
111 #include "ui/views/corewm/visibility_controller.h"
112 #include "ui/views/corewm/window_modality_controller.h"
113 #include "ui/views/focus/focus_manager_factory.h"
114 #include "ui/views/widget/native_widget_aura.h"
115 #include "ui/views/widget/widget.h"
117 #if defined(OS_CHROMEOS)
118 #if defined(USE_X11)
119 #include "ash/accelerators/magnifier_key_scroller.h"
120 #include "ash/accelerators/spoken_feedback_toggler.h"
121 #include "ash/ash_constants.h"
122 #include "ash/display/display_change_observer_chromeos.h"
123 #include "ash/display/display_error_observer_chromeos.h"
124 #include "ash/display/output_configurator_animation.h"
125 #include "ash/display/projecting_observer_chromeos.h"
126 #include "base/message_loop/message_pump_x11.h"
127 #include "base/sys_info.h"
128 #include "chromeos/display/output_configurator.h"
129 #endif // defined(USE_X11)
130 #include "ash/display/resolution_notification_controller.h"
131 #include "ash/sticky_keys/sticky_keys_controller.h"
132 #include "ash/system/chromeos/brightness/brightness_controller_chromeos.h"
133 #include "ash/system/chromeos/power/power_event_observer.h"
134 #include "ash/system/chromeos/power/power_status.h"
135 #include "ash/system/chromeos/power/user_activity_notifier.h"
136 #include "ash/system/chromeos/power/video_activity_notifier.h"
137 #endif // defined(OS_CHROMEOS)
139 namespace ash {
141 namespace {
143 using aura::Window;
144 using views::Widget;
146 // A Corewm VisibilityController subclass that calls the Ash animation routine
147 // so we can pick up our extended animations. See ash/wm/window_animations.h.
148 class AshVisibilityController : public views::corewm::VisibilityController {
149 public:
150 AshVisibilityController() {}
151 virtual ~AshVisibilityController() {}
153 private:
154 // Overridden from views::corewm::VisibilityController:
155 virtual bool CallAnimateOnChildWindowVisibilityChanged(
156 aura::Window* window,
157 bool visible) OVERRIDE {
158 return AnimateOnChildWindowVisibilityChanged(window, visible);
161 DISALLOW_COPY_AND_ASSIGN(AshVisibilityController);
164 } // namespace
166 // static
167 Shell* Shell::instance_ = NULL;
168 // static
169 bool Shell::initially_hide_cursor_ = false;
171 ////////////////////////////////////////////////////////////////////////////////
172 // Shell, public:
174 // static
175 Shell* Shell::CreateInstance(ShellDelegate* delegate) {
176 CHECK(!instance_);
177 instance_ = new Shell(delegate);
178 instance_->Init();
179 return instance_;
182 // static
183 Shell* Shell::GetInstance() {
184 DCHECK(instance_);
185 return instance_;
188 // static
189 bool Shell::HasInstance() {
190 return !!instance_;
193 // static
194 void Shell::DeleteInstance() {
195 delete instance_;
196 instance_ = NULL;
199 // static
200 internal::RootWindowController* Shell::GetPrimaryRootWindowController() {
201 return internal::GetRootWindowController(GetPrimaryRootWindow());
204 // static
205 Shell::RootWindowControllerList Shell::GetAllRootWindowControllers() {
206 return Shell::GetInstance()->display_controller()->
207 GetAllRootWindowControllers();
210 // static
211 aura::Window* Shell::GetPrimaryRootWindow() {
212 return GetInstance()->display_controller()->GetPrimaryRootWindow();
215 // static
216 aura::Window* Shell::GetTargetRootWindow() {
217 Shell* shell = GetInstance();
218 if (shell->scoped_target_root_window_)
219 return shell->scoped_target_root_window_;
220 return shell->target_root_window_;
223 // static
224 gfx::Screen* Shell::GetScreen() {
225 return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE);
228 // static
229 aura::Window::Windows Shell::GetAllRootWindows() {
230 return Shell::GetInstance()->display_controller()->
231 GetAllRootWindows();
234 // static
235 aura::Window* Shell::GetContainer(aura::Window* root_window,
236 int container_id) {
237 return root_window->GetChildById(container_id);
240 // static
241 const aura::Window* Shell::GetContainer(const aura::Window* root_window,
242 int container_id) {
243 return root_window->GetChildById(container_id);
246 // static
247 std::vector<aura::Window*> Shell::GetContainersFromAllRootWindows(
248 int container_id,
249 aura::Window* priority_root) {
250 std::vector<aura::Window*> containers;
251 aura::Window::Windows root_windows = GetAllRootWindows();
252 for (aura::Window::Windows::const_iterator it = root_windows.begin();
253 it != root_windows.end(); ++it) {
254 aura::Window* container = (*it)->GetChildById(container_id);
255 if (container) {
256 if (priority_root && priority_root->Contains(container))
257 containers.insert(containers.begin(), container);
258 else
259 containers.push_back(container);
262 return containers;
265 void Shell::ShowContextMenu(const gfx::Point& location_in_screen,
266 ui::MenuSourceType source_type) {
267 // No context menus if there is no session with an active user.
268 if (!session_state_delegate_->NumberOfLoggedInUsers())
269 return;
270 // No context menus when screen is locked.
271 if (session_state_delegate_->IsScreenLocked())
272 return;
274 aura::Window* root =
275 wm::GetRootWindowMatching(gfx::Rect(location_in_screen, gfx::Size()));
276 internal::GetRootWindowController(root)->
277 ShowContextMenu(location_in_screen, source_type);
280 void Shell::ToggleAppList(aura::Window* window) {
281 // If the context window is not given, show it on the target root window.
282 if (!window)
283 window = GetTargetRootWindow();
284 if (!app_list_controller_)
285 app_list_controller_.reset(new internal::AppListController);
286 app_list_controller_->SetVisible(!app_list_controller_->IsVisible(), window);
289 bool Shell::GetAppListTargetVisibility() const {
290 return app_list_controller_.get() &&
291 app_list_controller_->GetTargetVisibility();
294 aura::Window* Shell::GetAppListWindow() {
295 return app_list_controller_.get() ? app_list_controller_->GetWindow() : NULL;
298 app_list::AppListView* Shell::GetAppListView() {
299 return app_list_controller_.get() ? app_list_controller_->GetView() : NULL;
302 bool Shell::IsSystemModalWindowOpen() const {
303 if (simulate_modal_window_open_for_testing_)
304 return true;
305 const std::vector<aura::Window*> containers = GetContainersFromAllRootWindows(
306 internal::kShellWindowId_SystemModalContainer, NULL);
307 for (std::vector<aura::Window*>::const_iterator cit = containers.begin();
308 cit != containers.end(); ++cit) {
309 for (aura::Window::Windows::const_iterator wit = (*cit)->children().begin();
310 wit != (*cit)->children().end(); ++wit) {
311 if ((*wit)->GetProperty(aura::client::kModalKey) ==
312 ui::MODAL_TYPE_SYSTEM && (*wit)->TargetVisibility()) {
313 return true;
317 return false;
320 views::NonClientFrameView* Shell::CreateDefaultNonClientFrameView(
321 views::Widget* widget) {
322 // Use translucent-style window frames for dialogs.
323 return new CustomFrameViewAsh(widget);
326 void Shell::RotateFocus(Direction direction) {
327 focus_cycler_->RotateFocus(
328 direction == FORWARD ? internal::FocusCycler::FORWARD :
329 internal::FocusCycler::BACKWARD);
332 void Shell::SetDisplayWorkAreaInsets(Window* contains,
333 const gfx::Insets& insets) {
334 if (!display_controller_->UpdateWorkAreaOfDisplayNearestWindow(
335 contains, insets)) {
336 return;
338 FOR_EACH_OBSERVER(ShellObserver, observers_,
339 OnDisplayWorkAreaInsetsChanged());
342 void Shell::OnLoginStateChanged(user::LoginStatus status) {
343 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLoginStateChanged(status));
346 void Shell::OnLoginUserProfilePrepared() {
347 CreateShelf();
348 CreateKeyboard();
351 void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status) {
352 RootWindowControllerList controllers = GetAllRootWindowControllers();
353 for (RootWindowControllerList::iterator iter = controllers.begin();
354 iter != controllers.end(); ++iter)
355 (*iter)->UpdateAfterLoginStatusChange(status);
358 void Shell::OnAppTerminating() {
359 FOR_EACH_OBSERVER(ShellObserver, observers_, OnAppTerminating());
362 void Shell::OnLockStateChanged(bool locked) {
363 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLockStateChanged(locked));
364 #ifndef NDEBUG
365 // Make sure that there is no system modal in Lock layer when unlocked.
366 if (!locked) {
367 std::vector<aura::Window*> containers = GetContainersFromAllRootWindows(
368 internal::kShellWindowId_LockSystemModalContainer,
369 GetPrimaryRootWindow());
370 for (std::vector<aura::Window*>::const_iterator iter = containers.begin();
371 iter != containers.end(); ++iter) {
372 DCHECK_EQ(0u, (*iter)->children().size());
375 #endif
378 void Shell::OnCastingSessionStartedOrStopped(bool started) {
379 #if defined(OS_CHROMEOS) && defined(USE_X11)
380 if (projecting_observer_)
381 projecting_observer_->OnCastingSessionStartedOrStopped(started);
382 #endif
385 void Shell::CreateShelf() {
386 RootWindowControllerList controllers = GetAllRootWindowControllers();
387 for (RootWindowControllerList::iterator iter = controllers.begin();
388 iter != controllers.end(); ++iter)
389 (*iter)->shelf()->CreateShelf();
392 void Shell::CreateKeyboard() {
393 // TODO(bshe): Primary root window controller may not be the controller to
394 // attach virtual keyboard. See http://crbug.com/303429
395 InitKeyboard();
396 if (keyboard::IsKeyboardUsabilityExperimentEnabled()) {
397 display_controller()->virtual_keyboard_window_controller()->
398 ActivateKeyboard(keyboard_controller_.get());
399 } else {
400 GetPrimaryRootWindowController()->
401 ActivateKeyboard(keyboard_controller_.get());
405 void Shell::DeactivateKeyboard() {
406 if (keyboard_controller_.get()) {
407 RootWindowControllerList controllers = GetAllRootWindowControllers();
408 for (RootWindowControllerList::iterator iter = controllers.begin();
409 iter != controllers.end(); ++iter) {
410 (*iter)->DeactivateKeyboard(keyboard_controller_.get());
413 keyboard_controller_.reset();
416 void Shell::ShowShelf() {
417 RootWindowControllerList controllers = GetAllRootWindowControllers();
418 for (RootWindowControllerList::iterator iter = controllers.begin();
419 iter != controllers.end(); ++iter)
420 (*iter)->ShowShelf();
423 void Shell::AddShellObserver(ShellObserver* observer) {
424 observers_.AddObserver(observer);
427 void Shell::RemoveShellObserver(ShellObserver* observer) {
428 observers_.RemoveObserver(observer);
431 void Shell::EnableMaximizeModeWindowManager(bool enable) {
432 if (enable && !maximize_mode_window_manager_.get()) {
433 maximize_mode_window_manager_.reset(
434 new internal::MaximizeModeWindowManager());
435 } else if (!enable && maximize_mode_window_manager_.get()) {
436 maximize_mode_window_manager_.reset();
440 bool Shell::IsMaximizeModeWindowManagerEnabled() {
441 return maximize_mode_window_manager_.get() != NULL;
444 void Shell::UpdateShelfVisibility() {
445 RootWindowControllerList controllers = GetAllRootWindowControllers();
446 for (RootWindowControllerList::iterator iter = controllers.begin();
447 iter != controllers.end(); ++iter)
448 if ((*iter)->shelf())
449 (*iter)->UpdateShelfVisibility();
452 void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior,
453 aura::Window* root_window) {
454 ash::internal::ShelfLayoutManager::ForShelf(root_window)->
455 SetAutoHideBehavior(behavior);
458 ShelfAutoHideBehavior Shell::GetShelfAutoHideBehavior(
459 aura::Window* root_window) const {
460 return ash::internal::ShelfLayoutManager::ForShelf(root_window)->
461 auto_hide_behavior();
464 void Shell::SetShelfAlignment(ShelfAlignment alignment,
465 aura::Window* root_window) {
466 if (ash::internal::ShelfLayoutManager::ForShelf(root_window)->
467 SetAlignment(alignment)) {
468 FOR_EACH_OBSERVER(
469 ShellObserver, observers_, OnShelfAlignmentChanged(root_window));
473 ShelfAlignment Shell::GetShelfAlignment(aura::Window* root_window) {
474 return internal::GetRootWindowController(root_window)->
475 GetShelfLayoutManager()->GetAlignment();
478 void Shell::SetDimming(bool should_dim) {
479 RootWindowControllerList controllers = GetAllRootWindowControllers();
480 for (RootWindowControllerList::iterator iter = controllers.begin();
481 iter != controllers.end(); ++iter)
482 (*iter)->screen_dimmer()->SetDimming(should_dim);
485 void Shell::NotifyFullscreenStateChange(bool is_fullscreen,
486 aura::Window* root_window) {
487 FOR_EACH_OBSERVER(ShellObserver, observers_, OnFullscreenStateChanged(
488 is_fullscreen, root_window));
491 void Shell::CreateModalBackground(aura::Window* window) {
492 if (!modality_filter_) {
493 modality_filter_.reset(new internal::SystemModalContainerEventFilter(this));
494 AddPreTargetHandler(modality_filter_.get());
496 RootWindowControllerList controllers = GetAllRootWindowControllers();
497 for (RootWindowControllerList::iterator iter = controllers.begin();
498 iter != controllers.end(); ++iter)
499 (*iter)->GetSystemModalLayoutManager(window)->CreateModalBackground();
502 void Shell::OnModalWindowRemoved(aura::Window* removed) {
503 RootWindowControllerList controllers = GetAllRootWindowControllers();
504 bool activated = false;
505 for (RootWindowControllerList::iterator iter = controllers.begin();
506 iter != controllers.end() && !activated; ++iter) {
507 activated = (*iter)->GetSystemModalLayoutManager(removed)->
508 ActivateNextModalWindow();
510 if (!activated) {
511 RemovePreTargetHandler(modality_filter_.get());
512 modality_filter_.reset();
513 for (RootWindowControllerList::iterator iter = controllers.begin();
514 iter != controllers.end(); ++iter)
515 (*iter)->GetSystemModalLayoutManager(removed)->DestroyModalBackground();
519 WebNotificationTray* Shell::GetWebNotificationTray() {
520 return GetPrimaryRootWindowController()->shelf()->
521 status_area_widget()->web_notification_tray();
524 bool Shell::HasPrimaryStatusArea() {
525 ShelfWidget* shelf = GetPrimaryRootWindowController()->shelf();
526 return shelf && shelf->status_area_widget();
529 SystemTray* Shell::GetPrimarySystemTray() {
530 return GetPrimaryRootWindowController()->GetSystemTray();
533 ShelfDelegate* Shell::GetShelfDelegate() {
534 if (!shelf_delegate_) {
535 shelf_model_.reset(new ShelfModel);
536 // Creates ShelfItemDelegateManager before ShelfDelegate.
537 shelf_item_delegate_manager_.reset(
538 new ShelfItemDelegateManager(shelf_model_.get()));
540 shelf_delegate_.reset(delegate_->CreateShelfDelegate(shelf_model_.get()));
541 scoped_ptr<ShelfItemDelegate> controller(
542 new internal::AppListShelfItemDelegate);
544 // Finding the shelf model's location of the app list and setting its
545 // ShelfItemDelegate.
546 int app_list_index = shelf_model_->GetItemIndexForType(TYPE_APP_LIST);
547 DCHECK_GE(app_list_index, 0);
548 ShelfID app_list_id = shelf_model_->items()[app_list_index].id;
549 DCHECK(app_list_id);
550 shelf_item_delegate_manager_->SetShelfItemDelegate(app_list_id,
551 controller.Pass());
552 shelf_window_watcher_.reset(new internal::ShelfWindowWatcher(
553 shelf_model_.get(),
554 shelf_item_delegate_manager_.get()));
556 return shelf_delegate_.get();
559 void Shell::SetTouchHudProjectionEnabled(bool enabled) {
560 if (is_touch_hud_projection_enabled_ == enabled)
561 return;
563 is_touch_hud_projection_enabled_ = enabled;
564 FOR_EACH_OBSERVER(ShellObserver, observers_,
565 OnTouchHudProjectionToggled(enabled));
568 #if defined(OS_CHROMEOS)
569 ash::FirstRunHelper* Shell::CreateFirstRunHelper() {
570 return new ash::FirstRunHelperImpl;
573 void Shell::SetCursorCompositingEnabled(bool enabled) {
574 display_controller_->cursor_window_controller()->SetCursorCompositingEnabled(
575 enabled);
576 native_cursor_manager_->SetNativeCursorEnabled(!enabled);
578 #endif // defined(OS_CHROMEOS)
580 void Shell::DoInitialWorkspaceAnimation() {
581 return GetPrimaryRootWindowController()->workspace_controller()->
582 DoInitialAnimation();
585 ////////////////////////////////////////////////////////////////////////////////
586 // Shell, private:
588 Shell::Shell(ShellDelegate* delegate)
589 : target_root_window_(NULL),
590 scoped_target_root_window_(NULL),
591 delegate_(delegate),
592 window_positioner_(new WindowPositioner),
593 activation_client_(NULL),
594 #if defined(OS_CHROMEOS) && defined(USE_X11)
595 output_configurator_(new chromeos::OutputConfigurator()),
596 #endif // defined(OS_CHROMEOS) && defined(USE_X11)
597 native_cursor_manager_(new AshNativeCursorManager),
598 cursor_manager_(scoped_ptr<views::corewm::NativeCursorManager>(
599 native_cursor_manager_)),
600 simulate_modal_window_open_for_testing_(false),
601 is_touch_hud_projection_enabled_(false) {
602 DCHECK(delegate_.get());
603 gpu_support_.reset(delegate_->CreateGPUSupport());
604 display_manager_.reset(new internal::DisplayManager);
605 display_controller_.reset(new DisplayController);
606 #if defined(OS_CHROMEOS) && defined(USE_X11)
607 // TODO: Move this initialization into Shell::Init().
608 output_configurator_->Init(!gpu_support_->IsPanelFittingDisabled());
609 user_metrics_recorder_.reset(new UserMetricsRecorder);
610 #endif // defined(OS_CHROMEOS)
612 #if defined(OS_CHROMEOS)
613 internal::PowerStatus::Initialize();
614 #endif
617 Shell::~Shell() {
618 TRACE_EVENT0("shutdown", "ash::Shell::Destructor");
620 views::FocusManagerFactory::Install(NULL);
622 // Remove the focus from any window. This will prevent overhead and side
623 // effects (e.g. crashes) from changing focus during shutdown.
624 // See bug crbug.com/134502.
625 aura::client::GetFocusClient(GetPrimaryRootWindow())->FocusWindow(NULL);
627 // Please keep in same order as in Init() because it's easy to miss one.
628 if (window_modality_controller_)
629 window_modality_controller_.reset();
630 #if defined(OS_CHROMEOS) && defined(USE_X11)
631 RemovePreTargetHandler(magnifier_key_scroll_handler_.get());
632 magnifier_key_scroll_handler_.reset();
634 RemovePreTargetHandler(speech_feedback_handler_.get());
635 speech_feedback_handler_.reset();
636 #endif
637 RemovePreTargetHandler(user_activity_detector_.get());
638 RemovePreTargetHandler(overlay_filter_.get());
639 RemovePreTargetHandler(input_method_filter_.get());
640 RemovePreTargetHandler(accelerator_filter_.get());
641 RemovePreTargetHandler(event_transformation_handler_.get());
642 RemovePreTargetHandler(toplevel_window_event_handler_.get());
643 RemovePostTargetHandler(toplevel_window_event_handler_.get());
644 RemovePreTargetHandler(system_gesture_filter_.get());
645 RemovePreTargetHandler(keyboard_metrics_filter_.get());
646 if (mouse_cursor_filter_)
647 RemovePreTargetHandler(mouse_cursor_filter_.get());
649 // TooltipController is deleted with the Shell so removing its references.
650 RemovePreTargetHandler(tooltip_controller_.get());
652 // AppList needs to be released before shelf layout manager, which is
653 // destroyed with shelf container in the loop below. However, app list
654 // container is now on top of shelf container and released after it.
655 // TODO(xiyuan): Move it back when app list container is no longer needed.
656 app_list_controller_.reset();
658 // Destroy SystemTrayDelegate before destroying the status area(s).
659 system_tray_delegate_->Shutdown();
660 system_tray_delegate_.reset();
662 locale_notification_controller_.reset();
664 // Drag-and-drop must be canceled prior to close all windows.
665 drag_drop_controller_.reset();
667 // Controllers who have WindowObserver added must be deleted
668 // before |display_controller_| is deleted.
670 #if defined(OS_CHROMEOS)
671 // VideoActivityNotifier must be deleted before |video_detector_| is
672 // deleted because it's observing video activity through
673 // VideoDetectorObserver interface.
674 video_activity_notifier_.reset();
675 #endif // defined(OS_CHROMEOS)
676 video_detector_.reset();
678 shadow_controller_.reset();
679 resize_shadow_controller_.reset();
681 window_selector_controller_.reset();
682 window_cycle_controller_.reset();
683 mru_window_tracker_.reset();
685 // |shelf_window_watcher_| has a weak pointer to |shelf_Model_|
686 // and has window observers.
687 shelf_window_watcher_.reset();
689 // Destroy all child windows including widgets.
690 display_controller_->CloseChildWindows();
691 display_controller_->CloseNonDesktopDisplay();
693 // Chrome implementation of shelf delegate depends on FocusClient,
694 // so must be deleted before |focus_client_|.
695 shelf_delegate_.reset();
696 focus_client_.reset();
698 // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems
699 // needs to remove observers from it.
700 system_tray_notifier_.reset();
702 // These need a valid Shell instance to clean up properly, so explicitly
703 // delete them before invalidating the instance.
704 // Alphabetical. TODO(oshima): sort.
705 magnification_controller_.reset();
706 partial_magnification_controller_.reset();
707 tooltip_controller_.reset();
708 event_client_.reset();
709 nested_dispatcher_controller_.reset();
710 toplevel_window_event_handler_.reset();
711 user_action_client_.reset();
712 visibility_controller_.reset();
713 // |shelf_item_delegate_manager_| observes |shelf_model_|. It must be
714 // destroyed before |shelf_model_| is destroyed.
715 shelf_item_delegate_manager_.reset();
716 shelf_model_.reset();
718 power_button_controller_.reset();
719 lock_state_controller_.reset();
721 #if defined(OS_CHROMEOS)
722 resolution_notification_controller_.reset();
723 #endif
724 desktop_background_controller_.reset();
726 // This also deletes all RootWindows. Note that we invoke Shutdown() on
727 // DisplayController before resetting |display_controller_|, since destruction
728 // of its owned RootWindowControllers relies on the value.
729 display_manager_->CreateScreenForShutdown();
730 display_controller_->Shutdown();
731 display_controller_.reset();
732 screen_position_controller_.reset();
734 keyboard_controller_.reset();
735 accessibility_delegate_.reset();
736 new_window_delegate_.reset();
737 media_delegate_.reset();
739 #if defined(OS_CHROMEOS) && defined(USE_X11)
740 if (display_change_observer_)
741 output_configurator_->RemoveObserver(display_change_observer_.get());
742 if (output_configurator_animation_)
743 output_configurator_->RemoveObserver(output_configurator_animation_.get());
744 if (display_error_observer_)
745 output_configurator_->RemoveObserver(display_error_observer_.get());
746 if (projecting_observer_)
747 output_configurator_->RemoveObserver(projecting_observer_.get());
748 display_change_observer_.reset();
749 #endif // defined(OS_CHROMEOS)
751 #if defined(OS_CHROMEOS)
752 internal::PowerStatus::Shutdown();
753 #endif
755 DCHECK(instance_ == this);
756 instance_ = NULL;
759 void Shell::Init() {
760 CommandLine* command_line = CommandLine::ForCurrentProcess();
762 delegate_->PreInit();
763 if (keyboard::IsKeyboardUsabilityExperimentEnabled()) {
764 display_manager_->SetSecondDisplayMode(
765 internal::DisplayManager::VIRTUAL_KEYBOARD);
767 bool display_initialized = display_manager_->InitFromCommandLine();
768 #if defined(OS_CHROMEOS) && defined(USE_X11)
769 output_configurator_animation_.reset(
770 new internal::OutputConfiguratorAnimation());
771 output_configurator_->AddObserver(output_configurator_animation_.get());
773 projecting_observer_.reset(new internal::ProjectingObserver());
774 output_configurator_->AddObserver(projecting_observer_.get());
776 if (!display_initialized && base::SysInfo::IsRunningOnChromeOS()) {
777 display_change_observer_.reset(new internal::DisplayChangeObserver);
778 // Register |display_change_observer_| first so that the rest of
779 // observer gets invoked after the root windows are configured.
780 output_configurator_->AddObserver(display_change_observer_.get());
781 display_error_observer_.reset(new internal::DisplayErrorObserver());
782 output_configurator_->AddObserver(display_error_observer_.get());
783 output_configurator_->set_state_controller(display_change_observer_.get());
784 output_configurator_->set_mirroring_controller(display_manager_.get());
785 output_configurator_->Start(
786 delegate_->IsFirstRunAfterBoot() ? kChromeOsBootColor : 0);
787 display_initialized = true;
789 #endif // defined(OS_CHROMEOS) && defined(USE_X11)
790 if (!display_initialized)
791 display_manager_->InitDefaultDisplay();
793 // Install the custom factory first so that views::FocusManagers for Tray,
794 // Shelf, and WallPaper could be created by the factory.
795 views::FocusManagerFactory::Install(new AshFocusManagerFactory);
797 // Env creates the compositor. Historically it seems to have been implicitly
798 // initialized first by the ActivationController, but now that FocusController
799 // no longer does this we need to do it explicitly.
800 aura::Env::CreateInstance();
802 // The WindowModalityController needs to be at the front of the input event
803 // pretarget handler list to ensure that it processes input events when modal
804 // windows are active.
805 window_modality_controller_.reset(
806 new views::corewm::WindowModalityController(this));
808 AddPreTargetHandler(this);
810 env_filter_.reset(new views::corewm::CompoundEventFilter);
811 AddPreTargetHandler(env_filter_.get());
813 views::corewm::FocusController* focus_controller =
814 new views::corewm::FocusController(new wm::AshFocusRules);
815 focus_client_.reset(focus_controller);
816 activation_client_ = focus_controller;
817 activation_client_->AddObserver(this);
818 focus_cycler_.reset(new internal::FocusCycler());
820 screen_position_controller_.reset(new internal::ScreenPositionController);
821 window_tree_host_factory_.reset(delegate_->CreateWindowTreeHostFactory());
823 display_controller_->Start();
824 display_controller_->InitPrimaryDisplay();
825 aura::Window* root_window = display_controller_->GetPrimaryRootWindow();
826 target_root_window_ = root_window;
828 #if defined(OS_CHROMEOS)
829 resolution_notification_controller_.reset(
830 new internal::ResolutionNotificationController);
831 #endif
833 cursor_manager_.SetDisplay(GetScreen()->GetPrimaryDisplay());
835 nested_dispatcher_controller_.reset(new NestedDispatcherController);
836 accelerator_controller_.reset(new AcceleratorController);
838 #if defined(OS_CHROMEOS) && defined(USE_X11)
839 magnifier_key_scroll_handler_ = MagnifierKeyScroller::CreateHandler().Pass();
840 AddPreTargetHandler(magnifier_key_scroll_handler_.get());
841 speech_feedback_handler_ = SpokenFeedbackToggler::CreateHandler().Pass();
842 AddPreTargetHandler(speech_feedback_handler_.get());
843 #endif
845 // The order in which event filters are added is significant.
847 #if defined(OS_CHROMEOS)
848 // The StickyKeysController also rewrites events and must be added
849 // before observers, but after the EventRewriterEventFilter.
850 sticky_keys_controller_.reset(new StickyKeysController);
851 AddPreTargetHandler(sticky_keys_controller_.get());
852 #endif
854 // UserActivityDetector passes events to observers, so let them get
855 // rewritten first.
856 user_activity_detector_.reset(new UserActivityDetector);
857 AddPreTargetHandler(user_activity_detector_.get());
859 overlay_filter_.reset(new internal::OverlayEventFilter);
860 AddPreTargetHandler(overlay_filter_.get());
861 AddShellObserver(overlay_filter_.get());
863 input_method_filter_.reset(new views::corewm::InputMethodEventFilter(
864 root_window->GetDispatcher()->host()->GetAcceleratedWidget()));
865 AddPreTargetHandler(input_method_filter_.get());
867 accelerator_filter_.reset(new internal::AcceleratorFilter);
868 AddPreTargetHandler(accelerator_filter_.get());
870 event_transformation_handler_.reset(new internal::EventTransformationHandler);
871 AddPreTargetHandler(event_transformation_handler_.get());
873 toplevel_window_event_handler_.reset(new ToplevelWindowEventHandler);
875 system_gesture_filter_.reset(new internal::SystemGestureEventFilter);
876 AddPreTargetHandler(system_gesture_filter_.get());
878 keyboard_metrics_filter_.reset(new internal::KeyboardUMAEventFilter);
879 AddPreTargetHandler(keyboard_metrics_filter_.get());
881 // The keyboard system must be initialized before the RootWindowController is
882 // created.
883 #if defined(OS_CHROMEOS)
884 keyboard::InitializeKeyboard();
885 #endif
887 lock_state_controller_.reset(new LockStateController);
888 power_button_controller_.reset(new PowerButtonController(
889 lock_state_controller_.get()));
890 AddShellObserver(lock_state_controller_.get());
892 drag_drop_controller_.reset(new internal::DragDropController);
893 mouse_cursor_filter_.reset(new internal::MouseCursorEventFilter());
894 PrependPreTargetHandler(mouse_cursor_filter_.get());
896 // Create Controllers that may need root window.
897 // TODO(oshima): Move as many controllers before creating
898 // RootWindowController as possible.
899 visibility_controller_.reset(new AshVisibilityController);
900 user_action_client_.reset(delegate_->CreateUserActionClient());
902 magnification_controller_.reset(
903 MagnificationController::CreateInstance());
904 mru_window_tracker_.reset(new MruWindowTracker(activation_client_));
906 partial_magnification_controller_.reset(
907 new PartialMagnificationController());
909 autoclick_controller_.reset(AutoclickController::CreateInstance());
911 high_contrast_controller_.reset(new HighContrastController);
912 video_detector_.reset(new VideoDetector);
913 window_cycle_controller_.reset(new WindowCycleController());
914 window_selector_controller_.reset(new WindowSelectorController());
916 tooltip_controller_.reset(
917 new views::corewm::TooltipController(
918 scoped_ptr<views::corewm::Tooltip>(
919 new views::corewm::TooltipAura(gfx::SCREEN_TYPE_ALTERNATE))));
920 AddPreTargetHandler(tooltip_controller_.get());
922 event_client_.reset(new internal::EventClientImpl);
924 // This controller needs to be set before SetupManagedWindowMode.
925 desktop_background_controller_.reset(new DesktopBackgroundController());
926 user_wallpaper_delegate_.reset(delegate_->CreateUserWallpaperDelegate());
928 // StatusAreaWidget uses Shell's CapsLockDelegate.
929 caps_lock_delegate_.reset(delegate_->CreateCapsLockDelegate());
931 session_state_delegate_.reset(delegate_->CreateSessionStateDelegate());
932 accessibility_delegate_.reset(delegate_->CreateAccessibilityDelegate());
933 new_window_delegate_.reset(delegate_->CreateNewWindowDelegate());
934 media_delegate_.reset(delegate_->CreateMediaDelegate());
936 if (!command_line->HasSwitch(views::corewm::switches::kNoDropShadows)) {
937 resize_shadow_controller_.reset(new internal::ResizeShadowController());
938 shadow_controller_.reset(
939 new views::corewm::ShadowController(activation_client_));
942 // Create system_tray_notifier_ before the delegate.
943 system_tray_notifier_.reset(new ash::SystemTrayNotifier());
945 // Initialize system_tray_delegate_ before initializing StatusAreaWidget.
946 system_tray_delegate_.reset(delegate()->CreateSystemTrayDelegate());
947 DCHECK(system_tray_delegate_.get());
949 locale_notification_controller_.reset(
950 new internal::LocaleNotificationController);
952 // Initialize system_tray_delegate_ after StatusAreaWidget is created.
953 system_tray_delegate_->Initialize();
955 // TODO(oshima): Initialize all RootWindowControllers once, and
956 // initialize controller/delegates above when initializing the
957 // primary root window controller.
958 internal::RootWindowController::CreateForPrimaryDisplay(
959 root_window->GetDispatcher());
961 display_controller_->InitSecondaryDisplays();
963 // It needs to be created after RootWindowController has been created
964 // (which calls OnWindowResized has been called, otherwise the
965 // widget will not paint when restoring after a browser crash. Also it needs
966 // to be created after InitSecondaryDisplays() to initialize the wallpapers in
967 // the correct size.
968 user_wallpaper_delegate_->InitializeWallpaper();
970 if (initially_hide_cursor_)
971 cursor_manager_.HideCursor();
972 cursor_manager_.SetCursor(ui::kCursorPointer);
974 #if defined(OS_CHROMEOS)
975 // Set accelerator controller delegates.
976 accelerator_controller_->SetBrightnessControlDelegate(
977 scoped_ptr<ash::BrightnessControlDelegate>(
978 new ash::system::BrightnessControllerChromeos).Pass());
980 power_event_observer_.reset(new internal::PowerEventObserver());
981 user_activity_notifier_.reset(
982 new internal::UserActivityNotifier(user_activity_detector_.get()));
983 video_activity_notifier_.reset(
984 new internal::VideoActivityNotifier(video_detector_.get()));
985 #endif
987 weak_display_manager_factory_.reset(
988 new base::WeakPtrFactory<internal::DisplayManager>(
989 display_manager_.get()));
990 // The compositor thread and main message loop have to be running in
991 // order to create mirror window. Run it after the main message loop
992 // is started.
993 base::MessageLoopForUI::current()->PostTask(
994 FROM_HERE,
995 base::Bind(&internal::DisplayManager::CreateMirrorWindowIfAny,
996 weak_display_manager_factory_->GetWeakPtr()));
999 void Shell::InitKeyboard() {
1000 if (keyboard::IsKeyboardEnabled()) {
1001 if (keyboard_controller_.get()) {
1002 RootWindowControllerList controllers = GetAllRootWindowControllers();
1003 for (RootWindowControllerList::iterator iter = controllers.begin();
1004 iter != controllers.end(); ++iter) {
1005 (*iter)->DeactivateKeyboard(keyboard_controller_.get());
1008 keyboard::KeyboardControllerProxy* proxy =
1009 delegate_->CreateKeyboardControllerProxy();
1010 keyboard_controller_.reset(
1011 new keyboard::KeyboardController(proxy));
1015 void Shell::InitRootWindow(aura::Window* root_window) {
1016 DCHECK(activation_client_);
1017 DCHECK(visibility_controller_.get());
1018 DCHECK(drag_drop_controller_.get());
1019 DCHECK(window_cycle_controller_.get());
1021 aura::client::SetFocusClient(root_window, focus_client_.get());
1022 input_method_filter_->SetInputMethodPropertyInRootWindow(root_window);
1023 aura::client::SetActivationClient(root_window, activation_client_);
1024 views::corewm::FocusController* focus_controller =
1025 static_cast<views::corewm::FocusController*>(activation_client_);
1026 root_window->AddPreTargetHandler(focus_controller);
1027 aura::client::SetVisibilityClient(root_window, visibility_controller_.get());
1028 aura::client::SetDragDropClient(root_window, drag_drop_controller_.get());
1029 aura::client::SetScreenPositionClient(root_window,
1030 screen_position_controller_.get());
1031 aura::client::SetCursorClient(root_window, &cursor_manager_);
1032 aura::client::SetTooltipClient(root_window, tooltip_controller_.get());
1033 aura::client::SetEventClient(root_window, event_client_.get());
1035 aura::client::SetWindowMoveClient(root_window,
1036 toplevel_window_event_handler_.get());
1037 root_window->AddPreTargetHandler(toplevel_window_event_handler_.get());
1038 root_window->AddPostTargetHandler(toplevel_window_event_handler_.get());
1040 if (nested_dispatcher_controller_) {
1041 aura::client::SetDispatcherClient(root_window,
1042 nested_dispatcher_controller_.get());
1044 if (user_action_client_)
1045 aura::client::SetUserActionClient(root_window, user_action_client_.get());
1048 bool Shell::CanWindowReceiveEvents(aura::Window* window) {
1049 RootWindowControllerList controllers = GetAllRootWindowControllers();
1050 for (RootWindowControllerList::iterator iter = controllers.begin();
1051 iter != controllers.end(); ++iter) {
1052 internal::SystemModalContainerLayoutManager* layout_manager =
1053 (*iter)->GetSystemModalLayoutManager(window);
1054 if (layout_manager && layout_manager->CanWindowReceiveEvents(window))
1055 return true;
1057 return false;
1060 ////////////////////////////////////////////////////////////////////////////////
1061 // Shell, ui::EventTarget overrides:
1063 bool Shell::CanAcceptEvent(const ui::Event& event) {
1064 return true;
1067 ui::EventTarget* Shell::GetParentTarget() {
1068 return aura::Env::GetInstance();
1071 scoped_ptr<ui::EventTargetIterator> Shell::GetChildIterator() const {
1072 return scoped_ptr<ui::EventTargetIterator>();
1075 ui::EventTargeter* Shell::GetEventTargeter() {
1076 NOTREACHED();
1077 return NULL;
1080 void Shell::OnEvent(ui::Event* event) {
1083 ////////////////////////////////////////////////////////////////////////////////
1084 // Shell, aura::client::ActivationChangeObserver implementation:
1086 void Shell::OnWindowActivated(aura::Window* gained_active,
1087 aura::Window* lost_active) {
1088 if (gained_active)
1089 target_root_window_ = gained_active->GetRootWindow();
1092 } // namespace ash