fix segmentation fault error
[apps/agl-service-homescreen.git] / src / hs-apprecover.cpp
1 /*
2  * Copyright (c) 2019 TOYOTA MOTOR CORPORATION
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 #include "hs-apprecover.h"
18 #include "hs-appinfo.h"
19 #include "hs-proxy.h"
20 #include "hs-clientmanager.h"
21
22
23 HS_AppRecover* HS_AppRecover::me = nullptr;
24
25 /**
26  * screen_update event handler
27  *
28  * #### Parameters
29  * - api : the api
30  * - event : received event name
31  * - object : received json object
32  *
33  * #### Return
34  * 0 : event can transfer to others
35  * 1 : event not transfer to others
36  */
37 int on_screen_update_event(afb_api_t api, const char *event, struct json_object *object)
38 {
39     HS_AppRecover::instance()->screenUpdated(object);
40     return 0;
41 }
42
43 /**
44  * get instance
45  *
46  * #### Parameters
47  *  - Nothing
48  *
49  * #### Return
50  * HS_AppRecover instance pointer
51  *
52  */
53 HS_AppRecover* HS_AppRecover::instance(void)
54 {
55     if(me == nullptr)
56         me = new HS_AppRecover();
57
58     return me;
59 }
60
61 /**
62  * HS_AppRecover initialize function
63  *
64  * #### Parameters
65  *  - api : the api serving the request
66  *
67  * #### Return
68  * 0 : init success
69  * 1 : init fail
70  *
71  */
72 int HS_AppRecover::init(afb_api_t api)
73 {
74     setEventHook("windowmanager/screenUpdated", on_screen_update_event);
75     HS_WmProxy wm_proxy;
76     wm_proxy.subscribe(api, HS_WmProxy::Event_ScreenUpdated);
77     return 0;
78 }
79
80 /**
81  * starting recover applications
82  *
83  * #### Parameters
84  *  - api : the api
85  *  - map : recover app map
86  *
87  * #### Return
88  * None
89  *
90  */
91 void HS_AppRecover::startRecovery(afb_api_t api)
92 {
93     HS_ClientManager::instance()->addListener(this);
94
95     for(auto &key : HS_Config::keys_recover_type) {
96         for(auto &m : recover_app_map[key]){
97             struct app_recover_info recover_info = {
98                 .recover_type = key,
99                 .visibility = m.visibility,
100                 .after = m.after
101             };
102             m_recover_apps_list[m.appid] = std::move(recover_info);
103
104             // recover application
105             auto it = m_recovering_set.find(m.appid);
106             if(it == m_recovering_set.end()) {
107                 m_recovering_set.insert(m.appid);
108                 std::string &after = m_recover_apps_list[m.appid].after;
109                 if(!after.empty()) {
110                     auto w = m_wait_recover_set.find(m_recover_apps_list[m.appid].after);
111                     if(w != m_wait_recover_set.end()) {
112                         m_wait_recover_set[after].insert(m.appid);
113                     }
114                     else {
115                         std::set<std::string> new_set;
116                         new_set.insert(m.appid);
117                         m_wait_recover_set[after] = std::move(new_set);
118                     }
119                     continue;   // don't immediately start application, wait until after applicaiton started.
120                 }
121                 startApplication(api, m.appid);
122             }
123         }
124     }
125     recover_app_map.clear();
126 }
127
128 /**
129  * notify
130  *
131  * #### Parameters
132  *  - api : the api
133  *  - appid : application id
134  *
135  * #### Return
136  * None
137  *
138  */
139 void HS_AppRecover::notify(afb_api_t api, std::string appid)
140 {
141     AFB_INFO("recover appid=[%s].", appid.c_str());
142     auto it = m_recovering_set.find(appid);
143     if(it != m_recovering_set.end()) {
144         this->removeListenAppId(appid);
145         m_recovering_set.erase(appid);
146         auto ip = m_recover_apps_list.find(appid);
147         if(ip != m_recover_apps_list.end()
148         && ip->second.visibility) {
149             HS_ClientManager::instance()->pushEvent("showWindow", nullptr, appid);
150         }
151     }
152
153     // check wait recover application
154     auto w = m_wait_recover_set.find(appid);
155     if(w != m_wait_recover_set.end()) {
156         for(auto &ref : m_wait_recover_set[appid]) {
157             startApplication(api, ref);
158         }
159         m_wait_recover_set.erase(appid);
160     }
161
162     if(m_recovering_set.empty()) {
163         HS_ClientManager::instance()->removeListener(this);
164     }
165 }
166
167 /**
168  * screenUpdated event handler
169  *
170  * #### Parameters
171  *  - obj : screenUpdate object
172  *
173  * #### Return
174  * None
175  * 
176  */
177 void HS_AppRecover::screenUpdated(struct json_object *obj)
178 {
179     if(m_lastmode_list.empty()) {
180         AFB_NOTICE("init lastmode is null, so don't record lastmode.");
181         return;
182     }
183
184     std::set<std::string> s_mode;
185     struct json_object *ids_obj;
186     if(json_object_object_get_ex(obj, key_ids.c_str(), &ids_obj)) {
187         if(json_object_get_type(ids_obj) == json_type_array) {
188             int array_len = json_object_array_length(ids_obj);
189             for (int i = 0; i < array_len; ++i) {
190                 struct json_object *j_id = json_object_array_get_idx(ids_obj, i);
191                 std::string appid = json_object_get_string(j_id);
192                 if(!isHomescreenApp(appid)) {
193                     s_mode.insert(appid);
194                 }
195             }
196             if(!s_mode.empty()) {
197                 updateLastmode(s_mode);
198             }
199         }
200         else {
201             AFB_WARNING("screenUpdated list isn't array.");
202         }
203     }
204 }
205
206 /**
207  * start application
208  *
209  * #### Parameters
210  *  - api : the api
211  *  - appid : application id liked "dashboard"
212  *
213  * #### Return
214  * None
215  *
216  */
217 void HS_AppRecover::startApplication(afb_api_t api, const std::string &appid)
218 {
219     this->addListenAppId(appid);
220     HS_AfmMainProxy afm_proxy;
221     afm_proxy.start(api,  HS_AppInfo::instance()->getAppProperty(appid, _keyId));
222 }
223
224 /**
225  * update lastmode file
226  *
227  * #### Parameters
228  *  - set : new lastmode set
229  *
230  * #### Return
231  * None
232  * 
233  */
234 void HS_AppRecover::updateLastmode(std::set<std::string> &set)
235 {
236     if(set.size() == m_lastmode_list.size()) {
237         bool is_same = true;
238         for(auto &m : set) {
239             auto it = m_lastmode_list.find(m);
240             if(it == m_lastmode_list.end()) {
241                 is_same = false;
242                 break;
243             }
244         }
245         if(is_same) {   // lastmode aren't changed
246             return;
247         }
248     }
249
250     m_lastmode_list.swap(set);
251     struct json_object *arr_obj = json_object_new_array();
252     for(auto &it : m_lastmode_list) {
253         struct json_object *j_obj = json_object_new_object();
254         json_object_object_add(j_obj, HS_Config::key_appid.c_str(), json_object_new_string(it.c_str()));
255         json_object_object_add(j_obj, HS_Config::key_visibility.c_str(), json_object_new_string("visible"));
256         json_object_array_add(arr_obj, j_obj);
257     }
258
259     auto path = HS_Config::root_dir + "/etc/" + HS_Config::lastmode_json;
260     if(writeJsonFile(path.c_str(), arr_obj) < 0) {
261         AFB_ERROR("write lastmode error.");
262     }
263 }
264
265 /**
266  * judge whether app is Homescreen app
267  *
268  * #### Parameters
269  *  - appid : application id
270  *
271  * #### Return
272  * true : homescreen app
273  * false : not homescreen app
274  *
275  */
276 bool HS_AppRecover::isHomescreenApp(const std::string &appid) const
277 {
278     auto it = m_recover_apps_list.find(appid);
279     if(it != m_recover_apps_list.end()
280     && it->second.recover_type == HS_Config::keys_recover_type[0]) {
281             return true;
282     }
283     return false;
284 }