b9ce8338ab26ac4c0065e5e2df1019dbb63cfaf4
[platform/framework/native/appwidget-service.git] / src / FShell_AppWidgetContextHelper.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_AppWidgetContextHelper.cpp
19  * @brief       This is the implementation for the _AppWidgetContextHelper class.
20  */
21
22 #include <unique_ptr.h>
23 #include <FBaseColHashMap.h>
24 #include <FBaseString.h>
25 #include <FAppTypes.h>
26 #include <FBase_StringConverter.h>
27 #include <FApp_AppArg.h>
28 #include <FApp_AppMessageImpl.h>
29 #include <FApp_AppControlManager.h>
30 #include "FShell_AppWidgetContextHelper.h"
31 #include "FShell_AppWidgetManagerService.h"
32
33 namespace Tizen { namespace Shell  { namespace App
34 {
35
36 using namespace Tizen::App;
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39
40 result
41 _AppWidgetRequestHelper::SendAppControlRequest(const AppId& appId, const String& operation, HashMap* pArgs)
42 {
43         const String* pInstanceId = dynamic_cast<const String*>(pArgs->GetValue(String(ARG_KEY_INSTANCE_ID)));
44         SysTryReturnResult(NID_SHELL, pInstanceId, E_FAILURE, "Failed to get the instance id.");
45
46         SysSecureLog(NID_SHELL, "[ac] pInstanceId(%ls), operation(%ls), appId(%ls), arg count(%d)", pInstanceId->GetPointer(), operation.GetPointer(), appId.GetPointer(), pArgs->GetCount());
47
48         _AppMessageImpl msg;
49         msg.AddData(OSP_K_APPCONTROL_INTERNAL_OPERATION, L"appwidget");
50         Tizen::App::_AppArg::AddStrMap(msg.GetBundle(), pArgs);
51
52         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId) );
53         std::unique_ptr<char[]> pOperation(_StringConverter::CopyToCharArrayN(operation) );
54
55         const int TRY_COUNT = 3;
56         const int TRY_SLEEP_TIME = 65;
57
58         int retry = 0;
59
60         result r = E_FAILURE;
61         while (true)
62         {
63                 r = Tizen::App::_AppControlManager::GetInstance()->LaunchPkg(msg, pAppId.get(), pOperation.get(), null, null, null, null);
64                 if( r == E_SUCCESS)
65                 {
66                         SysLog(NID_SHELL, "[%s] Successed to invoke LaunchPkg()", GetErrorMessage(r));
67                         break;
68                 }
69
70                 if( retry ++ >= TRY_COUNT)
71                 {
72                         break;
73                 }
74
75                 SysLog(NID_SHELL, "[%s] Failed to invoke LaunchPkg(%s), %dth retry.", GetErrorMessage(r), pOperation.get(), retry);
76                 Tizen::Base::Runtime::Thread::Sleep(TRY_SLEEP_TIME);
77         }
78
79         return r;
80 }
81
82 result
83 _AppWidgetRequestHelper::SendIpcRequest(const int clientId, const String& operation, HashMap* pArgs)
84 {
85         const String* pInstanceId = dynamic_cast<const String*>(pArgs->GetValue(String(ARG_KEY_INSTANCE_ID)));
86         SysTryReturnResult(NID_SHELL, pInstanceId, E_FAILURE, "Failed to get the instance id.");
87
88         SysSecureLog(NID_SHELL, "[ipc] pInstanceId(%ls), operation(%ls), clientId(%d)", pInstanceId->GetPointer(), operation.GetPointer(), clientId);
89
90         bool sendResult = AppWidgetManagerService::GetInstance()->SendAppWidgetEvent(clientId, *pInstanceId, operation, pArgs);
91         SysTryReturnResult(NID_SHELL, sendResult, E_FAILURE, "Propagated.");
92
93         return E_SUCCESS;
94 }
95
96 }}}  // Tizen::Shell::App