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