fix segmentation fault error
[apps/agl-service-homescreen.git] / src / hs-clientmanager.h
1 /*
2  * Copyright (c) 2018 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 #ifndef HOMESCREEN_CLIENTMANAGER_H
18 #define HOMESCREEN_CLIENTMANAGER_H
19
20 #include <string>
21 #include <mutex>
22 #include <memory>
23 #include <unordered_map>
24 #include <list>
25 #include <set>
26 #include "hs-helper.h"
27 #include "hs-client.h"
28
29
30 class listener_interface {
31 public:
32     listener_interface(std::string uid, std::set<std::string> listen_appid = std::set<std::string>()) : m_uid(uid), m_listen_appid(listen_appid) {}
33     virtual ~listener_interface() {}
34     virtual void notify(afb_api_t api, std::string appid = "") = 0;
35     std::string myUid(void) {return m_uid;}
36     void addListenAppId(const std::string &appid) {m_listen_appid.insert(appid);}
37     void removeListenAppId(const std::string &appid) {m_listen_appid.erase(appid);}
38     bool isListenAppId(const std::string &appid) {
39         auto it = m_listen_appid.find(appid);
40         return it != m_listen_appid.end() ? true : false;
41     }
42     bool listenAppEmpty(void) {return m_listen_appid.empty();}
43     void clearListenAppSet(void) {m_listen_appid.clear();}
44 private:
45     std::string m_uid;
46     std::set<std::string> m_listen_appid;
47 };
48
49 typedef struct HS_ClientCtxt
50 {
51     std::string id;
52     HS_ClientCtxt(const char *appid)
53     {
54         id = appid;
55     }
56 } HS_ClientCtxt;
57
58
59 class HS_ClientManager {
60 public:
61     HS_ClientManager();
62     ~HS_ClientManager() = default;
63     HS_ClientManager(HS_ClientManager const &) = delete;
64     HS_ClientManager &operator=(HS_ClientManager const &) = delete;
65     HS_ClientManager(HS_ClientManager &&) = delete;
66     HS_ClientManager &operator=(HS_ClientManager &&) = delete;
67
68     static HS_ClientManager* instance(void);
69     int init(void);
70     int handleRequest(afb_req_t request, const char *verb, const char *appid = nullptr);
71     int pushEvent(const char *event, struct json_object *param, std::string appid = "");
72     void removeClientCtxt(void *data);  // don't use, internal only
73     void setStartupAppidAndArea(const std::pair<std::string, std::string> pair) {startup_appid = pair.first;startup_area = pair.second;}
74     bool isAppStarted(const std::string &appid);
75     void addListener(listener_interface* listener);
76     void removeListener(listener_interface* listener);
77
78 private:
79     HS_ClientCtxt* createClientCtxt(afb_req_t req, std::string appid);
80     HS_Client* addClient(afb_req_t req, std::string appid);
81     void removeClient(std::string appid);
82     void notifyListener(afb_api_t api, const std::string &appid);
83
84 private:
85     static HS_ClientManager* me;
86     std::unordered_map<std::string, listener_interface*> listener_list;
87     std::unordered_map<std::string, HS_Client*> client_list;
88     std::unordered_map<std::string, HS_ClientCtxt*> appid2ctxt;
89     std::mutex mtx;
90     std::string startup_appid, startup_area;
91 };
92
93 #endif // HOMESCREEN_CLIENTMANAGER_H