2804ead99465cf6d2764651113fd8e418268b525
[framework/osp/web.git] / src / controls / FWebCtrl_WebManager.cpp
1 //\r
2 // Open Service Platform\r
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.\r
4 //\r
5 // Licensed under the Apache License, Version 2.0 (the License);\r
6 // you may not use this file except in compliance with the License.\r
7 // You may obtain a copy of the License at\r
8 //\r
9 //     http://www.apache.org/licenses/LICENSE-2.0\r
10 //\r
11 // Unless required by applicable law or agreed to in writing, software\r
12 // distributed under the License is distributed on an "AS IS" BASIS,\r
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14 // See the License for the specific language governing permissions and\r
15 // limitations under the License.\r
16 //\r
17 \r
18 /**\r
19  * @file                FWebCtrl_WebManager.cpp\r
20  * @brief               The file contains the definition of _WebManager class.\r
21  *\r
22  * The file contains the definition of _WebManager class.\r
23  */\r
24 #include <pthread.h>\r
25 #include <net_connection.h>\r
26 #include <FBaseSysLog.h>\r
27 #include <FUiControl.h>\r
28 #include <FUiCtrl_FormImpl.h>\r
29 #include "FWebCtrl_EflWebkit.h"\r
30 #include "FWebCtrl_Web.h"\r
31 #include "FWebCtrl_WebImpl.h"\r
32 #include "FWebCtrl_WebManager.h"\r
33 #include "FWebCtrl_WebPopup.h"\r
34 \r
35 using namespace Tizen::Base;\r
36 using namespace Tizen::Base::Collection;\r
37 using namespace Tizen::Ui;\r
38 using namespace Tizen::Ui::Controls;\r
39 \r
40 namespace Tizen { namespace Web { namespace Controls\r
41 {\r
42 \r
43 \r
44 _WebManager* _WebManager::__pInstance = null;\r
45 \r
46 \r
47 _WebManager::_WebManager(void)\r
48         : __pWebList(null)\r
49         , __pCallbackList(null)\r
50         , __pActiveWeb(null)\r
51         , __pActivePopup(null)\r
52         , __pProxy(null)\r
53 {\r
54 }\r
55 \r
56 _WebManager::~_WebManager(void)\r
57 {\r
58         free(__pProxy);\r
59 }\r
60 \r
61 \r
62 void\r
63 _WebManager::InitWebManager(void)\r
64 {\r
65         result r = E_SUCCESS;\r
66 \r
67         std::unique_ptr<_WebManager> pInstance(new (std::nothrow) _WebManager());\r
68         SysTryReturnVoidResult(NID_WEB_CTRL, pInstance.get(), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));\r
69 \r
70         r = pInstance->Construct();\r
71         SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));\r
72 \r
73         __pInstance = pInstance.release();\r
74         std::atexit(DestroyWebManager);\r
75 }\r
76 \r
77 \r
78 void\r
79 _WebManager::DestroyWebManager(void)\r
80 {\r
81         delete __pInstance;\r
82         __pInstance = null;\r
83 }\r
84 \r
85 \r
86 _WebManager*\r
87 _WebManager::GetInstance(void)\r
88 {\r
89         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;\r
90         if (__pInstance== null)\r
91         {\r
92                 ClearLastResult();\r
93                 pthread_once(&onceBlock, InitWebManager);\r
94                 result r = GetLastResult();\r
95                 if (IsFailed(r))\r
96                 {\r
97                         onceBlock = PTHREAD_ONCE_INIT;\r
98                 }\r
99         }\r
100 \r
101         return __pInstance;\r
102 }\r
103 \r
104 \r
105 result\r
106 _WebManager::Construct(void)\r
107 {\r
108         result r = E_SUCCESS;\r
109 \r
110         std::unique_ptr<ArrayListT< int> > pWebList(new (std::nothrow) ArrayListT< int >());\r
111         SysTryReturnResult(NID_WEB_CTRL, pWebList.get(), E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");\r
112         r = pWebList->Construct();\r
113         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));\r
114 \r
115         std::unique_ptr<MultiHashMapT< int, int > > pCallbackList(new (std::nothrow) MultiHashMapT< int, int >());\r
116         SysTryReturnResult(NID_WEB_CTRL, pCallbackList.get(), E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");\r
117         r = pCallbackList->Construct();\r
118         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));\r
119 \r
120         r = InitializeProxyAddress();\r
121         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));      \r
122
123         __pWebList = std::move(pWebList);\r
124         __pCallbackList = std::move(pCallbackList);\r
125 \r
126         return E_SUCCESS;\r
127 }\r
128 \r
129 \r
130 result\r
131 _WebManager::InitializeProxyAddress(void)\r
132 {\r
133         int ret = -1;\r
134         connection_h handle = null;\r
135         char* pProxy = null;\r
136 \r
137         ret = connection_create(&handle);
138         SysTryReturnResult(NID_WEB_CTRL, ret == CONNECTION_ERROR_NONE, E_SYSTEM, "A system error has been occurred. Failed to create connection.");
139 \r
140         connection_address_family_e family = CONNECTION_ADDRESS_FAMILY_IPV4;
141
142         ret = connection_get_proxy(handle, family, &pProxy);
143         SysTryReturnResult(NID_WEB_CTRL, ret == CONNECTION_ERROR_NONE, E_SYSTEM, "A system error has been occurred. Failed to get proxy address.");\r
144 \r
145         ret = connection_destroy(handle);
146         SysTryReturnResult(NID_WEB_CTRL, ret == CONNECTION_ERROR_NONE, E_SYSTEM, "A system error has been occurred. Failed to destroy connection.");\r
147 \r
148         __pProxy = pProxy;\r
149 \r
150         return E_SUCCESS;\r
151 }\r
152 \r
153 \r
154 result\r
155 _WebManager::AddWeb(int webAdress)\r
156 {\r
157         result r = E_SUCCESS;\r
158 \r
159         _WebImpl* pImpl = reinterpret_cast< _WebImpl* >(webAdress);\r
160 \r
161         _Web* pWebCore = dynamic_cast< _Web* >(&(pImpl->GetCore()));\r
162 \r
163         r = pWebCore->GetEflWebkit()->SetProxyAddress(__pProxy);\r
164         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));\r
165 \r
166         r = __pWebList->Add(webAdress);\r
167         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));\r
168 \r
169         return E_SUCCESS;\r
170 }\r
171 \r
172 \r
173 result\r
174 _WebManager::RemoveWeb(int webAdress)\r
175 {\r
176         result r = E_SUCCESS;\r
177 \r
178         r = __pWebList->Remove(webAdress);\r
179         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));\r
180 \r
181         __pCallbackList->Remove(webAdress);\r
182 \r
183         return E_SUCCESS;\r
184 }\r
185 \r
186 \r
187 bool\r
188 _WebManager::IsValidWeb(int webAdress) const\r
189 {\r
190         return __pWebList->Contains(webAdress);\r
191 }\r
192 \r
193 \r
194 result\r
195 _WebManager::AddCallback(int callerAdress, int callbackAdress)\r
196 {\r
197         result r = E_SUCCESS;\r
198 \r
199         r = __pCallbackList->Add(callerAdress, callbackAdress);\r
200         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));\r
201 \r
202         return E_SUCCESS;\r
203 }\r
204 \r
205 \r
206 result\r
207 _WebManager::RemoveCallback(int callerAdress, int callbackAdress)\r
208 {\r
209         result r = E_SUCCESS;\r
210 \r
211         r = __pCallbackList->Remove(callerAdress, callbackAdress);\r
212         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));\r
213 \r
214         return E_SUCCESS;\r
215 }\r
216 \r
217 \r
218 bool\r
219 _WebManager::IsValidCallback(int callerAdress, int callbackAdress) const\r
220 {\r
221         bool ret = true;\r
222 \r
223         result r = __pCallbackList->Contains(callerAdress, callbackAdress, ret);\r
224         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, true, r, "[%s] Propagating.", GetErrorMessage(r));\r
225 \r
226         return ret;\r
227 }\r
228 \r
229 \r
230 void\r
231 _WebManager::SetActiveWeb(Tizen::Web::Controls::_WebImpl* pWebAddress)\r
232 {\r
233         if (__pActiveWeb == null)\r
234         {\r
235                 __pActiveWeb = pWebAddress;\r
236         }\r
237         else\r
238         {\r
239                 if (__pActiveWeb->IsVisible() == true)\r
240                 {\r
241                         return;\r
242                 }\r
243                 __pActiveWeb = pWebAddress;\r
244         }\r
245 \r
246         if (__pActivePopup != null)\r
247         {\r
248                 __pActivePopup->SetShowState(false);\r
249 \r
250                 _FormImpl* pFormImpl = __pActiveWeb->GetParentFormImpl(__pActiveWeb);\r
251 \r
252                 if (pFormImpl != null)\r
253                 {\r
254                         __pActivePopup->SetOwner(&pFormImpl->GetPublic());\r
255                 }\r
256                 else\r
257                 {\r
258                         __pActivePopup->SetOwner(&__pActiveWeb->GetPublic());\r
259                 }\r
260 \r
261                 __pActivePopup->SetShowState(true);\r
262                 __pActivePopup->Show();\r
263         }\r
264 }\r
265 \r
266 \r
267 void\r
268 _WebManager::RemoveActiveWeb(Tizen::Web::Controls::_WebImpl* pWebAddress)\r
269 {\r
270         if (__pActiveWeb == pWebAddress)\r
271         {\r
272                 __pActiveWeb = null;\r
273         }\r
274 }\r
275 \r
276 \r
277 void\r
278 _WebManager::SetActivePopup(Tizen::Web::Controls::_WebPopup* pPopupAddress)\r
279 {\r
280         __pActivePopup = pPopupAddress;\r
281 }\r
282 \r
283 \r
284 void\r
285 _WebManager::RemoveActivePopup(Tizen::Web::Controls::_WebPopup* pPopupAddress)\r
286 {\r
287         if (__pActivePopup == pPopupAddress)\r
288         {\r
289                 __pActivePopup = null;\r
290         }\r
291 }\r
292 \r
293 \r
294 } } }\r