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_VIEW_MANAGER_SERVICE_IMPL_H_
6#define MOJO_SERVICES_VIEW_MANAGER_VIEW_MANAGER_SERVICE_IMPL_H_
7
8#include <set>
9#include <string>
10#include <vector>
11
12#include "base/basictypes.h"
13#include "base/compiler_specific.h"
14#include "base/containers/hash_tables.h"
15#include "base/memory/scoped_ptr.h"
16#include "mojo/services/public/interfaces/surfaces/surface_id.mojom.h"
17#include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h"
18#include "mojo/services/view_manager/access_policy_delegate.h"
19#include "mojo/services/view_manager/ids.h"
20#include "mojo/services/view_manager/view_manager_export.h"
21
22namespace gfx {
23class Rect;
24}
25
26namespace mojo {
27namespace service {
28
29class AccessPolicy;
30class ConnectionManager;
31class ServerView;
32
33#if defined(OS_WIN)
34// Equivalent of NON_EXPORTED_BASE which does not work with the template snafu
35// below.
36#pragma warning(push)
37#pragma warning(disable : 4275)
38#endif
39
40// Manages a connection from the client.
41class MOJO_VIEW_MANAGER_EXPORT ViewManagerServiceImpl
42    : public InterfaceImpl<ViewManagerService>,
43      public AccessPolicyDelegate {
44 public:
45  ViewManagerServiceImpl(ConnectionManager* connection_manager,
46                         ConnectionSpecificId creator_id,
47                         const std::string& creator_url,
48                         const std::string& url,
49                         const ViewId& root_id,
50                         InterfaceRequest<ServiceProvider> service_provider);
51  virtual ~ViewManagerServiceImpl();
52
53  // Used to mark this connection as originating from a call to
54  // ViewManagerService::Connect(). When set OnConnectionError() deletes |this|.
55  void set_delete_on_connection_error() { delete_on_connection_error_ = true; }
56
57  ConnectionSpecificId id() const { return id_; }
58  ConnectionSpecificId creator_id() const { return creator_id_; }
59  const std::string& url() const { return url_; }
60
61  // Returns the View with the specified id.
62  ServerView* GetView(const ViewId& id) {
63    return const_cast<ServerView*>(
64        const_cast<const ViewManagerServiceImpl*>(this)->GetView(id));
65  }
66  const ServerView* GetView(const ViewId& id) const;
67
68  // Returns true if this has |id| as a root.
69  bool HasRoot(const ViewId& id) const;
70
71  // Invoked when a connection is destroyed.
72  void OnViewManagerServiceImplDestroyed(ConnectionSpecificId id);
73
74  // The following methods are invoked after the corresponding change has been
75  // processed. They do the appropriate bookkeeping and update the client as
76  // necessary.
77  void ProcessViewBoundsChanged(const ServerView* view,
78                                const gfx::Rect& old_bounds,
79                                const gfx::Rect& new_bounds,
80                                bool originated_change);
81  void ProcessWillChangeViewHierarchy(const ServerView* view,
82                                      const ServerView* new_parent,
83                                      const ServerView* old_parent,
84                                      bool originated_change);
85  void ProcessViewHierarchyChanged(const ServerView* view,
86                                   const ServerView* new_parent,
87                                   const ServerView* old_parent,
88                                   bool originated_change);
89  void ProcessViewReorder(const ServerView* view,
90                          const ServerView* relative_view,
91                          OrderDirection direction,
92                          bool originated_change);
93  void ProcessViewDeleted(const ViewId& view, bool originated_change);
94  void ProcessWillChangeViewVisibility(const ServerView* view,
95                                       bool originated_change);
96
97  // TODO(sky): move this to private section (currently can't because of
98  // bindings).
99  // InterfaceImp overrides:
100  virtual void OnConnectionError() MOJO_OVERRIDE;
101
102 private:
103  typedef std::map<ConnectionSpecificId, ServerView*> ViewMap;
104  typedef base::hash_set<Id> ViewIdSet;
105
106  bool IsViewKnown(const ServerView* view) const;
107
108  // These functions return true if the corresponding mojom function is allowed
109  // for this connection.
110  bool CanReorderView(const ServerView* view,
111                      const ServerView* relative_view,
112                      OrderDirection direction) const;
113
114  // Deletes a view owned by this connection. Returns true on success. |source|
115  // is the connection that originated the change.
116  bool DeleteViewImpl(ViewManagerServiceImpl* source, ServerView* view);
117
118  // If |view| is known (in |known_views_|) does nothing. Otherwise adds |view|
119  // to |views|, marks |view| as known and recurses.
120  void GetUnknownViewsFrom(const ServerView* view,
121                           std::vector<const ServerView*>* views);
122
123  // Removes |view| and all its descendants from |known_views_|. This does not
124  // recurse through views that were created by this connection. All views owned
125  // by this connection are added to |local_views|.
126  void RemoveFromKnown(const ServerView* view,
127                       std::vector<ServerView*>* local_views);
128
129  // Removes |view_id| from the set of roots this connection knows about.
130  void RemoveRoot(const ViewId& view_id);
131
132  void RemoveChildrenAsPartOfEmbed(const ViewId& view_id);
133
134  // Converts View(s) to ViewData(s) for transport. This assumes all the views
135  // are valid for the client. The parent of views the client is not allowed to
136  // see are set to NULL (in the returned ViewData(s)).
137  Array<ViewDataPtr> ViewsToViewDatas(
138      const std::vector<const ServerView*>& views);
139  ViewDataPtr ViewToViewData(const ServerView* view);
140
141  // Implementation of GetViewTree(). Adds |view| to |views| and recurses if
142  // CanDescendIntoViewForViewTree() returns true.
143  void GetViewTreeImpl(const ServerView* view,
144                       std::vector<const ServerView*>* views) const;
145
146  // Notify the client if the drawn state of any of the roots changes.
147  // |view| is the view that is changing to the drawn state |new_drawn_value|.
148  void NotifyDrawnStateChanged(const ServerView* view, bool new_drawn_value);
149
150  // ViewManagerService:
151  virtual void CreateView(Id transport_view_id,
152                          const Callback<void(ErrorCode)>& callback) OVERRIDE;
153  virtual void DeleteView(Id transport_view_id,
154                          const Callback<void(bool)>& callback) OVERRIDE;
155  virtual void AddView(Id parent_id,
156                       Id child_id,
157                       const Callback<void(bool)>& callback) OVERRIDE;
158  virtual void RemoveViewFromParent(
159      Id view_id,
160      const Callback<void(bool)>& callback) OVERRIDE;
161  virtual void ReorderView(Id view_id,
162                           Id relative_view_id,
163                           OrderDirection direction,
164                           const Callback<void(bool)>& callback) OVERRIDE;
165  virtual void GetViewTree(
166      Id view_id,
167      const Callback<void(Array<ViewDataPtr>)>& callback) OVERRIDE;
168  virtual void SetViewSurfaceId(Id view_id,
169                                SurfaceIdPtr surface_id,
170                                const Callback<void(bool)>& callback) OVERRIDE;
171  virtual void SetViewBounds(Id view_id,
172                             RectPtr bounds,
173                             const Callback<void(bool)>& callback) OVERRIDE;
174  virtual void SetViewVisibility(Id view_id,
175                                 bool visible,
176                                 const Callback<void(bool)>& callback) OVERRIDE;
177  virtual void Embed(const String& url,
178                     Id view_id,
179                     ServiceProviderPtr service_provider,
180                     const Callback<void(bool)>& callback) OVERRIDE;
181  virtual void DispatchOnViewInputEvent(Id view_id, EventPtr event) OVERRIDE;
182
183  // InterfaceImpl:
184  virtual void OnConnectionEstablished() MOJO_OVERRIDE;
185
186  // AccessPolicyDelegate:
187  virtual const base::hash_set<Id>& GetRootsForAccessPolicy() const OVERRIDE;
188  virtual bool IsViewKnownForAccessPolicy(
189      const ServerView* view) const OVERRIDE;
190  virtual bool IsViewRootOfAnotherConnectionForAccessPolicy(
191      const ServerView* view) const OVERRIDE;
192
193  ConnectionManager* connection_manager_;
194
195  // Id of this connection as assigned by ConnectionManager.
196  const ConnectionSpecificId id_;
197
198  // URL this connection was created for.
199  const std::string url_;
200
201  // ID of the connection that created us. If 0 it indicates either we were
202  // created by the root, or the connection that created us has been destroyed.
203  ConnectionSpecificId creator_id_;
204
205  // The URL of the app that embedded the app this connection was created for.
206  const std::string creator_url_;
207
208  scoped_ptr<AccessPolicy> access_policy_;
209
210  // The views and views created by this connection. This connection owns these
211  // objects.
212  ViewMap view_map_;
213
214  // The set of views that has been communicated to the client.
215  ViewIdSet known_views_;
216
217  // Set of root views from other connections. More specifically any time
218  // Embed() is invoked the id of the view is added to this set for the child
219  // connection. The connection Embed() was invoked on (the parent) doesn't
220  // directly track which connections are attached to which of its views. That
221  // information can be found by looking through the |roots_| of all
222  // connections.
223  ViewIdSet roots_;
224
225  // See description above setter.
226  bool delete_on_connection_error_;
227
228  InterfaceRequest<ServiceProvider> service_provider_;
229
230  DISALLOW_COPY_AND_ASSIGN(ViewManagerServiceImpl);
231};
232
233#if defined(OS_WIN)
234#pragma warning(pop)
235#endif
236
237}  // namespace service
238}  // namespace mojo
239
240#endif  // MOJO_SERVICES_VIEW_MANAGER_VIEW_MANAGER_SERVICE_IMPL_H_
241