| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "extensions/shell/browser/shell_desktop_controller.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 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/common/switches.h" | |
| 17 #include "ui/aura/client/cursor_client.h" | |
| 18 #include "ui/aura/client/default_capture_client.h" | |
| 19 #include "ui/aura/layout_manager.h" | |
| 20 #include "ui/aura/test/test_screen.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/gfx/native_widget_types.h" | |
| 28 #include "ui/gfx/screen.h" | |
| 29 #include "ui/wm/core/base_focus_rules.h" | |
| 30 #include "ui/wm/core/compound_event_filter.h" | |
| 31 #include "ui/wm/core/cursor_manager.h" | |
| 32 #include "ui/wm/core/focus_controller.h" | |
| 33 #include "ui/wm/core/input_method_event_filter.h" | |
| 34 #include "ui/wm/core/native_cursor_manager.h" | |
| 35 #include "ui/wm/core/native_cursor_manager_delegate.h" | |
| 36 #include "ui/wm/core/user_activity_detector.h" | |
| 37 | |
| 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 | |
| 44 | |
| 45 namespace extensions { | |
| 46 namespace { | |
| 47 | |
| 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 {} | |
| 53 | |
| 54 private: | |
| 55 // aura::LayoutManager: | |
| 56 void OnWindowResized() override {} | |
| 57 | |
| 58 void OnWindowAddedToLayout(aura::Window* child) override { | |
| 59 if (!child->parent()) | |
| 60 return; | |
| 61 | |
| 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)); | |
| 65 } | |
| 66 | |
| 67 void OnWillRemoveWindowFromLayout(aura::Window* child) override {} | |
| 68 | |
| 69 void OnWindowRemovedFromLayout(aura::Window* child) override {} | |
| 70 | |
| 71 void OnChildWindowVisibilityChanged(aura::Window* child, | |
| 72 bool visible) override {} | |
| 73 | |
| 74 void SetChildBounds(aura::Window* child, | |
| 75 const gfx::Rect& requested_bounds) override { | |
| 76 SetChildBoundsDirect(child, requested_bounds); | |
| 77 } | |
| 78 | |
| 79 DISALLOW_COPY_AND_ASSIGN(FillLayout); | |
| 80 }; | |
| 81 | |
| 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 {} | |
| 89 | |
| 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); | |
| 95 } | |
| 96 | |
| 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); | |
| 102 | |
| 103 if (delegate->IsCursorVisible()) | |
| 104 ApplyCursor(cursor); | |
| 105 } | |
| 106 | |
| 107 void SetVisibility(bool visible, | |
| 108 wm::NativeCursorManagerDelegate* delegate) override { | |
| 109 delegate->CommitVisibility(visible); | |
| 110 | |
| 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); | |
| 117 } | |
| 118 } | |
| 119 | |
| 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); | |
| 126 } | |
| 127 | |
| 128 void SetMouseEventsEnabled( | |
| 129 bool enabled, | |
| 130 wm::NativeCursorManagerDelegate* delegate) override { | |
| 131 delegate->CommitMouseEventsEnabled(enabled); | |
| 132 SetVisibility(delegate->IsCursorVisible(), delegate); | |
| 133 } | |
| 134 | |
| 135 private: | |
| 136 // Sets |cursor| as the active cursor within Aura. | |
| 137 void ApplyCursor(gfx::NativeCursor cursor) { host_->SetCursor(cursor); } | |
| 138 | |
| 139 aura::WindowTreeHost* host_; // Not owned. | |
| 140 | |
| 141 scoped_ptr<ui::ImageCursors> image_cursors_; | |
| 142 | |
| 143 DISALLOW_COPY_AND_ASSIGN(ShellNativeCursorManager); | |
| 144 }; | |
| 145 | |
| 146 class AppsFocusRules : public wm::BaseFocusRules { | |
| 147 public: | |
| 148 AppsFocusRules() {} | |
| 149 ~AppsFocusRules() override {} | |
| 150 | |
| 151 bool SupportsChildActivation(aura::Window* window) const override { | |
| 152 return true; | |
| 153 } | |
| 154 | |
| 155 private: | |
| 156 DISALLOW_COPY_AND_ASSIGN(AppsFocusRules); | |
| 157 }; | |
| 158 | |
| 159 } // namespace | |
| 160 | |
| 161 ShellDesktopController::ShellDesktopController() | |
| 162 : app_window_client_(new ShellAppWindowClient) { | |
| 163 extensions::AppWindowClient::Set(app_window_client_.get()); | |
| 164 | |
| 165 #if defined(OS_CHROMEOS) | |
| 166 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> | |
| 167 AddObserver(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(); | |
| 174 } | |
| 175 | |
| 176 ShellDesktopController::~ShellDesktopController() { | |
| 177 CloseAppWindows(); | |
| 178 DestroyRootWindow(); | |
| 179 #if defined(OS_CHROMEOS) | |
| 180 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> | |
| 181 RemoveObserver(this); | |
| 182 #endif | |
| 183 extensions::AppWindowClient::Set(NULL); | |
| 184 } | |
| 185 | |
| 186 aura::WindowTreeHost* ShellDesktopController::GetHost() { | |
| 187 return host_.get(); | |
| 188 } | |
| 189 | |
| 190 AppWindow* ShellDesktopController::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(); | |
| 196 } | |
| 197 | |
| 198 void ShellDesktopController::AddAppWindow(aura::Window* window) { | |
| 199 aura::Window* root_window = GetHost()->window(); | |
| 200 root_window->AddChild(window); | |
| 201 } | |
| 202 | |
| 203 void ShellDesktopController::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); | |
| 207 } | |
| 208 | |
| 209 void ShellDesktopController::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(); | |
| 217 } | |
| 218 | |
| 219 aura::Window* ShellDesktopController::GetDefaultParent( | |
| 220 aura::Window* context, | |
| 221 aura::Window* window, | |
| 222 const gfx::Rect& bounds) { | |
| 223 return host_->window(); | |
| 224 } | |
| 225 | |
| 226 #if defined(OS_CHROMEOS) | |
| 227 void ShellDesktopController::PowerButtonEventReceived( | |
| 228 bool down, | |
| 229 const base::TimeTicks& timestamp) { | |
| 230 if (down) { | |
| 231 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> | |
| 232 RequestShutdown(); | |
| 233 } | |
| 234 } | |
| 235 | |
| 236 void ShellDesktopController::OnDisplayModeChanged( | |
| 237 const std::vector<ui::DisplayConfigurator::DisplayState>& displays) { | |
| 238 gfx::Size size = GetPrimaryDisplaySize(); | |
| 239 if (!size.IsEmpty()) | |
| 240 host_->UpdateRootWindowSize(size); | |
| 241 } | |
| 242 #endif | |
| 243 | |
| 244 void ShellDesktopController::OnHostCloseRequested( | |
| 245 const aura::WindowTreeHost* host) { | |
| 246 DCHECK_EQ(host_.get(), host); | |
| 247 CloseAppWindows(); | |
| 248 base::MessageLoop::current()->PostTask(FROM_HERE, | |
| 249 base::MessageLoop::QuitClosure()); | |
| 250 } | |
| 251 | |
| 252 void ShellDesktopController::InitWindowManager() { | |
| 253 wm::FocusController* focus_controller = | |
| 254 new wm::FocusController(new AppsFocusRules()); | |
| 255 aura::client::SetFocusClient(host_->window(), focus_controller); | |
| 256 host_->window()->AddPreTargetHandler(focus_controller); | |
| 257 aura::client::SetActivationClient(host_->window(), focus_controller); | |
| 258 focus_client_.reset(focus_controller); | |
| 259 | |
| 260 input_method_filter_.reset( | |
| 261 new wm::InputMethodEventFilter(host_->GetAcceleratedWidget())); | |
| 262 input_method_filter_->SetInputMethodPropertyInRootWindow(host_->window()); | |
| 263 root_window_event_filter_->AddHandler(input_method_filter_.get()); | |
| 264 | |
| 265 capture_client_.reset( | |
| 266 new aura::client::DefaultCaptureClient(host_->window())); | |
| 267 | |
| 268 // Ensure new windows fill the display. | |
| 269 host_->window()->SetLayoutManager(new FillLayout); | |
| 270 | |
| 271 cursor_manager_.reset( | |
| 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 | |
| 286 } | |
| 287 | |
| 288 void ShellDesktopController::CreateRootWindow() { | |
| 289 // Set up basic pieces of ui::wm. | |
| 290 gfx::Size size; | |
| 291 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
| 292 if (command_line->HasSwitch(switches::kAppShellHostWindowBounds)) { | |
| 293 const std::string size_str = | |
| 294 command_line->GetSwitchValueASCII(switches::kAppShellHostWindowBounds); | |
| 295 int width, height; | |
| 296 CHECK_EQ(2, sscanf(size_str.c_str(), "%dx%d", &width, &height)); | |
| 297 size = gfx::Size(width, height); | |
| 298 } else { | |
| 299 size = GetPrimaryDisplaySize(); | |
| 300 } | |
| 301 if (size.IsEmpty()) | |
| 302 size = gfx::Size(1280, 720); | |
| 303 | |
| 304 test_screen_.reset(aura::TestScreen::Create(size)); | |
| 305 // TODO(jamescook): Replace this with a real Screen implementation. | |
| 306 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen_.get()); | |
| 307 // TODO(mukai): Set up input method. | |
| 308 | |
| 309 host_.reset(test_screen_->CreateHostForPrimaryDisplay()); | |
| 310 host_->InitHost(); | |
| 311 aura::client::SetWindowTreeClient(host_->window(), this); | |
| 312 root_window_event_filter_.reset(new wm::CompoundEventFilter); | |
| 313 host_->window()->AddPreTargetHandler(root_window_event_filter_.get()); | |
| 314 InitWindowManager(); | |
| 315 | |
| 316 host_->AddObserver(this); | |
| 317 | |
| 318 // Ensure the X window gets mapped. | |
| 319 host_->Show(); | |
| 320 } | |
| 321 | |
| 322 void ShellDesktopController::DestroyRootWindow() { | |
| 323 host_->RemoveObserver(this); | |
| 324 if (input_method_filter_) | |
| 325 root_window_event_filter_->RemoveHandler(input_method_filter_.get()); | |
| 326 if (user_activity_detector_) { | |
| 327 host_->event_processor()->GetRootTarget()->RemovePreTargetHandler( | |
| 328 user_activity_detector_.get()); | |
| 329 } | |
| 330 wm::FocusController* focus_controller = | |
| 331 static_cast<wm::FocusController*>(focus_client_.get()); | |
| 332 if (focus_controller) { | |
| 333 host_->window()->RemovePreTargetHandler(focus_controller); | |
| 334 aura::client::SetActivationClient(host_->window(), NULL); | |
| 335 } | |
| 336 root_window_event_filter_.reset(); | |
| 337 capture_client_.reset(); | |
| 338 input_method_filter_.reset(); | |
| 339 focus_client_.reset(); | |
| 340 cursor_manager_.reset(); | |
| 341 #if defined(OS_CHROMEOS) | |
| 342 user_activity_notifier_.reset(); | |
| 343 #endif | |
| 344 user_activity_detector_.reset(); | |
| 345 host_.reset(); | |
| 346 } | |
| 347 | |
| 348 gfx::Size ShellDesktopController::GetPrimaryDisplaySize() { | |
| 349 #if defined(OS_CHROMEOS) | |
| 350 const std::vector<ui::DisplayConfigurator::DisplayState>& displays = | |
| 351 display_configurator_->cached_displays(); | |
| 352 if (displays.empty()) | |
| 353 return gfx::Size(); | |
| 354 const ui::DisplayMode* mode = displays[0].display->current_mode(); | |
| 355 return mode ? mode->size() : gfx::Size(); | |
| 356 #else | |
| 357 return gfx::Size(); | |
| 358 #endif | |
| 359 } | |
| 360 | |
| 361 } // namespace extensions | |
| OLD | NEW |