7886c9ef685305cf028b2ef37b1e4f57e4f86e5d
[platform/framework/native/appwidget-service.git] / src / FShell_AppWidgetManagerService.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.1 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FShell_AppWidgetManagerService.cpp
20  * @brief       This is the implementation for the AppWidgetManagerService class.
21  */
22
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <unique_ptr.h>
26 #include <provider.h>
27 #include <aul.h>
28
29 #include <FBase.h>
30 #include <FBaseSysLog.h>
31 #include <FApp.h>
32 #include <FApp_AppManagerImpl.h>
33 #include <FIo_IpcServer.h>
34 #include <FBase_StringConverter.h>
35
36 #include "FShell_AppWidgetContext.h"
37 #include "FShell_AppWidgetPopupContext.h"
38 #include "FShell_AppWidgetManagerStub.h"
39
40 #include "FShell_AppWidgetManagerService.h"
41
42 namespace Tizen { namespace Shell  { namespace App {
43
44 using namespace Tizen::App;
45 using namespace Tizen::Base;
46 using namespace Tizen::Base::Collection;
47 using namespace Tizen::Base::Runtime;
48 using namespace Tizen::Io;
49 using namespace Tizen::System;
50 using namespace Tizen::Shell::App;
51
52
53 namespace
54 {
55 static const RequestId  LOCAL_EVENT_REQUEST_UPDATE = 0;
56 }
57
58 AppWidgetManagerService::AppWidgetManagerService()
59 {
60 }
61
62 AppWidgetManagerService::~AppWidgetManagerService()
63 {
64         __pingTimer.Cancel();
65         DeinitializeMasterDaemonEventReceiver();
66 }
67
68 AppWidgetManagerService*
69 AppWidgetManagerService::GetInstance(void)
70 {
71         static AppWidgetManagerService* pSelf = null;
72         if( pSelf == null)
73         {
74                 pSelf = new AppWidgetManagerService();
75                 SysTryReturn(NID_SHELL, pSelf != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY]");
76
77                 result r = pSelf->Construct();
78                 SysAssertf(!IsFailed(r), "Failed to construct AppWidgetManagerService");
79                 SysLog(NID_SHELL, "AppWidgetManagerService is created.");
80         }
81         return pSelf;
82 }
83
84 result
85 AppWidgetManagerService::Construct()
86 {
87 //      _AppManagerImpl::GetInstance()->AddEventListener(*this);
88
89         _AppWidgetManagerStub::StartIpcServer();
90
91         result r = __handlerThread.Construct(THREAD_TYPE_EVENT_DRIVEN);
92         SysTryReturn(NID_SHELL, IsFailed(r) == false, false, r, "[%s] Event thread creation failure.", GetErrorMessage(r));
93
94         r = __handlerThread.Start();
95         SysTryReturn(NID_SHELL, IsFailed(r) == false, false, r, "[%s] Event thread Start failure.", GetErrorMessage(r));
96
97         return InitializeMasterDaemonEventReceiver("osp-appwidget-service");
98 }
99
100 int
101 AppWidgetManagerService::AppWidgetConnected(struct event_arg *arg, void* data)
102 {
103     int ret;
104     ret = provider_send_hello();
105     if (ret == 0)
106     {
107         SysLog(NID_SHELL, "success to be connected with master daemon");
108         AppWidgetManagerService::GetInstance()->StartPingTimer();
109
110     }
111     else
112     {
113         SysLog(NID_SHELL, "failed to provider_send_hello()");
114     }
115     return ret;
116 }
117
118 int
119 AppWidgetManagerService::AppWidgetDisconnected(struct event_arg *arg, void* data)
120 {
121         SysLog(NID_SHELL, "success to be disconnected with master daemon");
122 //      aul_terminate_pid(getpid());
123     return 0;
124 }
125
126 void
127 AppWidgetManagerService::StartPingTimer()
128 {
129         __pingTimer.Construct(*this);//, true);
130         __pingTimer.StartAsRepeatable(120000);
131 }
132
133 void
134 AppWidgetManagerService::OnTimerExpired(Timer& timer)
135 {
136         SysLog(NID_SHELL, "provider_send_ping");
137         provider_send_ping();
138 }
139
140 _AppWidgetContext*
141 AppWidgetManagerService::Find(const String& appId, const String& instanceId) const
142 {
143         for( int i = 0; i < __appWidgetContextList.GetCount(); i++ )
144         {
145                 _AppWidgetContext* pAppWidgetContext = null;
146                 __appWidgetContextList.GetAt(i, pAppWidgetContext);
147 //              SysLog(NID_SHELL, "%ls", pAppWidgetContext->__instanceId.GetPointer());
148
149                 if ( pAppWidgetContext->__instanceId == instanceId )
150                 {
151 //                      SysAssert(pAppWidgetContext->__appId == appId)
152                         return pAppWidgetContext;
153                 }
154         }
155         return null;
156 }
157
158 result
159 AppWidgetManagerService::SetIpcClientIds(const Tizen::App::AppId& appId, int clientId)
160 {
161         for( int i = 0; i < __appWidgetContextList.GetCount(); i++ )
162         {
163                 _AppWidgetContext* pAppWidgetContext = null;
164                 __appWidgetContextList.GetAt(i, pAppWidgetContext);
165                 SysLog(NID_SHELL, "%ls", pAppWidgetContext->__instanceId.GetPointer());
166                 SysLog(NID_SHELL, "%ls, %ls", pAppWidgetContext->__providerId.GetPointer(), appId.GetPointer());
167
168                 if ( pAppWidgetContext->__appId == appId )
169                 {
170                         SysLog(NID_SHELL,"");
171                         pAppWidgetContext->SetIpcClientId(clientId);
172                         SysLog(NID_SHELL, "client is registered.(%d)", clientId);
173
174                         if( clientId == -1)//disconnected
175                         {
176 //                              pAppWidgetContext->ReleaseSharedMem();
177                                 pAppWidgetContext->Suspend();
178                                 if(pAppWidgetContext->GetAppWidgetPopup())
179                                 {
180                                         pAppWidgetContext->OnPopupDestoyed();
181                                 }
182                         }
183                 }
184         }
185         return E_SUCCESS;
186 }
187
188 void
189 AppWidgetManagerService::OnIpcClientConnected(const _IpcServer& server, int clientId)
190 {
191         String appId = server.GetClientApplicationId();
192         SysLog(NID_SHELL, "(%ls)\n", appId.GetPointer());
193
194         this->SetIpcClientIds(appId, clientId);
195 }
196
197 void
198 AppWidgetManagerService::OnIpcClientDisconnected(const _IpcServer& server, int clientId)
199 {
200         String appId = server.GetClientApplicationId();
201         SysLog(NID_SHELL, "(%ls)\n", appId.GetPointer());
202
203         this->SetIpcClientIds(appId, -1);
204 }
205
206 result
207 AppWidgetManagerService::AddAppWidget(_AppWidgetContext* pAppWidgetContext)
208 {
209         SysLog(NID_SHELL, "%ls, %ls, count(%d)", pAppWidgetContext->__providerId.GetPointer(), pAppWidgetContext->__instanceId.GetPointer(), __appWidgetContextList.GetCount());
210
211         return __appWidgetContextList.Add(pAppWidgetContext);
212 }
213
214 result
215 AppWidgetManagerService::RemoveAppWidget(const char* pPackageName, const char* pId, bool free)
216 {
217         SysLog(NID_SHELL, "%s, %s, count(%d)", pPackageName, pId, __appWidgetContextList.GetCount());
218
219         SysTryReturn(NID_SHELL, ( pPackageName != null && pId != null), null, E_INVALID_ARG, "[E_INVALID_ARG]");
220         SysLog(NID_SHELL, "%s, %s", pPackageName, pId);
221
222         _AppWidgetContext* pAppWidgetContext = Find(pPackageName, pId);
223         SysTryReturn(NID_SHELL, pAppWidgetContext, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND]");
224
225         result r = __appWidgetContextList.Remove(pAppWidgetContext);
226
227         if( __appWidgetContextList.GetCount() == 0 )
228         {
229                 SysLog(NID_SHELL, "No running native appWidget app remains, terminating osp appWidget service...");
230                 Tizen::App::App::GetInstance()->Terminate();
231         }
232
233         return r;
234 }
235
236
237 ///////////////////////////////////////////////////////
238 // MasterDaemonEventReceiver implementation
239 ///////////////////////////////////////////////////////
240 int
241 AppWidgetManagerService::OnAppWidgetCreate(struct event_arg *arg, int *width, int *height, double *priority, void* data)
242 {
243         SysTryReturn(NID_SHELL, arg != null, EINVAL, E_INVALID_ARG, "[E_INVALID_ARG]");
244         SysTryReturn(NID_SHELL, arg->pkgname != null, EINVAL, E_INVALID_ARG, "[E_INVALID_ARG]");
245         SysTryReturn(NID_SHELL, arg->id != null, EINVAL, E_INVALID_ARG, "[E_INVALID_ARG]");
246         SysTryReturn(NID_SHELL, arg->type == event_arg::EVENT_NEW, -EPERM, E_SUCCESS, "invalid argument from master");
247
248         SysLog(NID_SHELL, "packageName(%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);
249         SysTryReturn (NID_SHELL, AppWidgetManagerService::GetInstance()->Find(arg->pkgname, arg->id) == null, -EBUSY, E_SUCCESS,"already exist");
250
251         double default_priority = 1.0f;
252         _AppWidgetContext* pAppWidgetContext = new (std::nothrow)_AppWidgetContext( arg->info.lb_create.content, arg->pkgname, arg->id,
253                         arg->info.lb_create.width, arg->info.lb_create.height, arg->info.lb_create.period * 1000, default_priority);
254         SysTryReturn(NID_SHELL, pAppWidgetContext, 0, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY]");
255
256         AppWidgetManagerService* pAppWidgetService = AppWidgetManagerService::GetInstance();
257         pAppWidgetService->AddAppWidget(pAppWidgetContext);
258         pAppWidgetContext->OnAdded();
259
260         *priority = pAppWidgetContext->__priority;
261         *height= pAppWidgetContext->__height;
262         *width = pAppWidgetContext->__width;
263
264         SysLog(NID_SHELL, "Exit. %d appWidget(es)", pAppWidgetService->__appWidgetContextList.GetCount());
265     return 0;
266 }
267
268 int
269 AppWidgetManagerService::OnAppWidgetUpdate(struct event_arg *arg, void* data)
270 {
271         if( arg->id == null || strlen(arg->id) < 1)
272         {
273                 SysLog(NID_SHELL, "updating alls");
274                 AppWidgetManagerService::GetInstance()->UpdateAllAppWidgetsByAppId(arg->pkgname);
275         }
276         else
277         {
278                 _AppWidgetContext* pAppWidgetContext = AppWidgetManagerService::GetInstance()->Find(arg->pkgname, arg->id);
279                 SysTryReturnResult(NID_SHELL, pAppWidgetContext , E_SYSTEM, "[E_SYSTEM] pAppWidgetContext is null for appId(%s), instanceId(%s)", arg->pkgname, arg->id);
280
281                 pAppWidgetContext->OnUpdate(L"");
282         }
283
284         return 0;
285 }
286
287 int
288 AppWidgetManagerService::UpdateAllAppWidgetsByAppId(const String& providerId)
289 {
290         for( int i = 0; i < __appWidgetContextList.GetCount(); i++ )
291         {
292                 _AppWidgetContext* pAppWidgetContext = null;
293                 __appWidgetContextList.GetAt(i, pAppWidgetContext);
294
295                 if ( pAppWidgetContext->__providerId == providerId )
296                 {
297                         pAppWidgetContext->OnUpdate(L"");
298                 }
299         }
300         return E_SUCCESS;
301 }
302
303 int
304 AppWidgetManagerService::OnAppWidgetDestroy(struct event_arg *arg, void* data)
305 {
306         SysTryReturn (NID_SHELL, arg->type == event_arg::EVENT_DELETE, -EPERM, E_SUCCESS, "invalid argument from master");
307         SysLog(NID_SHELL, "Enter");
308
309         AppWidgetManagerService* pAppWidgetManagerService = AppWidgetManagerService::GetInstance();
310
311         _AppWidgetContext* pAppWidgetContext = pAppWidgetManagerService->Find(arg->pkgname, arg->id);
312         SysTryReturnResult(NID_SHELL, pAppWidgetContext , E_SYSTEM, "[E_SYSTEM] pAppWidgetContext is null for appId(%s), instanceId(%s)", arg->pkgname, arg->id);
313
314     pAppWidgetContext->OnRemoved();
315     pAppWidgetManagerService->RemoveAppWidget( arg->pkgname, arg->id, true);
316     delete pAppWidgetContext;
317
318     SysLog(NID_SHELL, "Exit");
319     return 0;
320 }
321
322 int
323 AppWidgetManagerService::OnAppWidgetPopupCreate(struct event_arg *arg, void* data)
324 {
325         SysTryReturn (NID_SHELL, arg->type == event_arg::EVENT_PD_CREATE, -EPERM, E_SUCCESS, "invalid argument from master");
326         SysLog(NID_SHELL, "packageName(%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);
327
328         _AppWidgetContext* pAppWidgetContext = AppWidgetManagerService::GetInstance()->Find(arg->pkgname, arg->id);
329         SysTryReturnResult(NID_SHELL, pAppWidgetContext , E_SYSTEM, "[E_SYSTEM] pAppWidgetContext is null for appId(%s), instanceId(%s)", arg->pkgname, arg->id);
330
331         /*if( pAppWidgetContext->GetAppWidgetPopup() )
332         {
333                 pAppWidgetContext->OnPopupDestoyed();
334         }*/
335         pAppWidgetContext->OnPopupCreated(arg->info.pd_create.x, arg->info.pd_create.y, arg->info.pd_create.w, arg->info.pd_create.h);
336
337     return 0;
338 }
339
340  int
341  AppWidgetManagerService::OnAppWidgetPopupDestroy(struct event_arg *arg, void* data)
342 {
343         SysTryReturn (NID_SHELL, arg->type == event_arg::EVENT_PD_DESTROY, -EPERM, E_SUCCESS, "invalid argument from master");
344         SysLog(NID_SHELL, "packageName(%s), id(%s)", arg->pkgname, arg->id);
345         _AppWidgetContext* pAppWidgetContext = AppWidgetManagerService::GetInstance()->Find(arg->pkgname, arg->id);
346         SysTryReturnResult(NID_SHELL, pAppWidgetContext , E_SYSTEM, "[E_SYSTEM] pAppWidgetContext is null for appId(%s), instanceId(%s)", arg->pkgname, arg->id);
347
348         pAppWidgetContext->OnPopupDestoyed();
349
350     return 0;
351 }
352
353 int
354 AppWidgetManagerService::OnAppWidgetPause(struct event_arg *arg, void* data)
355 {
356          SysTryReturn(NID_SHELL, arg, 0, E_SUCCESS, "arg is null!");
357
358          _AppWidgetContext* pAppWidgetContext = AppWidgetManagerService::GetInstance()->Find(arg->pkgname, arg->id);
359          SysTryReturnResult(NID_SHELL, pAppWidgetContext , 0, "[E_SYSTEM] pAppWidgetContext is null for appId(%s), instanceId(%s)", arg->pkgname, arg->id);
360
361          SysLog(NID_SHELL, "packageName(%s), id(%s)", arg->pkgname, arg->id);
362          pAppWidgetContext->OnBackground();
363     return 0;
364 }
365
366 int
367 AppWidgetManagerService::OnAppWidgetResume(struct event_arg *arg, void* data)
368 {
369         SysTryReturn(NID_SHELL, arg || arg->id || arg->pkgname, 0, E_SUCCESS, "arg is null!");
370
371         _AppWidgetContext* pAppWidgetContext = AppWidgetManagerService::GetInstance()->Find(arg->pkgname, arg->id);
372         SysTryReturnResult(NID_SHELL, pAppWidgetContext , E_SYSTEM, "[E_SYSTEM] pAppWidgetContext is null for appId(%s), instanceId(%s)", arg->pkgname, arg->id);
373
374         SysLog(NID_SHELL, "packageName(%s), id(%s)", arg->pkgname, arg->id);
375         pAppWidgetContext->OnForeground();
376     return 0;
377 }
378
379 int
380 AppWidgetManagerService::OnAppWidgetPauseAll(struct event_arg *arg, void* data)
381 {
382         SysLog(NID_SHELL,"");
383     return 0;
384 }
385
386 int
387 AppWidgetManagerService::OnAppWidgetResumeAll(struct event_arg *arg, void* data)
388 {
389         SysLog(NID_SHELL,"");
390     return 0;
391 }
392
393 int
394 AppWidgetManagerService::OnAppWidgetClick(struct event_arg *arg, void* data)
395 {
396         SysTryReturn (NID_SHELL, arg->type == event_arg::EVENT_CLICKED, -EPERM, E_SUCCESS, "invalid argument from master");
397         SysTryReturn(NID_SHELL, arg != null, -EPERM, E_SUCCESS, "arg is null!");
398         SysLog(NID_SHELL, "packageName(%s), id(%s), clicked.event(%s), clicked.x(%d), clicked.y(%d)", arg->pkgname, arg->id, arg->info.clicked.event, arg->info.clicked.x, arg->info.clicked.y);
399
400     return 0;
401 }
402
403 int
404 AppWidgetManagerService::OnAppWidgetResize(struct event_arg *arg, void* data)
405 {
406         SysTryReturn(NID_SHELL, arg || arg->id || arg->pkgname, 0, E_SUCCESS, "arg is null!");
407         SysTryReturn (NID_SHELL, arg->type == event_arg::EVENT_RESIZE, -EPERM, E_SUCCESS, "invalid argument from master");
408
409         SysLog(NID_SHELL, "packageName(%s), id(%s), resize.w(%d), resize.h(%d)", arg->pkgname, arg->id, arg->info.resize.w, arg->info.resize.h);
410
411         _AppWidgetContext* pAppWidgetContext = AppWidgetManagerService::GetInstance()->Find(arg->pkgname, arg->id);
412         SysTryReturnResult(NID_SHELL, pAppWidgetContext , E_SYSTEM, "[E_SYSTEM] pAppWidgetContext is null for appId(%s), instanceId(%s)", arg->pkgname, arg->id);
413
414         pAppWidgetContext->OnResize(arg->info.resize.w, arg->info.resize.h);
415
416     return 0;
417 }
418
419 int
420 AppWidgetManagerService::OnAppWidgetPeriodChaned(struct event_arg *arg, void* data)
421 {
422         SysTryReturn(NID_SHELL, arg != null, 0, E_SUCCESS, "arg is null!");
423         SysLog(NID_SHELL, "packageName(%s), id(%s), width(%d), height(%d), priority(%d)", arg->pkgname, arg->id);
424     return 0;
425 }
426
427 int
428 AppWidgetManagerService::OnAppWidgetRecreate(struct event_arg *arg, void* data)
429 {
430         SysLog(NID_SHELL, "");
431         return 0;
432 }
433
434 result
435 AppWidgetManagerService::InitializeMasterDaemonEventReceiver(const char *pServiceExecutableName)
436 {
437         SysTryReturnResult(NID_SHELL, pServiceExecutableName != null, E_INVALID_ARG, "");
438         SysLog(NID_SHELL, "Enter.");
439
440         __appWidgetContextList.Construct();
441
442         /*!
443          * \note
444          * Only for the buffer type
445          */
446     struct event_handler cbs;
447     memset(&cbs, 0, sizeof(event_handler));
448
449     cbs.connected = AppWidgetConnected,
450     cbs.disconnected = AppWidgetDisconnected,
451     cbs.pause = OnAppWidgetPauseAll,
452     cbs.resume = OnAppWidgetResumeAll,
453     cbs.lb_pause = OnAppWidgetPause,
454     cbs.lb_resume = OnAppWidgetResume,
455     cbs.lb_create = OnAppWidgetCreate,
456     cbs.lb_destroy = OnAppWidgetDestroy,
457     cbs.update_content = OnAppWidgetUpdate,
458     cbs.pd_create = OnAppWidgetPopupCreate,
459     cbs.pd_destroy = OnAppWidgetPopupDestroy,
460     cbs.clicked = OnAppWidgetClick,
461     cbs.resize = OnAppWidgetResize,
462     cbs.set_period = OnAppWidgetPeriodChaned;
463     cbs.lb_recreate = OnAppWidgetRecreate;/* Recover from the fault of slave */
464     //cbs.content_event = OnAppWidgetContentEvent,
465
466     int ret = provider_init(null, pServiceExecutableName, &cbs, this);
467     SysTryReturnResult(NID_SHELL, ret == 0, E_SYSTEM, "provider_init failed.");
468
469     SysLog(NID_SHELL, "Exit.");
470     return E_SUCCESS;
471 }
472
473 result
474 AppWidgetManagerService::DeinitializeMasterDaemonEventReceiver()
475 {
476         SysLog(NID_SHELL, "Enter.");
477     provider_fini();
478     SysLog(NID_SHELL, "Exit.");
479     return E_SUCCESS;
480 }
481
482 ///////////////////////////////////////////////////////
483 // stub implmentation
484 ///////////////////////////////////////////////////////
485
486 result
487 AppWidgetManagerService::RequestUpdate(const Tizen::App::AppId& appId, const Tizen::Base::String& providerName, const Tizen::Base::String& argument)
488 {
489         SysLog(NID_SHELL, "%ls, %ls", appId.GetPointer(), providerName.GetPointer() );
490
491         bool found = false;
492         for( int i = 0; i < __appWidgetContextList.GetCount(); i++ )
493         {
494                 _AppWidgetContext* pAppWidgetContext = null;
495                 __appWidgetContextList.GetAt(i, pAppWidgetContext);
496
497                 if ( pAppWidgetContext->__appId == appId && pAppWidgetContext->__providerName == providerName)
498                 {
499                         SysLog(NID_SHELL, "OK");
500
501                         ArrayList* pArray = new (std::nothrow) ArrayList();
502                         SysTryReturnResult(NID_SHELL, pArray, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY]");
503
504                         pArray->Construct();
505                         pArray->Add(pAppWidgetContext);
506                         pArray->Add(new String(argument));
507
508                         result r = __handlerThread.SendUserEvent(LOCAL_EVENT_REQUEST_UPDATE, pArray);
509                         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Propagated.", GetErrorMessage(r));
510
511                         found = true;
512                 }
513         }
514         SysLog(NID_SHELL, "Exit.");
515         return (found) ? E_SUCCESS : E_OBJ_NOT_FOUND;
516
517 }
518
519 result
520 AppWidgetManagerService::RequestUpdateInstance(const Tizen::Base::String& instanceId, const Tizen::Base::String& argument)
521 {
522         SysLog(NID_SHELL, "%ls, %ls", instanceId.GetPointer(), argument.GetPointer() );
523
524         for( int i = 0; i < __appWidgetContextList.GetCount(); i++ )
525         {
526                 _AppWidgetContext* pAppWidgetContext = null;
527                 __appWidgetContextList.GetAt(i, pAppWidgetContext);
528
529                 if ( pAppWidgetContext->__instanceId == instanceId)
530                 {
531                         SysLog(NID_SHELL, "OK");
532
533                         ArrayList* pArray = new (std::nothrow) ArrayList();
534                         SysTryReturnResult(NID_SHELL, pArray, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY]");
535
536                         pArray->Construct();
537                         pArray->Add(pAppWidgetContext);
538                         pArray->Add(new String(argument));
539
540                         result r = __handlerThread.SendUserEvent(LOCAL_EVENT_REQUEST_UPDATE, pArray);
541                         SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Propagated.", GetErrorMessage(r));
542
543                         return E_SUCCESS;
544                 }
545         }
546         SysLog(NID_SHELL, "Exit.");
547         return E_OBJ_NOT_FOUND;
548 }
549
550 result
551 AppWidgetManagerService::RequestSharedMemoryId(const Tizen::App::AppId& appId, const Tizen::Base::String& instanceId, int width, int height, int& shmId)
552 {
553         _AppWidgetContext* pAppWidgetContext = this->Find(appId, instanceId);
554         SysTryReturnResult(NID_SHELL, pAppWidgetContext , E_SYSTEM, "[E_SYSTEM] pAppWidgetContext is null for (%ls) (%ls)", appId.GetPointer(), instanceId.GetPointer());
555
556 //      if(pAppWidgetContext->HasValidClientId() == false)
557 //      {
558 //              String fullAppId = __pIpcServer->GetClientApplicationId();
559 //              this->SetIpcClientIds(fullAppId, __pIpcServer->GetClientId());
560 //      }
561
562         shmId = pAppWidgetContext->GetSharedMemId(width, height);
563         SysTryReturnResult(NID_SHELL, pAppWidgetContext , shmId != -1, "[E_SYSTEM] failed to GetSharedMemId for (%ls) (%ls)", appId.GetPointer(), instanceId.GetPointer());
564
565     return E_SUCCESS;
566 }
567
568 result
569 AppWidgetManagerService::RequestSharedMemoryIdForPD(const Tizen::App::AppId& appId, const Tizen::Base::String& instanceId, int width, int height, int& shmId)
570 {
571         _AppWidgetContext* pAppWidgetContext = this->Find(appId, instanceId);
572         SysTryReturnResult(NID_SHELL, pAppWidgetContext , E_SYSTEM, "[E_SYSTEM] pAppWidgetContext is null for (%ls) (%ls)", appId.GetPointer(), instanceId.GetPointer());
573
574         _AppWidgetPopupContext* pPd = pAppWidgetContext->GetAppWidgetPopup();
575         SysTryReturnResult(NID_SHELL, pPd , E_SYSTEM, "[E_SYSTEM] pPd is null for (%ls) (%ls)", appId.GetPointer(), instanceId.GetPointer());
576
577         if(pPd->HasValidClientId() == false)
578         {
579                 String fullAppId = __pIpcServer->GetClientApplicationId();
580                 pPd->SetIpcClientId(__pIpcServer->GetClientId());
581         }
582
583         shmId = pPd->GetSharedMemId(width, height);
584         SysTryReturnResult(NID_SHELL, pAppWidgetContext , shmId != -1, "[E_SYSTEM] failed to GetSharedMemId for (%ls) (%ls)", appId.GetPointer(), instanceId.GetPointer());
585
586     return E_SUCCESS;
587 }
588
589 result
590 AppWidgetManagerService::RequestSyncSharedMemory(const Tizen::App::AppId& appId, const Tizen::Base::String& instanceId, int width, int height)
591 {
592         _AppWidgetContext* pAppWidgetContext = this->Find(appId, instanceId);
593         SysTryReturnResult(NID_SHELL, pAppWidgetContext , E_SYSTEM, "[E_SYSTEM] pAppWidgetContext is null for (%ls) (%ls)", appId.GetPointer(), instanceId.GetPointer());
594
595         return pAppWidgetContext->RequestUpdateRemote(width, height);
596 }
597
598 result
599 AppWidgetManagerService::RequestSyncSharedMemoryForPD(const Tizen::App::AppId& appId, const Tizen::Base::String& instanceId)
600 {
601         _AppWidgetContext* pAppWidgetContext = this->Find(appId, instanceId);
602         SysTryReturnResult(NID_SHELL, pAppWidgetContext , E_SYSTEM, "[E_SYSTEM] pAppWidgetContext is null for (%ls) (%ls)", appId.GetPointer(), instanceId.GetPointer());
603         SysTryReturnResult(NID_SHELL, pAppWidgetContext->GetAppWidgetPopup(), E_SYSTEM, "[E_SYSTEM] GetAppWidgetPopup() returns null for (%ls) (%ls)", appId.GetPointer(), instanceId.GetPointer());
604
605         return pAppWidgetContext->GetAppWidgetPopup()->RequestUpdateRemote();
606 }
607
608 result
609 AppWidgetManagerService::RequestReleaseSharedMemory(const Tizen::App::AppId& appId, const Tizen::Base::String& instanceId)
610 {
611 //      std::unique_ptr<char[]> pkgname(_StringConverter::CopyToCharArrayN(appId));
612 //      std::unique_ptr<char[]> id(_StringConverter::CopyToCharArrayN(instanceId));
613 //
614 //      _AppWidgetContext* pAppWidgetContext = this->Find(appId, instanceId);
615 //      SysTryReturnResult(NID_SHELL, pAppWidgetContext , E_SYSTEM, "[E_SYSTEM] pAppWidgetContext is null for (%ls) (%ls)", appId.GetPointer(), instanceId.GetPointer());
616 //
617 //      return pAppWidgetContext->ReleaseSharedMem();
618         return E_SUCCESS;
619 }
620
621 result
622 AppWidgetManagerService::RequestReleaseSharedMemoryForPD(const Tizen::App::AppId& appId, const Tizen::Base::String& instanceId)
623 {
624 //      std::unique_ptr<char[]> pkgname(_StringConverter::CopyToCharArrayN(appId));
625 //      std::unique_ptr<char[]> id(_StringConverter::CopyToCharArrayN(instanceId));
626 //
627 //      _AppWidgetContext* pAppWidgetContext = this->Find(appId, instanceId);
628 //      SysTryReturnResult(NID_SHELL, pAppWidgetContext , E_SYSTEM, "[E_SYSTEM] pAppWidgetContext is null for (%ls) (%ls)", appId.GetPointer(), instanceId.GetPointer());
629 //      SysTryReturnResult(NID_SHELL, pAppWidgetContext->GetAppWidgetPopup(), E_SYSTEM, "[E_SYSTEM] GetAppWidgetPopup() returns null for (%ls) (%ls)", appId.GetPointer(), instanceId.GetPointer());
630 //
631 //      return pAppWidgetContext->GetAppWidgetPopup()->ReleaseSharedMem();
632         return E_SUCCESS;
633 }
634
635 AppWidgetManagerService::_TaskHandlerThread::~_TaskHandlerThread(void)
636 {
637
638 }
639
640 void
641 AppWidgetManagerService::_TaskHandlerThread::OnUserEventReceivedN(RequestId reqId, IList* pArgs)
642 {
643         SysTryReturnVoidResult(NID_SHELL, pArgs != null, E_INVALID_STATE, "pArgs is null!.");
644
645         SysLog(NID_SHELL, "Enter.");
646
647         switch (reqId)
648         {
649         case LOCAL_EVENT_REQUEST_UPDATE:
650         {
651                 _AppWidgetContext* pAppWidgetContext = dynamic_cast<_AppWidgetContext*>( pArgs->GetAt(0) );
652                 SysTryReturnVoidResult(NID_SHELL, pAppWidgetContext != null, E_INVALID_STATE, "_AppWidget is null!.");
653
654                 String* pArgument = dynamic_cast<String*>( pArgs->GetAt(1) );
655                 SysTryReturnVoidResult(NID_SHELL, pArgument != null, E_INVALID_STATE, "pArgument is null!.");
656
657                 pAppWidgetContext->OnUpdate(*pArgument);
658
659                 delete pArgument;
660                 pArgs->RemoveAll();
661                 delete pArgs;
662         }
663                 break;
664
665         default:
666                 SysAssertf(false, "never get here!");
667                 break;
668         }
669         SysLog(NID_SHELL, "Exit.");
670 }
671
672 } } } //namespace Tizen { namespace Shell  { namespace App {