Merge remote-tracking branch 'origin/master' into notification-service
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOnPostListener.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 #include "JniOnPostListener.h"
23 #include "JniOcResource.h"
24 #include "JniOcRepresentation.h"
25 #include "JniUtils.h"
26 #ifdef WITH_CLOUD
27 #include "JniOcAccountManager.h"
28 #endif
29
30 JniOnPostListener::JniOnPostListener(JNIEnv *env, jobject jListener, JniOcResource* owner)
31     : m_ownerResource(owner)
32 {
33     m_jwListener = env->NewWeakGlobalRef(jListener);
34 }
35
36 #ifdef WITH_CLOUD
37 JniOnPostListener::JniOnPostListener(JNIEnv *env, jobject jListener, JniOcAccountManager* owner)
38     : m_ownerAccountManager(owner)
39 {
40     m_jwListener = env->NewWeakGlobalRef(jListener);
41 }
42 #endif
43
44 JniOnPostListener::~JniOnPostListener()
45 {
46     if (m_jwListener)
47     {
48         jint ret = JNI_ERR;
49         JNIEnv *env = GetJNIEnv(ret);
50         if (nullptr == env)
51         {
52             return;
53         }
54
55         env->DeleteWeakGlobalRef(m_jwListener);
56         m_jwListener = nullptr;
57
58         if (JNI_EDETACHED == ret)
59         {
60             g_jvm->DetachCurrentThread();
61         }
62     }
63 }
64
65 void JniOnPostListener::onPostCallback(const HeaderOptions& headerOptions,
66     const OCRepresentation& ocRepresentation, const int eCode)
67 {
68     jint envRet = JNI_ERR;
69     JNIEnv *env = GetJNIEnv(envRet);
70     if (nullptr == env)
71     {
72         return;
73     }
74
75     jobject jListener = env->NewLocalRef(m_jwListener);
76     if (!jListener)
77     {
78         checkExAndRemoveListener(env);
79         if (JNI_EDETACHED == envRet)
80         {
81             g_jvm->DetachCurrentThread();
82         }
83         return;
84     }
85     jclass clsL = env->GetObjectClass(jListener);
86     if (!clsL)
87     {
88         checkExAndRemoveListener(env);
89         if (JNI_EDETACHED == envRet)
90         {
91             g_jvm->DetachCurrentThread();
92         }
93         return;
94     }
95
96     if (OC_STACK_OK != eCode && OC_STACK_RESOURCE_CREATED != eCode &&
97             OC_STACK_RESOURCE_CHANGED != eCode)
98     {
99         jobject ex = GetOcException(eCode, "stack error in onPostCallback");
100         if (!ex)
101         {
102             checkExAndRemoveListener(env);
103             if (JNI_EDETACHED == envRet)
104             {
105                 g_jvm->DetachCurrentThread();
106             }
107             return;
108         }
109         jmethodID midL = env->GetMethodID(clsL, "onPostFailed", "(Ljava/lang/Throwable;)V");
110         if (!midL)
111         {
112             checkExAndRemoveListener(env);
113             if (JNI_EDETACHED == envRet)
114             {
115                 g_jvm->DetachCurrentThread();
116             }
117             return;
118         }
119         env->CallVoidMethod(jListener, midL, ex);
120     }
121     else
122     {
123         jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
124         if (!jHeaderOptionList)
125         {
126             checkExAndRemoveListener(env);
127             if (JNI_EDETACHED == envRet)
128             {
129                 g_jvm->DetachCurrentThread();
130             }
131             return;
132         }
133
134         OCRepresentation * rep = new OCRepresentation(ocRepresentation);
135         jlong handle = reinterpret_cast<jlong>(rep);
136         jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
137             handle, true);
138         if (!jRepresentation)
139         {
140             delete rep;
141             checkExAndRemoveListener(env);
142             if (JNI_EDETACHED == envRet)
143             {
144                 g_jvm->DetachCurrentThread();
145             }
146             return;
147         }
148
149         jmethodID midL = env->GetMethodID(clsL, "onPostCompleted",
150             "(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;)V");
151         if (!midL)
152         {
153             checkExAndRemoveListener(env);
154             if (JNI_EDETACHED == envRet)
155             {
156                 g_jvm->DetachCurrentThread();
157             }
158             return;
159         }
160
161         env->CallVoidMethod(jListener, midL, jHeaderOptionList, jRepresentation);
162         if (env->ExceptionCheck())
163         {
164             LOGE("Java exception is thrown");
165             delete rep;
166         }
167     }
168
169     checkExAndRemoveListener(env);
170     if (JNI_EDETACHED == envRet)
171     {
172         g_jvm->DetachCurrentThread();
173     }
174 }
175
176 void JniOnPostListener::checkExAndRemoveListener(JNIEnv* env)
177 {
178     if (env->ExceptionCheck())
179     {
180         jthrowable ex = env->ExceptionOccurred();
181         env->ExceptionClear();
182 #ifndef WITH_CLOUD
183         m_ownerResource->removeOnPostListener(env, m_jwListener);
184 #else
185         if (m_ownerResource)
186         {
187             m_ownerResource->removeOnPostListener(env, m_jwListener);
188         }
189         if (m_ownerAccountManager)
190         {
191             m_ownerAccountManager->removeOnPostListener(env, m_jwListener);
192         }
193 #endif
194         env->Throw((jthrowable)ex);
195     }
196     else
197     {
198 #ifndef WITH_CLOUD
199         m_ownerResource->removeOnPostListener(env, m_jwListener);
200 #else
201         if (m_ownerResource)
202         {
203             m_ownerResource->removeOnPostListener(env, m_jwListener);
204         }
205         if (m_ownerAccountManager)
206         {
207             m_ownerAccountManager->removeOnPostListener(env, m_jwListener);
208         }
209 #endif
210     }
211 }