Temporary allocation of 20Mb before and after call to ServiceProcessControl::GetHisto...
[chromium-blink-merge.git] / ash / root_window_controller.cc
blob0e07dbe03e94cafc518cad419a90bb223e21f37c
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/root_window_controller.h"
7 #include <queue>
8 #include <vector>
10 #include "ash/ash_constants.h"
11 #include "ash/ash_switches.h"
12 #include "ash/desktop_background/desktop_background_controller.h"
13 #include "ash/desktop_background/desktop_background_widget_controller.h"
14 #include "ash/desktop_background/user_wallpaper_delegate.h"
15 #include "ash/display/display_manager.h"
16 #include "ash/focus_cycler.h"
17 #include "ash/high_contrast/high_contrast_controller.h"
18 #include "ash/host/ash_window_tree_host.h"
19 #include "ash/root_window_settings.h"
20 #include "ash/session/session_state_delegate.h"
21 #include "ash/shelf/shelf_layout_manager.h"
22 #include "ash/shelf/shelf_types.h"
23 #include "ash/shelf/shelf_widget.h"
24 #include "ash/shell.h"
25 #include "ash/shell_delegate.h"
26 #include "ash/shell_factory.h"
27 #include "ash/shell_window_ids.h"
28 #include "ash/switchable_windows.h"
29 #include "ash/system/status_area_widget.h"
30 #include "ash/system/tray/system_tray_delegate.h"
31 #include "ash/system/tray/system_tray_notifier.h"
32 #include "ash/touch/touch_hud_debug.h"
33 #include "ash/touch/touch_hud_projection.h"
34 #include "ash/touch/touch_observer_hud.h"
35 #include "ash/wm/always_on_top_controller.h"
36 #include "ash/wm/dock/docked_window_layout_manager.h"
37 #include "ash/wm/lock_layout_manager.h"
38 #include "ash/wm/panels/attached_panel_window_targeter.h"
39 #include "ash/wm/panels/panel_layout_manager.h"
40 #include "ash/wm/panels/panel_window_event_handler.h"
41 #include "ash/wm/root_window_layout_manager.h"
42 #include "ash/wm/screen_dimmer.h"
43 #include "ash/wm/stacking_controller.h"
44 #include "ash/wm/status_area_layout_manager.h"
45 #include "ash/wm/system_background_controller.h"
46 #include "ash/wm/system_modal_container_layout_manager.h"
47 #include "ash/wm/window_properties.h"
48 #include "ash/wm/window_state.h"
49 #include "ash/wm/window_util.h"
50 #include "ash/wm/workspace/workspace_layout_manager.h"
51 #include "ash/wm/workspace_controller.h"
52 #include "base/command_line.h"
53 #include "base/time/time.h"
54 #include "ui/aura/client/aura_constants.h"
55 #include "ui/aura/client/screen_position_client.h"
56 #include "ui/aura/window.h"
57 #include "ui/aura/window_delegate.h"
58 #include "ui/aura/window_event_dispatcher.h"
59 #include "ui/aura/window_observer.h"
60 #include "ui/aura/window_tracker.h"
61 #include "ui/base/hit_test.h"
62 #include "ui/base/models/menu_model.h"
63 #include "ui/gfx/display.h"
64 #include "ui/gfx/screen.h"
65 #include "ui/keyboard/keyboard_controller.h"
66 #include "ui/keyboard/keyboard_util.h"
67 #include "ui/views/controls/menu/menu_runner.h"
68 #include "ui/views/view_model.h"
69 #include "ui/views/view_model_utils.h"
70 #include "ui/wm/core/capture_controller.h"
71 #include "ui/wm/core/easy_resize_window_targeter.h"
72 #include "ui/wm/core/visibility_controller.h"
73 #include "ui/wm/core/window_util.h"
74 #include "ui/wm/public/drag_drop_client.h"
75 #include "ui/wm/public/tooltip_client.h"
76 #include "ui/wm/public/window_types.h"
78 #if defined(OS_CHROMEOS)
79 #include "ash/ash_touch_exploration_manager_chromeos.h"
80 #include "ash/wm/boot_splash_screen_chromeos.h"
81 #include "ui/chromeos/touch_exploration_controller.h"
82 #endif
84 namespace ash {
85 namespace {
87 #if defined(OS_CHROMEOS)
88 // Duration for the animation that hides the boot splash screen, in
89 // milliseconds. This should be short enough in relation to
90 // wm/window_animation.cc's brightness/grayscale fade animation that the login
91 // background image animation isn't hidden by the splash screen animation.
92 const int kBootSplashScreenHideDurationMs = 500;
93 #endif
95 // Creates a new window for use as a container.
96 aura::Window* CreateContainer(int window_id,
97 const char* name,
98 aura::Window* parent) {
99 aura::Window* container = new aura::Window(NULL);
100 container->set_id(window_id);
101 container->SetName(name);
102 container->Init(aura::WINDOW_LAYER_NOT_DRAWN);
103 parent->AddChild(container);
104 if (window_id != kShellWindowId_UnparentedControlContainer)
105 container->Show();
106 return container;
109 float ToRelativeValue(int value, int src, int dst) {
110 return static_cast<float>(value) / static_cast<float>(src) * dst;
113 void MoveOriginRelativeToSize(const gfx::Size& src_size,
114 const gfx::Size& dst_size,
115 gfx::Rect* bounds_in_out) {
116 gfx::Point origin = bounds_in_out->origin();
117 bounds_in_out->set_origin(gfx::Point(
118 ToRelativeValue(origin.x(), src_size.width(), dst_size.width()),
119 ToRelativeValue(origin.y(), src_size.height(), dst_size.height())));
122 // Reparents |window| to |new_parent|.
123 void ReparentWindow(aura::Window* window, aura::Window* new_parent) {
124 const gfx::Size src_size = window->parent()->bounds().size();
125 const gfx::Size dst_size = new_parent->bounds().size();
126 // Update the restore bounds to make it relative to the display.
127 wm::WindowState* state = wm::GetWindowState(window);
128 gfx::Rect restore_bounds;
129 bool has_restore_bounds = state->HasRestoreBounds();
131 bool update_bounds = (state->IsNormalOrSnapped() || state->IsMinimized()) &&
132 new_parent->id() != kShellWindowId_DockedContainer;
133 gfx::Rect local_bounds;
134 if (update_bounds) {
135 local_bounds = state->window()->bounds();
136 MoveOriginRelativeToSize(src_size, dst_size, &local_bounds);
139 if (has_restore_bounds) {
140 restore_bounds = state->GetRestoreBoundsInParent();
141 MoveOriginRelativeToSize(src_size, dst_size, &restore_bounds);
144 new_parent->AddChild(window);
146 // Docked windows have bounds handled by the layout manager in AddChild().
147 if (update_bounds)
148 window->SetBounds(local_bounds);
150 if (has_restore_bounds)
151 state->SetRestoreBoundsInParent(restore_bounds);
154 // Reparents the appropriate set of windows from |src| to |dst|.
155 void ReparentAllWindows(aura::Window* src, aura::Window* dst) {
156 // Set of windows to move.
157 const int kContainerIdsToMove[] = {
158 kShellWindowId_DefaultContainer,
159 kShellWindowId_DockedContainer,
160 kShellWindowId_PanelContainer,
161 kShellWindowId_AlwaysOnTopContainer,
162 kShellWindowId_SystemModalContainer,
163 kShellWindowId_LockSystemModalContainer,
164 kShellWindowId_UnparentedControlContainer, };
165 for (size_t i = 0; i < arraysize(kContainerIdsToMove); i++) {
166 int id = kContainerIdsToMove[i];
167 aura::Window* src_container = Shell::GetContainer(src, id);
168 aura::Window* dst_container = Shell::GetContainer(dst, id);
169 while (!src_container->children().empty()) {
170 // Restart iteration from the source container windows each time as they
171 // may change as a result of moving other windows.
172 aura::Window::Windows::const_iterator iter =
173 src_container->children().begin();
174 while (iter != src_container->children().end() &&
175 SystemModalContainerLayoutManager::IsModalBackground(*iter)) {
176 ++iter;
178 // If the entire window list is modal background windows then stop.
179 if (iter == src_container->children().end())
180 break;
181 ReparentWindow(*iter, dst_container);
186 // Mark the container window so that a widget added to this container will
187 // use the virtual screeen coordinates instead of parent.
188 void SetUsesScreenCoordinates(aura::Window* container) {
189 container->SetProperty(kUsesScreenCoordinatesKey, true);
192 // Mark the container window so that a widget added to this container will
193 // say in the same root window regardless of the bounds specified.
194 void DescendantShouldStayInSameRootWindow(aura::Window* container) {
195 container->SetProperty(kStayInSameRootWindowKey, true);
198 void SetUsesEasyResizeTargeter(aura::Window* container) {
199 gfx::Insets mouse_extend(-kResizeOutsideBoundsSize,
200 -kResizeOutsideBoundsSize,
201 -kResizeOutsideBoundsSize,
202 -kResizeOutsideBoundsSize);
203 gfx::Insets touch_extend = mouse_extend.Scale(
204 kResizeOutsideBoundsScaleForTouch);
205 container->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
206 new ::wm::EasyResizeWindowTargeter(container, mouse_extend,
207 touch_extend)));
210 // A window delegate which does nothing. Used to create a window that
211 // is a event target, but do nothing.
212 class EmptyWindowDelegate : public aura::WindowDelegate {
213 public:
214 EmptyWindowDelegate() {}
215 virtual ~EmptyWindowDelegate() {}
217 // aura::WindowDelegate overrides:
218 virtual gfx::Size GetMinimumSize() const OVERRIDE {
219 return gfx::Size();
221 virtual gfx::Size GetMaximumSize() const OVERRIDE {
222 return gfx::Size();
224 virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
225 const gfx::Rect& new_bounds) OVERRIDE {
227 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
228 return gfx::kNullCursor;
230 virtual int GetNonClientComponent(
231 const gfx::Point& point) const OVERRIDE {
232 return HTNOWHERE;
234 virtual bool ShouldDescendIntoChildForEventHandling(
235 aura::Window* child,
236 const gfx::Point& location) OVERRIDE {
237 return false;
239 virtual bool CanFocus() OVERRIDE {
240 return false;
242 virtual void OnCaptureLost() OVERRIDE {
244 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
246 virtual void OnDeviceScaleFactorChanged(
247 float device_scale_factor) OVERRIDE {
249 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {}
250 virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE {
251 delete this;
253 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {
255 virtual bool HasHitTestMask() const OVERRIDE {
256 return false;
258 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {}
260 private:
261 DISALLOW_COPY_AND_ASSIGN(EmptyWindowDelegate);
264 } // namespace
266 void RootWindowController::CreateForPrimaryDisplay(AshWindowTreeHost* host) {
267 RootWindowController* controller = new RootWindowController(host);
268 controller->Init(RootWindowController::PRIMARY,
269 Shell::GetInstance()->delegate()->IsFirstRunAfterBoot());
272 void RootWindowController::CreateForSecondaryDisplay(AshWindowTreeHost* host) {
273 RootWindowController* controller = new RootWindowController(host);
274 controller->Init(RootWindowController::SECONDARY, false /* first run */);
277 void RootWindowController::CreateForVirtualKeyboardDisplay(
278 AshWindowTreeHost* host) {
279 RootWindowController* controller = new RootWindowController(host);
280 controller->Init(RootWindowController::VIRTUAL_KEYBOARD,
281 false /* first run */);
284 // static
285 RootWindowController* RootWindowController::ForShelf(
286 const aura::Window* window) {
287 return GetRootWindowController(window->GetRootWindow());
290 // static
291 RootWindowController* RootWindowController::ForWindow(
292 const aura::Window* window) {
293 return GetRootWindowController(window->GetRootWindow());
296 // static
297 RootWindowController* RootWindowController::ForTargetRootWindow() {
298 return GetRootWindowController(Shell::GetTargetRootWindow());
301 // static
302 aura::Window* RootWindowController::GetContainerForWindow(
303 aura::Window* window) {
304 aura::Window* container = window->parent();
305 while (container && container->type() != ui::wm::WINDOW_TYPE_UNKNOWN)
306 container = container->parent();
307 return container;
310 RootWindowController::~RootWindowController() {
311 Shutdown();
312 ash_host_.reset();
313 // The CaptureClient needs to be around for as long as the RootWindow is
314 // valid.
315 capture_client_.reset();
318 aura::WindowTreeHost* RootWindowController::GetHost() {
319 return ash_host_->AsWindowTreeHost();
322 const aura::WindowTreeHost* RootWindowController::GetHost() const {
323 return ash_host_->AsWindowTreeHost();
326 aura::Window* RootWindowController::GetRootWindow() {
327 return GetHost()->window();
330 const aura::Window* RootWindowController::GetRootWindow() const {
331 return GetHost()->window();
334 void RootWindowController::SetWallpaperController(
335 DesktopBackgroundWidgetController* controller) {
336 wallpaper_controller_.reset(controller);
339 void RootWindowController::SetAnimatingWallpaperController(
340 AnimatingDesktopController* controller) {
341 if (animating_wallpaper_controller_.get())
342 animating_wallpaper_controller_->StopAnimating();
343 animating_wallpaper_controller_.reset(controller);
346 void RootWindowController::Shutdown() {
347 Shell* shell = Shell::GetInstance();
348 shell->RemoveShellObserver(this);
350 #if defined(OS_CHROMEOS)
351 if (touch_exploration_manager_) {
352 touch_exploration_manager_.reset();
354 #endif
356 if (animating_wallpaper_controller_.get())
357 animating_wallpaper_controller_->StopAnimating();
358 wallpaper_controller_.reset();
359 animating_wallpaper_controller_.reset();
360 aura::Window* root_window = GetRootWindow();
361 // Change the target root window before closing child windows. If any child
362 // being removed triggers a relayout of the shelf it will try to build a
363 // window list adding windows from the target root window's containers which
364 // may have already gone away.
365 if (Shell::GetTargetRootWindow() == root_window) {
366 shell->set_target_root_window(
367 Shell::GetPrimaryRootWindow() == root_window
368 ? NULL
369 : Shell::GetPrimaryRootWindow());
372 CloseChildWindows();
373 GetRootWindowSettings(root_window)->controller = NULL;
374 screen_dimmer_.reset();
375 workspace_controller_.reset();
376 // Forget with the display ID so that display lookup
377 // ends up with invalid display.
378 GetRootWindowSettings(root_window)->display_id =
379 gfx::Display::kInvalidDisplayID;
380 ash_host_->PrepareForShutdown();
382 system_background_.reset();
383 aura::client::SetScreenPositionClient(root_window, NULL);
386 SystemModalContainerLayoutManager*
387 RootWindowController::GetSystemModalLayoutManager(aura::Window* window) {
388 aura::Window* modal_container = NULL;
389 if (window) {
390 aura::Window* window_container = GetContainerForWindow(window);
391 if (window_container &&
392 window_container->id() >= kShellWindowId_LockScreenContainer) {
393 modal_container = GetContainer(kShellWindowId_LockSystemModalContainer);
394 } else {
395 modal_container = GetContainer(kShellWindowId_SystemModalContainer);
397 } else {
398 int modal_window_id = Shell::GetInstance()->session_state_delegate()
399 ->IsUserSessionBlocked() ? kShellWindowId_LockSystemModalContainer :
400 kShellWindowId_SystemModalContainer;
401 modal_container = GetContainer(modal_window_id);
403 return modal_container ? static_cast<SystemModalContainerLayoutManager*>(
404 modal_container->layout_manager()) : NULL;
407 aura::Window* RootWindowController::GetContainer(int container_id) {
408 return GetRootWindow()->GetChildById(container_id);
411 const aura::Window* RootWindowController::GetContainer(int container_id) const {
412 return ash_host_->AsWindowTreeHost()->window()->GetChildById(container_id);
415 void RootWindowController::ShowShelf() {
416 if (!shelf_->shelf())
417 return;
418 shelf_->shelf()->SetVisible(true);
419 shelf_->status_area_widget()->Show();
422 void RootWindowController::OnShelfCreated() {
423 if (panel_layout_manager_)
424 panel_layout_manager_->SetShelf(shelf_->shelf());
425 if (docked_layout_manager_) {
426 docked_layout_manager_->SetShelf(shelf_->shelf());
427 if (shelf_->shelf_layout_manager())
428 docked_layout_manager_->AddObserver(shelf_->shelf_layout_manager());
431 // Notify shell observers that the shelf has been created.
432 Shell::GetInstance()->OnShelfCreatedForRootWindow(GetRootWindow());
435 void RootWindowController::UpdateAfterLoginStatusChange(
436 user::LoginStatus status) {
437 if (status != user::LOGGED_IN_NONE)
438 mouse_event_target_.reset();
439 if (shelf_->status_area_widget())
440 shelf_->status_area_widget()->UpdateAfterLoginStatusChange(status);
443 void RootWindowController::HandleInitialDesktopBackgroundAnimationStarted() {
444 #if defined(OS_CHROMEOS)
445 if (CommandLine::ForCurrentProcess()->HasSwitch(
446 switches::kAshAnimateFromBootSplashScreen) &&
447 boot_splash_screen_.get()) {
448 // Make the splash screen fade out so it doesn't obscure the desktop
449 // wallpaper's brightness/grayscale animation.
450 boot_splash_screen_->StartHideAnimation(
451 base::TimeDelta::FromMilliseconds(kBootSplashScreenHideDurationMs));
453 #endif
456 void RootWindowController::OnWallpaperAnimationFinished(views::Widget* widget) {
457 // Make sure the wallpaper is visible.
458 system_background_->SetColor(SK_ColorBLACK);
459 #if defined(OS_CHROMEOS)
460 boot_splash_screen_.reset();
461 #endif
463 Shell::GetInstance()->user_wallpaper_delegate()->
464 OnWallpaperAnimationFinished();
465 // Only removes old component when wallpaper animation finished. If we
466 // remove the old one before the new wallpaper is done fading in there will
467 // be a white flash during the animation.
468 if (animating_wallpaper_controller()) {
469 DesktopBackgroundWidgetController* controller =
470 animating_wallpaper_controller()->GetController(true);
471 // |desktop_widget_| should be the same animating widget we try to move
472 // to |kDesktopController|. Otherwise, we may close |desktop_widget_|
473 // before move it to |kDesktopController|.
474 DCHECK_EQ(controller->widget(), widget);
475 // Release the old controller and close its background widget.
476 SetWallpaperController(controller);
480 void RootWindowController::CloseChildWindows() {
481 mouse_event_target_.reset();
483 // Remove observer as deactivating keyboard causes |docked_layout_manager_|
484 // to fire notifications.
485 if (docked_layout_manager_ && shelf_ && shelf_->shelf_layout_manager())
486 docked_layout_manager_->RemoveObserver(shelf_->shelf_layout_manager());
488 // Deactivate keyboard container before closing child windows and shutting
489 // down associated layout managers.
490 DeactivateKeyboard(keyboard::KeyboardController::GetInstance());
492 // panel_layout_manager_ needs to be shut down before windows are destroyed.
493 if (panel_layout_manager_) {
494 panel_layout_manager_->Shutdown();
495 panel_layout_manager_ = NULL;
497 // docked_layout_manager_ needs to be shut down before windows are destroyed.
498 if (docked_layout_manager_) {
499 docked_layout_manager_->Shutdown();
500 docked_layout_manager_ = NULL;
502 aura::Window* root_window = GetRootWindow();
503 aura::client::SetDragDropClient(root_window, NULL);
505 // TODO(harrym): Remove when Status Area Widget is a child view.
506 if (shelf_) {
507 shelf_->ShutdownStatusAreaWidget();
509 if (shelf_->shelf_layout_manager())
510 shelf_->shelf_layout_manager()->PrepareForShutdown();
513 // Close background widget first as it depends on tooltip.
514 wallpaper_controller_.reset();
515 animating_wallpaper_controller_.reset();
517 workspace_controller_.reset();
518 aura::client::SetTooltipClient(root_window, NULL);
520 // Explicitly destroy top level windows. We do this as during part of
521 // destruction such windows may query the RootWindow for state.
522 std::queue<aura::Window*> non_toplevel_windows;
523 non_toplevel_windows.push(root_window);
524 while (!non_toplevel_windows.empty()) {
525 aura::Window* non_toplevel_window = non_toplevel_windows.front();
526 non_toplevel_windows.pop();
527 aura::WindowTracker toplevel_windows;
528 for (size_t i = 0; i < non_toplevel_window->children().size(); ++i) {
529 aura::Window* child = non_toplevel_window->children()[i];
530 if (!child->owned_by_parent())
531 continue;
532 if (child->delegate())
533 toplevel_windows.Add(child);
534 else
535 non_toplevel_windows.push(child);
537 while (!toplevel_windows.windows().empty())
538 delete *toplevel_windows.windows().begin();
540 // And then remove the containers.
541 while (!root_window->children().empty()) {
542 aura::Window* window = root_window->children()[0];
543 if (window->owned_by_parent()) {
544 delete window;
545 } else {
546 root_window->RemoveChild(window);
550 shelf_.reset();
553 void RootWindowController::MoveWindowsTo(aura::Window* dst) {
554 // Forget the shelf early so that shelf don't update itself using wrong
555 // display info.
556 workspace_controller_->SetShelf(NULL);
557 ReparentAllWindows(GetRootWindow(), dst);
560 ShelfLayoutManager* RootWindowController::GetShelfLayoutManager() {
561 return shelf_->shelf_layout_manager();
564 SystemTray* RootWindowController::GetSystemTray() {
565 // We assume in throughout the code that this will not return NULL. If code
566 // triggers this for valid reasons, it should test status_area_widget first.
567 CHECK(shelf_->status_area_widget());
568 return shelf_->status_area_widget()->system_tray();
571 void RootWindowController::ShowContextMenu(const gfx::Point& location_in_screen,
572 ui::MenuSourceType source_type) {
573 DCHECK(Shell::GetInstance()->delegate());
574 scoped_ptr<ui::MenuModel> menu_model(
575 Shell::GetInstance()->delegate()->CreateContextMenu(
576 GetRootWindow(), NULL, NULL));
577 if (!menu_model)
578 return;
580 // Background controller may not be set yet if user clicked on status are
581 // before initial animation completion. See crbug.com/222218
582 if (!wallpaper_controller_.get())
583 return;
585 views::MenuRunner menu_runner(menu_model.get(),
586 views::MenuRunner::CONTEXT_MENU);
587 if (menu_runner.RunMenuAt(wallpaper_controller_->widget(),
588 NULL,
589 gfx::Rect(location_in_screen, gfx::Size()),
590 views::MENU_ANCHOR_TOPLEFT,
591 source_type) == views::MenuRunner::MENU_DELETED) {
592 return;
595 Shell::GetInstance()->UpdateShelfVisibility();
598 void RootWindowController::UpdateShelfVisibility() {
599 shelf_->shelf_layout_manager()->UpdateVisibilityState();
602 const aura::Window* RootWindowController::GetWindowForFullscreenMode() const {
603 const aura::Window* topmost_window = NULL;
604 const aura::Window* active_window = wm::GetActiveWindow();
605 if (active_window && active_window->GetRootWindow() == GetRootWindow() &&
606 IsSwitchableContainer(active_window->parent())) {
607 // Use the active window when it is on the current root window to determine
608 // the fullscreen state to allow temporarily using a panel or docked window
609 // (which are always above the default container) while a fullscreen
610 // window is open. We only use the active window when in a switchable
611 // container as the launcher should not exit fullscreen mode.
612 topmost_window = active_window;
613 } else {
614 // Otherwise, use the topmost window on the root window's default container
615 // when there is no active window on this root window.
616 const aura::Window::Windows& windows =
617 GetContainer(kShellWindowId_DefaultContainer)->children();
618 for (aura::Window::Windows::const_reverse_iterator iter = windows.rbegin();
619 iter != windows.rend(); ++iter) {
620 if (((*iter)->type() == ui::wm::WINDOW_TYPE_NORMAL ||
621 (*iter)->type() == ui::wm::WINDOW_TYPE_PANEL) &&
622 (*iter)->layer()->GetTargetVisibility()) {
623 topmost_window = *iter;
624 break;
628 while (topmost_window) {
629 if (wm::GetWindowState(topmost_window)->IsFullscreen())
630 return topmost_window;
631 topmost_window = ::wm::GetTransientParent(topmost_window);
633 return NULL;
636 void RootWindowController::ActivateKeyboard(
637 keyboard::KeyboardController* keyboard_controller) {
638 if (!keyboard::IsKeyboardEnabled() ||
639 GetContainer(kShellWindowId_VirtualKeyboardContainer)) {
640 return;
642 DCHECK(keyboard_controller);
643 if (!keyboard::IsKeyboardUsabilityExperimentEnabled()) {
644 keyboard_controller->AddObserver(shelf()->shelf_layout_manager());
645 keyboard_controller->AddObserver(panel_layout_manager_);
646 keyboard_controller->AddObserver(docked_layout_manager_);
647 keyboard_controller->AddObserver(workspace_controller_->layout_manager());
648 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(true);
650 aura::Window* parent = GetContainer(
651 kShellWindowId_VirtualKeyboardParentContainer);
652 DCHECK(parent);
653 aura::Window* keyboard_container =
654 keyboard_controller->GetContainerWindow();
655 keyboard_container->set_id(kShellWindowId_VirtualKeyboardContainer);
656 parent->AddChild(keyboard_container);
657 // TODO(oshima): Bounds of keyboard container should be handled by
658 // RootWindowLayoutManager. Remove this after fixed RootWindowLayoutManager.
659 keyboard_container->SetBounds(parent->bounds());
662 void RootWindowController::DeactivateKeyboard(
663 keyboard::KeyboardController* keyboard_controller) {
664 if (!keyboard_controller ||
665 !keyboard_controller->keyboard_container_initialized()) {
666 return;
668 aura::Window* keyboard_container =
669 keyboard_controller->GetContainerWindow();
670 if (keyboard_container->GetRootWindow() == GetRootWindow()) {
671 aura::Window* parent = GetContainer(
672 kShellWindowId_VirtualKeyboardParentContainer);
673 DCHECK(parent);
674 parent->RemoveChild(keyboard_container);
675 if (!keyboard::IsKeyboardUsabilityExperimentEnabled()) {
676 // Virtual keyboard may be deactivated while still showing, notify all
677 // observers that keyboard bounds changed to 0 before remove them.
678 keyboard_controller->NotifyKeyboardBoundsChanging(gfx::Rect());
679 keyboard_controller->RemoveObserver(shelf()->shelf_layout_manager());
680 keyboard_controller->RemoveObserver(panel_layout_manager_);
681 keyboard_controller->RemoveObserver(docked_layout_manager_);
682 keyboard_controller->RemoveObserver(
683 workspace_controller_->layout_manager());
684 Shell::GetInstance()->delegate()->VirtualKeyboardActivated(false);
689 bool RootWindowController::IsVirtualKeyboardWindow(aura::Window* window) {
690 aura::Window* parent = GetContainer(
691 kShellWindowId_VirtualKeyboardParentContainer);
692 return parent ? parent->Contains(window) : false;
695 ////////////////////////////////////////////////////////////////////////////////
696 // RootWindowController, private:
698 RootWindowController::RootWindowController(AshWindowTreeHost* ash_host)
699 : ash_host_(ash_host),
700 root_window_layout_(NULL),
701 docked_layout_manager_(NULL),
702 panel_layout_manager_(NULL),
703 touch_hud_debug_(NULL),
704 touch_hud_projection_(NULL) {
705 aura::Window* root_window = GetRootWindow();
706 GetRootWindowSettings(root_window)->controller = this;
707 screen_dimmer_.reset(new ScreenDimmer(root_window));
709 stacking_controller_.reset(new StackingController);
710 aura::client::SetWindowTreeClient(root_window, stacking_controller_.get());
711 capture_client_.reset(new ::wm::ScopedCaptureClient(root_window));
714 void RootWindowController::Init(RootWindowType root_window_type,
715 bool first_run_after_boot) {
716 aura::Window* root_window = GetRootWindow();
717 Shell* shell = Shell::GetInstance();
718 shell->InitRootWindow(root_window);
720 ash_host_->AsWindowTreeHost()->SetCursor(ui::kCursorPointer);
721 CreateContainersInRootWindow(root_window);
723 if (root_window_type == VIRTUAL_KEYBOARD) {
724 aura::Window* virtual_keyboard_parent_container = GetContainer(
725 kShellWindowId_VirtualKeyboardParentContainer);
726 virtual_keyboard_parent_container->SetBounds(root_window->bounds());
727 shell->InitKeyboard();
728 return;
731 CreateSystemBackground(first_run_after_boot);
733 InitLayoutManagers();
734 InitTouchHuds();
736 if (Shell::GetPrimaryRootWindowController()->
737 GetSystemModalLayoutManager(NULL)->has_modal_background()) {
738 GetSystemModalLayoutManager(NULL)->CreateModalBackground();
741 shell->AddShellObserver(this);
743 if (root_window_type == PRIMARY) {
744 root_window_layout()->OnWindowResized();
745 if (!keyboard::IsKeyboardUsabilityExperimentEnabled())
746 shell->InitKeyboard();
747 } else {
748 root_window_layout()->OnWindowResized();
749 ash_host_->AsWindowTreeHost()->Show();
751 // Create a shelf if a user is already logged in.
752 if (shell->session_state_delegate()->NumberOfLoggedInUsers())
753 shelf()->CreateShelf();
755 // Notify shell observers about new root window.
756 shell->OnRootWindowAdded(root_window);
759 #if defined(OS_CHROMEOS)
760 if (!CommandLine::ForCurrentProcess()->HasSwitch(
761 switches::kAshDisableTouchExplorationMode)) {
762 touch_exploration_manager_.reset(new AshTouchExplorationManager(this));
764 #endif
767 void RootWindowController::InitLayoutManagers() {
768 aura::Window* root_window = GetRootWindow();
769 root_window_layout_ = new RootWindowLayoutManager(root_window);
770 root_window->SetLayoutManager(root_window_layout_);
772 aura::Window* default_container =
773 GetContainer(kShellWindowId_DefaultContainer);
774 // Workspace manager has its own layout managers.
775 workspace_controller_.reset(
776 new WorkspaceController(default_container));
778 aura::Window* always_on_top_container =
779 GetContainer(kShellWindowId_AlwaysOnTopContainer);
780 always_on_top_container->SetLayoutManager(
781 new WorkspaceLayoutManager(always_on_top_container));
782 always_on_top_controller_.reset(new AlwaysOnTopController);
783 always_on_top_controller_->SetAlwaysOnTopContainer(always_on_top_container);
785 DCHECK(!shelf_.get());
786 aura::Window* shelf_container = GetContainer(kShellWindowId_ShelfContainer);
787 // TODO(harrym): Remove when status area is view.
788 aura::Window* status_container = GetContainer(kShellWindowId_StatusContainer);
789 shelf_.reset(new ShelfWidget(
790 shelf_container, status_container, workspace_controller()));
792 if (!Shell::GetInstance()->session_state_delegate()->
793 IsActiveUserSessionStarted()) {
794 // This window exists only to be a event target on login screen.
795 // It does not have to handle events, nor be visible.
796 mouse_event_target_.reset(new aura::Window(new EmptyWindowDelegate));
797 mouse_event_target_->Init(aura::WINDOW_LAYER_NOT_DRAWN);
799 aura::Window* lock_background_container =
800 GetContainer(kShellWindowId_LockScreenBackgroundContainer);
801 lock_background_container->AddChild(mouse_event_target_.get());
802 mouse_event_target_->Show();
805 // Create Docked windows layout manager
806 aura::Window* docked_container = GetContainer(kShellWindowId_DockedContainer);
807 docked_layout_manager_ =
808 new DockedWindowLayoutManager(docked_container, workspace_controller());
809 docked_container->SetLayoutManager(docked_layout_manager_);
811 // Installs SnapLayoutManager to containers who set the
812 // |kSnapsChildrenToPhysicalPixelBoundary| property.
813 wm::InstallSnapLayoutManagerToContainers(root_window);
815 // Create Panel layout manager
816 aura::Window* panel_container = GetContainer(kShellWindowId_PanelContainer);
817 panel_layout_manager_ = new PanelLayoutManager(panel_container);
818 panel_container->SetLayoutManager(panel_layout_manager_);
819 panel_container_handler_.reset(new PanelWindowEventHandler);
820 panel_container->AddPreTargetHandler(panel_container_handler_.get());
822 // Install an AttachedPanelWindowTargeter on the panel container to make it
823 // easier to correctly target shelf buttons with touch.
824 gfx::Insets mouse_extend(-kResizeOutsideBoundsSize,
825 -kResizeOutsideBoundsSize,
826 -kResizeOutsideBoundsSize,
827 -kResizeOutsideBoundsSize);
828 gfx::Insets touch_extend = mouse_extend.Scale(
829 kResizeOutsideBoundsScaleForTouch);
830 panel_container->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
831 new AttachedPanelWindowTargeter(panel_container,
832 mouse_extend,
833 touch_extend,
834 panel_layout_manager_)));
837 void RootWindowController::InitTouchHuds() {
838 CommandLine* command_line = CommandLine::ForCurrentProcess();
839 if (command_line->HasSwitch(switches::kAshTouchHud))
840 set_touch_hud_debug(new TouchHudDebug(GetRootWindow()));
841 if (Shell::GetInstance()->is_touch_hud_projection_enabled())
842 EnableTouchHudProjection();
845 void RootWindowController::CreateSystemBackground(
846 bool is_first_run_after_boot) {
847 SkColor color = SK_ColorBLACK;
848 #if defined(OS_CHROMEOS)
849 if (is_first_run_after_boot)
850 color = kChromeOsBootColor;
851 #endif
852 system_background_.reset(
853 new SystemBackgroundController(GetRootWindow(), color));
855 #if defined(OS_CHROMEOS)
856 // Make a copy of the system's boot splash screen so we can composite it
857 // onscreen until the desktop background is ready.
858 if (is_first_run_after_boot &&
859 (CommandLine::ForCurrentProcess()->HasSwitch(
860 switches::kAshCopyHostBackgroundAtBoot) ||
861 CommandLine::ForCurrentProcess()->HasSwitch(
862 switches::kAshAnimateFromBootSplashScreen)))
863 boot_splash_screen_.reset(new BootSplashScreen(GetHost()));
864 #endif
867 void RootWindowController::CreateContainersInRootWindow(
868 aura::Window* root_window) {
869 // These containers are just used by PowerButtonController to animate groups
870 // of containers simultaneously without messing up the current transformations
871 // on those containers. These are direct children of the root window; all of
872 // the other containers are their children.
874 // The desktop background container is not part of the lock animation, so it
875 // is not included in those animate groups.
876 // When screen is locked desktop background is moved to lock screen background
877 // container (moved back on unlock). We want to make sure that there's an
878 // opaque layer occluding the non-lock-screen layers.
879 aura::Window* desktop_background_container = CreateContainer(
880 kShellWindowId_DesktopBackgroundContainer,
881 "DesktopBackgroundContainer",
882 root_window);
883 ::wm::SetChildWindowVisibilityChangesAnimated(desktop_background_container);
885 aura::Window* non_lock_screen_containers = CreateContainer(
886 kShellWindowId_NonLockScreenContainersContainer,
887 "NonLockScreenContainersContainer",
888 root_window);
890 aura::Window* lock_background_containers = CreateContainer(
891 kShellWindowId_LockScreenBackgroundContainer,
892 "LockScreenBackgroundContainer",
893 root_window);
894 ::wm::SetChildWindowVisibilityChangesAnimated(lock_background_containers);
896 aura::Window* lock_screen_containers = CreateContainer(
897 kShellWindowId_LockScreenContainersContainer,
898 "LockScreenContainersContainer",
899 root_window);
900 aura::Window* lock_screen_related_containers = CreateContainer(
901 kShellWindowId_LockScreenRelatedContainersContainer,
902 "LockScreenRelatedContainersContainer",
903 root_window);
905 CreateContainer(kShellWindowId_UnparentedControlContainer,
906 "UnparentedControlContainer",
907 non_lock_screen_containers);
909 aura::Window* default_container = CreateContainer(
910 kShellWindowId_DefaultContainer,
911 "DefaultContainer",
912 non_lock_screen_containers);
913 ::wm::SetChildWindowVisibilityChangesAnimated(default_container);
914 wm::SetSnapsChildrenToPhysicalPixelBoundary(default_container);
915 SetUsesScreenCoordinates(default_container);
916 SetUsesEasyResizeTargeter(default_container);
918 aura::Window* always_on_top_container = CreateContainer(
919 kShellWindowId_AlwaysOnTopContainer,
920 "AlwaysOnTopContainer",
921 non_lock_screen_containers);
922 ::wm::SetChildWindowVisibilityChangesAnimated(always_on_top_container);
923 wm::SetSnapsChildrenToPhysicalPixelBoundary(always_on_top_container);
924 SetUsesScreenCoordinates(always_on_top_container);
926 aura::Window* docked_container = CreateContainer(
927 kShellWindowId_DockedContainer,
928 "DockedContainer",
929 non_lock_screen_containers);
930 ::wm::SetChildWindowVisibilityChangesAnimated(docked_container);
931 wm::SetSnapsChildrenToPhysicalPixelBoundary(docked_container);
932 SetUsesScreenCoordinates(docked_container);
933 SetUsesEasyResizeTargeter(docked_container);
935 aura::Window* shelf_container =
936 CreateContainer(kShellWindowId_ShelfContainer,
937 "ShelfContainer",
938 non_lock_screen_containers);
939 wm::SetSnapsChildrenToPhysicalPixelBoundary(shelf_container);
940 SetUsesScreenCoordinates(shelf_container);
941 DescendantShouldStayInSameRootWindow(shelf_container);
943 aura::Window* panel_container = CreateContainer(
944 kShellWindowId_PanelContainer,
945 "PanelContainer",
946 non_lock_screen_containers);
947 wm::SetSnapsChildrenToPhysicalPixelBoundary(panel_container);
948 SetUsesScreenCoordinates(panel_container);
950 aura::Window* shelf_bubble_container =
951 CreateContainer(kShellWindowId_ShelfBubbleContainer,
952 "ShelfBubbleContainer",
953 non_lock_screen_containers);
954 wm::SetSnapsChildrenToPhysicalPixelBoundary(shelf_bubble_container);
955 SetUsesScreenCoordinates(shelf_bubble_container);
956 DescendantShouldStayInSameRootWindow(shelf_bubble_container);
958 aura::Window* app_list_container =
959 CreateContainer(kShellWindowId_AppListContainer,
960 "AppListContainer",
961 non_lock_screen_containers);
962 wm::SetSnapsChildrenToPhysicalPixelBoundary(app_list_container);
963 SetUsesScreenCoordinates(app_list_container);
965 aura::Window* modal_container = CreateContainer(
966 kShellWindowId_SystemModalContainer,
967 "SystemModalContainer",
968 non_lock_screen_containers);
969 wm::SetSnapsChildrenToPhysicalPixelBoundary(modal_container);
970 modal_container->SetLayoutManager(
971 new SystemModalContainerLayoutManager(modal_container));
972 ::wm::SetChildWindowVisibilityChangesAnimated(modal_container);
973 SetUsesScreenCoordinates(modal_container);
974 SetUsesEasyResizeTargeter(modal_container);
976 // TODO(beng): Figure out if we can make this use
977 // SystemModalContainerEventFilter instead of stops_event_propagation.
978 aura::Window* lock_container = CreateContainer(
979 kShellWindowId_LockScreenContainer,
980 "LockScreenContainer",
981 lock_screen_containers);
982 wm::SetSnapsChildrenToPhysicalPixelBoundary(lock_container);
983 if (CommandLine::ForCurrentProcess()->HasSwitch(
984 switches::kAshDisableLockLayoutManager)) {
985 lock_container->SetLayoutManager(
986 new WorkspaceLayoutManager(lock_container));
987 } else {
988 lock_container->SetLayoutManager(new LockLayoutManager(lock_container));
990 SetUsesScreenCoordinates(lock_container);
991 // TODO(beng): stopsevents
993 aura::Window* lock_modal_container = CreateContainer(
994 kShellWindowId_LockSystemModalContainer,
995 "LockSystemModalContainer",
996 lock_screen_containers);
997 wm::SetSnapsChildrenToPhysicalPixelBoundary(lock_modal_container);
998 lock_modal_container->SetLayoutManager(
999 new SystemModalContainerLayoutManager(lock_modal_container));
1000 ::wm::SetChildWindowVisibilityChangesAnimated(lock_modal_container);
1001 SetUsesScreenCoordinates(lock_modal_container);
1002 SetUsesEasyResizeTargeter(lock_modal_container);
1004 aura::Window* status_container =
1005 CreateContainer(kShellWindowId_StatusContainer,
1006 "StatusContainer",
1007 lock_screen_related_containers);
1008 wm::SetSnapsChildrenToPhysicalPixelBoundary(status_container);
1009 SetUsesScreenCoordinates(status_container);
1010 DescendantShouldStayInSameRootWindow(status_container);
1012 aura::Window* settings_bubble_container = CreateContainer(
1013 kShellWindowId_SettingBubbleContainer,
1014 "SettingBubbleContainer",
1015 lock_screen_related_containers);
1016 ::wm::SetChildWindowVisibilityChangesAnimated(settings_bubble_container);
1017 wm::SetSnapsChildrenToPhysicalPixelBoundary(settings_bubble_container);
1018 SetUsesScreenCoordinates(settings_bubble_container);
1019 DescendantShouldStayInSameRootWindow(settings_bubble_container);
1021 aura::Window* menu_container = CreateContainer(
1022 kShellWindowId_MenuContainer,
1023 "MenuContainer",
1024 lock_screen_related_containers);
1025 ::wm::SetChildWindowVisibilityChangesAnimated(menu_container);
1026 wm::SetSnapsChildrenToPhysicalPixelBoundary(menu_container);
1027 SetUsesScreenCoordinates(menu_container);
1029 aura::Window* drag_drop_container = CreateContainer(
1030 kShellWindowId_DragImageAndTooltipContainer,
1031 "DragImageAndTooltipContainer",
1032 lock_screen_related_containers);
1033 ::wm::SetChildWindowVisibilityChangesAnimated(drag_drop_container);
1034 wm::SetSnapsChildrenToPhysicalPixelBoundary(drag_drop_container);
1035 SetUsesScreenCoordinates(drag_drop_container);
1037 aura::Window* overlay_container = CreateContainer(
1038 kShellWindowId_OverlayContainer,
1039 "OverlayContainer",
1040 lock_screen_related_containers);
1041 wm::SetSnapsChildrenToPhysicalPixelBoundary(overlay_container);
1042 SetUsesScreenCoordinates(overlay_container);
1044 aura::Window* virtual_keyboard_parent_container = CreateContainer(
1045 kShellWindowId_VirtualKeyboardParentContainer,
1046 "VirtualKeyboardParentContainer",
1047 root_window);
1048 wm::SetSnapsChildrenToPhysicalPixelBoundary(
1049 virtual_keyboard_parent_container);
1050 SetUsesScreenCoordinates(virtual_keyboard_parent_container);
1052 #if defined(OS_CHROMEOS)
1053 aura::Window* mouse_cursor_container = CreateContainer(
1054 kShellWindowId_MouseCursorContainer,
1055 "MouseCursorContainer",
1056 root_window);
1057 SetUsesScreenCoordinates(mouse_cursor_container);
1058 #endif
1060 CreateContainer(kShellWindowId_PowerButtonAnimationContainer,
1061 "PowerButtonAnimationContainer", root_window);
1064 void RootWindowController::EnableTouchHudProjection() {
1065 if (touch_hud_projection_)
1066 return;
1067 set_touch_hud_projection(new TouchHudProjection(GetRootWindow()));
1070 void RootWindowController::DisableTouchHudProjection() {
1071 if (!touch_hud_projection_)
1072 return;
1073 touch_hud_projection_->Remove();
1076 void RootWindowController::OnLoginStateChanged(user::LoginStatus status) {
1077 shelf_->shelf_layout_manager()->UpdateVisibilityState();
1080 void RootWindowController::OnTouchHudProjectionToggled(bool enabled) {
1081 if (enabled)
1082 EnableTouchHudProjection();
1083 else
1084 DisableTouchHudProjection();
1087 RootWindowController* GetRootWindowController(
1088 const aura::Window* root_window) {
1089 return root_window ? GetRootWindowSettings(root_window)->controller : NULL;
1092 } // namespace ash