Remove interval for box update and check appid of alarm register
[platform/framework/web/web-provider.git] / src / Core / BoxManager.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
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  * @file    BoxManager.cpp
18  * @author  Yunchan Cho (yunchan.cho@samsung.com)
19  */
20 #include <string>
21 #include <map>
22 #include <ctime>
23 #include <ewk_context.h>
24 #include <Plugin/IBoxPluginFactory.h>
25 #include <Plugin/box_plugin_interface.h>
26 #include "Util/Log.h"
27 #include "IBox.h"
28 #include "Box.h"
29 #include "BoxData.h"
30 #include "BoxManager.h"
31
32 BoxManager::BoxManager(IBoxPluginFactoryPtr factory)
33     : m_boxFactory(factory)
34 {
35     LogD("enter");
36 }
37
38 BoxManager::~BoxManager()
39 {
40     LogD("enter");
41 }
42
43 bool BoxManager::doCommand(const request_cmd_type type, const BoxInfoPtr& boxInfo)
44 {
45     bool result = false;
46
47     switch (type) {
48     case REQUEST_CMD_ADD_BOX:
49         result = requestAddBox(boxInfo, m_defaultContext);
50         break;
51     case REQUEST_CMD_REMOVE_BOX:
52         result = requestRemoveBox(boxInfo->instanceId);
53         break;
54     case REQUEST_CMD_RESIZE_BOX:
55         result = requestResizeBox(boxInfo->instanceId, boxInfo->boxWidth, boxInfo->boxHeight);
56         break;
57     case REQUEST_CMD_RESUME_BOX:
58         result = requestResumeBox(boxInfo->instanceId);
59         break;
60     case REQUEST_CMD_PAUSE_BOX:
61         result = requestPauseBox(boxInfo->instanceId);
62         break;
63     case REQUEST_CMD_RESUME_ALL:
64         result = requestResumeAll();
65         break;
66     case REQUEST_CMD_PAUSE_ALL:
67         result = requestPauseAll();
68         break;
69     case REQUEST_CMD_OPEN_PD:
70         result = requestOpenPd(boxInfo->instanceId,
71                     boxInfo->pdWidth, boxInfo->pdHeight,
72                     boxInfo->pdX, boxInfo->pdY);
73         break;
74     case REQUEST_CMD_CLOSE_PD:
75         result = requestClosePd(boxInfo->instanceId);
76         break;
77     case REQUEST_CMD_CHANGE_PERIOD:
78         result = requestChangePeriod(boxInfo->instanceId, boxInfo->period);
79         break;
80     case REQUEST_CMD_UPDATE_BOX:
81         result = requestUpdateBox(boxInfo->boxId, boxInfo->contentInfo);
82         break;
83     default:
84         LogD("not available request type");
85         break;
86     }
87
88     return result;
89 }
90
91 bool BoxManager::requestAddBox(BoxInfoPtr boxInfo, EwkContextPtr ewkContext)
92 {
93     IBoxPtr box;
94
95     // create new box
96     try {
97         if (!ewkContext) {
98             if (!m_defaultContext) {
99                 m_defaultContext = EwkContextPtr(ewk_context_new(), EwkContextDeleter());
100             }
101             ewkContext = m_defaultContext;
102         }
103         box = Box::create(boxInfo, m_boxFactory, ewkContext); 
104     } catch (...) {
105         LogD("exection occurs during adding box");
106         return false;
107     }
108
109     // show new box
110     if (!box->show()) {
111         LogD("problem occurs during rendering box");
112         return false;
113     }
114
115     insertBoxMap(boxInfo->instanceId, box); 
116     return true;
117 }
118
119 bool BoxManager::requestRemoveBox(std::string& instanceId)
120 {
121     IBoxPtr box = searchBoxMap(instanceId);
122     if (!box) {
123         return false;
124     }
125     
126     if (!box->hide()) { 
127         return false;
128     }
129
130     eraseBoxMap(instanceId);
131     return true;
132 }
133
134 bool BoxManager::requestResizeBox(std::string& instanceId, int width, int height)
135 {
136     LogD("enter");
137     IBoxPtr box = searchBoxMap(instanceId);
138     if (!box) {
139         return false;
140     }
141     
142     return box->resize(width, height);
143 }
144
145 bool BoxManager::requestResumeBox(std::string& instanceId)
146 {
147     LogD("enter");
148     IBoxPtr box = searchBoxMap(instanceId);
149     if (!box) {
150         return false;
151     }
152     
153     return box->resume();
154 }
155
156 bool BoxManager::requestPauseBox(std::string& instanceId)
157 {
158     LogD("enter");
159     IBoxPtr box = searchBoxMap(instanceId);
160     if (!box) {
161         return false;
162     }
163     
164     // paused by switching other page
165     return box->pause(false);
166 }
167
168 bool BoxManager::requestResumeAll()
169 {
170     LogD("enter");
171     for (auto it = m_boxMap.begin(); it != m_boxMap.end(); it++) {
172         if (it->second->isCurrentTab()) {
173             it->second->resume();
174         }
175     }
176
177     return true;
178 }
179
180 bool BoxManager::requestPauseAll()
181 {
182     LogD("enter");
183     for (auto it = m_boxMap.begin(); it != m_boxMap.end(); it++) {
184         if (it->second->isCurrentTab()) {
185             // paused by entering background
186             it->second->pause(true);
187         }
188     }
189
190     return true;
191 }
192
193 bool BoxManager::requestOpenPd(
194         std::string& instanceId,
195         int width, int height, double x, double y)
196 {
197     LogD("enter");
198     IBoxPtr box = searchBoxMap(instanceId);
199     if (!box) {
200         return false;
201     }
202     
203     return box->openPd(width, height, x, y);
204 }
205
206 bool BoxManager::requestClosePd(std::string& instanceId)
207 {
208     LogD("enter");
209     IBoxPtr box = searchBoxMap(instanceId);
210     if (!box) {
211         return false;
212     }
213
214     return box->closePd();
215 }
216
217 bool BoxManager::requestChangePeriod(std::string& instanceId, float period)
218 {
219     LogD("enter");
220     IBoxPtr box = searchBoxMap(instanceId);
221     if (!box) {
222         return false;
223     }
224
225     return box->changePeriod(period);
226 }
227
228 bool BoxManager::requestUpdateBox(std::string& boxId, std::string& contentInfo)
229 {
230     LogD("enter");
231
232     IBoxPtr box;
233     box.reset();
234
235     time_t requestTime = time(NULL);
236
237     for (auto it = m_boxMap.begin(); it != m_boxMap.end(); ++it) {
238         if (it->first.find(boxId) == std::string::npos) {
239             continue;
240         }
241         box = it->second;
242         box->update(requestTime, contentInfo);
243     }
244
245     return true;
246 }
247
248 void BoxManager::insertBoxMap(std::string& instanceId, IBoxPtr box)
249 {
250     if (!searchBoxMap(instanceId)) {
251         LogD("insert box to map: %s", instanceId.c_str());
252         m_boxMap.insert(BoxMapPair(instanceId, box));
253     } else {
254         LogD("this box was already inserted!");
255     }
256 }
257
258 void BoxManager::eraseBoxMap(std::string& instanceId)
259 {
260     LogD("erase box to map");
261     if (!searchBoxMap(instanceId)) {
262         LogD("not available box");
263         return;
264     }
265
266     m_boxMap.erase(instanceId);
267 }
268
269 void BoxManager::updateBoxMap(std::string& instanceId, IBoxPtr box)
270 {
271     if (searchBoxMap(instanceId)) {
272         eraseBoxMap(instanceId);
273     }
274
275     insertBoxMap(instanceId, box);
276 }
277
278 IBoxPtr BoxManager::searchBoxMap(std::string& instanceId)
279 {
280     LogD("enter");
281     IBoxPtr box;
282     box.reset();
283     auto it = m_boxMap.find(instanceId);
284     if (it != m_boxMap.end()) {
285         LogD("found box: %s (%p)", it->first.c_str(), it->second.get());
286         box = it->second;
287     }
288
289     return box;
290 }
291
292 void BoxManager::clearBoxMap()
293 {
294     m_boxMap.clear();
295 }
296
297 void BoxManager::EwkContextDeleter::operator()(Ewk_Context* ptr)
298 {
299     LogD("ewk context delete");
300     if (ptr) {
301         ewk_context_delete(ptr);
302     }
303 }
304