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