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 #ifndef SERVICES_UI_PUBLIC_CPP_WINDOW_H_ | |
6 #define SERVICES_UI_PUBLIC_CPP_WINDOW_H_ | |
7 | |
8 #include <stdint.h> | |
9 #include <vector> | |
10 | |
11 #include "base/callback.h" | |
12 #include "base/macros.h" | |
13 #include "base/observer_list.h" | |
14 #include "cc/surfaces/surface_info.h" | |
15 #include "services/service_manager/public/interfaces/interface_provider.mojom.h" | |
16 #include "services/ui/common/types.h" | |
17 #include "services/ui/public/cpp/window_compositor_frame_sink.h" | |
18 #include "services/ui/public/interfaces/mus_constants.mojom.h" | |
19 #include "services/ui/public/interfaces/window_tree.mojom.h" | |
20 #include "ui/gfx/geometry/insets.h" | |
21 #include "ui/gfx/geometry/rect.h" | |
22 | |
23 namespace gpu { | |
24 class GpuMemoryBufferManager; | |
25 } | |
26 | |
27 namespace ui { | |
28 | |
29 class InputEventHandler; | |
30 class WindowCompositorFrameSinkBinding; | |
31 class WindowObserver; | |
32 class WindowDropTarget; | |
33 class WindowTreeClient; | |
34 class WindowTreeClientPrivate; | |
35 | |
36 namespace { | |
37 class OrderChangedNotifier; | |
38 } | |
39 | |
40 // Defined in window_property.h (which we do not include) | |
41 template <typename T> | |
42 struct WindowProperty; | |
43 | |
44 // Windows are owned by the WindowTreeClient. See WindowTreeClientDelegate for | |
45 // details on ownership. | |
46 // | |
47 // TODO(beng): Right now, you'll have to implement a WindowObserver to track | |
48 // destruction and NULL any pointers you have. | |
49 // Investigate some kind of smart pointer or weak pointer for these. | |
50 class Window { | |
51 public: | |
52 using Children = std::vector<Window*>; | |
53 using EmbedCallback = base::Callback<void(bool)>; | |
54 using PropertyDeallocator = void (*)(int64_t value); | |
55 using SharedProperties = std::map<std::string, std::vector<uint8_t>>; | |
56 | |
57 // Destroys this window and all its children. Destruction is allowed for | |
58 // windows that were created by this connection, or the roots. For windows | |
59 // from other connections (except the roots), Destroy() does nothing. If the | |
60 // destruction is allowed observers are notified and the Window is | |
61 // immediately deleted. | |
62 void Destroy(); | |
63 | |
64 // Returns true if this client created and owns this window. | |
65 bool WasCreatedByThisClient() const; | |
66 | |
67 WindowTreeClient* window_tree() { return client_; } | |
68 | |
69 // The local_id is provided for client code. The local_id is not set or | |
70 // manipulated by mus. The default value is -1. | |
71 void set_local_id(int id) { local_id_ = id; } | |
72 int local_id() const { return local_id_; } | |
73 | |
74 int64_t display_id() const { return display_id_; } | |
75 | |
76 // Geometric disposition relative to parent window. | |
77 const gfx::Rect& bounds() const { return bounds_; } | |
78 void SetBounds(const gfx::Rect& bounds); | |
79 | |
80 // Geometric disposition relative to root window. | |
81 gfx::Rect GetBoundsInRoot() const; | |
82 | |
83 const gfx::Insets& client_area() const { return client_area_; } | |
84 const std::vector<gfx::Rect>& additional_client_areas() { | |
85 return additional_client_areas_; | |
86 } | |
87 void SetClientArea(const gfx::Insets& new_client_area) { | |
88 SetClientArea(new_client_area, std::vector<gfx::Rect>()); | |
89 } | |
90 void SetClientArea(const gfx::Insets& new_client_area, | |
91 const std::vector<gfx::Rect>& additional_client_areas); | |
92 | |
93 // Mouse events outside the hit test mask do not hit the window. Returns null | |
94 // if there is no mask. | |
95 const gfx::Rect* hit_test_mask() const { return hit_test_mask_.get(); } | |
96 void SetHitTestMask(const gfx::Rect& mask_rect); | |
97 void ClearHitTestMask(); | |
98 | |
99 // Visibility (also see IsDrawn()). When created windows are hidden. | |
100 bool visible() const { return visible_; } | |
101 void SetVisible(bool value); | |
102 | |
103 float opacity() const { return opacity_; } | |
104 void SetOpacity(float opacity); | |
105 | |
106 // Cursors | |
107 mojom::Cursor predefined_cursor() const { return cursor_id_; } | |
108 void SetPredefinedCursor(ui::mojom::Cursor cursor_id); | |
109 | |
110 // A Window is drawn if the Window and all its ancestors are visible and the | |
111 // Window is attached to the root. | |
112 bool IsDrawn() const; | |
113 | |
114 std::unique_ptr<WindowCompositorFrameSink> RequestCompositorFrameSink( | |
115 scoped_refptr<cc::ContextProvider> context_provider, | |
116 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager); | |
117 | |
118 void AttachCompositorFrameSink( | |
119 std::unique_ptr<WindowCompositorFrameSinkBinding> | |
120 compositor_frame_sink_binding); | |
121 | |
122 // The template-ized versions of the following methods rely on the presence | |
123 // of a mojo::TypeConverter<const std::vector<uint8_t>, T>. | |
124 // Sets a shared property on the window which is sent to the window server and | |
125 // shared with other clients that can view this window. | |
126 template <typename T> | |
127 void SetSharedProperty(const std::string& name, const T& data); | |
128 // Gets a shared property set on the window. The property must exist. Call | |
129 // HasSharedProperty() before calling. | |
130 template <typename T> | |
131 T GetSharedProperty(const std::string& name) const; | |
132 // Removes the shared property. | |
133 void ClearSharedProperty(const std::string& name); | |
134 bool HasSharedProperty(const std::string& name) const; | |
135 | |
136 // TODO(beng): Test only, should move to a helper. | |
137 const SharedProperties& shared_properties() { return properties_; } | |
138 | |
139 // Sets the |value| of the given window |property|. Setting to the default | |
140 // value (e.g., NULL) removes the property. The caller is responsible for the | |
141 // lifetime of any object set as a property on the Window. | |
142 // | |
143 // These properties are not visible to the window server. | |
144 template <typename T> | |
145 void SetLocalProperty(const WindowProperty<T>* property, T value); | |
146 | |
147 // Returns the value of the given window |property|. Returns the | |
148 // property-specific default value if the property was not previously set. | |
149 // | |
150 // These properties are only visible in the current process and are not | |
151 // shared with other mojo services. | |
152 template <typename T> | |
153 T GetLocalProperty(const WindowProperty<T>* property) const; | |
154 | |
155 // Sets the |property| to its default value. Useful for avoiding a cast when | |
156 // setting to NULL. | |
157 // | |
158 // These properties are only visible in the current process and are not | |
159 // shared with other mojo services. | |
160 template <typename T> | |
161 void ClearLocalProperty(const WindowProperty<T>* property); | |
162 | |
163 void set_input_event_handler(InputEventHandler* input_event_handler) { | |
164 input_event_handler_ = input_event_handler; | |
165 } | |
166 | |
167 // Observation. | |
168 void AddObserver(WindowObserver* observer); | |
169 void RemoveObserver(WindowObserver* observer); | |
170 | |
171 // Tree. | |
172 Window* parent() { return parent_; } | |
173 const Window* parent() const { return parent_; } | |
174 | |
175 Window* GetRoot() { | |
176 return const_cast<Window*>(const_cast<const Window*>(this)->GetRoot()); | |
177 } | |
178 const Window* GetRoot() const; | |
179 | |
180 void AddChild(Window* child); | |
181 void RemoveChild(Window* child); | |
182 const Children& children() const { return children_; } | |
183 | |
184 void Reorder(Window* relative, mojom::OrderDirection direction); | |
185 void MoveToFront(); | |
186 void MoveToBack(); | |
187 | |
188 // Returns true if |child| is this or a descendant of this. | |
189 bool Contains(const Window* child) const; | |
190 | |
191 void AddTransientWindow(Window* transient_window); | |
192 void RemoveTransientWindow(Window* transient_window); | |
193 | |
194 // TODO(fsamuel): Figure out if we want to refactor transient window | |
195 // management into a separate class. | |
196 // Transient tree. | |
197 Window* transient_parent() { return transient_parent_; } | |
198 const Window* transient_parent() const { return transient_parent_; } | |
199 const Children& transient_children() const { return transient_children_; } | |
200 | |
201 void SetModal(); | |
202 bool is_modal() const { return is_modal_; } | |
203 | |
204 Window* GetChildByLocalId(int id); | |
205 | |
206 void SetTextInputState(mojo::TextInputStatePtr state); | |
207 void SetImeVisibility(bool visible, mojo::TextInputStatePtr state); | |
208 | |
209 bool HasCapture() const; | |
210 void SetCapture(); | |
211 void ReleaseCapture(); | |
212 | |
213 // Focus. See WindowTreeClient::ClearFocus() to reset focus. | |
214 void SetFocus(); | |
215 bool HasFocus() const; | |
216 void SetCanFocus(bool can_focus); | |
217 | |
218 // Sets whether this window accepts drags. Passing a non-null |drop_target| | |
219 // will enable acceptance of drops. Passing null will disable it. | |
220 void SetCanAcceptDrops(WindowDropTarget* drop_target); | |
221 WindowDropTarget* drop_target() { return drop_target_; } | |
222 | |
223 // Sets whether this window accepts events. | |
224 void SetCanAcceptEvents(bool can_accept_events); | |
225 | |
226 // Embedding. See window_tree.mojom for details. | |
227 void Embed(ui::mojom::WindowTreeClientPtr client, uint32_t flags = 0); | |
228 | |
229 // NOTE: callback is run synchronously if Embed() is not allowed on this | |
230 // Window. | |
231 void Embed(ui::mojom::WindowTreeClientPtr client, | |
232 const EmbedCallback& callback, | |
233 uint32_t flags = 0); | |
234 | |
235 // TODO(sky): this API is only applicable to the WindowManager. Move it | |
236 // to a better place. | |
237 void RequestClose(); | |
238 | |
239 // Starts an inter-process drag and drop operation. When this finishes, will | |
240 // return the tuple [success, action_taken] to |callback|, where action_taken | |
241 // is one of the ui::mojom::kDropEffect constants in | |
242 // window_tree_constants.mojom. | |
243 void PerformDragDrop( | |
244 const std::map<std::string, std::vector<uint8_t>>& drag_data, | |
245 int drag_operation, | |
246 const gfx::Point& cursor_location, | |
247 const SkBitmap& bitmap, | |
248 const base::Callback<void(bool, uint32_t)>& callback); | |
249 | |
250 // Cancels the in progress drag started with PerformDragDrop(). | |
251 void CancelDragDrop(); | |
252 | |
253 // Tells the window manager to take control of moving the window. Returns | |
254 // true if the move wasn't canceled. | |
255 void PerformWindowMove(mojom::MoveLoopSource source, | |
256 const gfx::Point& cursor_location, | |
257 const base::Callback<void(bool)>& callback); | |
258 | |
259 // Tells the window manager to abort any current move initiated by | |
260 // PerformWindowMove(). | |
261 void CancelWindowMove(); | |
262 | |
263 // Returns an internal name, set by a client app when it creates a window. | |
264 std::string GetName() const; | |
265 | |
266 protected: | |
267 // This class is subclassed only by test classes that provide a public ctor. | |
268 Window(); | |
269 ~Window(); | |
270 | |
271 private: | |
272 friend class WindowPrivate; | |
273 friend class WindowTreeClient; | |
274 friend class WindowTreeClientPrivate; | |
275 | |
276 Window(WindowTreeClient* client, Id id); | |
277 | |
278 // Used to identify this Window on the server. Clients can not change this | |
279 // value. | |
280 Id server_id() const { return server_id_; } | |
281 | |
282 // Applies a shared property change locally and forwards to the server. If | |
283 // |data| is null, this property is deleted. | |
284 void SetSharedPropertyInternal(const std::string& name, | |
285 const std::vector<uint8_t>* data); | |
286 // Called by the public {Set,Get,Clear}Property functions. | |
287 int64_t SetLocalPropertyInternal(const void* key, | |
288 const char* name, | |
289 PropertyDeallocator deallocator, | |
290 int64_t value, | |
291 int64_t default_value); | |
292 int64_t GetLocalPropertyInternal(const void* key, | |
293 int64_t default_value) const; | |
294 | |
295 void LocalDestroy(); | |
296 void LocalAddChild(Window* child); | |
297 void LocalRemoveChild(Window* child); | |
298 void LocalAddTransientWindow(Window* transient_window); | |
299 void LocalRemoveTransientWindow(Window* transient_window); | |
300 void LocalSetModal(); | |
301 // Returns true if the order actually changed. | |
302 bool LocalReorder(Window* relative, mojom::OrderDirection direction); | |
303 void LocalSetBounds(const gfx::Rect& old_bounds, const gfx::Rect& new_bounds); | |
304 void LocalSetClientArea( | |
305 const gfx::Insets& new_client_area, | |
306 const std::vector<gfx::Rect>& additional_client_areas); | |
307 void LocalSetParentDrawn(bool drawn); | |
308 void LocalSetDisplay(int64_t display_id); | |
309 void LocalSetVisible(bool visible); | |
310 void LocalSetOpacity(float opacity); | |
311 void LocalSetPredefinedCursor(mojom::Cursor cursor_id); | |
312 void LocalSetSharedProperty(const std::string& name, | |
313 const std::vector<uint8_t>* data); | |
314 void LocalSetSurfaceInfo(const cc::SurfaceInfo& surface_info); | |
315 | |
316 // Notifies this winodw that its stacking position has changed. | |
317 void NotifyWindowStackingChanged(); | |
318 // Methods implementing visibility change notifications. See WindowObserver | |
319 // for more details. | |
320 void NotifyWindowVisibilityChanged(Window* target, bool visible); | |
321 // Notifies this window's observers. Returns false if |this| was deleted | |
322 // during the call (by an observer), otherwise true. | |
323 bool NotifyWindowVisibilityChangedAtReceiver(Window* target, bool visible); | |
324 // Notifies this window and its child hierarchy. Returns false if |this| was | |
325 // deleted during the call (by an observer), otherwise true. | |
326 bool NotifyWindowVisibilityChangedDown(Window* target, bool visible); | |
327 // Notifies this window and its parent hierarchy. | |
328 void NotifyWindowVisibilityChangedUp(Window* target, bool visible); | |
329 | |
330 // Returns true if embed is allowed for this node. If embedding is allowed all | |
331 // the children are removed. | |
332 bool PrepareForEmbed(); | |
333 | |
334 void RemoveTransientWindowImpl(Window* child); | |
335 static void ReorderWithoutNotification(Window* window, | |
336 Window* relative, | |
337 mojom::OrderDirection direction); | |
338 // Returns true if the order actually changed. | |
339 static bool ReorderImpl(Window* window, | |
340 Window* relative, | |
341 mojom::OrderDirection direction, | |
342 OrderChangedNotifier* notifier); | |
343 | |
344 // Returns a pointer to the stacking target that can be used by | |
345 // RestackTransientDescendants. | |
346 static Window** GetStackingTarget(Window* window); | |
347 | |
348 WindowTreeClient* client_; | |
349 Id server_id_; | |
350 int local_id_ = -1; | |
351 Window* parent_; | |
352 Children children_; | |
353 | |
354 Window* stacking_target_; | |
355 Window* transient_parent_; | |
356 Children transient_children_; | |
357 | |
358 bool is_modal_; | |
359 | |
360 base::ObserverList<WindowObserver> observers_; | |
361 InputEventHandler* input_event_handler_; | |
362 | |
363 gfx::Rect bounds_; | |
364 gfx::Insets client_area_; | |
365 std::vector<gfx::Rect> additional_client_areas_; | |
366 std::unique_ptr<gfx::Rect> hit_test_mask_; | |
367 | |
368 bool visible_; | |
369 float opacity_; | |
370 int64_t display_id_; | |
371 | |
372 // The client supplied delegate that receives drag events for this | |
373 // window (weak ptr). | |
374 WindowDropTarget* drop_target_ = nullptr; | |
375 | |
376 // Whether this window can accept events. Initialized to true to | |
377 // match ServerWindow. | |
378 bool can_accept_events_ = true; | |
379 | |
380 mojom::Cursor cursor_id_; | |
381 | |
382 SharedProperties properties_; | |
383 | |
384 // Drawn state of our parent. This is only meaningful for root Windows, in | |
385 // which the parent Window isn't exposed to the client. | |
386 bool parent_drawn_; | |
387 | |
388 // Value struct to keep the name and deallocator for this property. | |
389 // Key cannot be used for this purpose because it can be char* or | |
390 // WindowProperty<>. | |
391 struct Value { | |
392 const char* name; | |
393 int64_t value; | |
394 PropertyDeallocator deallocator; | |
395 }; | |
396 | |
397 std::map<const void*, Value> prop_map_; | |
398 | |
399 cc::SurfaceInfo surface_info_; | |
400 | |
401 DISALLOW_COPY_AND_ASSIGN(Window); | |
402 }; | |
403 | |
404 } // namespace ui | |
405 | |
406 #endif // SERVICES_UI_PUBLIC_CPP_WINDOW_H_ | |
OLD | NEW |