• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CLIENT_IMPL_H_
6 #define MOJO_SERVICES_PUBLIC_CPP_VIEW_MANAGER_LIB_VIEW_MANAGER_CLIENT_IMPL_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/node.h"
14 #include "mojo/services/public/cpp/view_manager/types.h"
15 #include "mojo/services/public/cpp/view_manager/view_manager.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 ViewManagerClientImpl : public ViewManager,
28                               public InterfaceImpl<ViewManagerClient> {
29  public:
30   explicit ViewManagerClientImpl(ViewManagerDelegate* delegate);
31   virtual ~ViewManagerClientImpl();
32 
connected()33   bool connected() const { return connected_; }
connection_id()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 CreateNode();
39   void DestroyNode(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 
set_changes_acked_callback(const base::Callback<void (void)> & callback)63   void set_changes_acked_callback(const base::Callback<void(void)>& callback) {
64     changes_acked_callback_ = callback;
65   }
ClearChangesAckedCallback()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(Node* 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, Node*> IdToNodeMap;
84   typedef std::map<Id, View*> IdToViewMap;
85 
86   // Overridden from ViewManager:
87   virtual const std::string& GetEmbedderURL() const OVERRIDE;
88   virtual const std::vector<Node*>& GetRoots() const OVERRIDE;
89   virtual Node* GetNodeById(Id id) OVERRIDE;
90   virtual View* GetViewById(Id id) OVERRIDE;
91 
92   // Overridden from InterfaceImpl:
93   virtual void OnConnectionEstablished() OVERRIDE;
94 
95   // Overridden from ViewManagerClient:
96   virtual void OnViewManagerConnectionEstablished(
97       ConnectionSpecificId connection_id,
98       const String& creator_url,
99       Id next_server_change_id,
100       Array<NodeDataPtr> nodes) OVERRIDE;
101   virtual void OnRootsAdded(Array<NodeDataPtr> nodes) OVERRIDE;
102   virtual void OnServerChangeIdAdvanced(Id next_server_change_id) OVERRIDE;
103   virtual void OnNodeBoundsChanged(Id node_id,
104                                    RectPtr old_bounds,
105                                    RectPtr new_bounds) OVERRIDE;
106   virtual void OnNodeHierarchyChanged(Id node_id,
107                                       Id new_parent_id,
108                                       Id old_parent_id,
109                                       Id server_change_id,
110                                       Array<NodeDataPtr> nodes) OVERRIDE;
111   virtual void OnNodeReordered(Id node_id,
112                                Id relative_node_id,
113                                OrderDirection direction,
114                                Id server_change_id) OVERRIDE;
115   virtual void OnNodeDeleted(Id node_id, Id server_change_id) OVERRIDE;
116   virtual void OnNodeViewReplaced(Id node,
117                                   Id new_view_id,
118                                   Id old_view_id) OVERRIDE;
119   virtual void OnViewDeleted(Id view_id) OVERRIDE;
120   virtual void OnViewInputEvent(Id view,
121                                 EventPtr event,
122                                 const Callback<void()>& callback) OVERRIDE;
123   virtual void DispatchOnViewInputEvent(Id view_id, EventPtr event) OVERRIDE;
124 
125   // Sync the client model with the service by enumerating the pending
126   // transaction queue and applying them in order.
127   void Sync();
128 
129   // Removes |transaction| from the pending queue. |transaction| must be at the
130   // front of the queue.
131   void RemoveFromPendingQueue(ViewManagerTransaction* transaction);
132 
133   void AddRoot(Node* root);
134   void RemoveRoot(Node* root);
135 
136   bool connected_;
137   ConnectionSpecificId connection_id_;
138   ConnectionSpecificId next_id_;
139   Id next_server_change_id_;
140 
141   std::string creator_url_;
142 
143   Transactions pending_transactions_;
144 
145   base::Callback<void(void)> changes_acked_callback_;
146 
147   ViewManagerDelegate* delegate_;
148 
149   std::vector<Node*> roots_;
150 
151   IdToNodeMap nodes_;
152   IdToViewMap views_;
153 
154   ViewManagerService* service_;
155 
156   DISALLOW_COPY_AND_ASSIGN(ViewManagerClientImpl);
157 };
158 
159 }  // namespace view_manager
160 }  // namespace mojo
161 
162 #endif  // MOJO_SERVICES_PUBLIC_CPP_VIEW_MANAGER_LIB_VIEW_MANAGER_CLIENT_IMPL_H_
163