Shell / ShellClient -> ServiceProvider
[chromium-blink-merge.git] / mojo / services / view_manager / root_node_manager.h
blob3aed8bd450eac765d88bdd274df2a3e0aca66894
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.
5 #ifndef MOJO_SERVICES_VIEW_MANAGER_ROOT_NODE_MANAGER_H_
6 #define MOJO_SERVICES_VIEW_MANAGER_ROOT_NODE_MANAGER_H_
8 #include <map>
10 #include "base/basictypes.h"
11 #include "mojo/services/view_manager/ids.h"
12 #include "mojo/services/view_manager/node.h"
13 #include "mojo/services/view_manager/node_delegate.h"
14 #include "mojo/services/view_manager/root_view_manager.h"
15 #include "mojo/services/view_manager/view_manager_export.h"
17 namespace mojo {
19 class ServiceProvider;
21 namespace view_manager {
22 namespace service {
24 class View;
25 class ViewManagerConnection;
27 // RootNodeManager is responsible for managing the set of ViewManagerConnections
28 // as well as providing the root of the node hierarchy.
29 class MOJO_VIEW_MANAGER_EXPORT RootNodeManager : public NodeDelegate {
30 public:
31 // Used to indicate if the server id should be incremented after notifiying
32 // clients of the change.
33 enum ChangeType {
34 CHANGE_TYPE_ADVANCE_SERVER_CHANGE_ID,
35 CHANGE_TYPE_DONT_ADVANCE_SERVER_CHANGE_ID,
38 // Create when a ViewManagerConnection is about to make a change. Ensures
39 // clients are notified of the correct change id.
40 class ScopedChange {
41 public:
42 ScopedChange(ViewManagerConnection* connection,
43 RootNodeManager* root,
44 RootNodeManager::ChangeType change_type,
45 bool is_delete_node);
46 ~ScopedChange();
48 private:
49 RootNodeManager* root_;
50 const ChangeType change_type_;
52 DISALLOW_COPY_AND_ASSIGN(ScopedChange);
55 explicit RootNodeManager(ServiceProvider* service_provider);
56 virtual ~RootNodeManager();
58 // Returns the id for the next ViewManagerConnection.
59 TransportConnectionId GetAndAdvanceNextConnectionId();
61 TransportChangeId next_server_change_id() const {
62 return next_server_change_id_;
65 void AddConnection(ViewManagerConnection* connection);
66 void RemoveConnection(ViewManagerConnection* connection);
68 // Returns the connection by id.
69 ViewManagerConnection* GetConnection(TransportConnectionId connection_id);
71 // Returns the Node identified by |id|.
72 Node* GetNode(const NodeId& id);
74 // Returns the View identified by |id|.
75 View* GetView(const ViewId& id);
77 Node* root() { return &root_; }
79 bool IsProcessingChange() const { return change_source_ != 0; }
81 bool is_processing_delete_node() const { return is_processing_delete_node_; }
83 // These functions trivially delegate to all ViewManagerConnections, which in
84 // term notify their clients.
85 void ProcessNodeBoundsChanged(const Node* node,
86 const gfx::Rect& old_bounds,
87 const gfx::Rect& new_bounds);
88 void ProcessNodeHierarchyChanged(const Node* node,
89 const Node* new_parent,
90 const Node* old_parent);
91 void ProcessNodeViewReplaced(const Node* node,
92 const View* new_view_id,
93 const View* old_view_id);
94 void ProcessNodeDeleted(const NodeId& node);
95 void ProcessViewDeleted(const ViewId& view);
97 private:
98 // Used to setup any static state needed by RootNodeManager.
99 struct Context {
100 Context();
101 ~Context();
104 typedef std::map<TransportConnectionId, ViewManagerConnection*> ConnectionMap;
106 // Invoked when a particular connection is about to make a change.
107 // Subsequently followed by FinishChange() once the change is done.
109 // Changes should never nest, meaning each PrepareForChange() must be
110 // balanced with a call to FinishChange() with no PrepareForChange()
111 // in between.
112 void PrepareForChange(ViewManagerConnection* connection, bool is_delete_node);
114 // Balances a call to PrepareForChange().
115 void FinishChange(ChangeType change_type);
117 // Returns true if the specified connection originated the current change.
118 bool IsChangeSource(TransportConnectionId connection_id) const {
119 return connection_id == change_source_;
122 // Overridden from NodeDelegate:
123 virtual void OnNodeHierarchyChanged(const Node* node,
124 const Node* new_parent,
125 const Node* old_parent) OVERRIDE;
126 virtual void OnNodeViewReplaced(const Node* node,
127 const View* new_view,
128 const View* old_view) OVERRIDE;
130 Context context_;
132 // ID to use for next ViewManagerConnection.
133 TransportConnectionId next_connection_id_;
135 TransportChangeId next_server_change_id_;
137 // Set of ViewManagerConnections.
138 ConnectionMap connection_map_;
140 // If non-zero we're processing a change from this client.
141 TransportConnectionId change_source_;
143 // True if we're processing a DeleteNode request.
144 bool is_processing_delete_node_;
146 RootViewManager root_view_manager_;
148 // Root node.
149 Node root_;
151 DISALLOW_COPY_AND_ASSIGN(RootNodeManager);
154 } // namespace service
155 } // namespace view_manager
156 } // namespace mojo
158 #endif // MOJO_SERVICES_VIEW_MANAGER_ROOT_NODE_MANAGER_H_