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_aura.h"
11 #include "base/command_line.h"
12 #include "extensions/browser/app_window/app_window.h"
13 #include "extensions/browser/app_window/native_app_window.h"
14 #include "extensions/shell/browser/shell_app_delegate.h"
15 #include "extensions/shell/browser/shell_app_window_client.h"
16 #include "extensions/shell/browser/shell_screen.h"
17 #include "extensions/shell/common/switches.h"
18 #include "ui/aura/client/cursor_client.h"
19 #include "ui/aura/client/default_capture_client.h"
20 #include "ui/aura/layout_manager.h"
21 #include "ui/aura/window.h"
22 #include "ui/aura/window_event_dispatcher.h"
23 #include "ui/aura/window_tree_host.h"
24 #include "ui/base/cursor/cursor.h"
25 #include "ui/base/cursor/image_cursors.h"
26 #include "ui/base/ime/input_method_initializer.h"
27 #include "ui/base/user_activity/user_activity_detector.h"
28 #include "ui/gfx/geometry/size.h"
29 #include "ui/gfx/native_widget_types.h"
30 #include "ui/gfx/screen.h"
31 #include "ui/wm/core/base_focus_rules.h"
32 #include "ui/wm/core/compound_event_filter.h"
33 #include "ui/wm/core/cursor_manager.h"
34 #include "ui/wm/core/focus_controller.h"
35 #include "ui/wm/core/input_method_event_filter.h"
36 #include "ui/wm/core/native_cursor_manager.h"
37 #include "ui/wm/core/native_cursor_manager_delegate.h"
39 #if defined(OS_CHROMEOS)
40 #include "chromeos/dbus/dbus_thread_manager.h"
41 #include "ui/chromeos/user_activity_power_manager_notifier.h"
42 #include "ui/display/types/display_mode.h"
43 #include "ui/display/types/display_snapshot.h"
46 namespace extensions
{
49 // A simple layout manager that makes each new window fill its parent.
50 class FillLayout
: public aura::LayoutManager
{
53 ~FillLayout() override
{}
56 // aura::LayoutManager:
57 void OnWindowResized() override
{}
59 void OnWindowAddedToLayout(aura::Window
* child
) override
{
63 // Create a rect at 0,0 with the size of the parent.
64 gfx::Size parent_size
= child
->parent()->bounds().size();
65 child
->SetBounds(gfx::Rect(parent_size
));
68 void OnWillRemoveWindowFromLayout(aura::Window
* child
) override
{}
70 void OnWindowRemovedFromLayout(aura::Window
* child
) override
{}
72 void OnChildWindowVisibilityChanged(aura::Window
* child
,
73 bool visible
) override
{}
75 void SetChildBounds(aura::Window
* child
,
76 const gfx::Rect
& requested_bounds
) override
{
77 SetChildBoundsDirect(child
, requested_bounds
);
80 DISALLOW_COPY_AND_ASSIGN(FillLayout
);
83 // A class that bridges the gap between CursorManager and Aura. It borrows
84 // heavily from AshNativeCursorManager.
85 class ShellNativeCursorManager
: public wm::NativeCursorManager
{
87 explicit ShellNativeCursorManager(aura::WindowTreeHost
* host
)
88 : host_(host
), image_cursors_(new ui::ImageCursors
) {}
89 ~ShellNativeCursorManager() override
{}
91 // wm::NativeCursorManager overrides.
92 void SetDisplay(const gfx::Display
& display
,
93 wm::NativeCursorManagerDelegate
* delegate
) override
{
94 if (image_cursors_
->SetDisplay(display
, display
.device_scale_factor()))
95 SetCursor(delegate
->GetCursor(), delegate
);
98 void SetCursor(gfx::NativeCursor cursor
,
99 wm::NativeCursorManagerDelegate
* delegate
) override
{
100 image_cursors_
->SetPlatformCursor(&cursor
);
101 cursor
.set_device_scale_factor(image_cursors_
->GetScale());
102 delegate
->CommitCursor(cursor
);
104 if (delegate
->IsCursorVisible())
108 void SetVisibility(bool visible
,
109 wm::NativeCursorManagerDelegate
* delegate
) override
{
110 delegate
->CommitVisibility(visible
);
113 SetCursor(delegate
->GetCursor(), delegate
);
115 gfx::NativeCursor
invisible_cursor(ui::kCursorNone
);
116 image_cursors_
->SetPlatformCursor(&invisible_cursor
);
117 ApplyCursor(invisible_cursor
);
121 void SetCursorSet(ui::CursorSetType cursor_set
,
122 wm::NativeCursorManagerDelegate
* delegate
) override
{
123 image_cursors_
->SetCursorSet(cursor_set
);
124 delegate
->CommitCursorSet(cursor_set
);
125 if (delegate
->IsCursorVisible())
126 SetCursor(delegate
->GetCursor(), delegate
);
129 void SetMouseEventsEnabled(
131 wm::NativeCursorManagerDelegate
* delegate
) override
{
132 delegate
->CommitMouseEventsEnabled(enabled
);
133 SetVisibility(delegate
->IsCursorVisible(), delegate
);
137 // Sets |cursor| as the active cursor within Aura.
138 void ApplyCursor(gfx::NativeCursor cursor
) { host_
->SetCursor(cursor
); }
140 aura::WindowTreeHost
* host_
; // Not owned.
142 scoped_ptr
<ui::ImageCursors
> image_cursors_
;
144 DISALLOW_COPY_AND_ASSIGN(ShellNativeCursorManager
);
147 class AppsFocusRules
: public wm::BaseFocusRules
{
150 ~AppsFocusRules() override
{}
152 bool SupportsChildActivation(aura::Window
* window
) const override
{
157 DISALLOW_COPY_AND_ASSIGN(AppsFocusRules
);
162 ShellDesktopControllerAura::ShellDesktopControllerAura()
163 : app_window_client_(new ShellAppWindowClient
) {
164 extensions::AppWindowClient::Set(app_window_client_
.get());
166 #if defined(OS_CHROMEOS)
167 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(
169 display_configurator_
.reset(new ui::DisplayConfigurator
);
170 display_configurator_
->Init(false);
171 display_configurator_
->ForceInitialConfigure(0);
172 display_configurator_
->AddObserver(this);
177 ShellDesktopControllerAura::~ShellDesktopControllerAura() {
180 #if defined(OS_CHROMEOS)
181 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(
184 extensions::AppWindowClient::Set(NULL
);
187 gfx::Size
ShellDesktopControllerAura::GetWindowSize() {
188 return host_
->window()->bounds().size();
191 AppWindow
* ShellDesktopControllerAura::CreateAppWindow(
192 content::BrowserContext
* context
,
193 const Extension
* extension
) {
194 app_windows_
.push_back(
195 new AppWindow(context
, new ShellAppDelegate
, extension
));
196 return app_windows_
.back();
199 void ShellDesktopControllerAura::AddAppWindow(gfx::NativeWindow window
) {
200 aura::Window
* root_window
= host_
->window();
201 root_window
->AddChild(window
);
204 void ShellDesktopControllerAura::RemoveAppWindow(AppWindow
* window
) {
205 auto iter
= std::find(app_windows_
.begin(), app_windows_
.end(), window
);
206 DCHECK(iter
!= app_windows_
.end());
207 app_windows_
.erase(iter
);
210 void ShellDesktopControllerAura::CloseAppWindows() {
211 // Create a copy of the window vector, because closing the windows will
212 // trigger RemoveAppWindow, which will invalidate the iterator.
213 // This vector should be small enough that this should not be an issue.
214 std::vector
<AppWindow
*> app_windows(app_windows_
);
215 for (AppWindow
* app_window
: app_windows
)
216 app_window
->GetBaseWindow()->Close(); // Close() deletes |app_window|.
217 app_windows_
.clear();
220 aura::Window
* ShellDesktopControllerAura::GetDefaultParent(
221 aura::Window
* context
,
222 aura::Window
* window
,
223 const gfx::Rect
& bounds
) {
224 return host_
->window();
227 #if defined(OS_CHROMEOS)
228 void ShellDesktopControllerAura::PowerButtonEventReceived(
230 const base::TimeTicks
& timestamp
) {
232 chromeos::DBusThreadManager::Get()
233 ->GetPowerManagerClient()
238 void ShellDesktopControllerAura::OnDisplayModeChanged(
239 const ui::DisplayConfigurator::DisplayStateList
& displays
) {
240 gfx::Size size
= GetPrimaryDisplaySize();
242 host_
->UpdateRootWindowSize(size
);
246 void ShellDesktopControllerAura::OnHostCloseRequested(
247 const aura::WindowTreeHost
* host
) {
248 DCHECK_EQ(host_
.get(), host
);
250 base::MessageLoop::current()->PostTask(FROM_HERE
,
251 base::MessageLoop::QuitClosure());
254 void ShellDesktopControllerAura::InitWindowManager() {
255 wm::FocusController
* focus_controller
=
256 new wm::FocusController(new AppsFocusRules());
257 aura::client::SetFocusClient(host_
->window(), focus_controller
);
258 host_
->window()->AddPreTargetHandler(focus_controller
);
259 aura::client::SetActivationClient(host_
->window(), focus_controller
);
260 focus_client_
.reset(focus_controller
);
262 input_method_filter_
.reset(
263 new wm::InputMethodEventFilter(host_
->GetAcceleratedWidget()));
264 input_method_filter_
->SetInputMethodPropertyInRootWindow(host_
->window());
265 root_window_event_filter_
->AddHandler(input_method_filter_
.get());
267 capture_client_
.reset(
268 new aura::client::DefaultCaptureClient(host_
->window()));
270 // Ensure new windows fill the display.
271 host_
->window()->SetLayoutManager(new FillLayout
);
273 cursor_manager_
.reset(
274 new wm::CursorManager(scoped_ptr
<wm::NativeCursorManager
>(
275 new ShellNativeCursorManager(host_
.get()))));
276 cursor_manager_
->SetDisplay(
277 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay());
278 cursor_manager_
->SetCursor(ui::kCursorPointer
);
279 aura::client::SetCursorClient(host_
->window(), cursor_manager_
.get());
281 user_activity_detector_
.reset(new ui::UserActivityDetector
);
282 host_
->event_processor()->GetRootTarget()->AddPreTargetHandler(
283 user_activity_detector_
.get());
284 #if defined(OS_CHROMEOS)
285 user_activity_notifier_
.reset(
286 new ui::UserActivityPowerManagerNotifier(user_activity_detector_
.get()));
290 void ShellDesktopControllerAura::CreateRootWindow() {
291 // Set up basic pieces of ui::wm.
293 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
294 if (command_line
->HasSwitch(switches::kAppShellHostWindowSize
)) {
295 const std::string size_str
=
296 command_line
->GetSwitchValueASCII(switches::kAppShellHostWindowSize
);
298 CHECK_EQ(2, sscanf(size_str
.c_str(), "%dx%d", &width
, &height
));
299 size
= gfx::Size(width
, height
);
301 size
= GetPrimaryDisplaySize();
304 size
= gfx::Size(1920, 1080);
306 screen_
.reset(new ShellScreen(size
));
307 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE
, screen_
.get());
308 // TODO(mukai): Set up input method.
310 host_
.reset(screen_
->CreateHostForPrimaryDisplay());
312 aura::client::SetWindowTreeClient(host_
->window(), this);
313 root_window_event_filter_
.reset(new wm::CompoundEventFilter
);
314 host_
->window()->AddPreTargetHandler(root_window_event_filter_
.get());
317 host_
->AddObserver(this);
319 // Ensure the X window gets mapped.
323 void ShellDesktopControllerAura::DestroyRootWindow() {
324 host_
->RemoveObserver(this);
325 if (input_method_filter_
)
326 root_window_event_filter_
->RemoveHandler(input_method_filter_
.get());
327 if (user_activity_detector_
) {
328 host_
->event_processor()->GetRootTarget()->RemovePreTargetHandler(
329 user_activity_detector_
.get());
331 wm::FocusController
* focus_controller
=
332 static_cast<wm::FocusController
*>(focus_client_
.get());
333 if (focus_controller
) {
334 host_
->window()->RemovePreTargetHandler(focus_controller
);
335 aura::client::SetActivationClient(host_
->window(), NULL
);
337 root_window_event_filter_
.reset();
338 capture_client_
.reset();
339 input_method_filter_
.reset();
340 focus_client_
.reset();
341 cursor_manager_
.reset();
342 #if defined(OS_CHROMEOS)
343 user_activity_notifier_
.reset();
345 user_activity_detector_
.reset();
350 gfx::Size
ShellDesktopControllerAura::GetPrimaryDisplaySize() {
351 #if defined(OS_CHROMEOS)
352 const ui::DisplayConfigurator::DisplayStateList
& displays
=
353 display_configurator_
->cached_displays();
354 if (displays
.empty())
356 const ui::DisplayMode
* mode
= displays
[0]->current_mode();
357 return mode
? mode
->size() : gfx::Size();
363 } // namespace extensions