James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 | |
James Robinson | 9b53c18 | 2014-12-02 11:00:50 -0800 | [diff] [blame] | 5 | #ifndef SERVICES_VIEW_MANAGER_VIEW_MANAGER_SERVICE_IMPL_H_ |
| 6 | #define SERVICES_VIEW_MANAGER_VIEW_MANAGER_SERVICE_IMPL_H_ |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 7 | |
| 8 | #include <set> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/basictypes.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 13 | #include "base/containers/hash_tables.h" |
| 14 | #include "base/memory/scoped_ptr.h" |
Viet-Trung Luu | 0f4f3ba | 2015-10-10 01:08:40 -0700 | [diff] [blame] | 15 | #include "mojo/services/surfaces/interfaces/surface_id.mojom.h" |
Viet-Trung Luu | 15a59a8 | 2015-10-10 01:11:00 -0700 | [diff] [blame] | 16 | #include "mojo/services/view_manager/interfaces/view_manager.mojom.h" |
James Robinson | 9b53c18 | 2014-12-02 11:00:50 -0800 | [diff] [blame] | 17 | #include "services/view_manager/access_policy_delegate.h" |
| 18 | #include "services/view_manager/ids.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 19 | |
| 20 | namespace gfx { |
| 21 | class Rect; |
| 22 | } |
| 23 | |
James Robinson | 100a9a7 | 2014-12-12 17:59:16 -0800 | [diff] [blame] | 24 | namespace view_manager { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 25 | |
| 26 | class AccessPolicy; |
| 27 | class ConnectionManager; |
| 28 | class ServerView; |
| 29 | |
Scott Violet | 5f5ce57 | 2014-11-13 18:01:22 -0800 | [diff] [blame] | 30 | // 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 Robinson | 100a9a7 | 2014-12-12 17:59:16 -0800 | [diff] [blame] | 34 | class ViewManagerServiceImpl : public mojo::ViewManagerService, |
Scott Violet | 5f5ce57 | 2014-11-13 18:01:22 -0800 | [diff] [blame] | 35 | public AccessPolicyDelegate { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 36 | public: |
James Robinson | 100a9a7 | 2014-12-12 17:59:16 -0800 | [diff] [blame] | 37 | using ViewIdSet = base::hash_set<mojo::Id>; |
Scott Violet | b36f757 | 2014-11-06 09:45:59 -0800 | [diff] [blame] | 38 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 39 | ViewManagerServiceImpl(ConnectionManager* connection_manager, |
James Robinson | 100a9a7 | 2014-12-12 17:59:16 -0800 | [diff] [blame] | 40 | mojo::ConnectionSpecificId creator_id, |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 41 | const std::string& creator_url, |
| 42 | const std::string& url, |
Scott Violet | 4bf7f06 | 2014-11-07 07:45:52 -0800 | [diff] [blame] | 43 | const ViewId& root_id); |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 44 | ~ViewManagerServiceImpl() override; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 45 | |
James Robinson | 3b67967 | 2015-01-21 18:36:01 -0800 | [diff] [blame] | 46 | // |services| and |exposed_services| are the ServiceProviders to pass to the |
| 47 | // client via OnEmbed(). |
James Robinson | 100a9a7 | 2014-12-12 17:59:16 -0800 | [diff] [blame] | 48 | void Init(mojo::ViewManagerClient* client, |
James Robinson | 2297c63 | 2015-02-06 11:06:34 -0800 | [diff] [blame] | 49 | mojo::ViewManagerServicePtr service_ptr, |
James Robinson | 3b67967 | 2015-01-21 18:36:01 -0800 | [diff] [blame] | 50 | mojo::InterfaceRequest<mojo::ServiceProvider> services, |
| 51 | mojo::ServiceProviderPtr exposed_services); |
Scott Violet | 4bf7f06 | 2014-11-07 07:45:52 -0800 | [diff] [blame] | 52 | |
James Robinson | 100a9a7 | 2014-12-12 17:59:16 -0800 | [diff] [blame] | 53 | mojo::ConnectionSpecificId id() const { return id_; } |
| 54 | mojo::ConnectionSpecificId creator_id() const { return creator_id_; } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 55 | const std::string& url() const { return url_; } |
| 56 | |
James Robinson | 100a9a7 | 2014-12-12 17:59:16 -0800 | [diff] [blame] | 57 | mojo::ViewManagerClient* client() { return client_; } |
Scott Violet | 5f5ce57 | 2014-11-13 18:01:22 -0800 | [diff] [blame] | 58 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 59 | // 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 Violet | a8ca772 | 2014-11-06 12:43:33 -0800 | [diff] [blame] | 66 | // Returns true if this connection's root is |id|. |
| 67 | bool IsRoot(const ViewId& id) const; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 68 | |
Scott Violet | 364f9e4 | 2014-11-19 10:58:18 -0800 | [diff] [blame] | 69 | // 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 Violet | b36f757 | 2014-11-06 09:45:59 -0800 | [diff] [blame] | 73 | // Invoked when a connection is about to be destroyed. |
| 74 | void OnWillDestroyViewManagerServiceImpl(ViewManagerServiceImpl* connection); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 75 | |
Scott Violet | 364f9e4 | 2014-11-19 10:58:18 -0800 | [diff] [blame] | 76 | // 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 Robinson | 100a9a7 | 2014-12-12 17:59:16 -0800 | [diff] [blame] | 79 | mojo::ErrorCode CreateView(const ViewId& view_id); |
Scott Violet | 364f9e4 | 2014-11-19 10:58:18 -0800 | [diff] [blame] | 80 | 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 Violet | 3b05b0b | 2015-02-09 12:25:31 -0800 | [diff] [blame] | 83 | 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 Violet | 7577711 | 2014-11-14 16:22:37 -0800 | [diff] [blame] | 88 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 89 | // 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 Seidel | 2b96156 | 2015-01-28 13:43:31 -0800 | [diff] [blame] | 96 | void ProcessViewportMetricsChanged(const mojo::ViewportMetrics& old_metrics, |
| 97 | const mojo::ViewportMetrics& new_metrics, |
| 98 | bool originated_change); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 99 | void ProcessWillChangeViewHierarchy(const ServerView* view, |
| 100 | const ServerView* new_parent, |
| 101 | const ServerView* old_parent, |
| 102 | bool originated_change); |
Elliot Glaysher | a74e2b3 | 2014-10-24 10:32:27 -0700 | [diff] [blame] | 103 | void ProcessViewPropertyChanged(const ServerView* view, |
| 104 | const std::string& name, |
| 105 | const std::vector<uint8_t>* new_data, |
| 106 | bool originated_change); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 107 | 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 Robinson | 100a9a7 | 2014-12-12 17:59:16 -0800 | [diff] [blame] | 113 | mojo::OrderDirection direction, |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 114 | bool originated_change); |
| 115 | void ProcessViewDeleted(const ViewId& view, bool originated_change); |
| 116 | void ProcessWillChangeViewVisibility(const ServerView* view, |
| 117 | bool originated_change); |
Elliot Glaysher | a74e2b3 | 2014-10-24 10:32:27 -0700 | [diff] [blame] | 118 | void ProcessViewPropertiesChanged(const ServerView* view, |
| 119 | bool originated_change); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 120 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 121 | private: |
James Robinson | 100a9a7 | 2014-12-12 17:59:16 -0800 | [diff] [blame] | 122 | typedef std::map<mojo::ConnectionSpecificId, ServerView*> ViewMap; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 123 | |
| 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 Robinson | 100a9a7 | 2014-12-12 17:59:16 -0800 | [diff] [blame] | 130 | mojo::OrderDirection direction) const; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 131 | |
| 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 Violet | a8ca772 | 2014-11-06 12:43:33 -0800 | [diff] [blame] | 147 | // Resets the root of this connection. |
| 148 | void RemoveRoot(); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 149 | |
| 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 Robinson | 100a9a7 | 2014-12-12 17:59:16 -0800 | [diff] [blame] | 155 | mojo::Array<mojo::ViewDataPtr> ViewsToViewDatas( |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 156 | const std::vector<const ServerView*>& views); |
James Robinson | 100a9a7 | 2014-12-12 17:59:16 -0800 | [diff] [blame] | 157 | mojo::ViewDataPtr ViewToViewData(const ServerView* view); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 158 | |
| 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 Violet | 0aa4fd2 | 2014-10-27 13:25:11 -0700 | [diff] [blame] | 168 | // Deletes all Views we own. |
| 169 | void DestroyViews(); |
| 170 | |
Scott Violet | 3b05b0b | 2015-02-09 12:25:31 -0800 | [diff] [blame] | 171 | bool PrepareForEmbed(const ViewId& view_id); |
| 172 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 173 | // ViewManagerService: |
James Robinson | 100a9a7 | 2014-12-12 17:59:16 -0800 | [diff] [blame] | 174 | 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 Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 199 | bool visible, |
James Robinson | 100a9a7 | 2014-12-12 17:59:16 -0800 | [diff] [blame] | 200 | 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 Violet | 3b05b0b | 2015-02-09 12:25:31 -0800 | [diff] [blame] | 205 | 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 Robinson | 100a9a7 | 2014-12-12 17:59:16 -0800 | [diff] [blame] | 212 | const mojo::Callback<void(bool)>& callback) override; |
Scott Violet | 6edabbf | 2015-02-25 16:02:50 -0800 | [diff] [blame] | 213 | void PerformAction(mojo::Id transport_view_id, |
| 214 | const mojo::String& action, |
| 215 | const mojo::Callback<void(bool)>& callback) override; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 216 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 217 | // AccessPolicyDelegate: |
Scott Violet | a8ca772 | 2014-11-06 12:43:33 -0800 | [diff] [blame] | 218 | bool IsRootForAccessPolicy(const ViewId& id) const override; |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 219 | bool IsViewKnownForAccessPolicy(const ServerView* view) const override; |
| 220 | bool IsViewRootOfAnotherConnectionForAccessPolicy( |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 221 | const ServerView* view) const override; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 222 | |
| 223 | ConnectionManager* connection_manager_; |
| 224 | |
| 225 | // Id of this connection as assigned by ConnectionManager. |
James Robinson | 100a9a7 | 2014-12-12 17:59:16 -0800 | [diff] [blame] | 226 | const mojo::ConnectionSpecificId id_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 227 | |
| 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 Robinson | 100a9a7 | 2014-12-12 17:59:16 -0800 | [diff] [blame] | 233 | mojo::ConnectionSpecificId creator_id_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 234 | |
| 235 | // The URL of the app that embedded the app this connection was created for. |
Scott Violet | 3b05b0b | 2015-02-09 12:25:31 -0800 | [diff] [blame] | 236 | // NOTE: this is empty if the connection was created by way of directly |
| 237 | // supplying the ViewManagerClient. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 238 | const std::string creator_url_; |
| 239 | |
James Robinson | 100a9a7 | 2014-12-12 17:59:16 -0800 | [diff] [blame] | 240 | mojo::ViewManagerClient* client_; |
Scott Violet | 5f5ce57 | 2014-11-13 18:01:22 -0800 | [diff] [blame] | 241 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 242 | scoped_ptr<AccessPolicy> access_policy_; |
| 243 | |
Scott Violet | 716bbdd | 2014-11-12 13:28:12 -0800 | [diff] [blame] | 244 | // The views created by this connection. This connection owns these objects. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 245 | ViewMap view_map_; |
| 246 | |
| 247 | // The set of views that has been communicated to the client. |
| 248 | ViewIdSet known_views_; |
| 249 | |
Scott Violet | a8ca772 | 2014-11-06 12:43:33 -0800 | [diff] [blame] | 250 | // 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 Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 254 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 255 | DISALLOW_COPY_AND_ASSIGN(ViewManagerServiceImpl); |
| 256 | }; |
| 257 | |
James Robinson | 100a9a7 | 2014-12-12 17:59:16 -0800 | [diff] [blame] | 258 | } // namespace view_manager |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 259 | |
James Robinson | 9b53c18 | 2014-12-02 11:00:50 -0800 | [diff] [blame] | 260 | #endif // SERVICES_VIEW_MANAGER_VIEW_MANAGER_SERVICE_IMPL_H_ |