Skip to content
This repository was archived by the owner on Sep 7, 2021. It is now read-only.

Commit c0a2d80

Browse files
committed
[[ libbrowser ]] Add actions to libbrowser API - loadHTMLtext, stop, reload
This patch extends the libbrowser API by adding the 'loadHTMLText', 'stop', and 'reload' actions.
1 parent fbc44bd commit c0a2d80

File tree

9 files changed

+135
-28
lines changed

9 files changed

+135
-28
lines changed

libbrowser/include/libbrowser.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ class MCBrowser : public MCBrowserRefCounted
185185
virtual bool GoBack() = 0;
186186
virtual bool GoForward() = 0;
187187
virtual bool GoToURL(const char *p_url) = 0;
188+
virtual bool LoadHTMLText(const char *p_htmltext, const char *p_base_url) = 0;
189+
virtual bool StopLoading() = 0;
190+
virtual bool Reload() = 0;
188191
virtual bool EvaluateJavaScript(const char *p_script, char *&r_result) = 0;
189192
};
190193

@@ -357,6 +360,9 @@ MC_BROWSER_DLLEXPORT bool MCBrowserSetIntegerProperty(MCBrowserRef p_browser, MC
357360
MC_BROWSER_DLLEXPORT bool MCBrowserGoBack(MCBrowserRef p_browser);
358361
MC_BROWSER_DLLEXPORT bool MCBrowserGoForward(MCBrowserRef p_browser);
359362
MC_BROWSER_DLLEXPORT bool MCBrowserGoToURL(MCBrowserRef p_browser, const char *p_url);
363+
MC_BROWSER_DLLEXPORT bool MCBrowserLoadHTMLText(MCBrowserRef p_browser, const char *p_htmltext, const char *p_baseurl);
364+
MC_BROWSER_DLLEXPORT bool MCBrowserStopLoading(MCBrowserRef p_browser);
365+
MC_BROWSER_DLLEXPORT bool MCBrowserReload(MCBrowserRef p_browser);
360366
MC_BROWSER_DLLEXPORT bool MCBrowserEvaluateJavaScript(MCBrowserRef p_browser, const char *p_script, char *&r_result);
361367

362368
enum MCBrowserNavigationEventType

libbrowser/src/libbrowser.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,33 @@ bool MCBrowserGoToURL(MCBrowserRef p_browser, const char *p_url)
574574
return p_browser->GoToURL(p_url);
575575
}
576576

577+
MC_BROWSER_DLLEXPORT_DEF
578+
bool MCBrowserLoadHTMLText(MCBrowserRef p_browser, const char *p_htmltext, const char *p_baseurl)
579+
{
580+
if (p_browser == nil)
581+
return false;
582+
583+
return p_browser->LoadHTMLText(p_htmltext, p_baseurl);
584+
}
585+
586+
MC_BROWSER_DLLEXPORT_DEF
587+
bool MCBrowserStopLoading(MCBrowserRef p_browser)
588+
{
589+
if (p_browser == nil)
590+
return false;
591+
592+
return p_browser->StopLoading();
593+
}
594+
595+
MC_BROWSER_DLLEXPORT_DEF
596+
bool MCBrowserReload(MCBrowserRef p_browser)
597+
{
598+
if (p_browser == nil)
599+
return false;
600+
601+
return p_browser->Reload();
602+
}
603+
577604
MC_BROWSER_DLLEXPORT_DEF
578605
bool MCBrowserEvaluateJavaScript(MCBrowserRef p_browser, const char *p_script, char *&r_result)
579606
{

libbrowser/src/libbrowser_android.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,25 @@ class MCAndroidWebViewBrowser : public MCBrowserBase
730730

731731
virtual bool GoToURL(const char *p_url)
732732
{
733-
MCAndroidObjectRemoteCall(m_view, "setUrl", "vs", nil, p_url);
733+
MCAndroidObjectRemoteCall(m_view, "setUrl", "vt", nil, p_url);
734+
return true;
735+
}
736+
737+
virtual bool LoadHTMLText(const char *p_htmltext, const char *p_base_url)
738+
{
739+
MCAndroidObjectRemoteCall(m_view, "loadHtml", "vtt", nil, p_base_url, p_htmltext);
740+
return true;
741+
}
742+
743+
virtual bool StopLoading(void)
744+
{
745+
MCAndroidObjectRemoteCall(m_view, "stopLoading", "v", nil);
746+
return true;
747+
}
748+
749+
virtual bool Reload(void)
750+
{
751+
MCAndroidObjectRemoteCall(m_view, "reload", "v", nil);
734752
return true;
735753
}
736754

@@ -739,7 +757,7 @@ class MCAndroidWebViewBrowser : public MCBrowserBase
739757
if (m_js_tag != nil)
740758
return false;
741759

742-
MCAndroidObjectRemoteCall(m_view, "executeJavaScript", "ss", &m_js_tag, p_script);
760+
MCAndroidObjectRemoteCall(m_view, "executeJavaScript", "tt", &m_js_tag, p_script);
743761

744762
// wait for result, timeout after 30 seconds
745763
double t_current_time = MCBrowserTimeInSeconds();
@@ -809,7 +827,7 @@ class MCAndroidWebViewBrowser : public MCBrowserBase
809827
char *t_url;
810828
t_url = nil;
811829

812-
MCAndroidObjectRemoteCall(m_view, "getUrl", "s", &t_url);
830+
MCAndroidObjectRemoteCall(m_view, "getUrl", "t", &t_url);
813831
if (t_url == nil)
814832
return MCCStringClone("", r_utf8_string);
815833

@@ -824,15 +842,14 @@ class MCAndroidWebViewBrowser : public MCBrowserBase
824842

825843
bool SetHTMLText(const char *p_utf8_string)
826844
{
827-
MCAndroidObjectRemoteCall(m_view, "loadHtml", "vst", nil, LIBBROWSER_DUMMY_URL, p_utf8_string);
828-
return true;
845+
return LoadHTMLText(p_utf8_string, LIBBROWSER_DUMMY_URL);
829846
}
830847

831848
bool GetUserAgent(char *&r_useragent)
832849
{
833850
char *t_useragent;
834851
t_useragent = nil;
835-
MCAndroidObjectRemoteCall(m_view, "getUserAgent", "s", &t_useragent);
852+
MCAndroidObjectRemoteCall(m_view, "getUserAgent", "t", &t_useragent);
836853

837854
if (t_useragent == nil)
838855
return false;
@@ -843,15 +860,15 @@ class MCAndroidWebViewBrowser : public MCBrowserBase
843860

844861
bool SetUserAgent(const char *p_useragent)
845862
{
846-
MCAndroidObjectRemoteCall(m_view, "setUserAgent", "vs", nil, p_useragent);
863+
MCAndroidObjectRemoteCall(m_view, "setUserAgent", "vt", nil, p_useragent);
847864
return true;
848865
}
849866

850867
bool GetJavaScriptHandlers(char *&r_js_handlers)
851868
{
852869
char *t_handlers;
853870
t_handlers = nil;
854-
MCAndroidObjectRemoteCall(m_view, "getJavaScriptHandlers", "s", &t_handlers);
871+
MCAndroidObjectRemoteCall(m_view, "getJavaScriptHandlers", "t", &t_handlers);
855872

856873
if (t_handlers == nil)
857874
return false;
@@ -862,7 +879,7 @@ class MCAndroidWebViewBrowser : public MCBrowserBase
862879

863880
bool SetJavaScriptHandlers(const char *p_js_handlers)
864881
{
865-
MCAndroidObjectRemoteCall(m_view, "setJavaScriptHandlers", "vs", nil, p_js_handlers);
882+
MCAndroidObjectRemoteCall(m_view, "setJavaScriptHandlers", "vt", nil, p_js_handlers);
866883
return true;
867884
}
868885

libbrowser/src/libbrowser_cef.cpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,20 +1572,29 @@ char *MCCefBrowserBase::GetSource(void)
15721572
return t_src_str;
15731573
}
15741574

1575-
void MCCefBrowserBase::SetSource(const char *p_source)
1575+
bool MCCefBrowserBase::LoadHTMLText(const char *p_htmltext, const char *p_base_url)
15761576
{
15771577
// IM-2014-06-25: [[ Bug 12701 ]] CEF will crash if given an empty source string,
15781578
// so replace here with the source of an empty page :)
1579-
if (p_source == nil || MCCStringLength(p_source) == 0)
1580-
p_source = "<html><head></head><body></body></html>";
1579+
if (p_htmltext == nil || MCCStringLength(p_htmltext) == 0)
1580+
p_htmltext = "<html><head></head><body></body></html>";
15811581

1582-
CefString t_source;
1583-
/* UNCHECKED */ MCCefStringFromUtf8String(p_source, t_source);
1582+
CefString t_htmltext;
1583+
if (!MCCefStringFromUtf8String(p_htmltext, t_htmltext))
1584+
return false;
15841585

1585-
// LoadString requires a valid url
1586-
CefString t_url(CEF_DUMMY_URL);
1586+
CefString t_base_url;
1587+
if (!MCCefStringFromUtf8String(p_base_url, t_base_url))
1588+
return false;
1589+
1590+
m_browser->GetMainFrame()->LoadString(t_htmltext, t_base_url);
15871591

1588-
m_browser->GetMainFrame()->LoadString(t_source, t_url);
1592+
return true;
1593+
}
1594+
1595+
void MCCefBrowserBase::SetSource(const char *p_source)
1596+
{
1597+
/* UNCHECKED */ LoadHTMLText(p_source, CEF_DUMMY_URL);
15891598
}
15901599

15911600
#define MCCEF_VERTICAL_OVERFLOW_PROPERTY "document.body.style.overflowY"
@@ -1784,6 +1793,18 @@ bool MCCefBrowserBase::GoToURL(const char *p_url)
17841793
return true;
17851794
}
17861795

1796+
bool MCCefBrowserBase::StopLoading(void)
1797+
{
1798+
m_browser->StopLoad();
1799+
return true;
1800+
}
1801+
1802+
bool MCCefBrowserBase::Reload(void)
1803+
{
1804+
m_browser->Reload();
1805+
return true;
1806+
}
1807+
17871808
bool MCCefBrowserBase::GoBack(void)
17881809
{
17891810
m_browser->GoBack();

libbrowser/src/libbrowser_cef.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class MCCefBrowserBase : public MCBrowserBase
5959
virtual bool GoBack();
6060
virtual bool GoForward();
6161
virtual bool GoToURL(const char *p_url);
62+
virtual bool LoadHTMLText(const char *p_htmltext, const char *p_base_url);
63+
virtual bool StopLoading();
64+
virtual bool Reload();
6265
virtual bool EvaluateJavaScript(const char *p_script, char *&r_result);
6366

6467
virtual void Destroy();

libbrowser/src/libbrowser_osx_webview.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class MCWebViewBrowser : public MCBrowserBase
4848
virtual bool GoBack();
4949
virtual bool GoForward();
5050
virtual bool GoToURL(const char *p_url);
51+
virtual bool LoadHTMLText(const char *p_htmltext, const char *p_base_url);
52+
virtual bool StopLoading();
53+
virtual bool Reload();
5154
virtual bool EvaluateJavaScript(const char *p_script, char *&r_result);
5255

5356
void SyncJavaScriptHandlers();
@@ -75,10 +78,7 @@ class MCWebViewBrowser : public MCBrowserBase
7578
bool SetAllowUserInteraction(bool p_value);
7679

7780
// Browser-specific actions
78-
bool ExecReload();
79-
bool ExecStop();
8081
bool ExecExecute(const char * p_script, char *&r_result);
81-
bool ExecLoad(const char *p_url, const char *p_html);
8282

8383
//UIScrollView *GetScrollView(void);
8484

libbrowser/src/libbrowser_osx_webview.mm

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ inline void MCBrowserRunBlockOnMainFiber(void (^p_block)(void))
474474

475475
bool MCWebViewBrowser::SetHTMLText(const char *p_htmltext)
476476
{
477-
return ExecLoad(LIBBROWSER_DUMMY_URL, p_htmltext);
477+
return LoadHTMLText(p_htmltext, LIBBROWSER_DUMMY_URL);
478478
}
479479

480480
bool MCWebViewBrowser::GetUserAgent(char*& r_user_agent)
@@ -812,8 +812,7 @@ + (BOOL)isSelectorExcludedFromWebScript:(SEL)selector
812812
return true;
813813
}
814814

815-
// Browser-specific actions
816-
bool MCWebViewBrowser::ExecStop()
815+
bool MCWebViewBrowser::StopLoading()
817816
{
818817
WebView *t_view;
819818
if (!GetView(t_view))
@@ -852,7 +851,7 @@ + (BOOL)isSelectorExcludedFromWebScript:(SEL)selector
852851
return true;
853852
}
854853

855-
bool MCWebViewBrowser::ExecReload()
854+
bool MCWebViewBrowser::Reload()
856855
{
857856
WebView *t_view;
858857
if (!GetView(t_view))
@@ -882,7 +881,7 @@ + (BOOL)isSelectorExcludedFromWebScript:(SEL)selector
882881
return MCCStringClone([t_result cStringUsingEncoding:NSUTF8StringEncoding], r_result);
883882
}
884883

885-
bool MCWebViewBrowser::ExecLoad(const char *p_url, const char *p_html)
884+
bool MCWebViewBrowser::LoadHTMLText(const char *p_html, const char *p_url)
886885
{
887886
WebView *t_view;
888887
if (!GetView(t_view))

libbrowser/src/libbrowser_wkwebview.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class MCWKWebViewBrowser : public MCBrowserBase
4747
virtual bool GoBack();
4848
virtual bool GoForward();
4949
virtual bool GoToURL(const char *p_url);
50+
virtual bool LoadHTMLText(const char *p_htmltext, const char *p_base_url);
51+
virtual bool StopLoading();
52+
virtual bool Reload();
5053
virtual bool EvaluateJavaScript(const char *p_script, char *&r_result);
5154

5255
void SyncJavaScriptHandlers();

libbrowser/src/libbrowser_wkwebview.mm

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,15 +312,15 @@ MCBrowserDataDetectorType MCBrowserDataDetectorTypeFromWKDataDetectorTypes(WKDat
312312
return EvaluateJavaScript("document.documentElement.outerHTML", r_htmltext);
313313
}
314314

315-
bool MCWKWebViewBrowser::SetHTMLText(const char *p_htmltext)
315+
bool MCWKWebViewBrowser::LoadHTMLText(const char *p_htmltext, const char *p_baseurl)
316316
{
317317
WKWebView *t_view;
318318
if (!GetWebView(t_view))
319319
return false;
320320

321321
MCBrowserRunBlockOnMainFiber(^{
322322
[m_delegate setPendingRequest: true];
323-
[t_view loadHTMLString: [NSString stringWithUTF8String: p_htmltext] baseURL: [NSURL URLWithString: [NSString stringWithUTF8String: LIBBROWSER_DUMMY_URL]]];
323+
[t_view loadHTMLString: [NSString stringWithUTF8String: p_htmltext] baseURL: [NSURL URLWithString: [NSString stringWithUTF8String: p_baseurl]]];
324324
});
325325

326326
/* UNCHECKED */ MCBrowserCStringAssignCopy(m_htmltext, p_htmltext);
@@ -329,6 +329,11 @@ MCBrowserDataDetectorType MCBrowserDataDetectorTypeFromWKDataDetectorTypes(WKDat
329329
return true;
330330
}
331331

332+
bool MCWKWebViewBrowser::SetHTMLText(const char *p_htmltext)
333+
{
334+
return LoadHTMLText(p_htmltext, LIBBROWSER_DUMMY_URL);
335+
}
336+
332337
////////////////////////////////////////////////////////////////////////////////
333338

334339
bool MCWKWebViewBrowser::GetJavaScriptHandlers(char *&r_handlers)
@@ -879,6 +884,32 @@ - (void)userContentController:(WKUserContentController *)userContentController d
879884
return true;
880885
}
881886

887+
bool MCWKWebViewBrowser::StopLoading()
888+
{
889+
WKWebView *t_view;
890+
if (!GetWebView(t_view))
891+
return false;
892+
893+
MCBrowserRunBlockOnMainFiber(^{
894+
[t_view stopLoading];
895+
});
896+
897+
return true;
898+
}
899+
900+
bool MCWKWebViewBrowser::Reload()
901+
{
902+
WKWebView *t_view;
903+
if (!GetWebView(t_view))
904+
return false;
905+
906+
MCBrowserRunBlockOnMainFiber(^{
907+
[t_view reload];
908+
});
909+
910+
return true;
911+
}
912+
882913
bool MCWKWebViewBrowser::GoForward()
883914
{
884915
WKWebView *t_view;
@@ -1154,7 +1185,7 @@ void _SyncEvaluateJavaScript(WKWebView *p_view, NSString *p_javascript, id &r_re
11541185
m_view.userInteractionEnabled = t_allow_user_interaction;
11551186

11561187
if (!MCCStringIsEmpty(m_htmltext))
1157-
/* UNCHECKED */ SetHTMLText(m_htmltext);
1188+
/* UNCHECKED */ LoadHTMLText(m_htmltext, m_url);
11581189
else if (!MCCStringIsEmpty(m_url))
11591190
GoToURL(m_url);
11601191
}

0 commit comments

Comments
 (0)