| OLD | NEW |
| (Empty) |
| 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/containers/hash_tables.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "mojo/services/public/interfaces/surfaces/surface_id.mojom.h" | |
| 16 #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h" | |
| 17 #include "mojo/services/view_manager/access_policy_delegate.h" | |
| 18 #include "mojo/services/view_manager/ids.h" | |
| 19 | |
| 20 namespace gfx { | |
| 21 class Rect; | |
| 22 } | |
| 23 | |
| 24 namespace mojo { | |
| 25 namespace service { | |
| 26 | |
| 27 class AccessPolicy; | |
| 28 class ConnectionManager; | |
| 29 class ServerView; | |
| 30 | |
| 31 // An instance of ViewManagerServiceImpl is created for every ViewManagerService | |
| 32 // request. ViewManagerServiceImpl tracks all the state and views created by a | |
| 33 // client. ViewManagerServiceImpl coordinates with ConnectionManager to update | |
| 34 // the client (and internal state) as necessary. | |
| 35 class ViewManagerServiceImpl : public ViewManagerService, | |
| 36 public AccessPolicyDelegate { | |
| 37 public: | |
| 38 using ViewIdSet = base::hash_set<Id>; | |
| 39 | |
| 40 ViewManagerServiceImpl(ConnectionManager* connection_manager, | |
| 41 ConnectionSpecificId creator_id, | |
| 42 const std::string& creator_url, | |
| 43 const std::string& url, | |
| 44 const ViewId& root_id); | |
| 45 ~ViewManagerServiceImpl() override; | |
| 46 | |
| 47 // |service_provider| is the ServiceProvider to pass to the client via | |
| 48 // OnEmbed(). | |
| 49 void Init(ViewManagerClient* client, | |
| 50 InterfaceRequest<ServiceProvider> service_provider); | |
| 51 | |
| 52 ConnectionSpecificId id() const { return id_; } | |
| 53 ConnectionSpecificId creator_id() const { return creator_id_; } | |
| 54 const std::string& url() const { return url_; } | |
| 55 | |
| 56 ViewManagerClient* client() { return client_; } | |
| 57 | |
| 58 // Returns the View with the specified id. | |
| 59 ServerView* GetView(const ViewId& id) { | |
| 60 return const_cast<ServerView*>( | |
| 61 const_cast<const ViewManagerServiceImpl*>(this)->GetView(id)); | |
| 62 } | |
| 63 const ServerView* GetView(const ViewId& id) const; | |
| 64 | |
| 65 // Returns true if this connection's root is |id|. | |
| 66 bool IsRoot(const ViewId& id) const; | |
| 67 | |
| 68 // Returns the id of the root node. This is null if the root has been | |
| 69 // destroyed but the connection is still valid. | |
| 70 const ViewId* root() const { return root_.get(); } | |
| 71 | |
| 72 // Invoked when a connection is about to be destroyed. | |
| 73 void OnWillDestroyViewManagerServiceImpl(ViewManagerServiceImpl* connection); | |
| 74 | |
| 75 // These functions are synchronous variants of those defined in the mojom. The | |
| 76 // ViewManagerService implementations all call into these. See the mojom for | |
| 77 // details. | |
| 78 ErrorCode CreateView(const ViewId& view_id); | |
| 79 bool AddView(const ViewId& parent_id, const ViewId& child_id); | |
| 80 std::vector<const ServerView*> GetViewTree(const ViewId& view_id) const; | |
| 81 bool SetViewVisibility(const ViewId& view_id, bool visible); | |
| 82 bool Embed(const std::string& url, | |
| 83 const ViewId& view_id, | |
| 84 InterfaceRequest<ServiceProvider> service_provider); | |
| 85 | |
| 86 // The following methods are invoked after the corresponding change has been | |
| 87 // processed. They do the appropriate bookkeeping and update the client as | |
| 88 // necessary. | |
| 89 void ProcessViewBoundsChanged(const ServerView* view, | |
| 90 const gfx::Rect& old_bounds, | |
| 91 const gfx::Rect& new_bounds, | |
| 92 bool originated_change); | |
| 93 void ProcessWillChangeViewHierarchy(const ServerView* view, | |
| 94 const ServerView* new_parent, | |
| 95 const ServerView* old_parent, | |
| 96 bool originated_change); | |
| 97 void ProcessViewPropertyChanged(const ServerView* view, | |
| 98 const std::string& name, | |
| 99 const std::vector<uint8_t>* new_data, | |
| 100 bool originated_change); | |
| 101 void ProcessViewHierarchyChanged(const ServerView* view, | |
| 102 const ServerView* new_parent, | |
| 103 const ServerView* old_parent, | |
| 104 bool originated_change); | |
| 105 void ProcessViewReorder(const ServerView* view, | |
| 106 const ServerView* relative_view, | |
| 107 OrderDirection direction, | |
| 108 bool originated_change); | |
| 109 void ProcessViewDeleted(const ViewId& view, bool originated_change); | |
| 110 void ProcessWillChangeViewVisibility(const ServerView* view, | |
| 111 bool originated_change); | |
| 112 void ProcessViewPropertiesChanged(const ServerView* view, | |
| 113 bool originated_change); | |
| 114 | |
| 115 private: | |
| 116 typedef std::map<ConnectionSpecificId, ServerView*> ViewMap; | |
| 117 | |
| 118 bool IsViewKnown(const ServerView* view) const; | |
| 119 | |
| 120 // These functions return true if the corresponding mojom function is allowed | |
| 121 // for this connection. | |
| 122 bool CanReorderView(const ServerView* view, | |
| 123 const ServerView* relative_view, | |
| 124 OrderDirection direction) const; | |
| 125 | |
| 126 // Deletes a view owned by this connection. Returns true on success. |source| | |
| 127 // is the connection that originated the change. | |
| 128 bool DeleteViewImpl(ViewManagerServiceImpl* source, ServerView* view); | |
| 129 | |
| 130 // If |view| is known (in |known_views_|) does nothing. Otherwise adds |view| | |
| 131 // to |views|, marks |view| as known and recurses. | |
| 132 void GetUnknownViewsFrom(const ServerView* view, | |
| 133 std::vector<const ServerView*>* views); | |
| 134 | |
| 135 // Removes |view| and all its descendants from |known_views_|. This does not | |
| 136 // recurse through views that were created by this connection. All views owned | |
| 137 // by this connection are added to |local_views|. | |
| 138 void RemoveFromKnown(const ServerView* view, | |
| 139 std::vector<ServerView*>* local_views); | |
| 140 | |
| 141 // Resets the root of this connection. | |
| 142 void RemoveRoot(); | |
| 143 | |
| 144 void RemoveChildrenAsPartOfEmbed(const ViewId& view_id); | |
| 145 | |
| 146 // Converts View(s) to ViewData(s) for transport. This assumes all the views | |
| 147 // are valid for the client. The parent of views the client is not allowed to | |
| 148 // see are set to NULL (in the returned ViewData(s)). | |
| 149 Array<ViewDataPtr> ViewsToViewDatas( | |
| 150 const std::vector<const ServerView*>& views); | |
| 151 ViewDataPtr ViewToViewData(const ServerView* view); | |
| 152 | |
| 153 // Implementation of GetViewTree(). Adds |view| to |views| and recurses if | |
| 154 // CanDescendIntoViewForViewTree() returns true. | |
| 155 void GetViewTreeImpl(const ServerView* view, | |
| 156 std::vector<const ServerView*>* views) const; | |
| 157 | |
| 158 // Notify the client if the drawn state of any of the roots changes. | |
| 159 // |view| is the view that is changing to the drawn state |new_drawn_value|. | |
| 160 void NotifyDrawnStateChanged(const ServerView* view, bool new_drawn_value); | |
| 161 | |
| 162 // Deletes all Views we own. | |
| 163 void DestroyViews(); | |
| 164 | |
| 165 // ViewManagerService: | |
| 166 void CreateView(Id transport_view_id, | |
| 167 const Callback<void(ErrorCode)>& callback) override; | |
| 168 void DeleteView(Id transport_view_id, | |
| 169 const Callback<void(bool)>& callback) override; | |
| 170 void AddView(Id parent_id, | |
| 171 Id child_id, | |
| 172 const Callback<void(bool)>& callback) override; | |
| 173 void RemoveViewFromParent(Id view_id, | |
| 174 const Callback<void(bool)>& callback) override; | |
| 175 void ReorderView(Id view_id, | |
| 176 Id relative_view_id, | |
| 177 OrderDirection direction, | |
| 178 const Callback<void(bool)>& callback) override; | |
| 179 void GetViewTree(Id view_id, | |
| 180 const Callback<void(Array<ViewDataPtr>)>& callback) override; | |
| 181 void SetViewSurfaceId(Id view_id, | |
| 182 SurfaceIdPtr surface_id, | |
| 183 const Callback<void(bool)>& callback) override; | |
| 184 void SetViewBounds(Id view_id, | |
| 185 RectPtr bounds, | |
| 186 const Callback<void(bool)>& callback) override; | |
| 187 void SetViewVisibility(Id view_id, | |
| 188 bool visible, | |
| 189 const Callback<void(bool)>& callback) override; | |
| 190 void SetViewProperty(Id view_id, | |
| 191 const String& name, | |
| 192 Array<uint8_t> value, | |
| 193 const Callback<void(bool)>& callback) override; | |
| 194 void Embed(const String& url, | |
| 195 Id view_id, | |
| 196 InterfaceRequest<ServiceProvider> service_provider, | |
| 197 const Callback<void(bool)>& callback) override; | |
| 198 | |
| 199 // AccessPolicyDelegate: | |
| 200 bool IsRootForAccessPolicy(const ViewId& id) const override; | |
| 201 bool IsViewKnownForAccessPolicy(const ServerView* view) const override; | |
| 202 bool IsViewRootOfAnotherConnectionForAccessPolicy( | |
| 203 const ServerView* view) const override; | |
| 204 | |
| 205 ConnectionManager* connection_manager_; | |
| 206 | |
| 207 // Id of this connection as assigned by ConnectionManager. | |
| 208 const ConnectionSpecificId id_; | |
| 209 | |
| 210 // URL this connection was created for. | |
| 211 const std::string url_; | |
| 212 | |
| 213 // ID of the connection that created us. If 0 it indicates either we were | |
| 214 // created by the root, or the connection that created us has been destroyed. | |
| 215 ConnectionSpecificId creator_id_; | |
| 216 | |
| 217 // The URL of the app that embedded the app this connection was created for. | |
| 218 const std::string creator_url_; | |
| 219 | |
| 220 ViewManagerClient* client_; | |
| 221 | |
| 222 scoped_ptr<AccessPolicy> access_policy_; | |
| 223 | |
| 224 // The views created by this connection. This connection owns these objects. | |
| 225 ViewMap view_map_; | |
| 226 | |
| 227 // The set of views that has been communicated to the client. | |
| 228 ViewIdSet known_views_; | |
| 229 | |
| 230 // The root of this connection. This is a scoped_ptr to reinforce the | |
| 231 // connection may have no root. A connection has no root if either the root | |
| 232 // is destroyed or Embed() is invoked on the root. | |
| 233 scoped_ptr<ViewId> root_; | |
| 234 | |
| 235 DISALLOW_COPY_AND_ASSIGN(ViewManagerServiceImpl); | |
| 236 }; | |
| 237 | |
| 238 } // namespace service | |
| 239 } // namespace mojo | |
| 240 | |
| 241 #endif // MOJO_SERVICES_VIEW_MANAGER_VIEW_MANAGER_SERVICE_IMPL_H_ | |
| OLD | NEW |