9f201b0845a6a45905710b4198d6f8791f04d8b0
[platform/framework/web/crosswalk.git] / src / mojo / services / view_manager / window_tree_host_impl.cc
1 // Copyright 2013 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 #include "mojo/services/view_manager/root_node_manager.h"
6 #include "mojo/services/view_manager/window_tree_host_impl.h"
7 #include "mojo/public/c/gles2/gles2.h"
8 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h"
9 #include "mojo/services/public/cpp/input_events/input_events_type_converters.h"
10 #include "mojo/services/view_manager/context_factory_impl.h"
11 #include "ui/aura/env.h"
12 #include "ui/aura/layout_manager.h"
13 #include "ui/aura/window.h"
14 #include "ui/aura/window_event_dispatcher.h"
15 #include "ui/compositor/compositor.h"
16 #include "ui/events/event.h"
17 #include "ui/events/event_constants.h"
18 #include "ui/gfx/geometry/insets.h"
19 #include "ui/gfx/geometry/rect.h"
20
21 namespace mojo {
22 namespace service {
23
24 // TODO(sky): nuke this. It shouldn't be static.
25 // static
26 ContextFactoryImpl* WindowTreeHostImpl::context_factory_ = NULL;
27
28 ////////////////////////////////////////////////////////////////////////////////
29 // RootLayoutManager, layout management for the root window's (one) child
30
31 class RootLayoutManager : public aura::LayoutManager {
32  public:
33   RootLayoutManager() : child_(NULL) {}
34
35   // Overridden from aura::LayoutManager
36   virtual void OnWindowResized() OVERRIDE;
37   virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE;
38   virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {}
39   virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {}
40   virtual void OnChildWindowVisibilityChanged(aura::Window* child,
41                                               bool visible) OVERRIDE {}
42   virtual void SetChildBounds(aura::Window* child,
43                               const gfx::Rect& requested_bounds) OVERRIDE;
44  private:
45   aura::Window* child_;
46
47   DISALLOW_COPY_AND_ASSIGN(RootLayoutManager);
48 };
49
50 void RootLayoutManager::OnWindowResized() {
51   if (child_)
52     child_->SetBounds(gfx::Rect(child_->parent()->bounds().size()));
53 }
54
55 void RootLayoutManager::OnWindowAddedToLayout(aura::Window* child) {
56   DCHECK(!child_);
57   child_ = child;
58 }
59
60 void RootLayoutManager::SetChildBounds(aura::Window* child,
61                                        const gfx::Rect& requested_bounds) {
62   SetChildBoundsDirect(child, gfx::Rect(requested_bounds.size()));
63 }
64
65 ////////////////////////////////////////////////////////////////////////////////
66 // WindowTreeHostImpl, public:
67
68 WindowTreeHostImpl::WindowTreeHostImpl(
69     NativeViewportPtr viewport,
70     const gfx::Rect& bounds,
71     const Callback<void()>& compositor_created_callback,
72     const Callback<void()>& native_viewport_closed_callback,
73     const Callback<void(EventPtr)>& event_received_callback)
74     : native_viewport_(viewport.Pass()),
75       compositor_created_callback_(compositor_created_callback),
76       native_viewport_closed_callback_(native_viewport_closed_callback),
77       event_received_callback_(event_received_callback),
78       bounds_(bounds) {
79   native_viewport_.set_client(this);
80   native_viewport_->Create(Rect::From(bounds));
81
82   MessagePipe pipe;
83   native_viewport_->CreateGLES2Context(
84       MakeRequest<CommandBuffer>(pipe.handle0.Pass()));
85
86   // The ContextFactory must exist before any Compositors are created.
87   if (context_factory_) {
88     delete context_factory_;
89     context_factory_ = NULL;
90   }
91   context_factory_ = new ContextFactoryImpl(pipe.handle1.Pass());
92   aura::Env::GetInstance()->set_context_factory(context_factory_);
93
94   window()->SetLayoutManager(new RootLayoutManager());
95 }
96
97 WindowTreeHostImpl::~WindowTreeHostImpl() {
98   DestroyCompositor();
99   DestroyDispatcher();
100 }
101
102 ////////////////////////////////////////////////////////////////////////////////
103 // WindowTreeHostImpl, aura::WindowTreeHost implementation:
104
105 ui::EventSource* WindowTreeHostImpl::GetEventSource() {
106   return this;
107 }
108
109 gfx::AcceleratedWidget WindowTreeHostImpl::GetAcceleratedWidget() {
110   NOTIMPLEMENTED() << "GetAcceleratedWidget";
111   return gfx::kNullAcceleratedWidget;
112 }
113
114 void WindowTreeHostImpl::Show() {
115   window()->Show();
116   native_viewport_->Show();
117 }
118
119 void WindowTreeHostImpl::Hide() {
120   native_viewport_->Hide();
121   window()->Hide();
122 }
123
124 gfx::Rect WindowTreeHostImpl::GetBounds() const {
125   return bounds_;
126 }
127
128 void WindowTreeHostImpl::SetBounds(const gfx::Rect& bounds) {
129   native_viewport_->SetBounds(Rect::From(bounds));
130 }
131
132 gfx::Point WindowTreeHostImpl::GetLocationOnNativeScreen() const {
133   return gfx::Point(0, 0);
134 }
135
136 void WindowTreeHostImpl::SetCapture() {
137   NOTIMPLEMENTED();
138 }
139
140 void WindowTreeHostImpl::ReleaseCapture() {
141   NOTIMPLEMENTED();
142 }
143
144 void WindowTreeHostImpl::PostNativeEvent(
145     const base::NativeEvent& native_event) {
146   NOTIMPLEMENTED();
147 }
148
149 void WindowTreeHostImpl::SetCursorNative(gfx::NativeCursor cursor) {
150   NOTIMPLEMENTED();
151 }
152
153 void WindowTreeHostImpl::MoveCursorToNative(const gfx::Point& location) {
154   NOTIMPLEMENTED();
155 }
156
157 void WindowTreeHostImpl::OnCursorVisibilityChangedNative(bool show) {
158   NOTIMPLEMENTED();
159 }
160
161 ////////////////////////////////////////////////////////////////////////////////
162 // WindowTreeHostImpl, ui::EventSource implementation:
163
164 ui::EventProcessor* WindowTreeHostImpl::GetEventProcessor() {
165   return dispatcher();
166 }
167
168 ////////////////////////////////////////////////////////////////////////////////
169 // WindowTreeHostImpl, NativeViewportClient implementation:
170
171 void WindowTreeHostImpl::OnCreated() {
172   CreateCompositor(GetAcceleratedWidget());
173   compositor_created_callback_.Run();
174 }
175
176 void WindowTreeHostImpl::OnBoundsChanged(RectPtr bounds) {
177   bounds_ = bounds.To<gfx::Rect>();
178   OnHostResized(bounds_.size());
179 }
180
181 void WindowTreeHostImpl::OnDestroyed(const mojo::Callback<void()>& callback) {
182   DestroyCompositor();
183   native_viewport_closed_callback_.Run();
184   // TODO(beng): quit the message loop once we are on our own thread.
185   callback.Run();
186 }
187
188 void WindowTreeHostImpl::OnEvent(EventPtr event,
189                                  const mojo::Callback<void()>& callback) {
190   event_received_callback_.Run(event.Pass());
191   callback.Run();
192 };
193
194 }  // namespace service
195 }  // namespace mojo