replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / android / EasySetupCore / src / main / jni / JniSecurityStatusListener.cpp
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 #include "JniSecurityStatusListener.h"
22 #include "JniRemoteEnrollee.h"
23 #include "oic_malloc.h"
24 #include "oic_string.h"
25
26 using namespace OIC::Service;
27
28 JniSecurityStatusListener::JniSecurityStatusListener(JNIEnv *env, jobject jListener,
29         JniRemoteEnrollee *owner)
30     : m_ownerResource(owner)
31 {
32     m_jwListener = env->NewWeakGlobalRef(jListener);
33 }
34
35 JniSecurityStatusListener::~JniSecurityStatusListener()
36 {
37     ES_LOGI("~JniSecurityStatusListener()");
38     if (m_jwListener)
39     {
40         jint ret;
41         JNIEnv *env = GetESJNIEnv(ret);
42         if (NULL == env) return;
43         env->DeleteWeakGlobalRef(m_jwListener);
44         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
45     }
46 }
47 void JniSecurityStatusListener::secProvisionStatusCallback(
48                 std::shared_ptr<SecProvisioningStatus> secProvisioningStatus)
49 {
50
51     ES_LOGI("JniSecurityStatusListener::secProvisionStatusCallback enter");
52
53     jint ret;
54     JNIEnv *env = GetESJNIEnv(ret);
55     if (NULL == env) return;
56     jobject jListener = env->NewLocalRef(m_jwListener);
57     if (!jListener)
58     {
59         checkExAndRemoveListener(env);
60         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
61         return;
62     }
63
64     jclass clsL = env->GetObjectClass(jListener);
65     if (!clsL)
66     {
67         checkExAndRemoveListener(env);
68         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
69         return;
70     }
71
72     jmethodID midL = env->GetMethodID(clsL, "onProgress",
73                                       "(Lorg/iotivity/service/easysetup/mediator/"
74                                       "SecurityProvisioningStatus;"
75                                       ")V");
76
77     if (!midL)
78     {
79         checkExAndRemoveListener(env);
80         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
81         return;
82     }
83
84     ESResult esResult = secProvisioningStatus->getESResult();
85
86     //create the java object
87     jobject jSecurityProvisioningStatus = NULL;
88     jSecurityProvisioningStatus = env->NewObject(g_cls_SecurityProvisioningStatus,
89                                         g_mid_SecurityProvisioningStatus_ctor,
90                                         (jint)esResult,
91                                         env->NewStringUTF(secProvisioningStatus->getDeviceUUID().c_str()));
92
93     ES_LOGE("JniSecurityStatusListener::onSecurityProvisioningStatus UUID : %s",
94                                                      secProvisioningStatus->getDeviceUUID().c_str());
95
96     if (!jSecurityProvisioningStatus)
97     {
98         ES_LOGE("JniSecurityStatusListener::onSecurityProvisioningStatus Unable to create the java object");
99         return ;
100     }
101
102     env->CallVoidMethod(jListener, midL, jSecurityProvisioningStatus);
103
104     if (env->ExceptionCheck())
105     {
106         ES_LOGE("Java exception is thrown");
107         checkExAndRemoveListener(env);
108         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
109         return;
110     }
111
112     if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
113 }
114
115 void JniSecurityStatusListener::checkExAndRemoveListener(JNIEnv *env)
116 {
117     if (env->ExceptionCheck())
118     {
119         jthrowable ex = env->ExceptionOccurred();
120         env->ExceptionClear();
121         m_ownerResource->removeStatusListener<JniSecurityStatusListener>(env, m_jwListener);
122         env->Throw((jthrowable)ex);
123     }
124     else
125     {
126         m_ownerResource->removeStatusListener<JniSecurityStatusListener>(env, m_jwListener);
127     }
128 }