61e90bc65f38ef84e4f9dbdda30ca4528394bbea
[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         __pIpcClient = new (std::nothrow) _IpcClient();
84         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_OUT_OF_MEMORY, "_IpcClient creation failed.");
85
86         const int MAX_TRY_COUNT = 5;
87         const int TRY_SLEEP_TIME = 500;
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                         return E_SUCCESS;
97                 }
98
99                 SysTryReturn(NID_APP, ++count < MAX_TRY_COUNT, E_SYSTEM, r, "[%s] Failed to connect service.(%s)", GetErrorMessage(r), IPC_SERVER_NAME);
100
101                 Tizen::Base::Runtime::Thread::Sleep(TRY_SLEEP_TIME);
102         }
103
104         SysLog(NID_SHELL, "Exit.");
105         return E_SUCCESS;
106 }
107
108 result
109 _AppWidgetManagerProxy::RequestUpdate(const Tizen::App::AppId& appId, const Tizen::Base::String& providerName, const Tizen::Base::String& argument)
110 {
111         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
112         SysLog(NID_SHELL, "Enter.");
113 //      String providerId;
114 //      if( providerName.IsEmpty() == true)
115 //      {
116 //              providerId = appId;
117 //      }
118 //      else
119 //      {
120 //              providerId = appId + "." + providerName;
121 //      }
122
123         result ret = E_SUCCESS;
124         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestUpdate(appId, providerName, argument, &ret));
125         result r = __pIpcClient->SendRequest(*pMsg.get());
126         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest failed.");
127
128         return ret;
129 }
130
131 result
132 _AppWidgetManagerProxy::RequestUpdateInstance(const Tizen::Base::String& instanceId, const Tizen::Base::String& argument)
133 {
134         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
135         SysLog(NID_SHELL, "Enter.");
136
137         result ret = E_SUCCESS;
138         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestUpdateInstance(instanceId, argument, &ret));
139         result r = __pIpcClient->SendRequest(*pMsg.get());
140         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest failed.");
141
142         return ret;
143 }
144
145 result
146 _AppWidgetManagerProxy::RequestSharedMemoryId(const Tizen::Base::String& instanceId, int width, int height, int& shmId )
147 {
148         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
149
150         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestSharedMemoryId(instanceId, width, height, &shmId));
151         result r = __pIpcClient->SendRequest(*pMsg.get());
152         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest failed.");
153         SysLog(NID_SHELL, "shmId(%d) is retrieved from appwidget-service", shmId);
154
155         return E_SUCCESS;
156 }
157
158 result
159 _AppWidgetManagerProxy::RequestSharedMemoryIdForPD(const Tizen::Base::String& instanceId, int width, int height, int& shmId )
160 {
161         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
162
163         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestSharedMemoryIdForPD(instanceId, width, height, &shmId));
164         result r = __pIpcClient->SendRequest(*pMsg.get());
165         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest is failed.");
166         SysLog(NID_SHELL, "shmId(%d) is retrieved from appwidget-service", shmId);
167
168         return E_SUCCESS;
169 }
170
171 result
172 _AppWidgetManagerProxy::RequestSyncSharedMemory(const Tizen::Base::String& instanceId, int width, int height)
173 {
174         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
175         SysLog(NID_SHELL, "");
176
177         result ret = E_FAILURE;
178         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestSyncSharedMemory(instanceId, width, height, &ret));
179         result r = __pIpcClient->SendRequest(*pMsg.get());
180         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest failed.");
181         return E_SUCCESS;
182 }
183
184 result
185 _AppWidgetManagerProxy::RequestSyncSharedMemoryForPD(const Tizen::Base::String& instanceId)
186 {
187         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
188         SysLog(NID_SHELL, "");
189
190         result ret = E_FAILURE;
191         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestSyncSharedMemoryForPD(instanceId, &ret));
192         result r = __pIpcClient->SendRequest(*pMsg.get());
193         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest failed.");
194         return E_SUCCESS;
195 }
196
197 result
198 _AppWidgetManagerProxy::RequestReleaseSharedMemory(const Tizen::Base::String& instanceId)
199 {
200         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
201         SysLog(NID_SHELL, "");
202
203         result ret = E_FAILURE;
204         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestReleaseSharedMemory(instanceId, &ret));
205         result r = __pIpcClient->SendRequest(*pMsg.get());
206         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest failed.");
207         return E_SUCCESS;
208 }
209
210 result
211 _AppWidgetManagerProxy::RequestReleaseSharedMemoryForPD(const Tizen::Base::String& instanceId)
212 {
213         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
214         SysLog(NID_SHELL, "");
215
216         result ret = E_FAILURE;
217         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_RequestReleaseSharedMemoryForPD(instanceId, &ret));
218         result r = __pIpcClient->SendRequest(*pMsg.get());
219         SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "SendRequest failed.");
220         return E_SUCCESS;
221 }
222
223 result
224 _AppWidgetManagerProxy::SendResult(const Tizen::Base::String& instanceId, bool isSucceeded)
225 {
226         SysTryReturnResult(NID_SHELL, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
227         SysLog(NID_SHELL, "");
228
229         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppWidgetManager_SendResult(instanceId, isSucceeded));
230         result r = __pIpcClient->SendRequest(*pMsg.get());
231         SysTryReturnResult(NID_SHELL, !IsFailed(r), r, "SendResult failed.");
232
233         return E_SUCCESS;
234 }
235
236 result
237 _AppWidgetManagerProxy::OnTouchEventReceived(const Tizen::Base::String& instanceId, int eventType, double timeStamp, double x, double y )
238 {
239         return _AppWidgetProviderManagerImpl::GetInstance()->ForwardTouchEvent(instanceId, eventType, timeStamp, x, y);
240 }
241
242 result
243 _AppWidgetManagerProxy::OnTouchEventReceivedForPD(const Tizen::Base::String& instanceId, int eventType, double timeStamp, double x, double y )
244 {
245         return _AppWidgetProviderManagerImpl::GetInstance()->ForwardTouchEventForPD(instanceId, eventType, timeStamp, x, y);
246 }
247
248 void
249 _AppWidgetManagerProxy::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
250 {
251         SysLog(NID_SHELL, "Enter.");
252         IPC_BEGIN_MESSAGE_MAP(_AppWidgetManagerProxy, message)
253                 IPC_MESSAGE_HANDLER_EX(AppWidgetManager_SendTouchEvent, &client, OnTouchEventReceived )
254                 IPC_MESSAGE_HANDLER_EX(AppWidgetManager_SendTouchEventForPD, &client, OnTouchEventReceivedForPD )
255         IPC_END_MESSAGE_MAP()
256
257         SysLog(NID_SHELL, "Exit.");
258 }
259
260 bool
261 _AppWidgetManagerProxy::IsConnected(void) const
262 {
263         return (__pIpcClient != null);
264 }
265
266 void
267 _AppWidgetManagerProxy::OnIpcServerDisconnected(_IpcClient& client)
268 {
269         SysLog(NID_SHELL, "IPC Server[%ls] is disconnected.", client.GetName().GetPointer());
270
271         delete __pIpcClient;
272         __pIpcClient = null;
273 }
274
275 }} // Tizen::Shell