[Release] livebox.web-provider-1.7
[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://www.tizenopensource.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         arg->info.lb_create.width = *width;
129         arg->info.lb_create.height = *height;
130     }
131
132     info->boxWidth = arg->info.lb_create.width;
133     info->boxHeight = arg->info.lb_create.height;
134     info->priority = 1.0f;
135     info->period = arg->info.lb_create.period;
136     if (arg->info.lb_create.content) {
137         info->contentInfo = std::string(arg->info.lb_create.content);
138     }
139
140     // box information
141     LogD("--------------------------------------------");
142     LogD("boxId: %s", info->boxId.c_str());
143     LogD("InstanceId: %s", info->instanceId.c_str());
144     LogD("width: %d", info->boxWidth);
145     LogD("height: %d", info->boxHeight);
146     LogD("update period: %d", info->period);
147     LogD("--------------------------------------------");
148
149     JobInfo* jobInfo = new JobInfo(REQUEST_CMD_ADD_BOX, info, This);
150     Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
151
152     return ret ? 0 : -1;
153 }
154
155 int BoxDaemonImpl::boxReCreateCallback(ProviderEventArgPtr arg, void* data)
156 {
157     LogD("enter");
158
159     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
160     BoxInfoPtr info = This->initializeBoxInfo(arg);
161     if (!info) {
162         return -1;
163     }
164
165     int* width;
166     int* height;
167
168     if ((arg->info.lb_recreate.width == 0) || (arg->info.lb_recreate.height == 0)) {
169         livebox_service_get_size(LB_SIZE_TYPE_1x1, width, height);
170         arg->info.lb_recreate.width = *width;
171         arg->info.lb_recreate.height = *height;
172     }
173
174     info->boxWidth = arg->info.lb_recreate.width;
175     info->boxHeight = arg->info.lb_recreate.height;
176     info->priority = 1.0f;
177     info->period = arg->info.lb_recreate.period;
178     if (arg->info.lb_recreate.content) {
179         info->contentInfo = std::string(arg->info.lb_recreate.content);
180     }
181
182     // box information
183     LogD("--------------------------------------------");
184     LogD("boxId: %s", info->boxId.c_str());
185     LogD("InstanceId: %s", info->instanceId.c_str());
186     LogD("width: %d", info->boxWidth);
187     LogD("height: %d", info->boxHeight);
188     LogD("update period: %f", info->period);
189     LogD("--------------------------------------------");
190
191     JobInfo* jobInfo = new JobInfo(REQUEST_CMD_ADD_BOX, info, This);
192     Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
193
194     return ret ? 0 : -1;
195 }
196
197 int BoxDaemonImpl::boxDestroyCallback(ProviderEventArgPtr arg, void* data)
198 {
199     LogD("enter");
200
201     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
202     BoxInfoPtr info = This->initializeBoxInfo(arg);
203     if (!info) {
204         return -1;
205     }
206
207     LogD("--------------------------------------------");
208     LogD("boxId: %s", info->boxId.c_str());
209     LogD("InstanceId: %s", info->instanceId.c_str());
210     LogD("--------------------------------------------");
211
212     JobInfo* jobInfo = new JobInfo(REQUEST_CMD_REMOVE_BOX, info, This);
213     Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
214
215     return ret ? 0 : -1;
216 }
217
218 int BoxDaemonImpl::pdCreateCallback(ProviderEventArgPtr arg, void* data)
219 {
220     LogD("enter");
221
222     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
223     BoxInfoPtr info = This->initializeBoxInfo(arg);
224     if (!info) {
225         return -1;
226     }
227     if (arg->info.pd_create.w == 0 || arg->info.pd_create.h == 0) {
228         return -1;
229     }
230
231     info->pdWidth = arg->info.pd_create.w;
232     info->pdHeight = arg->info.pd_create.h;
233
234     LogD("--------------------------------------------");
235     LogD("boxId: %s", info->boxId.c_str());
236     LogD("InstanceId: %s", info->instanceId.c_str());
237     LogD("width: %d", info->pdWidth);
238     LogD("height: %d", info->pdHeight);
239     LogD("--------------------------------------------");
240
241     JobInfo* jobInfo = new JobInfo(REQUEST_CMD_OPEN_PD, info, This);
242     Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
243
244     return ret ? 0 : -1;
245 }
246
247 int BoxDaemonImpl::pdDestroyCallback(ProviderEventArgPtr arg, void* data)
248 {
249     LogD("enter");
250
251     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
252     BoxInfoPtr info = This->initializeBoxInfo(arg);
253     if (!info) {
254         return -1;
255     }
256
257     LogD("--------------------------------------------");
258     LogD("boxId: %s", info->boxId.c_str());
259     LogD("InstanceId: %s", info->instanceId.c_str());
260     LogD("--------------------------------------------");
261
262     JobInfo* jobInfo = new JobInfo(REQUEST_CMD_CLOSE_PD, info, This);
263     Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
264
265     return ret ? 0 : -1;
266 }
267
268 int BoxDaemonImpl::clickedCallback(ProviderEventArgPtr arg, void* data)
269 {
270     LogD("enter");
271
272     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
273     BoxInfoPtr info = This->initializeBoxInfo(arg);
274     if (!info) {
275         return -1;
276     }
277
278     int flag = web_provider_livebox_get_auto_launch(info->boxId.c_str());
279     if (!flag) {
280         return -1;
281     }
282
283     BoxDaemonUtil::launchApplication(info->boxId, info->instanceId);
284     return 0;
285 }
286
287 int BoxDaemonImpl::resizeCallback(ProviderEventArgPtr arg, void* data)
288 {
289     LogD("enter");
290
291     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
292     BoxInfoPtr info = This->initializeBoxInfo(arg);
293     if (!info) {
294         return -1;
295     }
296     if (arg->info.resize.w == 0 || arg->info.resize.h == 0) {
297         return -1;
298     }
299
300     info->boxWidth = arg->info.resize.w;
301     info->boxHeight = arg->info.resize.h;
302
303     // box information
304     LogD("--------------------------------------------");
305     LogD("boxId: %s", info->boxId.c_str());
306     LogD("InstanceId: %s", info->instanceId.c_str());
307     LogD("width: %d", info->boxWidth);
308     LogD("height: %d", info->boxHeight);
309     LogD("--------------------------------------------");
310
311     JobInfo* jobInfo = new JobInfo(REQUEST_CMD_RESIZE_BOX, info, This);
312     Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
313
314     return ret ? 0 : -1;
315 }
316
317 int BoxDaemonImpl::boxPauseCallback(ProviderEventArgPtr arg, void* data)
318 {
319     LogD("enter");
320
321     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
322     BoxInfoPtr info = This->initializeBoxInfo(arg);
323     if (!info) {
324         return -1;
325     }
326
327     // box information
328     LogD("--------------------------------------------");
329     LogD("boxId: %s", info->boxId.c_str());
330     LogD("InstanceId: %s", info->instanceId.c_str());
331     LogD("--------------------------------------------");
332
333     JobInfo* jobInfo = new JobInfo(REQUEST_CMD_PAUSE_BOX, info, This);
334     Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
335
336     return ret ? 0 : -1;
337 }
338
339 int BoxDaemonImpl::boxResumeCallback(ProviderEventArgPtr arg, void* data)
340 {
341     LogD("enter");
342
343     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
344     BoxInfoPtr info = This->initializeBoxInfo(arg);
345     if (!info) {
346         return -1;
347     }
348
349     // box information
350     LogD("--------------------------------------------");
351     LogD("boxId: %s", info->boxId.c_str());
352     LogD("InstanceId: %s", info->instanceId.c_str());
353     LogD("--------------------------------------------");
354
355     JobInfo* jobInfo = new JobInfo(REQUEST_CMD_RESUME_BOX, info, This);
356     Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
357
358     return ret ? 0 : -1;
359 }
360
361 int BoxDaemonImpl::pauseCallback(ProviderEventArgPtr arg, void* data)
362 {
363     LogD("enter");
364
365     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
366
367     LogD("--------------------------------------------");
368     LogD("web-provider is paused");
369     LogD("--------------------------------------------");
370
371     JobInfo* jobInfo = new JobInfo(REQUEST_CMD_PAUSE_ALL, BoxInfoPtr(), This);
372     Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
373
374     return ret ? 0 : -1;
375 }
376
377 int BoxDaemonImpl::resumeCallback(ProviderEventArgPtr arg, void* data)
378 {
379     LogD("enter");
380
381     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
382
383     LogD("--------------------------------------------");
384     LogD("web-provider is resumed");
385     LogD("--------------------------------------------");
386
387     JobInfo* jobInfo = new JobInfo(REQUEST_CMD_RESUME_ALL, BoxInfoPtr(), This);
388     Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
389
390     return ret ? 0 : -1;
391 }
392
393 int BoxDaemonImpl::updateContentCallback(ProviderEventArgPtr arg, void* data)
394 {
395     LogD("enter");
396
397     return 0;
398 }
399
400 int BoxDaemonImpl::changePeriodCallback(ProviderEventArgPtr arg, void* data)
401 {
402     LogD("enter");
403     BoxDaemonImpl* This = static_cast<BoxDaemonImpl*>(data);
404     BoxInfoPtr info = This->initializeBoxInfo(arg);
405     if (!info) {
406         return -1;
407     }
408     info->period = arg->info.set_period.period;
409
410     LogD("--------------------------------------------");
411     LogD("boxId: %s", info->boxId.c_str());
412     LogD("InstanceId: %s", info->instanceId.c_str());
413     LogD("period: %f", info->period);
414     LogD("--------------------------------------------");
415
416     JobInfo* jobInfo = new JobInfo(REQUEST_CMD_CHANGE_PERIOD, info, This);
417     Ecore_Job* ret = ecore_job_add(requestBoxJobCallback, jobInfo);
418
419     return ret ? 0 : -1;
420 }
421
422 void BoxDaemonImpl::setProviderCallbacks(ProviderCallbacks& callbacks)
423 {
424     LogD("enter");
425
426     memset(&callbacks, 0, sizeof(callbacks));
427     callbacks.connected = BoxDaemonImpl::connectedCallback;
428     callbacks.disconnected = BoxDaemonImpl::disconnectedCallback;
429     callbacks.pause = BoxDaemonImpl::pauseCallback;
430     callbacks.resume = BoxDaemonImpl::resumeCallback;
431     callbacks.lb_create = BoxDaemonImpl::boxCreateCallback;
432     callbacks.lb_recreate = BoxDaemonImpl::boxReCreateCallback;
433     callbacks.lb_destroy = BoxDaemonImpl::boxDestroyCallback;
434     callbacks.lb_pause = BoxDaemonImpl::boxPauseCallback;
435     callbacks.lb_resume = BoxDaemonImpl::boxResumeCallback;
436     callbacks.pd_create = BoxDaemonImpl::pdCreateCallback;
437     callbacks.pd_destroy = BoxDaemonImpl::pdDestroyCallback;
438     callbacks.clicked = BoxDaemonImpl::clickedCallback;
439     callbacks.resize = BoxDaemonImpl::resizeCallback;
440     callbacks.update_content = BoxDaemonImpl::updateContentCallback;
441     callbacks.set_period = BoxDaemonImpl::changePeriodCallback;
442 }
443
444 const char* BoxDaemonImpl::getBoxType(const char* boxId)
445 {
446     LogD("enter");
447
448     if (!boxId) {
449         return NULL;
450     }
451     
452     const char* type = web_provider_livebox_get_box_type(boxId);
453     if (!type) {
454         std::string serviceBoxId(boxId);
455         std::string boxType = m_pluginConnector->getBoxType(serviceBoxId);
456         if (boxType.empty()) {
457             LogD("unrecognized box id");
458             return NULL; 
459         }
460         type = boxType.c_str();
461     }
462
463     LogD("box id: %s, type: %s", boxId, type);
464     return type;
465 }
466
467 BoxInfoPtr BoxDaemonImpl::initializeBoxInfo(ProviderEventArgPtr arg)
468 {
469     LogD("enter");
470
471     if (!arg) {
472         return BoxInfoPtr();
473     }
474
475     if (!arg->pkgname || !arg->id) {
476         LogD("pkgname or id don't exist");
477         return BoxInfoPtr();
478     }
479
480     const char* type = getBoxType(arg->pkgname);
481     if (!type) {
482         return BoxInfoPtr();
483     }
484
485     return BoxInfoPtr(new BoxInfo(type, arg->pkgname, arg->id));
486 }
487
488 Eina_Bool BoxDaemonImpl::pingToMasterCallback(void* data)
489 {
490     LogD("enter");
491
492     provider_send_ping();
493
494     return ECORE_CALLBACK_RENEW;
495 }
496
497 void BoxDaemonImpl::requestBoxJobCallback(void* data)
498 {
499     JobInfo* jobInfo = static_cast<JobInfo*>(data);
500     if (!jobInfo) {
501         LogD("no information for job");
502         return;
503     }
504
505     // just for debugging
506     //volatile int flag = 0;
507     //while(flag == 0) {;}
508
509     // request box job!
510     jobInfo->daemonImpl->m_pluginConnector->requestCommand(
511             jobInfo->cmdType, jobInfo->boxInfo);
512 }