Remove memory leak
[platform/core/context/context-service.git] / src / trigger / ContextMonitor.h
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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 _CONTEXT_TRIGGER_CONTEXT_MONITOR_H_
18 #define _CONTEXT_TRIGGER_CONTEXT_MONITOR_H_
19
20 #include <list>
21 #include <map>
22 #include <Json.h>
23 #include <DBusTypes.h>
24
25 namespace ctx {
26
27         class ContextManager;
28
29 namespace trigger {
30
31         class IContextListener;
32         class FactRequest;
33
34         class ContextMonitor {
35         public:
36                 static ContextMonitor* getInstance();
37                 static void setContextManager(ContextManager* ctxMgr);
38                 static void destroy();
39
40                 int subscribe(int ruleId, std::string subject, Json option, IContextListener* listener);
41                 int unsubscribe(int ruleId, std::string subject, Json option, IContextListener* listener);
42                 int read(std::string subject, Json option, IContextListener* listener);
43                 bool isSupported(std::string subject);
44                 bool isAllowed(const char *client, const char *subject);
45
46                 void replyResult(int reqId, int error, Json *requestResult = NULL);
47                 void replyResult(int reqId, int error, const char *subject, Json *option, Json *fact);
48                 void publishFact(int reqId, int error, const char *subject, Json *option, Json *fact);
49
50         private:
51                 ContextMonitor();
52                 ContextMonitor(const ContextMonitor& other);
53                 ~ContextMonitor();
54
55                 static ContextMonitor *__instance;
56                 static ContextManager *__contextMgr;
57
58                 int __subscribe(const char* subject, Json* option, IContextListener* listener);
59                 void __unsubscribe(const char *subject, int subscriptionId);
60                 int __read(const char *subject, Json *option, IContextListener* listener);
61
62                 struct SubscrInfo {
63                         int sid;
64                         std::string subject;
65                         Json option;
66                         std::list<IContextListener*> listenerList;
67
68                         SubscrInfo(int id, const char *subj, Json *opt) :
69                                 sid(id),
70                                 subject(subj)
71                         {
72                                 if (opt)
73                                         option = *opt;
74                         }
75                 };
76
77                 std::map<int, SubscrInfo*> __subscrMap;
78                 std::map<int, SubscrInfo*> __readMap;
79
80                 int __findSub(RequestType type, const char *subject, Json *option);
81                 bool __addSub(RequestType type, int sid, const char *subject, Json *option, IContextListener* listener);
82                 void __removeSub(RequestType type, const char *subject, Json *option);
83                 void __removeSub(RequestType type, int sid);
84                 int __addListener(RequestType type, int sid, IContextListener* listener);
85                 int __removeListener(RequestType type, int sid, IContextListener* listener);
86
87         };      /* class ContextMonitor */
88
89 }       /* namespace trigger */
90 }       /* namespace ctx */
91
92 #endif  /* End of _CONTEXT_TRIGGER_CONTEXT_MONITOR_H_ */