6ec33b2c99548c21ce6f127d2e8386fc81b2a795
[platform/framework/native/appwidget-service.git] / src / FShell_AppWidgetManagerService.cpp
1 //
2 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (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 /**
18  * @file        FShell_AppWidgetManagerService.cpp
19  * @brief       This is the implementation for the AppWidgetManagerService class.
20  */
21
22 #include <unique_ptr.h>
23 #include <provider.h>
24 #include <errno.h>
25
26 #include <FBase.h>
27 #include <FBaseSysLog.h>
28 #include <FBaseColIList.h>
29 #include <FAppApp.h>
30 #include <FSystem.h>
31 #include <FBaseComparerT.h>
32 #include <FApp_AppManagerImpl.h>
33 #include <FShell_AppWidgetManagerImpl.h>
34 #include <FIo_IpcServer.h>
35 #include <FBase_StringConverter.h>
36
37 #include "FShell_AppContext.h"
38 #include "FShell_AppWidgetPopupContext.h"
39 #include "FShell_AppWidgetManagerService.h"
40 #include "FShell_TemplateUtil.h"
41
42 namespace Tizen { namespace Shell  { namespace App {
43
44 using namespace std;
45 using namespace Tizen::App;
46 using namespace Tizen::Base;
47 using namespace Tizen::Base::Collection;
48 using namespace Tizen::Base::Runtime;
49 using namespace Tizen::Io;
50 using namespace Tizen::System;
51 using namespace Tizen::Shell::App;
52
53 namespace
54 {
55 // provider.h
56 enum instance_destroy_type
57 {
58         INSTANCE_DESTROY_DEFAULT,
59         INSTANCE_DESTROY_PKGMGR,
60         INSTANCE_DESTROY_TERMINATE,
61         INSTANCE_DESTROY_FAULT,
62         INSTANCE_DESTROY_UNKNOWN,
63 };
64
65 static const char APPNAME_OSP_APPWIDGET_SERVICE[] = "osp-appwidget-service";
66 static const int TIMER_DURATION_PING = 120000;
67 //extern const int UPDATE_PERIOD_MSEC_MIN;
68 }
69
70
71 AppWidgetManagerService* AppWidgetManagerService::__pTheInstance = null;
72
73 AppWidgetManagerService::AppWidgetManagerService(void)
74 {
75
76 }
77
78 AppWidgetManagerService::~AppWidgetManagerService(void)
79 {
80         SysLog(NID_SHELL, "Enter");
81
82         __pingTimer.Cancel();
83
84         DeinitializeCoreDaemonEventReceiver();
85
86         IMapEnumeratorT<String, _AppContext*>* pMapEnum = __appContextList.GetMapEnumeratorN();
87         if (pMapEnum != null)
88         {
89                 while (pMapEnum->MoveNext() == E_SUCCESS)
90                 {
91                         _AppContext* pAppContext = null;
92                         pMapEnum->GetValue(pAppContext);
93                         delete pAppContext;
94                 }
95
96                 delete pMapEnum;
97         }
98
99         SysLog(NID_SHELL, "Exit");
100 }
101
102 void
103 AppWidgetManagerService::InitSingleton(void)
104 {
105         unique_ptr<AppWidgetManagerService> pInstance(new (nothrow) AppWidgetManagerService());
106         SysTryReturnVoidResult(NID_SHELL, pInstance, E_OUT_OF_MEMORY, "The memory is insufficient.");
107
108         __pTheInstance = pInstance.release();
109         std::atexit(DestroySingleton);
110 }
111
112 void
113 AppWidgetManagerService::DestroySingleton(void)
114 {
115         delete __pTheInstance;
116 }
117
118 AppWidgetManagerService*
119 AppWidgetManagerService::GetInstance(void)
120 {
121         if (__pTheInstance == null)
122         {
123                 __pTheInstance = CreateInstance(APPNAME_OSP_APPWIDGET_SERVICE);
124         }
125         return __pTheInstance;
126 }
127
128 AppWidgetManagerService*
129 AppWidgetManagerService::CreateInstance(const char* pCoreDaemonId)
130 {
131         ClearLastResult();
132
133         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
134
135         if (__pTheInstance == null)
136         {
137                 pthread_once(&onceBlock, InitSingleton);
138                 result r = GetLastResult();
139                 if (IsFailed(r))
140                 {
141                         onceBlock = PTHREAD_ONCE_INIT;
142                 }
143
144                 r = __pTheInstance->Construct(pCoreDaemonId);
145                 SysAssertf(!IsFailed(r), "Failed to construct AppWidgetManagerService");
146                 SysLog(NID_SHELL, "AppWidgetManagerService is created.");
147         }
148         return __pTheInstance;
149 }
150
151 result
152 AppWidgetManagerService::Construct(const char* pCoreDaemonId)
153 {
154         result r = __appContextList.Construct();
155         SysTryReturnResult(NID_SHELL, !IsFailed(r), E_SYSTEM, "Failed to execute Construct.");
156
157         _AppWidgetManagerStub::StartIpcServer();
158
159         SettingInfo::AddSettingEventListener(*this);
160
161         return InitializeCoreDaemonEventReceiver(pCoreDaemonId);
162 }
163
164 int
165 AppWidgetManagerService::OnAppWidgetServiceConnected(struct event_arg *arg, void* data)
166 {
167     int ret;
168     ret = provider_send_hello();
169     if (ret == 0)
170     {
171         SysLog(NID_SHELL, "Success to be connected with master daemon");
172         AppWidgetManagerService::GetInstance()->StartPingTimer();
173
174     }
175     else
176     {
177         SysLog(NID_SHELL, "Failed to invoke provider_send_hello()");
178     }
179     return ret;
180 }
181
182 int
183 AppWidgetManagerService::OnAppWidgetServiceCDisconnected(struct event_arg *arg, void* data)
184 {
185         SysLog(NID_SHELL, "Disconnected with master daemon");
186     return 0;
187 }
188
189 void
190 AppWidgetManagerService::StartPingTimer(void)
191 {
192         result r = E_SUCCESS;
193
194         r = __pingTimer.Construct(*this);
195         SysTryReturnVoidResult(NID_SHELL, !IsFailed(r), E_SYSTEM, "");
196
197         r = __pingTimer.StartAsRepeatable(TIMER_DURATION_PING);
198         SysTryReturnVoidResult(NID_SHELL, !IsFailed(r), E_SYSTEM, "");
199 }
200
201 void
202 AppWidgetManagerService::OnTimerExpired(Timer& timer)
203 {
204         provider_send_ping();
205 }
206
207 _AppContext*
208 AppWidgetManagerService::FindAppContext(const Tizen::App::AppId& appId) const
209 {
210     _AppContext* pAppContext = null;
211         result r = __appContextList.GetValue(appId, pAppContext);
212         SysTryReturn(NID_SHELL, !IsFailed(r), null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND]");
213
214         return pAppContext;
215 }
216
217 result
218 AppWidgetManagerService::RemoveAppContext(const Tizen::App::AppId& appId)
219 {
220         return __appContextList.Remove(appId);
221 }
222
223 _AppWidgetContext*
224 AppWidgetManagerService::FindAppWidget(const AppId& appId, const String& instanceId) const
225 {
226         _AppContext* pAppContext = null;
227         result r = __appContextList.GetValue(appId, pAppContext);
228         SysTryReturn(NID_SHELL, !IsFailed(r), null, E_OBJ_NOT_FOUND, "appId(%ls) is not found", appId.GetPointer() );
229
230         return pAppContext->FindAppWidget(instanceId);
231 }
232
233 void
234 AppWidgetManagerService::OnIpcClientConnected(const _IpcServer& server, int clientId)
235 {
236         String appId = server.GetClientApplicationId();
237         SysLog(NID_SHELL, "appId(%ls)", appId.GetPointer());
238
239         _AppContext* pAppContext = null;
240         __appContextList.GetValue(appId, pAppContext);
241         if (pAppContext)
242         {
243                 pAppContext->OnIpcConnected(clientId);
244         }
245 }
246
247 void
248 AppWidgetManagerService::OnIpcClientDisconnected(const _IpcServer& server, int clientId)
249 {
250         String appId = server.GetClientApplicationId();
251         SysLog(NID_SHELL, "appId(%ls)", appId.GetPointer());
252
253         _AppContext* pAppContext = null;
254         __appContextList.GetValue(appId, pAppContext);
255         if (pAppContext)
256         {
257                 pAppContext->OnIpcDisconnected();
258         }
259 }
260
261 void
262 AppWidgetManagerService::OnSettingChanged(Tizen::Base::String& key)
263 {
264 //      const static wchar_t KEY_SETTING_FONT_SIZE[] = L"http://tizen.org/setting/font.size";
265         const static wchar_t KEY_SETTING_FONT_TYPE[] = L"http://tizen.org/setting/font.type";
266         const static wchar_t KEY_SETTING_LANGUAGE[] = L"http://tizen.org/setting/locale.language";
267         const static wchar_t KEY_SETTING_COUNTRY[] = L"http://tizen.org/setting/locale.country";
268
269         if( key == KEY_SETTING_FONT_TYPE
270 //              || key == KEY_SETTING_FONT_SIZE
271                 || key == KEY_SETTING_LANGUAGE
272                 || key == KEY_SETTING_COUNTRY )
273         {
274                 SysLog(NID_SHELL, "'%ls' is changed.", key.GetPointer() );
275                 RequestUpdateAllSuspened();
276         }
277 }
278
279 result
280 AppWidgetManagerService::AddAppWidget(const Tizen::Base::String& userInfo, const Tizen::Base::String& providerId, const Tizen::Base::String& instanceId, int width, int height, int period, int priority)
281 {
282         AppId appId;
283         String providerName;
284         _AppWidgetManagerImpl::ExtractAppIdAndProviderName(providerId, appId, providerName);
285
286         _AppContext* pAppContext = null;
287         bool containsKey = false;
288         __appContextList.ContainsKey(appId, containsKey);
289         if( containsKey == false)
290         {
291                 pAppContext = new(std::nothrow) _AppContext(appId);
292                 SysTryReturnResult(NID_SHELL, pAppContext, E_OUT_OF_MEMORY, "");
293                 __appContextList.Add(appId, pAppContext);
294         }
295         else
296         {
297                 __appContextList.GetValue(appId, pAppContext);
298         }
299
300         return pAppContext->AddAppWidget(userInfo, providerId, instanceId, width, height, period, priority);
301 }
302
303 ///////////////////////////////////////////////////////
304 // CoreDaemonEventReceiver implementation
305 ///////////////////////////////////////////////////////
306 int
307 AppWidgetManagerService::OnAppWidgetCreate(struct event_arg *arg, int *width, int *height, double *priority, void* data)
308 {
309         SysAssertf(arg != null && arg->pkgname != null && arg->id != null && arg->type == event_arg::EVENT_NEW, "The status of data-provider-master is invalid.");
310         SysSecureLog(NID_SHELL, "providerId(%s) id(%s) content(%s) cluster(%s) category(%s)", arg->pkgname, arg->id, arg->info.lb_create.content, arg->info.lb_create.cluster, arg->info.lb_create.category);
311
312         const int MAX_LENGTH = 0xFFFF;// FUi_Control.h
313         *width = arg->info.lb_create.width;
314         *height= arg->info.lb_create.height;
315         *priority = 1.0f;// Fixed as default
316         SysAssertf(*width >= 0 && *height >= 0 && *width <= MAX_LENGTH && *height <= MAX_LENGTH, "w:%d, h:%d", *width, *height);
317
318         result r = AppWidgetManagerService::GetInstance()->AddAppWidget(arg->info.lb_create.content, arg->pkgname, arg->id, *width, *height, arg->info.lb_create.period*1000, *priority);
319         SysTryReturn(NID_SHELL, !IsFailed(r), -1, r, "[%s] Failed to execute AddAppWidget.", GetErrorMessage(r));
320
321     return 0;
322 }
323
324 int
325 AppWidgetManagerService::OnAppWidgetUpdate(struct event_arg *arg, void* data)
326 {
327         SysAssertf(arg != null && arg->type == event_arg::EVENT_UPDATE_CONTENT, "The status of data-provider-master is invalid.");
328
329         bool isInstanceIdSpecified = (arg->id == null || strlen(arg->id) < 1) ? false : true;
330         if (isInstanceIdSpecified == false)
331         {
332                 AppId appId;
333                 String providerName;
334                 _AppWidgetManagerImpl::ExtractAppIdAndProviderName(arg->pkgname, appId, providerName);
335
336                 AppWidgetManagerService::GetInstance()->RequestUpdate(appId, providerName, L"");
337         }
338         else
339         {
340                 _AppContext* pAppContext = AppWidgetManagerService::GetInstance()->FindAppContext(_AppWidgetHelper::ExtractAppId(arg->pkgname));
341                 SysTryReturn(NID_SHELL, pAppContext != null, -1, E_OBJ_NOT_FOUND, "Failed to find _AppContext.");
342
343                 result r = pAppContext->UpdateAppWidget(arg->id);
344                 SysTryReturn(NID_SHELL, !IsFailed(r), -1, E_OBJ_NOT_FOUND, "Failed to update _AppContext.");
345         }
346
347         return 0;
348 }
349
350 int
351 AppWidgetManagerService::OnAppWidgetDestroy(struct event_arg *arg, void* data)
352 {
353         SysAssertf(arg != null && arg->type == event_arg::EVENT_DELETE, "The status of data-provider-master is invalid.");
354         SysLog(NID_SHELL, "lb_destroy.type (%d)", arg->info.lb_destroy.type);
355
356         if (arg->info.lb_destroy.type == INSTANCE_DESTROY_PKGMGR)
357         {
358                 SysLog(NID_SHELL, "INSTANCE_DESTROY_PKGMGR type, ignored.");
359                 return 0;
360         }
361
362         String appId = _AppWidgetHelper::ExtractAppId(arg->pkgname);
363         _AppContext* pAppContext = AppWidgetManagerService::GetInstance()->FindAppContext(appId);
364         SysTryReturn(NID_SHELL, pAppContext != null, -1, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Failed to find _AppContext.");
365
366         pAppContext->DestroyAppWidget(arg->id);
367
368         if (pAppContext->GetProviderCount() == 0)
369         {
370                 SysLog(NID_SHELL, "The provider count for (%ls) is 0.", appId.GetPointer());
371                 AppWidgetManagerService::GetInstance()->RemoveAppContext(appId);
372                 delete pAppContext;
373         }
374
375         return 0;
376 }
377
378 int
379 AppWidgetManagerService::OnAppWidgetPopupCreate(struct event_arg *arg, void* data)
380 {
381         SysAssertf(arg != null && arg->type == event_arg::EVENT_PD_CREATE, "The status of data-provider-master is invalid.");
382
383         SysSecureLog(NID_SHELL, "providerId(%s), id(%s), x(%d), y(%d), width(%f), height(%f), priority(%d)", arg->pkgname, arg->id, arg->info.pd_create.x, arg->info.pd_create.y, arg->info.pd_create.w, arg->info.pd_create.h);
384
385         _AppContext* pAppContext = AppWidgetManagerService::GetInstance()->FindAppContext(_AppWidgetHelper::ExtractAppId(arg->pkgname));
386         SysTryReturn(NID_SHELL, pAppContext != null, -1, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Failed to find _AppContext.");
387
388         pAppContext->CreateAppWidgetPopup(arg->id, arg->info.pd_create.x, arg->info.pd_create.y, arg->info.pd_create.w, arg->info.pd_create.h);
389
390         return 0;
391 }
392
393 int
394 AppWidgetManagerService::OnAppWidgetPopupDestroy(struct event_arg *arg, void* data)
395 {
396         SysAssertf(arg != null && arg->type == event_arg::EVENT_PD_DESTROY, "The status of data-provider-master is invalid.");
397
398         SysSecureLog(NID_SHELL, "providerId(%s), id(%s)", arg->pkgname, arg->id);
399
400         _AppContext* pAppContext = AppWidgetManagerService::GetInstance()->FindAppContext(_AppWidgetHelper::ExtractAppId(arg->pkgname));
401         SysTryReturn(NID_SHELL, pAppContext != null, -1, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Failed to find _AppContext.");
402
403         pAppContext->DestroyAppWidgetPopup(arg->id);
404
405         return 0;
406 }
407
408 int
409 AppWidgetManagerService::OnAppWidgetBackground(struct event_arg *arg, void* data)
410 {
411         SysAssertf(arg != null && arg->type == event_arg::EVENT_LB_PAUSE, "The status of data-provider-master is invalid.");
412
413         SysSecureLog(NID_SHELL, "providerId(%s), id(%s)", arg->pkgname, arg->id);
414
415         _AppContext* pAppContext = AppWidgetManagerService::GetInstance()->FindAppContext(_AppWidgetHelper::ExtractAppId(arg->pkgname));
416         SysTryReturn(NID_SHELL, pAppContext != null, -1, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Failed to find _AppContext.");
417
418         pAppContext->OnAppWidgetBackground(arg->id);
419
420         return 0;
421 }
422
423 int
424 AppWidgetManagerService::OnAppWidgetBackgroundAll(struct event_arg *arg, void* data)
425 {
426         SysAssertf(arg != null && arg->type == event_arg::EVENT_PAUSE, "The status of data-provider-master is invalid.");
427
428         unique_ptr< IMapEnumeratorT<String, _AppContext*> > pAppContextEnum(AppWidgetManagerService::GetInstance()->__appContextList.GetMapEnumeratorN());
429         SysTryReturnResult(NID_SHELL, pAppContextEnum, E_SYSTEM, "Failed to invoke __appContextList.GetMapEnumeratorN()");
430
431         while (pAppContextEnum->MoveNext() == E_SUCCESS)
432         {
433                 _AppContext* pAppContext = null;
434                 pAppContextEnum->GetValue(pAppContext);
435
436                 if (pAppContext)
437                 {
438                         pAppContext->OnAppWidgetBackgroundAll();
439                 }
440         }
441
442         return 0;
443 }
444
445 int
446 AppWidgetManagerService::OnAppWidgetForeground(struct event_arg *arg, void* data)
447 {
448         SysAssertf(arg != null && arg->type == event_arg::EVENT_LB_RESUME, "The status of data-provider-master is invalid.");
449
450         SysTryReturn(NID_SHELL, arg || arg->id || arg->pkgname, 0, E_SUCCESS, "arg is null!");
451         SysSecureLog(NID_SHELL, "providerId(%s), id(%s)", arg->pkgname, arg->id);
452
453         _AppContext* pAppContext = AppWidgetManagerService::GetInstance()->FindAppContext(_AppWidgetHelper::ExtractAppId(arg->pkgname));
454         SysTryReturn(NID_SHELL, pAppContext != null, -1, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Failed to find _AppContext.");
455
456         pAppContext->OnAppWidgetForeground(arg->id);
457
458         return 0;
459 }
460
461 int
462 AppWidgetManagerService::OnAppWidgetForegroundAll(struct event_arg *arg, void* data)
463 {
464         SysAssertf(arg != null && arg->type == event_arg::EVENT_RESUME, "The status of data-provider-master is invalid.");
465
466         unique_ptr< IMapEnumeratorT<String, _AppContext*> > pAppContextEnum(AppWidgetManagerService::GetInstance()->__appContextList.GetMapEnumeratorN());
467         SysTryReturnResult(NID_SHELL, pAppContextEnum, E_SYSTEM, "Failed to invoke __appContextList.GetMapEnumeratorN()");
468
469         while (pAppContextEnum->MoveNext() == E_SUCCESS)
470         {
471                 _AppContext* pAppContext = null;
472                 pAppContextEnum->GetValue(pAppContext);
473
474                 if (pAppContext)
475                 {
476                         pAppContext->OnAppWidgetForegroundAll();
477                 }
478         }
479
480         return 0;
481 }
482
483 int
484 AppWidgetManagerService::OnAppWidgetClick(struct event_arg *arg, void* data)
485 {
486         SysAssertf(arg != null && arg->type == event_arg::EVENT_CLICKED, "The status of data-provider-master is invalid.");
487
488         SysSecureLog(NID_SHELL, "providerId(%s), id(%s), clicked.event(%s), clicked.x(%f), clicked.y(%f)", arg->pkgname, arg->id, arg->info.clicked.event, arg->info.clicked.x, arg->info.clicked.y);
489
490     return 0;
491 }
492
493 int
494 AppWidgetManagerService::OnAppWidgetResize(struct event_arg *arg, void* data)
495 {
496         SysAssertf(arg != null && arg->type == event_arg::EVENT_RESIZE, "The status of data-provider-master is invalid.");
497
498         SysSecureLog(NID_SHELL, "providerId(%s), id(%s), resize.w(%d), resize.h(%d)", arg->pkgname, arg->id, arg->info.resize.w, arg->info.resize.h);
499
500         _AppContext* pAppContext = AppWidgetManagerService::GetInstance()->FindAppContext(_AppWidgetHelper::ExtractAppId(arg->pkgname));
501         SysTryReturn(NID_SHELL, pAppContext != null, -1, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Failed to find _AppContext.");
502
503         pAppContext->ResizeAppWidget(arg->id, arg->info.resize.w, arg->info.resize.h);
504
505     return 0;
506 }
507
508 int
509 AppWidgetManagerService::OnAppWidgetPeriodChanged(struct event_arg *arg, void* data)
510 {
511         SysAssertf(arg != null && arg->type == event_arg::EVENT_SET_PERIOD, "The status of data-provider-master is invalid.");
512
513         SysSecureLog(NID_SHELL, "providerId(%s), id(%s), width(%d), height(%d), priority(%d)", arg->pkgname, arg->id);
514
515     return 0;
516 }
517
518 int
519 AppWidgetManagerService::OnAppWidgetRecreate(struct event_arg *arg, void* data)
520 {
521         const int MAX_LENGTH = 0xFF;// FUi_Control.h
522         SysAssertf(arg != null && arg->pkgname != null && arg->id != null && arg->type == event_arg::EVENT_RENEW, "The status of data-provider-master is invalid.");
523         SysAssertf(arg->info.lb_recreate.width >= 0 && arg->info.lb_recreate.height >= 0 && arg->info.lb_recreate.width <= MAX_LENGTH && arg->info.lb_recreate.height <= MAX_LENGTH, "w:%d, h:%d", arg->info.lb_recreate.width, arg->info.lb_recreate.height);
524
525         SysSecureLog(NID_SHELL, "providerId(%s) id(%s) content(%s) cluster(%s) category(%s)", arg->pkgname, arg->id, arg->info.lb_recreate.content, arg->info.lb_recreate.cluster, arg->info.lb_recreate.category);
526
527         AppWidgetManagerService* pAppWidgetManagerService = AppWidgetManagerService::GetInstance();
528         SysTryReturn(NID_SHELL, pAppWidgetManagerService != null, -EBUSY, E_SYSTEM, "Failed to get the instance for AppWidgetManagerService.");
529
530         const double default_priority = 1.0f;
531         result r = pAppWidgetManagerService->AddAppWidget(arg->info.lb_recreate.content, arg->pkgname, arg->id, arg->info.lb_recreate.width, arg->info.lb_recreate.height, arg->info.lb_recreate.period * 1000, default_priority);
532         SysTryReturn(NID_SHELL, !IsFailed(r), -EBUSY, r, "[%s] Failed to execute AddAppWidget.", GetErrorMessage(r));
533
534         return 0;
535 }
536
537 result
538 AppWidgetManagerService::InitializeCoreDaemonEventReceiver(const char *pCoreDaemonId)
539 {
540         SysTryReturnResult(NID_SHELL, pCoreDaemonId != null, E_INVALID_ARG, "pCoreDaemonId should not be null!");
541
542     struct event_handler cbs;
543     memset(&cbs, 0, sizeof(event_handler));
544
545     cbs.connected = OnAppWidgetServiceConnected,
546     cbs.disconnected = OnAppWidgetServiceCDisconnected,
547     cbs.pause = OnAppWidgetBackgroundAll,
548     cbs.resume = OnAppWidgetForegroundAll,
549     cbs.lb_pause = OnAppWidgetBackground,
550     cbs.lb_resume = OnAppWidgetForeground,
551     cbs.lb_create = OnAppWidgetCreate,
552     cbs.lb_destroy = OnAppWidgetDestroy,
553     cbs.update_content = OnAppWidgetUpdate,
554     cbs.pd_create = OnAppWidgetPopupCreate,
555     cbs.pd_destroy = OnAppWidgetPopupDestroy,
556     cbs.clicked = OnAppWidgetClick,
557     cbs.resize = OnAppWidgetResize,
558     cbs.set_period = OnAppWidgetPeriodChanged;
559     cbs.lb_recreate = OnAppWidgetRecreate;/* Recover from the fault of slave */
560     //cbs.content_event = OnAppWidgetContentEvent,
561
562     int ret = provider_init(null, pCoreDaemonId, &cbs, this);
563     SysTryReturnResult(NID_SHELL, ret == 0, E_SYSTEM, "provider_init failed.");
564
565     SysLog(NID_SHELL, "provider_init is invoked with (%s)", pCoreDaemonId);
566     return E_SUCCESS;
567 }
568
569 result
570 AppWidgetManagerService::DeinitializeCoreDaemonEventReceiver(void)
571 {
572     provider_fini();
573     return E_SUCCESS;
574 }
575
576 ///////////////////////////////////////////////////////
577 // stub implementation
578 ///////////////////////////////////////////////////////
579 result
580 AppWidgetManagerService::RequestUpdate(const Tizen::App::AppId& appId, const Tizen::Base::String& providerName, const Tizen::Base::String& argument) const
581 {
582         _AppContext* pAppContext = null;
583         result r = __appContextList.GetValue(appId, pAppContext);
584         SysTryReturn(NID_SHELL, !IsFailed(r), null, E_OBJ_NOT_FOUND, "appId(%ls) is not found", appId.GetPointer() );
585
586         return pAppContext->RequestUpdate(providerName, argument);
587 }
588
589 result
590 AppWidgetManagerService::RequestUpdateAllSuspened(void) const
591 {
592         unique_ptr< IMapEnumeratorT<String, _AppContext*> > pAppContextEnum(__appContextList.GetMapEnumeratorN());
593         SysTryReturnResult(NID_SHELL, pAppContextEnum, E_SYSTEM, "Failed to invoke __appContextList.GetMapEnumeratorN()");
594
595         while (pAppContextEnum->MoveNext() == E_SUCCESS)
596         {
597                 _AppContext* pAppContext = null;
598                 pAppContextEnum->GetValue(pAppContext);
599
600                 if ( pAppContext && _AppManagerImpl::GetInstance()->IsRunning(pAppContext->GetAppId()) == false )
601                 {
602                         pAppContext->RequestUpdate(L"", L"");
603                 }
604         }
605
606         SysLog(NID_SHELL, "Exit.");
607         return E_SUCCESS;
608 }
609
610 result
611 AppWidgetManagerService::RequestUpdateInstance(const Tizen::App::AppId& appId, const Tizen::Base::String& instanceId, const Tizen::Base::String& argument)
612 {
613         SysSecureLog(NID_SHELL, "%ls, %ls", instanceId.GetPointer(), argument.GetPointer() );
614
615         _AppContext* pAppContext = AppWidgetManagerService::GetInstance()->FindAppContext(appId);
616         SysTryReturnResult(NID_SHELL, pAppContext != null, E_OBJ_NOT_FOUND, "Failed to find _AppContext.");
617
618         result r = pAppContext->RequestUpdateInstance(instanceId, argument);
619         SysTryReturnResult(NID_SHELL, !IsFailed(r), E_OBJ_NOT_FOUND, "Failed to execute RequestUpdateInstance.");
620
621         return E_SUCCESS;
622 }
623
624 result
625 AppWidgetManagerService::AcquireRemoteBuffer(const Tizen::App::AppId& appId, const Tizen::Base::String& instanceId, int width, int height, int& bufferId)
626 {
627         _AppContext* pAppContext = AppWidgetManagerService::GetInstance()->FindAppContext(appId);
628         SysTryReturnResult(NID_SHELL, pAppContext != null, E_OBJ_NOT_FOUND, "Failed to find _AppContext.");
629
630         pAppContext->AcquireRemoteBuffer(instanceId, width, height, bufferId);
631         SysSecureTryReturnResult(NID_SHELL, bufferId != -1, E_SYSTEM, "Failed to execute AcquireRemoteBuffer for (%ls) (%ls)", appId.GetPointer(), instanceId.GetPointer());
632
633     return E_SUCCESS;
634 }
635
636 result
637 AppWidgetManagerService::AcquireRemoteBufferForPD(const Tizen::App::AppId& appId, const Tizen::Base::String& instanceId, int width, int height, int& bufferId)
638 {
639         _AppContext* pAppContext = AppWidgetManagerService::GetInstance()->FindAppContext(appId);
640         SysTryReturnResult(NID_SHELL, pAppContext != null, E_OBJ_NOT_FOUND, "Failed to find _AppContext.");
641
642         pAppContext->AcquireRemoteBufferForPD(instanceId, width, height, bufferId);
643         SysSecureTryReturnResult(NID_SHELL, bufferId != -1, E_SYSTEM, "Failed to execute AcquireRemoteBufferForPD for (%ls) (%ls)", appId.GetPointer(), instanceId.GetPointer());
644
645         return E_SUCCESS;
646 }
647
648 result
649 AppWidgetManagerService::SyncRemoteBuffer(const Tizen::App::AppId& appId, const Tizen::Base::String& instanceId, int width, int height)
650 {
651         _AppContext* pAppContext = AppWidgetManagerService::GetInstance()->FindAppContext(appId);
652         SysTryReturnResult(NID_SHELL, pAppContext != null, E_OBJ_NOT_FOUND, "Failed to find _AppContext.");
653
654         return pAppContext->SyncRemoteBuffer(instanceId, width, height);
655 }
656
657 result
658 AppWidgetManagerService::SyncRemoteBufferForPD(const Tizen::App::AppId& appId, const Tizen::Base::String& instanceId)
659 {
660         _AppContext* pAppContext = AppWidgetManagerService::GetInstance()->FindAppContext(appId);
661         SysTryReturnResult(NID_SHELL, pAppContext != null, E_OBJ_NOT_FOUND, "Failed to find _AppContext.");
662
663         return pAppContext->SyncRemoteBufferForPD(instanceId);
664 }
665
666 result
667 AppWidgetManagerService::ReleaseRemoteBuffer(const Tizen::App::AppId& appId, const Tizen::Base::String& instanceId)
668 {
669         return E_SUCCESS;
670 }
671
672 result
673 AppWidgetManagerService::ReleaseRemoteBufferForPD(const Tizen::App::AppId& appId, const Tizen::Base::String& instanceId)
674 {
675         return E_SUCCESS;
676 }
677
678 result
679 AppWidgetManagerService::RequestProviderCount(const Tizen::App::AppId& appId, const Tizen::Base::String& instanceId, int& providerCount)
680 {
681         SysLog(NID_SHELL, "ENTER");
682
683         _AppContext* pAppContext = null;
684         result r = __appContextList.GetValue(appId, pAppContext);
685         SysTryReturnResult(NID_SHELL, pAppContext, E_OBJ_NOT_FOUND, "Failed to find _AppContext.");
686
687         providerCount = pAppContext->GetProviderCount();
688
689         r = pAppContext->DestroyAppWidget(instanceId, false);
690         SysTryReturnResult(NID_SHELL, !IsFailed(r), E_SYSTEM, "Failed to execute DestroyAppWidget.");
691
692         providerCount--;
693
694         pAppContext->SendPendingEvent();
695
696         return E_SUCCESS;
697 }
698
699 result
700 AppWidgetManagerService::SendResult(const Tizen::App::AppId& appId, const Tizen::Base::String& instanceId, bool isSucceeded)
701 {
702         _AppContext* pAppContext = null;
703         result r = __appContextList.GetValue(appId, pAppContext);
704         SysTryReturnResult(NID_SHELL, !IsFailed(r), E_OBJ_NOT_FOUND, "appId(%ls) is not found.", appId.GetPointer());
705
706         if (!isSucceeded)
707         {
708                 r = pAppContext->DestroyAppWidget(instanceId, false);
709                 SysTryReturnResult(NID_SHELL, !IsFailed(r), E_SYSTEM, "Failed to execute RemoveAppWidget.");
710         }
711         else    // for pending event excluding touch event.
712         {
713                 SysLog(NID_SHELL, "SendResult is called[true].");
714         }
715
716         pAppContext->SendPendingEvent();
717
718         return E_SUCCESS;
719 }
720
721 result
722 AppWidgetManagerService::SendAccessStatus(const Tizen::App::AppId& appId, const Tizen::Base::String& instanceId, int accessStatus)
723 {
724         _AppContext* pAppContext = AppWidgetManagerService::GetInstance()->FindAppContext(appId);
725         SysTryReturnResult(NID_SHELL, pAppContext != null, E_OBJ_NOT_FOUND, "Failed to find _AppContext.");
726
727         return pAppContext->SendAccessStatus(instanceId, accessStatus);
728 }
729
730 void
731 AppWidgetManagerService::OnUserEventReceivedN(RequestId reqId, IList* pArgs)
732 {
733         SysTryReturnVoidResult(NID_SHELL, pArgs != null, E_INVALID_STATE, "pArgs is null!.");
734
735         SysLog(NID_SHELL, "Enter.");
736
737         switch (reqId)
738         {
739         case LOCAL_EVENT_REQUEST_UPDATE:
740         {
741                 _AppContext* pAppContext = dynamic_cast<_AppContext*>( pArgs->GetAt(0) );
742                 SysTryReturnVoidResult(NID_SHELL, pAppContext != null, E_OBJ_NOT_FOUND, "");
743
744                 pAppContext->HandleUserEvent(pArgs);
745
746                 pArgs->RemoveAll();
747                 delete pArgs;
748         }
749                 break;
750
751         default:
752                 SysAssertf(false, "Never get here!");
753                 break;
754         }
755
756         SysLog(NID_SHELL, "Exit.");
757 }
758
759 // _AppWidgetHelper
760 AppId
761 _AppWidgetHelper::ExtractAppId(const String& providerId)
762 {
763         int indexOfSecondDot = 0;
764         result r = providerId.IndexOf(".", 12, indexOfSecondDot);//"1234567890._"
765         SysTryReturn(NID_SHELL, !IsFailed(r), L"", E_INVALID_ARG, "[E_INVALID_ARG] Failed to get the index from providerId(%ls).", providerId.GetPointer() );
766
767         String appId;
768         providerId.SubString(0, indexOfSecondDot, appId);
769         return appId;
770 }
771
772 String
773 _AppWidgetHelper::ExtractProviderName(const String& providerId)
774 {
775         int indexOfSecondDot = 0;
776         result r = providerId.IndexOf(".", 12, indexOfSecondDot);//"1234567890._"
777         SysTryReturn(NID_SHELL, !IsFailed(r), L"", E_INVALID_ARG, "[E_INVALID_ARG] Failed to get the index from providerId(%ls).", providerId.GetPointer() );
778
779         String providerName;
780         providerId.SubString(indexOfSecondDot + 1, providerName);
781         return providerName;
782 }
783
784 }}}     // Tizen::Shell::App