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
3 // found in the LICENSE file.
5 #include "extensions/shell/browser/shell_desktop_controller.h"
7 #include "base/command_line.h"
8 #include "content/public/browser/context_factory.h"
9 #include "extensions/shell/browser/shell_app_window_controller.h"
10 #include "extensions/shell/common/switches.h"
11 #include "ui/aura/client/cursor_client.h"
12 #include "ui/aura/client/default_capture_client.h"
13 #include "ui/aura/env.h"
14 #include "ui/aura/layout_manager.h"
15 #include "ui/aura/test/test_screen.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_event_dispatcher.h"
18 #include "ui/aura/window_tree_host.h"
19 #include "ui/base/cursor/cursor.h"
20 #include "ui/base/cursor/image_cursors.h"
21 #include "ui/base/ime/input_method_initializer.h"
22 #include "ui/gfx/native_widget_types.h"
23 #include "ui/gfx/screen.h"
24 #include "ui/wm/core/base_focus_rules.h"
25 #include "ui/wm/core/compound_event_filter.h"
26 #include "ui/wm/core/cursor_manager.h"
27 #include "ui/wm/core/focus_controller.h"
28 #include "ui/wm/core/input_method_event_filter.h"
29 #include "ui/wm/core/native_cursor_manager.h"
30 #include "ui/wm/core/native_cursor_manager_delegate.h"
31 #include "ui/wm/core/user_activity_detector.h"
33 #if defined(OS_CHROMEOS)
34 #include "ui/chromeos/user_activity_power_manager_notifier.h"
35 #include "ui/display/types/chromeos/display_mode.h"
36 #include "ui/display/types/chromeos/display_snapshot.h"
39 namespace extensions {
42 // A simple layout manager that makes each new window fill its parent.
43 class FillLayout : public aura::LayoutManager {
46 virtual ~FillLayout() {}
49 // aura::LayoutManager:
50 virtual void OnWindowResized() OVERRIDE {}
52 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE {
56 // Create a rect at 0,0 with the size of the parent.
57 gfx::Size parent_size = child->parent()->bounds().size();
58 child->SetBounds(gfx::Rect(parent_size));
61 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {}
63 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {}
65 virtual void OnChildWindowVisibilityChanged(aura::Window* child,
66 bool visible) OVERRIDE {}
68 virtual void SetChildBounds(aura::Window* child,
69 const gfx::Rect& requested_bounds) OVERRIDE {
70 SetChildBoundsDirect(child, requested_bounds);
73 DISALLOW_COPY_AND_ASSIGN(FillLayout);
76 // A class that bridges the gap between CursorManager and Aura. It borrows
77 // heavily from AshNativeCursorManager.
78 class ShellNativeCursorManager : public wm::NativeCursorManager {
80 explicit ShellNativeCursorManager(aura::WindowTreeHost* host)
81 : host_(host), image_cursors_(new ui::ImageCursors) {}
82 virtual ~ShellNativeCursorManager() {}
84 // wm::NativeCursorManager overrides.
85 virtual void SetDisplay(const gfx::Display& display,
86 wm::NativeCursorManagerDelegate* delegate) OVERRIDE {
87 if (image_cursors_->SetDisplay(display, display.device_scale_factor()))
88 SetCursor(delegate->GetCursor(), delegate);
91 virtual void SetCursor(gfx::NativeCursor cursor,
92 wm::NativeCursorManagerDelegate* delegate) OVERRIDE {
93 image_cursors_->SetPlatformCursor(&cursor);
94 cursor.set_device_scale_factor(image_cursors_->GetScale());
95 delegate->CommitCursor(cursor);
97 if (delegate->IsCursorVisible())
101 virtual void SetVisibility(
103 wm::NativeCursorManagerDelegate* delegate) OVERRIDE {
104 delegate->CommitVisibility(visible);
107 SetCursor(delegate->GetCursor(), delegate);
109 gfx::NativeCursor invisible_cursor(ui::kCursorNone);
110 image_cursors_->SetPlatformCursor(&invisible_cursor);
111 ApplyCursor(invisible_cursor);
115 virtual void SetCursorSet(
116 ui::CursorSetType cursor_set,
117 wm::NativeCursorManagerDelegate* delegate) OVERRIDE {
118 image_cursors_->SetCursorSet(cursor_set);
119 delegate->CommitCursorSet(cursor_set);
120 if (delegate->IsCursorVisible())
121 SetCursor(delegate->GetCursor(), delegate);
124 virtual void SetMouseEventsEnabled(
126 wm::NativeCursorManagerDelegate* delegate) OVERRIDE {
127 delegate->CommitMouseEventsEnabled(enabled);
128 SetVisibility(delegate->IsCursorVisible(), delegate);
132 // Sets |cursor| as the active cursor within Aura.
133 void ApplyCursor(gfx::NativeCursor cursor) { host_->SetCursor(cursor); }
135 aura::WindowTreeHost* host_; // Not owned.
137 scoped_ptr<ui::ImageCursors> image_cursors_;
139 DISALLOW_COPY_AND_ASSIGN(ShellNativeCursorManager);
142 class AppsFocusRules : public wm::BaseFocusRules {
145 virtual ~AppsFocusRules() {}
147 virtual bool SupportsChildActivation(aura::Window* window) const OVERRIDE {
152 DISALLOW_COPY_AND_ASSIGN(AppsFocusRules);
155 ShellDesktopController* g_instance = NULL;
159 ShellDesktopController::ShellDesktopController() {
160 #if defined(OS_CHROMEOS)
161 display_configurator_.reset(new ui::DisplayConfigurator);
162 display_configurator_->Init(false);
163 display_configurator_->ForceInitialConfigure(0);
164 display_configurator_->AddObserver(this);
166 aura::Env::CreateInstance(true);
167 aura::Env::GetInstance()->set_context_factory(content::GetContextFactory());
172 ShellDesktopController::~ShellDesktopController() {
173 app_window_controller_.reset();
176 aura::Env::DeleteInstance();
180 ShellDesktopController* ShellDesktopController::instance() {
184 void ShellDesktopController::SetAppWindowController(
185 ShellAppWindowController* app_window_controller) {
186 app_window_controller_.reset(app_window_controller);
189 ShellAppWindow* ShellDesktopController::CreateAppWindow(
190 content::BrowserContext* context) {
191 return app_window_controller_->CreateAppWindow(context);
194 void ShellDesktopController::CloseAppWindows() {
195 if (app_window_controller_)
196 app_window_controller_->CloseAppWindows();
199 aura::Window* ShellDesktopController::GetDefaultParent(
200 aura::Window* context,
201 aura::Window* window,
202 const gfx::Rect& bounds) {
203 return host_->window();
206 #if defined(OS_CHROMEOS)
207 void ShellDesktopController::OnDisplayModeChanged(
208 const std::vector<ui::DisplayConfigurator::DisplayState>& displays) {
209 gfx::Size size = GetPrimaryDisplaySize();
211 host_->UpdateRootWindowSize(size);
215 void ShellDesktopController::OnHostCloseRequested(
216 const aura::WindowTreeHost* host) {
217 DCHECK_EQ(host_.get(), host);
219 base::MessageLoop::current()->PostTask(FROM_HERE,
220 base::MessageLoop::QuitClosure());
223 void ShellDesktopController::CreateRootWindow() {
224 // Set up basic pieces of ui::wm.
226 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
227 if (command_line->HasSwitch(switches::kAppShellHostWindowBounds)) {
228 const std::string size_str =
229 command_line->GetSwitchValueASCII(switches::kAppShellHostWindowBounds);
231 CHECK_EQ(2, sscanf(size_str.c_str(), "%dx%d", &width, &height));
232 size = gfx::Size(width, height);
234 size = GetPrimaryDisplaySize();
237 size = gfx::Size(1280, 720);
239 test_screen_.reset(aura::TestScreen::Create(size));
240 // TODO(jamescook): Replace this with a real Screen implementation.
241 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen_.get());
242 // TODO(mukai): Set up input method.
244 host_.reset(test_screen_->CreateHostForPrimaryDisplay());
246 aura::client::SetWindowTreeClient(host_->window(), this);
247 root_window_event_filter_.reset(new wm::CompoundEventFilter);
248 host_->window()->AddPreTargetHandler(root_window_event_filter_.get());
251 host_->AddObserver(this);
253 // Ensure the X window gets mapped.
257 void ShellDesktopController::InitWindowManager() {
258 wm::FocusController* focus_controller =
259 new wm::FocusController(CreateFocusRules());
260 aura::client::SetFocusClient(host_->window(), focus_controller);
261 host_->window()->AddPreTargetHandler(focus_controller);
262 aura::client::SetActivationClient(host_->window(), focus_controller);
263 focus_client_.reset(focus_controller);
265 input_method_filter_.reset(
266 new wm::InputMethodEventFilter(host_->GetAcceleratedWidget()));
267 input_method_filter_->SetInputMethodPropertyInRootWindow(host_->window());
268 root_window_event_filter_->AddHandler(input_method_filter_.get());
270 capture_client_.reset(
271 new aura::client::DefaultCaptureClient(host_->window()));
273 // Ensure new windows fill the display.
274 host_->window()->SetLayoutManager(new FillLayout);
276 cursor_manager_.reset(
277 new wm::CursorManager(scoped_ptr<wm::NativeCursorManager>(
278 new ShellNativeCursorManager(host_.get()))));
279 cursor_manager_->SetDisplay(
280 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay());
281 cursor_manager_->SetCursor(ui::kCursorPointer);
282 aura::client::SetCursorClient(host_->window(), cursor_manager_.get());
284 user_activity_detector_.reset(new wm::UserActivityDetector);
285 host_->event_processor()->GetRootTarget()->AddPreTargetHandler(
286 user_activity_detector_.get());
287 #if defined(OS_CHROMEOS)
288 user_activity_notifier_.reset(
289 new ui::UserActivityPowerManagerNotifier(user_activity_detector_.get()));
293 wm::FocusRules* ShellDesktopController::CreateFocusRules() {
294 return new AppsFocusRules();
297 void ShellDesktopController::DestroyRootWindow() {
298 host_->RemoveObserver(this);
299 if (input_method_filter_)
300 root_window_event_filter_->RemoveHandler(input_method_filter_.get());
301 if (user_activity_detector_) {
302 host_->event_processor()->GetRootTarget()->RemovePreTargetHandler(
303 user_activity_detector_.get());
305 wm::FocusController* focus_controller =
306 static_cast<wm::FocusController*>(focus_client_.get());
307 if (focus_controller) {
308 host_->window()->RemovePreTargetHandler(focus_controller);
309 aura::client::SetActivationClient(host_->window(), NULL);
311 root_window_event_filter_.reset();
312 capture_client_.reset();
313 input_method_filter_.reset();
314 focus_client_.reset();
315 cursor_manager_.reset();
316 #if defined(OS_CHROMEOS)
317 user_activity_notifier_.reset();
319 user_activity_detector_.reset();
323 gfx::Size ShellDesktopController::GetPrimaryDisplaySize() {
324 #if defined(OS_CHROMEOS)
325 const std::vector<ui::DisplayConfigurator::DisplayState>& displays =
326 display_configurator_->cached_displays();
327 if (displays.empty())
329 const ui::DisplayMode* mode = displays[0].display->current_mode();
330 return mode ? mode->size() : gfx::Size();
336 } // namespace extensions