Skip to content

Commit

Permalink
Added getBrowserSource
Browse files Browse the repository at this point in the history
Syntax:
bool getBrowserSource ( function callback )
Callback parameters: callback ( string code )
  • Loading branch information
jushar committed Aug 1, 2015
1 parent cfd95d9 commit 3bdd7dc
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 0 deletions.
28 changes: 28 additions & 0 deletions MTA10/core/CWebView.cpp
Expand Up @@ -287,6 +287,34 @@ bool CWebView::SetAudioVolume ( float fVolume )
return true;
}

void CWebView::GetSourceCode ( const std::function<void( const std::string& code )>& callback )
{
class MyStringVisitor : public CefStringVisitor
{
private:
CWebView* webView;
std::function<void( const std::string& )> callback;

public:
MyStringVisitor ( CWebView* webView_, const std::function<void( const std::string& )>& callback_ ) : webView(webView_), callback(callback_) {}

virtual void Visit ( const CefString& code ) override
{
// Limit to 2MiB for now to prevent freezes (TODO: Optimize that and increase later)
if ( code.size () <= 2097152 )
{
// Call callback on main thread
g_pCore->GetWebCore ()->AddEventToEventQueue ( std::bind ( callback, code ), webView, "GetSourceCode_Visit" );
}
}

IMPLEMENT_REFCOUNTING(MyStringVisitor);
};

CefRefPtr<CefStringVisitor> visitor { new MyStringVisitor ( this, callback ) };
m_pWebView->GetMainFrame ()->GetSource ( visitor );
}

bool CWebView::GetFullPathFromLocal ( SString& strPath )
{
return m_pEventsInterface->Events_OnResourcePathCheck ( strPath );
Expand Down
2 changes: 2 additions & 0 deletions MTA10/core/CWebView.h
Expand Up @@ -72,6 +72,8 @@ class CWebView : public CWebViewInterface, private CefClient, private CefRenderH
inline float GetAudioVolume () { return m_fVolume; };
bool SetAudioVolume ( float fVolume );

void GetSourceCode ( const std::function<void( const std::string& code )>& callback );

bool GetFullPathFromLocal ( SString& strPath );

// CefClient methods
Expand Down
5 changes: 5 additions & 0 deletions MTA10/mods/shared_logic/CClientWebBrowser.cpp
Expand Up @@ -136,6 +136,11 @@ bool CClientWebBrowser::SetAudioVolume ( float fVolume )
return m_pWebView->SetAudioVolume ( fVolume );
}

void CClientWebBrowser::GetSourceCode ( const std::function<void( const std::string& code )>& callback )
{
return m_pWebView->GetSourceCode ( callback );
}

////////////////////////////////////////////////////////////////////////////
// //
// CWebBrowserEventsInterface implementation //
Expand Down
2 changes: 2 additions & 0 deletions MTA10/mods/shared_logic/CClientWebBrowser.h
Expand Up @@ -52,6 +52,8 @@ class CClientWebBrowser : public CClientTexture, public CWebBrowserEventsInterfa
float GetAudioVolume ();
bool SetAudioVolume ( float fVolume );

void GetSourceCode ( const std::function<void( const std::string& code )>& callback );


// CWebBrowserEventsInterface implementation
void Events_OnCreated () override;
Expand Down
41 changes: 41 additions & 0 deletions MTA10/mods/shared_logic/lua/CLuaFunctionDefs.Browser.cpp
Expand Up @@ -520,6 +520,47 @@ int CLuaFunctionDefs::GetBrowserSettings ( lua_State* luaVM )
return 1;
}

int CLuaFunctionDefs::GetBrowserSource ( lua_State* luaVM )
{
// bool getBrowserSource ( function callback )
CClientWebBrowser* pWebBrowser; CLuaFunctionRef callbackFunction;

CScriptArgReader argStream ( luaVM );
argStream.ReadUserData ( pWebBrowser );
argStream.ReadFunction ( callbackFunction );
argStream.ReadFunctionComplete ();

if ( !argStream.HasErrors () )
{
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
if ( pLuaMain && VERIFY_FUNCTION ( callbackFunction ) )
{
pWebBrowser->GetSourceCode ( [callbackFunction, pLuaMain]( const std::string& code ) {
/*
This function should not be called when the resource is about to stop as
stopping the resource destroys the browser element and thus cancels the
CefStringVisitor callback class (see CWebView::GetSourceCode::MyStringVisitor)
*/
if ( VERIFY_FUNCTION ( callbackFunction ) )
{
CLuaArguments arguments;
// TODO: Use SCharStringRef/direct string access instead of copying strings around
arguments.PushString ( code );
arguments.Call ( pLuaMain, callbackFunction );
}
});

lua_pushboolean ( luaVM, true );
return 1;
}
}
else
m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

lua_pushboolean ( luaVM, false );
return 1;
}

int CLuaFunctionDefs::GUICreateBrowser ( lua_State* luaVM )
{
// element guiCreateBrowser ( float x, float y, float width, float height, bool isLocal, bool isTransparent, bool relative, [element parent = nil] )
Expand Down
1 change: 1 addition & 0 deletions MTA10/mods/shared_logic/lua/CLuaFunctionDefs.h
Expand Up @@ -1040,6 +1040,7 @@ class CLuaFunctionDefs
LUA_DECLARE ( SetBrowserProperty );
LUA_DECLARE ( GetBrowserProperty );
LUA_DECLARE ( GetBrowserSettings );
LUA_DECLARE ( GetBrowserSource );
LUA_DECLARE ( GUICreateBrowser );
LUA_DECLARE ( GUIGetBrowser );

Expand Down
1 change: 1 addition & 0 deletions MTA10/mods/shared_logic/lua/CLuaManager.cpp
Expand Up @@ -1215,6 +1215,7 @@ void CLuaManager::LoadCFunctions ( void )
CLuaCFunctions::AddFunction ( "setBrowserProperty", CLuaFunctionDefs::SetBrowserProperty );
CLuaCFunctions::AddFunction ( "getBrowserProperty", CLuaFunctionDefs::GetBrowserProperty );
CLuaCFunctions::AddFunction ( "getBrowserSettings", CLuaFunctionDefs::GetBrowserSettings );
CLuaCFunctions::AddFunction ( "getBrowserSource", CLuaFunctionDefs::GetBrowserSource );
CLuaCFunctions::AddFunction ( "guiCreateBrowser", CLuaFunctionDefs::GUICreateBrowser );
CLuaCFunctions::AddFunction ( "guiGetBrowser", CLuaFunctionDefs::GUIGetBrowser );

Expand Down
2 changes: 2 additions & 0 deletions MTA10/sdk/core/CWebViewInterface.h
Expand Up @@ -47,6 +47,8 @@ class CWebViewInterface

virtual float GetAudioVolume () = 0;
virtual bool SetAudioVolume ( float fVolume ) = 0;

virtual void GetSourceCode ( const std::function<void( const std::string& code )>& callback ) = 0;
};

#endif

0 comments on commit 3bdd7dc

Please sign in to comment.