[3.0] Fixed crash issue by buffer overflow
[platform/core/uifw/dali-adaptor.git] / adaptors / ecore / wayland / clipboard-impl-ecore-wl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "clipboard-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <Ecore.h>
23 #include <Ecore_Wayland.h>
24 #include <dali/public-api/object/any.h>
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/integration-api/debug.h>
27 #include <unistd.h>
28
29 #ifdef DALI_ELDBUS_AVAILABLE
30 #include <Eldbus.h>
31 #endif // DALI_ELDBUS_AVAILABLE
32
33 // INTERNAL INCLUDES
34 #include <singleton-service-impl.h>
35
36 #define CBHM_DBUS_OBJPATH "/org/tizen/cbhm/dbus"
37 #ifndef CBHM_DBUS_INTERFACE
38 #define CBHM_DBUS_INTERFACE "org.tizen.cbhm.dbus"
39 #endif /* CBHM_DBUS_INTERFACE */
40
41 #define CLIPBOARD_STR  "CLIPBOARD_STR"
42 #define CLIPBOARD_BUFFER_SIZE 3000
43
44 ///////////////////////////////////////////////////////////////////////////////////////////////////
45 // Clipboard
46 ///////////////////////////////////////////////////////////////////////////////////////////////////
47
48 namespace Dali
49 {
50
51 namespace Internal
52 {
53
54 namespace Adaptor
55 {
56
57 struct Clipboard::Impl
58 {
59   Impl()
60   {
61     Eldbus_Object *eldbus_obj;
62     cbhm_conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
63     eldbus_obj = eldbus_object_get(cbhm_conn, CBHM_DBUS_INTERFACE, CBHM_DBUS_OBJPATH);
64     eldbus_proxy = eldbus_proxy_get(eldbus_obj, CBHM_DBUS_INTERFACE);
65     eldbus_name_owner_changed_callback_add(cbhm_conn, CBHM_DBUS_INTERFACE, NULL, cbhm_conn, EINA_TRUE);
66     eldbus_proxy_signal_handler_add(eldbus_proxy, "ItemClicked", _on_item_clicked, this);
67     mVisible = false;
68     mIsFirstTimeHidden = true;
69   }
70
71   ~Impl()
72   {
73     if (cbhm_conn)
74       eldbus_connection_unref(cbhm_conn);
75   }
76
77   Eldbus_Proxy* cbhm_proxy_get()
78   {
79     return eldbus_proxy;
80   }
81
82   Eldbus_Connection* cbhm_connection_get()
83   {
84     return cbhm_conn;
85   }
86
87   void SetItem( const std::string &itemData )
88   {
89     const char *data = itemData.c_str();
90     const char *types[10] = {0, };
91     int i = -1;
92
93     if (data == NULL)
94     {
95       return;
96     }
97     strncpy(mSendBuf, data, CLIPBOARD_BUFFER_SIZE);
98
99     // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
100     types[++i] = "CLIPBOARD_BEGIN";
101
102     types[++i] = "application/x-elementary-markup";
103     types[++i] = "text/plain";
104     types[++i] = "text/plain;charset=utf-8";
105
106     // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
107     types[++i] = "CLIPBOARD_END";
108     ecore_wl_dnd_selection_set(ecore_wl_input_get(), types);
109   }
110
111   void RequestItem()
112   {
113     const char *types[10] = {0, };
114     int i = -1;
115
116     types[++i] = "application/x-elementary-markup";
117     types[++i] = "text/plain";
118     types[++i] = "text/plain;charset=utf-8";
119     ecore_wl_dnd_selection_get(ecore_wl_input_get(), *types);
120   }
121
122   char *ExcuteSend( void *event )
123   {
124     Ecore_Wl_Event_Data_Source_Send *ev = (Ecore_Wl_Event_Data_Source_Send *)event;
125     int len_buf = strlen(mSendBuf);
126     int len_remained = len_buf;
127     int len_written = 0, ret;
128     char *buf = mSendBuf;
129
130     while (len_written < len_buf)
131     {
132        ret = write(ev->fd, buf, len_remained);
133        if (ret == -1) break;
134        buf += ret;
135        len_written += ret;
136        len_remained -= ret;
137     }
138     close(ev->fd);
139     return (char *)mSendBuf;
140   }
141
142   char *ExcuteReceive( void *event )
143   {
144     Ecore_Wl_Event_Selection_Data_Ready *ev = (Ecore_Wl_Event_Selection_Data_Ready *)event;
145
146     return (char *)ev->data;
147   }
148
149   int GetCount()
150   {
151     Eldbus_Message *reply, *req;
152     const char *errname = NULL, *errmsg = NULL;
153     int count = -1;
154
155     if (!(req = eldbus_proxy_method_call_new(eldbus_proxy, "CbhmGetCount")))
156     {
157       DALI_LOG_ERROR("Failed to create method call on org.freedesktop.DBus.Properties.Get");
158       return -1;
159     }
160
161     eldbus_message_ref(req);
162     reply = eldbus_proxy_send_and_block(eldbus_proxy, req, 100);
163     if (!reply || eldbus_message_error_get(reply, &errname, &errmsg))
164     {
165       DALI_LOG_ERROR("Unable to call method org.freedesktop.DBus.Properties.Get: %s %s",
166       errname, errmsg);
167       eldbus_message_unref(req);
168       return -1;
169     }
170
171     if (!eldbus_message_arguments_get(reply, "i", &count))
172     {
173       DALI_LOG_ERROR("Cannot get arguments from eldbus");
174       eldbus_message_unref(req);
175       return -1;
176     }
177
178     eldbus_message_unref(req);
179     DALI_LOG_ERROR("cbhm item count(%d)", count);
180     return count;
181   }
182
183   void ShowClipboard()
184   {
185     eldbus_proxy_call(cbhm_proxy_get(), "CbhmShow", NULL, NULL, -1, "s", "0");
186     mIsFirstTimeHidden = true;
187     mVisible = true;
188   }
189
190   void HideClipboard( bool skipFirstHide )
191   {
192     if ( skipFirstHide && mIsFirstTimeHidden )
193     {
194       mIsFirstTimeHidden = false;
195       return;
196     }
197     eldbus_proxy_call(cbhm_proxy_get(), "CbhmHide", NULL, NULL, -1, "");
198     mIsFirstTimeHidden = false;
199     mVisible = false;
200   }
201
202   bool IsVisible() const
203   {
204     return mVisible;
205   }
206
207   static void _on_item_clicked(void *data, const Eldbus_Message *msg EINA_UNUSED)
208   {
209     static_cast<Clipboard::Impl*>(data)->RequestItem();
210   }
211
212   Eldbus_Proxy *eldbus_proxy;
213   Eldbus_Connection *cbhm_conn;
214
215   char mSendBuf[CLIPBOARD_BUFFER_SIZE+1];
216   bool mVisible;
217   bool mIsFirstTimeHidden;
218 };
219
220 Clipboard::Clipboard(Impl* impl)
221 : mImpl(impl)
222 {
223 }
224
225 Clipboard::~Clipboard()
226 {
227 }
228
229 Dali::Clipboard Clipboard::Get()
230 {
231   Dali::Clipboard clipboard;
232
233   Dali::SingletonService service( SingletonService::Get() );
234   if ( service )
235   {
236     // Check whether the singleton is already created
237     Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::Clipboard ) );
238     if(handle)
239     {
240       // If so, downcast the handle
241       clipboard = Dali::Clipboard( dynamic_cast< Clipboard* >( handle.GetObjectPtr() ) );
242     }
243     else
244     {
245       Clipboard::Impl* impl( new Clipboard::Impl() );
246       clipboard = Dali::Clipboard( new Clipboard(impl) );
247       service.Register( typeid(Dali::Clipboard), clipboard );
248     }
249   }
250
251   return clipboard;
252 }
253
254 bool Clipboard::SetItem(const std::string &itemData )
255 {
256   mImpl->SetItem( itemData );
257   return true;
258 }
259
260 /*
261  * Request clipboard service to give an item
262  */
263 void Clipboard::RequestItem()
264 {
265   mImpl->RequestItem();
266 }
267
268 /*
269  * Get number of items in clipboard
270  */
271 unsigned int Clipboard::NumberOfItems()
272 {
273   return mImpl->GetCount();
274 }
275
276 void Clipboard::ShowClipboard()
277 {
278   mImpl->ShowClipboard();
279 }
280
281 void Clipboard::HideClipboard(bool skipFirstHide)
282 {
283   mImpl->HideClipboard(skipFirstHide);
284 }
285
286 bool Clipboard::IsVisible() const
287 {
288   return mImpl->IsVisible();
289 }
290
291 char* Clipboard::ExcuteBuffered( bool type, void *event )
292 {
293   return (type ?  mImpl->ExcuteSend( event ) : mImpl->ExcuteReceive( event ));
294 }
295
296 } // namespace Adaptor
297
298 } // namespace Internal
299
300 } // namespace Dali