Avoid using ToInternalValue in computing timings, so that the timings are consistent...
[chromium-blink-merge.git] / ash / shell.cc
blob64ebb4a75f5dbd61bde1090dab08c050d36dcbcf
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/accelerometer/accelerometer_controller.h"
15 #include "ash/ash_switches.h"
16 #include "ash/autoclick/autoclick_controller.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/frame/custom_frame_view_ash.h"
31 #include "ash/gpu_support.h"
32 #include "ash/high_contrast/high_contrast_controller.h"
33 #include "ash/host/ash_window_tree_host_init_params.h"
34 #include "ash/keyboard_uma_event_filter.h"
35 #include "ash/magnifier/magnification_controller.h"
36 #include "ash/magnifier/partial_magnification_controller.h"
37 #include "ash/media_delegate.h"
38 #include "ash/new_window_delegate.h"
39 #include "ash/root_window_controller.h"
40 #include "ash/session/session_state_delegate.h"
41 #include "ash/shelf/app_list_shelf_item_delegate.h"
42 #include "ash/shelf/shelf_delegate.h"
43 #include "ash/shelf/shelf_item_delegate.h"
44 #include "ash/shelf/shelf_item_delegate_manager.h"
45 #include "ash/shelf/shelf_layout_manager.h"
46 #include "ash/shelf/shelf_model.h"
47 #include "ash/shelf/shelf_widget.h"
48 #include "ash/shelf/shelf_window_watcher.h"
49 #include "ash/shell_delegate.h"
50 #include "ash/shell_factory.h"
51 #include "ash/shell_init_params.h"
52 #include "ash/shell_window_ids.h"
53 #include "ash/system/locale/locale_notification_controller.h"
54 #include "ash/system/status_area_widget.h"
55 #include "ash/system/tray/system_tray_delegate.h"
56 #include "ash/system/tray/system_tray_notifier.h"
57 #include "ash/wm/app_list_controller.h"
58 #include "ash/wm/ash_focus_rules.h"
59 #include "ash/wm/ash_native_cursor_manager.h"
60 #include "ash/wm/coordinate_conversion.h"
61 #include "ash/wm/event_client_impl.h"
62 #include "ash/wm/lock_state_controller.h"
63 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
64 #include "ash/wm/maximize_mode/maximize_mode_window_manager.h"
65 #include "ash/wm/mru_window_tracker.h"
66 #include "ash/wm/overlay_event_filter.h"
67 #include "ash/wm/overview/window_selector_controller.h"
68 #include "ash/wm/power_button_controller.h"
69 #include "ash/wm/resize_shadow_controller.h"
70 #include "ash/wm/root_window_layout_manager.h"
71 #include "ash/wm/screen_dimmer.h"
72 #include "ash/wm/system_gesture_event_filter.h"
73 #include "ash/wm/system_modal_container_event_filter.h"
74 #include "ash/wm/system_modal_container_layout_manager.h"
75 #include "ash/wm/toplevel_window_event_handler.h"
76 #include "ash/wm/video_detector.h"
77 #include "ash/wm/window_animations.h"
78 #include "ash/wm/window_cycle_controller.h"
79 #include "ash/wm/window_positioner.h"
80 #include "ash/wm/window_properties.h"
81 #include "ash/wm/window_util.h"
82 #include "ash/wm/workspace_controller.h"
83 #include "base/bind.h"
84 #include "base/debug/trace_event.h"
85 #include "ui/aura/client/aura_constants.h"
86 #include "ui/aura/env.h"
87 #include "ui/aura/layout_manager.h"
88 #include "ui/aura/window.h"
89 #include "ui/aura/window_event_dispatcher.h"
90 #include "ui/base/ui_base_switches.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/image/image_skia.h"
96 #include "ui/gfx/screen.h"
97 #include "ui/gfx/size.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/user_activity_detector.h"
115 #include "ui/wm/core/visibility_controller.h"
116 #include "ui/wm/core/window_modality_controller.h"
118 #if defined(OS_CHROMEOS)
119 #if defined(USE_X11)
120 #include "ash/accelerators/magnifier_key_scroller.h"
121 #include "ash/accelerators/spoken_feedback_toggler.h"
122 #include "ash/touch/touch_transformer_controller.h"
123 #include "ui/gfx/x/x11_types.h"
124 #endif // defined(USE_X11)
125 #include "ash/ash_constants.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 "base/bind_helpers.h"
140 #include "base/sys_info.h"
141 #include "ui/chromeos/user_activity_power_manager_notifier.h"
142 #include "ui/display/chromeos/display_configurator.h"
143 #endif // defined(OS_CHROMEOS)
145 namespace ash {
147 namespace {
149 using aura::Window;
150 using views::Widget;
152 // A Corewm VisibilityController subclass that calls the Ash animation routine
153 // so we can pick up our extended animations. See ash/wm/window_animations.h.
154 class AshVisibilityController : public ::wm::VisibilityController {
155 public:
156 AshVisibilityController() {}
157 virtual ~AshVisibilityController() {}
159 private:
160 // Overridden from ::wm::VisibilityController:
161 virtual bool CallAnimateOnChildWindowVisibilityChanged(
162 aura::Window* window,
163 bool visible) OVERRIDE {
164 return AnimateOnChildWindowVisibilityChanged(window, visible);
167 DISALLOW_COPY_AND_ASSIGN(AshVisibilityController);
170 AshWindowTreeHostInitParams ShellInitParamsToAshWindowTreeHostInitParams(
171 const ShellInitParams& shell_init_params) {
172 AshWindowTreeHostInitParams ash_init_params;
173 #if defined(OS_WIN)
174 ash_init_params.remote_hwnd = shell_init_params.remote_hwnd;
175 #endif
176 return ash_init_params;
179 } // namespace
181 // static
182 Shell* Shell::instance_ = NULL;
183 // static
184 bool Shell::initially_hide_cursor_ = false;
186 ////////////////////////////////////////////////////////////////////////////////
187 // Shell, public:
189 // static
190 Shell* Shell::CreateInstance(const ShellInitParams& init_params) {
191 CHECK(!instance_);
192 instance_ = new Shell(init_params.delegate);
193 instance_->Init(init_params);
194 return instance_;
197 // static
198 Shell* Shell::GetInstance() {
199 CHECK(instance_);
200 return instance_;
203 // static
204 bool Shell::HasInstance() {
205 return !!instance_;
208 // static
209 void Shell::DeleteInstance() {
210 delete instance_;
211 instance_ = NULL;
214 // static
215 RootWindowController* Shell::GetPrimaryRootWindowController() {
216 CHECK(HasInstance());
217 return GetRootWindowController(GetPrimaryRootWindow());
220 // static
221 Shell::RootWindowControllerList Shell::GetAllRootWindowControllers() {
222 CHECK(HasInstance());
223 return Shell::GetInstance()->display_controller()->
224 GetAllRootWindowControllers();
227 // static
228 aura::Window* Shell::GetPrimaryRootWindow() {
229 CHECK(HasInstance());
230 return GetInstance()->display_controller()->GetPrimaryRootWindow();
233 // static
234 aura::Window* Shell::GetTargetRootWindow() {
235 CHECK(HasInstance());
236 Shell* shell = GetInstance();
237 if (shell->scoped_target_root_window_)
238 return shell->scoped_target_root_window_;
239 return shell->target_root_window_;
242 // static
243 gfx::Screen* Shell::GetScreen() {
244 return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE);
247 // static
248 aura::Window::Windows Shell::GetAllRootWindows() {
249 CHECK(HasInstance());
250 return Shell::GetInstance()->display_controller()->
251 GetAllRootWindows();
254 // static
255 aura::Window* Shell::GetContainer(aura::Window* root_window,
256 int container_id) {
257 return root_window->GetChildById(container_id);
260 // static
261 const aura::Window* Shell::GetContainer(const aura::Window* root_window,
262 int container_id) {
263 return root_window->GetChildById(container_id);
266 // static
267 std::vector<aura::Window*> Shell::GetContainersFromAllRootWindows(
268 int container_id,
269 aura::Window* priority_root) {
270 std::vector<aura::Window*> containers;
271 aura::Window::Windows root_windows = GetAllRootWindows();
272 for (aura::Window::Windows::const_iterator it = root_windows.begin();
273 it != root_windows.end(); ++it) {
274 aura::Window* container = (*it)->GetChildById(container_id);
275 if (container) {
276 if (priority_root && priority_root->Contains(container))
277 containers.insert(containers.begin(), container);
278 else
279 containers.push_back(container);
282 return containers;
285 void Shell::ShowContextMenu(const gfx::Point& location_in_screen,
286 ui::MenuSourceType source_type) {
287 // No context menus if there is no session with an active user.
288 if (!session_state_delegate_->NumberOfLoggedInUsers())
289 return;
290 // No context menus when screen is locked.
291 if (session_state_delegate_->IsScreenLocked())
292 return;
294 aura::Window* root =
295 wm::GetRootWindowMatching(gfx::Rect(location_in_screen, gfx::Size()));
296 GetRootWindowController(root)
297 ->ShowContextMenu(location_in_screen, source_type);
300 void Shell::ShowAppList(aura::Window* window) {
301 // If the context window is not given, show it on the target root window.
302 if (!window)
303 window = GetTargetRootWindow();
304 if (!app_list_controller_)
305 app_list_controller_.reset(new AppListController);
306 app_list_controller_->Show(window);
309 void Shell::DismissAppList() {
310 if (!app_list_controller_)
311 return;
312 app_list_controller_->Dismiss();
315 void Shell::ToggleAppList(aura::Window* window) {
316 if (app_list_controller_ && app_list_controller_->IsVisible()) {
317 DismissAppList();
318 return;
321 ShowAppList(window);
324 bool Shell::GetAppListTargetVisibility() const {
325 return app_list_controller_.get() &&
326 app_list_controller_->GetTargetVisibility();
329 aura::Window* Shell::GetAppListWindow() {
330 return app_list_controller_.get() ? app_list_controller_->GetWindow() : NULL;
333 app_list::AppListView* Shell::GetAppListView() {
334 return app_list_controller_.get() ? app_list_controller_->GetView() : NULL;
337 bool Shell::IsSystemModalWindowOpen() const {
338 if (simulate_modal_window_open_for_testing_)
339 return true;
340 const std::vector<aura::Window*> containers = GetContainersFromAllRootWindows(
341 kShellWindowId_SystemModalContainer, NULL);
342 for (std::vector<aura::Window*>::const_iterator cit = containers.begin();
343 cit != containers.end(); ++cit) {
344 for (aura::Window::Windows::const_iterator wit = (*cit)->children().begin();
345 wit != (*cit)->children().end(); ++wit) {
346 if ((*wit)->GetProperty(aura::client::kModalKey) ==
347 ui::MODAL_TYPE_SYSTEM && (*wit)->TargetVisibility()) {
348 return true;
352 return false;
355 views::NonClientFrameView* Shell::CreateDefaultNonClientFrameView(
356 views::Widget* widget) {
357 // Use translucent-style window frames for dialogs.
358 return new CustomFrameViewAsh(widget);
361 void Shell::RotateFocus(Direction direction) {
362 focus_cycler_->RotateFocus(direction == FORWARD ? FocusCycler::FORWARD
363 : FocusCycler::BACKWARD);
366 void Shell::SetDisplayWorkAreaInsets(Window* contains,
367 const gfx::Insets& insets) {
368 if (!display_controller_->UpdateWorkAreaOfDisplayNearestWindow(
369 contains, insets)) {
370 return;
372 FOR_EACH_OBSERVER(ShellObserver, observers_,
373 OnDisplayWorkAreaInsetsChanged());
376 void Shell::OnLoginStateChanged(user::LoginStatus status) {
377 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLoginStateChanged(status));
380 void Shell::OnLoginUserProfilePrepared() {
381 CreateShelf();
382 CreateKeyboard();
385 void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status) {
386 RootWindowControllerList controllers = GetAllRootWindowControllers();
387 for (RootWindowControllerList::iterator iter = controllers.begin();
388 iter != controllers.end(); ++iter)
389 (*iter)->UpdateAfterLoginStatusChange(status);
392 void Shell::OnAppTerminating() {
393 FOR_EACH_OBSERVER(ShellObserver, observers_, OnAppTerminating());
396 void Shell::OnLockStateChanged(bool locked) {
397 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLockStateChanged(locked));
398 #ifndef NDEBUG
399 // Make sure that there is no system modal in Lock layer when unlocked.
400 if (!locked) {
401 std::vector<aura::Window*> containers = GetContainersFromAllRootWindows(
402 kShellWindowId_LockSystemModalContainer, GetPrimaryRootWindow());
403 for (std::vector<aura::Window*>::const_iterator iter = containers.begin();
404 iter != containers.end(); ++iter) {
405 DCHECK_EQ(0u, (*iter)->children().size());
408 #endif
411 void Shell::OnCastingSessionStartedOrStopped(bool started) {
412 #if defined(OS_CHROMEOS) && defined(USE_X11)
413 if (projecting_observer_)
414 projecting_observer_->OnCastingSessionStartedOrStopped(started);
415 #endif
418 void Shell::OnOverviewModeStarting() {
419 FOR_EACH_OBSERVER(ShellObserver, observers_, OnOverviewModeStarting());
422 void Shell::OnOverviewModeEnding() {
423 FOR_EACH_OBSERVER(ShellObserver, observers_, OnOverviewModeEnding());
426 void Shell::OnMaximizeModeStarted() {
427 FOR_EACH_OBSERVER(ShellObserver, observers_, OnMaximizeModeStarted());
430 void Shell::OnMaximizeModeEnded() {
431 FOR_EACH_OBSERVER(ShellObserver, observers_, OnMaximizeModeEnded());
434 void Shell::OnRootWindowAdded(aura::Window* root_window) {
435 FOR_EACH_OBSERVER(ShellObserver, observers_, OnRootWindowAdded(root_window));
438 void Shell::CreateShelf() {
439 RootWindowControllerList controllers = GetAllRootWindowControllers();
440 for (RootWindowControllerList::iterator iter = controllers.begin();
441 iter != controllers.end(); ++iter)
442 (*iter)->shelf()->CreateShelf();
445 void Shell::OnShelfCreatedForRootWindow(aura::Window* root_window) {
446 FOR_EACH_OBSERVER(ShellObserver,
447 observers_,
448 OnShelfCreatedForRootWindow(root_window));
451 void Shell::CreateKeyboard() {
452 // TODO(bshe): Primary root window controller may not be the controller to
453 // attach virtual keyboard. See http://crbug.com/303429
454 InitKeyboard();
455 if (keyboard::IsKeyboardUsabilityExperimentEnabled()) {
456 display_controller()->virtual_keyboard_window_controller()->
457 ActivateKeyboard(keyboard::KeyboardController::GetInstance());
458 } else {
459 GetPrimaryRootWindowController()->
460 ActivateKeyboard(keyboard::KeyboardController::GetInstance());
464 void Shell::DeactivateKeyboard() {
465 if (keyboard::KeyboardController::GetInstance()) {
466 RootWindowControllerList controllers = GetAllRootWindowControllers();
467 for (RootWindowControllerList::iterator iter = controllers.begin();
468 iter != controllers.end(); ++iter) {
469 (*iter)->DeactivateKeyboard(keyboard::KeyboardController::GetInstance());
472 keyboard::KeyboardController::ResetInstance(NULL);
475 void Shell::ShowShelf() {
476 RootWindowControllerList controllers = GetAllRootWindowControllers();
477 for (RootWindowControllerList::iterator iter = controllers.begin();
478 iter != controllers.end(); ++iter)
479 (*iter)->ShowShelf();
482 void Shell::AddShellObserver(ShellObserver* observer) {
483 observers_.AddObserver(observer);
486 void Shell::RemoveShellObserver(ShellObserver* observer) {
487 observers_.RemoveObserver(observer);
490 #if defined(OS_CHROMEOS)
491 bool Shell::ShouldSaveDisplaySettings() {
492 return !((maximize_mode_controller_->IsMaximizeModeWindowManagerEnabled() &&
493 maximize_mode_controller_->
494 ignore_display_configuration_updates()) ||
495 resolution_notification_controller_->DoesNotificationTimeout());
497 #endif
499 void Shell::UpdateShelfVisibility() {
500 RootWindowControllerList controllers = GetAllRootWindowControllers();
501 for (RootWindowControllerList::iterator iter = controllers.begin();
502 iter != controllers.end(); ++iter)
503 if ((*iter)->shelf())
504 (*iter)->UpdateShelfVisibility();
507 void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior,
508 aura::Window* root_window) {
509 ash::ShelfLayoutManager::ForShelf(root_window)->SetAutoHideBehavior(behavior);
512 ShelfAutoHideBehavior Shell::GetShelfAutoHideBehavior(
513 aura::Window* root_window) const {
514 return ash::ShelfLayoutManager::ForShelf(root_window)->auto_hide_behavior();
517 void Shell::SetShelfAlignment(ShelfAlignment alignment,
518 aura::Window* root_window) {
519 if (ash::ShelfLayoutManager::ForShelf(root_window)->SetAlignment(alignment)) {
520 FOR_EACH_OBSERVER(
521 ShellObserver, observers_, OnShelfAlignmentChanged(root_window));
525 ShelfAlignment Shell::GetShelfAlignment(const aura::Window* root_window) {
526 return GetRootWindowController(root_window)
527 ->GetShelfLayoutManager()
528 ->GetAlignment();
531 void Shell::SetDimming(bool should_dim) {
532 RootWindowControllerList controllers = GetAllRootWindowControllers();
533 for (RootWindowControllerList::iterator iter = controllers.begin();
534 iter != controllers.end(); ++iter)
535 (*iter)->screen_dimmer()->SetDimming(should_dim);
538 void Shell::NotifyFullscreenStateChange(bool is_fullscreen,
539 aura::Window* root_window) {
540 FOR_EACH_OBSERVER(ShellObserver, observers_, OnFullscreenStateChanged(
541 is_fullscreen, root_window));
544 void Shell::CreateModalBackground(aura::Window* window) {
545 if (!modality_filter_) {
546 modality_filter_.reset(new SystemModalContainerEventFilter(this));
547 AddPreTargetHandler(modality_filter_.get());
549 RootWindowControllerList controllers = GetAllRootWindowControllers();
550 for (RootWindowControllerList::iterator iter = controllers.begin();
551 iter != controllers.end(); ++iter)
552 (*iter)->GetSystemModalLayoutManager(window)->CreateModalBackground();
555 void Shell::OnModalWindowRemoved(aura::Window* removed) {
556 RootWindowControllerList controllers = GetAllRootWindowControllers();
557 bool activated = false;
558 for (RootWindowControllerList::iterator iter = controllers.begin();
559 iter != controllers.end() && !activated; ++iter) {
560 activated = (*iter)->GetSystemModalLayoutManager(removed)->
561 ActivateNextModalWindow();
563 if (!activated) {
564 RemovePreTargetHandler(modality_filter_.get());
565 modality_filter_.reset();
566 for (RootWindowControllerList::iterator iter = controllers.begin();
567 iter != controllers.end(); ++iter)
568 (*iter)->GetSystemModalLayoutManager(removed)->DestroyModalBackground();
572 WebNotificationTray* Shell::GetWebNotificationTray() {
573 return GetPrimaryRootWindowController()->shelf()->
574 status_area_widget()->web_notification_tray();
577 bool Shell::HasPrimaryStatusArea() {
578 ShelfWidget* shelf = GetPrimaryRootWindowController()->shelf();
579 return shelf && shelf->status_area_widget();
582 SystemTray* Shell::GetPrimarySystemTray() {
583 return GetPrimaryRootWindowController()->GetSystemTray();
586 ShelfDelegate* Shell::GetShelfDelegate() {
587 if (!shelf_delegate_) {
588 shelf_model_.reset(new ShelfModel);
589 // Creates ShelfItemDelegateManager before ShelfDelegate.
590 shelf_item_delegate_manager_.reset(
591 new ShelfItemDelegateManager(shelf_model_.get()));
593 shelf_delegate_.reset(delegate_->CreateShelfDelegate(shelf_model_.get()));
594 scoped_ptr<ShelfItemDelegate> controller(new AppListShelfItemDelegate);
596 // Finding the shelf model's location of the app list and setting its
597 // ShelfItemDelegate.
598 int app_list_index = shelf_model_->GetItemIndexForType(TYPE_APP_LIST);
599 DCHECK_GE(app_list_index, 0);
600 ShelfID app_list_id = shelf_model_->items()[app_list_index].id;
601 DCHECK(app_list_id);
602 shelf_item_delegate_manager_->SetShelfItemDelegate(app_list_id,
603 controller.Pass());
604 shelf_window_watcher_.reset(new ShelfWindowWatcher(
605 shelf_model_.get(), shelf_item_delegate_manager_.get()));
607 return shelf_delegate_.get();
610 void Shell::SetTouchHudProjectionEnabled(bool enabled) {
611 if (is_touch_hud_projection_enabled_ == enabled)
612 return;
614 is_touch_hud_projection_enabled_ = enabled;
615 FOR_EACH_OBSERVER(ShellObserver, observers_,
616 OnTouchHudProjectionToggled(enabled));
619 #if defined(OS_CHROMEOS)
620 ash::FirstRunHelper* Shell::CreateFirstRunHelper() {
621 return new ash::FirstRunHelperImpl;
624 void Shell::SetCursorCompositingEnabled(bool enabled) {
625 display_controller_->cursor_window_controller()->SetCursorCompositingEnabled(
626 enabled);
627 native_cursor_manager_->SetNativeCursorEnabled(!enabled);
629 #endif // defined(OS_CHROMEOS)
631 void Shell::DoInitialWorkspaceAnimation() {
632 return GetPrimaryRootWindowController()->workspace_controller()->
633 DoInitialAnimation();
636 ////////////////////////////////////////////////////////////////////////////////
637 // Shell, private:
639 Shell::Shell(ShellDelegate* delegate)
640 : target_root_window_(NULL),
641 scoped_target_root_window_(NULL),
642 delegate_(delegate),
643 window_positioner_(new WindowPositioner),
644 activation_client_(NULL),
645 accelerometer_controller_(new AccelerometerController()),
646 #if defined(OS_CHROMEOS)
647 display_configurator_(new ui::DisplayConfigurator()),
648 #endif // defined(OS_CHROMEOS)
649 native_cursor_manager_(new AshNativeCursorManager),
650 cursor_manager_(
651 scoped_ptr< ::wm::NativeCursorManager>(native_cursor_manager_)),
652 simulate_modal_window_open_for_testing_(false),
653 is_touch_hud_projection_enabled_(false) {
654 DCHECK(delegate_.get());
655 gpu_support_.reset(delegate_->CreateGPUSupport());
656 display_manager_.reset(new DisplayManager);
657 display_controller_.reset(new DisplayController);
658 #if defined(OS_CHROMEOS) && defined(USE_X11)
659 user_metrics_recorder_.reset(new UserMetricsRecorder);
660 #endif // defined(OS_CHROMEOS)
662 #if defined(OS_CHROMEOS)
663 PowerStatus::Initialize();
664 #endif
667 Shell::~Shell() {
668 TRACE_EVENT0("shutdown", "ash::Shell::Destructor");
670 delegate_->PreShutdown();
672 views::FocusManagerFactory::Install(NULL);
674 // Remove the focus from any window. This will prevent overhead and side
675 // effects (e.g. crashes) from changing focus during shutdown.
676 // See bug crbug.com/134502.
677 aura::client::GetFocusClient(GetPrimaryRootWindow())->FocusWindow(NULL);
679 // Please keep in same order as in Init() because it's easy to miss one.
680 if (window_modality_controller_)
681 window_modality_controller_.reset();
682 #if defined(OS_CHROMEOS) && defined(USE_X11)
683 RemovePreTargetHandler(magnifier_key_scroll_handler_.get());
684 magnifier_key_scroll_handler_.reset();
686 RemovePreTargetHandler(speech_feedback_handler_.get());
687 speech_feedback_handler_.reset();
688 #endif
689 RemovePreTargetHandler(user_activity_detector_.get());
690 RemovePreTargetHandler(overlay_filter_.get());
691 RemovePreTargetHandler(input_method_filter_.get());
692 RemovePreTargetHandler(accelerator_filter_.get());
693 RemovePreTargetHandler(event_transformation_handler_.get());
694 RemovePreTargetHandler(toplevel_window_event_handler_.get());
695 RemovePostTargetHandler(toplevel_window_event_handler_.get());
696 RemovePreTargetHandler(system_gesture_filter_.get());
697 RemovePreTargetHandler(keyboard_metrics_filter_.get());
698 RemovePreTargetHandler(mouse_cursor_filter_.get());
700 // TooltipController is deleted with the Shell so removing its references.
701 RemovePreTargetHandler(tooltip_controller_.get());
703 // Destroy maximize mode controller early on since it has some observers which
704 // need to be removed.
705 maximize_mode_controller_->Shutdown();
706 maximize_mode_controller_.reset();
708 // AppList needs to be released before shelf layout manager, which is
709 // destroyed with shelf container in the loop below. However, app list
710 // container is now on top of shelf container and released after it.
711 // TODO(xiyuan): Move it back when app list container is no longer needed.
712 app_list_controller_.reset();
714 #if defined(OS_CHROMEOS)
715 // Destroy the LastWindowClosedLogoutReminder before the
716 // LogoutConfirmationController.
717 last_window_closed_logout_reminder_.reset();
719 // Destroy the LogoutConfirmationController before the SystemTrayDelegate.
720 logout_confirmation_controller_.reset();
721 #endif
723 // Destroy SystemTrayDelegate before destroying the status area(s).
724 system_tray_delegate_->Shutdown();
725 system_tray_delegate_.reset();
727 locale_notification_controller_.reset();
729 // Drag-and-drop must be canceled prior to close all windows.
730 drag_drop_controller_.reset();
732 // Controllers who have WindowObserver added must be deleted
733 // before |display_controller_| is deleted.
735 #if defined(OS_CHROMEOS)
736 // VideoActivityNotifier must be deleted before |video_detector_| is
737 // deleted because it's observing video activity through
738 // VideoDetectorObserver interface.
739 video_activity_notifier_.reset();
740 #endif // defined(OS_CHROMEOS)
741 video_detector_.reset();
742 high_contrast_controller_.reset();
744 shadow_controller_.reset();
745 resize_shadow_controller_.reset();
747 window_cycle_controller_.reset();
748 window_selector_controller_.reset();
749 mru_window_tracker_.reset();
751 // |shelf_window_watcher_| has a weak pointer to |shelf_Model_|
752 // and has window observers.
753 shelf_window_watcher_.reset();
755 // Destroy all child windows including widgets.
756 display_controller_->CloseChildWindows();
757 display_controller_->CloseNonDesktopDisplay();
759 // Chrome implementation of shelf delegate depends on FocusClient,
760 // so must be deleted before |focus_client_|.
761 shelf_delegate_.reset();
762 focus_client_.reset();
764 // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems
765 // needs to remove observers from it.
766 system_tray_notifier_.reset();
768 // These need a valid Shell instance to clean up properly, so explicitly
769 // delete them before invalidating the instance.
770 // Alphabetical. TODO(oshima): sort.
771 magnification_controller_.reset();
772 partial_magnification_controller_.reset();
773 tooltip_controller_.reset();
774 event_client_.reset();
775 nested_accelerator_controller_.reset();
776 toplevel_window_event_handler_.reset();
777 visibility_controller_.reset();
778 // |shelf_item_delegate_manager_| observes |shelf_model_|. It must be
779 // destroyed before |shelf_model_| is destroyed.
780 shelf_item_delegate_manager_.reset();
781 shelf_model_.reset();
783 power_button_controller_.reset();
784 lock_state_controller_.reset();
786 #if defined(OS_CHROMEOS)
787 resolution_notification_controller_.reset();
788 #endif
789 desktop_background_controller_.reset();
790 mouse_cursor_filter_.reset();
792 #if defined(OS_CHROMEOS) && defined(USE_X11)
793 touch_transformer_controller_.reset();
794 #endif // defined(OS_CHROMEOS) && defined(USE_X11)
796 // This also deletes all RootWindows. Note that we invoke Shutdown() on
797 // DisplayController before resetting |display_controller_|, since destruction
798 // of its owned RootWindowControllers relies on the value.
799 display_manager_->CreateScreenForShutdown();
800 display_controller_->Shutdown();
801 display_controller_.reset();
802 screen_position_controller_.reset();
803 accessibility_delegate_.reset();
804 new_window_delegate_.reset();
805 media_delegate_.reset();
807 keyboard::KeyboardController::ResetInstance(NULL);
809 #if defined(OS_CHROMEOS)
810 if (display_change_observer_)
811 display_configurator_->RemoveObserver(display_change_observer_.get());
812 if (display_configurator_animation_)
813 display_configurator_->RemoveObserver(
814 display_configurator_animation_.get());
815 if (display_error_observer_)
816 display_configurator_->RemoveObserver(display_error_observer_.get());
817 if (projecting_observer_)
818 display_configurator_->RemoveObserver(projecting_observer_.get());
819 display_change_observer_.reset();
820 #endif // defined(OS_CHROMEOS)
822 #if defined(OS_CHROMEOS)
823 PowerStatus::Shutdown();
824 #endif
826 DCHECK(instance_ == this);
827 instance_ = NULL;
830 void Shell::Init(const ShellInitParams& init_params) {
831 delegate_->PreInit();
832 if (keyboard::IsKeyboardUsabilityExperimentEnabled()) {
833 display_manager_->SetSecondDisplayMode(DisplayManager::VIRTUAL_KEYBOARD);
835 bool display_initialized = display_manager_->InitFromCommandLine();
836 #if defined(OS_CHROMEOS)
837 display_configurator_->Init(!gpu_support_->IsPanelFittingDisabled());
838 display_configurator_animation_.reset(new DisplayConfiguratorAnimation());
839 display_configurator_->AddObserver(display_configurator_animation_.get());
841 projecting_observer_.reset(new ProjectingObserver());
842 display_configurator_->AddObserver(projecting_observer_.get());
844 if (!display_initialized && base::SysInfo::IsRunningOnChromeOS()) {
845 display_change_observer_.reset(new DisplayChangeObserver);
846 // Register |display_change_observer_| first so that the rest of
847 // observer gets invoked after the root windows are configured.
848 display_configurator_->AddObserver(display_change_observer_.get());
849 display_error_observer_.reset(new DisplayErrorObserver());
850 display_configurator_->AddObserver(display_error_observer_.get());
851 display_configurator_->set_state_controller(display_change_observer_.get());
852 display_configurator_->set_mirroring_controller(display_manager_.get());
853 display_configurator_->ForceInitialConfigure(
854 delegate_->IsFirstRunAfterBoot() ? kChromeOsBootColor : 0);
855 display_initialized = true;
857 #endif // defined(OS_CHROMEOS)
858 if (!display_initialized)
859 display_manager_->InitDefaultDisplay();
861 display_manager_->InitFontParams();
863 // Install the custom factory first so that views::FocusManagers for Tray,
864 // Shelf, and WallPaper could be created by the factory.
865 views::FocusManagerFactory::Install(new AshFocusManagerFactory);
867 aura::Env::CreateInstance(true);
868 aura::Env::GetInstance()->set_context_factory(init_params.context_factory);
870 // The WindowModalityController needs to be at the front of the input event
871 // pretarget handler list to ensure that it processes input events when modal
872 // windows are active.
873 window_modality_controller_.reset(
874 new ::wm::WindowModalityController(this));
876 env_filter_.reset(new ::wm::CompoundEventFilter);
877 AddPreTargetHandler(env_filter_.get());
879 ::wm::FocusController* focus_controller =
880 new ::wm::FocusController(new wm::AshFocusRules);
881 focus_client_.reset(focus_controller);
882 activation_client_ = focus_controller;
883 activation_client_->AddObserver(this);
884 focus_cycler_.reset(new FocusCycler());
886 screen_position_controller_.reset(new ScreenPositionController);
888 display_controller_->Start();
889 display_controller_->CreatePrimaryHost(
890 ShellInitParamsToAshWindowTreeHostInitParams(init_params));
891 aura::Window* root_window = display_controller_->GetPrimaryRootWindow();
892 target_root_window_ = root_window;
894 #if defined(OS_CHROMEOS)
895 resolution_notification_controller_.reset(
896 new ResolutionNotificationController);
897 #endif
899 cursor_manager_.SetDisplay(GetScreen()->GetPrimaryDisplay());
901 nested_accelerator_controller_.reset(
902 new ::wm::NestedAcceleratorController(new NestedAcceleratorDelegate));
903 accelerator_controller_.reset(new AcceleratorController);
904 maximize_mode_controller_.reset(new MaximizeModeController());
906 #if defined(OS_CHROMEOS) && defined(USE_X11)
907 magnifier_key_scroll_handler_ = MagnifierKeyScroller::CreateHandler().Pass();
908 AddPreTargetHandler(magnifier_key_scroll_handler_.get());
909 speech_feedback_handler_ = SpokenFeedbackToggler::CreateHandler().Pass();
910 AddPreTargetHandler(speech_feedback_handler_.get());
911 #endif
913 // The order in which event filters are added is significant.
915 // wm::UserActivityDetector passes events to observers, so let them get
916 // rewritten first.
917 user_activity_detector_.reset(new ::wm::UserActivityDetector);
918 AddPreTargetHandler(user_activity_detector_.get());
920 overlay_filter_.reset(new OverlayEventFilter);
921 AddPreTargetHandler(overlay_filter_.get());
922 AddShellObserver(overlay_filter_.get());
924 input_method_filter_.reset(new ::wm::InputMethodEventFilter(
925 root_window->GetHost()->GetAcceleratedWidget()));
926 AddPreTargetHandler(input_method_filter_.get());
928 accelerator_filter_.reset(new ::wm::AcceleratorFilter(
929 scoped_ptr< ::wm::AcceleratorDelegate>(new AcceleratorDelegate).Pass()));
930 AddPreTargetHandler(accelerator_filter_.get());
932 event_transformation_handler_.reset(new EventTransformationHandler);
933 AddPreTargetHandler(event_transformation_handler_.get());
935 toplevel_window_event_handler_.reset(new ToplevelWindowEventHandler);
937 system_gesture_filter_.reset(new SystemGestureEventFilter);
938 AddPreTargetHandler(system_gesture_filter_.get());
940 keyboard_metrics_filter_.reset(new KeyboardUMAEventFilter);
941 AddPreTargetHandler(keyboard_metrics_filter_.get());
943 // The keyboard system must be initialized before the RootWindowController is
944 // created.
945 #if defined(OS_CHROMEOS)
946 keyboard::InitializeKeyboard();
947 #endif
949 #if defined(OS_CHROMEOS)
950 sticky_keys_controller_.reset(new StickyKeysController);
951 #endif
953 lock_state_controller_.reset(new LockStateController);
954 power_button_controller_.reset(new PowerButtonController(
955 lock_state_controller_.get()));
956 #if defined(OS_CHROMEOS)
957 // Pass the initial display state to PowerButtonController.
958 power_button_controller_->OnDisplayModeChanged(
959 display_configurator_->cached_displays());
960 #endif
961 AddShellObserver(lock_state_controller_.get());
963 drag_drop_controller_.reset(new DragDropController);
964 mouse_cursor_filter_.reset(new MouseCursorEventFilter());
965 PrependPreTargetHandler(mouse_cursor_filter_.get());
967 // Create Controllers that may need root window.
968 // TODO(oshima): Move as many controllers before creating
969 // RootWindowController as possible.
970 visibility_controller_.reset(new AshVisibilityController);
972 magnification_controller_.reset(
973 MagnificationController::CreateInstance());
974 mru_window_tracker_.reset(new MruWindowTracker(activation_client_));
976 partial_magnification_controller_.reset(
977 new PartialMagnificationController());
979 autoclick_controller_.reset(AutoclickController::CreateInstance());
981 high_contrast_controller_.reset(new HighContrastController);
982 video_detector_.reset(new VideoDetector);
983 window_selector_controller_.reset(new WindowSelectorController());
984 window_cycle_controller_.reset(new WindowCycleController());
986 tooltip_controller_.reset(
987 new views::corewm::TooltipController(
988 scoped_ptr<views::corewm::Tooltip>(
989 new views::corewm::TooltipAura(gfx::SCREEN_TYPE_ALTERNATE))));
990 AddPreTargetHandler(tooltip_controller_.get());
992 event_client_.reset(new EventClientImpl);
994 // This controller needs to be set before SetupManagedWindowMode.
995 desktop_background_controller_.reset(new DesktopBackgroundController());
996 user_wallpaper_delegate_.reset(delegate_->CreateUserWallpaperDelegate());
998 session_state_delegate_.reset(delegate_->CreateSessionStateDelegate());
999 accessibility_delegate_.reset(delegate_->CreateAccessibilityDelegate());
1000 new_window_delegate_.reset(delegate_->CreateNewWindowDelegate());
1001 media_delegate_.reset(delegate_->CreateMediaDelegate());
1003 resize_shadow_controller_.reset(new ResizeShadowController());
1004 shadow_controller_.reset(
1005 new ::wm::ShadowController(activation_client_));
1007 // Create system_tray_notifier_ before the delegate.
1008 system_tray_notifier_.reset(new ash::SystemTrayNotifier());
1010 // Initialize system_tray_delegate_ before initializing StatusAreaWidget.
1011 system_tray_delegate_.reset(delegate()->CreateSystemTrayDelegate());
1012 DCHECK(system_tray_delegate_.get());
1014 locale_notification_controller_.reset(new LocaleNotificationController);
1016 // Initialize system_tray_delegate_ after StatusAreaWidget is created.
1017 system_tray_delegate_->Initialize();
1019 #if defined(OS_CHROMEOS)
1020 // Create the LogoutConfirmationController after the SystemTrayDelegate.
1021 logout_confirmation_controller_.reset(new LogoutConfirmationController(
1022 base::Bind(&SystemTrayDelegate::SignOut,
1023 base::Unretained(system_tray_delegate_.get()))));
1024 #endif
1026 #if defined(OS_CHROMEOS) && defined(USE_X11)
1027 // Create TouchTransformerController before DisplayController::InitDisplays()
1028 // since TouchTransformerController listens on
1029 // DisplayController::Observer::OnDisplaysInitialized().
1030 touch_transformer_controller_.reset(new TouchTransformerController());
1031 #endif // defined(OS_CHROMEOS) && defined(USE_X11)
1033 display_controller_->InitDisplays();
1035 // It needs to be created after RootWindowController has been created
1036 // (which calls OnWindowResized has been called, otherwise the
1037 // widget will not paint when restoring after a browser crash. Also it needs
1038 // to be created after InitSecondaryDisplays() to initialize the wallpapers in
1039 // the correct size.
1040 user_wallpaper_delegate_->InitializeWallpaper();
1042 if (initially_hide_cursor_)
1043 cursor_manager_.HideCursor();
1044 cursor_manager_.SetCursor(ui::kCursorPointer);
1046 #if defined(OS_CHROMEOS)
1047 // Set accelerator controller delegates.
1048 accelerator_controller_->SetBrightnessControlDelegate(
1049 scoped_ptr<ash::BrightnessControlDelegate>(
1050 new ash::system::BrightnessControllerChromeos).Pass());
1052 power_event_observer_.reset(new PowerEventObserver());
1053 user_activity_notifier_.reset(
1054 new ui::UserActivityPowerManagerNotifier(user_activity_detector_.get()));
1055 video_activity_notifier_.reset(
1056 new VideoActivityNotifier(video_detector_.get()));
1057 bluetooth_notification_controller_.reset(new BluetoothNotificationController);
1058 last_window_closed_logout_reminder_.reset(new LastWindowClosedLogoutReminder);
1059 #endif
1061 weak_display_manager_factory_.reset(
1062 new base::WeakPtrFactory<DisplayManager>(display_manager_.get()));
1063 // The compositor thread and main message loop have to be running in
1064 // order to create mirror window. Run it after the main message loop
1065 // is started.
1066 base::MessageLoopForUI::current()->PostTask(
1067 FROM_HERE,
1068 base::Bind(&DisplayManager::CreateMirrorWindowIfAny,
1069 weak_display_manager_factory_->GetWeakPtr()));
1072 void Shell::InitKeyboard() {
1073 if (keyboard::IsKeyboardEnabled()) {
1074 if (keyboard::KeyboardController::GetInstance()) {
1075 RootWindowControllerList controllers = GetAllRootWindowControllers();
1076 for (RootWindowControllerList::iterator iter = controllers.begin();
1077 iter != controllers.end(); ++iter) {
1078 (*iter)->DeactivateKeyboard(
1079 keyboard::KeyboardController::GetInstance());
1082 keyboard::KeyboardControllerProxy* proxy =
1083 delegate_->CreateKeyboardControllerProxy();
1084 keyboard::KeyboardController::ResetInstance(
1085 new keyboard::KeyboardController(proxy));
1089 void Shell::InitRootWindow(aura::Window* root_window) {
1090 DCHECK(activation_client_);
1091 DCHECK(visibility_controller_.get());
1092 DCHECK(drag_drop_controller_.get());
1094 aura::client::SetFocusClient(root_window, focus_client_.get());
1095 input_method_filter_->SetInputMethodPropertyInRootWindow(root_window);
1096 aura::client::SetActivationClient(root_window, activation_client_);
1097 ::wm::FocusController* focus_controller =
1098 static_cast< ::wm::FocusController*>(activation_client_);
1099 root_window->AddPreTargetHandler(focus_controller);
1100 aura::client::SetVisibilityClient(root_window, visibility_controller_.get());
1101 aura::client::SetDragDropClient(root_window, drag_drop_controller_.get());
1102 aura::client::SetScreenPositionClient(root_window,
1103 screen_position_controller_.get());
1104 aura::client::SetCursorClient(root_window, &cursor_manager_);
1105 aura::client::SetTooltipClient(root_window, tooltip_controller_.get());
1106 aura::client::SetEventClient(root_window, event_client_.get());
1108 aura::client::SetWindowMoveClient(root_window,
1109 toplevel_window_event_handler_.get());
1110 root_window->AddPreTargetHandler(toplevel_window_event_handler_.get());
1111 root_window->AddPostTargetHandler(toplevel_window_event_handler_.get());
1113 if (nested_accelerator_controller_) {
1114 aura::client::SetDispatcherClient(root_window,
1115 nested_accelerator_controller_.get());
1119 bool Shell::CanWindowReceiveEvents(aura::Window* window) {
1120 RootWindowControllerList controllers = GetAllRootWindowControllers();
1121 for (RootWindowControllerList::iterator iter = controllers.begin();
1122 iter != controllers.end(); ++iter) {
1123 SystemModalContainerLayoutManager* layout_manager =
1124 (*iter)->GetSystemModalLayoutManager(window);
1125 if (layout_manager && layout_manager->CanWindowReceiveEvents(window))
1126 return true;
1127 // Allow events to fall through to the virtual keyboard even if displaying
1128 // a system modal dialog.
1129 if ((*iter)->IsVirtualKeyboardWindow(window))
1130 return true;
1133 return false;
1136 ////////////////////////////////////////////////////////////////////////////////
1137 // Shell, ui::EventTarget overrides:
1139 bool Shell::CanAcceptEvent(const ui::Event& event) {
1140 return true;
1143 ui::EventTarget* Shell::GetParentTarget() {
1144 return aura::Env::GetInstance();
1147 scoped_ptr<ui::EventTargetIterator> Shell::GetChildIterator() const {
1148 return scoped_ptr<ui::EventTargetIterator>();
1151 ui::EventTargeter* Shell::GetEventTargeter() {
1152 NOTREACHED();
1153 return NULL;
1156 void Shell::OnEvent(ui::Event* event) {
1159 ////////////////////////////////////////////////////////////////////////////////
1160 // Shell, aura::client::ActivationChangeObserver implementation:
1162 void Shell::OnWindowActivated(aura::Window* gained_active,
1163 aura::Window* lost_active) {
1164 if (gained_active)
1165 target_root_window_ = gained_active->GetRootWindow();
1168 } // namespace ash