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