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 SERVICES_VIEW_MANAGER_VIEW_MANAGER_SERVICE_IMPL_H_ |
| 6 #define 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/view_manager/access_policy_delegate.h" |
| 16 #include "mojo/services/view_manager/ids.h" |
| 17 #include "third_party/mojo_services/src/surfaces/public/interfaces/surface_id.mo
jom.h" |
| 18 #include "third_party/mojo_services/src/view_manager/public/interfaces/view_mana
ger.mojom.h" |
| 19 |
| 20 namespace gfx { |
| 21 class Rect; |
| 22 } |
| 23 |
| 24 namespace view_manager { |
| 25 |
| 26 class AccessPolicy; |
| 27 class ConnectionManager; |
| 28 class ServerView; |
| 29 |
| 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. |
| 34 class ViewManagerServiceImpl : public mojo::ViewManagerService, |
| 35 public AccessPolicyDelegate { |
| 36 public: |
| 37 using ViewIdSet = base::hash_set<mojo::Id>; |
| 38 |
| 39 ViewManagerServiceImpl(ConnectionManager* connection_manager, |
| 40 mojo::ConnectionSpecificId creator_id, |
| 41 const std::string& creator_url, |
| 42 const std::string& url, |
| 43 const ViewId& root_id); |
| 44 ~ViewManagerServiceImpl() override; |
| 45 |
| 46 // |services| and |exposed_services| are the ServiceProviders to pass to the |
| 47 // client via OnEmbed(). |
| 48 void Init(mojo::ViewManagerClient* client, |
| 49 mojo::ViewManagerServicePtr service_ptr, |
| 50 mojo::InterfaceRequest<mojo::ServiceProvider> services, |
| 51 mojo::ServiceProviderPtr exposed_services); |
| 52 |
| 53 mojo::ConnectionSpecificId id() const { return id_; } |
| 54 mojo::ConnectionSpecificId creator_id() const { return creator_id_; } |
| 55 const std::string& url() const { return url_; } |
| 56 |
| 57 mojo::ViewManagerClient* client() { return client_; } |
| 58 |
| 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 |
| 66 // Returns true if this connection's root is |id|. |
| 67 bool IsRoot(const ViewId& id) const; |
| 68 |
| 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 |
| 73 // Invoked when a connection is about to be destroyed. |
| 74 void OnWillDestroyViewManagerServiceImpl(ViewManagerServiceImpl* connection); |
| 75 |
| 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. |
| 79 mojo::ErrorCode CreateView(const ViewId& view_id); |
| 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); |
| 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); |
| 88 |
| 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); |
| 96 void ProcessViewportMetricsChanged(const mojo::ViewportMetrics& old_metrics, |
| 97 const mojo::ViewportMetrics& new_metrics, |
| 98 bool originated_change); |
| 99 void ProcessWillChangeViewHierarchy(const ServerView* view, |
| 100 const ServerView* new_parent, |
| 101 const ServerView* old_parent, |
| 102 bool originated_change); |
| 103 void ProcessViewPropertyChanged(const ServerView* view, |
| 104 const std::string& name, |
| 105 const std::vector<uint8_t>* new_data, |
| 106 bool originated_change); |
| 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, |
| 113 mojo::OrderDirection direction, |
| 114 bool originated_change); |
| 115 void ProcessViewDeleted(const ViewId& view, bool originated_change); |
| 116 void ProcessWillChangeViewVisibility(const ServerView* view, |
| 117 bool originated_change); |
| 118 void ProcessViewPropertiesChanged(const ServerView* view, |
| 119 bool originated_change); |
| 120 |
| 121 private: |
| 122 typedef std::map<mojo::ConnectionSpecificId, ServerView*> ViewMap; |
| 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, |
| 130 mojo::OrderDirection direction) const; |
| 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 |
| 147 // Resets the root of this connection. |
| 148 void RemoveRoot(); |
| 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)). |
| 155 mojo::Array<mojo::ViewDataPtr> ViewsToViewDatas( |
| 156 const std::vector<const ServerView*>& views); |
| 157 mojo::ViewDataPtr ViewToViewData(const ServerView* view); |
| 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 |
| 168 // Deletes all Views we own. |
| 169 void DestroyViews(); |
| 170 |
| 171 bool PrepareForEmbed(const ViewId& view_id); |
| 172 |
| 173 // ViewManagerService: |
| 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, |
| 199 bool visible, |
| 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; |
| 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, |
| 212 const mojo::Callback<void(bool)>& callback) override; |
| 213 void PerformAction(mojo::Id transport_view_id, |
| 214 const mojo::String& action, |
| 215 const mojo::Callback<void(bool)>& callback) override; |
| 216 |
| 217 // AccessPolicyDelegate: |
| 218 bool IsRootForAccessPolicy(const ViewId& id) const override; |
| 219 bool IsViewKnownForAccessPolicy(const ServerView* view) const override; |
| 220 bool IsViewRootOfAnotherConnectionForAccessPolicy( |
| 221 const ServerView* view) const override; |
| 222 |
| 223 ConnectionManager* connection_manager_; |
| 224 |
| 225 // Id of this connection as assigned by ConnectionManager. |
| 226 const mojo::ConnectionSpecificId id_; |
| 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. |
| 233 mojo::ConnectionSpecificId creator_id_; |
| 234 |
| 235 // The URL of the app that embedded the app this connection was created for. |
| 236 // NOTE: this is empty if the connection was created by way of directly |
| 237 // supplying the ViewManagerClient. |
| 238 const std::string creator_url_; |
| 239 |
| 240 mojo::ViewManagerClient* client_; |
| 241 |
| 242 scoped_ptr<AccessPolicy> access_policy_; |
| 243 |
| 244 // The views created by this connection. This connection owns these objects. |
| 245 ViewMap view_map_; |
| 246 |
| 247 // The set of views that has been communicated to the client. |
| 248 ViewIdSet known_views_; |
| 249 |
| 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_; |
| 254 |
| 255 DISALLOW_COPY_AND_ASSIGN(ViewManagerServiceImpl); |
| 256 }; |
| 257 |
| 258 } // namespace view_manager |
| 259 |
| 260 #endif // SERVICES_VIEW_MANAGER_VIEW_MANAGER_SERVICE_IMPL_H_ |
OLD | NEW |