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_PUBLIC_CPP_VIEW_MANAGER_LIB_VIEW_MANAGER_SYNCHRONIZER_H_ | |
6 #define MOJO_SERVICES_PUBLIC_CPP_VIEW_MANAGER_LIB_VIEW_MANAGER_SYNCHRONIZER_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/callback.h" | |
10 #include "base/memory/scoped_vector.h" | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" | |
13 #include "mojo/services/public/cpp/view_manager/view_manager.h" | |
14 #include "mojo/services/public/cpp/view_manager/view_manager_types.h" | |
15 #include "mojo/services/public/cpp/view_manager/view_tree_node.h" | |
16 #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h" | |
17 | |
18 class SkBitmap; | |
19 | |
20 namespace mojo { | |
21 namespace view_manager { | |
22 | |
23 class ViewManager; | |
24 class ViewManagerTransaction; | |
25 | |
26 // Manages the connection with the View Manager service. | |
27 class ViewManagerSynchronizer : public ViewManager, | |
28 public InterfaceImpl<ViewManagerClient> { | |
29 public: | |
30 explicit ViewManagerSynchronizer(ViewManagerDelegate* delegate); | |
31 virtual ~ViewManagerSynchronizer(); | |
32 | |
33 bool connected() const { return connected_; } | |
34 ConnectionSpecificId connection_id() const { return connection_id_; } | |
35 | |
36 // API exposed to the node/view implementations that pushes local changes to | |
37 // the service. | |
38 Id CreateViewTreeNode(); | |
39 void DestroyViewTreeNode(Id node_id); | |
40 | |
41 Id CreateView(); | |
42 void DestroyView(Id view_id); | |
43 | |
44 // These methods take TransportIds. For views owned by the current connection, | |
45 // the connection id high word can be zero. In all cases, the TransportId 0x1 | |
46 // refers to the root node. | |
47 void AddChild(Id child_id, Id parent_id); | |
48 void RemoveChild(Id child_id, Id parent_id); | |
49 | |
50 void Reorder(Id node_id, Id relative_node_id, OrderDirection direction); | |
51 | |
52 // Returns true if the specified node/view was created by this connection. | |
53 bool OwnsNode(Id id) const; | |
54 bool OwnsView(Id id) const; | |
55 | |
56 void SetActiveView(Id node_id, Id view_id); | |
57 void SetBounds(Id node_id, const gfx::Rect& bounds); | |
58 void SetViewContents(Id view_id, const SkBitmap& contents); | |
59 void SetFocus(Id node_id); | |
60 | |
61 void Embed(const String& url, Id node_id); | |
62 | |
63 void set_changes_acked_callback(const base::Callback<void(void)>& callback) { | |
64 changes_acked_callback_ = callback; | |
65 } | |
66 void ClearChangesAckedCallback() { | |
67 changes_acked_callback_ = base::Callback<void(void)>(); | |
68 } | |
69 | |
70 // Start/stop tracking nodes & views. While tracked, they can be retrieved via | |
71 // ViewManager::GetNode/ViewById. | |
72 void AddNode(ViewTreeNode* node); | |
73 void RemoveNode(Id node_id); | |
74 | |
75 void AddView(View* view); | |
76 void RemoveView(Id view_id); | |
77 | |
78 private: | |
79 friend class RootObserver; | |
80 friend class ViewManagerTransaction; | |
81 | |
82 typedef ScopedVector<ViewManagerTransaction> Transactions; | |
83 typedef std::map<Id, ViewTreeNode*> IdToNodeMap; | |
84 typedef std::map<Id, View*> IdToViewMap; | |
85 typedef std::map<ConnectionSpecificId, | |
86 ViewManagerSynchronizer*> SynchronizerMap; | |
87 | |
88 // Overridden from ViewManager: | |
89 virtual const std::string& GetEmbedderURL() const OVERRIDE; | |
90 virtual const std::vector<ViewTreeNode*>& GetRoots() const OVERRIDE; | |
91 virtual ViewTreeNode* GetNodeById(Id id) OVERRIDE; | |
92 virtual View* GetViewById(Id id) OVERRIDE; | |
93 | |
94 // Overridden from InterfaceImpl: | |
95 virtual void OnConnectionEstablished() OVERRIDE; | |
96 | |
97 // Overridden from ViewManagerClient: | |
98 virtual void OnViewManagerConnectionEstablished( | |
99 ConnectionSpecificId connection_id, | |
100 const String& creator_url, | |
101 Id next_server_change_id, | |
102 Array<NodeDataPtr> nodes) OVERRIDE; | |
103 virtual void OnRootsAdded(Array<NodeDataPtr> nodes) OVERRIDE; | |
104 virtual void OnServerChangeIdAdvanced(Id next_server_change_id) OVERRIDE; | |
105 virtual void OnNodeBoundsChanged(Id node_id, | |
106 RectPtr old_bounds, | |
107 RectPtr new_bounds) OVERRIDE; | |
108 virtual void OnNodeHierarchyChanged(Id node_id, | |
109 Id new_parent_id, | |
110 Id old_parent_id, | |
111 Id server_change_id, | |
112 Array<NodeDataPtr> nodes) OVERRIDE; | |
113 virtual void OnNodeReordered(Id node_id, | |
114 Id relative_node_id, | |
115 OrderDirection direction, | |
116 Id server_change_id) OVERRIDE; | |
117 virtual void OnNodeDeleted(Id node_id, Id server_change_id) OVERRIDE; | |
118 virtual void OnNodeViewReplaced(Id node, | |
119 Id new_view_id, | |
120 Id old_view_id) OVERRIDE; | |
121 virtual void OnViewDeleted(Id view_id) OVERRIDE; | |
122 virtual void OnViewInputEvent(Id view, | |
123 EventPtr event, | |
124 const Callback<void()>& callback) OVERRIDE; | |
125 | |
126 // Sync the client model with the service by enumerating the pending | |
127 // transaction queue and applying them in order. | |
128 void Sync(); | |
129 | |
130 // Removes |transaction| from the pending queue. |transaction| must be at the | |
131 // front of the queue. | |
132 void RemoveFromPendingQueue(ViewManagerTransaction* transaction); | |
133 | |
134 void AddRoot(ViewTreeNode* root); | |
135 void RemoveRoot(ViewTreeNode* root); | |
136 | |
137 bool connected_; | |
138 ConnectionSpecificId connection_id_; | |
139 ConnectionSpecificId next_id_; | |
140 Id next_server_change_id_; | |
141 | |
142 std::string creator_url_; | |
143 | |
144 Transactions pending_transactions_; | |
145 | |
146 base::Callback<void(void)> changes_acked_callback_; | |
147 | |
148 ViewManagerDelegate* delegate_; | |
149 | |
150 std::vector<ViewTreeNode*> roots_; | |
151 | |
152 IdToNodeMap nodes_; | |
153 IdToViewMap views_; | |
154 | |
155 ViewManagerService* service_; | |
156 | |
157 DISALLOW_COPY_AND_ASSIGN(ViewManagerSynchronizer); | |
158 }; | |
159 | |
160 } // namespace view_manager | |
161 } // namespace mojo | |
162 | |
163 #endif // MOJO_SERVICES_PUBLIC_CPP_VIEW_MANAGER_LIB_VIEW_MANAGER_SYNCHRONIZER_H
_ | |
OLD | NEW |