Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / extensions / shell / browser / shell_desktop_controller_aura.cc
blobf89714a391996a1f3280c26bb3d66b80dea6d08a
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"
7 #include <algorithm>
8 #include <string>
9 #include <vector>
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/native_cursor_manager.h"
36 #include "ui/wm/core/native_cursor_manager_delegate.h"
38 #if defined(OS_CHROMEOS)
39 #include "chromeos/dbus/dbus_thread_manager.h"
40 #include "ui/chromeos/user_activity_power_manager_notifier.h"
41 #include "ui/display/types/display_mode.h"
42 #include "ui/display/types/display_snapshot.h"
43 #endif
45 namespace extensions {
46 namespace {
48 // A simple layout manager that makes each new window fill its parent.
49 class FillLayout : public aura::LayoutManager {
50 public:
51 FillLayout() {}
52 ~FillLayout() override {}
54 private:
55 // aura::LayoutManager:
56 void OnWindowResized() override {}
58 void OnWindowAddedToLayout(aura::Window* child) override {
59 if (!child->parent())
60 return;
62 // Create a rect at 0,0 with the size of the parent.
63 gfx::Size parent_size = child->parent()->bounds().size();
64 child->SetBounds(gfx::Rect(parent_size));
67 void OnWillRemoveWindowFromLayout(aura::Window* child) override {}
69 void OnWindowRemovedFromLayout(aura::Window* child) override {}
71 void OnChildWindowVisibilityChanged(aura::Window* child,
72 bool visible) override {}
74 void SetChildBounds(aura::Window* child,
75 const gfx::Rect& requested_bounds) override {
76 SetChildBoundsDirect(child, requested_bounds);
79 DISALLOW_COPY_AND_ASSIGN(FillLayout);
82 // A class that bridges the gap between CursorManager and Aura. It borrows
83 // heavily from AshNativeCursorManager.
84 class ShellNativeCursorManager : public wm::NativeCursorManager {
85 public:
86 explicit ShellNativeCursorManager(aura::WindowTreeHost* host)
87 : host_(host), image_cursors_(new ui::ImageCursors) {}
88 ~ShellNativeCursorManager() override {}
90 // wm::NativeCursorManager overrides.
91 void SetDisplay(const gfx::Display& display,
92 wm::NativeCursorManagerDelegate* delegate) override {
93 if (image_cursors_->SetDisplay(display, display.device_scale_factor()))
94 SetCursor(delegate->GetCursor(), delegate);
97 void SetCursor(gfx::NativeCursor cursor,
98 wm::NativeCursorManagerDelegate* delegate) override {
99 image_cursors_->SetPlatformCursor(&cursor);
100 cursor.set_device_scale_factor(image_cursors_->GetScale());
101 delegate->CommitCursor(cursor);
103 if (delegate->IsCursorVisible())
104 ApplyCursor(cursor);
107 void SetVisibility(bool visible,
108 wm::NativeCursorManagerDelegate* delegate) override {
109 delegate->CommitVisibility(visible);
111 if (visible) {
112 SetCursor(delegate->GetCursor(), delegate);
113 } else {
114 gfx::NativeCursor invisible_cursor(ui::kCursorNone);
115 image_cursors_->SetPlatformCursor(&invisible_cursor);
116 ApplyCursor(invisible_cursor);
120 void SetCursorSet(ui::CursorSetType cursor_set,
121 wm::NativeCursorManagerDelegate* delegate) override {
122 image_cursors_->SetCursorSet(cursor_set);
123 delegate->CommitCursorSet(cursor_set);
124 if (delegate->IsCursorVisible())
125 SetCursor(delegate->GetCursor(), delegate);
128 void SetMouseEventsEnabled(
129 bool enabled,
130 wm::NativeCursorManagerDelegate* delegate) override {
131 delegate->CommitMouseEventsEnabled(enabled);
132 SetVisibility(delegate->IsCursorVisible(), delegate);
135 private:
136 // Sets |cursor| as the active cursor within Aura.
137 void ApplyCursor(gfx::NativeCursor cursor) { host_->SetCursor(cursor); }
139 aura::WindowTreeHost* host_; // Not owned.
141 scoped_ptr<ui::ImageCursors> image_cursors_;
143 DISALLOW_COPY_AND_ASSIGN(ShellNativeCursorManager);
146 class AppsFocusRules : public wm::BaseFocusRules {
147 public:
148 AppsFocusRules() {}
149 ~AppsFocusRules() override {}
151 bool SupportsChildActivation(aura::Window* window) const override {
152 return true;
155 private:
156 DISALLOW_COPY_AND_ASSIGN(AppsFocusRules);
159 } // namespace
161 ShellDesktopControllerAura::ShellDesktopControllerAura()
162 : app_window_client_(new ShellAppWindowClient) {
163 extensions::AppWindowClient::Set(app_window_client_.get());
165 #if defined(OS_CHROMEOS)
166 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(
167 this);
168 display_configurator_.reset(new ui::DisplayConfigurator);
169 display_configurator_->Init(false);
170 display_configurator_->ForceInitialConfigure(0);
171 display_configurator_->AddObserver(this);
172 #endif
173 CreateRootWindow();
176 ShellDesktopControllerAura::~ShellDesktopControllerAura() {
177 CloseAppWindows();
178 DestroyRootWindow();
179 #if defined(OS_CHROMEOS)
180 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(
181 this);
182 #endif
183 extensions::AppWindowClient::Set(NULL);
186 gfx::Size ShellDesktopControllerAura::GetWindowSize() {
187 return host_->window()->bounds().size();
190 AppWindow* ShellDesktopControllerAura::CreateAppWindow(
191 content::BrowserContext* context,
192 const Extension* extension) {
193 app_windows_.push_back(
194 new AppWindow(context, new ShellAppDelegate, extension));
195 return app_windows_.back();
198 void ShellDesktopControllerAura::AddAppWindow(gfx::NativeWindow window) {
199 aura::Window* root_window = host_->window();
200 root_window->AddChild(window);
203 void ShellDesktopControllerAura::RemoveAppWindow(AppWindow* window) {
204 auto iter = std::find(app_windows_.begin(), app_windows_.end(), window);
205 DCHECK(iter != app_windows_.end());
206 app_windows_.erase(iter);
209 void ShellDesktopControllerAura::CloseAppWindows() {
210 // Create a copy of the window vector, because closing the windows will
211 // trigger RemoveAppWindow, which will invalidate the iterator.
212 // This vector should be small enough that this should not be an issue.
213 std::vector<AppWindow*> app_windows(app_windows_);
214 for (AppWindow* app_window : app_windows)
215 app_window->GetBaseWindow()->Close(); // Close() deletes |app_window|.
216 app_windows_.clear();
219 aura::Window* ShellDesktopControllerAura::GetDefaultParent(
220 aura::Window* context,
221 aura::Window* window,
222 const gfx::Rect& bounds) {
223 return host_->window();
226 #if defined(OS_CHROMEOS)
227 void ShellDesktopControllerAura::PowerButtonEventReceived(
228 bool down,
229 const base::TimeTicks& timestamp) {
230 if (down) {
231 chromeos::DBusThreadManager::Get()
232 ->GetPowerManagerClient()
233 ->RequestShutdown();
237 void ShellDesktopControllerAura::OnDisplayModeChanged(
238 const ui::DisplayConfigurator::DisplayStateList& displays) {
239 gfx::Size size = GetPrimaryDisplaySize();
240 if (!size.IsEmpty())
241 host_->UpdateRootWindowSize(size);
243 #endif
245 void ShellDesktopControllerAura::OnHostCloseRequested(
246 const aura::WindowTreeHost* host) {
247 DCHECK_EQ(host_.get(), host);
248 CloseAppWindows();
249 base::MessageLoop::current()->PostTask(FROM_HERE,
250 base::MessageLoop::QuitClosure());
253 void ShellDesktopControllerAura::InitWindowManager() {
254 wm::FocusController* focus_controller =
255 new wm::FocusController(new AppsFocusRules());
256 aura::client::SetFocusClient(host_->window(), focus_controller);
257 host_->window()->AddPreTargetHandler(focus_controller);
258 aura::client::SetActivationClient(host_->window(), focus_controller);
259 focus_client_.reset(focus_controller);
261 capture_client_.reset(
262 new aura::client::DefaultCaptureClient(host_->window()));
264 // Ensure new windows fill the display.
265 host_->window()->SetLayoutManager(new FillLayout);
267 cursor_manager_.reset(
268 new wm::CursorManager(scoped_ptr<wm::NativeCursorManager>(
269 new ShellNativeCursorManager(host_.get()))));
270 cursor_manager_->SetDisplay(
271 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay());
272 cursor_manager_->SetCursor(ui::kCursorPointer);
273 aura::client::SetCursorClient(host_->window(), cursor_manager_.get());
275 user_activity_detector_.reset(new ui::UserActivityDetector);
276 #if defined(OS_CHROMEOS)
277 user_activity_notifier_.reset(
278 new ui::UserActivityPowerManagerNotifier(user_activity_detector_.get()));
279 #endif
282 void ShellDesktopControllerAura::CreateRootWindow() {
283 // Set up basic pieces of ui::wm.
284 gfx::Size size;
285 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
286 if (command_line->HasSwitch(switches::kAppShellHostWindowSize)) {
287 const std::string size_str =
288 command_line->GetSwitchValueASCII(switches::kAppShellHostWindowSize);
289 int width, height;
290 CHECK_EQ(2, sscanf(size_str.c_str(), "%dx%d", &width, &height));
291 size = gfx::Size(width, height);
292 } else {
293 size = GetPrimaryDisplaySize();
295 if (size.IsEmpty())
296 size = gfx::Size(1920, 1080);
298 screen_.reset(new ShellScreen(size));
299 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
300 // TODO(mukai): Set up input method.
302 host_.reset(screen_->CreateHostForPrimaryDisplay());
303 aura::client::SetWindowTreeClient(host_->window(), this);
304 root_window_event_filter_.reset(new wm::CompoundEventFilter);
305 host_->window()->AddPreTargetHandler(root_window_event_filter_.get());
306 InitWindowManager();
308 host_->AddObserver(this);
310 // Ensure the X window gets mapped.
311 host_->Show();
314 void ShellDesktopControllerAura::DestroyRootWindow() {
315 host_->RemoveObserver(this);
316 wm::FocusController* focus_controller =
317 static_cast<wm::FocusController*>(focus_client_.get());
318 if (focus_controller) {
319 host_->window()->RemovePreTargetHandler(focus_controller);
320 aura::client::SetActivationClient(host_->window(), NULL);
322 root_window_event_filter_.reset();
323 capture_client_.reset();
324 focus_client_.reset();
325 cursor_manager_.reset();
326 #if defined(OS_CHROMEOS)
327 user_activity_notifier_.reset();
328 #endif
329 user_activity_detector_.reset();
330 host_.reset();
331 screen_.reset();
334 gfx::Size ShellDesktopControllerAura::GetPrimaryDisplaySize() {
335 #if defined(OS_CHROMEOS)
336 const ui::DisplayConfigurator::DisplayStateList& displays =
337 display_configurator_->cached_displays();
338 if (displays.empty())
339 return gfx::Size();
340 const ui::DisplayMode* mode = displays[0]->current_mode();
341 return mode ? mode->size() : gfx::Size();
342 #else
343 return gfx::Size();
344 #endif
347 } // namespace extensions