Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/content filter error architecture #239

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions Platform/Android/Android.mk
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Copyright (c) 2014 Readium Foundation and/or its licensees. All rights reserved.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Licensed under Gnu Affero General Public License Version 3 (provided, notwithstanding this
# notice, Readium Foundation reserves the right to license this material under a different
# separate license, and if you have done so, the terms of that separate license control and
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Licensed under Gnu Affero General Public License Version 3 (provided, notwithstanding this
# notice, Readium Foundation reserves the right to license this material under a different
# separate license, and if you have done so, the terms of that separate license control and
# the following references to GPL do not apply).
#
# This program is free software: you can redistribute it and/or modify it under the terms
# of the GNU Affero General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version. You should have
# received a copy of the GNU Affero General Public License along with this program. If not,
#
# This program is free software: you can redistribute it and/or modify it under the terms
# of the GNU Affero General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version. You should have
# received a copy of the GNU Affero General Public License along with this program. If not,
# see <http://www.gnu.org/licenses/>.

LOCAL_PATH := $(call my-dir)/../..
Expand Down Expand Up @@ -277,6 +277,7 @@ LOCAL_SRC_FILES := \
ePub3/ePub/filter_chain_byte_stream_range.cpp \
ePub3/ePub/filter_manager_impl.cpp \
ePub3/ePub/filter_manager.cpp \
ePub3/ePub/filter.cpp \
ePub3/ePub/PassThroughFilter.cpp \
ePub3/ePub/font_obfuscation.cpp \
ePub3/ePub/glossary.cpp \
Expand Down
48 changes: 46 additions & 2 deletions Platform/Android/jni/epub3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <ePub3/archive.h>
#include <ePub3/container.h>
#include <ePub3/filter.h>
#include <ePub3/initialization.h>
#include <ePub3/utilities/error_handler.h>

Expand Down Expand Up @@ -70,6 +71,9 @@ static const char *javaEPub3_appendBytesToBufferSignature = "(Ljava/nio/ByteBuff
static const char *javaEPub3_handleSdkErrorMethodName = "handleSdkError";
static const char *javaEPub3_handleSdkErrorSignature = "(Ljava/lang/String;Z)Z";

static const char *javaEPub3_handleContentFilterErrorMethodName = "handleContentFilterError";
static const char *javaEPub3_handleContentFilterErrorSignature = "(Ljava/lang/String;JLjava/lang/String;)V";


/*
* Exported variables
Expand All @@ -95,6 +99,11 @@ jmethodID createManifestItem_ID;
jmethodID addManifestItemToList_ID;


// Global variable to store the current Java virtual machine.
// This will be needed by the ContentFilterErrorHandler() function.
JavaVM *g_vm = NULL;


/*
* Internal variables
**************************************************/
Expand All @@ -110,7 +119,7 @@ static jmethodID addStringToList_ID;
static jmethodID createBuffer_ID;
static jmethodID appendBytesToBuffer_ID;
static jmethodID handleSdkError_ID;

static jmethodID handleContentFilterError_ID;

/*
* Exported functions
Expand Down Expand Up @@ -190,6 +199,10 @@ jboolean javaEPub3_handleSdkError(JNIEnv *env, jstring message, jboolean isSever
return b;
}

void javaEPub3_handleContentFilterError(JNIEnv *env, jstring filterId, jlong errorCode, jstring message) {
env->CallStaticVoidMethod(javaEPub3Class, handleContentFilterError_ID, filterId, errorCode, message);
}

/*
* Internal functions
**************************************************/
Expand All @@ -211,11 +224,13 @@ static int onLoad_cacheJavaElements_epub3(JNIEnv *env) {
javaEPub3_appendBytesToBufferMethodName, javaEPub3_appendBytesToBufferSignature, ONLOAD_ERROR);
INIT_STATIC_METHOD_ID_RETVAL(handleSdkError_ID, javaEPub3Class, javaEPub3ClassName,
javaEPub3_handleSdkErrorMethodName, javaEPub3_handleSdkErrorSignature, ONLOAD_ERROR);
INIT_STATIC_METHOD_ID_RETVAL(handleContentFilterError_ID, javaEPub3Class, javaEPub3ClassName,
javaEPub3_handleContentFilterErrorMethodName, javaEPub3_handleContentFilterErrorSignature, ONLOAD_ERROR);
// Return JNI_VERSION for OK, if not one of the lines above already returned ONLOAD_ERROR
return JNI_VERSION;
}

// needed only for the LauncherErrorHandler() C++ callback,
// needed only for the following C++ callbacks,
// set by initializeReadiumSDK()
static JNIEnv* m_env = nullptr;

Expand All @@ -238,6 +253,30 @@ static bool LauncherErrorHandler(const ePub3::error_details& err)
//return ePub3::DefaultErrorHandler(err);
}

/**
* Callback to be invoked when any ContentFilter has an error.
*/

static void ContentFilterErrorHandler(const std::string &filterId, unsigned int errorCode, const std::string &message)
{
// Making sure that we have the right JNI environment pointer for every thread.
// This function may be called in different threads, and you must have the right
// JNI environment pointer for the current thread, otherwise it will crash the app.
JNIEnv *env;
if (g_vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION) != JNI_OK) {
LOGE("ContentFilterErrorHandler: failed to get environment. VM doesn't support JNI version 1.6");
return;
}

jstring jFilterId = env->NewStringUTF(filterId.c_str());
jstring jMessage = env->NewStringUTF(message.c_str());

javaEPub3_handleContentFilterError(env, jFilterId, (jlong)errorCode, jMessage);

env->DeleteLocalRef(jFilterId);
env->DeleteLocalRef(jMessage);
}

/**
* Initializes the Readium SDK.
*/
Expand All @@ -249,6 +288,9 @@ static void initializeReadiumSDK(JNIEnv* env)

ePub3::ErrorHandlerFn launcherErrorHandler = LauncherErrorHandler;
ePub3::SetErrorHandler(launcherErrorHandler);

ePub3::ContentFilterErrorHandlerFn contentFilterErrorHandler = ContentFilterErrorHandler;
ePub3::ContentFilter::ResetContentFilterErrorHandler(contentFilterErrorHandler);

ePub3::InitializeSdk();
ePub3::PopulateFilterManager();
Expand All @@ -266,6 +308,8 @@ static void initializeReadiumSDK(JNIEnv* env)
*/
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
g_vm = vm;

// Get the JNI Environment to be able to initialize the cached the java elements
JNIEnv* env;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION) != JNI_OK) {
Expand Down
2 changes: 2 additions & 0 deletions Platform/Android/jni/epub3.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ void javaEPub3_appendBytesToBuffer(JNIEnv *env, jobject buffer, jbyteArray data)

jboolean javaEPub3_handleSdkError(JNIEnv *env, jstring message, jboolean isSevereEpubError);

void javaEPub3_handleContentFilterError(JNIEnv *env, jstring filterId, jlong errorCode, jstring message);

/*
* JNI functions
**************************************************/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2015 Readium Foundation and/or its licensees. All rights reserved.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// Licensed under Gnu Affero General Public License Version 3 (provided, notwithstanding this notice,
// Readium Foundation reserves the right to license this material under a different separate license,
// and if you have done so, the terms of that separate license control and the following references
// to GPL do not apply).
//
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU
// Affero General Public License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version. You should have received a copy of the GNU
// Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.


package org.readium.sdk.android;

public interface ContentFilterErrorHandler {
void handleContentFilterError(String filterId, long errorCode, String message);
}
12 changes: 12 additions & 0 deletions Platform/Android/src/org/readium/sdk/android/EPub3.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,16 @@ private static boolean handleSdkError(String message, boolean isSevereEpubError)
// never throws an exception
return true;
}

private static ContentFilterErrorHandler m_contentFilterErrorHandler = null;

public static void setContentFilterErrorHandler(ContentFilterErrorHandler handler) {
m_contentFilterErrorHandler = handler;
}

private static void handleContentFilterError(String filterId, long errorCode, String message) {
if (m_contentFilterErrorHandler != null) {
m_contentFilterErrorHandler.handleContentFilterError(filterId, errorCode, message);
}
}
}
8 changes: 8 additions & 0 deletions Platform/Apple/RDServices/Main/RDContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,19 @@

#import <Foundation/Foundation.h>


extern NSString * const EPub3ContentFilterErrorDomain;
extern NSString * const EPub3ContentFilterIdentifierKey;
extern NSString * const EPub3ContentFilterErrorCodeKey;
extern NSString * const EPub3ContentFilterErrorMessage;


@class RDContainer;

@protocol RDContainerDelegate <NSObject>

- (BOOL)container:(RDContainer *)container handleSdkError:(NSString *)message isSevereEpubError:(BOOL)isSevereEpubError;
- (void)container:(RDContainer *)container handleContentFilterError:(NSError *)error;

@end

Expand Down
21 changes: 21 additions & 0 deletions Platform/Apple/RDServices/Main/RDContainer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@
#import <ePub3/container.h>
#import <ePub3/initialization.h>
#import <ePub3/utilities/error_handler.h>
#import <epub3/filter.h>
#import "RDPackage.h"

NSString * const EPub3ContentFilterErrorDomain = @"ePub3ContentFilterErrorDomain";
NSString * const EPub3ContentFilterIdentifierKey = @"ePub3ContentFilterIdentifierKey";
NSString * const EPub3ContentFilterErrorCodeKey = @"ePub3ContentFilterErrorCodeKey";
NSString * const EPub3ContentFilterErrorMessage = @"ePub3ContentFilterErrorMessageKey";


@interface RDContainer () {
@private std::shared_ptr<ePub3::Container> m_container;
Expand Down Expand Up @@ -81,6 +87,21 @@ - (instancetype)initWithDelegate:(id <RDContainerDelegate>)delegate path:(NSStri
//return ePub3::DefaultErrorHandler(err);
};
ePub3::SetErrorHandler(sdkErrorHandler);

ePub3::ContentFilterErrorHandlerFn contentFilterErrorHandler = ^(const std::string &filterId, unsigned int errorCode, const std::string &message) {

NSDictionary *errorDictionary = @{
EPub3ContentFilterIdentifierKey : [NSString stringWithUTF8String:filterId.c_str()],
EPub3ContentFilterErrorCodeKey : [[NSNumber alloc] initWithUnsignedInt:errorCode],
EPub3ContentFilterErrorMessage : [NSString stringWithUTF8String:message.c_str()]
};

NSError *error = [[NSError alloc] initWithDomain:EPub3ContentFilterErrorDomain code:errorCode userInfo:errorDictionary];

[m_delegate container:self handleContentFilterError:error];
};

ePub3::ContentFilter::ResetContentFilterErrorHandler(contentFilterErrorHandler);

ePub3::InitializeSdk();
ePub3::PopulateFilterManager();
Expand Down
6 changes: 6 additions & 0 deletions Platform/Apple/ePub3.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
1EFA3ACB17AB0BEF003A4BC2 /* filter_manager_impl.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1EFA3ACA17AB0BEF003A4BC2 /* filter_manager_impl.cpp */; };
1EFA3ACC17AB0C7A003A4BC2 /* filter_manager_impl.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1EFA3ACA17AB0BEF003A4BC2 /* filter_manager_impl.cpp */; };
3418BA7D16C4151E009AA7EF /* ring_buffer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ABA88FD116C2B4ED00F2014B /* ring_buffer.cpp */; };
586C95941B41FCCE00FD3700 /* filter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 586C95931B41FCCE00FD3700 /* filter.cpp */; };
586C95951B41FCCE00FD3700 /* filter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 586C95931B41FCCE00FD3700 /* filter.cpp */; };
588D24201A02EF8F006A92BB /* PassThroughFilter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 588D241E1A02EF8F006A92BB /* PassThroughFilter.cpp */; };
588D24211A02EF8F006A92BB /* PassThroughFilter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 588D241E1A02EF8F006A92BB /* PassThroughFilter.cpp */; };
588D24221A02EF8F006A92BB /* PassThroughFilter.h in Headers */ = {isa = PBXBuildFile; fileRef = 588D241F1A02EF8F006A92BB /* PassThroughFilter.h */; };
Expand Down Expand Up @@ -445,6 +447,7 @@
1ED0084717A9DC6F00819EBD /* initialization.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = initialization.cpp; sourceTree = "<group>"; };
1EFA3AC917AB0BC0003A4BC2 /* filter_manager_impl.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = filter_manager_impl.h; sourceTree = "<group>"; };
1EFA3ACA17AB0BEF003A4BC2 /* filter_manager_impl.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = filter_manager_impl.cpp; sourceTree = "<group>"; };
586C95931B41FCCE00FD3700 /* filter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = filter.cpp; sourceTree = "<group>"; };
588D241E1A02EF8F006A92BB /* PassThroughFilter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PassThroughFilter.cpp; sourceTree = "<group>"; };
588D241F1A02EF8F006A92BB /* PassThroughFilter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PassThroughFilter.h; sourceTree = "<group>"; };
850B1AE816A75AB000619C3C /* TestData */ = {isa = PBXFileReference; lastKnownFileType = folder; name = TestData; path = ../../TestData; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1098,6 +1101,7 @@
A250D24D05706C9BB1AA54ED /* filter_chain_byte_stream_range.h */,
A250DFDBD90C7E9C632B1E00 /* filter_chain_byte_stream.cpp */,
A250D26DB00356B591AEFC29 /* filter_chain_byte_stream.h */,
586C95931B41FCCE00FD3700 /* filter.cpp */,
);
name = Filters;
sourceTree = "<group>";
Expand Down Expand Up @@ -2030,6 +2034,7 @@
buildActionMask = 2147483647;
files = (
ABA4BAEF16ADF64400161B77 /* string16.cc in Sources */,
586C95951B41FCCE00FD3700 /* filter.cpp in Sources */,
ABA4BAF016ADF64400161B77 /* gurl.cc in Sources */,
AB5284D417CBC395003D7BBF /* CPUCacheUtils.c in Sources */,
ABA4BAF316ADF64400161B77 /* url_canon_etc.cc in Sources */,
Expand Down Expand Up @@ -2174,6 +2179,7 @@
buildActionMask = 2147483647;
files = (
ABB18FE61656863300CFC651 /* mkstemp.c in Sources */,
586C95941B41FCCE00FD3700 /* filter.cpp in Sources */,
AB718C04184CEEA900F86C6B /* xml_bridge_dtrace_probes.d in Sources */,
ABB18FE81656863300CFC651 /* zip_add.c in Sources */,
ABB18FE91656863300CFC651 /* zip_add_dir.c in Sources */,
Expand Down
6 changes: 5 additions & 1 deletion ePub3/ePub/PassThroughFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ bool PassThroughFilter::SniffPassThroughContent(ConstManifestItemPtr item)
// auto mediaType = item->MediaType();
// return (mediaType == "audio/mp4" || mediaType == "audio/mpeg" || mediaType == "video/mp4" || mediaType == "video/mpeg");

return false;
return false;
}

ContentFilterPtr PassThroughFilter::PassThroughFactory(ConstPackagePtr package)
Expand Down Expand Up @@ -96,6 +96,10 @@ void *PassThroughFilter::FilterData(FilterContext *context, void *data, size_t l

if (!byteStream->IsOpen())
{
HandleContentFilterError(
std::string(PASS_THROUGH_FILTER_ID),
ContentFilterError::InputStreamCannotBeOpened,
std::string("Input ByteStream is not opened"));
return nullptr;
}

Expand Down
3 changes: 3 additions & 0 deletions ePub3/ePub/PassThroughFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include <cstring>
#import <ePub3/utilities/byte_stream.h>

#define PASS_THROUGH_FILTER_ID "3439DA53-2559-400D-8231-981ABA6A85B4"


EPUB3_BEGIN_NAMESPACE


Expand Down
29 changes: 29 additions & 0 deletions ePub3/ePub/filter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// filter.cpp
// ePub3
//
// Created by Nelson Leme on 5/28/15.
// Copyright (c) 2015 The Readium Foundation and contributors. All rights reserved.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// Licensed under Gnu Affero General Public License Version 3 (provided, notwithstanding this notice,
// Readium Foundation reserves the right to license this material under a different separate license,
// and if you have done so, the terms of that separate license control and the following references
// to GPL do not apply).
//
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU
// Affero General Public License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version. You should have received a copy of the GNU
// Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.


#include <stdio.h>
#include "filter.h"

EPUB3_BEGIN_NAMESPACE

ContentFilterErrorHandlerFn ContentFilter::s_contentFilterErrorHandler = ContentFilter::DefaultContentFilterErrorHandler;

EPUB3_END_NAMESPACE
Loading