Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / android / 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                     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                 }
78                 else
79                 {
80                     LOGD("OnEventListener: Failed to create global listener ref.");
81                     delete onEventListener;
82                 }
83                 LOGD("OnEventListener: new listener");
84             }
85             m_mapMutex.unlock();
86             return onEventListener;
87         }
88
89         /**
90          * @brief API for removing the Listener from listener Map.
91          */
92         void removeListener(JNIEnv *env, jobject jListener)
93         {
94             m_mapMutex.lock();
95             for (auto it = m_listenerMap.begin(); it != m_listenerMap.end(); ++it)
96             {
97                 if (env->IsSameObject(jListener, it->first))
98                 {
99                     auto refPair = it->second;
100                     if (refPair.second > 1)
101                     {
102                         refPair.second--;
103                         it->second = refPair;
104                         m_listenerMap.insert(*it);
105                         LOGI("OnEventListener: ref. count is decremented");
106                     }
107                     else
108                     {
109                         env->DeleteGlobalRef(it->first);
110                         T *listener = refPair.first;
111                         delete listener;
112                         m_listenerMap.erase(it);
113
114                         LOGI("OnEventListener is removed");
115                     }
116                     break;
117                 }
118             }
119             m_mapMutex.unlock();
120         }
121
122         /**
123          * @brief API for removing all the Listener from listener Map.
124          */
125         void removeAllListeners(JNIEnv *env)
126         {
127             m_mapMutex.lock();
128
129             for (auto &pair : m_listenerMap)
130             {
131                 env->DeleteGlobalRef(pair.first);
132                 auto refPair = pair.second;
133                 delete refPair.first;
134             }
135             m_listenerMap.clear();
136
137             m_mapMutex.unlock();
138         }
139
140     private:
141         std::map<jobject, std::pair<T *, int>> m_listenerMap;
142         std::mutex m_mapMutex;
143 };
144
145 #endif //__JNI_ES_LISTENER_MANAGER_H_