root_window_controller.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2017 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. #ifndef EXTENSIONS_SHELL_BROWSER_ROOT_WINDOW_CONTROLLER_H_
  5. #define EXTENSIONS_SHELL_BROWSER_ROOT_WINDOW_CONTROLLER_H_
  6. #include <list>
  7. #include <memory>
  8. #include "base/memory/raw_ptr.h"
  9. #include "extensions/browser/app_window/app_window_registry.h"
  10. #include "ui/aura/client/window_parenting_client.h"
  11. #include "ui/aura/window_tree_host_observer.h"
  12. #include "ui/gfx/native_widget_types.h"
  13. namespace aura {
  14. class WindowTreeHost;
  15. namespace client {
  16. class ScreenPositionClient;
  17. } // namespace client
  18. } // namespace aura
  19. namespace content {
  20. class BrowserContext;
  21. } // namespace content
  22. namespace gfx {
  23. class Rect;
  24. class Size;
  25. } // namespace gfx
  26. namespace extensions {
  27. class AppWindow;
  28. // Owns and manages a WindowTreeHost for a display. New AppWindows will fill
  29. // the entire root window. Any additional AppWindows are simply drawn over the
  30. // existing AppWindow(s) and cannot be resized except by resizing the
  31. // WindowTreeHost.
  32. // TODO(michaelpg): Allow app windows to move between displays when bounds are
  33. // updated via the chrome.app.window API.
  34. class RootWindowController : public aura::client::WindowParentingClient,
  35. public aura::WindowTreeHostObserver,
  36. public AppWindowRegistry::Observer {
  37. public:
  38. class DesktopDelegate {
  39. public:
  40. virtual ~DesktopDelegate() = default;
  41. // Called when the root window requests to be closed. This should eventually
  42. // destroy |root_window_controller|.
  43. virtual void CloseRootWindowController(
  44. RootWindowController* root_window_controller) = 0;
  45. };
  46. // RootWindowController initializes and displays a WindowTreeHost within
  47. // |bounds| (in physical pixels).
  48. // |desktop_delegate| must outlive the RootWindowController.
  49. RootWindowController(DesktopDelegate* desktop_delegate,
  50. const gfx::Rect& bounds,
  51. content::BrowserContext* browser_context);
  52. RootWindowController(const RootWindowController&) = delete;
  53. RootWindowController& operator=(const RootWindowController&) = delete;
  54. ~RootWindowController() override;
  55. // Attaches a NativeAppWindow's window to our root window.
  56. void AddAppWindow(AppWindow* app_window, gfx::NativeWindow window);
  57. // Unparents the AppWindow's window from our root window so it can be added to
  58. // a different RootWindowController.
  59. void RemoveAppWindow(AppWindow* app_window);
  60. // Closes the root window's AppWindows, resulting in their destruction.
  61. void CloseAppWindows();
  62. // Updates the size of the root window.
  63. // TODO(michaelpg): Handle display events to adapt or close the window.
  64. void UpdateSize(const gfx::Size& size);
  65. aura::WindowTreeHost* host() { return host_.get(); }
  66. // aura::client::WindowParentingClient:
  67. aura::Window* GetDefaultParent(aura::Window* window,
  68. const gfx::Rect& bounds) override;
  69. // aura::WindowTreeHostObserver:
  70. void OnHostCloseRequested(aura::WindowTreeHost* host) override;
  71. // AppWindowRegistry::Observer:
  72. void OnAppWindowRemoved(AppWindow* app_window) override;
  73. private:
  74. void DestroyWindowTreeHost();
  75. const raw_ptr<DesktopDelegate> desktop_delegate_;
  76. // The BrowserContext used to create AppWindows.
  77. const raw_ptr<content::BrowserContext> browser_context_;
  78. std::unique_ptr<aura::client::ScreenPositionClient> screen_position_client_;
  79. // The host we create.
  80. std::unique_ptr<aura::WindowTreeHost> host_;
  81. // List of AppWindows we've created. Used to close any remaining app windows
  82. // when |host_| is closed or |this| is destroyed.
  83. // Note: Pointers are unowned. NativeAppWindow::Close() will delete them.
  84. std::list<AppWindow*> app_windows_;
  85. };
  86. } // namespace extensions
  87. #endif // EXTENSIONS_SHELL_BROWSER_ROOT_WINDOW_CONTROLLER_H_