Merge branch 'security-summit' into 'master'
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniListenerManager.h
1 /*
2 * //******************************************************************
3 * //
4 * // Copyright 2015 Intel Corporation.
5 * //
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 * //
8 * // Licensed under the Apache License, Version 2.0 (the "License");
9 * // you may not use this file except in compliance with the License.
10 * // You may obtain a copy of the License at
11 * //
12 * //      http://www.apache.org/licenses/LICENSE-2.0
13 * //
14 * // Unless required by applicable law or agreed to in writing, software
15 * // distributed under the License is distributed on an "AS IS" BASIS,
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * // See the License for the specific language governing permissions and
18 * // limitations under the License.
19 * //
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 */
22
23 #include "JniOcStack.h"
24 #include <mutex>
25
26 #ifndef _JniListenerManager
27 #define _JniListenerManager
28
29 class JniOcResource;
30
31 template <class T>
32 class JniListenerManager
33 {
34 public:
35     T* addListener(JNIEnv* env, jobject jListener, JniOcResource* owner)
36     {
37         T *onEventListener = nullptr;
38
39         m_mapMutex.lock();
40
41         for (auto it = m_listenerMap.begin(); it != m_listenerMap.end(); ++it)
42         {
43             if (env->IsSameObject(jListener, it->first))
44             {
45                 auto refPair = it->second;
46                 onEventListener = refPair.first;
47                 refPair.second++;
48                 it->second = refPair;
49                 m_listenerMap.insert(*it);
50                 LOGD("OnEventListener: ref. count is incremented");
51                 break;
52             }
53         }
54         if (!onEventListener)
55         {
56             onEventListener = new T(env, jListener, owner);
57             jobject jgListener = env->NewGlobalRef(jListener);
58
59             if (jgListener)
60             {
61                 m_listenerMap.insert(
62                         std::pair<jobject,
63                         std::pair<T*, int>>(jgListener, std::pair<T*, int>(onEventListener, 1)));
64             }
65             else
66             {
67                 LOGD("OnEventListener: Failed to create global listener ref.");
68                 delete onEventListener;
69             }
70             LOGD("OnEventListener: new listener");
71         }
72         m_mapMutex.unlock();
73         return onEventListener;
74     }
75
76     void removeListener(JNIEnv* env, jobject jListener)
77     {
78         m_mapMutex.lock();
79         for (auto it = m_listenerMap.begin(); it != m_listenerMap.end(); ++it)
80         {
81             if (env->IsSameObject(jListener, it->first))
82             {
83                 auto refPair = it->second;
84                 if (refPair.second > 1)
85                 {
86                     refPair.second--;
87                     it->second = refPair;
88                     m_listenerMap.insert(*it);
89                     LOGI("OnEventListener: ref. count is decremented");
90                 }
91                 else
92                 {
93                     env->DeleteGlobalRef(it->first);
94                     T* listener = refPair.first;
95                     delete listener;
96                     m_listenerMap.erase(it);
97
98                     LOGI("OnEventListener is removed");
99                 }
100                 break;
101             }
102         }
103         m_mapMutex.unlock();
104     }
105
106     void removeAllListeners(JNIEnv* env)
107     {
108         m_mapMutex.lock();
109
110         for (auto& pair : m_listenerMap)
111         {
112             env->DeleteGlobalRef(pair.first);
113             auto refPair = pair.second;
114             delete refPair.first;
115         }
116         m_listenerMap.clear();
117
118         m_mapMutex.unlock();
119     }
120
121 private:
122     std::map<jobject, std::pair<T*, int>> m_listenerMap;
123     std::mutex m_mapMutex;
124 };
125
126 #endif