Skip to content

Commit

Permalink
Added callback to requestBrowserDomains
Browse files Browse the repository at this point in the history
  • Loading branch information
jushar committed Aug 26, 2015
1 parent 0ef0c3d commit 031ebcb
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 13 deletions.
10 changes: 8 additions & 2 deletions MTA10/core/CWebCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ void CWebCore::AddBlockedPage ( const SString& strURL, eWebFilterType filterType
m_Whitelist[strURL] = std::pair<bool, eWebFilterType> ( false, filterType );
}

void CWebCore::RequestPages ( const std::vector<SString>& pages )
void CWebCore::RequestPages ( const std::vector<SString>& pages, WebRequestCallback* pCallback )
{
// Add to pending pages queue
bool bNewItem = false;
Expand All @@ -323,9 +323,15 @@ void CWebCore::RequestPages ( const std::vector<SString>& pages )
m_pRequestsGUI = new CWebsiteRequests;

// Set pending requests memo content and show the window
m_pRequestsGUI->SetPendingRequests ( m_PendingRequests );
m_pRequestsGUI->SetPendingRequests ( m_PendingRequests, pCallback );
m_pRequestsGUI->Show ();
}
else
{
// Call callback immediately if nothing has changed (all entries are most likely already on the whitelist)
// There is still the possibility that all websites are blacklisted; this is not the usual case tho, so ignore for now (TODO)
(*pCallback)( true, pages );
}
}

void CWebCore::AllowPendingPages ( bool bRemember )
Expand Down
3 changes: 2 additions & 1 deletion MTA10/core/CWebCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ class CWebCore : public CWebCoreInterface
void InitialiseWhiteAndBlacklist ( bool bAddHardcoded = true, bool bAddDynamic = true );
void AddAllowedPage ( const SString& strURL, eWebFilterType filterType );
void AddBlockedPage ( const SString& strURL, eWebFilterType filterType );
void RequestPages ( const std::vector<SString>& pages );
void RequestPages ( const std::vector<SString>& pages, WebRequestCallback* pCallback = nullptr );
void AllowPendingPages ( bool bRemember );
void DenyPendingPages ();
std::vector<SString>& GetPendingRequests () { return m_PendingRequests; };
bool IsRequestsGUIVisible();

inline bool IsTestModeEnabled () { return m_bTestmodeEnabled; };
Expand Down
20 changes: 19 additions & 1 deletion MTA10/core/CWebsiteRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ bool CWebsiteRequests::IsVisible ()
return m_pWindow->IsVisible ();
}

void CWebsiteRequests::SetPendingRequests ( const std::vector<SString>& pendingRequests )
void CWebsiteRequests::SetPendingRequests ( const std::vector<SString>& pendingRequests, WebRequestCallback* pCallback )
{
SString content = "";
for ( std::vector<SString>::const_iterator iter = pendingRequests.begin(); iter != pendingRequests.end(); ++iter )
Expand All @@ -95,23 +95,41 @@ void CWebsiteRequests::SetPendingRequests ( const std::vector<SString>& pendingR

// Apply text
m_pAddressMemo->SetText ( content.c_str() );
if ( pCallback )
m_Callbacks.push_back ( *pCallback );
}

void CWebsiteRequests::Clear ()
{
m_pAddressMemo->SetText ("");
m_Callbacks.clear ();
}

void CWebsiteRequests::Callback ( bool bAllow )
{
// Call callbacks and clear list
const auto& pendingRequests = g_pCore->GetWebCore ()->GetPendingRequests ();
for ( auto&& callback : m_Callbacks )
{
callback ( bAllow, pendingRequests );
}
m_Callbacks.clear ();
}

bool CWebsiteRequests::OnAllowButtonClick ( CGUIElement* pElement )
{
Callback ( true );
g_pCore->GetWebCore ()->AllowPendingPages ( m_pCheckRemember->GetSelected () );
Hide ();

return true;
}

bool CWebsiteRequests::OnDenyButtonClick ( CGUIElement* pElement )
{
Callback ( false );
g_pCore->GetWebCore ()->DenyPendingPages ();
Hide ();

return true;
}
8 changes: 5 additions & 3 deletions MTA10/core/CWebsiteRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CWebsiteRequests
void Hide ();
bool IsVisible ();

void SetPendingRequests ( const std::vector<SString>& pendingRequests );
void SetPendingRequests ( const std::vector<SString>& pendingRequests, WebRequestCallback* pCallback = nullptr );
void Clear ();

protected:
Expand All @@ -39,10 +39,12 @@ class CWebsiteRequests
CGUICheckBox* m_pCheckRemember;
CGUIButton* m_pButtonAllow;
CGUIButton* m_pButtonDeny;
std::list<WebRequestCallback> m_Callbacks;

private:
bool OnAllowButtonClick(CGUIElement* pElement);
bool OnDenyButtonClick(CGUIElement* pElement);
void Callback ( bool bAllow );
bool OnAllowButtonClick ( CGUIElement* pElement );
bool OnDenyButtonClick ( CGUIElement* pElement );

};

Expand Down
32 changes: 28 additions & 4 deletions MTA10/mods/shared_logic/lua/CLuaFunctionDefs.Browser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ int CLuaFunctionDefs::CreateBrowser ( lua_State* luaVM )

int CLuaFunctionDefs::RequestBrowserDomains ( lua_State* luaVM )
{
// bool requestBrowserDomains ( table domains, bool isURL )
std::vector<SString> pages; bool bIsURL;
// bool requestBrowserDomains ( table domains, bool isURL [, function callback ] )
std::vector<SString> pages; bool bIsURL; CLuaFunctionRef callbackFunction;

CScriptArgReader argStream ( luaVM );
argStream.ReadStringTable ( pages );
argStream.ReadBool ( bIsURL, false );
argStream.ReadFunction ( callbackFunction, LUA_REFNIL );
argStream.ReadFunctionComplete ();

if ( !argStream.HasErrors () )
{
Expand All @@ -73,8 +75,30 @@ int CLuaFunctionDefs::RequestBrowserDomains ( lua_State* luaVM )
}
}

g_pCore->GetWebCore ()->RequestPages ( pages );
// Todo: Add a callback or event to check if the pagerequest dialog was successfully done
WebRequestCallback callback = [=]( bool bAllow, const std::vector<SString>& domains ) {
// Test if luaVM is still available
if ( m_pLuaManager->IsLuaVMValid ( luaVM ) && VERIFY_FUNCTION ( callbackFunction ) )
{
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
if ( !pLuaMain )
return;

CLuaArguments arguments;
arguments.PushBoolean ( bAllow );

CLuaArguments LuaTable;
int i = 0;
for ( const auto& domain : domains )
{
LuaTable.PushNumber ( ++i );
LuaTable.PushString ( domain );
}
arguments.PushTable ( &LuaTable );
arguments.Call ( pLuaMain, callbackFunction );
}
};

g_pCore->GetWebCore ()->RequestPages ( pages, VERIFY_FUNCTION ( callbackFunction ) ? &callback : nullptr );
lua_pushboolean ( luaVM, true );
return 1;
}
Expand Down
2 changes: 2 additions & 0 deletions MTA10/mods/shared_logic/lua/CLuaManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class CLuaManager
void AddToPendingDeleteList ( lua_State* m_luaVM ) { m_PendingDeleteList.push_back ( m_luaVM ); }
void ProcessPendingDeleteList ( void );

bool IsLuaVMValid ( lua_State* luaVM ) { return MapFindRef ( m_VirtualMachineMap, luaVM ) != nullptr; };

CClientGUIManager* m_pGUIManager;

private:
Expand Down
7 changes: 5 additions & 2 deletions MTA10/sdk/core/CWebCoreInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ enum eWebBrowserMouseButton
BROWSER_MOUSEBUTTON_RIGHT = 2
};

using WebRequestCallback = std::function<void(bool, const std::vector<SString>&)>;

class CWebCoreInterface
{
public:
Expand All @@ -58,16 +60,17 @@ class CWebCoreInterface
virtual SString GetDomainFromURL ( const SString& strURL ) = 0;
virtual void ResetFilter ( bool bResetRequestsOnly ) = 0;
//virtual void AddAllowedPage ( const SString& strURL ) = 0;
virtual void RequestPages ( const std::vector<SString>& pages ) = 0;
virtual void RequestPages ( const std::vector<SString>& pages, WebRequestCallback* pCallback = nullptr ) = 0;
virtual void AllowPendingPages ( bool bRemember ) = 0;
virtual void DenyPendingPages () = 0;
virtual std::vector<SString>& GetPendingRequests () = 0;
virtual bool IsRequestsGUIVisible() = 0;

virtual bool IsTestModeEnabled () = 0;
virtual void SetTestModeEnabled ( bool bEnabled ) = 0;

virtual CWebViewInterface* GetFocusedWebView () = 0;
virtual void SetFocusedWebView (CWebView* pWebView) = 0;
virtual void SetFocusedWebView ( CWebView* pWebView ) = 0;
virtual void ProcessInputMessage( UINT uMsg, WPARAM wParam, LPARAM lParam ) = 0;
virtual void ClearTextures () = 0;

Expand Down

0 comments on commit 031ebcb

Please sign in to comment.