/
CClientWebBrowser.cpp
270 lines (222 loc) · 8.57 KB
/
CClientWebBrowser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* (Shared logic for modifications)
* LICENSE: See LICENSE in the top level directory
* FILE: mods/shared_logic/CClientWebBrowser.cpp
* PURPOSE: ClientEntity webbrowser tab class
*
*****************************************************************************/
#include "StdInc.h"
#include "CClientWebBrowser.h"
CClientWebBrowser::CClientWebBrowser ( CClientManager* pManager, ElementID ID, CWebBrowserItem* pWebBrowserItem, bool bLocal, bool bTransparent )
: ClassInit ( this ), CClientTexture ( pManager, ID, pWebBrowserItem )
{
m_pResource = nullptr;
m_pManager = pManager;
SetTypeName ( "webbrowser" );
// Create the web view
m_pWebView = g_pCore->GetWebCore ()->CreateWebView ( pWebBrowserItem->m_uiSizeX, pWebBrowserItem->m_uiSizeY, bLocal, pWebBrowserItem, bTransparent );
// Set events interface
m_pWebView->SetWebBrowserEvents ( this );
// Initialise the webview after setting the events interface
m_pWebView->Initialise ();
}
CClientWebBrowser::~CClientWebBrowser ()
{
g_pCore->GetWebCore()->DestroyWebView ( m_pWebView );
m_pWebView = nullptr;
// Unlink from tree
Unlink ();
}
void CClientWebBrowser::Unlink ()
{
// Mark as being destroyed (DirectX surfaces won't be available anymore)
if ( m_pWebView )
m_pWebView->SetBeingDestroyed ( true );
CClientRenderElement::Unlink ();
}
bool CClientWebBrowser::IsLoading ()
{
return m_pWebView->IsLoading ();
}
bool CClientWebBrowser::LoadURL ( const SString& strURL, bool bFilterEnabled, const SString& strPostData, bool bURLEncoded )
{
return m_pWebView->LoadURL ( strURL, bFilterEnabled, strPostData, bURLEncoded );
}
void CClientWebBrowser::GetTitle ( SString& outPageTitle )
{
m_pWebView->GetTitle ( outPageTitle );
}
void CClientWebBrowser::GetURL ( SString& outURL )
{
m_pWebView->GetURL ( outURL );
}
void CClientWebBrowser::SetRenderingPaused ( bool bPaused )
{
m_pWebView->SetRenderingPaused ( bPaused );
}
void CClientWebBrowser::Focus ()
{
m_pWebView->Focus ();
}
bool CClientWebBrowser::ExecuteJavascript ( const SString& strJavascriptCode )
{
// Don't allow javascript code execution on remote websites
if ( !m_pWebView->IsLocal () )
return false;
m_pWebView->ExecuteJavascript ( strJavascriptCode );
return true;
}
bool CClientWebBrowser::SetProperty ( const SString& strKey, const SString& strValue )
{
return m_pWebView->SetProperty ( strKey, strValue );
}
bool CClientWebBrowser::GetProperty ( const SString& strKey, SString& outValue )
{
return m_pWebView->GetProperty ( strKey, outValue );
}
void CClientWebBrowser::InjectMouseMove ( int iPosX, int iPosY )
{
m_pWebView->InjectMouseMove ( iPosX, iPosY );
}
void CClientWebBrowser::InjectMouseDown ( eWebBrowserMouseButton mouseButton )
{
m_pWebView->InjectMouseDown ( mouseButton );
}
void CClientWebBrowser::InjectMouseUp ( eWebBrowserMouseButton mouseButton )
{
m_pWebView->InjectMouseUp ( mouseButton );
}
void CClientWebBrowser::InjectMouseWheel ( int iScrollVert, int iScrollHorz )
{
m_pWebView->InjectMouseWheel ( iScrollVert, iScrollHorz );
}
bool CClientWebBrowser::IsLocal ()
{
return m_pWebView->IsLocal ();
}
void CClientWebBrowser::SetTempURL ( const SString& strTempURL )
{
m_pWebView->SetTempURL ( strTempURL );
}
float CClientWebBrowser::GetAudioVolume ()
{
return m_pWebView->GetAudioVolume ();
}
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 //
// //
////////////////////////////////////////////////////////////////////////////
void CClientWebBrowser::Events_OnCreated ()
{
CLuaArguments Arguments;
CallEvent ( "onClientBrowserCreated", Arguments, false );
}
void CClientWebBrowser::Events_OnLoadingStart ( const SString& strURL, bool bMainFrame )
{
CLuaArguments Arguments;
Arguments.PushString ( strURL );
Arguments.PushBoolean ( bMainFrame );
CallEvent ( "onClientBrowserLoadingStart", Arguments, false );
}
void CClientWebBrowser::Events_OnDocumentReady ( const SString& strURL )
{
CLuaArguments Arguments;
Arguments.PushString ( strURL );
CallEvent ( "onClientBrowserDocumentReady", Arguments, false );
}
void CClientWebBrowser::Events_OnLoadingFailed ( const SString& strURL, int errorCode, const SString& errorDescription )
{
CLuaArguments Arguments;
Arguments.PushString ( strURL );
Arguments.PushNumber ( errorCode );
Arguments.PushString ( errorDescription );
CallEvent ( "onClientBrowserLoadingFailed", Arguments, false );
}
void CClientWebBrowser::Events_OnNavigate ( const SString& strURL, bool bIsBlocked )
{
CLuaArguments Arguments;
Arguments.PushString ( strURL );
Arguments.PushBoolean ( bIsBlocked );
CallEvent ( "onClientBrowserNavigate", Arguments, false );
}
void CClientWebBrowser::Events_OnPopup ( const SString& strTargetURL, const SString& strOpenerURL )
{
CLuaArguments Arguments;
Arguments.PushString ( strTargetURL );
Arguments.PushString ( strOpenerURL );
CallEvent ( "onClientBrowserPopup", Arguments, false );
}
void CClientWebBrowser::Events_OnChangeCursor ( unsigned char ucCursor )
{
CLuaArguments Arguments;
Arguments.PushNumber ( ucCursor );
CallEvent ( "onClientBrowserCursorChange", Arguments, false );
}
void CClientWebBrowser::Events_OnTriggerEvent ( const SString& strEventName, const std::vector<std::string>& arguments, bool bIsServer )
{
CLuaArguments Arguments;
for ( std::vector<std::string>::const_iterator iter = arguments.begin (); iter != arguments.end (); ++iter )
{
Arguments.PushString ( *iter );
}
if ( bIsServer )
{
CStaticFunctionDefinitions::TriggerServerEvent ( strEventName, *g_pClientGame->GetRootEntity (), Arguments );
}
else
{
bool bWasCancelled;
CStaticFunctionDefinitions::TriggerEvent ( strEventName, *this, Arguments, bWasCancelled );
}
}
void CClientWebBrowser::Events_OnTooltip ( const SString& strTooltip )
{
CLuaArguments Arguments;
Arguments.PushString ( strTooltip );
CallEvent ( "onClientBrowserTooltip", Arguments, false );
}
void CClientWebBrowser::Events_OnInputFocusChanged ( bool bGainedFocus )
{
CLuaArguments Arguments;
Arguments.PushBoolean ( bGainedFocus );
CallEvent ( "onClientBrowserInputFocusChanged", Arguments, false );
}
bool CClientWebBrowser::Events_OnResourcePathCheck ( SString& strURL )
{
// ATTENTION: This method is called within a secondary thread so be sure to use only thread safe functions
// If no resource is set, we are allowed to use the requested file
if ( !m_pResource )
return true;
CResource* pTempResource = m_pResource; // Make a copy to ignore a changed resource
if ( CResourceManager::ParseResourcePathInput ( strURL, pTempResource, strURL ) )
return true;
return false;
}
void CClientWebBrowser::Events_OnResourceBlocked ( const SString& strURL, const SString& strDomain, unsigned char reason )
{
CLuaArguments Arguments;
Arguments.PushString ( strURL );
Arguments.PushString ( strDomain );
Arguments.PushNumber ( reason );
CallEvent ( "onClientBrowserResourceBlocked", Arguments, false );
}
CClientGUIWebBrowser::CClientGUIWebBrowser ( bool isLocal, bool isTransparent, uint width, uint height, CClientManager* pManager, CLuaMain* pLuaMain, CGUIElement* pCGUIElement, ElementID ID )
: CClientGUIElement ( pManager, pLuaMain, pCGUIElement, ID )
{
m_pManager = pManager;
m_pBrowser = g_pClientGame->GetManager ()->GetRenderElementManager ()->CreateWebBrowser ( width, height, isLocal, isTransparent );
m_pBrowser->SetParent ( this ); // m_pBrowser gets deleted automatically by the element tree logic
// Set our owner resource
m_pBrowser->SetResource ( pLuaMain->GetResource () );
}