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