Merge tizen_5.0 codes into tizen_4.0
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOnDeleteListener.cpp
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 "JniOnDeleteListener.h"
24 #include "JniOcResource.h"
25 #include "JniUtils.h"
26
27 JniOnDeleteListener::JniOnDeleteListener(JNIEnv *env, jobject jListener, RemoveListenerCallback removeListener)
28     : m_removeListener(removeListener)
29 {
30     m_jwListener = env->NewWeakGlobalRef(jListener);
31 }
32
33 JniOnDeleteListener::~JniOnDeleteListener()
34 {
35     if (m_jwListener)
36     {
37         jint ret = JNI_ERR;
38         JNIEnv *env = GetJNIEnv(ret);
39         if (nullptr == env)
40         {
41             return;
42         }
43         env->DeleteWeakGlobalRef(m_jwListener);
44         m_jwListener = nullptr;
45
46         if (JNI_EDETACHED == ret)
47         {
48             g_jvm->DetachCurrentThread();
49         }
50     }
51 }
52
53 void JniOnDeleteListener::onDeleteCallback(const HeaderOptions& headerOptions, const int eCode)
54 {
55     jint envRet = JNI_ERR;
56     JNIEnv *env = GetJNIEnv(envRet);
57     if (nullptr == env)
58     {
59         return;
60     }
61
62     jobject jListener = env->NewLocalRef(m_jwListener);
63     if (!jListener)
64     {
65         checkExAndRemoveListener(env);
66         if (JNI_EDETACHED == envRet)
67         {
68             g_jvm->DetachCurrentThread();
69         }
70         return;
71     }
72     jclass clsL = env->GetObjectClass(jListener);
73     if (!clsL)
74     {
75         checkExAndRemoveListener(env);
76         if (JNI_EDETACHED == envRet)
77         {
78             g_jvm->DetachCurrentThread();
79         }
80         return;
81     }
82
83     if (OC_STACK_RESOURCE_DELETED != eCode)
84     {
85         jobject ex = GetOcException(eCode, "stack error in onDeleteCallback");
86         if (!ex)
87         {
88             checkExAndRemoveListener(env);
89             if (JNI_EDETACHED == envRet)
90             {
91                 g_jvm->DetachCurrentThread();
92             }
93             return;
94         }
95         jmethodID midL = env->GetMethodID(clsL, "onDeleteFailed", "(Ljava/lang/Throwable;)V");
96         if (!midL)
97         {
98             checkExAndRemoveListener(env);
99             if (JNI_EDETACHED == envRet)
100             {
101                 g_jvm->DetachCurrentThread();
102             }
103             return;
104         }
105         env->CallVoidMethod(jListener, midL, ex);
106     }
107     else
108     {
109         jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
110         if (!jHeaderOptionList)
111         {
112             checkExAndRemoveListener(env);
113             if (JNI_EDETACHED == envRet)
114             {
115                 g_jvm->DetachCurrentThread();
116             }
117             return;
118         }
119
120         jmethodID midL = env->GetMethodID(clsL, "onDeleteCompleted", "(Ljava/util/List;)V");
121         if (!midL)
122         {
123             checkExAndRemoveListener(env);
124             if (JNI_EDETACHED == envRet)
125             {
126                 g_jvm->DetachCurrentThread();
127             }
128             return;
129         }
130         env->CallVoidMethod(jListener, midL, jHeaderOptionList);
131     }
132
133     checkExAndRemoveListener(env);
134     if (JNI_EDETACHED == envRet)
135     {
136         g_jvm->DetachCurrentThread();
137     }
138 }
139
140 void JniOnDeleteListener::checkExAndRemoveListener(JNIEnv* env)
141 {
142     if (env->ExceptionCheck())
143     {
144         jthrowable ex = env->ExceptionOccurred();
145         env->ExceptionClear();
146         m_removeListener(env, m_jwListener);
147         env->Throw((jthrowable)ex);
148     }
149     else
150     {
151         m_removeListener(env, m_jwListener);
152     }
153 }