blob: e0ac50738f343bf0e46197fe9414e9c7e9d2894d [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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
James Robinson9b53c182014-12-02 11:00:50 -08005#ifndef SERVICES_VIEW_MANAGER_VIEW_MANAGER_SERVICE_IMPL_H_
6#define SERVICES_VIEW_MANAGER_VIEW_MANAGER_SERVICE_IMPL_H_
James Robinson646469d2014-10-03 15:33:28 -07007
8#include <set>
9#include <string>
10#include <vector>
11
12#include "base/basictypes.h"
James Robinson646469d2014-10-03 15:33:28 -070013#include "base/containers/hash_tables.h"
14#include "base/memory/scoped_ptr.h"
Viet-Trung Luu0f4f3ba2015-10-10 01:08:40 -070015#include "mojo/services/surfaces/interfaces/surface_id.mojom.h"
Viet-Trung Luu15a59a82015-10-10 01:11:00 -070016#include "mojo/services/view_manager/interfaces/view_manager.mojom.h"
James Robinson9b53c182014-12-02 11:00:50 -080017#include "services/view_manager/access_policy_delegate.h"
18#include "services/view_manager/ids.h"
James Robinson646469d2014-10-03 15:33:28 -070019
20namespace gfx {
21class Rect;
22}
23
James Robinson100a9a72014-12-12 17:59:16 -080024namespace view_manager {
James Robinson646469d2014-10-03 15:33:28 -070025
26class AccessPolicy;
27class ConnectionManager;
28class ServerView;
29
Scott Violet5f5ce572014-11-13 18:01:22 -080030// An instance of ViewManagerServiceImpl is created for every ViewManagerService
31// request. ViewManagerServiceImpl tracks all the state and views created by a
32// client. ViewManagerServiceImpl coordinates with ConnectionManager to update
33// the client (and internal state) as necessary.
James Robinson100a9a72014-12-12 17:59:16 -080034class ViewManagerServiceImpl : public mojo::ViewManagerService,
Scott Violet5f5ce572014-11-13 18:01:22 -080035 public AccessPolicyDelegate {
James Robinson646469d2014-10-03 15:33:28 -070036 public:
James Robinson100a9a72014-12-12 17:59:16 -080037 using ViewIdSet = base::hash_set<mojo::Id>;
Scott Violetb36f7572014-11-06 09:45:59 -080038
James Robinson646469d2014-10-03 15:33:28 -070039 ViewManagerServiceImpl(ConnectionManager* connection_manager,
James Robinson100a9a72014-12-12 17:59:16 -080040 mojo::ConnectionSpecificId creator_id,
James Robinson646469d2014-10-03 15:33:28 -070041 const std::string& creator_url,
42 const std::string& url,
Scott Violet4bf7f062014-11-07 07:45:52 -080043 const ViewId& root_id);
James Robinsone1b30cf2014-10-21 12:25:40 -070044 ~ViewManagerServiceImpl() override;
James Robinson646469d2014-10-03 15:33:28 -070045
James Robinson3b679672015-01-21 18:36:01 -080046 // |services| and |exposed_services| are the ServiceProviders to pass to the
47 // client via OnEmbed().
James Robinson100a9a72014-12-12 17:59:16 -080048 void Init(mojo::ViewManagerClient* client,
James Robinson2297c632015-02-06 11:06:34 -080049 mojo::ViewManagerServicePtr service_ptr,
James Robinson3b679672015-01-21 18:36:01 -080050 mojo::InterfaceRequest<mojo::ServiceProvider> services,
51 mojo::ServiceProviderPtr exposed_services);
Scott Violet4bf7f062014-11-07 07:45:52 -080052
James Robinson100a9a72014-12-12 17:59:16 -080053 mojo::ConnectionSpecificId id() const { return id_; }
54 mojo::ConnectionSpecificId creator_id() const { return creator_id_; }
James Robinson646469d2014-10-03 15:33:28 -070055 const std::string& url() const { return url_; }
56
James Robinson100a9a72014-12-12 17:59:16 -080057 mojo::ViewManagerClient* client() { return client_; }
Scott Violet5f5ce572014-11-13 18:01:22 -080058
James Robinson646469d2014-10-03 15:33:28 -070059 // Returns the View with the specified id.
60 ServerView* GetView(const ViewId& id) {
61 return const_cast<ServerView*>(
62 const_cast<const ViewManagerServiceImpl*>(this)->GetView(id));
63 }
64 const ServerView* GetView(const ViewId& id) const;
65
Scott Violeta8ca7722014-11-06 12:43:33 -080066 // Returns true if this connection's root is |id|.
67 bool IsRoot(const ViewId& id) const;
James Robinson646469d2014-10-03 15:33:28 -070068
Scott Violet364f9e42014-11-19 10:58:18 -080069 // Returns the id of the root node. This is null if the root has been
70 // destroyed but the connection is still valid.
71 const ViewId* root() const { return root_.get(); }
72
Scott Violetb36f7572014-11-06 09:45:59 -080073 // Invoked when a connection is about to be destroyed.
74 void OnWillDestroyViewManagerServiceImpl(ViewManagerServiceImpl* connection);
James Robinson646469d2014-10-03 15:33:28 -070075
Scott Violet364f9e42014-11-19 10:58:18 -080076 // These functions are synchronous variants of those defined in the mojom. The
77 // ViewManagerService implementations all call into these. See the mojom for
78 // details.
James Robinson100a9a72014-12-12 17:59:16 -080079 mojo::ErrorCode CreateView(const ViewId& view_id);
Scott Violet364f9e42014-11-19 10:58:18 -080080 bool AddView(const ViewId& parent_id, const ViewId& child_id);
81 std::vector<const ServerView*> GetViewTree(const ViewId& view_id) const;
82 bool SetViewVisibility(const ViewId& view_id, bool visible);
Scott Violet3b05b0b2015-02-09 12:25:31 -080083 bool EmbedUrl(const std::string& url,
84 const ViewId& view_id,
85 mojo::InterfaceRequest<mojo::ServiceProvider> services,
86 mojo::ServiceProviderPtr exposed_services);
87 bool Embed(const ViewId& view_id, mojo::ViewManagerClientPtr client);
Scott Violet75777112014-11-14 16:22:37 -080088
James Robinson646469d2014-10-03 15:33:28 -070089 // The following methods are invoked after the corresponding change has been
90 // processed. They do the appropriate bookkeeping and update the client as
91 // necessary.
92 void ProcessViewBoundsChanged(const ServerView* view,
93 const gfx::Rect& old_bounds,
94 const gfx::Rect& new_bounds,
95 bool originated_change);
Eric Seidel2b961562015-01-28 13:43:31 -080096 void ProcessViewportMetricsChanged(const mojo::ViewportMetrics& old_metrics,
97 const mojo::ViewportMetrics& new_metrics,
98 bool originated_change);
James Robinson646469d2014-10-03 15:33:28 -070099 void ProcessWillChangeViewHierarchy(const ServerView* view,
100 const ServerView* new_parent,
101 const ServerView* old_parent,
102 bool originated_change);
Elliot Glayshera74e2b32014-10-24 10:32:27 -0700103 void ProcessViewPropertyChanged(const ServerView* view,
104 const std::string& name,
105 const std::vector<uint8_t>* new_data,
106 bool originated_change);
James Robinson646469d2014-10-03 15:33:28 -0700107 void ProcessViewHierarchyChanged(const ServerView* view,
108 const ServerView* new_parent,
109 const ServerView* old_parent,
110 bool originated_change);
111 void ProcessViewReorder(const ServerView* view,
112 const ServerView* relative_view,
James Robinson100a9a72014-12-12 17:59:16 -0800113 mojo::OrderDirection direction,
James Robinson646469d2014-10-03 15:33:28 -0700114 bool originated_change);
115 void ProcessViewDeleted(const ViewId& view, bool originated_change);
116 void ProcessWillChangeViewVisibility(const ServerView* view,
117 bool originated_change);
Elliot Glayshera74e2b32014-10-24 10:32:27 -0700118 void ProcessViewPropertiesChanged(const ServerView* view,
119 bool originated_change);
James Robinson646469d2014-10-03 15:33:28 -0700120
James Robinson646469d2014-10-03 15:33:28 -0700121 private:
James Robinson100a9a72014-12-12 17:59:16 -0800122 typedef std::map<mojo::ConnectionSpecificId, ServerView*> ViewMap;
James Robinson646469d2014-10-03 15:33:28 -0700123
124 bool IsViewKnown(const ServerView* view) const;
125
126 // These functions return true if the corresponding mojom function is allowed
127 // for this connection.
128 bool CanReorderView(const ServerView* view,
129 const ServerView* relative_view,
James Robinson100a9a72014-12-12 17:59:16 -0800130 mojo::OrderDirection direction) const;
James Robinson646469d2014-10-03 15:33:28 -0700131
132 // Deletes a view owned by this connection. Returns true on success. |source|
133 // is the connection that originated the change.
134 bool DeleteViewImpl(ViewManagerServiceImpl* source, ServerView* view);
135
136 // If |view| is known (in |known_views_|) does nothing. Otherwise adds |view|
137 // to |views|, marks |view| as known and recurses.
138 void GetUnknownViewsFrom(const ServerView* view,
139 std::vector<const ServerView*>* views);
140
141 // Removes |view| and all its descendants from |known_views_|. This does not
142 // recurse through views that were created by this connection. All views owned
143 // by this connection are added to |local_views|.
144 void RemoveFromKnown(const ServerView* view,
145 std::vector<ServerView*>* local_views);
146
Scott Violeta8ca7722014-11-06 12:43:33 -0800147 // Resets the root of this connection.
148 void RemoveRoot();
James Robinson646469d2014-10-03 15:33:28 -0700149
150 void RemoveChildrenAsPartOfEmbed(const ViewId& view_id);
151
152 // Converts View(s) to ViewData(s) for transport. This assumes all the views
153 // are valid for the client. The parent of views the client is not allowed to
154 // see are set to NULL (in the returned ViewData(s)).
James Robinson100a9a72014-12-12 17:59:16 -0800155 mojo::Array<mojo::ViewDataPtr> ViewsToViewDatas(
James Robinson646469d2014-10-03 15:33:28 -0700156 const std::vector<const ServerView*>& views);
James Robinson100a9a72014-12-12 17:59:16 -0800157 mojo::ViewDataPtr ViewToViewData(const ServerView* view);
James Robinson646469d2014-10-03 15:33:28 -0700158
159 // Implementation of GetViewTree(). Adds |view| to |views| and recurses if
160 // CanDescendIntoViewForViewTree() returns true.
161 void GetViewTreeImpl(const ServerView* view,
162 std::vector<const ServerView*>* views) const;
163
164 // Notify the client if the drawn state of any of the roots changes.
165 // |view| is the view that is changing to the drawn state |new_drawn_value|.
166 void NotifyDrawnStateChanged(const ServerView* view, bool new_drawn_value);
167
Scott Violet0aa4fd22014-10-27 13:25:11 -0700168 // Deletes all Views we own.
169 void DestroyViews();
170
Scott Violet3b05b0b2015-02-09 12:25:31 -0800171 bool PrepareForEmbed(const ViewId& view_id);
172
James Robinson646469d2014-10-03 15:33:28 -0700173 // ViewManagerService:
James Robinson100a9a72014-12-12 17:59:16 -0800174 void CreateView(
175 mojo::Id transport_view_id,
176 const mojo::Callback<void(mojo::ErrorCode)>& callback) override;
177 void DeleteView(mojo::Id transport_view_id,
178 const mojo::Callback<void(bool)>& callback) override;
179 void AddView(mojo::Id parent_id,
180 mojo::Id child_id,
181 const mojo::Callback<void(bool)>& callback) override;
182 void RemoveViewFromParent(
183 mojo::Id view_id,
184 const mojo::Callback<void(bool)>& callback) override;
185 void ReorderView(mojo::Id view_id,
186 mojo::Id relative_view_id,
187 mojo::OrderDirection direction,
188 const mojo::Callback<void(bool)>& callback) override;
189 void GetViewTree(mojo::Id view_id,
190 const mojo::Callback<void(mojo::Array<mojo::ViewDataPtr>)>&
191 callback) override;
192 void SetViewSurfaceId(mojo::Id view_id,
193 mojo::SurfaceIdPtr surface_id,
194 const mojo::Callback<void(bool)>& callback) override;
195 void SetViewBounds(mojo::Id view_id,
196 mojo::RectPtr bounds,
197 const mojo::Callback<void(bool)>& callback) override;
198 void SetViewVisibility(mojo::Id view_id,
James Robinsone1b30cf2014-10-21 12:25:40 -0700199 bool visible,
James Robinson100a9a72014-12-12 17:59:16 -0800200 const mojo::Callback<void(bool)>& callback) override;
201 void SetViewProperty(mojo::Id view_id,
202 const mojo::String& name,
203 mojo::Array<uint8_t> value,
204 const mojo::Callback<void(bool)>& callback) override;
Scott Violet3b05b0b2015-02-09 12:25:31 -0800205 void EmbedUrl(const mojo::String& url,
206 mojo::Id transport_view_id,
207 mojo::InterfaceRequest<mojo::ServiceProvider> services,
208 mojo::ServiceProviderPtr exposed_services,
209 const mojo::Callback<void(bool)>& callback) override;
210 void Embed(mojo::Id transport_view_id,
211 mojo::ViewManagerClientPtr client,
James Robinson100a9a72014-12-12 17:59:16 -0800212 const mojo::Callback<void(bool)>& callback) override;
Scott Violet6edabbf2015-02-25 16:02:50 -0800213 void PerformAction(mojo::Id transport_view_id,
214 const mojo::String& action,
215 const mojo::Callback<void(bool)>& callback) override;
James Robinson646469d2014-10-03 15:33:28 -0700216
James Robinson646469d2014-10-03 15:33:28 -0700217 // AccessPolicyDelegate:
Scott Violeta8ca7722014-11-06 12:43:33 -0800218 bool IsRootForAccessPolicy(const ViewId& id) const override;
James Robinsone1b30cf2014-10-21 12:25:40 -0700219 bool IsViewKnownForAccessPolicy(const ServerView* view) const override;
220 bool IsViewRootOfAnotherConnectionForAccessPolicy(
James Robinsonbaf71d32014-10-08 13:00:20 -0700221 const ServerView* view) const override;
James Robinson646469d2014-10-03 15:33:28 -0700222
223 ConnectionManager* connection_manager_;
224
225 // Id of this connection as assigned by ConnectionManager.
James Robinson100a9a72014-12-12 17:59:16 -0800226 const mojo::ConnectionSpecificId id_;
James Robinson646469d2014-10-03 15:33:28 -0700227
228 // URL this connection was created for.
229 const std::string url_;
230
231 // ID of the connection that created us. If 0 it indicates either we were
232 // created by the root, or the connection that created us has been destroyed.
James Robinson100a9a72014-12-12 17:59:16 -0800233 mojo::ConnectionSpecificId creator_id_;
James Robinson646469d2014-10-03 15:33:28 -0700234
235 // The URL of the app that embedded the app this connection was created for.
Scott Violet3b05b0b2015-02-09 12:25:31 -0800236 // NOTE: this is empty if the connection was created by way of directly
237 // supplying the ViewManagerClient.
James Robinson646469d2014-10-03 15:33:28 -0700238 const std::string creator_url_;
239
James Robinson100a9a72014-12-12 17:59:16 -0800240 mojo::ViewManagerClient* client_;
Scott Violet5f5ce572014-11-13 18:01:22 -0800241
James Robinson646469d2014-10-03 15:33:28 -0700242 scoped_ptr<AccessPolicy> access_policy_;
243
Scott Violet716bbdd2014-11-12 13:28:12 -0800244 // The views created by this connection. This connection owns these objects.
James Robinson646469d2014-10-03 15:33:28 -0700245 ViewMap view_map_;
246
247 // The set of views that has been communicated to the client.
248 ViewIdSet known_views_;
249
Scott Violeta8ca7722014-11-06 12:43:33 -0800250 // The root of this connection. This is a scoped_ptr to reinforce the
251 // connection may have no root. A connection has no root if either the root
252 // is destroyed or Embed() is invoked on the root.
253 scoped_ptr<ViewId> root_;
James Robinson646469d2014-10-03 15:33:28 -0700254
James Robinson646469d2014-10-03 15:33:28 -0700255 DISALLOW_COPY_AND_ASSIGN(ViewManagerServiceImpl);
256};
257
James Robinson100a9a72014-12-12 17:59:16 -0800258} // namespace view_manager
James Robinson646469d2014-10-03 15:33:28 -0700259
James Robinson9b53c182014-12-02 11:00:50 -0800260#endif // SERVICES_VIEW_MANAGER_VIEW_MANAGER_SERVICE_IMPL_H_