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