tizen 2.3.1 release
[framework/web/mobile/wrt.git] / src / wrt-launchpad-daemon / src / web_app_core_data.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
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    web_app_core_data.cpp
18  * @author  Leerang Song (leerang.song@samsung.com)
19  */
20
21 #include "web_app_core_data.h"
22
23 #include <string>
24 #include <unistd.h>
25
26 #include <dpl/assert.h>
27 #include <dpl/availability.h>
28 #include <dpl/log/wrt_log.h>
29 #include <Ecore.h>
30
31 namespace {
32 } // anonymous namespace
33
34 WebAppCoreDataManager::WebAppCoreDataManager()
35 {
36 }
37
38 WebAppCoreDataManager::~WebAppCoreDataManager()
39 {
40 }
41
42 bool WebAppCoreDataManager::add(const std::string& appId)
43 {
44     if (appId.empty()) {
45         WrtLogE("appid is empty");
46         return false;
47     }
48
49     auto it = m_webAppCoreDataMap.find(appId);
50     if (it != m_webAppCoreDataMap.end()) {
51         WrtLogE("already exist");
52         return false;
53     }
54
55     WebAppCoreData* data = new WebAppCoreData();
56     data->status = WebAppCoreDataManager::Status::CREATED;
57     data->pid = -1;
58     m_webAppCoreDataMap.insert(WebAppCoreDataPair(appId, data));
59
60     return true;
61 }
62
63 bool WebAppCoreDataManager::remove(const std::string& appId)
64 {
65     if (appId.empty()) {
66         WrtLogE("appid is empty");
67         return false;
68     }
69
70     auto it = m_webAppCoreDataMap.find(appId);
71     if (it == m_webAppCoreDataMap.end()) {
72         WrtLogE("No data");
73         return false;
74     }
75
76     m_webAppCoreDataMap.erase(it);
77
78     return true;
79 }
80
81 void WebAppCoreDataManager::show(const std::string& appId)
82 {
83     setStatus(appId, WebAppCoreDataManager::Status::RUNNING);
84 }
85
86 void WebAppCoreDataManager::hide(const std::string& appId)
87 {
88     DPL_UNUSED_PARAM(appId);
89     // TODO sth
90 }
91
92 void WebAppCoreDataManager::resume(const std::string& appId)
93 {
94     setStatus(appId, WebAppCoreDataManager::Status::RUNNING);
95 }
96
97 void WebAppCoreDataManager::suspend(const std::string& appId)
98 {
99     setStatus(appId, WebAppCoreDataManager::Status::SUSPENDED);
100 }
101
102 bool WebAppCoreDataManager::isResumed(const std::string& appId)
103 {
104     return compareStatus(appId, WebAppCoreDataManager::Status::RUNNING);
105 }
106
107 bool WebAppCoreDataManager::isSuspended(const std::string& appId)
108 {
109     return compareStatus(appId, WebAppCoreDataManager::Status::SUSPENDED);
110 }
111
112 bool WebAppCoreDataManager::isAppIdFound(const std::string& appId)
113 {
114     if (appId.empty())
115         return false;
116     auto it = m_webAppCoreDataMap.find(appId);
117     if (it == m_webAppCoreDataMap.end()) {
118         WrtLogE("appid not found in map");
119         return false;
120     }
121     return true;
122 }
123
124 WebAppCoreDataManager::Status WebAppCoreDataManager::getStatus(const std::string& appId)
125 {
126     if (appId.empty()) {
127         WrtLogE("appid is empty");
128         Assert(false);
129     }
130
131     auto it = m_webAppCoreDataMap.find(appId);
132     if (it == m_webAppCoreDataMap.end()) {
133         WrtLogE("No data found for given pid");
134         Assert(false);
135     }
136
137     return it->second->status;
138 }
139
140 void WebAppCoreDataManager::setStatus(const std::string& appId, Status status)
141 {
142     if (appId.empty()) {
143         WrtLogE("appid is empty");
144         Assert(false);
145     }
146
147     auto it = m_webAppCoreDataMap.find(appId);
148     if (it == m_webAppCoreDataMap.end()) {
149         WrtLogE("No data");
150         Assert(false);
151     }
152
153     it->second->status = status;
154     return;
155 }
156
157 bool WebAppCoreDataManager::compareStatus(const std::string& appId, Status status)
158 {
159     return getStatus(appId) == status ? true : false;
160 }
161
162 void WebAppCoreDataManager::setPid(const std::string& appId, pid_t pid)
163 {
164     if (appId.empty()) {
165         WrtLogE("appid is empty");
166         Assert(false);
167     }
168
169     auto it = m_webAppCoreDataMap.find(appId);
170     if (it == m_webAppCoreDataMap.end()) {
171         WrtLogE("No data");
172         Assert(false);
173     }
174
175     it->second->pid = pid;
176 }
177
178 pid_t WebAppCoreDataManager::getPid(const std::string& appId)
179 {
180     if (appId.empty()) {
181         WrtLogE("appid is empty");
182         Assert(false);
183     }
184
185     auto it = m_webAppCoreDataMap.find(appId);
186     if (it == m_webAppCoreDataMap.end()) {
187         WrtLogE("No data");
188         Assert(false);
189     }
190
191     return it->second->pid;
192 }
193
194 void WebAppCoreDataManager::setFd(const std::string& appId, Ecore_Fd_Handler* fd)
195 {
196     if (appId.empty()) {
197         WrtLogE("appid is empty");
198         Assert(false);
199     }
200
201     auto it = m_webAppCoreDataMap.find(appId);
202     if (it == m_webAppCoreDataMap.end()) {
203         WrtLogE("No data");
204         Assert(false);
205     }
206
207     it->second->fd = fd;
208 }
209
210 Ecore_Fd_Handler* WebAppCoreDataManager::getFd(const std::string& appId)
211 {
212     if (appId.empty()) {
213         WrtLogE("appid is empty");
214         Assert(false);
215     }
216
217     auto it = m_webAppCoreDataMap.find(appId);
218     if (it == m_webAppCoreDataMap.end()) {
219         WrtLogE("No data");
220         Assert(false);
221     }
222
223     return it->second->fd;
224 }
225
226 std::string WebAppCoreDataManager::getAppIdFromFd(int fd)
227 {
228     if (fd < 0) {
229         WrtLogE("fail to get fd");
230         Assert(false);
231     }
232     std::string str="";
233
234     for (auto it = m_webAppCoreDataMap.begin(); it != m_webAppCoreDataMap.end(); ++it) {
235         if (ecore_main_fd_handler_fd_get(it->second->fd) == fd) {
236             str = it->first;
237             break;
238         }
239     }
240     return str;
241 }