Andorid(JNI) Implementation for Cloud Provisioning.
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOcCloudResultListener.cpp
1 /*
2 * //******************************************************************
3 * //
4 * // Copyright 2016 Samsung Electronics All Rights Reserved.
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 "JniOcCloudResultListener.h"
23
24 JniOcCloudResultListener::JniOcCloudResultListener(JNIEnv *env, jobject jListener,
25     RemoveCallback removeCloudResultListener)
26 {
27     m_jwListener = env->NewWeakGlobalRef(jListener);
28     m_removeCloudResultListener = removeCloudResultListener;
29 }
30
31 JniOcCloudResultListener::~JniOcCloudResultListener()
32 {
33     LOGI("~JniOcCloudResultListener()");
34     if (m_jwListener)
35     {
36         jint ret = JNI_ERR;
37         JNIEnv *env = GetJNIEnv(ret);
38         if (nullptr == env)
39         {
40             return;
41         }
42         env->DeleteWeakGlobalRef(m_jwListener);
43         if (JNI_EDETACHED == ret)
44         {
45             g_jvm->DetachCurrentThread();
46         }
47     }
48 }
49
50 void JniOcCloudResultListener::CloudResultListenerCB(int result, void *data,
51       ListenerFunc func)
52 {
53     jint ret = JNI_ERR;
54     JNIEnv *env = GetJNIEnv(ret);
55     if (nullptr == env)
56     {
57         return;
58     }
59
60     jobject jListener = env->NewLocalRef(m_jwListener);
61     if (!jListener)
62     {
63         checkExAndRemoveListener(env);
64         if (JNI_EDETACHED == ret)
65         {
66             g_jvm->DetachCurrentThread();
67         }
68         return;
69     }
70
71     jclass clsL = env->GetObjectClass(jListener);
72
73     if (!clsL)
74     {
75         checkExAndRemoveListener(env);
76         if (JNI_EDETACHED == ret)
77         {
78             g_jvm->DetachCurrentThread();
79         }
80         return;
81     }
82
83     std::string calledFunc;
84     switch (func)
85     {
86         case ListenerFunc::REQUEST_CERTIFICATE:
87             {
88                 calledFunc = "requestCertificateListener";
89             }
90             break;
91         case ListenerFunc::GET_ACLINFO:
92             {
93                 calledFunc = "getIndividualAclInfoListener";
94             }
95             break;
96         case ListenerFunc::GET_CRL:
97             {
98                 calledFunc = "getCRLListener";
99             }
100             break;
101         case ListenerFunc::POST_CRL:
102             {
103                 calledFunc = "postCRLListener";
104             }
105             break;
106         default:
107             {
108                 checkExAndRemoveListener(env);
109                 if (JNI_EDETACHED == ret)
110                 {
111                     g_jvm->DetachCurrentThread();
112                 }
113                 return;
114             }
115             return;
116     }
117
118     jmethodID midL = env->GetMethodID(clsL, calledFunc.c_str(), "(Z)V");
119     if (!midL)
120     {
121         checkExAndRemoveListener(env);
122         if (JNI_EDETACHED == ret)
123         {
124             g_jvm->DetachCurrentThread();
125         }
126         return;
127     }
128
129     env->CallVoidMethod(jListener, midL, (jboolean)result);
130     if (env->ExceptionCheck())
131     {
132         LOGE("Java exception is thrown");
133     }
134
135     checkExAndRemoveListener(env);
136     if (JNI_EDETACHED == ret)
137     {
138         g_jvm->DetachCurrentThread();
139     }
140 }
141
142 void JniOcCloudResultListener::checkExAndRemoveListener(JNIEnv* env)
143 {
144     if (env->ExceptionCheck())
145     {
146         jthrowable ex = env->ExceptionOccurred();
147         env->ExceptionClear();
148         m_removeCloudResultListener(env, m_jwListener);
149         env->Throw((jthrowable)ex);
150     }
151     else
152     {
153         m_removeCloudResultListener(env, m_jwListener);
154     }
155 }