38ed6de9e99d4eee375da44fc911f9076f1033d4
[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 ApplicationConnection;
23 class ViewManager;
24 class ViewManagerDelegate;
25 class ViewManagerTransaction;
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,
33                         ApplicationConnection* app_connection);
34   virtual ~ViewManagerClientImpl();
35
36   bool connected() const { return connected_; }
37   ConnectionSpecificId connection_id() const { return connection_id_; }
38
39   // API exposed to the view implementations that pushes local changes to the
40   // service.
41   Id CreateView();
42   void DestroyView(Id view_id);
43
44   // These methods take TransportIds. For views owned by the current connection,
45   // the connection id high word can be zero. In all cases, the TransportId 0x1
46   // refers to the root view.
47   void AddChild(Id child_id, Id parent_id);
48   void RemoveChild(Id child_id, Id parent_id);
49
50   void Reorder(Id view_id, Id relative_view_id, OrderDirection direction);
51
52   // Returns true if the specified view was created by this connection.
53   bool OwnsView(Id id) const;
54
55   void SetBounds(Id view_id, const gfx::Rect& bounds);
56   void SetViewContents(Id view_id, const SkBitmap& contents);
57   void SetFocus(Id view_id);
58   void SetVisible(Id view_id, bool visible);
59
60   void Embed(const String& url, Id view_id);
61   void Embed(const String& url,
62              Id view_id,
63              ServiceProviderPtr service_provider);
64
65   void set_change_acked_callback(const base::Callback<void(void)>& callback) {
66     change_acked_callback_ = callback;
67   }
68   void ClearChangeAckedCallback() {
69     change_acked_callback_ = base::Callback<void(void)>();
70   }
71
72   // Start/stop tracking views. While tracked, they can be retrieved via
73   // ViewManager::GetViewById.
74   void AddView(View* view);
75   void RemoveView(Id view_id);
76
77  private:
78   friend class RootObserver;
79
80   typedef std::map<Id, View*> IdToViewMap;
81
82   // Overridden from ViewManager:
83   virtual void SetWindowManagerDelegate(
84       WindowManagerDelegate* delegate) OVERRIDE;
85   virtual void DispatchEvent(View* target, EventPtr event) OVERRIDE;
86   virtual const std::string& GetEmbedderURL() const OVERRIDE;
87   virtual const std::vector<View*>& GetRoots() const OVERRIDE;
88   virtual View* GetViewById(Id id) OVERRIDE;
89
90   // Overridden from InterfaceImpl:
91   virtual void OnConnectionEstablished() OVERRIDE;
92
93   // Overridden from ViewManagerClient:
94   virtual void OnEmbed(ConnectionSpecificId connection_id,
95                        const String& creator_url,
96                        ViewDataPtr root,
97                        InterfaceRequest<ServiceProvider> services) OVERRIDE;
98   virtual void OnViewBoundsChanged(Id view_id,
99                                    RectPtr old_bounds,
100                                    RectPtr new_bounds) OVERRIDE;
101   virtual void OnViewHierarchyChanged(Id view_id,
102                                       Id new_parent_id,
103                                       Id old_parent_id,
104                                       Array<ViewDataPtr> views) OVERRIDE;
105   virtual void OnViewReordered(Id view_id,
106                                Id relative_view_id,
107                                OrderDirection direction) OVERRIDE;
108   virtual void OnViewDeleted(Id view_id) OVERRIDE;
109   virtual void OnViewInputEvent(Id view,
110                                 EventPtr event,
111                                 const Callback<void()>& callback) OVERRIDE;
112   virtual void Embed(
113       const String& url,
114       InterfaceRequest<ServiceProvider> service_provider) OVERRIDE;
115   virtual void DispatchOnViewInputEvent(EventPtr event) OVERRIDE;
116
117     // Overridden from WindowManagerClient:
118   virtual void OnWindowManagerReady() OVERRIDE;
119   virtual void OnCaptureChanged(Id old_capture_view_id,
120                                 Id new_capture_view_id) OVERRIDE;
121   virtual void OnFocusChanged(Id old_focused_view_id,
122                               Id new_focused_view_id) OVERRIDE;
123   virtual void OnActiveWindowChanged(Id old_focused_window,
124                                      Id new_focused_window) OVERRIDE;
125
126   void RemoveRoot(View* root);
127
128   void OnActionCompleted(bool success);
129   void OnActionCompletedWithErrorCode(ErrorCode code);
130
131   base::Callback<void(bool)> ActionCompletedCallback();
132   base::Callback<void(ErrorCode)> ActionCompletedCallbackWithErrorCode();
133
134   bool connected_;
135   ConnectionSpecificId connection_id_;
136   ConnectionSpecificId next_id_;
137
138   std::string creator_url_;
139
140   base::Callback<void(void)> change_acked_callback_;
141
142   ViewManagerDelegate* delegate_;
143   WindowManagerDelegate* window_manager_delegate_;
144
145   std::vector<View*> roots_;
146
147   IdToViewMap views_;
148
149   ViewManagerService* service_;
150
151   WindowManagerServicePtr window_manager_;
152
153   DISALLOW_COPY_AND_ASSIGN(ViewManagerClientImpl);
154 };
155
156 }  // namespace mojo
157
158 #endif  // MOJO_SERVICES_PUBLIC_CPP_VIEW_MANAGER_LIB_VIEW_MANAGER_CLIENT_IMPL_H_