Linux Sandbox: make sure Credentials test pass on TSAN
[chromium-blink-merge.git] / ash / shell.cc
blobee93a3421deaaf6943c754ee4addbe33574dd964
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/screen_orientation_controller_chromeos.h"
125 #include "ash/display/display_change_observer_chromeos.h"
126 #include "ash/display/display_configurator_animation.h"
127 #include "ash/display/display_error_observer_chromeos.h"
128 #include "ash/display/projecting_observer_chromeos.h"
129 #include "ash/display/resolution_notification_controller.h"
130 #include "ash/sticky_keys/sticky_keys_controller.h"
131 #include "ash/system/chromeos/bluetooth/bluetooth_notification_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/video_activity_notifier.h"
136 #include "ash/system/chromeos/session/last_window_closed_logout_reminder.h"
137 #include "ash/system/chromeos/session/logout_confirmation_controller.h"
138 #include "ash/touch/touch_transformer_controller.h"
139 #include "ash/virtual_keyboard_controller.h"
140 #include "base/bind_helpers.h"
141 #include "base/sys_info.h"
142 #include "chromeos/dbus/dbus_thread_manager.h"
143 #include "ui/chromeos/user_activity_power_manager_notifier.h"
144 #include "ui/display/chromeos/display_configurator.h"
145 #endif // defined(OS_CHROMEOS)
147 namespace ash {
149 namespace {
151 using aura::Window;
152 using views::Widget;
154 // A Corewm VisibilityController subclass that calls the Ash animation routine
155 // so we can pick up our extended animations. See ash/wm/window_animations.h.
156 class AshVisibilityController : public ::wm::VisibilityController {
157 public:
158 AshVisibilityController() {}
159 ~AshVisibilityController() override {}
161 private:
162 // Overridden from ::wm::VisibilityController:
163 bool CallAnimateOnChildWindowVisibilityChanged(aura::Window* window,
164 bool visible) override {
165 return AnimateOnChildWindowVisibilityChanged(window, visible);
168 DISALLOW_COPY_AND_ASSIGN(AshVisibilityController);
171 AshWindowTreeHostInitParams ShellInitParamsToAshWindowTreeHostInitParams(
172 const ShellInitParams& shell_init_params) {
173 AshWindowTreeHostInitParams ash_init_params;
174 #if defined(OS_WIN)
175 ash_init_params.remote_hwnd = shell_init_params.remote_hwnd;
176 #endif
177 return ash_init_params;
180 } // namespace
182 // static
183 Shell* Shell::instance_ = NULL;
184 // static
185 bool Shell::initially_hide_cursor_ = false;
187 ////////////////////////////////////////////////////////////////////////////////
188 // Shell, public:
190 // static
191 Shell* Shell::CreateInstance(const ShellInitParams& init_params) {
192 CHECK(!instance_);
193 instance_ = new Shell(init_params.delegate);
194 instance_->Init(init_params);
195 return instance_;
198 // static
199 Shell* Shell::GetInstance() {
200 CHECK(instance_);
201 return instance_;
204 // static
205 bool Shell::HasInstance() {
206 return !!instance_;
209 // static
210 void Shell::DeleteInstance() {
211 delete instance_;
212 instance_ = NULL;
215 // static
216 RootWindowController* Shell::GetPrimaryRootWindowController() {
217 CHECK(HasInstance());
218 return GetRootWindowController(GetPrimaryRootWindow());
221 // static
222 Shell::RootWindowControllerList Shell::GetAllRootWindowControllers() {
223 CHECK(HasInstance());
224 return Shell::GetInstance()->display_controller()->
225 GetAllRootWindowControllers();
228 // static
229 aura::Window* Shell::GetPrimaryRootWindow() {
230 CHECK(HasInstance());
231 return GetInstance()->display_controller()->GetPrimaryRootWindow();
234 // static
235 aura::Window* Shell::GetTargetRootWindow() {
236 CHECK(HasInstance());
237 Shell* shell = GetInstance();
238 if (shell->scoped_target_root_window_)
239 return shell->scoped_target_root_window_;
240 return shell->target_root_window_;
243 // static
244 gfx::Screen* Shell::GetScreen() {
245 return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE);
248 // static
249 aura::Window::Windows Shell::GetAllRootWindows() {
250 CHECK(HasInstance());
251 return Shell::GetInstance()->display_controller()->
252 GetAllRootWindows();
255 // static
256 aura::Window* Shell::GetContainer(aura::Window* root_window,
257 int container_id) {
258 return root_window->GetChildById(container_id);
261 // static
262 const aura::Window* Shell::GetContainer(const aura::Window* root_window,
263 int container_id) {
264 return root_window->GetChildById(container_id);
267 // static
268 std::vector<aura::Window*> Shell::GetContainersFromAllRootWindows(
269 int container_id,
270 aura::Window* priority_root) {
271 std::vector<aura::Window*> containers;
272 aura::Window::Windows root_windows = GetAllRootWindows();
273 for (aura::Window::Windows::const_iterator it = root_windows.begin();
274 it != root_windows.end(); ++it) {
275 aura::Window* container = (*it)->GetChildById(container_id);
276 if (container) {
277 if (priority_root && priority_root->Contains(container))
278 containers.insert(containers.begin(), container);
279 else
280 containers.push_back(container);
283 return containers;
286 void Shell::ShowContextMenu(const gfx::Point& location_in_screen,
287 ui::MenuSourceType source_type) {
288 // No context menus if there is no session with an active user.
289 if (!session_state_delegate_->NumberOfLoggedInUsers())
290 return;
291 // No context menus when screen is locked.
292 if (session_state_delegate_->IsScreenLocked())
293 return;
295 aura::Window* root =
296 wm::GetRootWindowMatching(gfx::Rect(location_in_screen, gfx::Size()));
297 GetRootWindowController(root)
298 ->ShowContextMenu(location_in_screen, source_type);
301 void Shell::ShowAppList(aura::Window* window) {
302 // If the context window is not given, show it on the target root window.
303 if (!window)
304 window = GetTargetRootWindow();
305 if (!app_list_controller_)
306 app_list_controller_.reset(new AppListController);
307 app_list_controller_->Show(window);
310 void Shell::DismissAppList() {
311 if (!app_list_controller_)
312 return;
313 app_list_controller_->Dismiss();
316 void Shell::ToggleAppList(aura::Window* window) {
317 if (app_list_controller_ && app_list_controller_->IsVisible()) {
318 DismissAppList();
319 return;
322 ShowAppList(window);
325 bool Shell::GetAppListTargetVisibility() const {
326 return app_list_controller_.get() &&
327 app_list_controller_->GetTargetVisibility();
330 aura::Window* Shell::GetAppListWindow() {
331 return app_list_controller_.get() ? app_list_controller_->GetWindow() : NULL;
334 app_list::AppListView* Shell::GetAppListView() {
335 return app_list_controller_.get() ? app_list_controller_->GetView() : NULL;
338 bool Shell::IsSystemModalWindowOpen() const {
339 if (simulate_modal_window_open_for_testing_)
340 return true;
341 const std::vector<aura::Window*> containers = GetContainersFromAllRootWindows(
342 kShellWindowId_SystemModalContainer, NULL);
343 for (std::vector<aura::Window*>::const_iterator cit = containers.begin();
344 cit != containers.end(); ++cit) {
345 for (aura::Window::Windows::const_iterator wit = (*cit)->children().begin();
346 wit != (*cit)->children().end(); ++wit) {
347 if ((*wit)->GetProperty(aura::client::kModalKey) ==
348 ui::MODAL_TYPE_SYSTEM && (*wit)->TargetVisibility()) {
349 return true;
353 return false;
356 views::NonClientFrameView* Shell::CreateDefaultNonClientFrameView(
357 views::Widget* widget) {
358 // Use translucent-style window frames for dialogs.
359 return new CustomFrameViewAsh(widget);
362 void Shell::RotateFocus(Direction direction) {
363 focus_cycler_->RotateFocus(direction == FORWARD ? FocusCycler::FORWARD
364 : FocusCycler::BACKWARD);
367 void Shell::SetDisplayWorkAreaInsets(Window* contains,
368 const gfx::Insets& insets) {
369 if (!display_controller_->UpdateWorkAreaOfDisplayNearestWindow(
370 contains, insets)) {
371 return;
373 FOR_EACH_OBSERVER(ShellObserver, observers_,
374 OnDisplayWorkAreaInsetsChanged());
377 void Shell::OnLoginStateChanged(user::LoginStatus status) {
378 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLoginStateChanged(status));
381 void Shell::OnLoginUserProfilePrepared() {
382 CreateShelf();
383 CreateKeyboard();
386 void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status) {
387 RootWindowControllerList controllers = GetAllRootWindowControllers();
388 for (RootWindowControllerList::iterator iter = controllers.begin();
389 iter != controllers.end(); ++iter)
390 (*iter)->UpdateAfterLoginStatusChange(status);
393 void Shell::OnAppTerminating() {
394 FOR_EACH_OBSERVER(ShellObserver, observers_, OnAppTerminating());
397 void Shell::OnLockStateChanged(bool locked) {
398 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLockStateChanged(locked));
399 #ifndef NDEBUG
400 // Make sure that there is no system modal in Lock layer when unlocked.
401 if (!locked) {
402 std::vector<aura::Window*> containers = GetContainersFromAllRootWindows(
403 kShellWindowId_LockSystemModalContainer, GetPrimaryRootWindow());
404 for (std::vector<aura::Window*>::const_iterator iter = containers.begin();
405 iter != containers.end(); ++iter) {
406 DCHECK_EQ(0u, (*iter)->children().size());
409 #endif
412 void Shell::OnCastingSessionStartedOrStopped(bool started) {
413 #if defined(OS_CHROMEOS)
414 if (projecting_observer_)
415 projecting_observer_->OnCastingSessionStartedOrStopped(started);
416 #endif
419 void Shell::OnOverviewModeStarting() {
420 FOR_EACH_OBSERVER(ShellObserver, observers_, OnOverviewModeStarting());
423 void Shell::OnOverviewModeEnded() {
424 FOR_EACH_OBSERVER(ShellObserver, observers_, OnOverviewModeEnded());
427 void Shell::OnMaximizeModeStarted() {
428 FOR_EACH_OBSERVER(ShellObserver, observers_, OnMaximizeModeStarted());
431 void Shell::OnMaximizeModeEnded() {
432 FOR_EACH_OBSERVER(ShellObserver, observers_, OnMaximizeModeEnded());
435 void Shell::OnRootWindowAdded(aura::Window* root_window) {
436 FOR_EACH_OBSERVER(ShellObserver, observers_, OnRootWindowAdded(root_window));
439 void Shell::CreateShelf() {
440 RootWindowControllerList controllers = GetAllRootWindowControllers();
441 for (RootWindowControllerList::iterator iter = controllers.begin();
442 iter != controllers.end(); ++iter)
443 (*iter)->shelf()->CreateShelf();
446 void Shell::OnShelfCreatedForRootWindow(aura::Window* root_window) {
447 FOR_EACH_OBSERVER(ShellObserver,
448 observers_,
449 OnShelfCreatedForRootWindow(root_window));
452 void Shell::CreateKeyboard() {
453 // TODO(bshe): Primary root window controller may not be the controller to
454 // attach virtual keyboard. See http://crbug.com/303429
455 InitKeyboard();
456 GetPrimaryRootWindowController()->
457 ActivateKeyboard(keyboard::KeyboardController::GetInstance());
460 void Shell::DeactivateKeyboard() {
461 if (keyboard::KeyboardController::GetInstance()) {
462 RootWindowControllerList controllers = GetAllRootWindowControllers();
463 for (RootWindowControllerList::iterator iter = controllers.begin();
464 iter != controllers.end(); ++iter) {
465 (*iter)->DeactivateKeyboard(keyboard::KeyboardController::GetInstance());
468 keyboard::KeyboardController::ResetInstance(NULL);
471 void Shell::ShowShelf() {
472 RootWindowControllerList controllers = GetAllRootWindowControllers();
473 for (RootWindowControllerList::iterator iter = controllers.begin();
474 iter != controllers.end(); ++iter)
475 (*iter)->ShowShelf();
478 void Shell::AddShellObserver(ShellObserver* observer) {
479 observers_.AddObserver(observer);
482 void Shell::RemoveShellObserver(ShellObserver* observer) {
483 observers_.RemoveObserver(observer);
486 #if defined(OS_CHROMEOS)
487 bool Shell::ShouldSaveDisplaySettings() {
488 return !(screen_orientation_controller_
489 ->ignore_display_configuration_updates() ||
490 resolution_notification_controller_->DoesNotificationTimeout());
492 #endif
494 void Shell::UpdateShelfVisibility() {
495 RootWindowControllerList controllers = GetAllRootWindowControllers();
496 for (RootWindowControllerList::iterator iter = controllers.begin();
497 iter != controllers.end(); ++iter)
498 if ((*iter)->shelf())
499 (*iter)->UpdateShelfVisibility();
502 void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior,
503 aura::Window* root_window) {
504 ash::ShelfLayoutManager::ForShelf(root_window)->SetAutoHideBehavior(behavior);
507 ShelfAutoHideBehavior Shell::GetShelfAutoHideBehavior(
508 aura::Window* root_window) const {
509 return ash::ShelfLayoutManager::ForShelf(root_window)->auto_hide_behavior();
512 void Shell::SetShelfAlignment(ShelfAlignment alignment,
513 aura::Window* root_window) {
514 if (ash::ShelfLayoutManager::ForShelf(root_window)->SetAlignment(alignment)) {
515 FOR_EACH_OBSERVER(
516 ShellObserver, observers_, OnShelfAlignmentChanged(root_window));
520 ShelfAlignment Shell::GetShelfAlignment(const aura::Window* root_window) {
521 return GetRootWindowController(root_window)
522 ->GetShelfLayoutManager()
523 ->GetAlignment();
526 void Shell::SetDimming(bool should_dim) {
527 RootWindowControllerList controllers = GetAllRootWindowControllers();
528 for (RootWindowControllerList::iterator iter = controllers.begin();
529 iter != controllers.end(); ++iter)
530 (*iter)->screen_dimmer()->SetDimming(should_dim);
533 void Shell::NotifyFullscreenStateChange(bool is_fullscreen,
534 aura::Window* root_window) {
535 FOR_EACH_OBSERVER(ShellObserver, observers_, OnFullscreenStateChanged(
536 is_fullscreen, root_window));
539 void Shell::CreateModalBackground(aura::Window* window) {
540 if (!modality_filter_) {
541 modality_filter_.reset(new SystemModalContainerEventFilter(this));
542 AddPreTargetHandler(modality_filter_.get());
544 RootWindowControllerList controllers = GetAllRootWindowControllers();
545 for (RootWindowControllerList::iterator iter = controllers.begin();
546 iter != controllers.end(); ++iter)
547 (*iter)->GetSystemModalLayoutManager(window)->CreateModalBackground();
550 void Shell::OnModalWindowRemoved(aura::Window* removed) {
551 RootWindowControllerList controllers = GetAllRootWindowControllers();
552 bool activated = false;
553 for (RootWindowControllerList::iterator iter = controllers.begin();
554 iter != controllers.end() && !activated; ++iter) {
555 activated = (*iter)->GetSystemModalLayoutManager(removed)->
556 ActivateNextModalWindow();
558 if (!activated) {
559 RemovePreTargetHandler(modality_filter_.get());
560 modality_filter_.reset();
561 for (RootWindowControllerList::iterator iter = controllers.begin();
562 iter != controllers.end(); ++iter)
563 (*iter)->GetSystemModalLayoutManager(removed)->DestroyModalBackground();
567 WebNotificationTray* Shell::GetWebNotificationTray() {
568 return GetPrimaryRootWindowController()->shelf()->
569 status_area_widget()->web_notification_tray();
572 bool Shell::HasPrimaryStatusArea() {
573 ShelfWidget* shelf = GetPrimaryRootWindowController()->shelf();
574 return shelf && shelf->status_area_widget();
577 SystemTray* Shell::GetPrimarySystemTray() {
578 return GetPrimaryRootWindowController()->GetSystemTray();
581 ShelfDelegate* Shell::GetShelfDelegate() {
582 if (!shelf_delegate_) {
583 shelf_model_.reset(new ShelfModel);
584 // Creates ShelfItemDelegateManager before ShelfDelegate.
585 shelf_item_delegate_manager_.reset(
586 new ShelfItemDelegateManager(shelf_model_.get()));
588 shelf_delegate_.reset(delegate_->CreateShelfDelegate(shelf_model_.get()));
589 scoped_ptr<ShelfItemDelegate> controller(new AppListShelfItemDelegate);
591 // Finding the shelf model's location of the app list and setting its
592 // ShelfItemDelegate.
593 int app_list_index = shelf_model_->GetItemIndexForType(TYPE_APP_LIST);
594 DCHECK_GE(app_list_index, 0);
595 ShelfID app_list_id = shelf_model_->items()[app_list_index].id;
596 DCHECK(app_list_id);
597 shelf_item_delegate_manager_->SetShelfItemDelegate(app_list_id,
598 controller.Pass());
599 shelf_window_watcher_.reset(new ShelfWindowWatcher(
600 shelf_model_.get(), shelf_item_delegate_manager_.get()));
602 return shelf_delegate_.get();
605 void Shell::SetTouchHudProjectionEnabled(bool enabled) {
606 if (is_touch_hud_projection_enabled_ == enabled)
607 return;
609 is_touch_hud_projection_enabled_ = enabled;
610 FOR_EACH_OBSERVER(ShellObserver, observers_,
611 OnTouchHudProjectionToggled(enabled));
614 #if defined(OS_CHROMEOS)
615 ash::FirstRunHelper* Shell::CreateFirstRunHelper() {
616 return new ash::FirstRunHelperImpl;
619 void Shell::SetCursorCompositingEnabled(bool enabled) {
620 display_controller_->cursor_window_controller()->SetCursorCompositingEnabled(
621 enabled);
622 native_cursor_manager_->SetNativeCursorEnabled(!enabled);
624 #endif // defined(OS_CHROMEOS)
626 void Shell::DoInitialWorkspaceAnimation() {
627 return GetPrimaryRootWindowController()->workspace_controller()->
628 DoInitialAnimation();
631 ////////////////////////////////////////////////////////////////////////////////
632 // Shell, private:
634 Shell::Shell(ShellDelegate* delegate)
635 : target_root_window_(NULL),
636 scoped_target_root_window_(NULL),
637 delegate_(delegate),
638 window_positioner_(new WindowPositioner),
639 activation_client_(NULL),
640 #if defined(OS_CHROMEOS)
641 display_configurator_(new ui::DisplayConfigurator()),
642 #endif // defined(OS_CHROMEOS)
643 native_cursor_manager_(new AshNativeCursorManager),
644 cursor_manager_(
645 scoped_ptr<::wm::NativeCursorManager>(native_cursor_manager_)),
646 simulate_modal_window_open_for_testing_(false),
647 is_touch_hud_projection_enabled_(false) {
648 DCHECK(delegate_.get());
649 gpu_support_.reset(delegate_->CreateGPUSupport());
650 display_manager_.reset(new DisplayManager);
651 display_controller_.reset(new DisplayController);
652 user_metrics_recorder_.reset(new UserMetricsRecorder);
654 #if defined(OS_CHROMEOS)
655 PowerStatus::Initialize();
656 #endif
659 Shell::~Shell() {
660 TRACE_EVENT0("shutdown", "ash::Shell::Destructor");
662 delegate_->PreShutdown();
664 views::FocusManagerFactory::Install(NULL);
666 // Remove the focus from any window. This will prevent overhead and side
667 // effects (e.g. crashes) from changing focus during shutdown.
668 // See bug crbug.com/134502.
669 aura::client::GetFocusClient(GetPrimaryRootWindow())->FocusWindow(NULL);
671 // Please keep in same order as in Init() because it's easy to miss one.
672 if (window_modality_controller_)
673 window_modality_controller_.reset();
674 #if defined(OS_CHROMEOS)
675 RemovePreTargetHandler(magnifier_key_scroll_handler_.get());
676 magnifier_key_scroll_handler_.reset();
678 RemovePreTargetHandler(speech_feedback_handler_.get());
679 speech_feedback_handler_.reset();
680 #endif
681 RemovePreTargetHandler(user_activity_detector_.get());
682 RemovePreTargetHandler(overlay_filter_.get());
683 RemovePreTargetHandler(input_method_filter_.get());
684 RemovePreTargetHandler(accelerator_filter_.get());
685 RemovePreTargetHandler(event_transformation_handler_.get());
686 RemovePreTargetHandler(toplevel_window_event_handler_.get());
687 RemovePostTargetHandler(toplevel_window_event_handler_.get());
688 RemovePreTargetHandler(system_gesture_filter_.get());
689 RemovePreTargetHandler(keyboard_metrics_filter_.get());
690 RemovePreTargetHandler(mouse_cursor_filter_.get());
692 // TooltipController is deleted with the Shell so removing its references.
693 RemovePreTargetHandler(tooltip_controller_.get());
695 #if defined(OS_CHROMEOS)
696 screen_orientation_controller_.reset();
697 #endif
699 // Destroy the virtual keyboard controller before the maximize mode controller
700 // since the latters destructor triggers events that the former is listening
701 // to but no longer cares about.
702 #if defined(OS_CHROMEOS)
703 virtual_keyboard_controller_.reset();
704 #endif
706 // Destroy maximize mode controller early on since it has some observers which
707 // need to be removed.
708 maximize_mode_controller_.reset();
710 // AppList needs to be released before shelf layout manager, which is
711 // destroyed with shelf container in the loop below. However, app list
712 // container is now on top of shelf container and released after it.
713 // TODO(xiyuan): Move it back when app list container is no longer needed.
714 app_list_controller_.reset();
716 #if defined(OS_CHROMEOS)
717 // Destroy the LastWindowClosedLogoutReminder before the
718 // LogoutConfirmationController.
719 last_window_closed_logout_reminder_.reset();
721 // Destroy the LogoutConfirmationController before the SystemTrayDelegate.
722 logout_confirmation_controller_.reset();
723 #endif
725 // Destroy SystemTrayDelegate before destroying the status area(s).
726 system_tray_delegate_->Shutdown();
727 system_tray_delegate_.reset();
729 locale_notification_controller_.reset();
731 // Drag-and-drop must be canceled prior to close all windows.
732 drag_drop_controller_.reset();
734 // Controllers who have WindowObserver added must be deleted
735 // before |display_controller_| is deleted.
737 #if defined(OS_CHROMEOS)
738 // VideoActivityNotifier must be deleted before |video_detector_| is
739 // deleted because it's observing video activity through
740 // VideoDetectorObserver interface.
741 video_activity_notifier_.reset();
742 #endif // defined(OS_CHROMEOS)
743 video_detector_.reset();
744 high_contrast_controller_.reset();
746 shadow_controller_.reset();
747 resize_shadow_controller_.reset();
749 window_cycle_controller_.reset();
750 window_selector_controller_.reset();
751 mru_window_tracker_.reset();
753 // |shelf_window_watcher_| has a weak pointer to |shelf_Model_|
754 // and has window observers.
755 shelf_window_watcher_.reset();
757 // Destroy all child windows including widgets.
758 display_controller_->CloseChildWindows();
759 display_controller_->CloseMirroringDisplay();
761 // Chrome implementation of shelf delegate depends on FocusClient,
762 // so must be deleted before |focus_client_|.
763 shelf_delegate_.reset();
764 focus_client_.reset();
766 // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems
767 // needs to remove observers from it.
768 system_tray_notifier_.reset();
770 // These need a valid Shell instance to clean up properly, so explicitly
771 // delete them before invalidating the instance.
772 // Alphabetical. TODO(oshima): sort.
773 magnification_controller_.reset();
774 partial_magnification_controller_.reset();
775 tooltip_controller_.reset();
776 event_client_.reset();
777 nested_accelerator_controller_.reset();
778 toplevel_window_event_handler_.reset();
779 visibility_controller_.reset();
780 // |shelf_item_delegate_manager_| observes |shelf_model_|. It must be
781 // destroyed before |shelf_model_| is destroyed.
782 shelf_item_delegate_manager_.reset();
783 shelf_model_.reset();
785 power_button_controller_.reset();
786 lock_state_controller_.reset();
788 #if defined(OS_CHROMEOS)
789 resolution_notification_controller_.reset();
790 #endif
791 desktop_background_controller_.reset();
792 partial_screenshot_controller_.reset();
793 mouse_cursor_filter_.reset();
795 #if defined(OS_CHROMEOS)
796 touch_transformer_controller_.reset();
797 #endif // defined(OS_CHROMEOS)
799 // This also deletes all RootWindows. Note that we invoke Shutdown() on
800 // DisplayController before resetting |display_controller_|, since destruction
801 // of its owned RootWindowControllers relies on the value.
802 display_manager_->CreateScreenForShutdown();
803 display_controller_->Shutdown();
804 display_controller_.reset();
805 screen_position_controller_.reset();
806 accessibility_delegate_.reset();
807 new_window_delegate_.reset();
808 media_delegate_.reset();
810 keyboard::KeyboardController::ResetInstance(NULL);
812 #if defined(OS_CHROMEOS)
813 if (display_change_observer_)
814 display_configurator_->RemoveObserver(display_change_observer_.get());
815 if (display_configurator_animation_)
816 display_configurator_->RemoveObserver(
817 display_configurator_animation_.get());
818 if (display_error_observer_)
819 display_configurator_->RemoveObserver(display_error_observer_.get());
820 if (projecting_observer_)
821 display_configurator_->RemoveObserver(projecting_observer_.get());
822 display_change_observer_.reset();
824 PowerStatus::Shutdown();
826 // Ensure that DBusThreadManager outlives this Shell.
827 DCHECK(chromeos::DBusThreadManager::IsInitialized());
828 #endif
830 DCHECK(instance_ == this);
831 instance_ = NULL;
834 void Shell::Init(const ShellInitParams& init_params) {
835 delegate_->PreInit();
836 bool display_initialized = display_manager_->InitFromCommandLine();
837 #if defined(OS_CHROMEOS)
838 display_configurator_->Init(!gpu_support_->IsPanelFittingDisabled());
839 display_configurator_animation_.reset(new DisplayConfiguratorAnimation());
840 display_configurator_->AddObserver(display_configurator_animation_.get());
842 // The DBusThreadManager must outlive this Shell. See the DCHECK in ~Shell.
843 chromeos::DBusThreadManager* dbus_thread_manager =
844 chromeos::DBusThreadManager::Get();
845 projecting_observer_.reset(
846 new ProjectingObserver(dbus_thread_manager->GetPowerManagerClient()));
847 display_configurator_->AddObserver(projecting_observer_.get());
849 if (!display_initialized && base::SysInfo::IsRunningOnChromeOS()) {
850 display_change_observer_.reset(new DisplayChangeObserver);
851 // Register |display_change_observer_| first so that the rest of
852 // observer gets invoked after the root windows are configured.
853 display_configurator_->AddObserver(display_change_observer_.get());
854 display_error_observer_.reset(new DisplayErrorObserver());
855 display_configurator_->AddObserver(display_error_observer_.get());
856 display_configurator_->set_state_controller(display_change_observer_.get());
857 display_configurator_->set_mirroring_controller(display_manager_.get());
858 display_configurator_->ForceInitialConfigure(
859 delegate_->IsFirstRunAfterBoot() ? kChromeOsBootColor : 0);
860 display_initialized = true;
862 #endif // defined(OS_CHROMEOS)
863 if (!display_initialized)
864 display_manager_->InitDefaultDisplay();
866 display_manager_->RefreshFontParams();
868 // Install the custom factory first so that views::FocusManagers for Tray,
869 // Shelf, and WallPaper could be created by the factory.
870 views::FocusManagerFactory::Install(new AshFocusManagerFactory);
872 aura::Env::CreateInstance(true);
873 aura::Env::GetInstance()->set_context_factory(init_params.context_factory);
875 // The WindowModalityController needs to be at the front of the input event
876 // pretarget handler list to ensure that it processes input events when modal
877 // windows are active.
878 window_modality_controller_.reset(
879 new ::wm::WindowModalityController(this));
881 env_filter_.reset(new ::wm::CompoundEventFilter);
882 AddPreTargetHandler(env_filter_.get());
884 wm::AshFocusRules* focus_rules = new wm::AshFocusRules();
886 ::wm::FocusController* focus_controller =
887 new ::wm::FocusController(focus_rules);
888 focus_client_.reset(focus_controller);
889 activation_client_ = focus_controller;
890 activation_client_->AddObserver(this);
891 focus_cycler_.reset(new FocusCycler());
893 screen_position_controller_.reset(new ScreenPositionController);
895 display_controller_->Start();
896 display_controller_->CreatePrimaryHost(
897 ShellInitParamsToAshWindowTreeHostInitParams(init_params));
898 aura::Window* root_window = display_controller_->GetPrimaryRootWindow();
899 target_root_window_ = root_window;
901 #if defined(OS_CHROMEOS)
902 resolution_notification_controller_.reset(
903 new ResolutionNotificationController);
904 #endif
906 cursor_manager_.SetDisplay(GetScreen()->GetPrimaryDisplay());
908 nested_accelerator_controller_.reset(
909 new ::wm::NestedAcceleratorController(new NestedAcceleratorDelegate));
910 accelerator_controller_.reset(new AcceleratorController);
911 maximize_mode_controller_.reset(new MaximizeModeController());
913 #if defined(OS_CHROMEOS)
914 magnifier_key_scroll_handler_ = MagnifierKeyScroller::CreateHandler();
915 AddPreTargetHandler(magnifier_key_scroll_handler_.get());
916 speech_feedback_handler_ = SpokenFeedbackToggler::CreateHandler();
917 AddPreTargetHandler(speech_feedback_handler_.get());
918 #endif
920 // The order in which event filters are added is significant.
922 // ui::UserActivityDetector passes events to observers, so let them get
923 // rewritten first.
924 user_activity_detector_.reset(new ui::UserActivityDetector);
925 AddPreTargetHandler(user_activity_detector_.get());
927 overlay_filter_.reset(new OverlayEventFilter);
928 AddPreTargetHandler(overlay_filter_.get());
929 AddShellObserver(overlay_filter_.get());
931 input_method_filter_.reset(new ::wm::InputMethodEventFilter(
932 root_window->GetHost()->GetAcceleratedWidget()));
933 AddPreTargetHandler(input_method_filter_.get());
935 accelerator_filter_.reset(new ::wm::AcceleratorFilter(
936 scoped_ptr< ::wm::AcceleratorDelegate>(new AcceleratorDelegate).Pass(),
937 accelerator_controller_->accelerator_history()));
938 AddPreTargetHandler(accelerator_filter_.get());
940 event_transformation_handler_.reset(new EventTransformationHandler);
941 AddPreTargetHandler(event_transformation_handler_.get());
943 toplevel_window_event_handler_.reset(new ToplevelWindowEventHandler);
945 system_gesture_filter_.reset(new SystemGestureEventFilter);
946 AddPreTargetHandler(system_gesture_filter_.get());
948 keyboard_metrics_filter_.reset(new KeyboardUMAEventFilter);
949 AddPreTargetHandler(keyboard_metrics_filter_.get());
951 // The keyboard system must be initialized before the RootWindowController is
952 // created.
953 #if defined(OS_CHROMEOS)
954 keyboard::InitializeKeyboard();
955 #endif
957 #if defined(OS_CHROMEOS)
958 sticky_keys_controller_.reset(new StickyKeysController);
959 #endif
961 lock_state_controller_.reset(new LockStateController);
962 power_button_controller_.reset(new PowerButtonController(
963 lock_state_controller_.get()));
964 #if defined(OS_CHROMEOS)
965 // Pass the initial display state to PowerButtonController.
966 power_button_controller_->OnDisplayModeChanged(
967 display_configurator_->cached_displays());
968 #endif
969 AddShellObserver(lock_state_controller_.get());
971 drag_drop_controller_.reset(new DragDropController);
972 // |partial_screenshot_controller_| needs to be created (and prepended as a
973 // pre-target handler) at this point, because |mouse_cursor_filter_| needs to
974 // process mouse events prior to partial screenshot session.
975 // See http://crbug.com/459214
976 partial_screenshot_controller_.reset(new PartialScreenshotController());
977 mouse_cursor_filter_.reset(new MouseCursorEventFilter());
978 PrependPreTargetHandler(mouse_cursor_filter_.get());
980 // Create Controllers that may need root window.
981 // TODO(oshima): Move as many controllers before creating
982 // RootWindowController as possible.
983 visibility_controller_.reset(new AshVisibilityController);
985 magnification_controller_.reset(
986 MagnificationController::CreateInstance());
987 mru_window_tracker_.reset(new MruWindowTracker(activation_client_,
988 focus_rules));
990 partial_magnification_controller_.reset(
991 new PartialMagnificationController());
993 autoclick_controller_.reset(AutoclickController::CreateInstance());
995 high_contrast_controller_.reset(new HighContrastController);
996 video_detector_.reset(new VideoDetector);
997 window_selector_controller_.reset(new WindowSelectorController());
998 window_cycle_controller_.reset(new WindowCycleController());
1000 tooltip_controller_.reset(new views::corewm::TooltipController(
1001 scoped_ptr<views::corewm::Tooltip>(new views::corewm::TooltipAura)));
1002 AddPreTargetHandler(tooltip_controller_.get());
1004 event_client_.reset(new EventClientImpl);
1006 // This controller needs to be set before SetupManagedWindowMode.
1007 desktop_background_controller_.reset(new DesktopBackgroundController());
1008 user_wallpaper_delegate_.reset(delegate_->CreateUserWallpaperDelegate());
1010 session_state_delegate_.reset(delegate_->CreateSessionStateDelegate());
1011 accessibility_delegate_.reset(delegate_->CreateAccessibilityDelegate());
1012 new_window_delegate_.reset(delegate_->CreateNewWindowDelegate());
1013 media_delegate_.reset(delegate_->CreateMediaDelegate());
1015 resize_shadow_controller_.reset(new ResizeShadowController());
1016 shadow_controller_.reset(
1017 new ::wm::ShadowController(activation_client_));
1019 // Create system_tray_notifier_ before the delegate.
1020 system_tray_notifier_.reset(new ash::SystemTrayNotifier());
1022 // Initialize system_tray_delegate_ before initializing StatusAreaWidget.
1023 system_tray_delegate_.reset(delegate()->CreateSystemTrayDelegate());
1024 DCHECK(system_tray_delegate_.get());
1026 locale_notification_controller_.reset(new LocaleNotificationController);
1028 // Initialize system_tray_delegate_ after StatusAreaWidget is created.
1029 system_tray_delegate_->Initialize();
1031 #if defined(OS_CHROMEOS)
1032 // Create the LogoutConfirmationController after the SystemTrayDelegate.
1033 logout_confirmation_controller_.reset(new LogoutConfirmationController(
1034 base::Bind(&SystemTrayDelegate::SignOut,
1035 base::Unretained(system_tray_delegate_.get()))));
1037 // Create TouchTransformerController before DisplayController::InitDisplays()
1038 // since TouchTransformerController listens on
1039 // DisplayController::Observer::OnDisplaysInitialized().
1040 touch_transformer_controller_.reset(new TouchTransformerController());
1041 #endif // defined(OS_CHROMEOS)
1043 display_controller_->InitDisplays();
1045 #if defined(OS_CHROMEOS)
1046 // Needs to be created after InitDisplays() since it may cause the virtual
1047 // keyboard to be deployed.
1048 virtual_keyboard_controller_.reset(new VirtualKeyboardController);
1049 #endif // defined(OS_CHROMEOS)
1051 // It needs to be created after RootWindowController has been created
1052 // (which calls OnWindowResized has been called, otherwise the
1053 // widget will not paint when restoring after a browser crash. Also it needs
1054 // to be created after InitSecondaryDisplays() to initialize the wallpapers in
1055 // the correct size.
1056 user_wallpaper_delegate_->InitializeWallpaper();
1058 if (initially_hide_cursor_)
1059 cursor_manager_.HideCursor();
1060 cursor_manager_.SetCursor(ui::kCursorPointer);
1062 #if defined(OS_CHROMEOS)
1063 // Set accelerator controller delegates.
1064 accelerator_controller_->SetBrightnessControlDelegate(
1065 scoped_ptr<ash::BrightnessControlDelegate>(
1066 new ash::system::BrightnessControllerChromeos).Pass());
1068 power_event_observer_.reset(new PowerEventObserver());
1069 user_activity_notifier_.reset(
1070 new ui::UserActivityPowerManagerNotifier(user_activity_detector_.get()));
1071 video_activity_notifier_.reset(
1072 new VideoActivityNotifier(video_detector_.get()));
1073 bluetooth_notification_controller_.reset(new BluetoothNotificationController);
1074 last_window_closed_logout_reminder_.reset(new LastWindowClosedLogoutReminder);
1075 screen_orientation_controller_.reset(new ScreenOrientationController());
1076 #endif
1077 // The compositor thread and main message loop have to be running in
1078 // order to create mirror window. Run it after the main message loop
1079 // is started.
1080 display_manager_->CreateMirrorWindowAsyncIfAny();
1083 void Shell::InitKeyboard() {
1084 if (keyboard::IsKeyboardEnabled()) {
1085 if (keyboard::KeyboardController::GetInstance()) {
1086 RootWindowControllerList controllers = GetAllRootWindowControllers();
1087 for (RootWindowControllerList::iterator iter = controllers.begin();
1088 iter != controllers.end(); ++iter) {
1089 (*iter)->DeactivateKeyboard(
1090 keyboard::KeyboardController::GetInstance());
1093 keyboard::KeyboardControllerProxy* proxy =
1094 delegate_->CreateKeyboardControllerProxy();
1095 keyboard::KeyboardController::ResetInstance(
1096 new keyboard::KeyboardController(proxy));
1100 void Shell::InitRootWindow(aura::Window* root_window) {
1101 DCHECK(activation_client_);
1102 DCHECK(visibility_controller_.get());
1103 DCHECK(drag_drop_controller_.get());
1105 aura::client::SetFocusClient(root_window, focus_client_.get());
1106 input_method_filter_->SetInputMethodPropertyInRootWindow(root_window);
1107 aura::client::SetActivationClient(root_window, activation_client_);
1108 ::wm::FocusController* focus_controller =
1109 static_cast< ::wm::FocusController*>(activation_client_);
1110 root_window->AddPreTargetHandler(focus_controller);
1111 aura::client::SetVisibilityClient(root_window, visibility_controller_.get());
1112 aura::client::SetDragDropClient(root_window, drag_drop_controller_.get());
1113 aura::client::SetScreenPositionClient(root_window,
1114 screen_position_controller_.get());
1115 aura::client::SetCursorClient(root_window, &cursor_manager_);
1116 aura::client::SetTooltipClient(root_window, tooltip_controller_.get());
1117 aura::client::SetEventClient(root_window, event_client_.get());
1119 aura::client::SetWindowMoveClient(root_window,
1120 toplevel_window_event_handler_.get());
1121 root_window->AddPreTargetHandler(toplevel_window_event_handler_.get());
1122 root_window->AddPostTargetHandler(toplevel_window_event_handler_.get());
1124 if (nested_accelerator_controller_) {
1125 aura::client::SetDispatcherClient(root_window,
1126 nested_accelerator_controller_.get());
1130 bool Shell::CanWindowReceiveEvents(aura::Window* window) {
1131 RootWindowControllerList controllers = GetAllRootWindowControllers();
1132 for (RootWindowControllerList::iterator iter = controllers.begin();
1133 iter != controllers.end(); ++iter) {
1134 SystemModalContainerLayoutManager* layout_manager =
1135 (*iter)->GetSystemModalLayoutManager(window);
1136 if (layout_manager && layout_manager->CanWindowReceiveEvents(window))
1137 return true;
1138 // Allow events to fall through to the virtual keyboard even if displaying
1139 // a system modal dialog.
1140 if ((*iter)->IsVirtualKeyboardWindow(window))
1141 return true;
1144 return false;
1147 ////////////////////////////////////////////////////////////////////////////////
1148 // Shell, ui::EventTarget overrides:
1150 bool Shell::CanAcceptEvent(const ui::Event& event) {
1151 return true;
1154 ui::EventTarget* Shell::GetParentTarget() {
1155 return aura::Env::GetInstance();
1158 scoped_ptr<ui::EventTargetIterator> Shell::GetChildIterator() const {
1159 return scoped_ptr<ui::EventTargetIterator>();
1162 ui::EventTargeter* Shell::GetEventTargeter() {
1163 NOTREACHED();
1164 return NULL;
1167 void Shell::OnEvent(ui::Event* event) {
1170 ////////////////////////////////////////////////////////////////////////////////
1171 // Shell, aura::client::ActivationChangeObserver implementation:
1173 void Shell::OnWindowActivated(aura::Window* gained_active,
1174 aura::Window* lost_active) {
1175 if (gained_active)
1176 target_root_window_ = gained_active->GetRootWindow();
1179 } // namespace ash