Move VISUAL_STATE promise to activation
[chromium-blink-merge.git] / ash / shell.cc
blobd7dc9b5623fa2c35b446e4d5f695c7889076cf8f
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_delegate.h"
12 #include "ash/accelerators/focus_manager_factory.h"
13 #include "ash/accelerators/nested_accelerator_delegate.h"
14 #include "ash/ash_switches.h"
15 #include "ash/autoclick/autoclick_controller.h"
16 #include "ash/desktop_background/desktop_background_controller.h"
17 #include "ash/desktop_background/desktop_background_view.h"
18 #include "ash/desktop_background/user_wallpaper_delegate.h"
19 #include "ash/display/cursor_window_controller.h"
20 #include "ash/display/display_controller.h"
21 #include "ash/display/display_manager.h"
22 #include "ash/display/event_transformation_handler.h"
23 #include "ash/display/mouse_cursor_event_filter.h"
24 #include "ash/display/screen_position_controller.h"
25 #include "ash/drag_drop/drag_drop_controller.h"
26 #include "ash/first_run/first_run_helper_impl.h"
27 #include "ash/focus_cycler.h"
28 #include "ash/frame/custom_frame_view_ash.h"
29 #include "ash/gpu_support.h"
30 #include "ash/high_contrast/high_contrast_controller.h"
31 #include "ash/host/ash_window_tree_host_init_params.h"
32 #include "ash/keyboard_uma_event_filter.h"
33 #include "ash/magnifier/magnification_controller.h"
34 #include "ash/magnifier/partial_magnification_controller.h"
35 #include "ash/media_delegate.h"
36 #include "ash/new_window_delegate.h"
37 #include "ash/root_window_controller.h"
38 #include "ash/session/session_state_delegate.h"
39 #include "ash/shelf/app_list_shelf_item_delegate.h"
40 #include "ash/shelf/shelf_delegate.h"
41 #include "ash/shelf/shelf_item_delegate.h"
42 #include "ash/shelf/shelf_item_delegate_manager.h"
43 #include "ash/shelf/shelf_layout_manager.h"
44 #include "ash/shelf/shelf_model.h"
45 #include "ash/shelf/shelf_widget.h"
46 #include "ash/shelf/shelf_window_watcher.h"
47 #include "ash/shell_delegate.h"
48 #include "ash/shell_factory.h"
49 #include "ash/shell_init_params.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/utility/partial_screenshot_controller.h"
56 #include "ash/wm/app_list_controller.h"
57 #include "ash/wm/ash_focus_rules.h"
58 #include "ash/wm/ash_native_cursor_manager.h"
59 #include "ash/wm/coordinate_conversion.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_controller.h"
63 #include "ash/wm/maximize_mode/maximize_mode_window_manager.h"
64 #include "ash/wm/mru_window_tracker.h"
65 #include "ash/wm/overlay_event_filter.h"
66 #include "ash/wm/overview/window_selector_controller.h"
67 #include "ash/wm/power_button_controller.h"
68 #include "ash/wm/resize_shadow_controller.h"
69 #include "ash/wm/root_window_layout_manager.h"
70 #include "ash/wm/screen_dimmer.h"
71 #include "ash/wm/system_gesture_event_filter.h"
72 #include "ash/wm/system_modal_container_event_filter.h"
73 #include "ash/wm/system_modal_container_layout_manager.h"
74 #include "ash/wm/toplevel_window_event_handler.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/trace_event/trace_event.h"
84 #include "ui/aura/client/aura_constants.h"
85 #include "ui/aura/env.h"
86 #include "ui/aura/layout_manager.h"
87 #include "ui/aura/window.h"
88 #include "ui/aura/window_event_dispatcher.h"
89 #include "ui/base/ui_base_switches.h"
90 #include "ui/base/user_activity/user_activity_detector.h"
91 #include "ui/compositor/layer.h"
92 #include "ui/compositor/layer_animator.h"
93 #include "ui/events/event_target_iterator.h"
94 #include "ui/gfx/display.h"
95 #include "ui/gfx/geometry/size.h"
96 #include "ui/gfx/image/image_skia.h"
97 #include "ui/gfx/screen.h"
98 #include "ui/keyboard/keyboard.h"
99 #include "ui/keyboard/keyboard_controller.h"
100 #include "ui/keyboard/keyboard_switches.h"
101 #include "ui/keyboard/keyboard_util.h"
102 #include "ui/message_center/message_center.h"
103 #include "ui/views/corewm/tooltip_aura.h"
104 #include "ui/views/corewm/tooltip_controller.h"
105 #include "ui/views/focus/focus_manager_factory.h"
106 #include "ui/views/widget/native_widget_aura.h"
107 #include "ui/views/widget/widget.h"
108 #include "ui/wm/core/accelerator_filter.h"
109 #include "ui/wm/core/compound_event_filter.h"
110 #include "ui/wm/core/focus_controller.h"
111 #include "ui/wm/core/input_method_event_filter.h"
112 #include "ui/wm/core/nested_accelerator_controller.h"
113 #include "ui/wm/core/shadow_controller.h"
114 #include "ui/wm/core/visibility_controller.h"
115 #include "ui/wm/core/window_modality_controller.h"
117 #if defined(OS_CHROMEOS)
118 #if defined(USE_X11)
119 #include "ui/gfx/x/x11_types.h"
120 #endif // defined(USE_X11)
121 #include "ash/accelerators/magnifier_key_scroller.h"
122 #include "ash/accelerators/spoken_feedback_toggler.h"
123 #include "ash/ash_constants.h"
124 #include "ash/content/display/display_color_manager_chromeos.h"
125 #include "ash/content/display/screen_orientation_controller_chromeos.h"
126 #include "ash/display/display_change_observer_chromeos.h"
127 #include "ash/display/display_configurator_animation.h"
128 #include "ash/display/display_error_observer_chromeos.h"
129 #include "ash/display/projecting_observer_chromeos.h"
130 #include "ash/display/resolution_notification_controller.h"
131 #include "ash/sticky_keys/sticky_keys_controller.h"
132 #include "ash/system/chromeos/bluetooth/bluetooth_notification_controller.h"
133 #include "ash/system/chromeos/brightness/brightness_controller_chromeos.h"
134 #include "ash/system/chromeos/power/power_event_observer.h"
135 #include "ash/system/chromeos/power/power_status.h"
136 #include "ash/system/chromeos/power/video_activity_notifier.h"
137 #include "ash/system/chromeos/session/last_window_closed_logout_reminder.h"
138 #include "ash/system/chromeos/session/logout_confirmation_controller.h"
139 #include "ash/touch/touch_transformer_controller.h"
140 #include "ash/virtual_keyboard_controller.h"
141 #include "base/bind_helpers.h"
142 #include "base/sys_info.h"
143 #include "chromeos/dbus/dbus_thread_manager.h"
144 #include "ui/chromeos/user_activity_power_manager_notifier.h"
145 #include "ui/display/chromeos/display_configurator.h"
146 #endif // defined(OS_CHROMEOS)
148 namespace ash {
150 namespace {
152 using aura::Window;
153 using views::Widget;
155 // A Corewm VisibilityController subclass that calls the Ash animation routine
156 // so we can pick up our extended animations. See ash/wm/window_animations.h.
157 class AshVisibilityController : public ::wm::VisibilityController {
158 public:
159 AshVisibilityController() {}
160 ~AshVisibilityController() override {}
162 private:
163 // Overridden from ::wm::VisibilityController:
164 bool CallAnimateOnChildWindowVisibilityChanged(aura::Window* window,
165 bool visible) override {
166 return AnimateOnChildWindowVisibilityChanged(window, visible);
169 DISALLOW_COPY_AND_ASSIGN(AshVisibilityController);
172 AshWindowTreeHostInitParams ShellInitParamsToAshWindowTreeHostInitParams(
173 const ShellInitParams& shell_init_params) {
174 AshWindowTreeHostInitParams ash_init_params;
175 #if defined(OS_WIN)
176 ash_init_params.remote_hwnd = shell_init_params.remote_hwnd;
177 #endif
178 return ash_init_params;
181 } // namespace
183 // static
184 Shell* Shell::instance_ = NULL;
185 // static
186 bool Shell::initially_hide_cursor_ = false;
188 ////////////////////////////////////////////////////////////////////////////////
189 // Shell, public:
191 // static
192 Shell* Shell::CreateInstance(const ShellInitParams& init_params) {
193 CHECK(!instance_);
194 instance_ = new Shell(init_params.delegate);
195 instance_->Init(init_params);
196 return instance_;
199 // static
200 Shell* Shell::GetInstance() {
201 CHECK(instance_);
202 return instance_;
205 // static
206 bool Shell::HasInstance() {
207 return !!instance_;
210 // static
211 void Shell::DeleteInstance() {
212 delete instance_;
213 instance_ = NULL;
216 // static
217 RootWindowController* Shell::GetPrimaryRootWindowController() {
218 CHECK(HasInstance());
219 return GetRootWindowController(GetPrimaryRootWindow());
222 // static
223 Shell::RootWindowControllerList Shell::GetAllRootWindowControllers() {
224 CHECK(HasInstance());
225 return Shell::GetInstance()->display_controller()->
226 GetAllRootWindowControllers();
229 // static
230 aura::Window* Shell::GetPrimaryRootWindow() {
231 CHECK(HasInstance());
232 return GetInstance()->display_controller()->GetPrimaryRootWindow();
235 // static
236 aura::Window* Shell::GetTargetRootWindow() {
237 CHECK(HasInstance());
238 Shell* shell = GetInstance();
239 if (shell->scoped_target_root_window_)
240 return shell->scoped_target_root_window_;
241 return shell->target_root_window_;
244 // static
245 gfx::Screen* Shell::GetScreen() {
246 return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE);
249 // static
250 aura::Window::Windows Shell::GetAllRootWindows() {
251 CHECK(HasInstance());
252 return Shell::GetInstance()->display_controller()->
253 GetAllRootWindows();
256 // static
257 aura::Window* Shell::GetContainer(aura::Window* root_window,
258 int container_id) {
259 return root_window->GetChildById(container_id);
262 // static
263 const aura::Window* Shell::GetContainer(const aura::Window* root_window,
264 int container_id) {
265 return root_window->GetChildById(container_id);
268 // static
269 std::vector<aura::Window*> Shell::GetContainersFromAllRootWindows(
270 int container_id,
271 aura::Window* priority_root) {
272 std::vector<aura::Window*> containers;
273 aura::Window::Windows root_windows = GetAllRootWindows();
274 for (aura::Window::Windows::const_iterator it = root_windows.begin();
275 it != root_windows.end(); ++it) {
276 aura::Window* container = (*it)->GetChildById(container_id);
277 if (container) {
278 if (priority_root && priority_root->Contains(container))
279 containers.insert(containers.begin(), container);
280 else
281 containers.push_back(container);
284 return containers;
287 void Shell::ShowContextMenu(const gfx::Point& location_in_screen,
288 ui::MenuSourceType source_type) {
289 // No context menus if there is no session with an active user.
290 if (!session_state_delegate_->NumberOfLoggedInUsers())
291 return;
292 // No context menus when screen is locked.
293 if (session_state_delegate_->IsScreenLocked())
294 return;
296 aura::Window* root =
297 wm::GetRootWindowMatching(gfx::Rect(location_in_screen, gfx::Size()));
298 GetRootWindowController(root)
299 ->ShowContextMenu(location_in_screen, source_type);
302 void Shell::ShowAppList(aura::Window* window) {
303 // If the context window is not given, show it on the target root window.
304 if (!window)
305 window = GetTargetRootWindow();
306 if (!app_list_controller_)
307 app_list_controller_.reset(new AppListController);
308 app_list_controller_->Show(window);
311 void Shell::DismissAppList() {
312 if (!app_list_controller_)
313 return;
314 app_list_controller_->Dismiss();
317 void Shell::ToggleAppList(aura::Window* window) {
318 if (app_list_controller_ && app_list_controller_->IsVisible()) {
319 DismissAppList();
320 return;
323 ShowAppList(window);
326 bool Shell::GetAppListTargetVisibility() const {
327 return app_list_controller_.get() &&
328 app_list_controller_->GetTargetVisibility();
331 aura::Window* Shell::GetAppListWindow() {
332 return app_list_controller_.get() ? app_list_controller_->GetWindow() : NULL;
335 app_list::AppListView* Shell::GetAppListView() {
336 return app_list_controller_.get() ? app_list_controller_->GetView() : NULL;
339 bool Shell::IsSystemModalWindowOpen() const {
340 if (simulate_modal_window_open_for_testing_)
341 return true;
342 const std::vector<aura::Window*> containers = GetContainersFromAllRootWindows(
343 kShellWindowId_SystemModalContainer, NULL);
344 for (std::vector<aura::Window*>::const_iterator cit = containers.begin();
345 cit != containers.end(); ++cit) {
346 for (aura::Window::Windows::const_iterator wit = (*cit)->children().begin();
347 wit != (*cit)->children().end(); ++wit) {
348 if ((*wit)->GetProperty(aura::client::kModalKey) ==
349 ui::MODAL_TYPE_SYSTEM && (*wit)->TargetVisibility()) {
350 return true;
354 return false;
357 views::NonClientFrameView* Shell::CreateDefaultNonClientFrameView(
358 views::Widget* widget) {
359 // Use translucent-style window frames for dialogs.
360 return new CustomFrameViewAsh(widget);
363 void Shell::RotateFocus(Direction direction) {
364 focus_cycler_->RotateFocus(direction == FORWARD ? FocusCycler::FORWARD
365 : FocusCycler::BACKWARD);
368 void Shell::SetDisplayWorkAreaInsets(Window* contains,
369 const gfx::Insets& insets) {
370 if (!display_controller_->UpdateWorkAreaOfDisplayNearestWindow(
371 contains, insets)) {
372 return;
374 FOR_EACH_OBSERVER(ShellObserver, observers_,
375 OnDisplayWorkAreaInsetsChanged());
378 void Shell::OnLoginStateChanged(user::LoginStatus status) {
379 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLoginStateChanged(status));
382 void Shell::OnLoginUserProfilePrepared() {
383 CreateShelf();
384 CreateKeyboard();
387 void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status) {
388 RootWindowControllerList controllers = GetAllRootWindowControllers();
389 for (RootWindowControllerList::iterator iter = controllers.begin();
390 iter != controllers.end(); ++iter)
391 (*iter)->UpdateAfterLoginStatusChange(status);
394 void Shell::OnAppTerminating() {
395 FOR_EACH_OBSERVER(ShellObserver, observers_, OnAppTerminating());
398 void Shell::OnLockStateChanged(bool locked) {
399 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLockStateChanged(locked));
400 #ifndef NDEBUG
401 // Make sure that there is no system modal in Lock layer when unlocked.
402 if (!locked) {
403 std::vector<aura::Window*> containers = GetContainersFromAllRootWindows(
404 kShellWindowId_LockSystemModalContainer, GetPrimaryRootWindow());
405 for (std::vector<aura::Window*>::const_iterator iter = containers.begin();
406 iter != containers.end(); ++iter) {
407 DCHECK_EQ(0u, (*iter)->children().size());
410 #endif
413 void Shell::OnCastingSessionStartedOrStopped(bool started) {
414 #if defined(OS_CHROMEOS)
415 FOR_EACH_OBSERVER(ShellObserver, observers_,
416 OnCastingSessionStartedOrStopped(started));
417 #endif
420 void Shell::OnOverviewModeStarting() {
421 FOR_EACH_OBSERVER(ShellObserver, observers_, OnOverviewModeStarting());
424 void Shell::OnOverviewModeEnded() {
425 FOR_EACH_OBSERVER(ShellObserver, observers_, OnOverviewModeEnded());
428 void Shell::OnMaximizeModeStarted() {
429 FOR_EACH_OBSERVER(ShellObserver, observers_, OnMaximizeModeStarted());
432 void Shell::OnMaximizeModeEnded() {
433 FOR_EACH_OBSERVER(ShellObserver, observers_, OnMaximizeModeEnded());
436 void Shell::OnRootWindowAdded(aura::Window* root_window) {
437 FOR_EACH_OBSERVER(ShellObserver, observers_, OnRootWindowAdded(root_window));
440 void Shell::CreateShelf() {
441 RootWindowControllerList controllers = GetAllRootWindowControllers();
442 for (RootWindowControllerList::iterator iter = controllers.begin();
443 iter != controllers.end(); ++iter)
444 (*iter)->shelf()->CreateShelf();
447 void Shell::OnShelfCreatedForRootWindow(aura::Window* root_window) {
448 FOR_EACH_OBSERVER(ShellObserver,
449 observers_,
450 OnShelfCreatedForRootWindow(root_window));
453 void Shell::CreateKeyboard() {
454 // TODO(bshe): Primary root window controller may not be the controller to
455 // attach virtual keyboard. See http://crbug.com/303429
456 InitKeyboard();
457 GetPrimaryRootWindowController()->
458 ActivateKeyboard(keyboard::KeyboardController::GetInstance());
461 void Shell::DeactivateKeyboard() {
462 if (keyboard::KeyboardController::GetInstance()) {
463 RootWindowControllerList controllers = GetAllRootWindowControllers();
464 for (RootWindowControllerList::iterator iter = controllers.begin();
465 iter != controllers.end(); ++iter) {
466 (*iter)->DeactivateKeyboard(keyboard::KeyboardController::GetInstance());
469 keyboard::KeyboardController::ResetInstance(NULL);
472 void Shell::ShowShelf() {
473 RootWindowControllerList controllers = GetAllRootWindowControllers();
474 for (RootWindowControllerList::iterator iter = controllers.begin();
475 iter != controllers.end(); ++iter)
476 (*iter)->ShowShelf();
479 void Shell::AddShellObserver(ShellObserver* observer) {
480 observers_.AddObserver(observer);
483 void Shell::RemoveShellObserver(ShellObserver* observer) {
484 observers_.RemoveObserver(observer);
487 #if defined(OS_CHROMEOS)
488 bool Shell::ShouldSaveDisplaySettings() {
489 // TODO(oshima): Allow saving the settings even in unified desktop mode.
490 return !(screen_orientation_controller_
491 ->ignore_display_configuration_updates() ||
492 resolution_notification_controller_->DoesNotificationTimeout()) &&
493 !switches::UnifiedDesktopEnabled();
495 #endif
497 void Shell::UpdateShelfVisibility() {
498 RootWindowControllerList controllers = GetAllRootWindowControllers();
499 for (RootWindowControllerList::iterator iter = controllers.begin();
500 iter != controllers.end(); ++iter)
501 if ((*iter)->shelf())
502 (*iter)->UpdateShelfVisibility();
505 void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior,
506 aura::Window* root_window) {
507 ash::ShelfLayoutManager::ForShelf(root_window)->SetAutoHideBehavior(behavior);
510 ShelfAutoHideBehavior Shell::GetShelfAutoHideBehavior(
511 aura::Window* root_window) const {
512 return ash::ShelfLayoutManager::ForShelf(root_window)->auto_hide_behavior();
515 void Shell::SetShelfAlignment(ShelfAlignment alignment,
516 aura::Window* root_window) {
517 if (ash::ShelfLayoutManager::ForShelf(root_window)->SetAlignment(alignment)) {
518 FOR_EACH_OBSERVER(
519 ShellObserver, observers_, OnShelfAlignmentChanged(root_window));
523 ShelfAlignment Shell::GetShelfAlignment(const aura::Window* root_window) {
524 return GetRootWindowController(root_window)
525 ->GetShelfLayoutManager()
526 ->GetAlignment();
529 void Shell::SetDimming(bool should_dim) {
530 RootWindowControllerList controllers = GetAllRootWindowControllers();
531 for (RootWindowControllerList::iterator iter = controllers.begin();
532 iter != controllers.end(); ++iter)
533 (*iter)->screen_dimmer()->SetDimming(should_dim);
536 void Shell::NotifyFullscreenStateChange(bool is_fullscreen,
537 aura::Window* root_window) {
538 FOR_EACH_OBSERVER(ShellObserver, observers_, OnFullscreenStateChanged(
539 is_fullscreen, root_window));
542 void Shell::CreateModalBackground(aura::Window* window) {
543 if (!modality_filter_) {
544 modality_filter_.reset(new SystemModalContainerEventFilter(this));
545 AddPreTargetHandler(modality_filter_.get());
547 RootWindowControllerList controllers = GetAllRootWindowControllers();
548 for (RootWindowControllerList::iterator iter = controllers.begin();
549 iter != controllers.end(); ++iter)
550 (*iter)->GetSystemModalLayoutManager(window)->CreateModalBackground();
553 void Shell::OnModalWindowRemoved(aura::Window* removed) {
554 RootWindowControllerList controllers = GetAllRootWindowControllers();
555 bool activated = false;
556 for (RootWindowControllerList::iterator iter = controllers.begin();
557 iter != controllers.end() && !activated; ++iter) {
558 activated = (*iter)->GetSystemModalLayoutManager(removed)->
559 ActivateNextModalWindow();
561 if (!activated) {
562 RemovePreTargetHandler(modality_filter_.get());
563 modality_filter_.reset();
564 for (RootWindowControllerList::iterator iter = controllers.begin();
565 iter != controllers.end(); ++iter)
566 (*iter)->GetSystemModalLayoutManager(removed)->DestroyModalBackground();
570 WebNotificationTray* Shell::GetWebNotificationTray() {
571 return GetPrimaryRootWindowController()->shelf()->
572 status_area_widget()->web_notification_tray();
575 bool Shell::HasPrimaryStatusArea() {
576 ShelfWidget* shelf = GetPrimaryRootWindowController()->shelf();
577 return shelf && shelf->status_area_widget();
580 SystemTray* Shell::GetPrimarySystemTray() {
581 return GetPrimaryRootWindowController()->GetSystemTray();
584 ShelfDelegate* Shell::GetShelfDelegate() {
585 if (!shelf_delegate_) {
586 shelf_model_.reset(new ShelfModel);
587 // Creates ShelfItemDelegateManager before ShelfDelegate.
588 shelf_item_delegate_manager_.reset(
589 new ShelfItemDelegateManager(shelf_model_.get()));
591 shelf_delegate_.reset(delegate_->CreateShelfDelegate(shelf_model_.get()));
592 scoped_ptr<ShelfItemDelegate> controller(new AppListShelfItemDelegate);
594 // Finding the shelf model's location of the app list and setting its
595 // ShelfItemDelegate.
596 int app_list_index = shelf_model_->GetItemIndexForType(TYPE_APP_LIST);
597 DCHECK_GE(app_list_index, 0);
598 ShelfID app_list_id = shelf_model_->items()[app_list_index].id;
599 DCHECK(app_list_id);
600 shelf_item_delegate_manager_->SetShelfItemDelegate(app_list_id,
601 controller.Pass());
602 shelf_window_watcher_.reset(new ShelfWindowWatcher(
603 shelf_model_.get(), shelf_item_delegate_manager_.get()));
605 return shelf_delegate_.get();
608 void Shell::SetTouchHudProjectionEnabled(bool enabled) {
609 if (is_touch_hud_projection_enabled_ == enabled)
610 return;
612 is_touch_hud_projection_enabled_ = enabled;
613 FOR_EACH_OBSERVER(ShellObserver, observers_,
614 OnTouchHudProjectionToggled(enabled));
617 #if defined(OS_CHROMEOS)
618 ash::FirstRunHelper* Shell::CreateFirstRunHelper() {
619 return new ash::FirstRunHelperImpl;
622 void Shell::SetCursorCompositingEnabled(bool enabled) {
623 display_controller_->cursor_window_controller()->SetCursorCompositingEnabled(
624 enabled);
625 native_cursor_manager_->SetNativeCursorEnabled(!enabled);
627 #endif // defined(OS_CHROMEOS)
629 void Shell::DoInitialWorkspaceAnimation() {
630 return GetPrimaryRootWindowController()->workspace_controller()->
631 DoInitialAnimation();
634 ////////////////////////////////////////////////////////////////////////////////
635 // Shell, private:
637 Shell::Shell(ShellDelegate* delegate)
638 : target_root_window_(NULL),
639 scoped_target_root_window_(NULL),
640 delegate_(delegate),
641 window_positioner_(new WindowPositioner),
642 activation_client_(NULL),
643 #if defined(OS_CHROMEOS)
644 display_configurator_(new ui::DisplayConfigurator()),
645 #endif // defined(OS_CHROMEOS)
646 native_cursor_manager_(new AshNativeCursorManager),
647 cursor_manager_(
648 scoped_ptr<::wm::NativeCursorManager>(native_cursor_manager_)),
649 simulate_modal_window_open_for_testing_(false),
650 is_touch_hud_projection_enabled_(false) {
651 DCHECK(delegate_.get());
652 gpu_support_.reset(delegate_->CreateGPUSupport());
653 display_manager_.reset(new DisplayManager);
654 display_controller_.reset(new DisplayController);
655 user_metrics_recorder_.reset(new UserMetricsRecorder);
657 #if defined(OS_CHROMEOS)
658 PowerStatus::Initialize();
659 #endif
662 Shell::~Shell() {
663 TRACE_EVENT0("shutdown", "ash::Shell::Destructor");
665 delegate_->PreShutdown();
667 views::FocusManagerFactory::Install(NULL);
669 // Remove the focus from any window. This will prevent overhead and side
670 // effects (e.g. crashes) from changing focus during shutdown.
671 // See bug crbug.com/134502.
672 aura::client::GetFocusClient(GetPrimaryRootWindow())->FocusWindow(NULL);
674 // Please keep in same order as in Init() because it's easy to miss one.
675 if (window_modality_controller_)
676 window_modality_controller_.reset();
677 #if defined(OS_CHROMEOS)
678 RemovePreTargetHandler(magnifier_key_scroll_handler_.get());
679 magnifier_key_scroll_handler_.reset();
681 RemovePreTargetHandler(speech_feedback_handler_.get());
682 speech_feedback_handler_.reset();
683 #endif
684 RemovePreTargetHandler(overlay_filter_.get());
685 RemovePreTargetHandler(input_method_filter_.get());
686 RemovePreTargetHandler(accelerator_filter_.get());
687 RemovePreTargetHandler(event_transformation_handler_.get());
688 RemovePreTargetHandler(toplevel_window_event_handler_.get());
689 RemovePostTargetHandler(toplevel_window_event_handler_.get());
690 RemovePreTargetHandler(system_gesture_filter_.get());
691 RemovePreTargetHandler(keyboard_metrics_filter_.get());
692 RemovePreTargetHandler(mouse_cursor_filter_.get());
694 // TooltipController is deleted with the Shell so removing its references.
695 RemovePreTargetHandler(tooltip_controller_.get());
697 #if defined(OS_CHROMEOS)
698 screen_orientation_controller_.reset();
699 #endif
701 // Destroy the virtual keyboard controller before the maximize mode controller
702 // since the latters destructor triggers events that the former is listening
703 // to but no longer cares about.
704 #if defined(OS_CHROMEOS)
705 virtual_keyboard_controller_.reset();
706 #endif
708 // Destroy maximize mode controller early on since it has some observers which
709 // need to be removed.
710 maximize_mode_controller_.reset();
712 // AppList needs to be released before shelf layout manager, which is
713 // destroyed with shelf container in the loop below. However, app list
714 // container is now on top of shelf container and released after it.
715 // TODO(xiyuan): Move it back when app list container is no longer needed.
716 app_list_controller_.reset();
718 #if defined(OS_CHROMEOS)
719 // Destroy the LastWindowClosedLogoutReminder before the
720 // LogoutConfirmationController.
721 last_window_closed_logout_reminder_.reset();
723 // Destroy the LogoutConfirmationController before the SystemTrayDelegate.
724 logout_confirmation_controller_.reset();
725 #endif
727 // Destroy SystemTrayDelegate before destroying the status area(s).
728 system_tray_delegate_->Shutdown();
729 system_tray_delegate_.reset();
731 locale_notification_controller_.reset();
733 // Drag-and-drop must be canceled prior to close all windows.
734 drag_drop_controller_.reset();
736 // Controllers who have WindowObserver added must be deleted
737 // before |display_controller_| is deleted.
739 #if defined(OS_CHROMEOS)
740 // VideoActivityNotifier must be deleted before |video_detector_| is
741 // deleted because it's observing video activity through
742 // VideoDetectorObserver interface.
743 video_activity_notifier_.reset();
744 #endif // defined(OS_CHROMEOS)
745 video_detector_.reset();
746 high_contrast_controller_.reset();
748 shadow_controller_.reset();
749 resize_shadow_controller_.reset();
751 window_cycle_controller_.reset();
752 window_selector_controller_.reset();
753 mru_window_tracker_.reset();
755 // |shelf_window_watcher_| has a weak pointer to |shelf_Model_|
756 // and has window observers.
757 shelf_window_watcher_.reset();
759 // Destroy all child windows including widgets.
760 display_controller_->CloseChildWindows();
762 // Chrome implementation of shelf delegate depends on FocusClient,
763 // so must be deleted before |focus_client_|.
764 shelf_delegate_.reset();
765 focus_client_.reset();
767 // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems
768 // needs to remove observers from it.
769 system_tray_notifier_.reset();
771 // These need a valid Shell instance to clean up properly, so explicitly
772 // delete them before invalidating the instance.
773 // Alphabetical. TODO(oshima): sort.
774 magnification_controller_.reset();
775 partial_magnification_controller_.reset();
776 tooltip_controller_.reset();
777 event_client_.reset();
778 nested_accelerator_controller_.reset();
779 toplevel_window_event_handler_.reset();
780 visibility_controller_.reset();
781 // |shelf_item_delegate_manager_| observes |shelf_model_|. It must be
782 // destroyed before |shelf_model_| is destroyed.
783 shelf_item_delegate_manager_.reset();
784 shelf_model_.reset();
786 power_button_controller_.reset();
787 lock_state_controller_.reset();
789 #if defined(OS_CHROMEOS)
790 resolution_notification_controller_.reset();
791 #endif
792 desktop_background_controller_.reset();
793 partial_screenshot_controller_.reset();
794 mouse_cursor_filter_.reset();
796 #if defined(OS_CHROMEOS)
797 touch_transformer_controller_.reset();
798 #endif // defined(OS_CHROMEOS)
800 // This also deletes all RootWindows. Note that we invoke Shutdown() on
801 // DisplayController before resetting |display_controller_|, since destruction
802 // of its owned RootWindowControllers relies on the value.
803 display_manager_->CreateScreenForShutdown();
804 display_controller_->Shutdown();
805 display_controller_.reset();
806 screen_position_controller_.reset();
807 accessibility_delegate_.reset();
808 new_window_delegate_.reset();
809 media_delegate_.reset();
811 keyboard::KeyboardController::ResetInstance(NULL);
813 #if defined(OS_CHROMEOS)
814 display_color_manager_.reset();
815 if (display_change_observer_)
816 display_configurator_->RemoveObserver(display_change_observer_.get());
817 if (display_configurator_animation_)
818 display_configurator_->RemoveObserver(
819 display_configurator_animation_.get());
820 if (display_error_observer_)
821 display_configurator_->RemoveObserver(display_error_observer_.get());
822 if (projecting_observer_) {
823 display_configurator_->RemoveObserver(projecting_observer_.get());
824 RemoveShellObserver(projecting_observer_.get());
826 display_change_observer_.reset();
828 PowerStatus::Shutdown();
830 // Ensure that DBusThreadManager outlives this Shell.
831 DCHECK(chromeos::DBusThreadManager::IsInitialized());
832 #endif
834 DCHECK(instance_ == this);
835 instance_ = NULL;
838 void Shell::Init(const ShellInitParams& init_params) {
839 delegate_->PreInit();
840 bool display_initialized = display_manager_->InitFromCommandLine();
841 #if defined(OS_CHROMEOS)
842 display_configurator_->Init(!gpu_support_->IsPanelFittingDisabled());
843 display_configurator_animation_.reset(new DisplayConfiguratorAnimation());
844 display_configurator_->AddObserver(display_configurator_animation_.get());
846 // The DBusThreadManager must outlive this Shell. See the DCHECK in ~Shell.
847 chromeos::DBusThreadManager* dbus_thread_manager =
848 chromeos::DBusThreadManager::Get();
849 projecting_observer_.reset(
850 new ProjectingObserver(dbus_thread_manager->GetPowerManagerClient()));
851 display_configurator_->AddObserver(projecting_observer_.get());
852 AddShellObserver(projecting_observer_.get());
854 if (!display_initialized && base::SysInfo::IsRunningOnChromeOS()) {
855 display_change_observer_.reset(new DisplayChangeObserver);
856 // Register |display_change_observer_| first so that the rest of
857 // observer gets invoked after the root windows are configured.
858 display_configurator_->AddObserver(display_change_observer_.get());
859 display_error_observer_.reset(new DisplayErrorObserver());
860 display_configurator_->AddObserver(display_error_observer_.get());
861 display_configurator_->set_state_controller(display_change_observer_.get());
862 display_configurator_->set_mirroring_controller(display_manager_.get());
863 display_configurator_->ForceInitialConfigure(
864 delegate_->IsFirstRunAfterBoot() ? kChromeOsBootColor : 0);
865 display_initialized = true;
867 display_color_manager_.reset(
868 new DisplayColorManager(display_configurator_.get()));
869 #endif // defined(OS_CHROMEOS)
870 if (!display_initialized)
871 display_manager_->InitDefaultDisplay();
873 display_manager_->RefreshFontParams();
875 // Install the custom factory first so that views::FocusManagers for Tray,
876 // Shelf, and WallPaper could be created by the factory.
877 views::FocusManagerFactory::Install(new AshFocusManagerFactory);
879 aura::Env::CreateInstance(true);
880 aura::Env::GetInstance()->set_context_factory(init_params.context_factory);
882 // The WindowModalityController needs to be at the front of the input event
883 // pretarget handler list to ensure that it processes input events when modal
884 // windows are active.
885 window_modality_controller_.reset(
886 new ::wm::WindowModalityController(this));
888 env_filter_.reset(new ::wm::CompoundEventFilter);
889 AddPreTargetHandler(env_filter_.get());
891 wm::AshFocusRules* focus_rules = new wm::AshFocusRules();
893 ::wm::FocusController* focus_controller =
894 new ::wm::FocusController(focus_rules);
895 focus_client_.reset(focus_controller);
896 activation_client_ = focus_controller;
897 activation_client_->AddObserver(this);
898 focus_cycler_.reset(new FocusCycler());
900 screen_position_controller_.reset(new ScreenPositionController);
902 display_controller_->Start();
903 display_controller_->CreatePrimaryHost(
904 ShellInitParamsToAshWindowTreeHostInitParams(init_params));
905 aura::Window* root_window = display_controller_->GetPrimaryRootWindow();
906 target_root_window_ = root_window;
908 #if defined(OS_CHROMEOS)
909 resolution_notification_controller_.reset(
910 new ResolutionNotificationController);
911 #endif
913 cursor_manager_.SetDisplay(GetScreen()->GetPrimaryDisplay());
915 nested_accelerator_controller_.reset(
916 new ::wm::NestedAcceleratorController(new NestedAcceleratorDelegate));
917 accelerator_controller_.reset(new AcceleratorController);
918 maximize_mode_controller_.reset(new MaximizeModeController());
920 #if defined(OS_CHROMEOS)
921 magnifier_key_scroll_handler_ = MagnifierKeyScroller::CreateHandler();
922 AddPreTargetHandler(magnifier_key_scroll_handler_.get());
923 speech_feedback_handler_ = SpokenFeedbackToggler::CreateHandler();
924 AddPreTargetHandler(speech_feedback_handler_.get());
925 #endif
927 // The order in which event filters are added is significant.
929 // ui::UserActivityDetector passes events to observers, so let them get
930 // rewritten first.
931 user_activity_detector_.reset(new ui::UserActivityDetector);
933 overlay_filter_.reset(new OverlayEventFilter);
934 AddPreTargetHandler(overlay_filter_.get());
935 AddShellObserver(overlay_filter_.get());
937 input_method_filter_.reset(new ::wm::InputMethodEventFilter(
938 root_window->GetHost()->GetAcceleratedWidget()));
939 AddPreTargetHandler(input_method_filter_.get());
941 accelerator_filter_.reset(new ::wm::AcceleratorFilter(
942 scoped_ptr< ::wm::AcceleratorDelegate>(new AcceleratorDelegate).Pass(),
943 accelerator_controller_->accelerator_history()));
944 AddPreTargetHandler(accelerator_filter_.get());
946 event_transformation_handler_.reset(new EventTransformationHandler);
947 AddPreTargetHandler(event_transformation_handler_.get());
949 toplevel_window_event_handler_.reset(new ToplevelWindowEventHandler);
951 system_gesture_filter_.reset(new SystemGestureEventFilter);
952 AddPreTargetHandler(system_gesture_filter_.get());
954 keyboard_metrics_filter_.reset(new KeyboardUMAEventFilter);
955 AddPreTargetHandler(keyboard_metrics_filter_.get());
957 // The keyboard system must be initialized before the RootWindowController is
958 // created.
959 #if defined(OS_CHROMEOS)
960 keyboard::InitializeKeyboard();
961 #endif
963 #if defined(OS_CHROMEOS)
964 sticky_keys_controller_.reset(new StickyKeysController);
965 #endif
967 lock_state_controller_.reset(new LockStateController);
968 power_button_controller_.reset(new PowerButtonController(
969 lock_state_controller_.get()));
970 #if defined(OS_CHROMEOS)
971 // Pass the initial display state to PowerButtonController.
972 power_button_controller_->OnDisplayModeChanged(
973 display_configurator_->cached_displays());
974 #endif
975 AddShellObserver(lock_state_controller_.get());
977 drag_drop_controller_.reset(new DragDropController);
978 // |partial_screenshot_controller_| needs to be created (and prepended as a
979 // pre-target handler) at this point, because |mouse_cursor_filter_| needs to
980 // process mouse events prior to partial screenshot session.
981 // See http://crbug.com/459214
982 partial_screenshot_controller_.reset(new PartialScreenshotController());
983 mouse_cursor_filter_.reset(new MouseCursorEventFilter());
984 PrependPreTargetHandler(mouse_cursor_filter_.get());
986 // Create Controllers that may need root window.
987 // TODO(oshima): Move as many controllers before creating
988 // RootWindowController as possible.
989 visibility_controller_.reset(new AshVisibilityController);
991 magnification_controller_.reset(
992 MagnificationController::CreateInstance());
993 mru_window_tracker_.reset(new MruWindowTracker(activation_client_,
994 focus_rules));
996 partial_magnification_controller_.reset(
997 new PartialMagnificationController());
999 autoclick_controller_.reset(AutoclickController::CreateInstance());
1001 high_contrast_controller_.reset(new HighContrastController);
1002 video_detector_.reset(new VideoDetector);
1003 window_selector_controller_.reset(new WindowSelectorController());
1004 window_cycle_controller_.reset(new WindowCycleController());
1006 tooltip_controller_.reset(new views::corewm::TooltipController(
1007 scoped_ptr<views::corewm::Tooltip>(new views::corewm::TooltipAura)));
1008 AddPreTargetHandler(tooltip_controller_.get());
1010 event_client_.reset(new EventClientImpl);
1012 // This controller needs to be set before SetupManagedWindowMode.
1013 desktop_background_controller_.reset(new DesktopBackgroundController());
1014 user_wallpaper_delegate_.reset(delegate_->CreateUserWallpaperDelegate());
1016 session_state_delegate_.reset(delegate_->CreateSessionStateDelegate());
1017 accessibility_delegate_.reset(delegate_->CreateAccessibilityDelegate());
1018 new_window_delegate_.reset(delegate_->CreateNewWindowDelegate());
1019 media_delegate_.reset(delegate_->CreateMediaDelegate());
1021 resize_shadow_controller_.reset(new ResizeShadowController());
1022 shadow_controller_.reset(
1023 new ::wm::ShadowController(activation_client_));
1025 // Create system_tray_notifier_ before the delegate.
1026 system_tray_notifier_.reset(new ash::SystemTrayNotifier());
1028 // Initialize system_tray_delegate_ before initializing StatusAreaWidget.
1029 system_tray_delegate_.reset(delegate()->CreateSystemTrayDelegate());
1030 DCHECK(system_tray_delegate_.get());
1032 locale_notification_controller_.reset(new LocaleNotificationController);
1034 // Initialize system_tray_delegate_ after StatusAreaWidget is created.
1035 system_tray_delegate_->Initialize();
1037 #if defined(OS_CHROMEOS)
1038 // Create the LogoutConfirmationController after the SystemTrayDelegate.
1039 logout_confirmation_controller_.reset(new LogoutConfirmationController(
1040 base::Bind(&SystemTrayDelegate::SignOut,
1041 base::Unretained(system_tray_delegate_.get()))));
1043 // Create TouchTransformerController before DisplayController::InitDisplays()
1044 // since TouchTransformerController listens on
1045 // DisplayController::Observer::OnDisplaysInitialized().
1046 touch_transformer_controller_.reset(new TouchTransformerController());
1047 #endif // defined(OS_CHROMEOS)
1049 display_controller_->InitDisplays();
1051 #if defined(OS_CHROMEOS)
1052 // Needs to be created after InitDisplays() since it may cause the virtual
1053 // keyboard to be deployed.
1054 virtual_keyboard_controller_.reset(new VirtualKeyboardController);
1055 #endif // defined(OS_CHROMEOS)
1057 // It needs to be created after RootWindowController has been created
1058 // (which calls OnWindowResized has been called, otherwise the
1059 // widget will not paint when restoring after a browser crash. Also it needs
1060 // to be created after InitSecondaryDisplays() to initialize the wallpapers in
1061 // the correct size.
1062 user_wallpaper_delegate_->InitializeWallpaper();
1064 if (initially_hide_cursor_)
1065 cursor_manager_.HideCursor();
1066 cursor_manager_.SetCursor(ui::kCursorPointer);
1068 #if defined(OS_CHROMEOS)
1069 // Set accelerator controller delegates.
1070 accelerator_controller_->SetBrightnessControlDelegate(
1071 scoped_ptr<ash::BrightnessControlDelegate>(
1072 new ash::system::BrightnessControllerChromeos).Pass());
1074 power_event_observer_.reset(new PowerEventObserver());
1075 user_activity_notifier_.reset(
1076 new ui::UserActivityPowerManagerNotifier(user_activity_detector_.get()));
1077 video_activity_notifier_.reset(
1078 new VideoActivityNotifier(video_detector_.get()));
1079 bluetooth_notification_controller_.reset(new BluetoothNotificationController);
1080 last_window_closed_logout_reminder_.reset(new LastWindowClosedLogoutReminder);
1081 screen_orientation_controller_.reset(new ScreenOrientationController());
1082 #endif
1083 // The compositor thread and main message loop have to be running in
1084 // order to create mirror window. Run it after the main message loop
1085 // is started.
1086 display_manager_->CreateMirrorWindowAsyncIfAny();
1089 void Shell::InitKeyboard() {
1090 if (keyboard::IsKeyboardEnabled()) {
1091 if (keyboard::KeyboardController::GetInstance()) {
1092 RootWindowControllerList controllers = GetAllRootWindowControllers();
1093 for (RootWindowControllerList::iterator iter = controllers.begin();
1094 iter != controllers.end(); ++iter) {
1095 (*iter)->DeactivateKeyboard(
1096 keyboard::KeyboardController::GetInstance());
1099 keyboard::KeyboardControllerProxy* proxy =
1100 delegate_->CreateKeyboardControllerProxy();
1101 keyboard::KeyboardController::ResetInstance(
1102 new keyboard::KeyboardController(proxy));
1106 void Shell::InitRootWindow(aura::Window* root_window) {
1107 DCHECK(activation_client_);
1108 DCHECK(visibility_controller_.get());
1109 DCHECK(drag_drop_controller_.get());
1111 aura::client::SetFocusClient(root_window, focus_client_.get());
1112 input_method_filter_->SetInputMethodPropertyInRootWindow(root_window);
1113 aura::client::SetActivationClient(root_window, activation_client_);
1114 ::wm::FocusController* focus_controller =
1115 static_cast< ::wm::FocusController*>(activation_client_);
1116 root_window->AddPreTargetHandler(focus_controller);
1117 aura::client::SetVisibilityClient(root_window, visibility_controller_.get());
1118 aura::client::SetDragDropClient(root_window, drag_drop_controller_.get());
1119 aura::client::SetScreenPositionClient(root_window,
1120 screen_position_controller_.get());
1121 aura::client::SetCursorClient(root_window, &cursor_manager_);
1122 aura::client::SetTooltipClient(root_window, tooltip_controller_.get());
1123 aura::client::SetEventClient(root_window, event_client_.get());
1125 aura::client::SetWindowMoveClient(root_window,
1126 toplevel_window_event_handler_.get());
1127 root_window->AddPreTargetHandler(toplevel_window_event_handler_.get());
1128 root_window->AddPostTargetHandler(toplevel_window_event_handler_.get());
1130 if (nested_accelerator_controller_) {
1131 aura::client::SetDispatcherClient(root_window,
1132 nested_accelerator_controller_.get());
1136 bool Shell::CanWindowReceiveEvents(aura::Window* window) {
1137 RootWindowControllerList controllers = GetAllRootWindowControllers();
1138 for (RootWindowControllerList::iterator iter = controllers.begin();
1139 iter != controllers.end(); ++iter) {
1140 SystemModalContainerLayoutManager* layout_manager =
1141 (*iter)->GetSystemModalLayoutManager(window);
1142 if (layout_manager && layout_manager->CanWindowReceiveEvents(window))
1143 return true;
1144 // Allow events to fall through to the virtual keyboard even if displaying
1145 // a system modal dialog.
1146 if ((*iter)->IsVirtualKeyboardWindow(window))
1147 return true;
1150 return false;
1153 ////////////////////////////////////////////////////////////////////////////////
1154 // Shell, ui::EventTarget overrides:
1156 bool Shell::CanAcceptEvent(const ui::Event& event) {
1157 return true;
1160 ui::EventTarget* Shell::GetParentTarget() {
1161 return aura::Env::GetInstance();
1164 scoped_ptr<ui::EventTargetIterator> Shell::GetChildIterator() const {
1165 return scoped_ptr<ui::EventTargetIterator>();
1168 ui::EventTargeter* Shell::GetEventTargeter() {
1169 NOTREACHED();
1170 return NULL;
1173 void Shell::OnEvent(ui::Event* event) {
1176 ////////////////////////////////////////////////////////////////////////////////
1177 // Shell, aura::client::ActivationChangeObserver implementation:
1179 void Shell::OnWindowActivated(aura::Window* gained_active,
1180 aura::Window* lost_active) {
1181 if (gained_active)
1182 target_root_window_ = gained_active->GetRootWindow();
1185 } // namespace ash