2 * //******************************************************************
4 * // Copyright 2015 Intel Corporation.
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 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 #include "JniOnDeleteListener.h"
24 #include "JniOcResource.h"
27 JniOnDeleteListener::JniOnDeleteListener(JNIEnv *env, jobject jListener, JniOcResource* owner)
28 : m_ownerResource(owner)
30 m_jwListener = env->NewWeakGlobalRef(jListener);
33 JniOnDeleteListener::~JniOnDeleteListener()
38 JNIEnv *env = GetJNIEnv(ret);
39 if (NULL == env) return;
41 env->DeleteWeakGlobalRef(m_jwListener);
42 m_jwListener = nullptr;
44 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
48 void JniOnDeleteListener::onDeleteCallback(const HeaderOptions& headerOptions, const int eCode)
51 JNIEnv *env = GetJNIEnv(envRet);
52 if (NULL == env) return;
54 jobject jListener = env->NewLocalRef(m_jwListener);
57 checkExAndRemoveListener(env);
58 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
61 jclass clsL = env->GetObjectClass(jListener);
64 checkExAndRemoveListener(env);
65 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
69 if (OC_STACK_RESOURCE_DELETED != eCode)
71 jobject ex = GetOcException(eCode, "stack error in onDeleteCallback");
74 checkExAndRemoveListener(env);
75 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
78 jmethodID midL = env->GetMethodID(clsL, "onDeleteFailed", "(Ljava/lang/Throwable;)V");
81 checkExAndRemoveListener(env);
82 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
85 env->CallVoidMethod(jListener, midL, ex);
89 jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
90 if (!jHeaderOptionList)
92 checkExAndRemoveListener(env);
93 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
97 jmethodID midL = env->GetMethodID(clsL, "onDeleteCompleted", "(Ljava/util/List;)V");
100 checkExAndRemoveListener(env);
101 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
104 env->CallVoidMethod(jListener, midL, jHeaderOptionList);
107 checkExAndRemoveListener(env);
108 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
111 void JniOnDeleteListener::checkExAndRemoveListener(JNIEnv* env)
113 if (env->ExceptionCheck())
115 jthrowable ex = env->ExceptionOccurred();
116 env->ExceptionClear();
117 m_ownerResource->removeOnDeleteListener(env, m_jwListener);
118 env->Throw((jthrowable)ex);
122 m_ownerResource->removeOnDeleteListener(env, m_jwListener);