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 | |
oshima
2014/05/30 16:22:51
virtual dtor
Jun Mukai
2014/05/30 19:57:47
Done.
| |
150 virtual bool SupportsChildActivation(aura::Window* window) const OVERRIDE { | |
151 return true; | |
152 } | |
153 | |
154 private: | |
155 DISALLOW_COPY_AND_ASSIGN(AppsFocusRules); | |
156 }; | |
157 | |
141 ShellDesktopController* g_instance = NULL; | 158 ShellDesktopController* g_instance = NULL; |
142 | 159 |
143 } // namespace | 160 } // namespace |
144 | 161 |
145 ShellDesktopController::ShellDesktopController() { | 162 ShellDesktopController::ShellDesktopController() { |
146 #if defined(OS_CHROMEOS) | 163 #if defined(OS_CHROMEOS) |
147 display_configurator_.reset(new ui::DisplayConfigurator); | 164 display_configurator_.reset(new ui::DisplayConfigurator); |
148 display_configurator_->Init(false); | 165 display_configurator_->Init(false); |
149 display_configurator_->ForceInitialConfigure(0); | 166 display_configurator_->ForceInitialConfigure(0); |
150 display_configurator_->AddObserver(this); | 167 display_configurator_->AddObserver(this); |
151 #endif | 168 #endif |
152 CreateRootWindow(); | 169 aura::Env::CreateInstance(true); |
153 | 170 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 | 171 |
171 g_instance = this; | 172 g_instance = this; |
172 } | 173 } |
James Cook
2014/05/30 17:30:18
I like how the constructor does less work now.
| |
173 | 174 |
174 ShellDesktopController::~ShellDesktopController() { | 175 ShellDesktopController::~ShellDesktopController() { |
175 // The app window must be explicitly closed before desktop teardown. | 176 // The app window must be explicitly closed before desktop teardown. |
176 DCHECK(!app_window_); | 177 DCHECK(!app_window_); |
177 g_instance = NULL; | 178 g_instance = NULL; |
178 GetWindowTreeHost()->event_processor()->GetRootTarget() | |
179 ->RemovePreTargetHandler(user_activity_detector_.get()); | |
180 DestroyRootWindow(); | 179 DestroyRootWindow(); |
181 aura::Env::DeleteInstance(); | 180 aura::Env::DeleteInstance(); |
182 } | 181 } |
183 | 182 |
184 // static | 183 // static |
185 ShellDesktopController* ShellDesktopController::instance() { | 184 ShellDesktopController* ShellDesktopController::instance() { |
186 return g_instance; | 185 return g_instance; |
187 } | 186 } |
188 | 187 |
189 ShellAppWindow* ShellDesktopController::CreateAppWindow( | 188 ShellAppWindow* ShellDesktopController::CreateAppWindow( |
190 content::BrowserContext* context) { | 189 content::BrowserContext* context) { |
191 aura::Window* root_window = GetWindowTreeHost()->window(); | 190 aura::Window* root_window = host_->window(); |
192 | 191 |
193 app_window_.reset(new ShellAppWindow); | 192 app_window_.reset(new ShellAppWindow); |
194 app_window_->Init(context, root_window->bounds().size()); | 193 app_window_->Init(context, root_window->bounds().size()); |
195 | 194 |
196 // Attach the web contents view to our window hierarchy. | 195 // Attach the web contents view to our window hierarchy. |
197 aura::Window* content = app_window_->GetNativeWindow(); | 196 aura::Window* content = app_window_->GetNativeWindow(); |
198 root_window->AddChild(content); | 197 root_window->AddChild(content); |
199 content->Show(); | 198 content->Show(); |
200 | 199 |
201 return app_window_.get(); | 200 return app_window_.get(); |
202 } | 201 } |
203 | 202 |
204 void ShellDesktopController::CloseAppWindow() { app_window_.reset(); } | 203 void ShellDesktopController::CloseAppWindow() { app_window_.reset(); } |
205 | 204 |
206 aura::WindowTreeHost* ShellDesktopController::GetWindowTreeHost() { | 205 aura::Window* ShellDesktopController::GetDefaultParent( |
207 return wm_test_helper_->host(); | 206 aura::Window* context, |
207 aura::Window* window, | |
208 const gfx::Rect& bounds) { | |
209 return host_->window(); | |
208 } | 210 } |
209 | 211 |
210 #if defined(OS_CHROMEOS) | 212 #if defined(OS_CHROMEOS) |
211 void ShellDesktopController::OnDisplayModeChanged( | 213 void ShellDesktopController::OnDisplayModeChanged( |
212 const std::vector<ui::DisplayConfigurator::DisplayState>& displays) { | 214 const std::vector<ui::DisplayConfigurator::DisplayState>& displays) { |
213 gfx::Size size = GetPrimaryDisplaySize(); | 215 gfx::Size size = GetPrimaryDisplaySize(); |
214 if (!size.IsEmpty()) | 216 if (!size.IsEmpty()) |
215 wm_test_helper_->host()->UpdateRootWindowSize(size); | 217 host_->UpdateRootWindowSize(size); |
216 } | 218 } |
217 #endif | 219 #endif |
218 | 220 |
221 void ShellDesktopController::OnHostCloseRequested( | |
222 const aura::WindowTreeHost* host) { | |
223 DCHECK_EQ(host_.get(), host); | |
224 CloseAppWindow(); | |
225 base::MessageLoop::current()->PostTask(FROM_HERE, | |
226 base::MessageLoop::QuitClosure()); | |
227 } | |
228 | |
219 void ShellDesktopController::CreateRootWindow() { | 229 void ShellDesktopController::CreateRootWindow() { |
220 test_screen_.reset(aura::TestScreen::Create()); | 230 test_screen_.reset(aura::TestScreen::Create()); |
221 // TODO(jamescook): Replace this with a real Screen implementation. | 231 // TODO(jamescook): Replace this with a real Screen implementation. |
222 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen_.get()); | 232 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen_.get()); |
223 // TODO(jamescook): Initialize a real input method. | 233 // TODO(jamescook): Initialize a real input method. |
224 ui::InitializeInputMethodForTesting(); | 234 ui::InitializeInputMethodForTesting(); |
225 | 235 |
226 // Set up basic pieces of ui::wm. | 236 // Set up basic pieces of ui::wm. |
227 gfx::Size size = GetPrimaryDisplaySize(); | 237 gfx::Size size = GetPrimaryDisplaySize(); |
228 if (size.IsEmpty()) | 238 if (size.IsEmpty()) |
229 size = gfx::Size(800, 600); | 239 size = gfx::Size(800, 600); |
230 wm_test_helper_.reset( | 240 |
231 new wm::WMTestHelper(size, content::GetContextFactory())); | 241 host_.reset(aura::WindowTreeHost::Create(gfx::Rect(size))); |
242 host_->InitHost(); | |
243 aura::client::SetWindowTreeClient(host_->window(), this); | |
244 root_window_event_filter_.reset(new wm::CompoundEventFilter); | |
245 host_->window()->AddPreTargetHandler(root_window_event_filter_.get()); | |
246 InitWindowManager(); | |
247 | |
248 host_->AddObserver(this); | |
249 | |
250 // Ensure the X window gets mapped. | |
251 host_->Show(); | |
252 } | |
253 | |
254 void ShellDesktopController::InitWindowManager() { | |
255 focus_client_.reset(new wm::FocusController(new AppsFocusRules())); | |
256 aura::client::SetFocusClient(host_->window(), focus_client_.get()); | |
257 | |
258 input_method_filter_.reset( | |
259 new wm::InputMethodEventFilter(host_->GetAcceleratedWidget())); | |
260 input_method_filter_->SetInputMethodPropertyInRootWindow(host_->window()); | |
261 root_window_event_filter_->AddHandler(input_method_filter_.get()); | |
262 | |
263 new wm::DefaultActivationClient(host_->window()); | |
264 | |
265 capture_client_.reset( | |
266 new aura::client::DefaultCaptureClient(host_->window())); | |
232 | 267 |
233 // Ensure new windows fill the display. | 268 // Ensure new windows fill the display. |
234 aura::WindowTreeHost* host = wm_test_helper_->host(); | 269 host_->window()->SetLayoutManager(new FillLayout); |
235 host->window()->SetLayoutManager(new FillLayout); | |
236 | 270 |
237 // Ensure the X window gets mapped. | 271 cursor_manager_.reset( |
238 host->Show(); | 272 new wm::CursorManager(scoped_ptr<wm::NativeCursorManager>( |
273 new ShellNativeCursorManager(host_.get())))); | |
274 cursor_manager_->SetDisplay( | |
275 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay()); | |
276 cursor_manager_->SetCursor(ui::kCursorPointer); | |
277 aura::client::SetCursorClient(host_->window(), cursor_manager_.get()); | |
278 | |
279 user_activity_detector_.reset(new wm::UserActivityDetector); | |
280 host_->event_processor()->GetRootTarget()->AddPreTargetHandler( | |
281 user_activity_detector_.get()); | |
282 #if defined(OS_CHROMEOS) | |
283 user_activity_notifier_.reset( | |
284 new ui::UserActivityPowerManagerNotifier(user_activity_detector_.get())); | |
285 #endif | |
239 } | 286 } |
240 | 287 |
241 void ShellDesktopController::DestroyRootWindow() { | 288 void ShellDesktopController::DestroyRootWindow() { |
242 wm_test_helper_.reset(); | 289 if (!host_) |
290 return; | |
oshima
2014/05/30 16:22:51
when are these null checks necessary?
Jun Mukai
2014/05/30 19:57:47
removed.
|host_| is NULL in ctor and this method i
| |
291 | |
292 host_->RemoveObserver(this); | |
293 if (input_method_filter_) | |
294 root_window_event_filter_->RemoveHandler(input_method_filter_.get()); | |
295 if (user_activity_detector_) { | |
296 host_->event_processor()->GetRootTarget()->RemovePreTargetHandler( | |
297 user_activity_detector_.get()); | |
298 } | |
299 root_window_event_filter_.reset(); | |
300 capture_client_.reset(); | |
301 input_method_filter_.reset(); | |
302 focus_client_.reset(); | |
303 cursor_manager_.reset(); | |
304 #if defined(OS_CHROMEOS) | |
305 user_activity_notifier_.reset(); | |
306 #endif | |
307 user_activity_detector_.reset(); | |
308 host_.reset(); | |
243 ui::ShutdownInputMethodForTesting(); | 309 ui::ShutdownInputMethodForTesting(); |
244 } | 310 } |
245 | 311 |
246 gfx::Size ShellDesktopController::GetPrimaryDisplaySize() { | 312 gfx::Size ShellDesktopController::GetPrimaryDisplaySize() { |
247 #if defined(OS_CHROMEOS) | 313 #if defined(OS_CHROMEOS) |
248 const std::vector<ui::DisplayConfigurator::DisplayState>& displays = | 314 const std::vector<ui::DisplayConfigurator::DisplayState>& displays = |
249 display_configurator_->cached_displays(); | 315 display_configurator_->cached_displays(); |
250 if (displays.empty()) | 316 if (displays.empty()) |
251 return gfx::Size(); | 317 return gfx::Size(); |
252 const ui::DisplayMode* mode = displays[0].display->current_mode(); | 318 const ui::DisplayMode* mode = displays[0].display->current_mode(); |
253 return mode ? mode->size() : gfx::Size(); | 319 return mode ? mode->size() : gfx::Size(); |
254 #else | 320 #else |
255 return gfx::Size(); | 321 return gfx::Size(); |
256 #endif | 322 #endif |
257 } | 323 } |
258 | 324 |
259 } // namespace apps | 325 } // namespace apps |
OLD | NEW |