Skip to content

Commit

Permalink
Merge pull request #281 from readium/feature/lcp
Browse files Browse the repository at this point in the history
feature/lcp
  • Loading branch information
danielweck authored Mar 21, 2017
2 parents 3828ae5 + 28db6f0 commit 7ec3ca6
Show file tree
Hide file tree
Showing 42 changed files with 1,155 additions and 449 deletions.
253 changes: 179 additions & 74 deletions Platform/Android/epub3/src/main/jni/epub3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "packagejni.h"
#include "iri.h"
#include "resource_stream.h"
#include "content_module_exception.h"


using namespace std;
Expand Down Expand Up @@ -75,13 +76,17 @@ static const char *javaEPub3_handleSdkErrorSignature = "(Ljava/lang/String;Z)Z";
* Exported variables
**************************************************/

#if ENABLE_ZIP_ARCHIVE_WRITER

/**
* Global variable to share the Android Cache directory with the
* Core SDK. It is used in /ePub3/ePub/zip_archive.cpp to store
* temporary files.
*/
char gAndroidCacheDir[PATH_MAX] = {0};

#endif //ENABLE_ZIP_ARCHIVE_WRITER

//TODO: Remove when all these when passed to respective classes
jclass javaJavaObjectsFactoryClass = NULL;
jmethodID createSpineItemList_ID;
Expand Down Expand Up @@ -348,6 +353,8 @@ JNIEXPORT void JNI_OnUnload(JavaVM* vm, void* reserved) {
//TODO: Fill when needed
}

#if ENABLE_ZIP_ARCHIVE_WRITER

/*
* Class: org_readium_sdk_android_EPub3
* Method: setCachePath
Expand All @@ -364,6 +371,8 @@ Java_org_readium_sdk_android_EPub3_setCachePath(JNIEnv* env, jobject thiz, jstri
RELEASE_UTF8(cachePath, str);
}

#endif //ENABLE_ZIP_ARCHIVE_WRITER

/*
* Class: org_readium_sdk_android_EPub3
* Method: setContentFiltersRegistrationHandler
Expand Down Expand Up @@ -394,115 +403,211 @@ Java_org_readium_sdk_android_EPub3_setContentFiltersRegistrationHandler(JNIEnv*
contentFiltersRegistrationHandler_Run_ID = m;
}
}
//
///*
// * Class: org_readium_sdk_android_EPub3
// * Method: isEpub3Book
// * Signature: (Ljava/lang/String;)Z
// */
//JNIEXPORT jboolean JNICALL Java_org_readium_sdk_android_EPub3_isEpub3Book(JNIEnv* env, jobject thiz, jstring path) {
// // Initialize core ePub3 SDK
// initializeReadiumSDK(env);
//
// std::string _path = jni::StringUTF(env, path);
// LOGD("EPub3.isEpub3Book(): path received is '%s'", _path.c_str());
//
// shared_ptr<ePub3::Container> _container = nullptr;
// try {
// _container = ePub3::Container::OpenContainer(_path);
//
// shared_ptr<ePub3::Package> _package = nullptr;
// try {
// _package = _container->DefaultPackage();
//
// if(_package != nullptr) {
// ePub3::string versionStr;
// int version = 0;
//
// versionStr = _package->Version();
// if(versionStr.empty()) {
// LOGE("EPub3.isEpub3Book(): couldn't get package version");
// } else {
// // GNU libstdc++ seems to not want to let us use these C++11 routines...
//#ifndef _LIBCPP_VERSION
// version = (int)strtol(versionStr.c_str(), nullptr, 10);
//#else
// version = std::stoi(versionStr.stl_str());
//#endif
//
// if(version >= 3) {
// LOGD("EPub3.isEpub3Book(): returning true");
// return JNI_TRUE;
// }
// }
//
// }
// }
// catch(const std::invalid_argument& ex) {
// LOGE("EPub3.isEpub3Book(): failed to open package: %s\n", ex.what());
// }
// }
// catch (const std::invalid_argument& ex) {
// LOGE("EPub3.isEpub3Book(): failed to open container: %s\n", ex.what());
// }
//
// LOGD("EPub3.isEpub3Book(): returning false");
// return JNI_FALSE;
//}


/*
* Class: org_readium_sdk_android_EPub3
* Method: isEpub3Book
* Signature: (Ljava/lang/String;)Z
* Method: openBook
* Signature: (Ljava/lang/String;)Lorg/readium/sdk/android/Container
*/
JNIEXPORT jboolean JNICALL Java_org_readium_sdk_android_EPub3_isEpub3Book(JNIEnv* env, jobject thiz, jstring path) {
// Initialize core ePub3 SDK
initializeReadiumSDK(env);
JNIEXPORT jobject JNICALL
Java_org_readium_sdk_android_EPub3_openBook(JNIEnv* env, jobject thiz, jstring path) {

// Initialize core ePub3 SDK
initializeReadiumSDK(env);

std::string _path = jni::StringUTF(env, path);
LOGD("EPub3.isEpub3Book(): path received is '%s'", _path.c_str());
char *nativePath;
GET_UTF8_RETVAL(nativePath, path, NULL);
LOGD("EPub3.openBook(): path received is '%s'", nativePath);

std::string spath = std::string(nativePath);

shared_ptr<ePub3::Container> _container = nullptr;
try {
_container = ePub3::Container::OpenContainer(_path);

shared_ptr<ePub3::Package> _package = nullptr;
try {
_package = _container->DefaultPackage();

if(_package != nullptr) {
ePub3::string versionStr;
int version = 0;

versionStr = _package->Version();
if(versionStr.empty()) {
LOGE("EPub3.isEpub3Book(): couldn't get package version");
} else {
// GNU libstdc++ seems to not want to let us use these C++11 routines...
#ifndef _LIBCPP_VERSION
version = (int)strtol(versionStr.c_str(), nullptr, 10);
#else
version = std::stoi(versionStr.stl_str());
#endif
_container = ePub3::Container::OpenContainer(spath);
}
catch (const ePub3::ContentModuleExceptionDecryptFlow& ex) {
LOGD("OpenContainer() ContentModuleExceptionDecryptFlow: %s\n", ex.what());

if(version >= 3) {
LOGD("EPub3.isEpub3Book(): returning true");
return JNI_TRUE;
}
}
RELEASE_UTF8(path, nativePath);

}
}
catch(const std::invalid_argument& ex) {
LOGE("EPub3.isEpub3Book(): failed to open package: %s\n", ex.what());
}
return nullptr;
}
catch (const ePub3::ContentModuleException& ex) {
LOGD("OpenContainer() ContentModuleException: %s\n", ex.what());

RELEASE_UTF8(path, nativePath);


jstring jmessage = m_env->NewStringUTF(ex.what());
jboolean b = javaEPub3_handleSdkError(m_env, jmessage, (jboolean)true);
m_env->DeleteLocalRef(jmessage);

return nullptr;
}
catch (const std::invalid_argument& ex) {
LOGE("EPub3.isEpub3Book(): failed to open container: %s\n", ex.what());
catch (const std::exception& ex) {
LOGD("OpenContainer() EXCEPTION: %s\n", ex.what());

RELEASE_UTF8(path, nativePath);



jstring jmessage = m_env->NewStringUTF(ex.what());
jboolean b = javaEPub3_handleSdkError(m_env, jmessage, (jboolean)true);
m_env->DeleteLocalRef(jmessage);

return nullptr;
}

LOGD("EPub3.isEpub3Book(): returning false");
return JNI_FALSE;
if (_container == nullptr) {
LOGD("OpenContainer() NULL\n");

RELEASE_UTF8(path, nativePath);
return nullptr;
}

LOGD("EPub3.openBook(): _container OK, version: %s\n", _container->Version().c_str());

// Save container before sending it to Java
jni::Pointer container(_container, POINTER_GPS("container"));

jobject jContainer = javaContainer_createContainer(env, container.getId(), path);

auto packages = _container->Packages();

for (auto packageIt = packages.begin(); packageIt != packages.end(); ++packageIt) {
auto _package = &*(&*packageIt);
LOGD("EPub3.openBook(): package type: %p %s\n", _package, typeid(_package).name());

// Save package before sending it to Java
jni::Pointer package(*_package, POINTER_GPS("package"));

javaContainer_addPackageToContainer(env, jContainer, package.getId());
LOGD("EPub3.openBook(): package added");
}

//TODO: Just for testing dump
//std::string dump = jni::PointerPool::dump();
//LOGD("openBook(): pointer pool dump: %s", dump.c_str());

RELEASE_UTF8(path, nativePath);

return jContainer;
}

/*
* Class: org_readium_sdk_android_EPub3
* Method: openBook
* Signature: (Ljava/lang/String;)Lorg/readium/sdk/android/Container
*/

JNIEXPORT jobject JNICALL
Java_org_readium_sdk_android_EPub3_openBook(JNIEnv* env, jobject thiz, jstring path) {
// Initialize core ePub3 SDK
initializeReadiumSDK(env);
Java_org_readium_sdk_android_EPub3_openBookPlain(JNIEnv* env, jobject thiz, jstring path) {

char *nativePath;
GET_UTF8_RETVAL(nativePath, path, NULL);
LOGD("EPub3.openBook(): path received is '%s'", nativePath);
// Initialize core ePub3 SDK
initializeReadiumSDK(env);

char *nativePath;
GET_UTF8_RETVAL(nativePath, path, NULL);
LOGD("EPub3.openBookPlain(): path received is '%s'", nativePath);

std::string spath = std::string(nativePath);

std::string spath = std::string(nativePath);

shared_ptr<ePub3::Container> _container = nullptr;
try {
_container = ePub3::Container::OpenContainer(spath);
_container = ePub3::Container::OpenContainerForContentModule(spath);
}
catch (const std::invalid_argument& ex) {
LOGD("OpenContainer() EXCEPTION: %s\n", ex.what());
catch (const std::exception& ex) {
LOGD("OpenContainerForContentModule() EXCEPTION: %s\n", ex.what());

RELEASE_UTF8(path, nativePath);
return nullptr;
}

LOGD("EPub3.openBook(): _container OK, version: %s\n", _container->Version().c_str());

// Save container before sending it to Java
jni::Pointer container(_container, POINTER_GPS("container"));
if (_container == nullptr) {
LOGD("OpenContainerForContentModule() NULL\n");

RELEASE_UTF8(path, nativePath);
return nullptr;
}

jobject jContainer = javaContainer_createContainer(env, container.getId(), path);
LOGD("EPub3.openBookPlain(): _container OK, version: %s\n", _container->Version().c_str());

// Save container before sending it to Java
jni::Pointer container(_container, POINTER_GPS("container"));

jobject jContainer = javaContainer_createContainer(env, container.getId(), path);

auto packages = _container->Packages();

for (auto packageIt = packages.begin(); packageIt != packages.end(); ++packageIt) {
auto _package = &*(&*packageIt);
LOGD("EPub3.openBook(): package type: %p %s\n", _package, typeid(_package).name());
auto _package = &*(&*packageIt);
LOGD("EPub3.openBookPlain(): package type: %p %s\n", _package, typeid(_package).name());

// Save package before sending it to Java
jni::Pointer package(*_package, POINTER_GPS("package"));
// Save package before sending it to Java
jni::Pointer package(*_package, POINTER_GPS("package"));

javaContainer_addPackageToContainer(env, jContainer, package.getId());
LOGD("EPub3.openBook(): package added");
LOGD("EPub3.openBookPlain(): package added");
}

//TODO: Just for testing dump
//std::string dump = jni::PointerPool::dump();
//LOGD("openBook(): pointer pool dump: %s", dump.c_str());
//TODO: Just for testing dump
//std::string dump = jni::PointerPool::dump();
//LOGD("openBook(): pointer pool dump: %s", dump.c_str());

RELEASE_UTF8(path, nativePath);

return jContainer;
return jContainer;
}

/*
Expand All @@ -511,8 +616,8 @@ Java_org_readium_sdk_android_EPub3_openBook(JNIEnv* env, jobject thiz, jstring p
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_readium_sdk_android_EPub3_initialize(JNIEnv* env, jobject thiz) {
// Initialize core ePub3 SDK
initializeReadiumSDK(env);
// Initialize core ePub3 SDK
initializeReadiumSDK(env);
}

/*
Expand All @@ -521,7 +626,7 @@ JNIEXPORT void JNICALL Java_org_readium_sdk_android_EPub3_initialize(JNIEnv* env
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_org_readium_sdk_android_EPub3_releaseNativePointer(JNIEnv* env, jobject thiz, jlong ptr) {
jni::PointerPool::del(ptr);
jni::PointerPool::del(ptr);
}


Expand Down
Loading

0 comments on commit 7ec3ca6

Please sign in to comment.