[Release] livebox.web-provider-1.9
[platform/framework/web/web-provider.git] / src / Daemon / BoxDaemonImpl.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Flora License, Version 1.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://floralicense.org/license/
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  * @file    BoxDaemonImpl.cpp
18  * @author  Yunchan Cho (yunchan.cho@samsung.com)
19  */
20 #include <sys/types.h>
21 #include <unistd.h>
22 #include <string>
23 #include <cstring>
24 #include <aul.h>
25 #include <Ecore_X.h>
26 #include <provider.h>
27 #include <livebox-service.h>
28 #include <Core/Util/Log.h>
29 #include <Plugin/IBoxPluginConnector.h>
30 #include <Plugin/BoxPluginConnector.h>
31 #include <API/web_provider_livebox_info.h>
32 #include "BoxDaemonUtil.h"
33 #include "BoxDaemonImpl.h"
34
35 #define MASTER_PROVIDER_PING_TIME  120.0f
36
37 BoxDaemonImpl::BoxDaemonImpl()
38     : m_pluginConnector(BoxPluginConnector::create())
39 {
40 }
41
42 BoxDaemonImpl::~BoxDaemonImpl()
43 {
44 }
45
46 bool BoxDaemonImpl::start(std::string& name)
47 {
48     LogD("enter");
49
50     // initialize existing plugins for web livebox
51     if (!m_pluginConnector->initialize()) { 
52         LogD("failed to initialize plugins");
53         return false;
54     }
55         
56     // set name
57     m_daemonName = name;
58
59     // provider init
60     ProviderCallbacks callbacks;
61     setProviderCallbacks(callbacks);
62
63     int ret = provider_init(ecore_x_display_get(),
64                             m_daemonName.c_str(),
65                             &callbacks,
66                             this);
67     if (ret < 0) {
68         LogD("failed to initialize provider");
69         return false;
70     }
71
72     return true;
73 }
74
75 bool BoxDaemonImpl::stop()
76 {
77     LogD("enter");
78
79     // deinitialize provider
80     provider_fini();
81
82     // deinitialize existing plugins for web livebox
83     if (!m_pluginConnector->shutdown()) { 
84         LogD("failed to shutdown plugins");
85         return false;
86     }
87
88     return true;
89 }
90
91 int BoxDaemonImpl::connectedCallback(ProviderEventArgPtr arg, void* data)
92 {
93     LogD("enter");
94
95     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
96     if (!provider_send_hello()) {
97         This->m_pingTimer = 
98             ecore_timer_add(MASTER_PROVIDER_PING_TIME, pingToMasterCallback, This);
99     }
100
101     return 0;
102 }
103
104 int BoxDaemonImpl::disconnectedCallback(ProviderEventArgPtr arg, void* data)
105 {
106     LogD("enter");
107
108         aul_terminate_pid(getpid());
109
110     return 0;
111 }
112
113 int BoxDaemonImpl::boxCreateCallback( 
114         ProviderEventArgPtr arg, 
115         int* width, int* height, 
116         double* priority, void* data)
117 {
118     LogD("enter");
119
120     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
121     BoxInfoPtr info = This->initializeBoxInfo(arg);
122     if (!info) {
123         return -1;
124     }
125
126     if ((arg->info.lb_create.width == 0) || (arg->info.lb_create.height == 0)) {
127         livebox_service_get_size(LB_SIZE_TYPE_1x1, width, height);
128     } else {
129         *width = arg->info.lb_create.width;
130         *height = arg->info.lb_create.height;
131     }
132
133     info->boxWidth = *width;
134     info->boxHeight = *height;
135     info->priority = 1.0f;
136     info->period = arg->info.lb_create.period;
137     if (arg->info.lb_create.content) {
138         info->contentInfo = std::string(arg->info.lb_create.content);
139     }
140
141     // box information
142     LogD("--------------------------------------------");
143     LogD("boxId: %s", info->boxId.c_str());
144     LogD("InstanceId: %s", info->instanceId.c_str());
145     LogD("width: %d", info->boxWidth);
146     LogD("height: %d", info->boxHeight);
147     LogD("update period: %d", info->period);
148     LogD("--------------------------------------------");
149
150     return ecore_job_add(
151                     requestBoxJobCallback,
152                     new JobInfo(REQUEST_CMD_ADD_BOX, info, This))
153                     ? 0 : -1;
154 }
155
156 int BoxDaemonImpl::boxReCreateCallback(ProviderEventArgPtr arg, void* data)
157 {
158     LogD("enter");
159
160     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
161     BoxInfoPtr info = This->initializeBoxInfo(arg);
162     if (!info) {
163         return -1;
164     }
165
166     int* width;
167     int* height;
168
169     if ((arg->info.lb_recreate.width == 0) || (arg->info.lb_recreate.height == 0)) {
170         livebox_service_get_size(LB_SIZE_TYPE_1x1, width, height);
171         arg->info.lb_recreate.width = *width;
172         arg->info.lb_recreate.height = *height;
173     }
174
175     info->boxWidth = arg->info.lb_recreate.width;
176     info->boxHeight = arg->info.lb_recreate.height;
177     info->priority = 1.0f;
178     info->period = arg->info.lb_recreate.period;
179     if (arg->info.lb_recreate.content) {
180         info->contentInfo = std::string(arg->info.lb_recreate.content);
181     }
182
183     // box information
184     LogD("--------------------------------------------");
185     LogD("boxId: %s", info->boxId.c_str());
186     LogD("InstanceId: %s", info->instanceId.c_str());
187     LogD("width: %d", info->boxWidth);
188     LogD("height: %d", info->boxHeight);
189     LogD("update period: %f", info->period);
190     LogD("--------------------------------------------");
191
192     return ecore_job_add(
193                     requestBoxJobCallback,
194                     new JobInfo(REQUEST_CMD_ADD_BOX, info, This))
195                     ? 0 : -1;
196 }
197
198 int BoxDaemonImpl::boxDestroyCallback(ProviderEventArgPtr arg, void* data)
199 {
200     LogD("enter");
201
202     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
203     BoxInfoPtr info = This->initializeBoxInfo(arg);
204     if (!info) {
205         return -1;
206     }
207
208     LogD("--------------------------------------------");
209     LogD("boxId: %s", info->boxId.c_str());
210     LogD("InstanceId: %s", info->instanceId.c_str());
211     LogD("--------------------------------------------");
212
213     return ecore_job_add(
214                     requestBoxJobCallback,
215                     new JobInfo(REQUEST_CMD_REMOVE_BOX, info, This))
216                     ? 0 : -1;
217 }
218
219 int BoxDaemonImpl::pdCreateCallback(ProviderEventArgPtr arg, void* data)
220 {
221     LogD("enter");
222
223     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
224     BoxInfoPtr info = This->initializeBoxInfo(arg);
225     if (!info) {
226         return -1;
227     }
228     if (arg->info.pd_create.w == 0 || arg->info.pd_create.h == 0) {
229         return -1;
230     }
231
232     info->pdWidth = arg->info.pd_create.w;
233     info->pdHeight = arg->info.pd_create.h;
234
235     LogD("--------------------------------------------");
236     LogD("boxId: %s", info->boxId.c_str());
237     LogD("InstanceId: %s", info->instanceId.c_str());
238     LogD("width: %d", info->pdWidth);
239     LogD("height: %d", info->pdHeight);
240     LogD("--------------------------------------------");
241
242     return ecore_job_add(
243                 requestBoxJobCallback,
244                 new JobInfo(REQUEST_CMD_OPEN_PD, info, This))
245                 ? 0 : -1;
246 }
247
248 int BoxDaemonImpl::pdDestroyCallback(ProviderEventArgPtr arg, void* data)
249 {
250     LogD("enter");
251
252     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
253     BoxInfoPtr info = This->initializeBoxInfo(arg);
254     if (!info) {
255         return -1;
256     }
257
258     LogD("--------------------------------------------");
259     LogD("boxId: %s", info->boxId.c_str());
260     LogD("InstanceId: %s", info->instanceId.c_str());
261     LogD("--------------------------------------------");
262
263     return ecore_job_add(
264                 requestBoxJobCallback,
265                 new JobInfo(REQUEST_CMD_CLOSE_PD, info, This))
266                 ? 0 : -1;
267 }
268
269 int BoxDaemonImpl::clickedCallback(ProviderEventArgPtr arg, void* data)
270 {
271     LogD("enter");
272
273     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
274     BoxInfoPtr info = This->initializeBoxInfo(arg);
275     if (!info) {
276         return -1;
277     }
278
279     int flag = web_provider_livebox_get_auto_launch(info->boxId.c_str());
280     if (!flag) {
281         return -1;
282     }
283
284     BoxDaemonUtil::launchApplication(info->boxId, info->instanceId);
285     return 0;
286 }
287
288 int BoxDaemonImpl::resizeCallback(ProviderEventArgPtr arg, void* data)
289 {
290     LogD("enter");
291
292     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
293     BoxInfoPtr info = This->initializeBoxInfo(arg);
294     if (!info) {
295         return -1;
296     }
297     if (arg->info.resize.w == 0 || arg->info.resize.h == 0) {
298         return -1;
299     }
300
301     info->boxWidth = arg->info.resize.w;
302     info->boxHeight = arg->info.resize.h;
303
304     // box information
305     LogD("--------------------------------------------");
306     LogD("boxId: %s", info->boxId.c_str());
307     LogD("InstanceId: %s", info->instanceId.c_str());
308     LogD("width: %d", info->boxWidth);
309     LogD("height: %d", info->boxHeight);
310     LogD("--------------------------------------------");
311
312     return ecore_job_add(
313                 requestBoxJobCallback,
314                 new JobInfo(REQUEST_CMD_RESIZE_BOX, info, This))
315                 ? 0 : -1;
316 }
317
318 int BoxDaemonImpl::boxPauseCallback(ProviderEventArgPtr arg, void* data)
319 {
320     LogD("enter");
321
322     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
323     BoxInfoPtr info = This->initializeBoxInfo(arg);
324     if (!info) {
325         return -1;
326     }
327
328     // box information
329     LogD("--------------------------------------------");
330     LogD("boxId: %s", info->boxId.c_str());
331     LogD("InstanceId: %s", info->instanceId.c_str());
332     LogD("--------------------------------------------");
333
334     return ecore_job_add(
335                 requestBoxJobCallback,
336                 new JobInfo(REQUEST_CMD_PAUSE_BOX, info, This))
337                 ? 0 : -1;
338 }
339
340 int BoxDaemonImpl::boxResumeCallback(ProviderEventArgPtr arg, void* data)
341 {
342     LogD("enter");
343
344     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
345     BoxInfoPtr info = This->initializeBoxInfo(arg);
346     if (!info) {
347         return -1;
348     }
349
350     // box information
351     LogD("--------------------------------------------");
352     LogD("boxId: %s", info->boxId.c_str());
353     LogD("InstanceId: %s", info->instanceId.c_str());
354     LogD("--------------------------------------------");
355
356     return ecore_job_add(
357                 requestBoxJobCallback,
358                 new JobInfo(REQUEST_CMD_RESUME_BOX, info, This))
359                 ? 0 : -1;
360 }
361
362 int BoxDaemonImpl::pauseCallback(ProviderEventArgPtr arg, void* data)
363 {
364     LogD("enter");
365
366     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
367
368     LogD("--------------------------------------------");
369     LogD("web-provider is paused");
370     LogD("--------------------------------------------");
371
372     return ecore_job_add(
373                 requestBoxJobCallback,
374                 new JobInfo(REQUEST_CMD_PAUSE_ALL, BoxInfoPtr(), This))
375                 ? 0 : -1;
376 }
377
378 int BoxDaemonImpl::resumeCallback(ProviderEventArgPtr arg, void* data)
379 {
380     LogD("enter");
381
382     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
383
384     LogD("--------------------------------------------");
385     LogD("web-provider is resumed");
386     LogD("--------------------------------------------");
387
388     return ecore_job_add(
389                 requestBoxJobCallback,
390                 new JobInfo(REQUEST_CMD_RESUME_ALL, BoxInfoPtr(), This))
391                 ? 0 : -1;
392 }
393
394 int BoxDaemonImpl::updateContentCallback(ProviderEventArgPtr arg, void* data)
395 {
396     LogD("enter");
397
398     return 0;
399 }
400
401 int BoxDaemonImpl::changePeriodCallback(ProviderEventArgPtr arg, void* data)
402 {
403     LogD("enter");
404     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
405     BoxInfoPtr info = This->initializeBoxInfo(arg);
406     if (!info) {
407         return -1;
408     }
409     info->period = arg->info.set_period.period;
410
411     LogD("--------------------------------------------");
412     LogD("boxId: %s", info->boxId.c_str());
413     LogD("InstanceId: %s", info->instanceId.c_str());
414     LogD("period: %f", info->period);
415     LogD("--------------------------------------------");
416
417     return ecore_job_add(
418                 requestBoxJobCallback,
419                 new JobInfo(REQUEST_CMD_CHANGE_PERIOD, info, This))
420                 ? 0 : -1;
421 }
422
423 void BoxDaemonImpl::setProviderCallbacks(ProviderCallbacks& callbacks)
424 {
425     LogD("enter");
426
427     memset(&callbacks, 0, sizeof(callbacks));
428     callbacks.connected = BoxDaemonImpl::connectedCallback;
429     callbacks.disconnected = BoxDaemonImpl::disconnectedCallback;
430     callbacks.pause = BoxDaemonImpl::pauseCallback;
431     callbacks.resume = BoxDaemonImpl::resumeCallback;
432     callbacks.lb_create = BoxDaemonImpl::boxCreateCallback;
433     callbacks.lb_recreate = BoxDaemonImpl::boxReCreateCallback;
434     callbacks.lb_destroy = BoxDaemonImpl::boxDestroyCallback;
435     callbacks.lb_pause = BoxDaemonImpl::boxPauseCallback;
436     callbacks.lb_resume = BoxDaemonImpl::boxResumeCallback;
437     callbacks.pd_create = BoxDaemonImpl::pdCreateCallback;
438     callbacks.pd_destroy = BoxDaemonImpl::pdDestroyCallback;
439     callbacks.clicked = BoxDaemonImpl::clickedCallback;
440     callbacks.resize = BoxDaemonImpl::resizeCallback;
441     callbacks.update_content = BoxDaemonImpl::updateContentCallback;
442     callbacks.set_period = BoxDaemonImpl::changePeriodCallback;
443 }
444
445 const char* BoxDaemonImpl::getBoxType(const char* boxId)
446 {
447     LogD("enter");
448
449     if (!boxId) {
450         return NULL;
451     }
452     
453     const char* type = web_provider_livebox_get_box_type(boxId);
454     std::string boxType;
455     if (!type) {
456         std::string serviceBoxId(boxId);
457         boxType = m_pluginConnector->getBoxType(serviceBoxId);
458         if (boxType.empty()) {
459             LogD("unrecognized box id");
460             return NULL; 
461         }
462         type = strdup(boxType.c_str());
463     }
464
465     LogD("box id: %s, type: %s", boxId, type);
466     return type;
467 }
468
469 BoxInfoPtr BoxDaemonImpl::initializeBoxInfo(ProviderEventArgPtr arg)
470 {
471     LogD("enter");
472
473     if (!arg) {
474         return BoxInfoPtr();
475     }
476
477     if (!arg->pkgname || !arg->id) {
478         LogD("pkgname or id don't exist");
479         return BoxInfoPtr();
480     }
481
482     const char* type = getBoxType(arg->pkgname);
483     if (!type) {
484         return BoxInfoPtr();
485     }
486
487     BoxInfoPtr infoPtr = BoxInfoPtr(new BoxInfo(type, arg->pkgname, arg->id));
488     delete[] type;
489
490     return infoPtr;
491 }
492
493 Eina_Bool BoxDaemonImpl::pingToMasterCallback(void* data)
494 {
495     LogD("enter");
496
497     provider_send_ping();
498
499     return ECORE_CALLBACK_RENEW;
500 }
501
502 void BoxDaemonImpl::requestBoxJobCallback(void* data)
503 {
504     JobInfo* jobInfo = static_cast<JobInfo*>(data);
505     if (!jobInfo) {
506         LogD("no information for job");
507         return;
508     }
509
510     // just for debugging
511     //volatile int flag = 0;
512     //while(flag == 0) {;}
513
514     // request box job!
515     jobInfo->daemonImpl->m_pluginConnector->requestCommand(
516             jobInfo->cmdType, jobInfo->boxInfo);
517 }