Get GenericScopedHandle::Set to preserve LastError code
[chromium-blink-merge.git] / ash / shell.cc
blob2484eac0d401bbe8a7ceb3b3e2856c621ee149f3
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_manager.h"
21 #include "ash/display/event_transformation_handler.h"
22 #include "ash/display/mouse_cursor_event_filter.h"
23 #include "ash/display/screen_position_controller.h"
24 #include "ash/display/window_tree_host_manager.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/ime/input_method_event_handler.h"
33 #include "ash/keyboard_uma_event_filter.h"
34 #include "ash/magnifier/magnification_controller.h"
35 #include "ash/magnifier/partial_magnification_controller.h"
36 #include "ash/media_delegate.h"
37 #include "ash/new_window_delegate.h"
38 #include "ash/root_window_controller.h"
39 #include "ash/session/session_state_delegate.h"
40 #include "ash/shelf/app_list_shelf_item_delegate.h"
41 #include "ash/shelf/shelf_delegate.h"
42 #include "ash/shelf/shelf_item_delegate.h"
43 #include "ash/shelf/shelf_item_delegate_manager.h"
44 #include "ash/shelf/shelf_layout_manager.h"
45 #include "ash/shelf/shelf_model.h"
46 #include "ash/shelf/shelf_widget.h"
47 #include "ash/shelf/shelf_window_watcher.h"
48 #include "ash/shell_delegate.h"
49 #include "ash/shell_factory.h"
50 #include "ash/shell_init_params.h"
51 #include "ash/shell_window_ids.h"
52 #include "ash/system/locale/locale_notification_controller.h"
53 #include "ash/system/status_area_widget.h"
54 #include "ash/system/tray/system_tray_delegate.h"
55 #include "ash/system/tray/system_tray_notifier.h"
56 #include "ash/utility/partial_screenshot_controller.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/trace_event/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/base/user_activity/user_activity_detector.h"
92 #include "ui/compositor/layer.h"
93 #include "ui/compositor/layer_animator.h"
94 #include "ui/events/event_target_iterator.h"
95 #include "ui/gfx/display.h"
96 #include "ui/gfx/geometry/size.h"
97 #include "ui/gfx/image/image_skia.h"
98 #include "ui/gfx/screen.h"
99 #include "ui/keyboard/keyboard.h"
100 #include "ui/keyboard/keyboard_controller.h"
101 #include "ui/keyboard/keyboard_switches.h"
102 #include "ui/keyboard/keyboard_util.h"
103 #include "ui/message_center/message_center.h"
104 #include "ui/views/corewm/tooltip_aura.h"
105 #include "ui/views/corewm/tooltip_controller.h"
106 #include "ui/views/focus/focus_manager_factory.h"
107 #include "ui/views/widget/native_widget_aura.h"
108 #include "ui/views/widget/widget.h"
109 #include "ui/wm/core/accelerator_filter.h"
110 #include "ui/wm/core/compound_event_filter.h"
111 #include "ui/wm/core/focus_controller.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_ = nullptr;
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_ = nullptr;
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()
226 ->window_tree_host_manager()
227 ->GetAllRootWindowControllers();
230 // static
231 aura::Window* Shell::GetPrimaryRootWindow() {
232 CHECK(HasInstance());
233 return GetInstance()->window_tree_host_manager()->GetPrimaryRootWindow();
236 // static
237 aura::Window* Shell::GetTargetRootWindow() {
238 CHECK(HasInstance());
239 Shell* shell = GetInstance();
240 if (shell->scoped_target_root_window_)
241 return shell->scoped_target_root_window_;
242 return shell->target_root_window_;
245 // static
246 gfx::Screen* Shell::GetScreen() {
247 return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE);
250 // static
251 aura::Window::Windows Shell::GetAllRootWindows() {
252 CHECK(HasInstance());
253 return Shell::GetInstance()->window_tree_host_manager()->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()
333 : nullptr;
336 app_list::AppListView* Shell::GetAppListView() {
337 return app_list_controller_.get() ? app_list_controller_->GetView() : nullptr;
340 bool Shell::IsSystemModalWindowOpen() const {
341 if (simulate_modal_window_open_for_testing_)
342 return true;
343 const std::vector<aura::Window*> containers = GetContainersFromAllRootWindows(
344 kShellWindowId_SystemModalContainer, nullptr);
345 for (std::vector<aura::Window*>::const_iterator cit = containers.begin();
346 cit != containers.end(); ++cit) {
347 for (aura::Window::Windows::const_iterator wit = (*cit)->children().begin();
348 wit != (*cit)->children().end(); ++wit) {
349 if ((*wit)->GetProperty(aura::client::kModalKey) ==
350 ui::MODAL_TYPE_SYSTEM && (*wit)->TargetVisibility()) {
351 return true;
355 return false;
358 views::NonClientFrameView* Shell::CreateDefaultNonClientFrameView(
359 views::Widget* widget) {
360 // Use translucent-style window frames for dialogs.
361 return new CustomFrameViewAsh(widget);
364 void Shell::RotateFocus(Direction direction) {
365 focus_cycler_->RotateFocus(direction == FORWARD ? FocusCycler::FORWARD
366 : FocusCycler::BACKWARD);
369 void Shell::SetDisplayWorkAreaInsets(Window* contains,
370 const gfx::Insets& insets) {
371 if (!window_tree_host_manager_->UpdateWorkAreaOfDisplayNearestWindow(
372 contains, insets)) {
373 return;
375 FOR_EACH_OBSERVER(ShellObserver, observers_,
376 OnDisplayWorkAreaInsetsChanged());
379 void Shell::OnLoginStateChanged(user::LoginStatus status) {
380 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLoginStateChanged(status));
383 void Shell::OnLoginUserProfilePrepared() {
384 CreateShelf();
385 CreateKeyboard();
388 void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status) {
389 RootWindowControllerList controllers = GetAllRootWindowControllers();
390 for (RootWindowControllerList::iterator iter = controllers.begin();
391 iter != controllers.end(); ++iter)
392 (*iter)->UpdateAfterLoginStatusChange(status);
395 void Shell::OnAppTerminating() {
396 FOR_EACH_OBSERVER(ShellObserver, observers_, OnAppTerminating());
399 void Shell::OnLockStateChanged(bool locked) {
400 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLockStateChanged(locked));
401 #ifndef NDEBUG
402 // Make sure that there is no system modal in Lock layer when unlocked.
403 if (!locked) {
404 std::vector<aura::Window*> containers = GetContainersFromAllRootWindows(
405 kShellWindowId_LockSystemModalContainer, GetPrimaryRootWindow());
406 for (std::vector<aura::Window*>::const_iterator iter = containers.begin();
407 iter != containers.end(); ++iter) {
408 DCHECK_EQ(0u, (*iter)->children().size());
411 #endif
414 void Shell::OnCastingSessionStartedOrStopped(bool started) {
415 #if defined(OS_CHROMEOS)
416 FOR_EACH_OBSERVER(ShellObserver, observers_,
417 OnCastingSessionStartedOrStopped(started));
418 #endif
421 void Shell::OnOverviewModeStarting() {
422 FOR_EACH_OBSERVER(ShellObserver, observers_, OnOverviewModeStarting());
425 void Shell::OnOverviewModeEnded() {
426 FOR_EACH_OBSERVER(ShellObserver, observers_, OnOverviewModeEnded());
429 void Shell::OnMaximizeModeStarted() {
430 FOR_EACH_OBSERVER(ShellObserver, observers_, OnMaximizeModeStarted());
433 void Shell::OnMaximizeModeEnded() {
434 FOR_EACH_OBSERVER(ShellObserver, observers_, OnMaximizeModeEnded());
437 void Shell::OnRootWindowAdded(aura::Window* root_window) {
438 FOR_EACH_OBSERVER(ShellObserver, observers_, OnRootWindowAdded(root_window));
441 void Shell::CreateShelf() {
442 RootWindowControllerList controllers = GetAllRootWindowControllers();
443 for (RootWindowControllerList::iterator iter = controllers.begin();
444 iter != controllers.end(); ++iter)
445 (*iter)->shelf()->CreateShelf();
448 void Shell::OnShelfCreatedForRootWindow(aura::Window* root_window) {
449 FOR_EACH_OBSERVER(ShellObserver,
450 observers_,
451 OnShelfCreatedForRootWindow(root_window));
454 void Shell::CreateKeyboard() {
455 // TODO(bshe): Primary root window controller may not be the controller to
456 // attach virtual keyboard. See http://crbug.com/303429
457 InitKeyboard();
458 GetPrimaryRootWindowController()->
459 ActivateKeyboard(keyboard::KeyboardController::GetInstance());
462 void Shell::DeactivateKeyboard() {
463 if (keyboard::KeyboardController::GetInstance()) {
464 RootWindowControllerList controllers = GetAllRootWindowControllers();
465 for (RootWindowControllerList::iterator iter = controllers.begin();
466 iter != controllers.end(); ++iter) {
467 (*iter)->DeactivateKeyboard(keyboard::KeyboardController::GetInstance());
470 keyboard::KeyboardController::ResetInstance(nullptr);
473 void Shell::ShowShelf() {
474 RootWindowControllerList controllers = GetAllRootWindowControllers();
475 for (RootWindowControllerList::iterator iter = controllers.begin();
476 iter != controllers.end(); ++iter)
477 (*iter)->ShowShelf();
480 void Shell::AddShellObserver(ShellObserver* observer) {
481 observers_.AddObserver(observer);
484 void Shell::RemoveShellObserver(ShellObserver* observer) {
485 observers_.RemoveObserver(observer);
488 #if defined(OS_CHROMEOS)
489 bool Shell::ShouldSaveDisplaySettings() {
490 return !(screen_orientation_controller_
491 ->ignore_display_configuration_updates() ||
492 resolution_notification_controller_->DoesNotificationTimeout());
494 #endif
496 void Shell::UpdateShelfVisibility() {
497 RootWindowControllerList controllers = GetAllRootWindowControllers();
498 for (RootWindowControllerList::iterator iter = controllers.begin();
499 iter != controllers.end(); ++iter)
500 if ((*iter)->shelf())
501 (*iter)->UpdateShelfVisibility();
504 void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior,
505 aura::Window* root_window) {
506 ash::ShelfLayoutManager::ForShelf(root_window)->SetAutoHideBehavior(behavior);
509 ShelfAutoHideBehavior Shell::GetShelfAutoHideBehavior(
510 aura::Window* root_window) const {
511 return ash::ShelfLayoutManager::ForShelf(root_window)->auto_hide_behavior();
514 void Shell::SetShelfAlignment(ShelfAlignment alignment,
515 aura::Window* root_window) {
516 if (ash::ShelfLayoutManager::ForShelf(root_window)->SetAlignment(alignment)) {
517 FOR_EACH_OBSERVER(
518 ShellObserver, observers_, OnShelfAlignmentChanged(root_window));
522 ShelfAlignment Shell::GetShelfAlignment(const aura::Window* root_window) {
523 return GetRootWindowController(root_window)
524 ->GetShelfLayoutManager()
525 ->GetAlignment();
528 void Shell::SetDimming(bool should_dim) {
529 RootWindowControllerList controllers = GetAllRootWindowControllers();
530 for (RootWindowControllerList::iterator iter = controllers.begin();
531 iter != controllers.end(); ++iter)
532 (*iter)->screen_dimmer()->SetDimming(should_dim);
535 void Shell::NotifyFullscreenStateChange(bool is_fullscreen,
536 aura::Window* root_window) {
537 FOR_EACH_OBSERVER(ShellObserver, observers_, OnFullscreenStateChanged(
538 is_fullscreen, root_window));
541 void Shell::CreateModalBackground(aura::Window* window) {
542 if (!modality_filter_) {
543 modality_filter_.reset(new SystemModalContainerEventFilter(this));
544 AddPreTargetHandler(modality_filter_.get());
546 RootWindowControllerList controllers = GetAllRootWindowControllers();
547 for (RootWindowControllerList::iterator iter = controllers.begin();
548 iter != controllers.end(); ++iter)
549 (*iter)->GetSystemModalLayoutManager(window)->CreateModalBackground();
552 void Shell::OnModalWindowRemoved(aura::Window* removed) {
553 RootWindowControllerList controllers = GetAllRootWindowControllers();
554 bool activated = false;
555 for (RootWindowControllerList::iterator iter = controllers.begin();
556 iter != controllers.end() && !activated; ++iter) {
557 activated = (*iter)->GetSystemModalLayoutManager(removed)->
558 ActivateNextModalWindow();
560 if (!activated) {
561 RemovePreTargetHandler(modality_filter_.get());
562 modality_filter_.reset();
563 for (RootWindowControllerList::iterator iter = controllers.begin();
564 iter != controllers.end(); ++iter)
565 (*iter)->GetSystemModalLayoutManager(removed)->DestroyModalBackground();
569 WebNotificationTray* Shell::GetWebNotificationTray() {
570 return GetPrimaryRootWindowController()->shelf()->
571 status_area_widget()->web_notification_tray();
574 bool Shell::HasPrimaryStatusArea() {
575 ShelfWidget* shelf = GetPrimaryRootWindowController()->shelf();
576 return shelf && shelf->status_area_widget();
579 SystemTray* Shell::GetPrimarySystemTray() {
580 return GetPrimaryRootWindowController()->GetSystemTray();
583 ShelfDelegate* Shell::GetShelfDelegate() {
584 if (!shelf_delegate_) {
585 shelf_model_.reset(new ShelfModel);
586 // Creates ShelfItemDelegateManager before ShelfDelegate.
587 shelf_item_delegate_manager_.reset(
588 new ShelfItemDelegateManager(shelf_model_.get()));
590 shelf_delegate_.reset(delegate_->CreateShelfDelegate(shelf_model_.get()));
591 scoped_ptr<ShelfItemDelegate> controller(new AppListShelfItemDelegate);
593 // Finding the shelf model's location of the app list and setting its
594 // ShelfItemDelegate.
595 int app_list_index = shelf_model_->GetItemIndexForType(TYPE_APP_LIST);
596 DCHECK_GE(app_list_index, 0);
597 ShelfID app_list_id = shelf_model_->items()[app_list_index].id;
598 DCHECK(app_list_id);
599 shelf_item_delegate_manager_->SetShelfItemDelegate(app_list_id,
600 controller.Pass());
601 shelf_window_watcher_.reset(new ShelfWindowWatcher(
602 shelf_model_.get(), shelf_item_delegate_manager_.get()));
604 return shelf_delegate_.get();
607 void Shell::SetTouchHudProjectionEnabled(bool enabled) {
608 if (is_touch_hud_projection_enabled_ == enabled)
609 return;
611 is_touch_hud_projection_enabled_ = enabled;
612 FOR_EACH_OBSERVER(ShellObserver, observers_,
613 OnTouchHudProjectionToggled(enabled));
616 #if defined(OS_CHROMEOS)
617 ash::FirstRunHelper* Shell::CreateFirstRunHelper() {
618 return new ash::FirstRunHelperImpl;
621 void Shell::SetCursorCompositingEnabled(bool enabled) {
622 window_tree_host_manager_->cursor_window_controller()
623 ->SetCursorCompositingEnabled(enabled);
624 native_cursor_manager_->SetNativeCursorEnabled(!enabled);
626 #endif // defined(OS_CHROMEOS)
628 void Shell::DoInitialWorkspaceAnimation() {
629 return GetPrimaryRootWindowController()->workspace_controller()->
630 DoInitialAnimation();
633 ////////////////////////////////////////////////////////////////////////////////
634 // Shell, private:
636 Shell::Shell(ShellDelegate* delegate)
637 : target_root_window_(nullptr),
638 scoped_target_root_window_(nullptr),
639 delegate_(delegate),
640 window_positioner_(new WindowPositioner),
641 activation_client_(nullptr),
642 #if defined(OS_CHROMEOS)
643 display_configurator_(new ui::DisplayConfigurator()),
644 #endif // defined(OS_CHROMEOS)
645 native_cursor_manager_(new AshNativeCursorManager),
646 cursor_manager_(
647 scoped_ptr<::wm::NativeCursorManager>(native_cursor_manager_)),
648 simulate_modal_window_open_for_testing_(false),
649 is_touch_hud_projection_enabled_(false) {
650 DCHECK(delegate_.get());
651 DCHECK(aura::Env::GetInstanceDontCreate());
652 gpu_support_.reset(delegate_->CreateGPUSupport());
653 display_manager_.reset(new DisplayManager);
654 window_tree_host_manager_.reset(new WindowTreeHostManager);
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 user_metrics_recorder_->OnShellShuttingDown();
667 delegate_->PreShutdown();
669 views::FocusManagerFactory::Install(nullptr);
671 // Remove the focus from any window. This will prevent overhead and side
672 // effects (e.g. crashes) from changing focus during shutdown.
673 // See bug crbug.com/134502.
674 aura::client::GetFocusClient(GetPrimaryRootWindow())->FocusWindow(nullptr);
676 // Please keep in same order as in Init() because it's easy to miss one.
677 if (window_modality_controller_)
678 window_modality_controller_.reset();
679 RemovePreTargetHandler(
680 window_tree_host_manager_->input_method_event_handler());
681 #if defined(OS_CHROMEOS)
682 RemovePreTargetHandler(magnifier_key_scroll_handler_.get());
683 magnifier_key_scroll_handler_.reset();
685 RemovePreTargetHandler(speech_feedback_handler_.get());
686 speech_feedback_handler_.reset();
687 #endif
688 RemovePreTargetHandler(overlay_filter_.get());
689 RemovePreTargetHandler(accelerator_filter_.get());
690 RemovePreTargetHandler(event_transformation_handler_.get());
691 RemovePreTargetHandler(toplevel_window_event_handler_.get());
692 RemovePostTargetHandler(toplevel_window_event_handler_.get());
693 RemovePreTargetHandler(system_gesture_filter_.get());
694 RemovePreTargetHandler(keyboard_metrics_filter_.get());
695 RemovePreTargetHandler(mouse_cursor_filter_.get());
697 // TooltipController is deleted with the Shell so removing its references.
698 RemovePreTargetHandler(tooltip_controller_.get());
700 #if defined(OS_CHROMEOS)
701 screen_orientation_controller_.reset();
702 #endif
704 // Destroy the virtual keyboard controller before the maximize mode controller
705 // since the latters destructor triggers events that the former is listening
706 // to but no longer cares about.
707 #if defined(OS_CHROMEOS)
708 virtual_keyboard_controller_.reset();
709 #endif
711 // Destroy maximize mode controller early on since it has some observers which
712 // need to be removed.
713 maximize_mode_controller_.reset();
715 // AppList needs to be released before shelf layout manager, which is
716 // destroyed with shelf container in the loop below. However, app list
717 // container is now on top of shelf container and released after it.
718 // TODO(xiyuan): Move it back when app list container is no longer needed.
719 app_list_controller_.reset();
721 #if defined(OS_CHROMEOS)
722 // Destroy the LastWindowClosedLogoutReminder before the
723 // LogoutConfirmationController.
724 last_window_closed_logout_reminder_.reset();
726 // Destroy the LogoutConfirmationController before the SystemTrayDelegate.
727 logout_confirmation_controller_.reset();
728 #endif
730 // Destroy SystemTrayDelegate before destroying the status area(s).
731 system_tray_delegate_->Shutdown();
732 system_tray_delegate_.reset();
734 locale_notification_controller_.reset();
736 // Drag-and-drop must be canceled prior to close all windows.
737 drag_drop_controller_.reset();
739 // Controllers who have WindowObserver added must be deleted
740 // before |window_tree_host_manager_| is deleted.
742 #if defined(OS_CHROMEOS)
743 // VideoActivityNotifier must be deleted before |video_detector_| is
744 // deleted because it's observing video activity through
745 // VideoDetectorObserver interface.
746 video_activity_notifier_.reset();
747 #endif // defined(OS_CHROMEOS)
748 video_detector_.reset();
749 high_contrast_controller_.reset();
751 shadow_controller_.reset();
752 resize_shadow_controller_.reset();
754 window_cycle_controller_.reset();
755 window_selector_controller_.reset();
757 // |shelf_window_watcher_| has a weak pointer to |shelf_Model_|
758 // and has window observers.
759 shelf_window_watcher_.reset();
761 // Destroy all child windows including widgets.
762 window_tree_host_manager_->CloseChildWindows();
763 // MruWindowTracker must be destroyed after all windows have been deleted to
764 // avoid a possible crash when Shell is destroyed from a non-normal shutdown
765 // path. (crbug.com/485438).
766 mru_window_tracker_.reset();
768 // Chrome implementation of shelf delegate depends on FocusClient,
769 // so must be deleted before |focus_client_|.
770 shelf_delegate_.reset();
771 focus_client_.reset();
773 // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems
774 // needs to remove observers from it.
775 system_tray_notifier_.reset();
777 // These need a valid Shell instance to clean up properly, so explicitly
778 // delete them before invalidating the instance.
779 // Alphabetical. TODO(oshima): sort.
780 magnification_controller_.reset();
781 partial_magnification_controller_.reset();
782 tooltip_controller_.reset();
783 event_client_.reset();
784 nested_accelerator_controller_.reset();
785 toplevel_window_event_handler_.reset();
786 visibility_controller_.reset();
787 // |shelf_item_delegate_manager_| observes |shelf_model_|. It must be
788 // destroyed before |shelf_model_| is destroyed.
789 shelf_item_delegate_manager_.reset();
790 shelf_model_.reset();
792 power_button_controller_.reset();
793 lock_state_controller_.reset();
795 #if defined(OS_CHROMEOS)
796 resolution_notification_controller_.reset();
797 #endif
798 desktop_background_controller_.reset();
799 partial_screenshot_controller_.reset();
800 mouse_cursor_filter_.reset();
802 #if defined(OS_CHROMEOS)
803 touch_transformer_controller_.reset();
804 #endif // defined(OS_CHROMEOS)
806 // This also deletes all RootWindows. Note that we invoke Shutdown() on
807 // WindowTreeHostManager before resetting |window_tree_host_manager_|, since
808 // destruction
809 // of its owned RootWindowControllers relies on the value.
810 display_manager_->CreateScreenForShutdown();
811 window_tree_host_manager_->Shutdown();
812 window_tree_host_manager_.reset();
813 screen_position_controller_.reset();
814 accessibility_delegate_.reset();
815 new_window_delegate_.reset();
816 media_delegate_.reset();
818 keyboard::KeyboardController::ResetInstance(nullptr);
820 #if defined(OS_CHROMEOS)
821 display_color_manager_.reset();
822 if (display_change_observer_)
823 display_configurator_->RemoveObserver(display_change_observer_.get());
824 if (display_configurator_animation_)
825 display_configurator_->RemoveObserver(
826 display_configurator_animation_.get());
827 if (display_error_observer_)
828 display_configurator_->RemoveObserver(display_error_observer_.get());
829 if (projecting_observer_) {
830 display_configurator_->RemoveObserver(projecting_observer_.get());
831 RemoveShellObserver(projecting_observer_.get());
833 display_change_observer_.reset();
835 PowerStatus::Shutdown();
837 // Ensure that DBusThreadManager outlives this Shell.
838 DCHECK(chromeos::DBusThreadManager::IsInitialized());
839 #endif
841 DCHECK(instance_ == this);
842 instance_ = nullptr;
845 void Shell::Init(const ShellInitParams& init_params) {
846 delegate_->PreInit();
847 bool display_initialized = display_manager_->InitFromCommandLine();
848 #if defined(OS_CHROMEOS)
849 display_configurator_->Init(!gpu_support_->IsPanelFittingDisabled());
850 display_configurator_animation_.reset(new DisplayConfiguratorAnimation());
851 display_configurator_->AddObserver(display_configurator_animation_.get());
853 // The DBusThreadManager must outlive this Shell. See the DCHECK in ~Shell.
854 chromeos::DBusThreadManager* dbus_thread_manager =
855 chromeos::DBusThreadManager::Get();
856 projecting_observer_.reset(
857 new ProjectingObserver(dbus_thread_manager->GetPowerManagerClient()));
858 display_configurator_->AddObserver(projecting_observer_.get());
859 AddShellObserver(projecting_observer_.get());
861 if (!display_initialized && base::SysInfo::IsRunningOnChromeOS()) {
862 display_change_observer_.reset(new DisplayChangeObserver);
863 // Register |display_change_observer_| first so that the rest of
864 // observer gets invoked after the root windows are configured.
865 display_configurator_->AddObserver(display_change_observer_.get());
866 display_error_observer_.reset(new DisplayErrorObserver());
867 display_configurator_->AddObserver(display_error_observer_.get());
868 display_configurator_->set_state_controller(display_change_observer_.get());
869 display_configurator_->set_mirroring_controller(display_manager_.get());
870 display_configurator_->ForceInitialConfigure(
871 delegate_->IsFirstRunAfterBoot() ? kChromeOsBootColor : 0);
872 display_initialized = true;
874 display_color_manager_.reset(
875 new DisplayColorManager(display_configurator_.get()));
876 #endif // defined(OS_CHROMEOS)
877 if (!display_initialized)
878 display_manager_->InitDefaultDisplay();
880 display_manager_->RefreshFontParams();
882 // Install the custom factory first so that views::FocusManagers for Tray,
883 // Shelf, and WallPaper could be created by the factory.
884 views::FocusManagerFactory::Install(new AshFocusManagerFactory);
886 aura::Env::GetInstance()->set_context_factory(init_params.context_factory);
888 // The WindowModalityController needs to be at the front of the input event
889 // pretarget handler list to ensure that it processes input events when modal
890 // windows are active.
891 window_modality_controller_.reset(
892 new ::wm::WindowModalityController(this));
894 env_filter_.reset(new ::wm::CompoundEventFilter);
895 AddPreTargetHandler(env_filter_.get());
897 wm::AshFocusRules* focus_rules = new wm::AshFocusRules();
899 ::wm::FocusController* focus_controller =
900 new ::wm::FocusController(focus_rules);
901 focus_client_.reset(focus_controller);
902 activation_client_ = focus_controller;
903 activation_client_->AddObserver(this);
904 focus_cycler_.reset(new FocusCycler());
906 screen_position_controller_.reset(new ScreenPositionController);
908 window_tree_host_manager_->Start();
909 window_tree_host_manager_->CreatePrimaryHost(
910 ShellInitParamsToAshWindowTreeHostInitParams(init_params));
911 aura::Window* root_window = window_tree_host_manager_->GetPrimaryRootWindow();
912 target_root_window_ = root_window;
914 #if defined(OS_CHROMEOS)
915 resolution_notification_controller_.reset(
916 new ResolutionNotificationController);
917 #endif
919 cursor_manager_.SetDisplay(GetScreen()->GetPrimaryDisplay());
921 nested_accelerator_controller_.reset(
922 new ::wm::NestedAcceleratorController(new NestedAcceleratorDelegate));
923 accelerator_controller_.reset(new AcceleratorController);
924 maximize_mode_controller_.reset(new MaximizeModeController());
926 AddPreTargetHandler(window_tree_host_manager_->input_method_event_handler());
928 #if defined(OS_CHROMEOS)
929 magnifier_key_scroll_handler_ = MagnifierKeyScroller::CreateHandler();
930 AddPreTargetHandler(magnifier_key_scroll_handler_.get());
931 speech_feedback_handler_ = SpokenFeedbackToggler::CreateHandler();
932 AddPreTargetHandler(speech_feedback_handler_.get());
933 #endif
935 // The order in which event filters are added is significant.
937 // ui::UserActivityDetector passes events to observers, so let them get
938 // rewritten first.
939 user_activity_detector_.reset(new ui::UserActivityDetector);
941 overlay_filter_.reset(new OverlayEventFilter);
942 AddPreTargetHandler(overlay_filter_.get());
943 AddShellObserver(overlay_filter_.get());
945 accelerator_filter_.reset(new ::wm::AcceleratorFilter(
946 scoped_ptr< ::wm::AcceleratorDelegate>(new AcceleratorDelegate).Pass(),
947 accelerator_controller_->accelerator_history()));
948 AddPreTargetHandler(accelerator_filter_.get());
950 event_transformation_handler_.reset(new EventTransformationHandler);
951 AddPreTargetHandler(event_transformation_handler_.get());
953 toplevel_window_event_handler_.reset(new ToplevelWindowEventHandler);
955 system_gesture_filter_.reset(new SystemGestureEventFilter);
956 AddPreTargetHandler(system_gesture_filter_.get());
958 keyboard_metrics_filter_.reset(new KeyboardUMAEventFilter);
959 AddPreTargetHandler(keyboard_metrics_filter_.get());
961 // The keyboard system must be initialized before the RootWindowController is
962 // created.
963 #if defined(OS_CHROMEOS)
964 keyboard::InitializeKeyboard();
965 #endif
967 #if defined(OS_CHROMEOS)
968 sticky_keys_controller_.reset(new StickyKeysController);
969 #endif
971 lock_state_controller_.reset(new LockStateController);
972 power_button_controller_.reset(new PowerButtonController(
973 lock_state_controller_.get()));
974 #if defined(OS_CHROMEOS)
975 // Pass the initial display state to PowerButtonController.
976 power_button_controller_->OnDisplayModeChanged(
977 display_configurator_->cached_displays());
978 #endif
979 AddShellObserver(lock_state_controller_.get());
981 drag_drop_controller_.reset(new DragDropController);
982 // |partial_screenshot_controller_| needs to be created (and prepended as a
983 // pre-target handler) at this point, because |mouse_cursor_filter_| needs to
984 // process mouse events prior to partial screenshot session.
985 // See http://crbug.com/459214
986 partial_screenshot_controller_.reset(new PartialScreenshotController());
987 mouse_cursor_filter_.reset(new MouseCursorEventFilter());
988 PrependPreTargetHandler(mouse_cursor_filter_.get());
990 // Create Controllers that may need root window.
991 // TODO(oshima): Move as many controllers before creating
992 // RootWindowController as possible.
993 visibility_controller_.reset(new AshVisibilityController);
995 magnification_controller_.reset(
996 MagnificationController::CreateInstance());
997 mru_window_tracker_.reset(new MruWindowTracker(activation_client_,
998 focus_rules));
1000 partial_magnification_controller_.reset(
1001 new PartialMagnificationController());
1003 autoclick_controller_.reset(AutoclickController::CreateInstance());
1005 high_contrast_controller_.reset(new HighContrastController);
1006 video_detector_.reset(new VideoDetector);
1007 window_selector_controller_.reset(new WindowSelectorController());
1008 window_cycle_controller_.reset(new WindowCycleController());
1010 tooltip_controller_.reset(new views::corewm::TooltipController(
1011 scoped_ptr<views::corewm::Tooltip>(new views::corewm::TooltipAura)));
1012 AddPreTargetHandler(tooltip_controller_.get());
1014 event_client_.reset(new EventClientImpl);
1016 // This controller needs to be set before SetupManagedWindowMode.
1017 desktop_background_controller_.reset(new DesktopBackgroundController());
1018 user_wallpaper_delegate_.reset(delegate_->CreateUserWallpaperDelegate());
1020 session_state_delegate_.reset(delegate_->CreateSessionStateDelegate());
1021 accessibility_delegate_.reset(delegate_->CreateAccessibilityDelegate());
1022 new_window_delegate_.reset(delegate_->CreateNewWindowDelegate());
1023 media_delegate_.reset(delegate_->CreateMediaDelegate());
1025 resize_shadow_controller_.reset(new ResizeShadowController());
1026 shadow_controller_.reset(
1027 new ::wm::ShadowController(activation_client_));
1029 // Create system_tray_notifier_ before the delegate.
1030 system_tray_notifier_.reset(new ash::SystemTrayNotifier());
1032 // Initialize system_tray_delegate_ before initializing StatusAreaWidget.
1033 system_tray_delegate_.reset(delegate()->CreateSystemTrayDelegate());
1034 DCHECK(system_tray_delegate_.get());
1036 locale_notification_controller_.reset(new LocaleNotificationController);
1038 // Initialize system_tray_delegate_ after StatusAreaWidget is created.
1039 system_tray_delegate_->Initialize();
1041 #if defined(OS_CHROMEOS)
1042 // Create the LogoutConfirmationController after the SystemTrayDelegate.
1043 logout_confirmation_controller_.reset(new LogoutConfirmationController(
1044 base::Bind(&SystemTrayDelegate::SignOut,
1045 base::Unretained(system_tray_delegate_.get()))));
1047 // Create TouchTransformerController before
1048 // WindowTreeHostManager::InitDisplays()
1049 // since TouchTransformerController listens on
1050 // WindowTreeHostManager::Observer::OnDisplaysInitialized().
1051 touch_transformer_controller_.reset(new TouchTransformerController());
1052 #endif // defined(OS_CHROMEOS)
1054 window_tree_host_manager_->InitHosts();
1056 #if defined(OS_CHROMEOS)
1057 // Needs to be created after InitDisplays() since it may cause the virtual
1058 // keyboard to be deployed.
1059 virtual_keyboard_controller_.reset(new VirtualKeyboardController);
1060 #endif // defined(OS_CHROMEOS)
1062 // It needs to be created after RootWindowController has been created
1063 // (which calls OnWindowResized has been called, otherwise the
1064 // widget will not paint when restoring after a browser crash. Also it needs
1065 // to be created after InitSecondaryDisplays() to initialize the wallpapers in
1066 // the correct size.
1067 user_wallpaper_delegate_->InitializeWallpaper();
1069 if (initially_hide_cursor_)
1070 cursor_manager_.HideCursor();
1071 cursor_manager_.SetCursor(ui::kCursorPointer);
1073 #if defined(OS_CHROMEOS)
1074 // Set accelerator controller delegates.
1075 accelerator_controller_->SetBrightnessControlDelegate(
1076 scoped_ptr<ash::BrightnessControlDelegate>(
1077 new ash::system::BrightnessControllerChromeos).Pass());
1079 power_event_observer_.reset(new PowerEventObserver());
1080 user_activity_notifier_.reset(
1081 new ui::UserActivityPowerManagerNotifier(user_activity_detector_.get()));
1082 video_activity_notifier_.reset(
1083 new VideoActivityNotifier(video_detector_.get()));
1084 bluetooth_notification_controller_.reset(new BluetoothNotificationController);
1085 last_window_closed_logout_reminder_.reset(new LastWindowClosedLogoutReminder);
1086 screen_orientation_controller_.reset(new ScreenOrientationController());
1087 #endif
1088 // The compositor thread and main message loop have to be running in
1089 // order to create mirror window. Run it after the main message loop
1090 // is started.
1091 display_manager_->CreateMirrorWindowAsyncIfAny();
1093 user_metrics_recorder_->OnShellInitialized();
1096 void Shell::InitKeyboard() {
1097 if (keyboard::IsKeyboardEnabled()) {
1098 if (keyboard::KeyboardController::GetInstance()) {
1099 RootWindowControllerList controllers = GetAllRootWindowControllers();
1100 for (RootWindowControllerList::iterator iter = controllers.begin();
1101 iter != controllers.end(); ++iter) {
1102 (*iter)->DeactivateKeyboard(
1103 keyboard::KeyboardController::GetInstance());
1106 keyboard::KeyboardControllerProxy* proxy =
1107 delegate_->CreateKeyboardControllerProxy();
1108 keyboard::KeyboardController::ResetInstance(
1109 new keyboard::KeyboardController(proxy));
1113 void Shell::InitRootWindow(aura::Window* root_window) {
1114 DCHECK(activation_client_);
1115 DCHECK(visibility_controller_.get());
1116 DCHECK(drag_drop_controller_.get());
1118 aura::client::SetFocusClient(root_window, focus_client_.get());
1119 aura::client::SetActivationClient(root_window, activation_client_);
1120 ::wm::FocusController* focus_controller =
1121 static_cast< ::wm::FocusController*>(activation_client_);
1122 root_window->AddPreTargetHandler(focus_controller);
1123 aura::client::SetVisibilityClient(root_window, visibility_controller_.get());
1124 aura::client::SetDragDropClient(root_window, drag_drop_controller_.get());
1125 aura::client::SetScreenPositionClient(root_window,
1126 screen_position_controller_.get());
1127 aura::client::SetCursorClient(root_window, &cursor_manager_);
1128 aura::client::SetTooltipClient(root_window, tooltip_controller_.get());
1129 aura::client::SetEventClient(root_window, event_client_.get());
1131 aura::client::SetWindowMoveClient(root_window,
1132 toplevel_window_event_handler_.get());
1133 root_window->AddPreTargetHandler(toplevel_window_event_handler_.get());
1134 root_window->AddPostTargetHandler(toplevel_window_event_handler_.get());
1136 if (nested_accelerator_controller_) {
1137 aura::client::SetDispatcherClient(root_window,
1138 nested_accelerator_controller_.get());
1142 bool Shell::CanWindowReceiveEvents(aura::Window* window) {
1143 RootWindowControllerList controllers = GetAllRootWindowControllers();
1144 for (RootWindowControllerList::iterator iter = controllers.begin();
1145 iter != controllers.end(); ++iter) {
1146 SystemModalContainerLayoutManager* layout_manager =
1147 (*iter)->GetSystemModalLayoutManager(window);
1148 if (layout_manager && layout_manager->CanWindowReceiveEvents(window))
1149 return true;
1150 // Allow events to fall through to the virtual keyboard even if displaying
1151 // a system modal dialog.
1152 if ((*iter)->IsVirtualKeyboardWindow(window))
1153 return true;
1156 return false;
1159 ////////////////////////////////////////////////////////////////////////////////
1160 // Shell, ui::EventTarget overrides:
1162 bool Shell::CanAcceptEvent(const ui::Event& event) {
1163 return true;
1166 ui::EventTarget* Shell::GetParentTarget() {
1167 return aura::Env::GetInstance();
1170 scoped_ptr<ui::EventTargetIterator> Shell::GetChildIterator() const {
1171 return scoped_ptr<ui::EventTargetIterator>();
1174 ui::EventTargeter* Shell::GetEventTargeter() {
1175 NOTREACHED();
1176 return nullptr;
1179 void Shell::OnEvent(ui::Event* event) {
1182 ////////////////////////////////////////////////////////////////////////////////
1183 // Shell, aura::client::ActivationChangeObserver implementation:
1185 void Shell::OnWindowActivated(
1186 aura::client::ActivationChangeObserver::ActivationReason reason,
1187 aura::Window* gained_active,
1188 aura::Window* lost_active) {
1189 if (gained_active)
1190 target_root_window_ = gained_active->GetRootWindow();
1193 } // namespace ash