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