Add UMA stats for ExtensionCache
[chromium-blink-merge.git] / ash / root_window_controller.cc
blobe5030ad297e26b8cd7fcd347a2cef72dc3065a8c
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/root_window_settings.h"
19 #include "ash/session_state_delegate.h"
20 #include "ash/shelf/shelf_layout_manager.h"
21 #include "ash/shelf/shelf_types.h"
22 #include "ash/shelf/shelf_widget.h"
23 #include "ash/shell.h"
24 #include "ash/shell_delegate.h"
25 #include "ash/shell_factory.h"
26 #include "ash/shell_window_ids.h"
27 #include "ash/system/status_area_widget.h"
28 #include "ash/system/tray/system_tray_delegate.h"
29 #include "ash/touch/touch_hud_debug.h"
30 #include "ash/touch/touch_hud_projection.h"
31 #include "ash/touch/touch_observer_hud.h"
32 #include "ash/wm/always_on_top_controller.h"
33 #include "ash/wm/base_layout_manager.h"
34 #include "ash/wm/dock/docked_window_layout_manager.h"
35 #include "ash/wm/panels/panel_layout_manager.h"
36 #include "ash/wm/panels/panel_window_event_handler.h"
37 #include "ash/wm/root_window_layout_manager.h"
38 #include "ash/wm/screen_dimmer.h"
39 #include "ash/wm/stacking_controller.h"
40 #include "ash/wm/status_area_layout_manager.h"
41 #include "ash/wm/system_background_controller.h"
42 #include "ash/wm/system_modal_container_layout_manager.h"
43 #include "ash/wm/window_properties.h"
44 #include "ash/wm/window_state.h"
45 #include "ash/wm/window_util.h"
46 #include "ash/wm/workspace/workspace_layout_manager.h"
47 #include "ash/wm/workspace_controller.h"
48 #include "base/command_line.h"
49 #include "base/time/time.h"
50 #include "ui/aura/client/aura_constants.h"
51 #include "ui/aura/client/drag_drop_client.h"
52 #include "ui/aura/client/tooltip_client.h"
53 #include "ui/aura/root_window.h"
54 #include "ui/aura/window.h"
55 #include "ui/aura/window_delegate.h"
56 #include "ui/aura/window_observer.h"
57 #include "ui/aura/window_tracker.h"
58 #include "ui/base/hit_test.h"
59 #include "ui/base/models/menu_model.h"
60 #include "ui/gfx/display.h"
61 #include "ui/gfx/screen.h"
62 #include "ui/keyboard/keyboard_controller.h"
63 #include "ui/keyboard/keyboard_util.h"
64 #include "ui/views/controls/menu/menu_runner.h"
65 #include "ui/views/corewm/capture_controller.h"
66 #include "ui/views/corewm/visibility_controller.h"
67 #include "ui/views/corewm/window_util.h"
68 #include "ui/views/view_model.h"
69 #include "ui/views/view_model_utils.h"
70 #include "ui/wm/public/easy_resize_window_targeter.h"
71 #include "ui/wm/public/window_types.h"
73 #if defined(OS_CHROMEOS)
74 #include "ash/wm/boot_splash_screen_chromeos.h"
75 #endif
77 namespace ash {
78 namespace {
80 #if defined(OS_CHROMEOS)
81 // Duration for the animation that hides the boot splash screen, in
82 // milliseconds. This should be short enough in relation to
83 // wm/window_animation.cc's brightness/grayscale fade animation that the login
84 // background image animation isn't hidden by the splash screen animation.
85 const int kBootSplashScreenHideDurationMs = 500;
86 #endif
88 // Creates a new window for use as a container.
89 aura::Window* CreateContainer(int window_id,
90 const char* name,
91 aura::Window* parent) {
92 aura::Window* container = new aura::Window(NULL);
93 container->set_id(window_id);
94 container->SetName(name);
95 container->Init(aura::WINDOW_LAYER_NOT_DRAWN);
96 parent->AddChild(container);
97 if (window_id != internal::kShellWindowId_UnparentedControlContainer)
98 container->Show();
99 return container;
102 float ToRelativeValue(int value, int src, int dst) {
103 return static_cast<float>(value) / static_cast<float>(src) * dst;
106 void MoveOriginRelativeToSize(const gfx::Size& src_size,
107 const gfx::Size& dst_size,
108 gfx::Rect* bounds_in_out) {
109 gfx::Point origin = bounds_in_out->origin();
110 bounds_in_out->set_origin(gfx::Point(
111 ToRelativeValue(origin.x(), src_size.width(), dst_size.width()),
112 ToRelativeValue(origin.y(), src_size.height(), dst_size.height())));
115 // Reparents |window| to |new_parent|.
116 void ReparentWindow(aura::Window* window, aura::Window* new_parent) {
117 const gfx::Size src_size = window->parent()->bounds().size();
118 const gfx::Size dst_size = new_parent->bounds().size();
119 // Update the restore bounds to make it relative to the display.
120 wm::WindowState* state = wm::GetWindowState(window);
121 gfx::Rect restore_bounds;
122 bool has_restore_bounds = state->HasRestoreBounds();
124 bool update_bounds = (state->IsNormalShowState() || state->IsMinimized()) &&
125 new_parent->id() != internal::kShellWindowId_DockedContainer;
126 gfx::Rect local_bounds;
127 if (update_bounds) {
128 local_bounds = state->window()->bounds();
129 MoveOriginRelativeToSize(src_size, dst_size, &local_bounds);
132 if (has_restore_bounds) {
133 restore_bounds = state->GetRestoreBoundsInParent();
134 MoveOriginRelativeToSize(src_size, dst_size, &restore_bounds);
137 new_parent->AddChild(window);
139 // Docked windows have bounds handled by the layout manager in AddChild().
140 if (update_bounds)
141 window->SetBounds(local_bounds);
143 if (has_restore_bounds)
144 state->SetRestoreBoundsInParent(restore_bounds);
147 // Reparents the appropriate set of windows from |src| to |dst|.
148 void ReparentAllWindows(aura::Window* src, aura::Window* dst) {
149 // Set of windows to move.
150 const int kContainerIdsToMove[] = {
151 internal::kShellWindowId_DefaultContainer,
152 internal::kShellWindowId_DockedContainer,
153 internal::kShellWindowId_PanelContainer,
154 internal::kShellWindowId_AlwaysOnTopContainer,
155 internal::kShellWindowId_SystemModalContainer,
156 internal::kShellWindowId_LockSystemModalContainer,
157 internal::kShellWindowId_InputMethodContainer,
158 internal::kShellWindowId_UnparentedControlContainer,
160 for (size_t i = 0; i < arraysize(kContainerIdsToMove); i++) {
161 int id = kContainerIdsToMove[i];
162 aura::Window* src_container = Shell::GetContainer(src, id);
163 aura::Window* dst_container = Shell::GetContainer(dst, id);
164 while (!src_container->children().empty()) {
165 // Restart iteration from the source container windows each time as they
166 // may change as a result of moving other windows.
167 aura::Window::Windows::const_iterator iter =
168 src_container->children().begin();
169 while (iter != src_container->children().end() &&
170 internal::SystemModalContainerLayoutManager::IsModalBackground(
171 *iter)) {
172 ++iter;
174 // If the entire window list is modal background windows then stop.
175 if (iter == src_container->children().end())
176 break;
177 ReparentWindow(*iter, dst_container);
182 // Mark the container window so that a widget added to this container will
183 // use the virtual screeen coordinates instead of parent.
184 void SetUsesScreenCoordinates(aura::Window* container) {
185 container->SetProperty(internal::kUsesScreenCoordinatesKey, true);
188 // Mark the container window so that a widget added to this container will
189 // say in the same root window regardless of the bounds specified.
190 void DescendantShouldStayInSameRootWindow(aura::Window* container) {
191 container->SetProperty(internal::kStayInSameRootWindowKey, true);
194 void SetUsesEasyResizeTargeter(aura::Window* container) {
195 gfx::Insets mouse_extend(-kResizeOutsideBoundsSize,
196 -kResizeOutsideBoundsSize,
197 -kResizeOutsideBoundsSize,
198 -kResizeOutsideBoundsSize);
199 gfx::Insets touch_extend = mouse_extend.Scale(
200 kResizeOutsideBoundsScaleForTouch);
201 container->set_event_targeter(scoped_ptr<ui::EventTargeter>(
202 new ::wm::EasyResizeWindowTargeter(container, mouse_extend,
203 touch_extend)));
206 // A window delegate which does nothing. Used to create a window that
207 // is a event target, but do nothing.
208 class EmptyWindowDelegate : public aura::WindowDelegate {
209 public:
210 EmptyWindowDelegate() {}
211 virtual ~EmptyWindowDelegate() {}
213 // aura::WindowDelegate overrides:
214 virtual gfx::Size GetMinimumSize() const OVERRIDE {
215 return gfx::Size();
217 virtual gfx::Size GetMaximumSize() const OVERRIDE {
218 return gfx::Size();
220 virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
221 const gfx::Rect& new_bounds) OVERRIDE {
223 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
224 return gfx::kNullCursor;
226 virtual int GetNonClientComponent(
227 const gfx::Point& point) const OVERRIDE {
228 return HTNOWHERE;
230 virtual bool ShouldDescendIntoChildForEventHandling(
231 aura::Window* child,
232 const gfx::Point& location) OVERRIDE {
233 return false;
235 virtual bool CanFocus() OVERRIDE {
236 return false;
238 virtual void OnCaptureLost() OVERRIDE {
240 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
242 virtual void OnDeviceScaleFactorChanged(
243 float device_scale_factor) OVERRIDE {
245 virtual void OnWindowDestroying() OVERRIDE {}
246 virtual void OnWindowDestroyed() OVERRIDE {
247 delete this;
249 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {
251 virtual bool HasHitTestMask() const OVERRIDE {
252 return false;
254 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {}
255 virtual void DidRecreateLayer(ui::Layer* old_layer,
256 ui::Layer* new_layer) OVERRIDE {}
258 private:
259 DISALLOW_COPY_AND_ASSIGN(EmptyWindowDelegate);
262 } // namespace
264 namespace internal {
266 void RootWindowController::CreateForPrimaryDisplay(
267 aura::RootWindow* root) {
268 RootWindowController* controller = new RootWindowController(root);
269 controller->Init(RootWindowController::PRIMARY,
270 Shell::GetInstance()->delegate()->IsFirstRunAfterBoot());
273 void RootWindowController::CreateForSecondaryDisplay(aura::RootWindow * root) {
274 RootWindowController* controller = new RootWindowController(root);
275 controller->Init(RootWindowController::SECONDARY, false /* first run */);
278 void RootWindowController::CreateForVirtualKeyboardDisplay(
279 aura::RootWindow * root) {
280 RootWindowController* controller = new RootWindowController(root);
281 controller->Init(RootWindowController::VIRTUAL_KEYBOARD,
282 false /* first run */);
285 // static
286 RootWindowController* RootWindowController::ForShelf(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 internal::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 root_window_.reset();
313 // The CaptureClient needs to be around for as long as the RootWindow is
314 // valid.
315 capture_client_.reset();
318 void RootWindowController::SetWallpaperController(
319 DesktopBackgroundWidgetController* controller) {
320 wallpaper_controller_.reset(controller);
323 void RootWindowController::SetAnimatingWallpaperController(
324 AnimatingDesktopController* controller) {
325 if (animating_wallpaper_controller_.get())
326 animating_wallpaper_controller_->StopAnimating();
327 animating_wallpaper_controller_.reset(controller);
330 void RootWindowController::Shutdown() {
331 Shell::GetInstance()->RemoveShellObserver(this);
333 if (animating_wallpaper_controller_.get())
334 animating_wallpaper_controller_->StopAnimating();
335 wallpaper_controller_.reset();
336 animating_wallpaper_controller_.reset();
338 // Change the target root window before closing child windows. If any child
339 // being removed triggers a relayout of the shelf it will try to build a
340 // window list adding windows from the target root window's containers which
341 // may have already gone away.
342 if (Shell::GetTargetRootWindow() == root_window()) {
343 Shell::GetInstance()->set_target_root_window(
344 Shell::GetPrimaryRootWindow() == root_window() ?
345 NULL : Shell::GetPrimaryRootWindow());
348 CloseChildWindows();
349 GetRootWindowSettings(root_window())->controller = NULL;
350 screen_dimmer_.reset();
351 workspace_controller_.reset();
352 // Forget with the display ID so that display lookup
353 // ends up with invalid display.
354 internal::GetRootWindowSettings(root_window())->display_id =
355 gfx::Display::kInvalidDisplayID;
356 // And this root window should no longer process events.
357 root_window_->PrepareForShutdown();
359 system_background_.reset();
362 SystemModalContainerLayoutManager*
363 RootWindowController::GetSystemModalLayoutManager(aura::Window* window) {
364 aura::Window* modal_container = NULL;
365 if (window) {
366 aura::Window* window_container = GetContainerForWindow(window);
367 if (window_container &&
368 window_container->id() >= kShellWindowId_LockScreenContainer) {
369 modal_container = GetContainer(kShellWindowId_LockSystemModalContainer);
370 } else {
371 modal_container = GetContainer(kShellWindowId_SystemModalContainer);
373 } else {
374 int modal_window_id = Shell::GetInstance()->session_state_delegate()
375 ->IsUserSessionBlocked() ? kShellWindowId_LockSystemModalContainer :
376 kShellWindowId_SystemModalContainer;
377 modal_container = GetContainer(modal_window_id);
379 return modal_container ? static_cast<SystemModalContainerLayoutManager*>(
380 modal_container->layout_manager()) : NULL;
383 aura::Window* RootWindowController::GetContainer(int container_id) {
384 return root_window()->GetChildById(container_id);
387 const aura::Window* RootWindowController::GetContainer(int container_id) const {
388 return root_window_->window()->GetChildById(container_id);
391 void RootWindowController::ShowShelf() {
392 if (!shelf_->shelf())
393 return;
394 shelf_->shelf()->SetVisible(true);
395 shelf_->status_area_widget()->Show();
398 void RootWindowController::OnShelfCreated() {
399 if (panel_layout_manager_)
400 panel_layout_manager_->SetShelf(shelf_->shelf());
401 if (docked_layout_manager_) {
402 docked_layout_manager_->SetShelf(shelf_->shelf());
403 if (shelf_->shelf_layout_manager())
404 docked_layout_manager_->AddObserver(shelf_->shelf_layout_manager());
408 void RootWindowController::UpdateAfterLoginStatusChange(
409 user::LoginStatus status) {
410 if (status != user::LOGGED_IN_NONE)
411 mouse_event_target_.reset();
412 if (shelf_->status_area_widget())
413 shelf_->status_area_widget()->UpdateAfterLoginStatusChange(status);
416 void RootWindowController::HandleInitialDesktopBackgroundAnimationStarted() {
417 #if defined(OS_CHROMEOS)
418 if (CommandLine::ForCurrentProcess()->HasSwitch(
419 switches::kAshAnimateFromBootSplashScreen) &&
420 boot_splash_screen_.get()) {
421 // Make the splash screen fade out so it doesn't obscure the desktop
422 // wallpaper's brightness/grayscale animation.
423 boot_splash_screen_->StartHideAnimation(
424 base::TimeDelta::FromMilliseconds(kBootSplashScreenHideDurationMs));
426 #endif
429 void RootWindowController::OnWallpaperAnimationFinished(views::Widget* widget) {
430 // Make sure the wallpaper is visible.
431 system_background_->SetColor(SK_ColorBLACK);
432 #if defined(OS_CHROMEOS)
433 boot_splash_screen_.reset();
434 #endif
436 Shell::GetInstance()->user_wallpaper_delegate()->
437 OnWallpaperAnimationFinished();
438 // Only removes old component when wallpaper animation finished. If we
439 // remove the old one before the new wallpaper is done fading in there will
440 // be a white flash during the animation.
441 if (animating_wallpaper_controller()) {
442 DesktopBackgroundWidgetController* controller =
443 animating_wallpaper_controller()->GetController(true);
444 // |desktop_widget_| should be the same animating widget we try to move
445 // to |kDesktopController|. Otherwise, we may close |desktop_widget_|
446 // before move it to |kDesktopController|.
447 DCHECK_EQ(controller->widget(), widget);
448 // Release the old controller and close its background widget.
449 SetWallpaperController(controller);
453 void RootWindowController::CloseChildWindows() {
454 mouse_event_target_.reset();
456 // Deactivate keyboard container before closing child windows and shutting
457 // down associated layout managers.
458 DeactivateKeyboard(Shell::GetInstance()->keyboard_controller());
460 // panel_layout_manager_ needs to be shut down before windows are destroyed.
461 if (panel_layout_manager_) {
462 panel_layout_manager_->Shutdown();
463 panel_layout_manager_ = NULL;
465 // docked_layout_manager_ needs to be shut down before windows are destroyed.
466 if (docked_layout_manager_) {
467 if (shelf_ && shelf_->shelf_layout_manager())
468 docked_layout_manager_->RemoveObserver(shelf_->shelf_layout_manager());
469 docked_layout_manager_->Shutdown();
470 docked_layout_manager_ = NULL;
473 aura::client::SetDragDropClient(root_window(), NULL);
475 // TODO(harrym): Remove when Status Area Widget is a child view.
476 if (shelf_) {
477 shelf_->ShutdownStatusAreaWidget();
479 if (shelf_->shelf_layout_manager())
480 shelf_->shelf_layout_manager()->PrepareForShutdown();
483 // Close background widget first as it depends on tooltip.
484 wallpaper_controller_.reset();
485 animating_wallpaper_controller_.reset();
487 workspace_controller_.reset();
488 aura::client::SetTooltipClient(root_window(), NULL);
490 // Explicitly destroy top level windows. We do this as during part of
491 // destruction such windows may query the RootWindow for state.
492 std::queue<aura::Window*> non_toplevel_windows;
493 non_toplevel_windows.push(root_window());
494 while (!non_toplevel_windows.empty()) {
495 aura::Window* non_toplevel_window = non_toplevel_windows.front();
496 non_toplevel_windows.pop();
497 aura::WindowTracker toplevel_windows;
498 for (size_t i = 0; i < non_toplevel_window->children().size(); ++i) {
499 aura::Window* child = non_toplevel_window->children()[i];
500 if (!child->owned_by_parent())
501 continue;
502 if (child->delegate())
503 toplevel_windows.Add(child);
504 else
505 non_toplevel_windows.push(child);
507 while (!toplevel_windows.windows().empty())
508 delete *toplevel_windows.windows().begin();
510 // And then remove the containers.
511 while (!root_window()->children().empty()) {
512 aura::Window* window = root_window()->children()[0];
513 if (window->owned_by_parent()) {
514 delete window;
515 } else {
516 root_window()->RemoveChild(window);
520 shelf_.reset();
523 void RootWindowController::MoveWindowsTo(aura::Window* dst) {
524 // Forget the shelf early so that shelf don't update itself using wrong
525 // display info.
526 workspace_controller_->SetShelf(NULL);
527 ReparentAllWindows(root_window(), dst);
530 ShelfLayoutManager* RootWindowController::GetShelfLayoutManager() {
531 return shelf_->shelf_layout_manager();
534 SystemTray* RootWindowController::GetSystemTray() {
535 // We assume in throughout the code that this will not return NULL. If code
536 // triggers this for valid reasons, it should test status_area_widget first.
537 CHECK(shelf_->status_area_widget());
538 return shelf_->status_area_widget()->system_tray();
541 void RootWindowController::ShowContextMenu(const gfx::Point& location_in_screen,
542 ui::MenuSourceType source_type) {
543 DCHECK(Shell::GetInstance()->delegate());
544 scoped_ptr<ui::MenuModel> menu_model(
545 Shell::GetInstance()->delegate()->CreateContextMenu(root_window(),
546 NULL,
547 NULL));
548 if (!menu_model)
549 return;
551 // Background controller may not be set yet if user clicked on status are
552 // before initial animation completion. See crbug.com/222218
553 if (!wallpaper_controller_.get())
554 return;
556 views::MenuRunner menu_runner(menu_model.get());
557 if (menu_runner.RunMenuAt(wallpaper_controller_->widget(),
558 NULL, gfx::Rect(location_in_screen, gfx::Size()),
559 views::MenuItemView::TOPLEFT, source_type,
560 views::MenuRunner::CONTEXT_MENU) ==
561 views::MenuRunner::MENU_DELETED) {
562 return;
565 Shell::GetInstance()->UpdateShelfVisibility();
568 void RootWindowController::UpdateShelfVisibility() {
569 shelf_->shelf_layout_manager()->UpdateVisibilityState();
572 const aura::Window* RootWindowController::GetWindowForFullscreenMode() const {
573 const aura::Window::Windows& windows =
574 GetContainer(kShellWindowId_DefaultContainer)->children();
575 const aura::Window* topmost_window = NULL;
576 for (aura::Window::Windows::const_reverse_iterator iter = windows.rbegin();
577 iter != windows.rend(); ++iter) {
578 if (((*iter)->type() == ui::wm::WINDOW_TYPE_NORMAL ||
579 (*iter)->type() == ui::wm::WINDOW_TYPE_PANEL) &&
580 (*iter)->layer()->GetTargetVisibility()) {
581 topmost_window = *iter;
582 break;
585 while (topmost_window) {
586 if (wm::GetWindowState(topmost_window)->IsFullscreen())
587 return topmost_window;
588 topmost_window = views::corewm::GetTransientParent(topmost_window);
590 return NULL;
593 void RootWindowController::ActivateKeyboard(
594 keyboard::KeyboardController* keyboard_controller) {
595 if (!keyboard::IsKeyboardEnabled() ||
596 GetContainer(kShellWindowId_VirtualKeyboardContainer)) {
597 return;
599 DCHECK(keyboard_controller);
600 if (!keyboard::IsKeyboardUsabilityExperimentEnabled()) {
601 keyboard_controller->AddObserver(shelf()->shelf_layout_manager());
602 keyboard_controller->AddObserver(panel_layout_manager_);
603 keyboard_controller->AddObserver(docked_layout_manager_);
605 aura::Window* parent = root_window();
606 aura::Window* keyboard_container =
607 keyboard_controller->GetContainerWindow();
608 keyboard_container->set_id(kShellWindowId_VirtualKeyboardContainer);
609 parent->AddChild(keyboard_container);
610 // TODO(oshima): Bounds of keyboard container should be handled by
611 // RootWindowLayoutManager. Remove this after fixed RootWindowLayoutManager.
612 keyboard_container->SetBounds(parent->bounds());
615 void RootWindowController::DeactivateKeyboard(
616 keyboard::KeyboardController* keyboard_controller) {
617 if (!keyboard_controller ||
618 !keyboard_controller->keyboard_container_initialized()) {
619 return;
621 aura::Window* keyboard_container =
622 keyboard_controller->GetContainerWindow();
623 if (keyboard_container->GetRootWindow() == root_window()) {
624 root_window()->RemoveChild(keyboard_container);
625 if (!keyboard::IsKeyboardUsabilityExperimentEnabled()) {
626 keyboard_controller->RemoveObserver(shelf()->shelf_layout_manager());
627 keyboard_controller->RemoveObserver(panel_layout_manager_);
628 keyboard_controller->RemoveObserver(docked_layout_manager_);
633 ////////////////////////////////////////////////////////////////////////////////
634 // RootWindowController, private:
636 RootWindowController::RootWindowController(aura::RootWindow* root_window)
637 : root_window_(root_window),
638 root_window_layout_(NULL),
639 docked_layout_manager_(NULL),
640 panel_layout_manager_(NULL),
641 touch_hud_debug_(NULL),
642 touch_hud_projection_(NULL) {
643 GetRootWindowSettings(root_window_->window())->controller = this;
644 screen_dimmer_.reset(new ScreenDimmer(root_window_->window()));
646 stacking_controller_.reset(new StackingController);
647 aura::client::SetWindowTreeClient(root_window_->window(),
648 stacking_controller_.get());
649 capture_client_.reset(
650 new views::corewm::ScopedCaptureClient(root_window_->window()));
653 void RootWindowController::Init(RootWindowType root_window_type,
654 bool first_run_after_boot) {
655 Shell* shell = Shell::GetInstance();
656 shell->InitRootWindow(root_window());
658 root_window_->SetCursor(ui::kCursorPointer);
659 CreateContainersInRootWindow(root_window_->window());
661 if (root_window_type == VIRTUAL_KEYBOARD) {
662 shell->InitKeyboard();
663 return;
666 CreateSystemBackground(first_run_after_boot);
668 InitLayoutManagers();
669 InitTouchHuds();
671 if (Shell::GetPrimaryRootWindowController()->
672 GetSystemModalLayoutManager(NULL)->has_modal_background()) {
673 GetSystemModalLayoutManager(NULL)->CreateModalBackground();
676 shell->AddShellObserver(this);
678 if (root_window_type == PRIMARY) {
679 root_window_layout()->OnWindowResized();
680 if (!keyboard::IsKeyboardUsabilityExperimentEnabled())
681 shell->InitKeyboard();
682 } else {
683 root_window_layout()->OnWindowResized();
684 shell->desktop_background_controller()->OnRootWindowAdded(root_window());
685 shell->high_contrast_controller()->OnRootWindowAdded(
686 root_window_->window());
687 root_window_->host()->Show();
689 // Create a shelf if a user is already logged in.
690 if (shell->session_state_delegate()->NumberOfLoggedInUsers())
691 shelf()->CreateShelf();
695 void RootWindowController::InitLayoutManagers() {
696 root_window_layout_ = new RootWindowLayoutManager(root_window());
697 root_window()->SetLayoutManager(root_window_layout_);
699 aura::Window* default_container =
700 GetContainer(kShellWindowId_DefaultContainer);
701 // Workspace manager has its own layout managers.
702 workspace_controller_.reset(
703 new WorkspaceController(default_container));
705 aura::Window* always_on_top_container =
706 GetContainer(kShellWindowId_AlwaysOnTopContainer);
707 always_on_top_container->SetLayoutManager(
708 new internal::WorkspaceLayoutManager(
709 always_on_top_container));
710 always_on_top_controller_.reset(new internal::AlwaysOnTopController);
711 always_on_top_controller_->SetAlwaysOnTopContainer(always_on_top_container);
713 DCHECK(!shelf_.get());
714 aura::Window* shelf_container =
715 GetContainer(internal::kShellWindowId_ShelfContainer);
716 // TODO(harrym): Remove when status area is view.
717 aura::Window* status_container =
718 GetContainer(internal::kShellWindowId_StatusContainer);
719 shelf_.reset(new ShelfWidget(
720 shelf_container, status_container, workspace_controller()));
722 if (!Shell::GetInstance()->session_state_delegate()->
723 IsActiveUserSessionStarted()) {
724 // This window exists only to be a event target on login screen.
725 // It does not have to handle events, nor be visible.
726 mouse_event_target_.reset(new aura::Window(new EmptyWindowDelegate));
727 mouse_event_target_->Init(aura::WINDOW_LAYER_NOT_DRAWN);
729 aura::Window* lock_background_container =
730 GetContainer(internal::kShellWindowId_LockScreenBackgroundContainer);
731 lock_background_container->AddChild(mouse_event_target_.get());
732 mouse_event_target_->Show();
735 // Create Docked windows layout manager
736 aura::Window* docked_container = GetContainer(
737 internal::kShellWindowId_DockedContainer);
738 docked_layout_manager_ =
739 new internal::DockedWindowLayoutManager(docked_container,
740 workspace_controller());
741 docked_container->SetLayoutManager(docked_layout_manager_);
743 // Create Panel layout manager
744 aura::Window* panel_container = GetContainer(
745 internal::kShellWindowId_PanelContainer);
746 panel_layout_manager_ =
747 new internal::PanelLayoutManager(panel_container);
748 panel_container->SetLayoutManager(panel_layout_manager_);
749 panel_container_handler_.reset(new PanelWindowEventHandler);
750 panel_container->AddPreTargetHandler(panel_container_handler_.get());
753 void RootWindowController::InitTouchHuds() {
754 CommandLine* command_line = CommandLine::ForCurrentProcess();
755 if (command_line->HasSwitch(switches::kAshTouchHud))
756 set_touch_hud_debug(new TouchHudDebug(root_window()));
757 if (Shell::GetInstance()->is_touch_hud_projection_enabled())
758 EnableTouchHudProjection();
761 void RootWindowController::CreateSystemBackground(
762 bool is_first_run_after_boot) {
763 SkColor color = SK_ColorBLACK;
764 #if defined(OS_CHROMEOS)
765 if (is_first_run_after_boot)
766 color = kChromeOsBootColor;
767 #endif
768 system_background_.reset(
769 new SystemBackgroundController(root_window(), color));
771 #if defined(OS_CHROMEOS)
772 // Make a copy of the system's boot splash screen so we can composite it
773 // onscreen until the desktop background is ready.
774 if (is_first_run_after_boot &&
775 (CommandLine::ForCurrentProcess()->HasSwitch(
776 switches::kAshCopyHostBackgroundAtBoot) ||
777 CommandLine::ForCurrentProcess()->HasSwitch(
778 switches::kAshAnimateFromBootSplashScreen)))
779 boot_splash_screen_.reset(new BootSplashScreen(root_window_.get()));
780 #endif
783 void RootWindowController::CreateContainersInRootWindow(
784 aura::Window* root_window) {
785 // These containers are just used by PowerButtonController to animate groups
786 // of containers simultaneously without messing up the current transformations
787 // on those containers. These are direct children of the root window; all of
788 // the other containers are their children.
790 // The desktop background container is not part of the lock animation, so it
791 // is not included in those animate groups.
792 // When screen is locked desktop background is moved to lock screen background
793 // container (moved back on unlock). We want to make sure that there's an
794 // opaque layer occluding the non-lock-screen layers.
795 aura::Window* desktop_background_container = CreateContainer(
796 kShellWindowId_DesktopBackgroundContainer,
797 "DesktopBackgroundContainer",
798 root_window);
799 views::corewm::SetChildWindowVisibilityChangesAnimated(
800 desktop_background_container);
802 aura::Window* non_lock_screen_containers = CreateContainer(
803 kShellWindowId_NonLockScreenContainersContainer,
804 "NonLockScreenContainersContainer",
805 root_window);
807 aura::Window* lock_background_containers = CreateContainer(
808 kShellWindowId_LockScreenBackgroundContainer,
809 "LockScreenBackgroundContainer",
810 root_window);
811 views::corewm::SetChildWindowVisibilityChangesAnimated(
812 lock_background_containers);
814 aura::Window* lock_screen_containers = CreateContainer(
815 kShellWindowId_LockScreenContainersContainer,
816 "LockScreenContainersContainer",
817 root_window);
818 aura::Window* lock_screen_related_containers = CreateContainer(
819 kShellWindowId_LockScreenRelatedContainersContainer,
820 "LockScreenRelatedContainersContainer",
821 root_window);
823 CreateContainer(kShellWindowId_UnparentedControlContainer,
824 "UnparentedControlContainer",
825 non_lock_screen_containers);
827 aura::Window* default_container = CreateContainer(
828 kShellWindowId_DefaultContainer,
829 "DefaultContainer",
830 non_lock_screen_containers);
831 views::corewm::SetChildWindowVisibilityChangesAnimated(default_container);
832 SetUsesScreenCoordinates(default_container);
833 SetUsesEasyResizeTargeter(default_container);
835 aura::Window* always_on_top_container = CreateContainer(
836 kShellWindowId_AlwaysOnTopContainer,
837 "AlwaysOnTopContainer",
838 non_lock_screen_containers);
839 views::corewm::SetChildWindowVisibilityChangesAnimated(
840 always_on_top_container);
841 SetUsesScreenCoordinates(always_on_top_container);
843 aura::Window* docked_container = CreateContainer(
844 kShellWindowId_DockedContainer,
845 "DockedContainer",
846 non_lock_screen_containers);
847 views::corewm::SetChildWindowVisibilityChangesAnimated(docked_container);
848 SetUsesScreenCoordinates(docked_container);
849 SetUsesEasyResizeTargeter(docked_container);
851 aura::Window* shelf_container =
852 CreateContainer(kShellWindowId_ShelfContainer,
853 "ShelfContainer",
854 non_lock_screen_containers);
855 SetUsesScreenCoordinates(shelf_container);
856 DescendantShouldStayInSameRootWindow(shelf_container);
858 aura::Window* panel_container = CreateContainer(
859 kShellWindowId_PanelContainer,
860 "PanelContainer",
861 non_lock_screen_containers);
862 SetUsesScreenCoordinates(panel_container);
863 SetUsesEasyResizeTargeter(panel_container);
865 aura::Window* shelf_bubble_container =
866 CreateContainer(kShellWindowId_ShelfBubbleContainer,
867 "ShelfBubbleContainer",
868 non_lock_screen_containers);
869 SetUsesScreenCoordinates(shelf_bubble_container);
870 DescendantShouldStayInSameRootWindow(shelf_bubble_container);
872 aura::Window* app_list_container =
873 CreateContainer(kShellWindowId_AppListContainer,
874 "AppListContainer",
875 non_lock_screen_containers);
876 SetUsesScreenCoordinates(app_list_container);
878 aura::Window* modal_container = CreateContainer(
879 kShellWindowId_SystemModalContainer,
880 "SystemModalContainer",
881 non_lock_screen_containers);
882 modal_container->SetLayoutManager(
883 new SystemModalContainerLayoutManager(modal_container));
884 views::corewm::SetChildWindowVisibilityChangesAnimated(modal_container);
885 SetUsesScreenCoordinates(modal_container);
886 SetUsesEasyResizeTargeter(modal_container);
888 aura::Window* input_method_container = CreateContainer(
889 kShellWindowId_InputMethodContainer,
890 "InputMethodContainer",
891 non_lock_screen_containers);
892 views::corewm::SetChildWindowVisibilityChangesAnimated(
893 input_method_container);
894 SetUsesScreenCoordinates(input_method_container);
896 // TODO(beng): Figure out if we can make this use
897 // SystemModalContainerEventFilter instead of stops_event_propagation.
898 aura::Window* lock_container = CreateContainer(
899 kShellWindowId_LockScreenContainer,
900 "LockScreenContainer",
901 lock_screen_containers);
902 lock_container->SetLayoutManager(
903 new internal::WorkspaceLayoutManager(lock_container));
904 SetUsesScreenCoordinates(lock_container);
905 // TODO(beng): stopsevents
907 aura::Window* lock_modal_container = CreateContainer(
908 kShellWindowId_LockSystemModalContainer,
909 "LockSystemModalContainer",
910 lock_screen_containers);
911 lock_modal_container->SetLayoutManager(
912 new SystemModalContainerLayoutManager(lock_modal_container));
913 views::corewm::SetChildWindowVisibilityChangesAnimated(lock_modal_container);
914 SetUsesScreenCoordinates(lock_modal_container);
915 SetUsesEasyResizeTargeter(lock_modal_container);
917 aura::Window* status_container =
918 CreateContainer(kShellWindowId_StatusContainer,
919 "StatusContainer",
920 lock_screen_related_containers);
921 SetUsesScreenCoordinates(status_container);
922 DescendantShouldStayInSameRootWindow(status_container);
924 aura::Window* settings_bubble_container = CreateContainer(
925 kShellWindowId_SettingBubbleContainer,
926 "SettingBubbleContainer",
927 lock_screen_related_containers);
928 views::corewm::SetChildWindowVisibilityChangesAnimated(
929 settings_bubble_container);
930 SetUsesScreenCoordinates(settings_bubble_container);
931 DescendantShouldStayInSameRootWindow(settings_bubble_container);
933 aura::Window* menu_container = CreateContainer(
934 kShellWindowId_MenuContainer,
935 "MenuContainer",
936 lock_screen_related_containers);
937 views::corewm::SetChildWindowVisibilityChangesAnimated(menu_container);
938 SetUsesScreenCoordinates(menu_container);
940 aura::Window* drag_drop_container = CreateContainer(
941 kShellWindowId_DragImageAndTooltipContainer,
942 "DragImageAndTooltipContainer",
943 lock_screen_related_containers);
944 views::corewm::SetChildWindowVisibilityChangesAnimated(drag_drop_container);
945 SetUsesScreenCoordinates(drag_drop_container);
947 aura::Window* overlay_container = CreateContainer(
948 kShellWindowId_OverlayContainer,
949 "OverlayContainer",
950 lock_screen_related_containers);
951 SetUsesScreenCoordinates(overlay_container);
953 CreateContainer(kShellWindowId_PowerButtonAnimationContainer,
954 "PowerButtonAnimationContainer", root_window) ;
957 void RootWindowController::EnableTouchHudProjection() {
958 if (touch_hud_projection_)
959 return;
960 set_touch_hud_projection(new TouchHudProjection(root_window()));
963 void RootWindowController::DisableTouchHudProjection() {
964 if (!touch_hud_projection_)
965 return;
966 touch_hud_projection_->Remove();
969 void RootWindowController::OnLoginStateChanged(user::LoginStatus status) {
970 shelf_->shelf_layout_manager()->UpdateVisibilityState();
973 void RootWindowController::OnTouchHudProjectionToggled(bool enabled) {
974 if (enabled)
975 EnableTouchHudProjection();
976 else
977 DisableTouchHudProjection();
980 RootWindowController* GetRootWindowController(
981 const aura::Window* root_window) {
982 return root_window ? GetRootWindowSettings(root_window)->controller : NULL;
985 } // namespace internal
986 } // namespace ash