Skip to content
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
37 changes: 37 additions & 0 deletions librtt/Rtt_Event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2742,6 +2742,43 @@ UrlRequestEvent::Push( lua_State *L ) const

// ----------------------------------------------------------------------------

CommonEvent::CommonEvent( const char *fEventName, const char *fData )
: fEventName( fEventName ),
fData( fData )
{
}

const char*
CommonEvent::Name() const
{
return fEventName;
}

int
CommonEvent::Push( lua_State *L ) const
{
if ( Rtt_VERIFY( Super::Push( L ) ) )
{
if ( fData )
{
if ( 0 == LuaContext::JsonDecode( L, fData) )
{
lua_setfield( L, -2, "detail" );
}
else
{
lua_pop( L, 1 );
lua_pushstring( L, fData );
lua_setfield( L, -2, "detail" );
}
}
}

return 1;
}

// ----------------------------------------------------------------------------

const char UserInputEvent::kName[] = "userInput";

const char*
Expand Down
21 changes: 21 additions & 0 deletions librtt/Rtt_Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,27 @@ class UrlRequestEvent : public VirtualEvent

// ----------------------------------------------------------------------------

// Common event
class CommonEvent : public VirtualEvent
{
public:
typedef VirtualEvent Super;
typedef CommonEvent Self;

public:
CommonEvent( const char *fEventName, const char *fData );

public:
virtual const char* Name() const;
virtual int Push( lua_State *L ) const;

private:
const char *fEventName;
const char *fData;
};

// ----------------------------------------------------------------------------

// Local event
class UserInputEvent : public VirtualEvent
{
Expand Down
34 changes: 34 additions & 0 deletions librtt/Rtt_LuaContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,40 @@ LuaContext::IsBinaryLua( const char* filename )
return result;
}

int
LuaContext::JsonEncode( lua_State *L, int index )
{
bool success = false;

lua_getglobal( L, "require" );
lua_pushstring( L, "json" );
if ( DoCall( L, 1, 1 ) == 0 )
{
lua_getfield( L, -1, "encode" );
lua_remove(L, -2);
lua_pushvalue( L, index );
success = ( DoCall( L, 1, 1 ) == 0 );
}
return success ? 0 : -1;
}

int
LuaContext::JsonDecode( lua_State *L, const char* json )
{
bool success = false;

lua_getglobal( L, "require" );
lua_pushstring( L, "json" );
if ( DoCall( L, 1, 1 ) == 0 )
{
lua_getfield( L, -1, "decode" );
lua_remove(L, -2);
lua_pushstring(L, json);
success = ( DoCall( L, 1, 1 ) == 0 );
}
return success ? 0 : -1;
}

// ----------------------------------------------------------------------------

LuaContext::LuaContext( ::lua_State* L )
Expand Down
2 changes: 2 additions & 0 deletions librtt/Rtt_LuaContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class LuaContext
static int OpenJson( lua_State *L );
static int OpenWidget( lua_State *L );
static int OpenStoryboard( lua_State *L );
static int JsonEncode( lua_State *L, int index );
static int JsonDecode( lua_State *L, const char *json );

public:
// Generic, re-entrant Lua callbacks
Expand Down
102 changes: 101 additions & 1 deletion platform/android/ndk/Rtt_AndroidWebViewObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@

namespace Rtt
{

// ----------------------------------------------------------------------------

static const char* kCoronaEventPrefix = "JS_";

AndroidWebViewObject::AndroidWebViewObject(
const Rect& bounds, AndroidDisplayObjectRegistry *displayObjectRegistry, NativeToJavaBridge *ntjb )
: Super( bounds, displayObjectRegistry, ntjb ),
Expand Down Expand Up @@ -251,6 +252,85 @@ AndroidWebViewObject::DeleteCookies( lua_State *L )
return 0;
}

int
AndroidWebViewObject::InjectJS( lua_State *L )
{
const LuaProxyVTable& table = PlatformDisplayObject::GetWebViewObjectProxyVTable();
AndroidWebViewObject *view = (AndroidWebViewObject *)luaL_todisplayobject(L, 1, table);
if ( view )
{
const char *jsCode = lua_tostring( L, 2 );
view->InjectJSCode( jsCode );
}

return 0;
}

int
AndroidWebViewObject::RegisterCallback( lua_State *L )
{
const LuaProxyVTable& table = PlatformDisplayObject::GetWebViewObjectProxyVTable();
AndroidWebViewObject *view = (AndroidWebViewObject *)luaL_todisplayobject( L, 1, table );
if ( view )
{
const char *eventName = lua_tostring( L, 2 );
String jsEventName(kCoronaEventPrefix);
jsEventName.Append( eventName );
view->AddEventListener( L, 3, jsEventName.GetString() );
}

return 0;
}

int
AndroidWebViewObject::On( lua_State *L )
{
const LuaProxyVTable& table = PlatformDisplayObject::GetWebViewObjectProxyVTable();
AndroidWebViewObject *view = (AndroidWebViewObject *)luaL_todisplayobject( L, 1, table );
if ( view )
{
const char *eventName = lua_tostring( L, 2 );
String jsEventName(kCoronaEventPrefix);
jsEventName.Append( eventName );
view->AddEventListener( L, 3, jsEventName.GetString() );
}

return 0;
}

int
AndroidWebViewObject::Send( lua_State *L )
{
const LuaProxyVTable& table = PlatformDisplayObject::GetWebViewObjectProxyVTable();
AndroidWebViewObject *view = (AndroidWebViewObject *)luaL_todisplayobject( L, 1, table );
if ( view )
{
const char* eventName = lua_tostring( L, 2 );
const char* jsonContent = "{}";
if ( 0 == LuaContext::JsonEncode( L, 3 ) )
{
jsonContent = lua_tostring( L, -1 );
}

String s( "window.dispatchEvent(new CustomEvent('" );
s.Append( kCoronaEventPrefix );
s.Append( eventName );
s.Append( "', {detail: " );
s.Append( jsonContent );
s.Append( "}));" );

view->InjectJSCode( s.GetString() );
}

return 0;
}

void
AndroidWebViewObject::InjectJSCode( const char *jsCode )
{
fNativeToJavaBridge->WebViewRequestInjectJS( GetId(), jsCode );
}

int
AndroidWebViewObject::ValueForKey( lua_State *L, const char key[] ) const
{
Expand All @@ -263,6 +343,26 @@ AndroidWebViewObject::ValueForKey( lua_State *L, const char key[] ) const
lua_pushlightuserdata( L, fNativeToJavaBridge );
lua_pushcclosure( L, Request, 1 );
}
else if ( strcmp( "injectJS", key ) == 0 )
{
lua_pushlightuserdata( L, fNativeToJavaBridge );
lua_pushcclosure( L, InjectJS, 1 );
}
else if ( strcmp( "registerCallback", key ) == 0 )
{
lua_pushlightuserdata( L, fNativeToJavaBridge );
lua_pushcclosure( L, RegisterCallback, 1 );
}
else if ( strcmp( "on", key ) == 0 )
{
lua_pushlightuserdata( L, fNativeToJavaBridge );
lua_pushcclosure( L, On, 1 );
}
else if ( strcmp( "send", key ) == 0 )
{
lua_pushlightuserdata( L, fNativeToJavaBridge );
lua_pushcclosure( L, Send, 1 );
}
else if ( strcmp( "stop", key ) == 0 )
{
lua_pushlightuserdata( L, fNativeToJavaBridge );
Expand Down
5 changes: 5 additions & 0 deletions platform/android/ndk/Rtt_AndroidWebViewObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ class AndroidWebViewObject : public AndroidDisplayObject
static int Reload( lua_State *L );
static int Resize( lua_State *L );
static int DeleteCookies( lua_State *L );
static int InjectJS( lua_State *L );
static int RegisterCallback( lua_State *L );
static int On( lua_State *L );
static int Send( lua_State *L );

public:
void Request(const char *url, const MPlatform::Directory baseDirectory);
void Request(const char *url, const char *baseUrl);
void InjectJSCode( const char *jsCode );
bool IsPopup() const { return fIsPopup; }
bool IsAutoCancelEnabled() const { return fAutoCancelEnabled; }
bool CanGoBack() const { return fCanGoBack; }
Expand Down
42 changes: 42 additions & 0 deletions platform/android/ndk/jni/JavaToNativeBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,48 @@ JavaToNativeBridge::WebViewHistoryUpdated( JNIEnv * env, int id, jboolean canGoB
view->SetCanGoForward(canGoForward);
}

void
JavaToNativeBridge::WebViewJSInterfaceCommonEvent( JNIEnv * env, int id, jstring type, jstring data, jboolean noResult )
{
// Validate.
if (!fPlatform)
{
return;
}

// Fetch the display object by ID.
Rtt::AndroidWebViewObject *view = (Rtt::AndroidWebViewObject*)(fPlatform->GetNativeDisplayObjectById(id));
if (!view)
{
return;
}

jstringResult typeS(env, type);
jstringResult dataS(env, data);
Rtt::CommonEvent e(typeS.getUTF8(), dataS.getUTF8());

lua_State *L = view->GetL();
int status = view->Rtt::DisplayObject::DispatchEventWithTarget( L, e, 1 );
if ( status == 0 && (! noResult ) )
{
int retValueIndex = lua_gettop( L );
const char* jsonContent = "{}";
if ( 0 == Rtt::LuaContext::JsonEncode( L, retValueIndex ) )
{
jsonContent = lua_tostring( L, -1 );
}
Rtt::String s( "window.dispatchEvent(new CustomEvent('" );
s.Append( typeS.getUTF8() );
s.Append( "', {detail: " );
s.Append( jsonContent );
s.Append( "}));" );
view->InjectJSCode( s.GetString() );

lua_pop( L, 1 );
}
lua_pop( L, 1 );
}

void
JavaToNativeBridge::WebViewClosed( JNIEnv * env, int id )
{
Expand Down
1 change: 1 addition & 0 deletions platform/android/ndk/jni/JavaToNativeBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class JavaToNativeBridge
void WebViewFinishedLoadUrl( JNIEnv * env, int id, jstring url );
void WebViewDidFailLoadUrl( JNIEnv * env, int id, jstring url, jstring msg, int code );
void WebViewHistoryUpdated( JNIEnv * env, int id, jboolean canGoBack, jboolean canGoForward );
void WebViewJSInterfaceCommonEvent( JNIEnv * env, int id, jstring type, jstring data, jboolean noResult );
void WebViewClosed( JNIEnv * env, int id );
void AdsRequestEvent(bool isError);
void ImagePickerEvent(JNIEnv *env, jstring selectedImageFileName);
Expand Down
6 changes: 6 additions & 0 deletions platform/android/ndk/jni/JavaToNativeShim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,12 @@ Java_com_ansca_corona_JavaToNativeShim_nativeWebViewHistoryUpdated(JNIEnv * env,
JavaToNativeBridgeFromMemoryAddress(bridgeAddress)->WebViewHistoryUpdated( env, id, canGoBack, canGoForward );
}

JNIEXPORT void JNICALL
Java_com_ansca_corona_JavaToNativeShim_nativeWebViewJSInterfaceCommonEvent(JNIEnv * env, jclass cd, jlong bridgeAddress, jint id, jstring type, jstring data, jboolean noResult)
{
JavaToNativeBridgeFromMemoryAddress(bridgeAddress)->WebViewJSInterfaceCommonEvent( env, id, type, data, noResult );
}

JNIEXPORT void JNICALL
Java_com_ansca_corona_JavaToNativeShim_nativeWebViewClosed(JNIEnv * env, jclass cd, jlong bridgeAddress, jint id)
{
Expand Down
25 changes: 25 additions & 0 deletions platform/android/ndk/jni/NativeToJavaBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3107,6 +3107,31 @@ NativeToJavaBridge::WebViewRequestDeleteCookies( int id )
HandleJavaException();
}

void
NativeToJavaBridge::WebViewRequestInjectJS( int id, const char * jsCode )
{
NativeTrace trace( "NativeToJavaBridge::WebViewRequestInjectJS" );

jclassInstance bridge( GetJNIEnv(), kNativeToJavaBridge );

if ( bridge.isValid() )
{
jmethodID mid = bridge.getEnv()->GetStaticMethodID( bridge.getClass(),
"callWebViewInjectJS", "(Lcom/ansca/corona/CoronaRuntime;ILjava/lang/String;)V" );

if ( mid != NULL )
{
jstringParam textJ( bridge.getEnv(), jsCode );
if ( textJ.isValid() )
{
bridge.getEnv()->CallStaticVoidMethod( bridge.getClass(), mid, fCoronaRuntime, id, textJ.getValue() );
HandleJavaException();
}
}
}
}


void
NativeToJavaBridge::VideoViewCreate(
int id, int left, int top, int width, int height)
Expand Down
1 change: 1 addition & 0 deletions platform/android/ndk/jni/NativeToJavaBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ class NativeToJavaBridge
void WebViewRequestGoBack( int id );
void WebViewRequestGoForward( int id );
void WebViewRequestDeleteCookies( int id );
void WebViewRequestInjectJS( int id, const char * jsCode );
bool WebPopupShouldLoadUrl( int id, const char * url );
bool WebPopupDidFailLoadUrl( int id, const char * url, const char * msg, int code );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,14 @@ JNIEXPORT void JNICALL Java_com_ansca_corona_JavaToNativeShim_nativeWebViewDidFa
JNIEXPORT void JNICALL Java_com_ansca_corona_JavaToNativeShim_nativeWebViewHistoryUpdated
(JNIEnv *, jclass, jlong, jint, jboolean, jboolean);

/*
* Class: com_ansca_corona_JavaToNativeShim
* Method: nativeWebViewJSInterfaceCommonEvent
* Signature: (ILjava/lang/String;Ljava/lang/String;I)V
*/
JNIEXPORT void JNICALL Java_com_ansca_corona_JavaToNativeShim_nativeWebViewJSInterfaceCommonEvent
(JNIEnv *, jclass, jlong, jint, jstring, jstring, jboolean);

/*
* Class: com_ansca_corona_JavaToNativeShim
* Method: nativeWebViewShouldLoadUrl
Expand Down
Loading