replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / android / EasySetupCore / src / main / jni / JniEsListenerManager.h
1 /******************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 /** @file   JniEsLisenerManager.h
22  *
23  *   @brief  This file contains JNI Listener Manager class for JniRemoteEnrollee
24  */
25
26 #ifndef __JNI_ES_LISTENER_MANAGER_H_
27 #define __JNI_ES_LISTENER_MANAGER_H_
28
29 #include <mutex>
30 #include <map>
31
32 class JniRemoteEnrollee;
33
34 /**
35  * @class   JniEsListenerManager
36  * @brief   This class provides functions for managing listeners
37  *
38  */
39 template <class T>
40 class JniEsListenerManager
41 {
42     public:
43         /**
44          * API for Adding the Listener to listener Map.
45          *
46          * @param owner - JniRemoteEnrollee
47          */
48         T *addListener(JNIEnv *env, jobject jListener, JniRemoteEnrollee *owner)
49         {
50             T *onEventListener = NULL;
51
52             m_mapMutex.lock();
53
54             for (auto it = m_listenerMap.begin(); it != m_listenerMap.end(); ++it)
55             {
56                 if (env->IsSameObject(jListener, it->first))
57                 {
58                     auto refPair = it->second;
59                     onEventListener = refPair.first;
60                     refPair.second++;
61                     it->second = refPair;
62                     m_listenerMap.insert(*it);
63                     ES_LOGD("OnEventListener: ref. count is incremented");
64                     break;
65                 }
66             }
67             if (!onEventListener)
68             {
69                 onEventListener = new T(env, jListener, owner);
70                 jobject jgListener = env->NewGlobalRef(jListener);
71
72                 if (jgListener)
73                 {
74                     m_listenerMap.insert(
75                         std::pair < jobject,
76                         std::pair<T *, int >> (jgListener, std::pair<T *, int>(onEventListener, 1)));
77                     ES_LOGD("OnEventListener: new listener");
78                 }
79                 else
80                 {
81                     ES_LOGD("OnEventListener: Failed to create global listener ref.");
82                     delete onEventListener;
83                     onEventListener = NULL;
84                 }
85             }
86             m_mapMutex.unlock();
87             return onEventListener;
88         }
89
90         /**
91          * @brief API for removing the Listener from listener Map.
92          */
93         void removeListener(JNIEnv *env, jobject jListener)
94         {
95             m_mapMutex.lock();
96             for (auto it = m_listenerMap.begin(); it != m_listenerMap.end(); ++it)
97             {
98                 if (env->IsSameObject(jListener, it->first))
99                 {
100                     auto refPair = it->second;
101                     if (refPair.second > 1)
102                     {
103                         refPair.second--;
104                         it->second = refPair;
105                         m_listenerMap.insert(*it);
106                         ES_LOGI("OnEventListener: ref. count is decremented");
107                     }
108                     else
109                     {
110                         env->DeleteGlobalRef(it->first);
111                         T *listener = refPair.first;
112                         delete listener;
113                         m_listenerMap.erase(it);
114
115                         ES_LOGI("OnEventListener is removed");
116                     }
117                     break;
118                 }
119             }
120             m_mapMutex.unlock();
121         }
122
123         /**
124          * @brief API for removing all the Listener from listener Map.
125          */
126         void removeAllListeners(JNIEnv *env)
127         {
128             m_mapMutex.lock();
129
130             for (auto &pair : m_listenerMap)
131             {
132                 env->DeleteGlobalRef(pair.first);
133                 auto refPair = pair.second;
134                 delete refPair.first;
135             }
136             m_listenerMap.clear();
137
138             m_mapMutex.unlock();
139         }
140
141     private:
142         std::map<jobject, std::pair<T *, int>> m_listenerMap;
143         std::mutex m_mapMutex;
144 };
145
146 #endif //__JNI_ES_LISTENER_MANAGER_H_