1 // Copyright (c) 2012 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 #ifndef UI_VIEWS_WIDGET_DESKTOP_AURA_DESKTOP_NATIVE_WIDGET_AURA_H_
6 #define UI_VIEWS_WIDGET_DESKTOP_AURA_DESKTOP_NATIVE_WIDGET_AURA_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "base/macros.h"
12 #include "base/memory/weak_ptr.h"
13 #include "ui/aura/client/drag_drop_delegate.h"
14 #include "ui/aura/client/focus_change_observer.h"
15 #include "ui/aura/window_delegate.h"
16 #include "ui/aura/window_tree_host_observer.h"
17 #include "ui/base/cursor/cursor.h"
18 #include "ui/views/widget/native_widget_private.h"
19 #include "ui/wm/core/compound_event_filter.h"
20 #include "ui/wm/public/activation_change_observer.h"
21 #include "ui/wm/public/activation_delegate.h"
22 
23 namespace aura {
24 class WindowEventDispatcher;
25 class WindowTreeHost;
26 namespace client {
27 class DragDropClient;
28 class ScreenPositionClient;
29 class WindowParentingClient;
30 }  // namespace client
31 }  // namespace aura
32 
33 namespace wm {
34 class CompoundEventFilter;
35 class CursorManager;
36 class FocusController;
37 class ShadowController;
38 class VisibilityController;
39 class WindowModalityController;
40 }  // namespace wm
41 
42 namespace views {
43 namespace corewm {
44 class TooltipController;
45 }
46 class DesktopCaptureClient;
47 class DesktopEventClient;
48 class DesktopNativeCursorManager;
49 class DesktopWindowTreeHost;
50 class DropHelper;
51 class FocusManagerEventHandler;
52 class TooltipManagerAura;
53 class WindowReorderer;
54 
55 // DesktopNativeWidgetAura handles top-level widgets on Windows, Linux, and
56 // Chrome OS with mash.
57 class VIEWS_EXPORT DesktopNativeWidgetAura
58     : public internal::NativeWidgetPrivate,
59       public aura::WindowDelegate,
60       public wm::ActivationDelegate,
61       public wm::ActivationChangeObserver,
62       public aura::client::FocusChangeObserver,
63       public aura::client::DragDropDelegate,
64       public aura::WindowTreeHostObserver {
65  public:
66   explicit DesktopNativeWidgetAura(internal::NativeWidgetDelegate* delegate);
67   ~DesktopNativeWidgetAura() override;
68 
69   // Maps from window to DesktopNativeWidgetAura. |window| must be a root
70   // window.
71   static DesktopNativeWidgetAura* ForWindow(aura::Window* window);
72 
73   // Used to explicitly set a DesktopWindowTreeHost. Must be called before
74   // InitNativeWidget().
75   void SetDesktopWindowTreeHost(
76       std::unique_ptr<DesktopWindowTreeHost> desktop_window_tree_host);
77 
78   // Called by our DesktopWindowTreeHost after it has deleted native resources;
79   // this is the signal that we should start our shutdown.
80   virtual void OnHostClosed();
81 
82   // TODO(beng): remove this method and replace with an implementation of
83   //             WindowDestroying() that takes the window being destroyed.
84   // Called from ~DesktopWindowTreeHost. This takes the WindowEventDispatcher
85   // as by the time we get here |dispatcher_| is NULL.
86   virtual void OnDesktopWindowTreeHostDestroyed(aura::WindowTreeHost* host);
87 
root_window_event_filter()88   wm::CompoundEventFilter* root_window_event_filter() {
89     return root_window_event_filter_.get();
90   }
host()91   aura::WindowTreeHost* host() { return host_.get(); }
92 
content_window()93   aura::Window* content_window() { return content_window_; }
94 
widget_type()95   Widget::InitParams::Type widget_type() const { return widget_type_; }
96 
97   // Ensures that the correct window is activated/deactivated based on whether
98   // we are being activated/deactivated.
99   void HandleActivationChanged(bool active);
100 
101   // Called before the window tree host will close.
102   void OnHostWillClose();
103 
104   // Overridden from internal::NativeWidgetPrivate:
105   gfx::NativeWindow GetNativeWindow() const override;
106 
107   // Configures the appropriate aura::Windows based on the
108   // DesktopWindowTreeHost's transparency.
109   void UpdateWindowTransparency();
110 
111  protected:
112   // internal::NativeWidgetPrivate:
113   void InitNativeWidget(Widget::InitParams params) override;
114   void OnWidgetInitDone() override;
115   std::unique_ptr<NonClientFrameView> CreateNonClientFrameView() override;
116   bool ShouldUseNativeFrame() const override;
117   bool ShouldWindowContentsBeTransparent() const override;
118   void FrameTypeChanged() override;
119   Widget* GetWidget() override;
120   const Widget* GetWidget() const override;
121   gfx::NativeView GetNativeView() const override;
122   Widget* GetTopLevelWidget() override;
123   const ui::Compositor* GetCompositor() const override;
124   const ui::Layer* GetLayer() const override;
125   void ReorderNativeViews() override;
126   void ViewRemoved(View* view) override;
127   void SetNativeWindowProperty(const char* name, void* value) override;
128   void* GetNativeWindowProperty(const char* name) const override;
129   TooltipManager* GetTooltipManager() const override;
130   void SetCapture() override;
131   void ReleaseCapture() override;
132   bool HasCapture() const override;
133   ui::InputMethod* GetInputMethod() override;
134   void CenterWindow(const gfx::Size& size) override;
135   void GetWindowPlacement(gfx::Rect* bounds,
136                           ui::WindowShowState* maximized) const override;
137   bool SetWindowTitle(const base::string16& title) override;
138   void SetWindowIcons(const gfx::ImageSkia& window_icon,
139                       const gfx::ImageSkia& app_icon) override;
140   void InitModalType(ui::ModalType modal_type) override;
141   gfx::Rect GetWindowBoundsInScreen() const override;
142   gfx::Rect GetClientAreaBoundsInScreen() const override;
143   gfx::Rect GetRestoredBounds() const override;
144   std::string GetWorkspace() const override;
145   void SetBounds(const gfx::Rect& bounds) override;
146   void SetBoundsConstrained(const gfx::Rect& bounds) override;
147   void SetSize(const gfx::Size& size) override;
148   void StackAbove(gfx::NativeView native_view) override;
149   void StackAtTop() override;
150   void SetShape(std::unique_ptr<Widget::ShapeRects> shape) override;
151   void Close() override;
152   void CloseNow() override;
153   void Show(ui::WindowShowState show_state,
154             const gfx::Rect& restore_bounds) override;
155   void Hide() override;
156   bool IsVisible() const override;
157   void Activate() override;
158   void Deactivate() override;
159   bool IsActive() const override;
160   void SetZOrderLevel(ui::ZOrderLevel order) override;
161   ui::ZOrderLevel GetZOrderLevel() const override;
162   void SetVisibleOnAllWorkspaces(bool always_visible) override;
163   bool IsVisibleOnAllWorkspaces() const override;
164   void Maximize() override;
165   void Minimize() override;
166   bool IsMaximized() const override;
167   bool IsMinimized() const override;
168   void Restore() override;
169   void SetFullscreen(bool fullscreen) override;
170   bool IsFullscreen() const override;
171   void SetCanAppearInExistingFullscreenSpaces(
172       bool can_appear_in_existing_fullscreen_spaces) override;
173   void SetOpacity(float opacity) override;
174   void SetAspectRatio(const gfx::SizeF& aspect_ratio) override;
175   void FlashFrame(bool flash_frame) override;
176   void RunShellDrag(View* view,
177                     std::unique_ptr<ui::OSExchangeData> data,
178                     const gfx::Point& location,
179                     int operation,
180                     ui::mojom::DragEventSource source) override;
181   void SchedulePaintInRect(const gfx::Rect& rect) override;
182   void ScheduleLayout() override;
183   void SetCursor(gfx::NativeCursor cursor) override;
184   bool IsMouseEventsEnabled() const override;
185   bool IsMouseButtonDown() const override;
186   void ClearNativeFocus() override;
187   gfx::Rect GetWorkAreaBoundsInScreen() const override;
188   Widget::MoveLoopResult RunMoveLoop(
189       const gfx::Vector2d& drag_offset,
190       Widget::MoveLoopSource source,
191       Widget::MoveLoopEscapeBehavior escape_behavior) override;
192   void EndMoveLoop() override;
193   void SetVisibilityChangedAnimationsEnabled(bool value) override;
194   void SetVisibilityAnimationDuration(const base::TimeDelta& duration) override;
195   void SetVisibilityAnimationTransition(
196       Widget::VisibilityTransition transition) override;
197   bool IsTranslucentWindowOpacitySupported() const override;
198   ui::GestureRecognizer* GetGestureRecognizer() override;
199   void OnSizeConstraintsChanged() override;
200   void OnNativeViewHierarchyWillChange() override;
201   void OnNativeViewHierarchyChanged() override;
202   std::string GetName() const override;
203 
204   // aura::WindowDelegate:
205   gfx::Size GetMinimumSize() const override;
206   gfx::Size GetMaximumSize() const override;
OnBoundsChanged(const gfx::Rect & old_bounds,const gfx::Rect & new_bounds)207   void OnBoundsChanged(const gfx::Rect& old_bounds,
208                        const gfx::Rect& new_bounds) override {}
209   gfx::NativeCursor GetCursor(const gfx::Point& point) override;
210   int GetNonClientComponent(const gfx::Point& point) const override;
211   bool ShouldDescendIntoChildForEventHandling(
212       aura::Window* child,
213       const gfx::Point& location) override;
214   bool CanFocus() override;
215   void OnCaptureLost() override;
216   void OnPaint(const ui::PaintContext& context) override;
217   void OnDeviceScaleFactorChanged(float old_device_scale_factor,
218                                   float new_device_scale_factor) override;
219   void OnWindowDestroying(aura::Window* window) override;
220   void OnWindowDestroyed(aura::Window* window) override;
221   void OnWindowTargetVisibilityChanged(bool visible) override;
222   bool HasHitTestMask() const override;
223   void GetHitTestMask(SkPath* mask) const override;
224   void UpdateVisualState() override;
225 
226   // ui::EventHandler:
227   void OnKeyEvent(ui::KeyEvent* event) override;
228   void OnMouseEvent(ui::MouseEvent* event) override;
229   void OnScrollEvent(ui::ScrollEvent* event) override;
230   void OnGestureEvent(ui::GestureEvent* event) override;
231 
232   // wm::ActivationDelegate:
233   bool ShouldActivate() const override;
234 
235   // wm::ActivationChangeObserver:
236   void OnWindowActivated(wm::ActivationChangeObserver::ActivationReason reason,
237                          aura::Window* gained_active,
238                          aura::Window* lost_active) override;
239 
240   // aura::client::FocusChangeObserver:
241   void OnWindowFocused(aura::Window* gained_focus,
242                        aura::Window* lost_focus) override;
243 
244   // ura::client::DragDropDelegate:
245   void OnDragEntered(const ui::DropTargetEvent& event) override;
246   int OnDragUpdated(const ui::DropTargetEvent& event) override;
247   void OnDragExited() override;
248   int OnPerformDrop(const ui::DropTargetEvent& event,
249                     std::unique_ptr<ui::OSExchangeData> data) override;
250 
251   // aura::WindowTreeHostObserver:
252   void OnHostCloseRequested(aura::WindowTreeHost* host) override;
253   void OnHostResized(aura::WindowTreeHost* host) override;
254   void OnHostWorkspaceChanged(aura::WindowTreeHost* host) override;
255   void OnHostMovedInPixels(aura::WindowTreeHost* host,
256                            const gfx::Point& new_origin_in_pixels) override;
257 
258  private:
259   friend class RootWindowDestructionObserver;
260 
261   void RootWindowDestroyed();
262 
263   // Notify the root view of our widget of a native accessibility event.
264   void NotifyAccessibilityEvent(ax::mojom::Event event_type);
265 
266   std::unique_ptr<aura::WindowTreeHost> host_;
267   DesktopWindowTreeHost* desktop_window_tree_host_;
268 
269   // See class documentation for Widget in widget.h for a note about ownership.
270   Widget::InitParams::Ownership ownership_;
271 
272   // Internal name.
273   std::string name_;
274 
275   std::unique_ptr<DesktopCaptureClient> capture_client_;
276 
277   // This is the return value from GetNativeView().
278   // WARNING: this may be NULL, in particular during shutdown it becomes NULL.
279   aura::Window* content_window_;
280 
281   internal::NativeWidgetDelegate* native_widget_delegate_;
282 
283   std::unique_ptr<wm::FocusController> focus_client_;
284   std::unique_ptr<aura::client::ScreenPositionClient> position_client_;
285   std::unique_ptr<aura::client::DragDropClient> drag_drop_client_;
286   std::unique_ptr<aura::client::WindowParentingClient> window_parenting_client_;
287   std::unique_ptr<DesktopEventClient> event_client_;
288   std::unique_ptr<FocusManagerEventHandler> focus_manager_event_handler_;
289 
290   // Toplevel event filter which dispatches to other event filters.
291   std::unique_ptr<wm::CompoundEventFilter> root_window_event_filter_;
292 
293   std::unique_ptr<DropHelper> drop_helper_;
294   int last_drop_operation_;
295 
296   std::unique_ptr<corewm::TooltipController> tooltip_controller_;
297   std::unique_ptr<TooltipManagerAura> tooltip_manager_;
298 
299   std::unique_ptr<wm::VisibilityController> visibility_controller_;
300 
301   std::unique_ptr<wm::WindowModalityController> window_modality_controller_;
302 
303   bool restore_focus_on_activate_;
304 
305   gfx::NativeCursor cursor_;
306   // We must manually reference count the number of users of |cursor_manager_|
307   // because the cursors created by |cursor_manager_| are shared among the
308   // DNWAs. We can't just stuff this in a LazyInstance because we need to
309   // destroy this as the last DNWA happens; we can't put it off until
310   // (potentially) after we tear down the X11 connection because that's a
311   // crash.
312   static int cursor_reference_count_;
313   static wm::CursorManager* cursor_manager_;
314   static DesktopNativeCursorManager* native_cursor_manager_;
315 
316   std::unique_ptr<wm::ShadowController> shadow_controller_;
317 
318   // Reorders child windows of |window_| associated with a view based on the
319   // order of the associated views in the widget's view hierarchy.
320   std::unique_ptr<WindowReorderer> window_reorderer_;
321 
322   // See class documentation for Widget in widget.h for a note about type.
323   Widget::InitParams::Type widget_type_;
324 
325   // See DesktopWindowTreeHost::ShouldUseDesktopNativeCursorManager().
326   bool use_desktop_native_cursor_manager_ = false;
327 
328   // The following factory is used for calls to close the NativeWidgetAura
329   // instance.
330   base::WeakPtrFactory<DesktopNativeWidgetAura> close_widget_factory_{this};
331 
332   DISALLOW_COPY_AND_ASSIGN(DesktopNativeWidgetAura);
333 };
334 
335 }  // namespace views
336 
337 #endif  // UI_VIEWS_WIDGET_DESKTOP_AURA_DESKTOP_NATIVE_WIDGET_AURA_H_
338