[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / ash / wm / stacking_controller.cc
bloba9df31da6f2e5d642bda0f08dbcda6ef22e9b447
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/wm/stacking_controller.h"
7 #include "ash/display/display_controller.h"
8 #include "ash/shell.h"
9 #include "ash/shell_delegate.h"
10 #include "ash/shell_window_ids.h"
11 #include "ash/wm/always_on_top_controller.h"
12 #include "ash/wm/coordinate_conversion.h"
13 #include "ash/wm/window_properties.h"
14 #include "ui/aura/client/aura_constants.h"
15 #include "ui/aura/root_window.h"
16 #include "ui/aura/window.h"
17 #include "ui/base/ui_base_types.h"
19 namespace ash {
20 namespace {
22 // Find a root window that matches the |bounds|. If the virtual screen
23 // coordinates is enabled and the bounds is specified, the root window
24 // that matches the window's bound will be used. Otherwise, it'll
25 // return the active root window.
26 aura::RootWindow* FindContainerRoot(const gfx::Rect& bounds) {
27 if (bounds.x() == 0 && bounds.y() == 0 && bounds.IsEmpty())
28 return Shell::GetActiveRootWindow();
29 return wm::GetRootWindowMatching(bounds);
32 aura::Window* GetContainerById(aura::RootWindow* root, int id) {
33 return Shell::GetContainer(root, id);
36 aura::Window* GetContainerForWindow(aura::Window* window) {
37 aura::Window* container = window->parent();
38 while (container && container->type() != aura::client::WINDOW_TYPE_UNKNOWN)
39 container = container->parent();
40 return container;
43 bool IsSystemModal(aura::Window* window) {
44 return window->GetProperty(aura::client::kModalKey) == ui::MODAL_TYPE_SYSTEM;
47 bool IsWindowModal(aura::Window* window) {
48 return window->transient_parent() &&
49 window->GetProperty(aura::client::kModalKey) == ui::MODAL_TYPE_WINDOW;
52 } // namespace
54 ////////////////////////////////////////////////////////////////////////////////
55 // StackingController, public:
57 StackingController::StackingController() {
60 StackingController::~StackingController() {
63 ////////////////////////////////////////////////////////////////////////////////
64 // StackingController, aura::StackingClient implementation:
66 aura::Window* StackingController::GetDefaultParent(aura::Window* context,
67 aura::Window* window,
68 const gfx::Rect& bounds) {
69 aura::RootWindow* target_root = NULL;
70 if (window->transient_parent()) {
71 // Transient window should use the same root as its transient parent.
72 target_root = window->transient_parent()->GetRootWindow();
73 } else {
74 target_root = FindContainerRoot(bounds);
77 switch (window->type()) {
78 case aura::client::WINDOW_TYPE_NORMAL:
79 case aura::client::WINDOW_TYPE_POPUP:
80 if (IsSystemModal(window))
81 return GetSystemModalContainer(target_root, window);
82 else if (IsWindowModal(window))
83 return GetContainerForWindow(window->transient_parent());
84 return GetAlwaysOnTopController(target_root)->GetContainer(window);
85 case aura::client::WINDOW_TYPE_CONTROL:
86 return GetContainerById(
87 target_root, internal::kShellWindowId_UnparentedControlContainer);
88 case aura::client::WINDOW_TYPE_PANEL:
89 return GetContainerById(target_root,
90 internal::kShellWindowId_PanelContainer);
91 case aura::client::WINDOW_TYPE_MENU:
92 return GetContainerById(
93 target_root, internal::kShellWindowId_MenuContainer);
94 case aura::client::WINDOW_TYPE_TOOLTIP:
95 return GetContainerById(
96 target_root, internal::kShellWindowId_DragImageAndTooltipContainer);
97 default:
98 NOTREACHED() << "Window " << window->id()
99 << " has unhandled type " << window->type();
100 break;
102 return NULL;
105 ////////////////////////////////////////////////////////////////////////////////
106 // StackingController, private:
108 aura::Window* StackingController::GetSystemModalContainer(
109 aura::RootWindow* root,
110 aura::Window* window) const {
111 DCHECK(IsSystemModal(window));
113 // If screen lock is not active and user session is active,
114 // all modal windows are placed into the normal modal container.
115 if (!Shell::GetInstance()->delegate()->IsScreenLocked() &&
116 Shell::GetInstance()->delegate()->IsSessionStarted()) {
117 return GetContainerById(root,
118 internal::kShellWindowId_SystemModalContainer);
121 // Otherwise those that originate from LockScreen container and above are
122 // placed in the screen lock modal container.
123 aura::Window* lock_container =
124 GetContainerById(root, internal::kShellWindowId_LockScreenContainer);
125 int lock_container_id = lock_container->id();
126 int window_container_id = window->transient_parent()->parent()->id();
128 aura::Window* container = NULL;
129 if (window_container_id < lock_container_id) {
130 container = GetContainerById(
131 root, internal::kShellWindowId_SystemModalContainer);
132 } else {
133 container = GetContainerById(
134 root, internal::kShellWindowId_LockSystemModalContainer);
137 return container;
140 // TODO(oshima): Remove this once extended desktop is on by default.
141 internal::AlwaysOnTopController*
142 StackingController::GetAlwaysOnTopController(aura::RootWindow* root_window) {
143 internal::AlwaysOnTopController* controller =
144 root_window->GetProperty(internal::kAlwaysOnTopControllerKey);
145 if (!controller) {
146 controller = new internal::AlwaysOnTopController;
147 controller->SetAlwaysOnTopContainer(
148 root_window->GetChildById(
149 internal::kShellWindowId_AlwaysOnTopContainer));
150 // RootWindow owns the AlwaysOnTopController object.
151 root_window->SetProperty(internal::kAlwaysOnTopControllerKey, controller);
153 return controller;
156 } // namespace ash