fix proxy not to set instance when it's failed to construct, check exception for...
[platform/framework/native/shell.git] / src / FShell_AppWidgetManagerProxy.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_AppWidgetManagerProxy.cpp
19  * @brief       This is the implementation for the _AppWidgetManagerProxy class.
20  */
21
22 #include <FBaseRt.h>
23 #include <FIo_IpcClient.h>
24 #include <FShell_AppWidgetManagerIpcMessage.h>
25 #include "FShell_AppWidgetProviderManagerImpl.h"
26 #include "FShell_AppWidgetManagerProxy.h"
27
28 namespace Tizen { namespace Shell {
29
30 namespace
31 {
32 const char IPC_SERVER_NAME[] = "osp.shell.ipcserver.appwidgetmanager";
33 };
34
35 using namespace Tizen::Io;
36
37
38 _AppWidgetManagerProxy::_AppWidgetManagerProxy()
39         : __pIpcClient(null)
40 {
41         SysLog(NID_SHELL, "");
42 }
43
44 _AppWidgetManagerProxy::~_AppWidgetManagerProxy()
45 {
46         if (__pIpcClient)
47         {
48                 delete __pIpcClient;
49         }
50 }
51
52 _AppWidgetManagerProxy*
53 _AppWidgetManagerProxy::GetInstance()
54 {
55         static std::unique_ptr<_AppWidgetManagerProxy> __pAppWidgetProxy(null);
56         if (__pAppWidgetProxy.get() == null)
57         {
58                 std::unique_ptr<_AppWidgetManagerProxy> pAppWidgetProxy(new (std::nothrow)_AppWidgetManagerProxy);
59                 SysTryReturn(NID_SHELL, pAppWidgetProxy.get(), null, E_OUT_OF_MEMORY, "Allocating new _AppWidgetManagerProxy failed!");
60
61                 result r = pAppWidgetProxy->Construct();
62                 SysTryReturn(NID_SHELL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
63
64                 __pAppWidgetProxy = std::move(pAppWidgetProxy);
65         }
66         else
67         {
68                 if (!__pAppWidgetProxy->IsConnected())
69                 {
70                         result r = __pAppWidgetProxy->Construct();
71                         SysTryReturn(NID_SHELL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
72                 }
73         }
74
75         return __pAppWidgetProxy.get();
76 }
77
78 result
79 _AppWidgetManagerProxy::Construct(void)
80 {
81         SysLog(NID_SHELL, "Enter.");
82
83         std::unique_ptr<_IpcClient> pIpcClient(new (std::nothrow) _IpcClient() );
84         SysTryReturnResult(NID_SHELL, pIpcClient, E_OUT_OF_MEMORY, "_IpcClient creation failed.");
85
86         const int MAX_TRY_COUNT = 5;
87         const int TRY_SLEEP_TIME = 250;
88
89         int count = 0;
90         while (true)
91         {
92                 result r = pIpcClient->Construct(IPC_SERVER_NAME, this);
93                 if (r == E_SUCCESS)
94                 {
95                         SysLog(NID_APP, "Succeeded in connecting service(%s)", IPC_SERVER_NAME);
96                         __pIpcClient = pIpcClient.release();
97                         return E_SUCCESS;
98                 }
99
100                 SysTryReturn(NID_APP, ++count < MAX_TRY_COUNT, E_SYSTEM, r, "[%s] Failed to connect service.(%s)", GetErrorMessage(r), IPC_SERVER_NAME);
101
102                 Tizen::Base::Runtime::Thread::Sleep(TRY_SLEEP_TIME);
103         }
104
105         SysLog(NID_SHELL, "Exit.");
106         return E_SUCCESS;
107 }
108
109 result
110 _AppWidgetManagerProxy::RequestUpdate(const Tizen::App::AppId& appId, const Tizen::Base::String& providerName, const Tizen::Base::String& argument)
111 {
112         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_SYSTEM, "__pIpcClient instance must not be null.");
113         SysLog(NID_SHELL, "Enter.");
114 //      String providerId;
115 //      if( providerName.IsEmpty() == true)
116 //      {
117 //              providerId = appId;
118 //      }
119 //      else
120 //      {
121 //              providerId = appId + "." + providerName;
122 //      }
123
124         result ret = E_SUCCESS;
125         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestUpdate(appId, providerName, argument, &ret));
126         result r = __pIpcClient->SendRequest(*pMsg.get());
127         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest failed.");
128
129         return ret;
130 }
131
132 result
133 _AppWidgetManagerProxy::RequestUpdateInstance(const Tizen::Base::String& instanceId, const Tizen::Base::String& argument)
134 {
135         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_SYSTEM, "__pIpcClient instance must not be null.");
136         SysLog(NID_SHELL, "Enter.");
137
138         result ret = E_SUCCESS;
139         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestUpdateInstance(instanceId, argument, &ret));
140         result r = __pIpcClient->SendRequest(*pMsg.get());
141         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest failed.");
142
143         return ret;
144 }
145
146 result
147 _AppWidgetManagerProxy::RequestSharedMemoryId(const Tizen::Base::String& instanceId, int width, int height, int& shmId )
148 {
149         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_SYSTEM, "__pIpcClient instance must not be null.");
150
151         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestSharedMemoryId(instanceId, width, height, &shmId));
152         result r = __pIpcClient->SendRequest(*pMsg.get());
153         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest failed.");
154         SysLog(NID_SHELL, "shmId(%d) is retrieved from appwidget-service", shmId);
155
156         return E_SUCCESS;
157 }
158
159 result
160 _AppWidgetManagerProxy::RequestSharedMemoryIdForPD(const Tizen::Base::String& instanceId, int width, int height, int& shmId )
161 {
162         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_SYSTEM, "__pIpcClient instance must not be null.");
163
164         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestSharedMemoryIdForPD(instanceId, width, height, &shmId));
165         result r = __pIpcClient->SendRequest(*pMsg.get());
166         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest is failed.");
167         SysLog(NID_SHELL, "shmId(%d) is retrieved from appwidget-service", shmId);
168
169         return E_SUCCESS;
170 }
171
172 result
173 _AppWidgetManagerProxy::RequestSyncSharedMemory(const Tizen::Base::String& instanceId, int width, int height)
174 {
175         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_SYSTEM, "__pIpcClient instance must not be null.");
176         SysLog(NID_SHELL, "");
177
178         result ret = E_FAILURE;
179         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestSyncSharedMemory(instanceId, width, height, &ret));
180         result r = __pIpcClient->SendRequest(*pMsg.get());
181         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest failed.");
182         return E_SUCCESS;
183 }
184
185 result
186 _AppWidgetManagerProxy::RequestSyncSharedMemoryForPD(const Tizen::Base::String& instanceId)
187 {
188         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_SYSTEM, "__pIpcClient instance must not be null.");
189         SysLog(NID_SHELL, "");
190
191         result ret = E_FAILURE;
192         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestSyncSharedMemoryForPD(instanceId, &ret));
193         result r = __pIpcClient->SendRequest(*pMsg.get());
194         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest failed.");
195         return E_SUCCESS;
196 }
197
198 result
199 _AppWidgetManagerProxy::RequestReleaseSharedMemory(const Tizen::Base::String& instanceId)
200 {
201         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_SYSTEM, "__pIpcClient instance must not be null.");
202         SysLog(NID_SHELL, "");
203
204         result ret = E_FAILURE;
205         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestReleaseSharedMemory(instanceId, &ret));
206         result r = __pIpcClient->SendRequest(*pMsg.get());
207         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest failed.");
208         return E_SUCCESS;
209 }
210
211 result
212 _AppWidgetManagerProxy::RequestReleaseSharedMemoryForPD(const Tizen::Base::String& instanceId)
213 {
214         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_SYSTEM, "__pIpcClient instance must not be null.");
215         SysLog(NID_SHELL, "");
216
217         result ret = E_FAILURE;
218         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestReleaseSharedMemoryForPD(instanceId, &ret));
219         result r = __pIpcClient->SendRequest(*pMsg.get());
220         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest failed.");
221         return E_SUCCESS;
222 }
223
224 result
225 _AppWidgetManagerProxy::SendResult(const Tizen::Base::String& instanceId, bool isSucceeded)
226 {
227         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_SYSTEM, "__pIpcClient instance must not be null.");
228         SysLog(NID_SHELL, "");
229
230         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_SendResult(instanceId, isSucceeded));
231         result r = __pIpcClient->SendRequest(*pMsg.get());
232         SysTryReturnResult(NID_SHELL, !IsFailed(r), r, "SendResult failed.");
233
234         return E_SUCCESS;
235 }
236
237 result
238 _AppWidgetManagerProxy::OnTouchEventReceived(const Tizen::Base::String& instanceId, int eventType, double timeStamp, double x, double y )
239 {
240         return _AppWidgetProviderManagerImpl::GetInstance()->ForwardTouchEvent(instanceId, eventType, timeStamp, x, y);
241 }
242
243 result
244 _AppWidgetManagerProxy::OnTouchEventReceivedForPD(const Tizen::Base::String& instanceId, int eventType, double timeStamp, double x, double y )
245 {
246         return _AppWidgetProviderManagerImpl::GetInstance()->ForwardTouchEventForPD(instanceId, eventType, timeStamp, x, y);
247 }
248
249 void
250 _AppWidgetManagerProxy::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
251 {
252         SysLog(NID_SHELL, "Enter.");
253         IPC_BEGIN_MESSAGE_MAP(_AppWidgetManagerProxy, message)
254                 IPC_MESSAGE_HANDLER_EX(AppWidgetManager_SendTouchEvent, &client, OnTouchEventReceived )
255                 IPC_MESSAGE_HANDLER_EX(AppWidgetManager_SendTouchEventForPD, &client, OnTouchEventReceivedForPD )
256         IPC_END_MESSAGE_MAP()
257
258         SysLog(NID_SHELL, "Exit.");
259 }
260
261 bool
262 _AppWidgetManagerProxy::IsConnected(void) const
263 {
264         return (__pIpcClient != null);
265 }
266
267 void
268 _AppWidgetManagerProxy::OnIpcServerDisconnected(_IpcClient& client)
269 {
270         SysLog(NID_SHELL, "IPC Server[%ls] is disconnected.", client.GetName().GetPointer());
271
272         delete __pIpcClient;
273         __pIpcClient = null;
274 }
275
276 }} // Tizen::Shell