void CXXContext::deregisterProxyComponent(long contextAddress) { LOGV("deregisterProxyComponent contextAddress %ld", contextAddress); JNIContext *jniContext = JNIContext::sharedInstance(); pthread_mutex_lock(&proxyComponentMapMutex); proxyComponentMap[contextAddress] = 0; std::map<long,jobject>::const_iterator iter; for (iter = proxyComponentMap.begin(); iter != proxyComponentMap.end(); iter++) { if ((*iter).first == contextAddress) { jobject externalObject = (jobject) (*iter).second; LOGV("deregisterProxyComponent erasing contextAddress %ld", contextAddress); proxyComponentMap.erase(contextAddress); proxyComponentRefCountMap[externalObject]--; if (proxyComponentRefCountMap[externalObject] <= 0) { proxyComponentRefCountMap.erase(externalObject); jniContext->deleteGlobalRef(externalObject); } break; } } pthread_mutex_unlock(&proxyComponentMapMutex); }
CXXContext::~CXXContext() { JNIContext *jniContext = JNIContext::sharedInstance(); ectx_method_createProxiedCallback = 0; if (ectx_class != 0) { jniContext->deleteGlobalRef(ectx_class); ectx_class = 0; } if (ectx_object != 0) { jniContext->deleteGlobalRef(ectx_object); ectx_object = 0; } if (actx_object != 0) { jniContext->deleteGlobalRef(actx_object); actx_object = 0; } sm_sharedInstance = 0; }
void CXXContext::setAndroidContext(jobject applicationContext) { LOGV("setApplicationContext applicationContext %ld", (long) applicationContext); JNIContext *jniContext = JNIContext::sharedInstance(); jniContext->deleteGlobalRef(actx_object); actx_object = jniContext->localToGlobalRef(applicationContext); LOGV("updated actx_object %ld", (long) actx_object); }
jobject CXXContext::createProxiedCallback(long contextAddress, long proxiedObjectID, const char *externalClassName) { LOGV("createProxiedCallback contextAddress %ld proxiedObjectID %d proxiedClass %s", contextAddress, proxiedObjectID, externalClassName); JNIContext *jni = JNIContext::sharedInstance(); jclass clazz = jni->getClassRef(externalClassName); jobject object = jni->callObjectMethod(ectx_object, ectx_method_createProxiedCallback, (jlong) contextAddress, (jlong) proxiedObjectID, clazz); jni->deleteLocalRef(clazz); return object; }
jobject createProxiedComponent(long contextAddress, const char *externalClassName, jmethodID methodID, jvalue *args) { LOGV("createProxiedComponent contextAddress %ld proxiedClass %s using args %ld", contextAddress, externalClassName, (long) args); JNIContext *jni = JNIContext::sharedInstance(); jclass clazz = jni->getClassRef(externalClassName); jobject object = jni->createNewObject(clazz, methodID, args); jni->deleteLocalRef(clazz); return object; }
int CXXContext::createCXXContext(JavaVM *vm, jobject applicationContext) { pthread_mutex_lock(&mutex); if (sm_sharedInstance == 0) { JNIEnv* env = 0; if (vm->GetEnv(reinterpret_cast<void**>(&env), CXX_JNI_VERSION) != JNI_OK) { LOGW("Java VM failed to initialize"); pthread_mutex_unlock(&mutex); return CXX_ERR; } JNIContext::createContext(vm); JNIContext *jni = JNIContext::sharedInstance(); jobject cxxJavaContext = jni->invokeStaticObjectMethod("com/zynga/sdk/cxx/CXXContext", "getInstance", "()Lcom/zynga/sdk/cxx/CXXContext;"); new CXXContext(applicationContext, cxxJavaContext); } pthread_mutex_unlock(&mutex); return CXX_OK; }
long CXXContext::findProxiedComponent(jobject javaObject) { LOGV("findProxiedComponent javaObject address %ld", (long) javaObject); long contextAddress = 0; pthread_mutex_lock(&proxyComponentMapMutex); JNIContext *jniContext = JNIContext::sharedInstance(); std::map<long,jobject>::const_iterator iter; for (iter = proxyComponentMap.begin(); iter != proxyComponentMap.end(); iter++) { jobject proxiedJavaObject = (*iter).second; if (jniContext->isSameInstance(proxiedJavaObject, javaObject)) { contextAddress = (long) (*iter).first; LOGV("findProxiedComponent contextAddress %ld", contextAddress); break; } } pthread_mutex_unlock(&proxyComponentMapMutex); return contextAddress; }
bool CXXContext::deleteProxyComponent(jobject externalObject) { LOGV("deleteProxyComponent externalObject %ld", (long) externalObject); JNIContext *jni = JNIContext::sharedInstance(); bool success = false; jni->pushLocalFrame(); if (jni->newLocalRef(externalObject) != 0) { jobject globalRef = jni->newGlobalRef(externalObject); jni->deleteGlobalRef(globalRef); success = true; } jni->popLocalFrame(); return success; }
void convert__byte_array(long& java_value, long& cxx_value, const CXXTypeHierarchy cxx_type_hierarchy, const converter_t& converter_type, std::stack<long>& converter_stack) { JNIContext *jni = JNIContext::sharedInstance(); if (converter_type == CONVERT_TO_JAVA) { jni->pushLocalFrame(); cxx_converter item_converter = get_converter(converter_stack); std::vector<byte> *cxx_vector = (std::vector<byte> *) cxx_value; int count = cxx_vector->size(); char java_array[count]; int item_idx = 0; for(std::vector<byte>::iterator it = cxx_vector->begin(); it != cxx_vector->end(); ++it) { byte item = (byte) *it; java_array[item_idx++] = item; } java_value = (long) jni->createByteArray(*java_array, count); java_value = (long) jni->popLocalFrame((jobject) java_value); } else if (converter_type == CONVERT_TO_CXX) { jni->pushLocalFrame(); cxx_converter item_converter = get_converter(converter_stack); std::vector<byte> *cxx_vector = new std::vector<byte>(); int count = (int) jni->getArrayLength((jarray) java_value); char * java_array = jni->getByteArray((jbyteArray) java_value); for (int idx = 0 ; idx < count; idx++) { char item = (char) java_array[idx]; cxx_vector->push_back((byte) item); } cxx_value = (long) cxx_vector; jni->popLocalFrame(); } log("convert__byte_array exit"); }
CXXContext::CXXContext(jobject applicationContext, jobject cxxJavaContext) { sm_sharedInstance = this; JNIContext *jniContext = JNIContext::sharedInstance(); actx_object = jniContext->localToGlobalRef(applicationContext); LOGV("loaded actx_object %d", actx_object); ectx_object = jniContext->localToGlobalRef(cxxJavaContext); LOGV("loaded ectx_object %d", ectx_object); ectx_class = jniContext->getClassRef(JAVA_CLASS_NAME); ectx_class = (jclass) jniContext->localToGlobalRef(ectx_class); LOGV("loaded ectx_class %d", ectx_class); ectx_method_createProxiedCallback = jniContext->getMethodID(ectx_class, JAVA_CREATE_PROXIED_METHOD_NAME, JAVA_CREATE_PROXIED_METHOD_SIGNATURE); LOGV("loaded ectx_method_createProxiedCallback"); }
void convert__object_array_type(long& java_value, long& cxx_value, const CXXTypeHierarchy cxx_type_hierarchy, const converter_t& converter_type, std::stack<long>& converter_stack) { JNIContext *jni = JNIContext::sharedInstance(); if (converter_type == CONVERT_TO_JAVA) { jni->pushLocalFrame(); cxx_converter item_converter = get_converter(converter_stack); std::vector<long> *cxx_vector = (std::vector<long> *) cxx_value; int count = cxx_vector->size(); std::string child_type = jni->getJNIName( std::string("java.lang.Object") ); CXXTypeHierarchy item_type; item_type.type_name = child_type; std::vector<CXXTypeHierarchy> child_types = cxx_type_hierarchy.child_types; if (child_types.size() > 0) { item_type = child_types.at(0); child_type = jni->getJNIName( item_type.type_name ); } java_value = (long) jni->createObjectArray(count, jni->getClassRef( child_type.c_str() )); for(std::vector<long>::iterator it = cxx_vector->begin(); it != cxx_vector->end(); ++it) { long cxx_item_ptr = (long) *it; long java_item_ptr = 0; int item_idx = it - cxx_vector->begin(); item_converter(java_item_ptr, cxx_item_ptr, item_type, converter_type, converter_stack); jni->setObjectArrayElement((jobjectArray) java_value, item_idx, (jobject) java_item_ptr); } java_value = (long) jni->popLocalFrame((jobject) java_value); } else if (converter_type == CONVERT_TO_CXX) { jni->pushLocalFrame(); std::string child_type = jni->getJNIName( std::string("java.lang.Object") ); CXXTypeHierarchy item_type; item_type.type_name = child_type; std::vector<CXXTypeHierarchy> child_types = cxx_type_hierarchy.child_types; if (child_types.size() > 0) { item_type = child_types.at(0); child_type = jni->getJNIName( item_type.type_name ); } cxx_converter item_converter = get_converter(converter_stack); std::vector<long> *cxx_vector = (std::vector<long> *) cxx_value; int size = (int) jni->getArrayLength((jobjectArray) java_value); for (int idx = 0 ; idx < size; idx++) { long java_item_ptr = (long) jni->getObjectArrayElement((jobjectArray) java_value, idx); long cxx_item_ptr = 0; item_converter(java_item_ptr, cxx_item_ptr, item_type, converter_type, converter_stack); cxx_vector->push_back(cxx_item_ptr); } jni->popLocalFrame(); } }