OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "apps/shell/browser/shell_desktop_controller.h" | 5 #include "apps/shell/browser/shell_desktop_controller.h" |
6 | 6 |
7 #include "apps/shell/browser/shell_app_window.h" | 7 #include "apps/shell/browser/shell_app_window.h" |
8 #include "content/public/browser/context_factory.h" | 8 #include "content/public/browser/context_factory.h" |
9 #include "ui/aura/client/cursor_client.h" | 9 #include "ui/aura/client/cursor_client.h" |
| 10 #include "ui/aura/client/default_capture_client.h" |
10 #include "ui/aura/env.h" | 11 #include "ui/aura/env.h" |
11 #include "ui/aura/layout_manager.h" | 12 #include "ui/aura/layout_manager.h" |
12 #include "ui/aura/test/test_screen.h" | 13 #include "ui/aura/test/test_screen.h" |
13 #include "ui/aura/window.h" | 14 #include "ui/aura/window.h" |
14 #include "ui/aura/window_event_dispatcher.h" | 15 #include "ui/aura/window_event_dispatcher.h" |
15 #include "ui/aura/window_tree_host.h" | 16 #include "ui/aura/window_tree_host.h" |
16 #include "ui/base/cursor/cursor.h" | 17 #include "ui/base/cursor/cursor.h" |
17 #include "ui/base/cursor/image_cursors.h" | 18 #include "ui/base/cursor/image_cursors.h" |
18 #include "ui/base/ime/input_method_initializer.h" | 19 #include "ui/base/ime/input_method_initializer.h" |
19 #include "ui/gfx/native_widget_types.h" | 20 #include "ui/gfx/native_widget_types.h" |
20 #include "ui/gfx/screen.h" | 21 #include "ui/gfx/screen.h" |
| 22 #include "ui/wm/core/base_focus_rules.h" |
| 23 #include "ui/wm/core/compound_event_filter.h" |
21 #include "ui/wm/core/cursor_manager.h" | 24 #include "ui/wm/core/cursor_manager.h" |
| 25 #include "ui/wm/core/default_activation_client.h" |
| 26 #include "ui/wm/core/focus_controller.h" |
| 27 #include "ui/wm/core/input_method_event_filter.h" |
22 #include "ui/wm/core/native_cursor_manager.h" | 28 #include "ui/wm/core/native_cursor_manager.h" |
23 #include "ui/wm/core/native_cursor_manager_delegate.h" | 29 #include "ui/wm/core/native_cursor_manager_delegate.h" |
24 #include "ui/wm/core/user_activity_detector.h" | 30 #include "ui/wm/core/user_activity_detector.h" |
25 #include "ui/wm/test/wm_test_helper.h" | |
26 | 31 |
27 #if defined(OS_CHROMEOS) | 32 #if defined(OS_CHROMEOS) |
28 #include "ui/chromeos/user_activity_power_manager_notifier.h" | 33 #include "ui/chromeos/user_activity_power_manager_notifier.h" |
29 #include "ui/display/types/chromeos/display_mode.h" | 34 #include "ui/display/types/chromeos/display_mode.h" |
30 #include "ui/display/types/chromeos/display_snapshot.h" | 35 #include "ui/display/types/chromeos/display_snapshot.h" |
31 #endif | 36 #endif |
32 | 37 |
33 namespace apps { | 38 namespace apps { |
34 namespace { | 39 namespace { |
35 | 40 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 host_->SetCursor(cursor); | 136 host_->SetCursor(cursor); |
132 } | 137 } |
133 | 138 |
134 aura::WindowTreeHost* host_; // Not owned. | 139 aura::WindowTreeHost* host_; // Not owned. |
135 | 140 |
136 scoped_ptr<ui::ImageCursors> image_cursors_; | 141 scoped_ptr<ui::ImageCursors> image_cursors_; |
137 | 142 |
138 DISALLOW_COPY_AND_ASSIGN(ShellNativeCursorManager); | 143 DISALLOW_COPY_AND_ASSIGN(ShellNativeCursorManager); |
139 }; | 144 }; |
140 | 145 |
| 146 class AppsFocusRules : public wm::BaseFocusRules { |
| 147 public: |
| 148 AppsFocusRules() {} |
| 149 virtual ~AppsFocusRules() {} |
| 150 |
| 151 virtual bool SupportsChildActivation(aura::Window* window) const OVERRIDE { |
| 152 return true; |
| 153 } |
| 154 |
| 155 private: |
| 156 DISALLOW_COPY_AND_ASSIGN(AppsFocusRules); |
| 157 }; |
| 158 |
141 ShellDesktopController* g_instance = NULL; | 159 ShellDesktopController* g_instance = NULL; |
142 | 160 |
143 } // namespace | 161 } // namespace |
144 | 162 |
145 ShellDesktopController::ShellDesktopController() { | 163 ShellDesktopController::ShellDesktopController() { |
146 #if defined(OS_CHROMEOS) | 164 #if defined(OS_CHROMEOS) |
147 display_configurator_.reset(new ui::DisplayConfigurator); | 165 display_configurator_.reset(new ui::DisplayConfigurator); |
148 display_configurator_->Init(false); | 166 display_configurator_->Init(false); |
149 display_configurator_->ForceInitialConfigure(0); | 167 display_configurator_->ForceInitialConfigure(0); |
150 display_configurator_->AddObserver(this); | 168 display_configurator_->AddObserver(this); |
151 #endif | 169 #endif |
152 CreateRootWindow(); | 170 aura::Env::CreateInstance(true); |
153 | 171 aura::Env::GetInstance()->set_context_factory(content::GetContextFactory()); |
154 cursor_manager_.reset( | |
155 new wm::CursorManager(scoped_ptr<wm::NativeCursorManager>( | |
156 new ShellNativeCursorManager(GetWindowTreeHost())))); | |
157 cursor_manager_->SetDisplay( | |
158 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay()); | |
159 cursor_manager_->SetCursor(ui::kCursorPointer); | |
160 aura::client::SetCursorClient( | |
161 GetWindowTreeHost()->window(), cursor_manager_.get()); | |
162 | |
163 user_activity_detector_.reset(new wm::UserActivityDetector); | |
164 GetWindowTreeHost()->event_processor()->GetRootTarget()->AddPreTargetHandler( | |
165 user_activity_detector_.get()); | |
166 #if defined(OS_CHROMEOS) | |
167 user_activity_notifier_.reset( | |
168 new ui::UserActivityPowerManagerNotifier(user_activity_detector_.get())); | |
169 #endif | |
170 | 172 |
171 g_instance = this; | 173 g_instance = this; |
172 } | 174 } |
173 | 175 |
174 ShellDesktopController::~ShellDesktopController() { | 176 ShellDesktopController::~ShellDesktopController() { |
175 // The app window must be explicitly closed before desktop teardown. | 177 // The app window must be explicitly closed before desktop teardown. |
176 DCHECK(!app_window_); | 178 DCHECK(!app_window_); |
177 g_instance = NULL; | 179 g_instance = NULL; |
178 GetWindowTreeHost()->event_processor()->GetRootTarget() | |
179 ->RemovePreTargetHandler(user_activity_detector_.get()); | |
180 DestroyRootWindow(); | 180 DestroyRootWindow(); |
181 aura::Env::DeleteInstance(); | 181 aura::Env::DeleteInstance(); |
182 } | 182 } |
183 | 183 |
184 // static | 184 // static |
185 ShellDesktopController* ShellDesktopController::instance() { | 185 ShellDesktopController* ShellDesktopController::instance() { |
186 return g_instance; | 186 return g_instance; |
187 } | 187 } |
188 | 188 |
189 ShellAppWindow* ShellDesktopController::CreateAppWindow( | 189 ShellAppWindow* ShellDesktopController::CreateAppWindow( |
190 content::BrowserContext* context) { | 190 content::BrowserContext* context) { |
191 aura::Window* root_window = GetWindowTreeHost()->window(); | 191 aura::Window* root_window = host_->window(); |
192 | 192 |
193 app_window_.reset(new ShellAppWindow); | 193 app_window_.reset(new ShellAppWindow); |
194 app_window_->Init(context, root_window->bounds().size()); | 194 app_window_->Init(context, root_window->bounds().size()); |
195 | 195 |
196 // Attach the web contents view to our window hierarchy. | 196 // Attach the web contents view to our window hierarchy. |
197 aura::Window* content = app_window_->GetNativeWindow(); | 197 aura::Window* content = app_window_->GetNativeWindow(); |
198 root_window->AddChild(content); | 198 root_window->AddChild(content); |
199 content->Show(); | 199 content->Show(); |
200 | 200 |
201 return app_window_.get(); | 201 return app_window_.get(); |
202 } | 202 } |
203 | 203 |
204 void ShellDesktopController::CloseAppWindow() { app_window_.reset(); } | 204 void ShellDesktopController::CloseAppWindow() { app_window_.reset(); } |
205 | 205 |
206 aura::WindowTreeHost* ShellDesktopController::GetWindowTreeHost() { | 206 aura::Window* ShellDesktopController::GetDefaultParent( |
207 return wm_test_helper_->host(); | 207 aura::Window* context, |
| 208 aura::Window* window, |
| 209 const gfx::Rect& bounds) { |
| 210 return host_->window(); |
208 } | 211 } |
209 | 212 |
210 #if defined(OS_CHROMEOS) | 213 #if defined(OS_CHROMEOS) |
211 void ShellDesktopController::OnDisplayModeChanged( | 214 void ShellDesktopController::OnDisplayModeChanged( |
212 const std::vector<ui::DisplayConfigurator::DisplayState>& displays) { | 215 const std::vector<ui::DisplayConfigurator::DisplayState>& displays) { |
213 gfx::Size size = GetPrimaryDisplaySize(); | 216 gfx::Size size = GetPrimaryDisplaySize(); |
214 if (!size.IsEmpty()) | 217 if (!size.IsEmpty()) |
215 wm_test_helper_->host()->UpdateRootWindowSize(size); | 218 host_->UpdateRootWindowSize(size); |
216 } | 219 } |
217 #endif | 220 #endif |
218 | 221 |
| 222 void ShellDesktopController::OnHostCloseRequested( |
| 223 const aura::WindowTreeHost* host) { |
| 224 DCHECK_EQ(host_.get(), host); |
| 225 CloseAppWindow(); |
| 226 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 227 base::MessageLoop::QuitClosure()); |
| 228 } |
| 229 |
219 void ShellDesktopController::CreateRootWindow() { | 230 void ShellDesktopController::CreateRootWindow() { |
220 test_screen_.reset(aura::TestScreen::Create()); | 231 test_screen_.reset(aura::TestScreen::Create()); |
221 // TODO(jamescook): Replace this with a real Screen implementation. | 232 // TODO(jamescook): Replace this with a real Screen implementation. |
222 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen_.get()); | 233 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen_.get()); |
223 // TODO(jamescook): Initialize a real input method. | 234 // TODO(jamescook): Initialize a real input method. |
224 ui::InitializeInputMethodForTesting(); | 235 ui::InitializeInputMethodForTesting(); |
225 | 236 |
226 // Set up basic pieces of ui::wm. | 237 // Set up basic pieces of ui::wm. |
227 gfx::Size size = GetPrimaryDisplaySize(); | 238 gfx::Size size = GetPrimaryDisplaySize(); |
228 if (size.IsEmpty()) | 239 if (size.IsEmpty()) |
229 size = gfx::Size(800, 600); | 240 size = gfx::Size(800, 600); |
230 wm_test_helper_.reset( | 241 |
231 new wm::WMTestHelper(size, content::GetContextFactory())); | 242 host_.reset(aura::WindowTreeHost::Create(gfx::Rect(size))); |
| 243 host_->InitHost(); |
| 244 aura::client::SetWindowTreeClient(host_->window(), this); |
| 245 root_window_event_filter_.reset(new wm::CompoundEventFilter); |
| 246 host_->window()->AddPreTargetHandler(root_window_event_filter_.get()); |
| 247 InitWindowManager(); |
| 248 |
| 249 host_->AddObserver(this); |
| 250 |
| 251 // Ensure the X window gets mapped. |
| 252 host_->Show(); |
| 253 } |
| 254 |
| 255 void ShellDesktopController::InitWindowManager() { |
| 256 focus_client_.reset(new wm::FocusController(new AppsFocusRules())); |
| 257 aura::client::SetFocusClient(host_->window(), focus_client_.get()); |
| 258 |
| 259 input_method_filter_.reset( |
| 260 new wm::InputMethodEventFilter(host_->GetAcceleratedWidget())); |
| 261 input_method_filter_->SetInputMethodPropertyInRootWindow(host_->window()); |
| 262 root_window_event_filter_->AddHandler(input_method_filter_.get()); |
| 263 |
| 264 new wm::DefaultActivationClient(host_->window()); |
| 265 |
| 266 capture_client_.reset( |
| 267 new aura::client::DefaultCaptureClient(host_->window())); |
232 | 268 |
233 // Ensure new windows fill the display. | 269 // Ensure new windows fill the display. |
234 aura::WindowTreeHost* host = wm_test_helper_->host(); | 270 host_->window()->SetLayoutManager(new FillLayout); |
235 host->window()->SetLayoutManager(new FillLayout); | |
236 | 271 |
237 // Ensure the X window gets mapped. | 272 cursor_manager_.reset( |
238 host->Show(); | 273 new wm::CursorManager(scoped_ptr<wm::NativeCursorManager>( |
| 274 new ShellNativeCursorManager(host_.get())))); |
| 275 cursor_manager_->SetDisplay( |
| 276 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay()); |
| 277 cursor_manager_->SetCursor(ui::kCursorPointer); |
| 278 aura::client::SetCursorClient(host_->window(), cursor_manager_.get()); |
| 279 |
| 280 user_activity_detector_.reset(new wm::UserActivityDetector); |
| 281 host_->event_processor()->GetRootTarget()->AddPreTargetHandler( |
| 282 user_activity_detector_.get()); |
| 283 #if defined(OS_CHROMEOS) |
| 284 user_activity_notifier_.reset( |
| 285 new ui::UserActivityPowerManagerNotifier(user_activity_detector_.get())); |
| 286 #endif |
239 } | 287 } |
240 | 288 |
241 void ShellDesktopController::DestroyRootWindow() { | 289 void ShellDesktopController::DestroyRootWindow() { |
242 wm_test_helper_.reset(); | 290 host_->RemoveObserver(this); |
| 291 if (input_method_filter_) |
| 292 root_window_event_filter_->RemoveHandler(input_method_filter_.get()); |
| 293 if (user_activity_detector_) { |
| 294 host_->event_processor()->GetRootTarget()->RemovePreTargetHandler( |
| 295 user_activity_detector_.get()); |
| 296 } |
| 297 root_window_event_filter_.reset(); |
| 298 capture_client_.reset(); |
| 299 input_method_filter_.reset(); |
| 300 focus_client_.reset(); |
| 301 cursor_manager_.reset(); |
| 302 #if defined(OS_CHROMEOS) |
| 303 user_activity_notifier_.reset(); |
| 304 #endif |
| 305 user_activity_detector_.reset(); |
| 306 host_.reset(); |
243 ui::ShutdownInputMethodForTesting(); | 307 ui::ShutdownInputMethodForTesting(); |
244 } | 308 } |
245 | 309 |
246 gfx::Size ShellDesktopController::GetPrimaryDisplaySize() { | 310 gfx::Size ShellDesktopController::GetPrimaryDisplaySize() { |
247 #if defined(OS_CHROMEOS) | 311 #if defined(OS_CHROMEOS) |
248 const std::vector<ui::DisplayConfigurator::DisplayState>& displays = | 312 const std::vector<ui::DisplayConfigurator::DisplayState>& displays = |
249 display_configurator_->cached_displays(); | 313 display_configurator_->cached_displays(); |
250 if (displays.empty()) | 314 if (displays.empty()) |
251 return gfx::Size(); | 315 return gfx::Size(); |
252 const ui::DisplayMode* mode = displays[0].display->current_mode(); | 316 const ui::DisplayMode* mode = displays[0].display->current_mode(); |
253 return mode ? mode->size() : gfx::Size(); | 317 return mode ? mode->size() : gfx::Size(); |
254 #else | 318 #else |
255 return gfx::Size(); | 319 return gfx::Size(); |
256 #endif | 320 #endif |
257 } | 321 } |
258 | 322 |
259 } // namespace apps | 323 } // namespace apps |
OLD | NEW |