c8bc48e7a254adb18ec6065e3c4eaab93aa77794
[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 extern const std::string _listen_all;
30
31 class listener_interface {
32 public:
33     listener_interface(std::string uid, std::set<std::string> listen_appid = std::set<std::string>()) : m_uid(uid), m_listen_appid(listen_appid) {}
34     virtual ~listener_interface() {}
35     virtual void notify(afb_api_t api, std::string appid = "") = 0;
36     std::string myUid(void) {return m_uid;}
37     std::set<std::string> listenAppSet(void) {return m_listen_appid;}
38     void addListenAppId(std::string appid) {m_listen_appid.insert(appid);}
39     bool isListenAppId(std::string &appid) {
40         auto it = m_listen_appid.find(appid);
41         return it != m_listen_appid.end() ? true : false;
42     }
43     bool listenAppEmpty(void) {return m_listen_appid.empty();}
44     void clearListenAppSet(void) {m_listen_appid.clear();}
45 private:
46     std::string m_uid;
47     std::set<std::string> m_listen_appid;
48 };
49
50 typedef struct HS_ClientCtxt
51 {
52     std::string id;
53     HS_ClientCtxt(const char *appid)
54     {
55         id = appid;
56     }
57 } HS_ClientCtxt;
58
59
60 class HS_ClientManager {
61 public:
62     HS_ClientManager();
63     ~HS_ClientManager() = default;
64     HS_ClientManager(HS_ClientManager const &) = delete;
65     HS_ClientManager &operator=(HS_ClientManager const &) = delete;
66     HS_ClientManager(HS_ClientManager &&) = delete;
67     HS_ClientManager &operator=(HS_ClientManager &&) = delete;
68
69     static HS_ClientManager* instance(void);
70     int init(void);
71     int handleRequest(afb_req_t request, const char *verb, const char *appid = nullptr);
72     int pushEvent(const char *event, struct json_object *param, std::string appid = "");
73     void removeClientCtxt(void *data);  // don't use, internal only
74     void setStartupAppid(const std::string &appid) {startup_appid = appid;}
75     bool isAppStarted(const std::string &appid);
76     void addListener(listener_interface* listener);
77     void removeListener(listener_interface* listener);
78
79 private:
80     HS_ClientCtxt* createClientCtxt(afb_req_t req, std::string appid);
81     HS_Client* addClient(afb_req_t req, std::string appid);
82     void removeClient(std::string appid);
83     bool checkRegisterApp(afb_api_t api, const std::string &appid);
84     void notifyListener(afb_api_t api, const std::string &appid);
85
86 private:
87     static HS_ClientManager* me;
88     std::unordered_map<std::string, std::list<listener_interface*>> listener_list;
89     std::unordered_map<std::string, HS_Client*> client_list;
90     std::unordered_map<std::string, HS_ClientCtxt*> appid2ctxt;
91     std::mutex mtx;
92     std::string startup_appid;
93 };
94
95 #endif // HOMESCREEN_CLIENTMANAGER_H