AppControl launch logic refactoring
[platform/framework/native/appfw.git] / src / app / FApp_AppArg.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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     FApp_AppArg.cpp
19 * @brief        This is the implementation for the _AppArg.cpp class.
20 */
21
22 #include <cstdio>
23 #include <cstdlib>
24 #include <new>
25 #include <typeinfo>
26 #include <unique_ptr.h>
27
28 #include <aul.h>
29 #include <app.h>
30 #include <bundle.h>
31 #include <appsvc/appsvc.h>
32 #include <Ecore_X.h>
33
34 #include <FBaseInteger.h>
35 #include <FBaseColArrayList.h>
36 #include <FBaseColHashMap.h>
37 #include <FBaseByteBuffer.h>
38 #include <FBaseUtilStringTokenizer.h>
39 #include <FBaseSysLog.h>
40
41 #include <FBase_StringConverter.h>
42
43 #include "FApp_MapDataControlImpl.h"
44 #include "FApp_AppControlImpl.h"
45 #include "FApp_SqlDataControlImpl.h"
46 #include "FApp_AppControlEventArg.h"
47 #include "FApp_AppArg.h"
48 #include "FApp_AppMessageImpl.h"
49 #include "FApp_Aul.h"
50
51 using namespace Tizen::Base;
52 using namespace Tizen::Base::Collection;
53 using namespace Tizen::Base::Utility;
54
55
56 extern "C" int appsvc_allow_transient_app(bundle*, Ecore_X_Window);
57
58 namespace Tizen { namespace App
59 {
60
61 static const char OSP_V_LAUNCH_TYPE_LAUNCH[] = "launch";
62 static const char OSP_V_LAUNCH_TYPE_APPCONTROL[] = "appcontrol";
63 static const char OSP_V_LAUNCH_TYPE_DATACONTROL[] = "datacontrol";
64 static const char OSP_V_LAUNCH_TYPE_CONDTION[] = "condition";
65 static const char OSP_V_REQUEST_TYPE_SQL_QUERY[] = "sql_query";
66 static const char OSP_V_REQUEST_TYPE_SQL_INSERT[] = "sql_insert";
67 static const char OSP_V_REQUEST_TYPE_SQL_UPDATE[] = "sql_update";
68 static const char OSP_V_REQUEST_TYPE_SQL_DELETE[] = "sql_delete";
69 static const char OSP_V_REQUEST_TYPE_MAP_QEURY[] = "map_query";
70 static const char OSP_V_REQUEST_TYPE_MAP_INSERT[] = "map_insert";
71 static const char OSP_V_REQUEST_TYPE_MAP_UPDATE[] = "map_update";
72 static const char OSP_V_REQUEST_TYPE_MAP_DELETE[] = "map_delete";
73 static const char OSP_V_VERSION_2_1_0_3[] = "ver_2.1.0.3";
74
75 static const char BUNDLE_KEY_PREFIX_AUL[] = "__AUL_";
76 static const char BUNDLE_KEY_PREFIX_SERVICE[] = "__APP_SVC_";
77 static const char BUNDLE_KEY_PREFIX_OSP[] = "__OSP_";
78 static const char BUNDLE_KEY_PREFIX_UG[] = "__UG_";
79
80 static const char OSP_K_SUBMODE_CALLEE[] = "__OSP_SUB_CALLEE__";
81 static const char OSP_K_SERVICE_CALLEE[] = "__OSP_SERVICE_CALLEE__";
82
83 const char TIZEN_NOTIFICATION_DATA[] = "http://tizen.org/appcontrol/data/notification";
84 const wchar_t LEGACY_OPERATION_MAIN[] = L"osp.operation.MAIN";
85
86
87 _AppArg::_AppArg(void)
88         : __pBundle(null)
89 {
90 }
91
92
93 _AppArg::~_AppArg(void)
94 {
95         if (__pBundle)
96         {
97                 bundle_free(__pBundle);
98         }
99 }
100
101
102 result
103 _AppArg::Construct(const String& argText)
104 {
105         __pBundle = bundle_create();
106         SysTryReturnResult(NID_APP, __pBundle != null, E_OUT_OF_MEMORY, "Bundle creation failure.");
107
108         return CreateNotificationArg(__pBundle, argText);
109 }
110
111
112 result
113 _AppArg::Construct(const IList* pList)
114 {
115         __pBundle = bundle_create();
116         SysTryReturnResult(NID_APP, __pBundle != null, E_OUT_OF_MEMORY, "Bundle creation failure.");
117
118         return CreateLaunchArg(__pBundle, pList);
119 }
120
121
122 result
123 _AppArg::Construct(const _AppControlImpl& ac, const IList* pList)
124 {
125         __pBundle = bundle_create();
126         SysTryReturnResult(NID_APP, __pBundle != null, E_OUT_OF_MEMORY, "Bundle creation failure.");
127
128         return CreateAppControlArg(__pBundle, ac, pList);
129 }
130
131
132 result
133 _AppArg::Construct(const _AppControlImpl& ac, const String* pUri, const String* pMime, const IMap* pList)
134 {
135         __pBundle = bundle_create();
136         SysTryReturnResult(NID_APP, __pBundle != null, E_OUT_OF_MEMORY, "Bundle creation failure.");
137
138         return CreateAppControlArg(__pBundle, ac, pUri, pMime, pList);
139 }
140
141
142 result
143 _AppArg::Construct(const _SqlDataControlImpl& dc, _DataControlRequestType requestType, const IList* pList)
144 {
145         __pBundle = bundle_create();
146         SysTryReturnResult(NID_APP, __pBundle != null, E_OUT_OF_MEMORY, "Bundle creation failure.");
147
148         return CreateSqlDataControlArg(__pBundle, dc, requestType, pList);
149 }
150
151
152 result
153 _AppArg::Construct(const _MapDataControlImpl& dc, _DataControlRequestType requestType, const IList* pList)
154 {
155         __pBundle = bundle_create();
156         SysTryReturnResult(NID_APP, __pBundle != null, E_OUT_OF_MEMORY, "Bundle creation failure.");
157
158         return CreateMapDataControlArg(__pBundle, dc, requestType, pList);
159 }
160
161
162 result
163 _AppArg::ConstructResult(const _AppArg& arg, const IList* pList)
164 {
165         int ret = aul_create_result_bundle(arg.GetBundle(), &__pBundle);
166         SysTryReturnResult(NID_APP, __pBundle != null, E_INVALID_STATE, "Bundle creatioin from service handle failure : %d.", ret);
167
168         return CreateResultArg(__pBundle, pList);
169 }
170
171
172 result
173 _AppArg::ConstructResult(const _AppArg& arg, const IMap* pMap)
174 {
175         int ret = aul_create_result_bundle(arg.GetBundle(), &__pBundle);
176         SysTryReturnResult(NID_APP, __pBundle != null, E_INVALID_STATE, "Bundle creatioin from service handle failure : %d.", ret);
177
178         return CreateResultArg(__pBundle, pMap);
179 }
180
181
182 result
183 _AppArg::Construct(const bundle* b)
184 {
185         __pBundle = bundle_dup(const_cast<bundle*>(b));
186         SysTryReturnResult(NID_APP, __pBundle != null, E_INVALID_STATE, "Bundle creatioin from service handle failure.");
187
188         return E_SUCCESS;
189 }
190
191
192 result
193 _AppArg::ConstructForAppLaunchCondition(const String& condition, const IList* pList)
194 {
195         __pBundle = bundle_create();
196         SysTryReturnResult(NID_APP, __pBundle != null, E_OUT_OF_MEMORY, "Bundle creation failure.");
197
198         return CreateAppLaunchConditionArg(__pBundle, condition, pList);
199 }
200
201 result
202 _AppArg::ConstructForAppLaunchCondition(const String& condition, const IList* pList, const IMap* pMap)
203 {
204         __pBundle = bundle_create();
205         SysTryReturnResult(NID_APP, __pBundle != null, E_OUT_OF_MEMORY, "Bundle creation failure.");
206
207         CreateAppLaunchConditionArg(__pBundle, condition, pList);
208
209         return CreateResultArg(__pBundle, pMap);
210 }
211
212 ArrayList*
213 _AppArg::GetArgListN(int num) const
214 {
215         bundle* pBundle = __pBundle;
216         SysTryReturn(NID_APP, pBundle != null, null, E_INVALID_STATE, "[E_INVALID_STATE] Improper bundle state.");
217
218         ArrayList* pList = new (std::nothrow) ArrayList();
219         SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
220
221         pList->Construct();
222
223         const char* p = NULL;
224         _AppHandler launch_type = GetHandler(pBundle);
225
226         switch (launch_type)
227         {
228         case _APP_HANDLER_APPCONTROL:
229                 // request Id
230                 // [FIXME] proper request Id required
231                 pList->Add(*new (std::nothrow) String(GetRequestId(num)));
232
233                 // category
234                 p = appsvc_get_category(pBundle);
235                 if (p)
236                 {
237                         pList->Add(*new (std::nothrow) String(p));
238                 }
239                 else
240                 {
241                         pList->Add(*new (std::nothrow) String(L""));
242                 }
243
244                 // MIME type
245                 p = appsvc_get_mime(pBundle);
246                 if (p)
247                 {
248                         pList->Add(*new (std::nothrow) String(p));
249                 }
250                 else
251                 {
252                         pList->Add(*new (std::nothrow) String(L""));
253                 }
254
255                 // URI scheme
256                 p = appsvc_get_uri(pBundle);
257                 if (p)
258                 {
259                         pList->Add(*new (std::nothrow) String(p));
260                 }
261                 else
262                 {
263                         pList->Add(*new (std::nothrow) String(L""));
264                 }
265
266                 break;
267
268         case _APP_HANDLER_DATACONTROL:
269                 // appId
270                 AddListFromBundle(pList, pBundle, OSP_K_APPID);
271                 // request type
272                 AddListFromBundle(pList, pBundle, OSP_K_DATACONTROL_REQUEST_TYPE);
273                 // reqId
274                 AddListFromBundle(pList, pBundle, OSP_K_REQUEST_ID);
275                 // providerId
276                 AddListFromBundle(pList, pBundle, OSP_K_DATACONTROL_PROVIDER);
277                 break;
278
279         case _APP_HANDLER_LAUNCH_COND:
280                 pList->Add(*new (std::nothrow) String(LEGACY_LAUNCH_REASON_CONDITIONAL));
281                 AddListFromBundle(pList, pBundle, OSP_K_COND);
282                 break;
283
284         case _APP_HANDLER_LAUNCH_NORMAL:
285                 pList->Add(*new (std::nothrow) String(LEGACY_LAUNCH_REASON_NORMAL));
286                 pList->Add(*new (std::nothrow) String(LEGACY_OPERATION_MAIN));
287                 break;
288
289         default:
290                 SysLog(NID_APP, "Invalid handler type");
291                 break;
292         }
293
294         SetArgList(__pBundle, pList);
295
296         return pList;
297 }
298
299
300 ArrayList*
301 _AppArg::GetArgListN(void) const
302 {
303         SysTryReturn(NID_APP, __pBundle != null, null, E_INVALID_STATE, "[E_INVALID_STATE] Improper bundle state.");
304
305         ArrayList* pList = new (std::nothrow) ArrayList();
306         SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
307
308         pList->Construct();
309
310         SetArgList(__pBundle, pList);
311
312         return pList;
313 }
314
315 // the returned map is allocated using SingleObjectDeleter
316 HashMap*
317 _AppArg::GetArgMapN(void) const
318 {
319         SysTryReturn(NID_APP, __pBundle != null, null, E_INVALID_STATE, "[E_INVALID_STATE] Improper bundle state.");
320
321         //Print();
322
323         HashMap* pHashMap = new (std::nothrow) HashMap(SingleObjectDeleter);
324         SysTryReturn(NID_APP, pHashMap != null, null, E_OUT_OF_MEMORY, "HashMap creation failure.");
325
326         pHashMap->Construct();
327
328         SetArgMap(__pBundle, pHashMap);
329
330         if (pHashMap->GetCount() == 0)
331         {
332                 delete pHashMap;
333                 return null;
334         }
335
336         return pHashMap;
337 }
338
339 static bool
340 IsInternalKey(const char* pKey)
341 {
342         if (strncmp(BUNDLE_KEY_PREFIX_AUL, pKey, strlen(BUNDLE_KEY_PREFIX_AUL)) == 0)
343         {
344                 return true;
345         }
346
347         if (strncmp(BUNDLE_KEY_PREFIX_SERVICE, pKey, strlen(BUNDLE_KEY_PREFIX_SERVICE)) == 0)
348         {
349                 return true;
350         }
351
352         if (strncmp(BUNDLE_KEY_PREFIX_OSP, pKey, strlen(BUNDLE_KEY_PREFIX_OSP)) == 0)
353         {
354                 return true;
355         }
356
357         if (strncmp(BUNDLE_KEY_PREFIX_UG, pKey, strlen(BUNDLE_KEY_PREFIX_UG)) == 0)
358         {
359                 return true;
360         }
361
362         return false;
363 }
364
365 static void
366 BundleIterFnCb(const char* pKey, const int type, const bundle_keyval_t* pVal, void* pData)
367 {
368         HashMap* pMap = static_cast<HashMap*>(pData);
369
370         if (pKey && pVal && pMap)
371         {
372                 if (IsInternalKey(pKey))
373                 {
374                         //SysLog(NID_APP, "(%s)", pKey);
375                         return;
376                 }
377
378                 size_t size = 0;
379                 char* pStr = NULL;
380                 switch (type)
381                 {
382                 case BUNDLE_TYPE_STR:
383                         bundle_keyval_get_basic_val(const_cast<bundle_keyval_t*>(pVal), reinterpret_cast<void**>(&pStr), &size);
384                         if (pStr)
385                         {
386                                 pMap->Add(new (std::nothrow) String(pKey), new (std::nothrow) String(pStr));
387                         }
388                         break;
389                 case BUNDLE_TYPE_STR_ARRAY:
390                         {
391                                 void** pArr = NULL;
392                                 bundle_keyval_get_array_val(const_cast<bundle_keyval_t*>(pVal), &pArr, &size, NULL);
393                                 if (pArr && size > 0)
394                                 {
395                                         ArrayList* pList = new (std::nothrow) ArrayList(SingleObjectDeleter);
396                                         if (pList)
397                                         {
398                                                 pList->Construct();
399
400                                                 for (size_t i = 0; i < size; i++)
401                                                 {
402                                                         // type unsafe ugly static casting required
403                                                         pList->Add(new (std::nothrow) String(static_cast<char*>(*(pArr + i))));
404                                                 }
405
406                                                 const int count = pList->GetCount();
407                                                 if (count != 0)
408                                                 {
409                                                         SysLog(NID_APP, "Adding %d elements for %s", count, pKey);
410                                                         pMap->Add(new (std::nothrow) String(pKey), pList);
411                                                 }
412                                                 else
413                                                 {
414                                                         SysLog(NID_APP, "No object for %s", pKey);
415                                                         delete pList;
416                                                 }
417                                         }
418                                 }
419                                 else
420                                 {
421                                         SysLog(NID_APP, "No entry for str array %s(%d)", pKey, size);
422                                 }
423                         }
424                         break;
425                 case BUNDLE_TYPE_BYTE:
426                         bundle_keyval_get_basic_val(const_cast<bundle_keyval_t*>(pVal), reinterpret_cast<void**>(&pStr), &size);
427
428                         SysLog(NID_IO, "Bundle byte value = %s, size = %d", pStr, size);
429
430                         if (pStr)
431                         {
432                                 ByteBuffer* pBuffer = new (std::nothrow) ByteBuffer();
433                                 SysTryLog(NID_IO, pBuffer != null, "The memory is insufficient.");
434                                 result r = pBuffer->Construct(size);
435                                 SysTryLog(NID_IO, r == E_SUCCESS, "Constructing pBuffer is failed.");
436
437                                 r = pBuffer->SetArray((const byte*)pStr, 0, size);
438                                 SysTryLog(NID_IO, r == E_SUCCESS, "SetArray()for ByteBuffer is failed.");
439
440                                 pBuffer->Flip();
441
442                                 pMap->Add(new (std::nothrow) String(pKey), pBuffer);
443                         }
444                         break;
445                 default:
446                         SysLog(NID_APP, "Invalid type for %s : %d", pKey, type);
447                         break;
448                 }
449         }
450 }
451
452 result
453 _AppArg::SetArgMap(bundle* pBundle, HashMap* pMap)
454 {
455         bundle_foreach(pBundle, BundleIterFnCb, reinterpret_cast<void*>(pMap));
456
457         return E_SUCCESS;
458 }
459
460 result
461 _AppArg::SetArgList(bundle* pBundle, ArrayList* pList)
462 {
463         // actual argument below
464         int len = 0;
465         const char** pa = appsvc_get_data_array(pBundle, OSP_K_ARG, &len);
466         if (pa)
467         {
468                 for (int i = 0; i < len; i++)
469                 {
470                         if (pa[i])
471                         {
472                                 //SysLog(NID_APP, "%d/%dth arg [%s]", i, len, pa[i]);
473                                 pList->Add(*new (std::nothrow) String(pa[i]));
474                         }
475                 }
476         }
477
478         const char* p = appsvc_get_uri(pBundle);
479         if (p)
480         {
481                 pList->Add(*new (std::nothrow) String(p));
482                 SysLog(NID_APP, "argument is %s", p);
483         }
484
485         return E_SUCCESS;
486 }
487
488
489 String
490 _AppArg::GetValue(const char* key) const
491 {
492         const char* p = appsvc_get_data(__pBundle, key);
493         return String(p);
494 }
495
496
497 _AppHandler
498 _AppArg::GetHandler(bundle* b)
499 {
500         SysTryReturn(NID_APP, b != null, _APP_HANDLER_NONE, E_INVALID_STATE, "[E_INVALID_STATE] Improper bundle state.");
501
502         const char* p = null;
503
504         p = appsvc_get_data(b, OSP_K_LAUNCH_TYPE);
505
506         if (p)
507         {
508                 if (strcmp(p, OSP_V_LAUNCH_TYPE_DATACONTROL) == 0)
509                 {
510                         return _APP_HANDLER_DATACONTROL;
511                 }
512
513                 if (strcmp(p, OSP_V_LAUNCH_TYPE_APPCONTROL) == 0)
514                 {
515                         SysLog(NID_APP, "Building AppControl arguments.");
516
517                         return _APP_HANDLER_APPCONTROL;
518                 }
519                 else
520                 {
521                         // not appcontrol nor datacontrol => normal launch or condlaunch
522                         p = appsvc_get_data(b, OSP_K_COND);
523                         if (p)
524                         {
525                                 SysLog(NID_APP, "Building Conditional AppLaunch arguments.");
526                                 return _APP_HANDLER_LAUNCH_COND;
527                         }
528                         else
529                         {
530                                 SysLog(NID_APP, "Building Normal AppLaunch arguments.");
531                                 return _APP_HANDLER_LAUNCH_NORMAL;
532                         }
533                 }
534         }
535
536         // fallback
537         return _APP_HANDLER_APPCONTROL;
538 }
539
540
541 int
542 _AppArg::GetCallerPid(bundle* pBundle)
543 {
544         const char* pBundleValue = bundle_get_val(pBundle, AUL_K_ORG_CALLER_PID);
545         if (pBundleValue == NULL)
546         {
547                 pBundleValue = bundle_get_val(pBundle, AUL_K_CALLER_PID);
548         }
549
550         SysTryReturn(NID_APP, pBundleValue != null, -1, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Object not found.");
551
552         return atoi(pBundleValue);
553 }
554
555
556 AppId
557 _AppArg::GetCallerAppId(void) const
558 {
559         const char* pBundleValue = bundle_get_val(__pBundle, AUL_K_CALLER_APPID);
560
561         return String(pBundleValue);
562 }
563
564
565 AppId
566 _AppArg::GetCalleeAppId(void) const
567 {
568         const char* pBundleValue = bundle_get_val(__pBundle, AUL_K_CALLEE_APPID);
569
570         const AppId retVal = pBundleValue;
571
572         return _Aul::GetRealAppId(retVal);
573 }
574
575
576 void
577 _AppArg::AddListFromBundle(ArrayList* pList, bundle* bk, const char* key)
578 {
579         bundle* pBundle = bk;
580
581         const char* p = appsvc_get_data(pBundle, key);
582         if (p)
583         {
584                 pList->Add(*new (std::nothrow) String(p));
585         }
586         else
587         {
588                 pList->Add(*new (std::nothrow) String(L""));
589         }
590 }
591
592
593 result
594 _AppArg::AddStrArray(bundle* b, const String& key, const IList* pList)
595 {
596         std::unique_ptr<char[]> pKey(_StringConverter::CopyToCharArrayN(key));
597
598         return AddStrArray(b, pKey.get(), pList);
599 }
600
601
602 result
603 _AppArg::AddStrArray(bundle* pb, const char* key, const IList* pList)
604 {
605         SysTryReturnResult(NID_APP, pb != NULL, E_INVALID_ARG, "Empty bundle.");
606
607         if (pList == null || pList->GetCount() == 0)
608         {
609                 SysLog(NID_APP, "No element added for bundle.");
610                 return E_SUCCESS;
611         }
612
613         _AppMessageImpl::AddValueArray(pb, key, pList);
614
615         _AppMessageImpl::AddData(pb, pList);
616
617         return E_SUCCESS;
618 }
619
620
621 result
622 _AppArg::AddStrMap(bundle* b, const IMap* pMap)
623 {
624         bundle* pb = b;
625         SysTryReturnResult(NID_APP, pb != NULL, E_INVALID_ARG, "Empty bundle.");
626
627         if (pMap == null || pMap->GetCount() == 0)
628         {
629                 SysLog(NID_APP, "No element added for bundle.");
630                 return E_SUCCESS;
631         }
632
633         std::unique_ptr<IMapEnumerator> pEnum (pMap->GetMapEnumeratorN());
634         while(pEnum->MoveNext() == E_SUCCESS)
635         {
636                 const String* pKey = static_cast<const String*>(pEnum->GetKey());
637                 const Object* pObj = pEnum->GetValue();
638
639                 if (pKey && pObj)
640                 {
641                         if (typeid(*pObj) == typeid(const String))
642                         {
643                                 const String* pVal = static_cast<const String*>(pEnum->GetValue());
644                                 if (pVal)
645                                 {
646                                         _AppMessageImpl::AddData(pb, *pKey, *pVal);
647                                 }
648                         }
649                         else if (typeid(*pObj) == typeid(const ArrayList))
650                         {
651                                 const ArrayList* pList = static_cast<const ArrayList*>(pEnum->GetValue());
652                                 if (pList)
653                                 {
654                                         SysLog(NID_APP, "ArrayList type");
655
656                                         _AppMessageImpl::AddValueArray(pb, *pKey, pList);
657                                 }
658                         }
659                         else if (typeid(*pObj) == typeid(const ByteBuffer))
660                         {
661                                 SysLog(NID_APP, "ByteBuffer type");
662                                 const ByteBuffer* pBuffer = static_cast<const ByteBuffer*>(pObj);
663
664                                 std::unique_ptr<char[]> pBundleKey(_StringConverter::CopyToCharArrayN(*pKey));
665                                 bundle_add_byte(b, pBundleKey.get(), pBuffer->GetPointer(), pBuffer->GetLimit());
666                         }
667                 }
668         }
669
670         return E_SUCCESS;
671 }
672
673
674 result
675 _AppArg::FillMapFromList(IMap* pMap, const IList* pList)
676 {
677         if (pMap == null || pList == null)
678         {
679                 return E_SUCCESS;
680         }
681
682         std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
683         SysTryReturnResult(NID_APP, pEnum != null, E_OUT_OF_MEMORY, "Getting enumerator failed.");
684
685         String key;
686         String value;
687         while (pEnum->MoveNext() == E_SUCCESS)
688         {
689                 String* pStr = dynamic_cast<String*>(pEnum->GetCurrent());
690
691                 int index = -1;
692                 if (pStr == null || pStr->IndexOf(L':', 0, index) != E_SUCCESS)
693                 {
694                         continue;
695                 }
696                 pStr->SubString(0, index, key);
697                 if (key.IsEmpty())
698                 {
699                         continue;
700                 }
701
702                 pStr->SubString(index + 1, value);
703
704                 pMap->Add(new String(key), new String(value));
705
706                 SysLog(NID_APP, "Added (%ls, %ls).", key.GetPointer(), value.GetPointer());
707         }
708
709         return E_SUCCESS;
710 }
711
712
713 result
714 _AppArg::FillLegacyAppControlResult(IList& list, int res, const IMap* pArgs, const Tizen::Base::String& aId)
715 {
716         switch (res)
717         {
718         case APP_CTRL_RESULT_SUCCEEDED:
719                 list.Add(* new (std::nothrow) String(APPCONTROL_RESULT_SUCCEEDED));
720                 break;
721         case APP_CTRL_RESULT_CANCELED:
722                 list.Add(* new (std::nothrow) String(APPCONTROL_RESULT_CANCELED));
723                 return E_SUCCESS;
724         case APP_CTRL_RESULT_TERMINATED:
725                 list.Add(* new (std::nothrow) String(APPCONTROL_RESULT_TERMINATED));
726                 return E_SUCCESS;
727         case APP_CTRL_RESULT_ABORTED:
728                 list.Add(* new (std::nothrow) String("aborted"));
729                 return E_SUCCESS;
730                 //case APP_CTRL_RESULT_FAILED:
731         default:
732                 list.Add(* new (std::nothrow) String(APPCONTROL_RESULT_FAILED));
733                 return E_SUCCESS;
734         }
735
736         if (pArgs == null)
737         {
738                 return E_SUCCESS;
739         }
740
741         bool isPathRegistered = false;
742         // handle APP_CTRL_RESULT_SUCCEEDED only
743         std::unique_ptr<IMapEnumerator> pMapEnum(pArgs->GetMapEnumeratorN());
744
745         while(pMapEnum->MoveNext() == E_SUCCESS)
746         {
747                 String* pKey = static_cast<String*>(pMapEnum->GetKey());
748                 if (pKey == null)
749                 {
750                         SysLog(NID_APP, "Invalid entry.");
751                         continue;
752                 }
753
754                 if (*pKey == L"path" || *pKey == L"http://tizen.org/appcontrol/data/selected")
755                 {
756                         if (!isPathRegistered)
757                         {
758                                 isPathRegistered = true;
759                         }
760                         else
761                         {
762                                 SysLog(NID_APP, "Selected path key is already registered.");
763                                 continue;
764                         }
765                 }
766
767                 String* pVal = dynamic_cast<String*>(pMapEnum->GetValue());
768                 if (pVal)
769                 {
770                         SysLog(NID_APP, "Adding value (%ls).", pVal->GetPointer());
771
772                         StringTokenizer strTok(*pVal, L';');
773                         if (strTok.GetTokenCount() == 0)
774                         {
775                                 list.Add(* new (std::nothrow) String(*pVal));
776                         }
777                         else
778                         {
779                                 String token;
780                                 while(strTok.HasMoreTokens())
781                                 {
782                                         strTok.GetNextToken(token);
783                                         list.Add(* new (std::nothrow) String(token));
784                                         SysLog(NID_APP, "Adding tokenized value (%ls).", token.GetPointer());
785                                 }
786                         }
787                 }
788         }
789
790         return E_SUCCESS;
791 }
792
793
794 ArrayList*
795 _AppArg::GetListN(bundle* b, const char* key)
796 {
797         bundle* pb = b;
798         if (pb == null)
799         {
800                 return null;
801         }
802
803         const char** pValArray = null;
804         int len = 0;
805
806         pValArray = appsvc_get_data_array(b, key, &len);
807         if (len == 0 || pValArray == null)
808         {
809                 return null;
810         }
811
812         ArrayList* pList = new (std::nothrow) ArrayList;
813         SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
814
815         pList->Construct();
816
817         for (int i = 0; i < len; i++)
818         {
819                 pList->Add(*new (std::nothrow) String(pValArray[i]));
820         }
821
822         return pList;
823 }
824
825
826 result
827 _AppArg::CreateNotificationArg(bundle* b, const String& arg)
828 {
829         SysAssertf(b != null, "Valid bundle should be supplied");
830
831         bundle* pb = b;
832
833         if (!arg.IsEmpty())
834         {
835                 std::unique_ptr<char[]> pStr(_StringConverter::CopyToCharArrayN(arg));
836                 bundle_add(pb, TIZEN_NOTIFICATION_DATA, pStr.get());
837         }
838
839         bundle_add(pb, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_LAUNCH);
840
841         return E_SUCCESS;
842 }
843
844
845 result
846 _AppArg::CreateLaunchArg(bundle* b, const IList* pList)
847 {
848         SysAssertf(b != null, "Valid bundle should be supplied");
849
850         bundle* pb = b;
851
852         AddStrArray(pb, OSP_K_ARG, pList);
853
854         bundle_add(pb, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_LAUNCH);
855
856         return E_SUCCESS;
857 }
858
859
860 result
861 _AppArg::CreateAppLaunchConditionArg(bundle* b, const String& condition, const IList* pList)
862 {
863         SysAssertf(b != null, "Valid bundle should be supplied");
864         SysLog(NID_APP, "");
865
866         bundle* pb = b;
867
868         AddStrArray(pb, OSP_K_ARG, pList);
869
870         std::unique_ptr<char[]> p(_StringConverter::CopyToCharArrayN(condition));
871         if (p)
872         {
873                 bundle_add(pb, OSP_K_COND, p.get());
874         }
875
876         bundle_add(pb, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_CONDTION);
877
878         return E_SUCCESS;
879 }
880
881
882 result
883 _AppArg::CreateAppControlArg(bundle* b, const _AppControlImpl& ac, const IList* pList)
884 {
885         SysAssertf(b != null, "Valid bundle should be supplied");
886
887         bundle* pb = b;
888
889         AddStrArray(pb, OSP_K_ARG, pList);
890
891         std::unique_ptr<char[]> pOperation(_StringConverter::CopyToCharArrayN(ac._opId));
892         if (pOperation)
893         {
894                 appsvc_set_operation(pb, pOperation.get());
895         }
896
897         bundle_add(pb, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_APPCONTROL);
898
899         return E_SUCCESS;
900 }
901
902
903 result
904 _AppArg::CreateAppControlArg(bundle* b, const _AppControlImpl& ac, const String* pUriData, const String* pMimeType, const IMap* pMap)
905 {
906         return CreateAppControlArg(b, ac._opId, pUriData, pMimeType, pMap);
907 }
908
909
910 result
911 _AppArg::CreateAppControlArg(bundle* b, const String& oId, const String* pUriData, const String* pMimeType, const IMap* pMap)
912 {
913         SysAssertf(b != null, "Valid bundle should be supplied");
914
915         bundle* pb = b;
916
917         std::unique_ptr<char[]> pOperation(_StringConverter::CopyToCharArrayN(oId));
918         if (pOperation.get())
919         {
920                 appsvc_set_operation(pb, pOperation.get());
921         }
922
923         if (pUriData)
924         {
925                 std::unique_ptr<char[]> pUri(_StringConverter::CopyToCharArrayN(*pUriData));
926                 if (pUri.get())
927                 {
928                         appsvc_set_uri(pb, pUri.get());
929                 }
930         }
931
932         if (pMimeType)
933         {
934                 std::unique_ptr<char[]> pMime(_StringConverter::CopyToCharArrayN(*pMimeType));
935                 if (pMime.get())
936                 {
937                         appsvc_set_mime(pb, pMime.get());
938                 }
939         }
940
941         AddStrMap(pb, pMap);
942
943         bundle_add(pb, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_APPCONTROL);
944
945         return E_SUCCESS;
946 }
947
948
949 namespace
950 {
951 const int MAX_LEN_DATA_CONTROL_REQ_TYPE = 8;
952 }
953 result
954 _AppArg::CreateSqlDataControlArg(bundle* b, const _SqlDataControlImpl& dc, _DataControlRequestType requestType,
955                                                                  const IList* pArgList)
956 {
957         SysAssertf(b != null, "Valid bundle should be supplied");
958
959         bundle_add(b, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_DATACONTROL);
960
961         char dataControlRequestType[MAX_LEN_DATA_CONTROL_REQ_TYPE] = {0, };
962         snprintf(dataControlRequestType, MAX_LEN_DATA_CONTROL_REQ_TYPE, "%d", static_cast<int>(requestType));
963         bundle_add(b, OSP_K_DATACONTROL_REQUEST_TYPE, dataControlRequestType);
964         bundle_add(b, OSP_K_DATACONTROL_PROTOCOL_VERSION, OSP_V_VERSION_2_1_0_3);
965         bundle_add(b, AUL_K_NO_CANCEL, "1");
966
967         std::unique_ptr<char[]> pProvider(_StringConverter::CopyToCharArrayN(dc.__providerId));
968         if (pProvider)
969         {
970                 bundle_add(b, OSP_K_DATACONTROL_PROVIDER, pProvider.get());
971         }
972
973         AddStrArray(b, OSP_K_ARG, pArgList);
974
975         return E_SUCCESS;
976 }
977
978
979 result
980 _AppArg::CreateMapDataControlArg(bundle* b, const _MapDataControlImpl& dc, _DataControlRequestType requestType,
981                                                                  const IList* pArgList)
982 {
983         SysAssertf(b != null, "Valid bundle should be supplied");
984
985         bundle_add(b, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_DATACONTROL);
986
987         char dataControlRequestType[MAX_LEN_DATA_CONTROL_REQ_TYPE] = {0, };
988         snprintf(dataControlRequestType, MAX_LEN_DATA_CONTROL_REQ_TYPE, "%d", static_cast < int >(requestType));
989         bundle_add(b, OSP_K_DATACONTROL_REQUEST_TYPE, dataControlRequestType);
990         bundle_add(b, OSP_K_DATACONTROL_PROTOCOL_VERSION, OSP_V_VERSION_2_1_0_3);
991         bundle_add(b, AUL_K_NO_CANCEL, "1");
992
993         std::unique_ptr<char[]> pProvider(_StringConverter::CopyToCharArrayN(dc.__providerId));
994         if (pProvider)
995         {
996                 bundle_add(b, OSP_K_DATACONTROL_PROVIDER, pProvider.get());
997         }
998
999         AddStrArray(b, OSP_K_ARG, pArgList);
1000
1001         return E_SUCCESS;
1002 }
1003
1004
1005 result
1006 _AppArg::CreateResultArg(bundle* b, const IList* pList)
1007 {
1008         SysAssertf(b != null, "Valid bundle should be supplied");
1009
1010         bundle* pb = b;
1011
1012         AddStrArray(pb, OSP_K_ARG, pList);
1013
1014         _AppMessageImpl::AddData(pb, pList);
1015
1016         return E_SUCCESS;
1017 }
1018
1019
1020 result
1021 _AppArg::CreateResultArg(bundle* b, const IMap* pMap)
1022 {
1023         SysAssertf(b != null, "Valid bundle should be supplied");
1024
1025         bundle* pb = b;
1026
1027         AddStrMap(pb, pMap);
1028
1029         return E_SUCCESS;
1030 }
1031
1032
1033 bundle*
1034 _AppArg::CreateBundleFromSvc(void* svc)
1035 {
1036         bundle* pBundle = GetBundleFromSvc(svc);
1037         if (pBundle)
1038         {
1039                 return bundle_dup(pBundle);
1040         }
1041
1042         return null;
1043 }
1044
1045
1046 bundle*
1047 _AppArg::GetBundleFromSvc(void* svc)
1048 {
1049         bundle* pBundle = NULL;
1050         int ret = service_to_bundle(static_cast<service_h>(svc), &pBundle);
1051
1052         return (ret == SERVICE_ERROR_NONE) ? pBundle : NULL;
1053 }
1054
1055
1056 String
1057 _AppArg::GetRequestId(int num)
1058 {
1059         String str;
1060         str.Format(10, L"req%05d", num);
1061         return str;
1062 }
1063
1064
1065 int
1066 _AppArg::GetRequestId(const String& str)
1067 {
1068         int i = 0;
1069         String sub;
1070
1071         result r = str.SubString(3, sub);
1072         SysTryReturn(NID_APP, !IsFailed(r), -1, r, "[%s] Propagating.", GetErrorMessage(r));
1073
1074         r = Integer::Parse(sub, i);
1075         SysTryReturn(NID_APP, !IsFailed(r), -1, r, "[%s] Propagating.", GetErrorMessage(r));
1076
1077         return i;
1078 }
1079
1080
1081 void
1082 _AppArg::UpdateAppId(bundle* b, const AppId& appId)
1083 {
1084         SysTryReturnVoidResult(NID_APP, b != null, E_INVALID_STATE, "[E_INVALID_STATE] Improper bundle state.");
1085
1086         std::unique_ptr<char[]> pId(_StringConverter::CopyToCharArrayN(appId));
1087         SysTryReturnVoidResult(NID_APP, pId != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Converting %ls failed.", appId.GetPointer());
1088
1089         int res = bundle_add(b, OSP_K_APPID, pId.get());
1090         if (res < 0 && errno == EPERM) // key exists
1091         {
1092                 bundle_del(b, OSP_K_APPID);
1093                 bundle_add(b, OSP_K_APPID, pId.get());
1094         }
1095
1096         appsvc_set_appid(b, pId.get());
1097 }
1098
1099
1100 void
1101 _AppArg::UpdateRequestId(bundle* pBundle, int reqId)
1102 {
1103         SysTryReturnVoidResult(NID_APP, pBundle != null, E_INVALID_STATE, "[E_INVALID_STATE] Improper bundle state.");
1104
1105         if (reqId < 0)
1106         {
1107                 //SysLog(NID_APP, "Requested ID is %d", reqId);
1108                 return;
1109         }
1110
1111         char buffer[32] = {0, };
1112         snprintf(buffer, 32, "%d", reqId);
1113         int res = bundle_add(pBundle, OSP_K_REQUEST_ID, buffer);
1114         if (res < 0 && errno == EPERM) // key exists
1115         {
1116                 bundle_del(pBundle, OSP_K_REQUEST_ID);
1117                 bundle_add(pBundle, OSP_K_REQUEST_ID, buffer);
1118         }
1119 }
1120
1121 void
1122 _AppArg::UpdateKeyValue(bundle* pBundle, const char* pKey, const String& value)
1123 {
1124         SysTryReturnVoidResult(NID_APP, pBundle != null, E_INVALID_STATE, "[E_INVALID_STATE] Improper bundle state.");
1125
1126         char pBuffer[128] = {0, };
1127         snprintf(pBuffer, 128, "%ls", value.GetPointer());
1128         int res = bundle_add(pBundle, pKey, pBuffer);
1129         if (res < 0 && errno == EPERM) // key exists
1130         {
1131                 bundle_del(pBundle, pKey);
1132                 bundle_add(pBundle, pKey, pBuffer);
1133         }
1134 }
1135
1136
1137 void
1138 _AppArg::UpdateSubMode(bundle* pBundle)
1139 {
1140         appsvc_add_data(pBundle, OSP_K_SUBMODE_CALLEE, "1");
1141 }
1142
1143
1144 bool
1145 _AppArg::IsSubMode(bundle* pBundle)
1146 {
1147         const char* p = appsvc_get_data(pBundle, OSP_K_SUBMODE_CALLEE);
1148         return (p && (strncmp(p, "1", sizeof(char)) == 0));
1149 }
1150
1151
1152 void
1153 _AppArg::UpdateServiceApp(bundle* pBundle)
1154 {
1155         appsvc_add_data(pBundle, OSP_K_SERVICE_CALLEE, "1");
1156 }
1157
1158
1159 bool
1160 _AppArg::IsServiceApp(bundle* pBundle)
1161 {
1162         const char* p = appsvc_get_data(pBundle, OSP_K_SERVICE_CALLEE);
1163         return (p && (strncmp(p, "1", sizeof(char)) == 0));
1164 }
1165
1166
1167 int
1168 _AppArg::GetRequestIdFromBundle(bundle* pBundle)
1169 {
1170         const char* p = appsvc_get_data(pBundle, OSP_K_REQUEST_ID);
1171         if (p == NULL)
1172         {
1173                 return -1;
1174         }
1175
1176         int i = atoi(p);
1177         return (i < 0) ? -1 : i;
1178 }
1179
1180
1181 result
1182 _AppArg::UpdateWindowHandle(bundle* pBundle, long handle)
1183 {
1184         appsvc_allow_transient_app(pBundle, handle);
1185
1186         SysLog(NID_APP, "Window Handle 0x%x added.", handle);
1187
1188         return E_SUCCESS;
1189 }
1190
1191
1192 void
1193 _AppArg::PrintSvcHandle(void* svc)
1194 {
1195         service_h service = static_cast<service_h>(svc);
1196
1197         if (service == null)
1198         {
1199                 return;
1200         }
1201
1202         Print(GetBundleFromSvc(service));
1203 }
1204
1205
1206 static void
1207 BundlePrintIterFnCb(const char* pKey, const int type, const bundle_keyval_t* pVal, void* pData)
1208 {
1209         if (pKey && pVal)
1210         {
1211                 size_t size = 0;
1212                 char* pStr = NULL;
1213                 switch (type)
1214                 {
1215                 case BUNDLE_TYPE_STR:
1216                         bundle_keyval_get_basic_val(const_cast<bundle_keyval_t*>(pVal), reinterpret_cast<void**>(&pStr), &size);
1217                         if (pStr)
1218                         {
1219                                 SysSecureLog(NID_APP, "(%s, %s)", pKey, pStr);
1220                         }
1221                         break;
1222                 default:
1223                         SysLog(NID_APP, "Invalid type for %s : %d", pKey, type);
1224                         break;
1225                 }
1226         }
1227 }
1228
1229 void
1230 _AppArg::Print(bundle* b)
1231 {
1232         if (b == null)
1233         {
1234                 return;
1235         }
1236
1237         bundle_foreach(b, BundlePrintIterFnCb, NULL);
1238
1239         int len = 0;
1240         const char** pa = appsvc_get_data_array(b, OSP_K_ARG, &len);
1241         if (pa)
1242         {
1243                 for (int i = 0; i < len; i++)
1244                 {
1245                         if (pa[i])
1246                         {
1247                                 SysLog(NID_APP, "%dth arg [%s]", i, pa[i]);
1248                         }
1249                 }
1250         }
1251 }
1252
1253 } } // Tizen::App