2 * //******************************************************************
4 * // Copyright 2015 Samsung Electronics All Rights Reserved.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
12 * // http://www.apache.org/licenses/LICENSE-2.0
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.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #include "JniProvisionResultListner.h"
23 #include "JniOcSecureResource.h"
24 #include "JniSecureUtils.h"
26 JniProvisionResultListner::JniProvisionResultListner(JNIEnv *env, jobject jListener,
27 RemoveCallback removeProvisionResultListener)
29 m_jwListener = env->NewWeakGlobalRef(jListener);
30 m_removeProvisionResultListener = removeProvisionResultListener;
33 JniProvisionResultListner::~JniProvisionResultListner()
35 LOGI("~JniProvisionResultListner()");
39 JNIEnv *env = GetJNIEnv(ret);
44 env->DeleteWeakGlobalRef(m_jwListener);
45 if (JNI_EDETACHED == ret)
47 g_jvm->DetachCurrentThread();
52 void JniProvisionResultListner::ProvisionResultCallback(PMResultList_t *result, int hasError,
56 JNIEnv *env = GetJNIEnv(ret);
62 jobject jListener = env->NewLocalRef(m_jwListener);
65 checkExAndRemoveListener(env);
66 if (JNI_EDETACHED == ret)
68 g_jvm->DetachCurrentThread();
73 jclass clsL = env->GetObjectClass(jListener);
77 checkExAndRemoveListener(env);
78 if (JNI_EDETACHED == ret)
80 g_jvm->DetachCurrentThread();
86 jobject jResultList = JniSecureUtils::convertProvisionresultVectorToJavaList(env, result);
89 checkExAndRemoveListener(env);
90 if (JNI_EDETACHED == ret)
92 g_jvm->DetachCurrentThread();
97 std::string calledFunc;
100 case ListenerFunc::OWNERSHIPTRANSFER:
102 calledFunc = "doOwnershipTransferListener";
105 case ListenerFunc::PROVISIONACL:
107 calledFunc = "provisionAclListener";
110 case ListenerFunc::PROVISIONCREDENTIALS:
112 calledFunc = "provisionCredentialsListener";
115 case ListenerFunc::PROVISIONTRUSTCERTCHAIN:
117 calledFunc = "provisionTrustCertChainListener";
120 case ListenerFunc::UNLINKDEVICES:
122 calledFunc = "unlinkDevicesListener";
125 case ListenerFunc::REMOVEDEVICE:
127 calledFunc = "removeDeviceListener";
130 case ListenerFunc::PROVISIONPAIRWISEDEVICES:
132 calledFunc = "provisionPairwiseDevicesListener";
135 case ListenerFunc::PROVISIONDIRECTPAIRING:
137 calledFunc = "provisionDirectPairingListener";
142 checkExAndRemoveListener(env);
143 if (JNI_EDETACHED == ret)
145 g_jvm->DetachCurrentThread();
152 jmethodID midL = env->GetMethodID(clsL, calledFunc.c_str(), "(Ljava/util/List;I)V");
155 checkExAndRemoveListener(env);
156 if (JNI_EDETACHED == ret)
158 g_jvm->DetachCurrentThread();
162 env->CallVoidMethod(jListener, midL, jResultList, (jint)hasError);
163 if (env->ExceptionCheck())
165 LOGE("Java exception is thrown");
168 checkExAndRemoveListener(env);
169 if (JNI_EDETACHED == ret)
171 g_jvm->DetachCurrentThread();
175 void JniProvisionResultListner::checkExAndRemoveListener(JNIEnv* env)
177 if (env->ExceptionCheck())
179 jthrowable ex = env->ExceptionOccurred();
180 env->ExceptionClear();
181 m_removeProvisionResultListener(env, m_jwListener);
182 env->Throw((jthrowable)ex);
186 m_removeProvisionResultListener(env, m_jwListener);