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 MOJO_SERVICES_VIEW_MANAGER_CPP_VIEW_H_ | |
6 #define MOJO_SERVICES_VIEW_MANAGER_CPP_VIEW_H_ | |
7 | |
8 #include <stdint.h> | |
9 #include <vector> | |
10 | |
11 #include "base/observer_list.h" | |
12 #include "geometry/interfaces/geometry.mojom.h" | |
13 #include "mojo/public/cpp/bindings/array.h" | |
14 #include "mojo/public/cpp/system/macros.h" | |
15 #include "mojo/public/interfaces/application/service_provider.mojom.h" | |
16 #include "surfaces/interfaces/surface_id.mojom.h" | |
17 #include "view_manager/cpp/types.h" | |
18 #include "view_manager/interfaces/view_manager.mojom.h" | |
19 #include "view_manager/interfaces/view_manager_constants.mojom.h" | |
20 | |
21 namespace mojo { | |
22 | |
23 class ServiceProviderImpl; | |
24 class View; | |
25 class ViewManager; | |
26 class ViewObserver; | |
27 | |
28 // Defined in view_property.h (which we do not include) | |
29 template <typename T> | |
30 struct ViewProperty; | |
31 | |
32 // Views are owned by the ViewManager. | |
33 // TODO(beng): Right now, you'll have to implement a ViewObserver to track | |
34 // destruction and NULL any pointers you have. | |
35 // Investigate some kind of smart pointer or weak pointer for these. | |
36 class View { | |
37 public: | |
38 using Children = std::vector<View*>; | |
39 using SharedProperties = std::map<std::string, std::vector<uint8_t>>; | |
40 | |
41 // Destroys this view and all its children. | |
42 void Destroy(); | |
43 | |
44 ViewManager* view_manager() { return manager_; } | |
45 | |
46 // Configuration. | |
47 Id id() const { return id_; } | |
48 | |
49 // Geometric disposition. | |
50 const Rect& bounds() const { return bounds_; } | |
51 void SetBounds(const Rect& bounds); | |
52 | |
53 // Visibility (also see IsDrawn()). When created views are hidden. | |
54 bool visible() const { return visible_; } | |
55 void SetVisible(bool value); | |
56 | |
57 const ViewportMetrics& viewport_metrics() { return *viewport_metrics_; } | |
58 | |
59 // Returns the set of string to bag of byte properties. These properties are | |
60 // shared with the view manager. | |
61 const SharedProperties& shared_properties() const { return properties_; } | |
62 // Sets a property. If |data| is null, this property is deleted. | |
63 void SetSharedProperty(const std::string& name, | |
64 const std::vector<uint8_t>* data); | |
65 | |
66 // Sets the |value| of the given window |property|. Setting to the default | |
67 // value (e.g., NULL) removes the property. The caller is responsible for the | |
68 // lifetime of any object set as a property on the View. | |
69 // | |
70 // These properties are not visible to the view manager. | |
71 template <typename T> | |
72 void SetLocalProperty(const ViewProperty<T>* property, T value); | |
73 | |
74 // Returns the value of the given window |property|. Returns the | |
75 // property-specific default value if the property was not previously set. | |
76 // | |
77 // These properties are only visible in the current process and are not | |
78 // shared with other mojo services. | |
79 template <typename T> | |
80 T GetLocalProperty(const ViewProperty<T>* property) const; | |
81 | |
82 // Sets the |property| to its default value. Useful for avoiding a cast when | |
83 // setting to NULL. | |
84 // | |
85 // These properties are only visible in the current process and are not | |
86 // shared with other mojo services. | |
87 template <typename T> | |
88 void ClearLocalProperty(const ViewProperty<T>* property); | |
89 | |
90 // Type of a function to delete a property that this view owns. | |
91 typedef void (*PropertyDeallocator)(int64_t value); | |
92 | |
93 // A View is drawn if the View and all its ancestors are visible and the | |
94 // View is attached to the root. | |
95 bool IsDrawn() const; | |
96 | |
97 // Observation. | |
98 void AddObserver(ViewObserver* observer); | |
99 void RemoveObserver(ViewObserver* observer); | |
100 | |
101 // Tree. | |
102 View* parent() { return parent_; } | |
103 const View* parent() const { return parent_; } | |
104 const Children& children() const { return children_; } | |
105 View* GetRoot() { | |
106 return const_cast<View*>(const_cast<const View*>(this)->GetRoot()); | |
107 } | |
108 const View* GetRoot() const; | |
109 | |
110 void AddChild(View* child); | |
111 void RemoveChild(View* child); | |
112 | |
113 void Reorder(View* relative, OrderDirection direction); | |
114 void MoveToFront(); | |
115 void MoveToBack(); | |
116 | |
117 bool Contains(View* child) const; | |
118 | |
119 View* GetChildById(Id id); | |
120 | |
121 void SetSurfaceId(SurfaceIdPtr id); | |
122 | |
123 // Focus. | |
124 void SetFocus(); | |
125 | |
126 // Embedding. See view_manager.mojom for details. | |
127 void Embed(const String& url); | |
128 void Embed(const String& url, | |
129 InterfaceRequest<ServiceProvider> services, | |
130 ServiceProviderPtr exposed_services); | |
131 void Embed(ViewManagerClientPtr client); | |
132 | |
133 protected: | |
134 // This class is subclassed only by test classes that provide a public ctor. | |
135 View(); | |
136 ~View(); | |
137 | |
138 private: | |
139 friend class ViewPrivate; | |
140 friend class ViewManagerClientImpl; | |
141 | |
142 View(ViewManager* manager, Id id); | |
143 | |
144 // Called by the public {Set,Get,Clear}Property functions. | |
145 int64_t SetLocalPropertyInternal(const void* key, | |
146 const char* name, | |
147 PropertyDeallocator deallocator, | |
148 int64_t value, | |
149 int64_t default_value); | |
150 int64_t GetLocalPropertyInternal(const void* key, | |
151 int64_t default_value) const; | |
152 | |
153 void LocalDestroy(); | |
154 void LocalAddChild(View* child); | |
155 void LocalRemoveChild(View* child); | |
156 // Returns true if the order actually changed. | |
157 bool LocalReorder(View* relative, OrderDirection direction); | |
158 void LocalSetBounds(const Rect& old_bounds, const Rect& new_bounds); | |
159 void LocalSetViewportMetrics(const ViewportMetrics& old_metrics, | |
160 const ViewportMetrics& new_metrics); | |
161 void LocalSetDrawn(bool drawn); | |
162 void LocalSetVisible(bool visible); | |
163 | |
164 // Methods implementing visibility change notifications. See ViewObserver | |
165 // for more details. | |
166 void NotifyViewVisibilityChanged(View* target); | |
167 // Notifies this view's observers. Returns false if |this| was deleted during | |
168 // the call (by an observer), otherwise true. | |
169 bool NotifyViewVisibilityChangedAtReceiver(View* target); | |
170 // Notifies this view and its child hierarchy. Returns false if |this| was | |
171 // deleted during the call (by an observer), otherwise true. | |
172 bool NotifyViewVisibilityChangedDown(View* target); | |
173 // Notifies this view and its parent hierarchy. | |
174 void NotifyViewVisibilityChangedUp(View* target); | |
175 | |
176 // Returns true if embed is allowed for this node. If embedding is allowed all | |
177 // the children are removed. | |
178 bool PrepareForEmbed(); | |
179 | |
180 ViewManager* manager_; | |
181 Id id_; | |
182 View* parent_; | |
183 Children children_; | |
184 | |
185 base::ObserverList<ViewObserver> observers_; | |
186 | |
187 Rect bounds_; | |
188 ViewportMetricsPtr viewport_metrics_; | |
189 | |
190 bool visible_; | |
191 | |
192 SharedProperties properties_; | |
193 | |
194 // Drawn state is derived from the visible state and the parent's visible | |
195 // state. This field is only used if the view has no parent (eg it's a root). | |
196 bool drawn_; | |
197 | |
198 // Value struct to keep the name and deallocator for this property. | |
199 // Key cannot be used for this purpose because it can be char* or | |
200 // WindowProperty<>. | |
201 struct Value { | |
202 const char* name; | |
203 int64_t value; | |
204 PropertyDeallocator deallocator; | |
205 }; | |
206 | |
207 std::map<const void*, Value> prop_map_; | |
208 | |
209 MOJO_DISALLOW_COPY_AND_ASSIGN(View); | |
210 }; | |
211 | |
212 } // namespace mojo | |
213 | |
214 #endif // MOJO_SERVICES_VIEW_MANAGER_CPP_VIEW_H_ | |
OLD | NEW |