Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / mojo / services / public / cpp / view_manager / lib / view_manager_client_impl.h
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_PUBLIC_CPP_VIEW_MANAGER_LIB_VIEW_MANAGER_CLIENT_IMPL_H_
6 #define MOJO_SERVICES_PUBLIC_CPP_VIEW_MANAGER_LIB_VIEW_MANAGER_CLIENT_IMPL_H_
7
8 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/memory/scoped_vector.h"
11 #include "base/memory/weak_ptr.h"
12 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h"
13 #include "mojo/services/public/cpp/view_manager/types.h"
14 #include "mojo/services/public/cpp/view_manager/view.h"
15 #include "mojo/services/public/cpp/view_manager/view_manager.h"
16 #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h"
17 #include "mojo/services/public/interfaces/window_manager/window_manager.mojom.h"
18
19 class SkBitmap;
20
21 namespace mojo {
22 class ViewManager;
23 class ViewManagerDelegate;
24 class ViewManagerTransaction;
25 class Shell;
26
27 // Manages the connection with the View Manager service.
28 class ViewManagerClientImpl : public ViewManager,
29                               public InterfaceImpl<ViewManagerClient>,
30                               public WindowManagerClient {
31  public:
32   ViewManagerClientImpl(ViewManagerDelegate* delegate, Shell* shell);
33   virtual ~ViewManagerClientImpl();
34
35   bool connected() const { return connected_; }
36   ConnectionSpecificId connection_id() const { return connection_id_; }
37
38   // API exposed to the view implementations that pushes local changes to the
39   // service.
40   Id CreateView();
41   void DestroyView(Id view_id);
42
43   // These methods take TransportIds. For views owned by the current connection,
44   // the connection id high word can be zero. In all cases, the TransportId 0x1
45   // refers to the root view.
46   void AddChild(Id child_id, Id parent_id);
47   void RemoveChild(Id child_id, Id parent_id);
48
49   void Reorder(Id view_id, Id relative_view_id, OrderDirection direction);
50
51   // Returns true if the specified view was created by this connection.
52   bool OwnsView(Id id) const;
53
54   void SetBounds(Id view_id, const gfx::Rect& bounds);
55   void SetSurfaceId(Id view_id, SurfaceIdPtr surface_id);
56   void SetFocus(Id view_id);
57   void SetVisible(Id view_id, bool visible);
58
59   void Embed(const String& url, Id view_id);
60   void Embed(const String& url,
61              Id view_id,
62              ServiceProviderPtr service_provider);
63
64   void set_change_acked_callback(const base::Callback<void(void)>& callback) {
65     change_acked_callback_ = callback;
66   }
67   void ClearChangeAckedCallback() {
68     change_acked_callback_ = base::Callback<void(void)>();
69   }
70
71   // Start/stop tracking views. While tracked, they can be retrieved via
72   // ViewManager::GetViewById.
73   void AddView(View* view);
74   void RemoveView(Id view_id);
75
76   Shell* shell() { return shell_; }
77
78  private:
79   friend class RootObserver;
80
81   typedef std::map<Id, View*> IdToViewMap;
82
83   // Overridden from ViewManager:
84   virtual void SetWindowManagerDelegate(
85       WindowManagerDelegate* delegate) OVERRIDE;
86   virtual void DispatchEvent(View* target, EventPtr event) OVERRIDE;
87   virtual const std::string& GetEmbedderURL() const OVERRIDE;
88   virtual const std::vector<View*>& GetRoots() const OVERRIDE;
89   virtual View* GetViewById(Id id) OVERRIDE;
90
91   // Overridden from InterfaceImpl:
92   virtual void OnConnectionEstablished() OVERRIDE;
93
94   // Overridden from ViewManagerClient:
95   virtual void OnEmbed(ConnectionSpecificId connection_id,
96                        const String& creator_url,
97                        ViewDataPtr root,
98                        InterfaceRequest<ServiceProvider> services) OVERRIDE;
99   virtual void OnViewBoundsChanged(Id view_id,
100                                    RectPtr old_bounds,
101                                    RectPtr new_bounds) OVERRIDE;
102   virtual void OnViewHierarchyChanged(Id view_id,
103                                       Id new_parent_id,
104                                       Id old_parent_id,
105                                       Array<ViewDataPtr> views) OVERRIDE;
106   virtual void OnViewReordered(Id view_id,
107                                Id relative_view_id,
108                                OrderDirection direction) OVERRIDE;
109   virtual void OnViewDeleted(Id view_id) OVERRIDE;
110   virtual void OnViewVisibilityChanged(Id view_id, bool visible) OVERRIDE;
111   virtual void OnViewDrawnStateChanged(Id view_id, bool drawn) OVERRIDE;
112   virtual void OnViewInputEvent(Id view_id,
113                                 EventPtr event,
114                                 const Callback<void()>& callback) OVERRIDE;
115   virtual void Embed(
116       const String& url,
117       InterfaceRequest<ServiceProvider> service_provider) OVERRIDE;
118   virtual void DispatchOnViewInputEvent(EventPtr event) OVERRIDE;
119
120     // Overridden from WindowManagerClient:
121   virtual void OnWindowManagerReady() OVERRIDE;
122   virtual void OnCaptureChanged(Id old_capture_view_id,
123                                 Id new_capture_view_id) OVERRIDE;
124   virtual void OnFocusChanged(Id old_focused_view_id,
125                               Id new_focused_view_id) OVERRIDE;
126   virtual void OnActiveWindowChanged(Id old_focused_window,
127                                      Id new_focused_window) OVERRIDE;
128
129   void RemoveRoot(View* root);
130
131   void OnActionCompleted(bool success);
132   void OnActionCompletedWithErrorCode(ErrorCode code);
133
134   BitmapUploader* BitmapUploaderForView(Id view_id);
135
136   base::Callback<void(bool)> ActionCompletedCallback();
137   base::Callback<void(ErrorCode)> ActionCompletedCallbackWithErrorCode();
138
139   bool connected_;
140   ConnectionSpecificId connection_id_;
141   ConnectionSpecificId next_id_;
142
143   std::string creator_url_;
144
145   base::Callback<void(void)> change_acked_callback_;
146
147   ViewManagerDelegate* delegate_;
148   WindowManagerDelegate* window_manager_delegate_;
149
150   std::vector<View*> roots_;
151
152   IdToViewMap views_;
153
154   ViewManagerService* service_;
155
156   WindowManagerServicePtr window_manager_;
157
158   // TODO(jamesr): Remove once all callers switch from SetContents to
159   // SetSurfaceId.
160   Shell* shell_;
161
162   DISALLOW_COPY_AND_ASSIGN(ViewManagerClientImpl);
163 };
164
165 }  // namespace mojo
166
167 #endif  // MOJO_SERVICES_PUBLIC_CPP_VIEW_MANAGER_LIB_VIEW_MANAGER_CLIENT_IMPL_H_