2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FApp_AppManagerProxy.cpp
19 * @brief This is the implementation for the _AppManagerProxy class.
25 #include <FBaseErrors.h>
27 #include <FBaseSysLog.h>
28 #include <FIo_IpcClient.h>
30 #include "FApp_Types.h"
31 #include "FApp_AppInfo.h"
32 #include "FApp_IAppManagerEventListener.h"
33 #include "FApp_IAppManagerServiceEventListener.h"
34 #include "FApp_AppManagerEventArg.h"
35 #include "FApp_AppManagerProxy.h"
36 #include "FApp_AppManagerIpcMessage.h"
41 const char IPC_SERVER_NAME[] = "osp.app.ipcserver.appmanager";
42 const int INVALID_CLIENT_ID = -1;
46 namespace Tizen { namespace App
49 using namespace Tizen::Base;
50 using namespace Tizen::Io;
51 using namespace Tizen::Base::Runtime;
54 _IAppManager* _AppManagerProxy::__pSelf = null;
55 _IAppManagerServiceEventListener* _AppManagerProxy::__pServiceEventListener = null;
57 _AppManagerProxy::_AppManagerProxy(void)
63 _AppManagerProxy::~_AppManagerProxy(void)
70 _AppManagerProxy::Construct(void)
72 __pIpcClient = new (std::nothrow) _IpcClient();
73 SysTryReturnResult(NID_APP, __pIpcClient != null, E_OUT_OF_MEMORY, "_IpcClient creation failed.");
75 result r = __pIpcClient->Construct(IPC_SERVER_NAME, this);
76 SysTryReturn(NID_APP, !IsFailed(r), r, r, "_IpcClient constructing faliied [%s].", GetErrorMessage(r));
82 _AppManagerProxy::GetService(void)
84 _AppManagerProxy* pProxy = null;
88 SysLog(NID_APP, "Create new instance");
90 pProxy = new (std::nothrow) _AppManagerProxy();
91 SysTryReturn(NID_APP, pProxy != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] failed to create _AppManagerProxy");
93 result r = pProxy->Construct();
94 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] pProxy->Construct() failure.", GetErrorMessage(r));
107 _AppManagerProxy::SetService(_IAppManager* pAppManager)
109 __pSelf = pAppManager;
114 _AppManagerProxy::LaunchApplication(const AppId& appId, int req)
116 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
119 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_LaunchApplication(appId, req));
120 result r = __pIpcClient->SendRequest(*pMsg.get());
121 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
127 _AppManagerProxy::TerminateApplication(const AppId& appId)
129 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
132 result response = E_SUCCESS;
133 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_TerminateApplication(appId, &response));
134 result r = __pIpcClient->SendRequest(*pMsg.get());
135 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
141 _AppManagerProxy::IsRunning(const AppId& appId)
143 bool isRunning = false;
145 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_IsRunning(appId, &isRunning));
146 result r = __pIpcClient->SendRequest(*pMsg.get());
147 SysTryReturn(NID_APP, !IsFailed(r), false, r, "SendRequest is failed.");
152 _AppManagerProxy::GetRunningAppList(Collection::ArrayList* pArray)
154 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
158 result response = E_SUCCESS;
159 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_GetRunningAppList(pArray, &response));
160 result r = __pIpcClient->SendRequest(*pMsg.get());
161 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
167 _AppManagerProxy::RegisterApplication(const String& packageId, const String& executableName, _AppType appType, int pId)
169 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
172 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_RegisterApplication(packageId, executableName, static_cast<int>(appType), pId));
173 result r = __pIpcClient->SendRequest(*pMsg.get());
174 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
180 _AppManagerProxy::UnregisterApplication(int pId)
182 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
186 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_UnregisterApplication(pId));
187 result r = __pIpcClient->SendRequest(*pMsg.get());
188 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
194 _AppManagerProxy::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
196 IPC_BEGIN_MESSAGE_MAP(_AppManagerProxy, message)
197 IPC_MESSAGE_HANDLER_EX(AppManager_OnEventReceived, &client, OnEventReceived )
198 IPC_MESSAGE_HANDLER_EX(AppManager_OnTerminateApplicationRequested, &client, OnTerminateApplicationRequested )
199 IPC_END_MESSAGE_MAP()
203 _AppManagerProxy::OnEventReceived(const _AppManagerEventArg& arg)
205 SysTryReturnResult(NID_APP, __pServiceEventListener != null, E_INVALID_STATE, "__pServiceEventListener instance must not be null.");
207 __pServiceEventListener->OnServiceEventReceived(-1, arg);
213 _AppManagerProxy::OnTerminateApplicationRequested(void)
215 SysTryReturnVoidResult(NID_APP, __pServiceEventListener != null, E_INVALID_STATE, "[E_INVALID_STATE] __pServiceEventListener instance must not be null.");
217 __pServiceEventListener->OnTerminateApplicationRequested(-1);
221 _AppManagerProxy::InitEventListener(_IAppManagerServiceEventListener* pListener)
223 __pServiceEventListener = pListener;
228 _AppManagerProxy::AddEventListener(int clientId)
230 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
233 std::auto_ptr<IPC::Message> pMsg(new (std::nothrow) AppManager_AddEventListener(_AppInfo::GetProcessId()));
234 result r = __pIpcClient->SendRequest(*pMsg);
235 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
241 _AppManagerProxy::RemoveEventListener(int clientId)
243 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
246 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_RemoveEventListener(_AppInfo::GetProcessId()));
247 result r = __pIpcClient->SendRequest(*pMsg);
248 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");