Rename base::FieldTrialList::RegistrationList --> RegistrationMap.
[chromium-blink-merge.git] / ash / shell.cc
blobded8d840834864058784b6d067e0b3e44b48f40f
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/shell.h"
7 #include <algorithm>
8 #include <string>
10 #include "ash/accelerators/accelerator_controller.h"
11 #include "ash/accelerators/accelerator_filter.h"
12 #include "ash/accelerators/focus_manager_factory.h"
13 #include "ash/accelerators/nested_dispatcher_controller.h"
14 #include "ash/ash_switches.h"
15 #include "ash/autoclick/autoclick_controller.h"
16 #include "ash/caps_lock_delegate.h"
17 #include "ash/desktop_background/desktop_background_controller.h"
18 #include "ash/desktop_background/desktop_background_view.h"
19 #include "ash/desktop_background/user_wallpaper_delegate.h"
20 #include "ash/display/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/resolution_notification_controller.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/high_contrast/high_contrast_controller.h"
31 #include "ash/host/root_window_host_factory.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/screen_ash.h"
39 #include "ash/session_state_delegate.h"
40 #include "ash/shelf/app_list_shelf_item_delegate.h"
41 #include "ash/shelf/shelf_delegate.h"
42 #include "ash/shelf/shelf_item_delegate.h"
43 #include "ash/shelf/shelf_item_delegate_manager.h"
44 #include "ash/shelf/shelf_layout_manager.h"
45 #include "ash/shelf/shelf_model.h"
46 #include "ash/shelf/shelf_widget.h"
47 #include "ash/shelf/shelf_window_watcher.h"
48 #include "ash/shell_delegate.h"
49 #include "ash/shell_factory.h"
50 #include "ash/shell_window_ids.h"
51 #include "ash/system/locale/locale_notification_controller.h"
52 #include "ash/system/status_area_widget.h"
53 #include "ash/system/tray/system_tray_delegate.h"
54 #include "ash/system/tray/system_tray_notifier.h"
55 #include "ash/wm/app_list_controller.h"
56 #include "ash/wm/ash_focus_rules.h"
57 #include "ash/wm/ash_native_cursor_manager.h"
58 #include "ash/wm/base_layout_manager.h"
59 #include "ash/wm/coordinate_conversion.h"
60 #include "ash/wm/custom_frame_view_ash.h"
61 #include "ash/wm/event_client_impl.h"
62 #include "ash/wm/event_rewriter_event_filter.h"
63 #include "ash/wm/lock_state_controller.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/user_activity_detector.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/command_line.h"
85 #include "base/debug/leak_annotations.h"
86 #include "base/debug/trace_event.h"
87 #include "content/public/browser/user_metrics.h"
88 #include "ui/aura/client/aura_constants.h"
89 #include "ui/aura/client/user_action_client.h"
90 #include "ui/aura/env.h"
91 #include "ui/aura/layout_manager.h"
92 #include "ui/aura/root_window.h"
93 #include "ui/aura/window.h"
94 #include "ui/base/ui_base_switches.h"
95 #include "ui/compositor/layer.h"
96 #include "ui/compositor/layer_animator.h"
97 #include "ui/events/event_target_iterator.h"
98 #include "ui/gfx/display.h"
99 #include "ui/gfx/image/image_skia.h"
100 #include "ui/gfx/screen.h"
101 #include "ui/gfx/size.h"
102 #include "ui/keyboard/keyboard.h"
103 #include "ui/keyboard/keyboard_controller.h"
104 #include "ui/keyboard/keyboard_switches.h"
105 #include "ui/keyboard/keyboard_util.h"
106 #include "ui/message_center/message_center.h"
107 #include "ui/views/corewm/compound_event_filter.h"
108 #include "ui/views/corewm/corewm_switches.h"
109 #include "ui/views/corewm/focus_controller.h"
110 #include "ui/views/corewm/input_method_event_filter.h"
111 #include "ui/views/corewm/shadow_controller.h"
112 #include "ui/views/corewm/tooltip_aura.h"
113 #include "ui/views/corewm/tooltip_controller.h"
114 #include "ui/views/corewm/visibility_controller.h"
115 #include "ui/views/corewm/window_modality_controller.h"
116 #include "ui/views/focus/focus_manager_factory.h"
117 #include "ui/views/widget/native_widget_aura.h"
118 #include "ui/views/widget/widget.h"
120 #if defined(OS_CHROMEOS)
121 #if defined(USE_X11)
122 #include "ash/ash_constants.h"
123 #include "ash/display/display_change_observer_chromeos.h"
124 #include "ash/display/display_error_observer_chromeos.h"
125 #include "ash/display/output_configurator_animation.h"
126 #include "base/message_loop/message_pump_x11.h"
127 #include "base/sys_info.h"
128 #include "chromeos/display/output_configurator.h"
129 #include "content/public/browser/gpu_data_manager.h"
130 #include "gpu/config/gpu_feature_type.h"
131 #endif // defined(USE_X11)
132 #include "ash/sticky_keys/sticky_keys_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/user_activity_notifier.h"
137 #include "ash/system/chromeos/power/video_activity_notifier.h"
138 #endif // defined(OS_CHROMEOS)
140 namespace ash {
142 namespace {
144 using aura::Window;
145 using views::Widget;
147 // A Corewm VisibilityController subclass that calls the Ash animation routine
148 // so we can pick up our extended animations. See ash/wm/window_animations.h.
149 class AshVisibilityController : public views::corewm::VisibilityController {
150 public:
151 AshVisibilityController() {}
152 virtual ~AshVisibilityController() {}
154 private:
155 // Overridden from views::corewm::VisibilityController:
156 virtual bool CallAnimateOnChildWindowVisibilityChanged(
157 aura::Window* window,
158 bool visible) OVERRIDE {
159 return AnimateOnChildWindowVisibilityChanged(window, visible);
162 DISALLOW_COPY_AND_ASSIGN(AshVisibilityController);
165 } // namespace
167 // static
168 Shell* Shell::instance_ = NULL;
169 // static
170 bool Shell::initially_hide_cursor_ = false;
172 ////////////////////////////////////////////////////////////////////////////////
173 // Shell, public:
175 // static
176 Shell* Shell::CreateInstance(ShellDelegate* delegate) {
177 CHECK(!instance_);
178 instance_ = new Shell(delegate);
179 instance_->Init();
180 return instance_;
183 // static
184 Shell* Shell::GetInstance() {
185 DCHECK(instance_);
186 return instance_;
189 // static
190 bool Shell::HasInstance() {
191 return !!instance_;
194 // static
195 void Shell::DeleteInstance() {
196 delete instance_;
197 instance_ = NULL;
200 // static
201 internal::RootWindowController* Shell::GetPrimaryRootWindowController() {
202 return internal::GetRootWindowController(GetPrimaryRootWindow());
205 // static
206 Shell::RootWindowControllerList Shell::GetAllRootWindowControllers() {
207 return Shell::GetInstance()->display_controller()->
208 GetAllRootWindowControllers();
211 // static
212 aura::Window* Shell::GetPrimaryRootWindow() {
213 return GetInstance()->display_controller()->GetPrimaryRootWindow();
216 // static
217 aura::Window* Shell::GetTargetRootWindow() {
218 Shell* shell = GetInstance();
219 if (shell->scoped_target_root_window_)
220 return shell->scoped_target_root_window_;
221 return shell->target_root_window_;
224 // static
225 gfx::Screen* Shell::GetScreen() {
226 return gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE);
229 // static
230 aura::Window::Windows Shell::GetAllRootWindows() {
231 return Shell::GetInstance()->display_controller()->
232 GetAllRootWindows();
235 // static
236 aura::Window* Shell::GetContainer(aura::Window* root_window,
237 int container_id) {
238 return root_window->GetChildById(container_id);
241 // static
242 const aura::Window* Shell::GetContainer(const aura::Window* root_window,
243 int container_id) {
244 return root_window->GetChildById(container_id);
247 // static
248 std::vector<aura::Window*> Shell::GetContainersFromAllRootWindows(
249 int container_id,
250 aura::Window* priority_root) {
251 std::vector<aura::Window*> containers;
252 aura::Window::Windows root_windows = GetAllRootWindows();
253 for (aura::Window::Windows::const_iterator it = root_windows.begin();
254 it != root_windows.end(); ++it) {
255 aura::Window* container = (*it)->GetChildById(container_id);
256 if (container) {
257 if (priority_root && priority_root->Contains(container))
258 containers.insert(containers.begin(), container);
259 else
260 containers.push_back(container);
263 return containers;
266 void Shell::ShowContextMenu(const gfx::Point& location_in_screen,
267 ui::MenuSourceType source_type) {
268 // No context menus if there is no session with an active user.
269 if (!session_state_delegate_->NumberOfLoggedInUsers())
270 return;
271 // No context menus when screen is locked.
272 if (session_state_delegate_->IsScreenLocked())
273 return;
275 aura::Window* root =
276 wm::GetRootWindowMatching(gfx::Rect(location_in_screen, gfx::Size()));
277 internal::GetRootWindowController(root)->
278 ShowContextMenu(location_in_screen, source_type);
281 void Shell::ToggleAppList(aura::Window* window) {
282 // If the context window is not given, show it on the target root window.
283 if (!window)
284 window = GetTargetRootWindow();
285 if (!app_list_controller_)
286 app_list_controller_.reset(new internal::AppListController);
287 app_list_controller_->SetVisible(!app_list_controller_->IsVisible(), window);
290 bool Shell::GetAppListTargetVisibility() const {
291 return app_list_controller_.get() &&
292 app_list_controller_->GetTargetVisibility();
295 aura::Window* Shell::GetAppListWindow() {
296 return app_list_controller_.get() ? app_list_controller_->GetWindow() : NULL;
299 app_list::AppListView* Shell::GetAppListView() {
300 return app_list_controller_.get() ? app_list_controller_->GetView() : NULL;
303 bool Shell::IsSystemModalWindowOpen() const {
304 if (simulate_modal_window_open_for_testing_)
305 return true;
306 const std::vector<aura::Window*> containers = GetContainersFromAllRootWindows(
307 internal::kShellWindowId_SystemModalContainer, NULL);
308 for (std::vector<aura::Window*>::const_iterator cit = containers.begin();
309 cit != containers.end(); ++cit) {
310 for (aura::Window::Windows::const_iterator wit = (*cit)->children().begin();
311 wit != (*cit)->children().end(); ++wit) {
312 if ((*wit)->GetProperty(aura::client::kModalKey) ==
313 ui::MODAL_TYPE_SYSTEM && (*wit)->TargetVisibility()) {
314 return true;
318 return false;
321 views::NonClientFrameView* Shell::CreateDefaultNonClientFrameView(
322 views::Widget* widget) {
323 // Use translucent-style window frames for dialogs.
324 return new CustomFrameViewAsh(widget);
327 void Shell::RotateFocus(Direction direction) {
328 focus_cycler_->RotateFocus(
329 direction == FORWARD ? internal::FocusCycler::FORWARD :
330 internal::FocusCycler::BACKWARD);
333 void Shell::SetDisplayWorkAreaInsets(Window* contains,
334 const gfx::Insets& insets) {
335 if (!display_controller_->UpdateWorkAreaOfDisplayNearestWindow(
336 contains, insets)) {
337 return;
339 FOR_EACH_OBSERVER(ShellObserver, observers_,
340 OnDisplayWorkAreaInsetsChanged());
343 void Shell::OnLoginStateChanged(user::LoginStatus status) {
344 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLoginStateChanged(status));
347 void Shell::OnLoginUserProfilePrepared() {
348 CreateShelf();
349 CreateKeyboard();
352 void Shell::UpdateAfterLoginStatusChange(user::LoginStatus status) {
353 RootWindowControllerList controllers = GetAllRootWindowControllers();
354 for (RootWindowControllerList::iterator iter = controllers.begin();
355 iter != controllers.end(); ++iter)
356 (*iter)->UpdateAfterLoginStatusChange(status);
359 void Shell::OnAppTerminating() {
360 FOR_EACH_OBSERVER(ShellObserver, observers_, OnAppTerminating());
363 void Shell::OnLockStateChanged(bool locked) {
364 FOR_EACH_OBSERVER(ShellObserver, observers_, OnLockStateChanged(locked));
365 #ifndef NDEBUG
366 // Make sure that there is no system modal in Lock layer when unlocked.
367 if (!locked) {
368 std::vector<aura::Window*> containers = GetContainersFromAllRootWindows(
369 internal::kShellWindowId_LockSystemModalContainer,
370 GetPrimaryRootWindow());
371 for (std::vector<aura::Window*>::const_iterator iter = containers.begin();
372 iter != containers.end(); ++iter) {
373 DCHECK_EQ(0u, (*iter)->children().size());
376 #endif
379 void Shell::CreateShelf() {
380 RootWindowControllerList controllers = GetAllRootWindowControllers();
381 for (RootWindowControllerList::iterator iter = controllers.begin();
382 iter != controllers.end(); ++iter)
383 (*iter)->shelf()->CreateShelf();
386 void Shell::CreateKeyboard() {
387 // TODO(bshe): Primary root window controller may not be the controller to
388 // attach virtual keyboard. See http://crbug.com/303429
389 InitKeyboard();
390 if (keyboard::IsKeyboardUsabilityExperimentEnabled()) {
391 display_controller()->virtual_keyboard_window_controller()->
392 ActivateKeyboard(keyboard_controller_.get());
393 } else {
394 GetPrimaryRootWindowController()->
395 ActivateKeyboard(keyboard_controller_.get());
399 void Shell::ShowShelf() {
400 RootWindowControllerList controllers = GetAllRootWindowControllers();
401 for (RootWindowControllerList::iterator iter = controllers.begin();
402 iter != controllers.end(); ++iter)
403 (*iter)->ShowShelf();
406 void Shell::AddShellObserver(ShellObserver* observer) {
407 observers_.AddObserver(observer);
410 void Shell::RemoveShellObserver(ShellObserver* observer) {
411 observers_.RemoveObserver(observer);
414 void Shell::UpdateShelfVisibility() {
415 RootWindowControllerList controllers = GetAllRootWindowControllers();
416 for (RootWindowControllerList::iterator iter = controllers.begin();
417 iter != controllers.end(); ++iter)
418 if ((*iter)->shelf())
419 (*iter)->UpdateShelfVisibility();
422 void Shell::SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior,
423 aura::Window* root_window) {
424 ash::internal::ShelfLayoutManager::ForShelf(root_window)->
425 SetAutoHideBehavior(behavior);
428 ShelfAutoHideBehavior Shell::GetShelfAutoHideBehavior(
429 aura::Window* root_window) const {
430 return ash::internal::ShelfLayoutManager::ForShelf(root_window)->
431 auto_hide_behavior();
434 void Shell::SetShelfAlignment(ShelfAlignment alignment,
435 aura::Window* root_window) {
436 if (ash::internal::ShelfLayoutManager::ForShelf(root_window)->
437 SetAlignment(alignment)) {
438 FOR_EACH_OBSERVER(
439 ShellObserver, observers_, OnShelfAlignmentChanged(root_window));
443 ShelfAlignment Shell::GetShelfAlignment(aura::Window* root_window) {
444 return internal::GetRootWindowController(root_window)->
445 GetShelfLayoutManager()->GetAlignment();
448 void Shell::SetDimming(bool should_dim) {
449 RootWindowControllerList controllers = GetAllRootWindowControllers();
450 for (RootWindowControllerList::iterator iter = controllers.begin();
451 iter != controllers.end(); ++iter)
452 (*iter)->screen_dimmer()->SetDimming(should_dim);
455 void Shell::NotifyFullscreenStateChange(bool is_fullscreen,
456 aura::Window* root_window) {
457 FOR_EACH_OBSERVER(ShellObserver, observers_, OnFullscreenStateChanged(
458 is_fullscreen, root_window));
461 void Shell::CreateModalBackground(aura::Window* window) {
462 if (!modality_filter_) {
463 modality_filter_.reset(new internal::SystemModalContainerEventFilter(this));
464 AddPreTargetHandler(modality_filter_.get());
466 RootWindowControllerList controllers = GetAllRootWindowControllers();
467 for (RootWindowControllerList::iterator iter = controllers.begin();
468 iter != controllers.end(); ++iter)
469 (*iter)->GetSystemModalLayoutManager(window)->CreateModalBackground();
472 void Shell::OnModalWindowRemoved(aura::Window* removed) {
473 RootWindowControllerList controllers = GetAllRootWindowControllers();
474 bool activated = false;
475 for (RootWindowControllerList::iterator iter = controllers.begin();
476 iter != controllers.end() && !activated; ++iter) {
477 activated = (*iter)->GetSystemModalLayoutManager(removed)->
478 ActivateNextModalWindow();
480 if (!activated) {
481 RemovePreTargetHandler(modality_filter_.get());
482 modality_filter_.reset();
483 for (RootWindowControllerList::iterator iter = controllers.begin();
484 iter != controllers.end(); ++iter)
485 (*iter)->GetSystemModalLayoutManager(removed)->DestroyModalBackground();
489 WebNotificationTray* Shell::GetWebNotificationTray() {
490 return GetPrimaryRootWindowController()->shelf()->
491 status_area_widget()->web_notification_tray();
494 bool Shell::HasPrimaryStatusArea() {
495 ShelfWidget* shelf = GetPrimaryRootWindowController()->shelf();
496 return shelf && shelf->status_area_widget();
499 SystemTray* Shell::GetPrimarySystemTray() {
500 return GetPrimaryRootWindowController()->GetSystemTray();
503 ShelfDelegate* Shell::GetShelfDelegate() {
504 if (!shelf_delegate_) {
505 shelf_model_.reset(new ShelfModel);
506 // Creates ShelfItemDelegateManager before ShelfDelegate.
507 shelf_item_delegate_manager_.reset(
508 new ShelfItemDelegateManager(shelf_model_.get()));
510 shelf_delegate_.reset(delegate_->CreateShelfDelegate(shelf_model_.get()));
511 scoped_ptr<ShelfItemDelegate> controller(
512 new internal::AppListShelfItemDelegate);
514 // Finding the shelf model's location of the app list and setting its
515 // ShelfItemDelegate.
516 int app_list_index = shelf_model_->GetItemIndexForType(TYPE_APP_LIST);
517 DCHECK_GE(app_list_index, 0);
518 LauncherID app_list_id = shelf_model_->items()[app_list_index].id;
519 DCHECK(app_list_id);
520 shelf_item_delegate_manager_->SetShelfItemDelegate(app_list_id,
521 controller.Pass());
522 shelf_window_watcher_.reset(new internal::ShelfWindowWatcher(
523 shelf_model_.get(),
524 shelf_item_delegate_manager_.get()));
526 return shelf_delegate_.get();
529 void Shell::SetTouchHudProjectionEnabled(bool enabled) {
530 if (is_touch_hud_projection_enabled_ == enabled)
531 return;
533 is_touch_hud_projection_enabled_ = enabled;
534 FOR_EACH_OBSERVER(ShellObserver, observers_,
535 OnTouchHudProjectionToggled(enabled));
538 #if defined(OS_CHROMEOS)
539 ash::FirstRunHelper* Shell::CreateFirstRunHelper() {
540 return new ash::FirstRunHelperImpl;
542 #endif // defined(OS_CHROMEOS)
544 void Shell::DoInitialWorkspaceAnimation() {
545 return GetPrimaryRootWindowController()->workspace_controller()->
546 DoInitialAnimation();
549 ////////////////////////////////////////////////////////////////////////////////
550 // Shell, private:
552 Shell::Shell(ShellDelegate* delegate)
553 : screen_(new ScreenAsh),
554 target_root_window_(NULL),
555 scoped_target_root_window_(NULL),
556 delegate_(delegate),
557 window_positioner_(new WindowPositioner),
558 activation_client_(NULL),
559 #if defined(OS_CHROMEOS) && defined(USE_X11)
560 output_configurator_(new chromeos::OutputConfigurator()),
561 #endif // defined(OS_CHROMEOS) && defined(USE_X11)
562 native_cursor_manager_(new AshNativeCursorManager),
563 cursor_manager_(scoped_ptr<views::corewm::NativeCursorManager>(
564 native_cursor_manager_)),
565 simulate_modal_window_open_for_testing_(false),
566 is_touch_hud_projection_enabled_(false) {
567 DCHECK(delegate_.get());
568 display_manager_.reset(new internal::DisplayManager);
570 ANNOTATE_LEAKING_OBJECT_PTR(screen_); // see crbug.com/156466
571 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_ALTERNATE, screen_);
572 if (!gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE))
573 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_);
574 display_controller_.reset(new DisplayController);
575 #if defined(OS_CHROMEOS) && defined(USE_X11)
576 bool is_panel_fitting_disabled =
577 content::GpuDataManager::GetInstance()->IsFeatureBlacklisted(
578 gpu::GPU_FEATURE_TYPE_PANEL_FITTING);
580 output_configurator_->Init(!is_panel_fitting_disabled);
581 user_metrics_recorder_.reset(new UserMetricsRecorder);
583 base::MessagePumpX11::Current()->AddDispatcherForRootWindow(
584 output_configurator());
585 // We can't do this with a root window listener because XI_HierarchyChanged
586 // messages don't have a target window.
587 base::MessagePumpX11::Current()->AddObserver(output_configurator());
588 #endif // defined(OS_CHROMEOS)
590 #if defined(OS_CHROMEOS)
591 internal::PowerStatus::Initialize();
592 #endif
595 Shell::~Shell() {
596 TRACE_EVENT0("shutdown", "ash::Shell::Destructor");
598 views::FocusManagerFactory::Install(NULL);
600 // Remove the focus from any window. This will prevent overhead and side
601 // effects (e.g. crashes) from changing focus during shutdown.
602 // See bug crbug.com/134502.
603 aura::client::GetFocusClient(GetPrimaryRootWindow())->FocusWindow(NULL);
605 // Please keep in same order as in Init() because it's easy to miss one.
606 if (window_modality_controller_)
607 window_modality_controller_.reset();
608 RemovePreTargetHandler(event_rewriter_filter_.get());
609 RemovePreTargetHandler(user_activity_detector_.get());
610 RemovePreTargetHandler(overlay_filter_.get());
611 RemovePreTargetHandler(input_method_filter_.get());
612 RemovePreTargetHandler(accelerator_filter_.get());
613 RemovePreTargetHandler(event_transformation_handler_.get());
614 RemovePreTargetHandler(toplevel_window_event_handler_.get());
615 RemovePostTargetHandler(toplevel_window_event_handler_.get());
616 RemovePreTargetHandler(system_gesture_filter_.get());
617 RemovePreTargetHandler(keyboard_metrics_filter_.get());
618 if (mouse_cursor_filter_)
619 RemovePreTargetHandler(mouse_cursor_filter_.get());
621 // TooltipController is deleted with the Shell so removing its references.
622 RemovePreTargetHandler(tooltip_controller_.get());
624 // AppList needs to be released before shelf layout manager, which is
625 // destroyed with shelf container in the loop below. However, app list
626 // container is now on top of shelf container and released after it.
627 // TODO(xiyuan): Move it back when app list container is no longer needed.
628 app_list_controller_.reset();
630 // Destroy SystemTrayDelegate before destroying the status area(s).
631 system_tray_delegate_->Shutdown();
632 system_tray_delegate_.reset();
634 locale_notification_controller_.reset();
636 // Drag-and-drop must be canceled prior to close all windows.
637 drag_drop_controller_.reset();
639 // Controllers who have WindowObserver added must be deleted
640 // before |display_controller_| is deleted.
642 #if defined(OS_CHROMEOS)
643 // VideoActivityNotifier must be deleted before |video_detector_| is
644 // deleted because it's observing video activity through
645 // VideoDetectorObserver interface.
646 video_activity_notifier_.reset();
647 #endif // defined(OS_CHROMEOS)
648 video_detector_.reset();
650 shadow_controller_.reset();
651 resize_shadow_controller_.reset();
653 window_selector_controller_.reset();
654 window_cycle_controller_.reset();
655 mru_window_tracker_.reset();
657 // |shelf_window_watcher_| has a weak pointer to |shelf_Model_|
658 // and has window observers.
659 shelf_window_watcher_.reset();
661 // Destroy all child windows including widgets.
662 display_controller_->CloseChildWindows();
663 display_controller_->CloseNonDesktopDisplay();
665 // Chrome implementation of shelf delegate depends on FocusClient,
666 // so must be deleted before |focus_client_|.
667 shelf_delegate_.reset();
668 focus_client_.reset();
670 // Destroy SystemTrayNotifier after destroying SystemTray as TrayItems
671 // needs to remove observers from it.
672 system_tray_notifier_.reset();
674 // These need a valid Shell instance to clean up properly, so explicitly
675 // delete them before invalidating the instance.
676 // Alphabetical. TODO(oshima): sort.
677 magnification_controller_.reset();
678 partial_magnification_controller_.reset();
679 tooltip_controller_.reset();
680 event_client_.reset();
681 nested_dispatcher_controller_.reset();
682 toplevel_window_event_handler_.reset();
683 user_action_client_.reset();
684 visibility_controller_.reset();
685 // |shelf_item_delegate_manager_| observes |shelf_model_|. It must be
686 // destroyed before |shelf_model_| is destroyed.
687 shelf_item_delegate_manager_.reset();
688 shelf_model_.reset();
690 power_button_controller_.reset();
691 lock_state_controller_.reset();
693 resolution_notification_controller_.reset();
694 desktop_background_controller_.reset();
696 // This also deletes all RootWindows. Note that we invoke Shutdown() on
697 // DisplayController before resetting |display_controller_|, since destruction
698 // of its owned RootWindowControllers relies on the value.
699 display_controller_->Shutdown();
700 display_controller_.reset();
701 screen_position_controller_.reset();
703 keyboard_controller_.reset();
704 accessibility_delegate_.reset();
705 new_window_delegate_.reset();
706 media_delegate_.reset();
708 #if defined(OS_CHROMEOS) && defined(USE_X11)
709 if (display_change_observer_)
710 output_configurator_->RemoveObserver(display_change_observer_.get());
711 if (output_configurator_animation_)
712 output_configurator_->RemoveObserver(output_configurator_animation_.get());
713 if (display_error_observer_)
714 output_configurator_->RemoveObserver(display_error_observer_.get());
715 base::MessagePumpX11::Current()->RemoveDispatcherForRootWindow(
716 output_configurator());
717 base::MessagePumpX11::Current()->RemoveObserver(output_configurator());
718 display_change_observer_.reset();
719 #endif // defined(OS_CHROMEOS)
721 #if defined(OS_CHROMEOS)
722 internal::PowerStatus::Shutdown();
723 #endif
725 DCHECK(instance_ == this);
726 instance_ = NULL;
729 void Shell::Init() {
730 CommandLine* command_line = CommandLine::ForCurrentProcess();
732 delegate_->PreInit();
733 if (keyboard::IsKeyboardUsabilityExperimentEnabled()) {
734 display_manager_->SetSecondDisplayMode(
735 internal::DisplayManager::VIRTUAL_KEYBOARD);
737 bool display_initialized = display_manager_->InitFromCommandLine();
738 #if defined(OS_CHROMEOS) && defined(USE_X11)
739 output_configurator_animation_.reset(
740 new internal::OutputConfiguratorAnimation());
741 output_configurator_->AddObserver(output_configurator_animation_.get());
743 if (!display_initialized && base::SysInfo::IsRunningOnChromeOS()) {
744 display_change_observer_.reset(new internal::DisplayChangeObserver);
745 // Register |display_change_observer_| first so that the rest of
746 // observer gets invoked after the root windows are configured.
747 output_configurator_->AddObserver(display_change_observer_.get());
748 display_error_observer_.reset(new internal::DisplayErrorObserver());
749 output_configurator_->AddObserver(display_error_observer_.get());
750 output_configurator_->set_state_controller(display_change_observer_.get());
751 output_configurator_->set_mirroring_controller(display_manager_.get());
752 output_configurator_->Start(
753 delegate_->IsFirstRunAfterBoot() ? kChromeOsBootColor : 0);
754 display_initialized = true;
756 #endif // defined(OS_CHROMEOS) && defined(USE_X11)
757 if (!display_initialized)
758 display_manager_->InitDefaultDisplay();
760 // Install the custom factory first so that views::FocusManagers for Tray,
761 // Shelf, and WallPaper could be created by the factory.
762 views::FocusManagerFactory::Install(new AshFocusManagerFactory);
764 // Env creates the compositor. Historically it seems to have been implicitly
765 // initialized first by the ActivationController, but now that FocusController
766 // no longer does this we need to do it explicitly.
767 aura::Env::CreateInstance();
769 // The WindowModalityController needs to be at the front of the input event
770 // pretarget handler list to ensure that it processes input events when modal
771 // windows are active.
772 window_modality_controller_.reset(
773 new views::corewm::WindowModalityController(this));
775 AddPreTargetHandler(this);
777 env_filter_.reset(new views::corewm::CompoundEventFilter);
778 AddPreTargetHandler(env_filter_.get());
780 views::corewm::FocusController* focus_controller =
781 new views::corewm::FocusController(new wm::AshFocusRules);
782 focus_client_.reset(focus_controller);
783 activation_client_ = focus_controller;
784 activation_client_->AddObserver(this);
785 focus_cycler_.reset(new internal::FocusCycler());
787 screen_position_controller_.reset(new internal::ScreenPositionController);
788 root_window_host_factory_.reset(delegate_->CreateRootWindowHostFactory());
790 display_controller_->Start();
791 display_controller_->InitPrimaryDisplay();
792 aura::Window* root_window = display_controller_->GetPrimaryRootWindow();
793 target_root_window_ = root_window;
795 resolution_notification_controller_.reset(
796 new internal::ResolutionNotificationController);
798 cursor_manager_.SetDisplay(DisplayController::GetPrimaryDisplay());
800 nested_dispatcher_controller_.reset(new NestedDispatcherController);
801 accelerator_controller_.reset(new AcceleratorController);
803 // The order in which event filters are added is significant.
804 event_rewriter_filter_.reset(new internal::EventRewriterEventFilter);
805 AddPreTargetHandler(event_rewriter_filter_.get());
807 #if defined(OS_CHROMEOS)
808 // The StickyKeysController also rewrites events and must be added
809 // before observers, but after the EventRewriterEventFilter.
810 sticky_keys_controller_.reset(new StickyKeysController);
811 AddPreTargetHandler(sticky_keys_controller_.get());
812 #endif
814 // UserActivityDetector passes events to observers, so let them get
815 // rewritten first.
816 user_activity_detector_.reset(new UserActivityDetector);
817 AddPreTargetHandler(user_activity_detector_.get());
819 overlay_filter_.reset(new internal::OverlayEventFilter);
820 AddPreTargetHandler(overlay_filter_.get());
821 AddShellObserver(overlay_filter_.get());
823 input_method_filter_.reset(new views::corewm::InputMethodEventFilter(
824 root_window->GetDispatcher()->host()->GetAcceleratedWidget()));
825 AddPreTargetHandler(input_method_filter_.get());
827 accelerator_filter_.reset(new internal::AcceleratorFilter);
828 AddPreTargetHandler(accelerator_filter_.get());
830 event_transformation_handler_.reset(new internal::EventTransformationHandler);
831 AddPreTargetHandler(event_transformation_handler_.get());
833 toplevel_window_event_handler_.reset(new ToplevelWindowEventHandler);
834 AddPreTargetHandler(toplevel_window_event_handler_.get());
835 AddPostTargetHandler(toplevel_window_event_handler_.get());
837 system_gesture_filter_.reset(new internal::SystemGestureEventFilter);
838 AddPreTargetHandler(system_gesture_filter_.get());
840 keyboard_metrics_filter_.reset(new internal::KeyboardUMAEventFilter);
841 AddPreTargetHandler(keyboard_metrics_filter_.get());
843 // The keyboard system must be initialized before the RootWindowController is
844 // created.
845 if (keyboard::IsKeyboardEnabled())
846 keyboard::InitializeKeyboard();
848 lock_state_controller_.reset(new LockStateController);
849 power_button_controller_.reset(new PowerButtonController(
850 lock_state_controller_.get()));
851 AddShellObserver(lock_state_controller_.get());
853 drag_drop_controller_.reset(new internal::DragDropController);
854 mouse_cursor_filter_.reset(new internal::MouseCursorEventFilter());
855 PrependPreTargetHandler(mouse_cursor_filter_.get());
857 // Create Controllers that may need root window.
858 // TODO(oshima): Move as many controllers before creating
859 // RootWindowController as possible.
860 visibility_controller_.reset(new AshVisibilityController);
861 user_action_client_.reset(delegate_->CreateUserActionClient());
863 magnification_controller_.reset(
864 MagnificationController::CreateInstance());
865 mru_window_tracker_.reset(new MruWindowTracker(activation_client_));
867 partial_magnification_controller_.reset(
868 new PartialMagnificationController());
870 autoclick_controller_.reset(AutoclickController::CreateInstance());
872 high_contrast_controller_.reset(new HighContrastController);
873 video_detector_.reset(new VideoDetector);
874 window_cycle_controller_.reset(new WindowCycleController());
875 window_selector_controller_.reset(new WindowSelectorController());
877 tooltip_controller_.reset(
878 new views::corewm::TooltipController(
879 scoped_ptr<views::corewm::Tooltip>(
880 new views::corewm::TooltipAura(gfx::SCREEN_TYPE_ALTERNATE))));
881 AddPreTargetHandler(tooltip_controller_.get());
883 event_client_.reset(new internal::EventClientImpl);
885 // This controller needs to be set before SetupManagedWindowMode.
886 desktop_background_controller_.reset(new DesktopBackgroundController());
887 user_wallpaper_delegate_.reset(delegate_->CreateUserWallpaperDelegate());
889 // StatusAreaWidget uses Shell's CapsLockDelegate.
890 caps_lock_delegate_.reset(delegate_->CreateCapsLockDelegate());
892 session_state_delegate_.reset(delegate_->CreateSessionStateDelegate());
893 accessibility_delegate_.reset(delegate_->CreateAccessibilityDelegate());
894 new_window_delegate_.reset(delegate_->CreateNewWindowDelegate());
895 media_delegate_.reset(delegate_->CreateMediaDelegate());
897 if (!command_line->HasSwitch(views::corewm::switches::kNoDropShadows)) {
898 resize_shadow_controller_.reset(new internal::ResizeShadowController());
899 shadow_controller_.reset(
900 new views::corewm::ShadowController(activation_client_));
903 // Create system_tray_notifier_ before the delegate.
904 system_tray_notifier_.reset(new ash::SystemTrayNotifier());
906 // Initialize system_tray_delegate_ before initializing StatusAreaWidget.
907 system_tray_delegate_.reset(delegate()->CreateSystemTrayDelegate());
908 DCHECK(system_tray_delegate_.get());
910 locale_notification_controller_.reset(
911 new internal::LocaleNotificationController);
913 // Initialize system_tray_delegate_ after StatusAreaWidget is created.
914 system_tray_delegate_->Initialize();
916 // TODO(oshima): Initialize all RootWindowControllers once, and
917 // initialize controller/delegates above when initializing the
918 // primary root window controller.
919 internal::RootWindowController::CreateForPrimaryDisplay(
920 root_window->GetDispatcher());
922 display_controller_->InitSecondaryDisplays();
924 // It needs to be created after RootWindowController has been created
925 // (which calls OnWindowResized has been called, otherwise the
926 // widget will not paint when restoring after a browser crash. Also it needs
927 // to be created after InitSecondaryDisplays() to initialize the wallpapers in
928 // the correct size.
929 user_wallpaper_delegate_->InitializeWallpaper();
931 if (initially_hide_cursor_)
932 cursor_manager_.HideCursor();
933 cursor_manager_.SetCursor(ui::kCursorPointer);
935 #if defined(OS_CHROMEOS)
936 // Set accelerator controller delegates.
937 accelerator_controller_->SetBrightnessControlDelegate(
938 scoped_ptr<ash::BrightnessControlDelegate>(
939 new ash::system::BrightnessControllerChromeos).Pass());
941 power_event_observer_.reset(new internal::PowerEventObserver());
942 user_activity_notifier_.reset(
943 new internal::UserActivityNotifier(user_activity_detector_.get()));
944 video_activity_notifier_.reset(
945 new internal::VideoActivityNotifier(video_detector_.get()));
946 #endif
948 weak_display_manager_factory_.reset(
949 new base::WeakPtrFactory<internal::DisplayManager>(
950 display_manager_.get()));
951 // The compositor thread and main message loop have to be running in
952 // order to create mirror window. Run it after the main message loop
953 // is started.
954 base::MessageLoopForUI::current()->PostTask(
955 FROM_HERE,
956 base::Bind(&internal::DisplayManager::CreateMirrorWindowIfAny,
957 weak_display_manager_factory_->GetWeakPtr()));
960 void Shell::InitKeyboard() {
961 if (keyboard::IsKeyboardEnabled()) {
962 if (keyboard_controller_.get()) {
963 RootWindowControllerList controllers = GetAllRootWindowControllers();
964 for (RootWindowControllerList::iterator iter = controllers.begin();
965 iter != controllers.end(); ++iter) {
966 (*iter)->DeactivateKeyboard(keyboard_controller_.get());
969 keyboard::KeyboardControllerProxy* proxy =
970 delegate_->CreateKeyboardControllerProxy();
971 keyboard_controller_.reset(
972 new keyboard::KeyboardController(proxy));
976 void Shell::InitRootWindow(aura::Window* root_window) {
977 DCHECK(activation_client_);
978 DCHECK(visibility_controller_.get());
979 DCHECK(drag_drop_controller_.get());
980 DCHECK(window_cycle_controller_.get());
982 aura::client::SetFocusClient(root_window, focus_client_.get());
983 input_method_filter_->SetInputMethodPropertyInRootWindow(root_window);
984 aura::client::SetActivationClient(root_window, activation_client_);
985 views::corewm::FocusController* focus_controller =
986 static_cast<views::corewm::FocusController*>(activation_client_);
987 root_window->AddPreTargetHandler(focus_controller);
988 aura::client::SetVisibilityClient(root_window, visibility_controller_.get());
989 aura::client::SetDragDropClient(root_window, drag_drop_controller_.get());
990 aura::client::SetScreenPositionClient(root_window,
991 screen_position_controller_.get());
992 aura::client::SetCursorClient(root_window, &cursor_manager_);
993 aura::client::SetTooltipClient(root_window, tooltip_controller_.get());
994 aura::client::SetEventClient(root_window, event_client_.get());
995 aura::client::SetWindowMoveClient(root_window,
996 toplevel_window_event_handler_.get());
998 if (nested_dispatcher_controller_) {
999 aura::client::SetDispatcherClient(root_window,
1000 nested_dispatcher_controller_.get());
1002 if (user_action_client_)
1003 aura::client::SetUserActionClient(root_window, user_action_client_.get());
1006 bool Shell::CanWindowReceiveEvents(aura::Window* window) {
1007 RootWindowControllerList controllers = GetAllRootWindowControllers();
1008 for (RootWindowControllerList::iterator iter = controllers.begin();
1009 iter != controllers.end(); ++iter) {
1010 internal::SystemModalContainerLayoutManager* layout_manager =
1011 (*iter)->GetSystemModalLayoutManager(window);
1012 if (layout_manager && layout_manager->CanWindowReceiveEvents(window))
1013 return true;
1015 return false;
1018 ////////////////////////////////////////////////////////////////////////////////
1019 // Shell, ui::EventTarget overrides:
1021 bool Shell::CanAcceptEvent(const ui::Event& event) {
1022 return true;
1025 ui::EventTarget* Shell::GetParentTarget() {
1026 return aura::Env::GetInstance();
1029 scoped_ptr<ui::EventTargetIterator> Shell::GetChildIterator() const {
1030 return scoped_ptr<ui::EventTargetIterator>();
1033 ui::EventTargeter* Shell::GetEventTargeter() {
1034 NOTREACHED();
1035 return NULL;
1038 void Shell::OnEvent(ui::Event* event) {
1041 ////////////////////////////////////////////////////////////////////////////////
1042 // Shell, aura::client::ActivationChangeObserver implementation:
1044 void Shell::OnWindowActivated(aura::Window* gained_active,
1045 aura::Window* lost_active) {
1046 if (gained_active)
1047 target_root_window_ = gained_active->GetRootWindow();
1050 } // namespace ash