- add sources.
[platform/framework/web/crosswalk.git] / src / ozone / impl / ipc / display_channel_host.cc
1 // Copyright 2013 Intel Corporation. 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 "ozone/impl/ipc/display_channel_host.h"
6
7 #include "base/bind.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "ozone/impl/ipc/messages.h"
10 #include "ozone/impl/ozone_display.h"
11 #include "ozone/wayland/dispatcher.h"
12
13 namespace ozonewayland {
14
15 // This should be same as defined in display_channel.
16 #define CHANNEL_ROUTE_ID -0x1
17
18 OzoneDisplayChannelHost::OzoneDisplayChannelHost()
19     : channel_(NULL) {
20   dispatcher_ = WaylandDispatcher::GetInstance();
21 }
22
23 OzoneDisplayChannelHost::~OzoneDisplayChannelHost() {
24   OzoneDisplay::GetInstance()->OnChannelHostDestroyed();
25   DCHECK(deferred_messages_.empty());
26 }
27
28 void OzoneDisplayChannelHost::EstablishChannel() {
29   if (channel_)
30     return;
31
32   content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE,
33       base::Bind(base::IgnoreResult(&OzoneDisplayChannelHost::UpdateConnection),
34           this));
35 }
36
37 void OzoneDisplayChannelHost::SendWidgetState(unsigned w,
38                                               unsigned state,
39                                               unsigned width,
40                                               unsigned height) {
41   if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)) {
42     content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE,
43         base::Bind(&OzoneDisplayChannelHost::SendWidgetState,
44             base::Unretained(this), w, state, width, height));
45     return;
46   }
47
48   Send(new WaylandWindow_State(CHANNEL_ROUTE_ID, w, state, width, height));
49 }
50
51 void OzoneDisplayChannelHost::SendWidgetAttributes(unsigned widget,
52                                                    unsigned parent,
53                                                    unsigned x,
54                                                    unsigned y,
55                                                    unsigned type) {
56   if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)) {
57     content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE,
58         base::Bind(&OzoneDisplayChannelHost::SendWidgetAttributes,
59             base::Unretained(this), widget, parent, x, y, type));
60     return;
61   }
62
63   Send(new WaylandWindow_Attributes(CHANNEL_ROUTE_ID,
64                                     widget,
65                                     parent,
66                                     x,
67                                     y,
68                                     type));
69 }
70
71 void OzoneDisplayChannelHost::SendWidgetTitle(
72     unsigned w, const string16& title) {
73   if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)) {
74     content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE,
75         base::Bind(&OzoneDisplayChannelHost::SendWidgetTitle,
76             base::Unretained(this), w, title));
77     return;
78   }
79
80   Send(new WaylandWindow_Title(CHANNEL_ROUTE_ID, w, title));
81 }
82
83 void OzoneDisplayChannelHost::OnChannelEstablished() {
84   DCHECK(channel_);
85   Send(new WaylandMsg_DisplayChannelEstablished(CHANNEL_ROUTE_ID));
86   while (!deferred_messages_.empty()) {
87     Send(deferred_messages_.front());
88     deferred_messages_.pop();
89   }
90 }
91
92 void OzoneDisplayChannelHost::OnMotionNotify(float x, float y) {
93   dispatcher_->MotionNotify(x, y);
94 }
95
96 void OzoneDisplayChannelHost::OnButtonNotify(unsigned handle,
97                                              int state,
98                                              int flags,
99                                              float x,
100                                              float y) {
101   dispatcher_->ButtonNotify(handle, state, flags, x, y);
102 }
103
104 void OzoneDisplayChannelHost::OnAxisNotify(float x,
105                                            float y,
106                                            float xoffset,
107                                            float yoffset) {
108   dispatcher_->AxisNotify(x, y, xoffset, yoffset);
109 }
110
111 void OzoneDisplayChannelHost::OnPointerEnter(unsigned handle,
112                                              float x,
113                                              float y) {
114   dispatcher_->PointerEnter(handle, x, y);
115 }
116
117 void OzoneDisplayChannelHost::OnPointerLeave(unsigned handle,
118                                              float x,
119                                              float y) {
120   dispatcher_->PointerLeave(handle, x, y);
121 }
122
123 void OzoneDisplayChannelHost::OnKeyNotify(unsigned type,
124                                           unsigned code,
125                                           unsigned modifiers) {
126   dispatcher_->KeyNotify(type, code, modifiers);
127 }
128
129 void OzoneDisplayChannelHost::OnOutputSizeChanged(unsigned width,
130                                                   unsigned height) {
131   OzoneDisplay::GetInstance()->OnOutputSizeChanged(width, height);
132 }
133
134 bool OzoneDisplayChannelHost::OnMessageReceived(const IPC::Message& message) {
135   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)) <<
136       "Must handle messages that were dispatched to another thread!";
137
138   bool handled = true;
139   IPC_BEGIN_MESSAGE_MAP(OzoneDisplayChannelHost, message)
140   IPC_MESSAGE_HANDLER(WaylandInput_MotionNotify, OnMotionNotify)
141   IPC_MESSAGE_HANDLER(WaylandInput_ButtonNotify, OnButtonNotify)
142   IPC_MESSAGE_HANDLER(WaylandInput_AxisNotify, OnAxisNotify)
143   IPC_MESSAGE_HANDLER(WaylandInput_PointerEnter, OnPointerEnter)
144   IPC_MESSAGE_HANDLER(WaylandInput_PointerLeave, OnPointerLeave)
145   IPC_MESSAGE_HANDLER(WaylandInput_KeyNotify, OnKeyNotify)
146   IPC_MESSAGE_HANDLER(WaylandInput_OutputSize, OnOutputSizeChanged)
147   IPC_MESSAGE_UNHANDLED(handled = false)
148   IPC_END_MESSAGE_MAP()
149
150   return handled;
151 }
152
153 void OzoneDisplayChannelHost::OnFilterAdded(IPC::Channel* channel) {
154   channel_ = channel;
155 }
156
157 void OzoneDisplayChannelHost::OnChannelClosing() {
158   channel_ = NULL;
159 }
160
161 bool OzoneDisplayChannelHost::Send(IPC::Message* message) {
162   if (!channel_) {
163     deferred_messages_.push(message);
164     return true;
165   }
166
167   // Callee takes ownership of message, regardless of whether Send is
168   // successful. See IPC::Sender.
169   scoped_ptr<IPC::Message> scoped_message(message);
170   return channel_->Send(scoped_message.release());
171 }
172
173 void OzoneDisplayChannelHost::UpdateConnection() {
174   content::GpuProcessHost* host = content::GpuProcessHost::Get(
175       content::GpuProcessHost::GPU_PROCESS_KIND_SANDBOXED,
176       content::CAUSE_FOR_GPU_LAUNCH_BROWSER_STARTUP);
177
178   DCHECK(host);
179   host->AddFilter(this);
180   OnChannelEstablished();
181 }
182
183 }  // namespace ozonewayland