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

Commit 1281162

Browse files
author
Ian Macphail
committed
[[ Bug 22279 ]] Change method of setting page source in CEF libbrowser init
This patch fixes an issue where setting the source of a newly created browser widget would fail by replacing the initial invalid startup url with a valid blank data url, causing libCEF to set up the render process. This avoids a known libCEF bug where LoadSource would have no effect on browsers where the render process has not yet been created.
1 parent 75ba271 commit 1281162

File tree

1 file changed

+55
-10
lines changed

1 file changed

+55
-10
lines changed

libbrowser/src/libbrowser_cef.cpp

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "libbrowser_cef.h"
2222

2323
#include <include/cef_app.h>
24+
#include <include/cef_parser.h>
2425
#include <include/wrapper/cef_scoped_temp_dir.h>
2526
#include <list>
2627
#include <set>
@@ -741,6 +742,15 @@ struct MCCefErrorInfo
741742

742743
class MCCefBrowserClient : public CefClient, CefLifeSpanHandler, CefRequestHandler, /* CefDownloadHandler ,*/ CefLoadHandler, CefContextMenuHandler, CefDragHandler
743744
{
745+
public:
746+
enum PageOrigin
747+
{
748+
kNone,
749+
kSetUrl,
750+
kSetSource,
751+
kBrowse,
752+
};
753+
744754
private:
745755
int m_browser_id;
746756
int m_popup_browser_id = 0;
@@ -755,6 +765,9 @@ class MCCefBrowserClient : public CefClient, CefLifeSpanHandler, CefRequestHandl
755765

756766
CefString m_last_request_url;
757767

768+
PageOrigin m_displayed_page_origin;
769+
PageOrigin m_loading_page_origin;
770+
758771
// Error handling - we need to keep track of url that failed to load in a
759772
// frame so we can send the correct url in onLoadEnd()
760773
void AddLoadErrorFrame(int64_t p_id, const CefString &p_url, const CefString &p_error_msg, CefLoadHandler::ErrorCode p_error_code)
@@ -813,6 +826,7 @@ class MCCefBrowserClient : public CefClient, CefLifeSpanHandler, CefRequestHandl
813826
{
814827
m_owner = p_owner;
815828
m_browser_id = 0;
829+
m_displayed_page_origin = m_loading_page_origin = PageOrigin::kNone;
816830
}
817831

818832
// IM-2014-07-21: [[ Bug 12296 ]] Method to allow owner to notify client of its deletion
@@ -848,6 +862,16 @@ class MCCefBrowserClient : public CefClient, CefLifeSpanHandler, CefRequestHandl
848862
return t_iter != m_ignore_urls.end();
849863
}
850864

865+
void SetLoadingPageOrigin(PageOrigin p_origin)
866+
{
867+
m_loading_page_origin = p_origin;
868+
}
869+
870+
PageOrigin GetDisplayedPageOrigin()
871+
{
872+
return m_displayed_page_origin;
873+
}
874+
851875
MCCefMessageResult &GetMessageResult()
852876
{
853877
return m_message_result;
@@ -1012,6 +1036,9 @@ class MCCefBrowserClient : public CefClient, CefLifeSpanHandler, CefRequestHandl
10121036
CefString t_url;
10131037
t_url = p_request->GetURL();
10141038

1039+
if (p_user_gesture)
1040+
SetLoadingPageOrigin(PageOrigin::kBrowse);
1041+
10151042
if (IgnoreUrl(t_url))
10161043
return false;
10171044

@@ -1142,16 +1169,19 @@ class MCCefBrowserClient : public CefClient, CefLifeSpanHandler, CefRequestHandl
11421169
if (!t_is_error)
11431170
t_url = p_frame->GetURL();
11441171

1145-
if (IgnoreUrl(t_url))
1172+
bool t_frame;
1173+
t_frame = !p_frame->IsMain();
1174+
1175+
if (!t_frame && !t_is_error)
1176+
m_displayed_page_origin = m_loading_page_origin;
1177+
1178+
if ((!t_frame && m_loading_page_origin==PageOrigin::kSetSource) || IgnoreUrl(t_url))
11461179
return;
11471180

11481181
char *t_url_str;
11491182
t_url_str = nil;
11501183
/* UNCHECKED */ MCCefStringToUtf8String(t_url, t_url_str);
11511184

1152-
bool t_frame;
1153-
t_frame = !p_frame->IsMain();
1154-
11551185
if (!t_is_error)
11561186
{
11571187
if (!t_frame)
@@ -1179,16 +1209,16 @@ class MCCefBrowserClient : public CefClient, CefLifeSpanHandler, CefRequestHandl
11791209
if (!t_is_error)
11801210
t_url = p_frame->GetURL();
11811211

1182-
if (IgnoreUrl(t_url))
1212+
bool t_frame;
1213+
t_frame = !p_frame->IsMain();
1214+
1215+
if ((!t_frame && m_loading_page_origin == PageOrigin::kSetSource) || IgnoreUrl(t_url))
11831216
return CefLoadHandler::OnLoadEnd(p_browser, p_frame, p_http_status_code);
11841217

11851218
char *t_url_str;
11861219
t_url_str = nil;
11871220
/* UNCHECKED */ MCCefStringToUtf8String(t_url, t_url_str);
11881221

1189-
bool t_frame;
1190-
t_frame = !p_frame->IsMain();
1191-
11921222
if (t_is_error)
11931223
{
11941224
if (t_error_code != ERR_UNKNOWN_URL_SCHEME)
@@ -1266,10 +1296,13 @@ bool MCCefBrowserBase::Initialize()
12661296

12671297
// IM-2014-05-06: [[ Bug 12384 ]] Browser must be created with non-empty URL or setting
12681298
// htmltext will not work
1269-
CefString t_url(CEF_DUMMY_URL);
1299+
1300+
// Using a blank data url here forces the CEF render process to load,
1301+
// allowing subsequent calls to MCCefBrowserBase::LoadHTMLText to succeed
1302+
CefString t_url("data:text/html;charset=utf-8,");
12701303

12711304
// IM-2014-05-06: [[ Bug 12384 ]] Prevent callback messages for dummy URL
1272-
m_client->AddIgnoreUrl(t_url);
1305+
m_client->SetLoadingPageOrigin(MCCefBrowserClient::PageOrigin::kSetSource);
12731306
PlatformConfigureWindow(t_window_info);
12741307

12751308
if (MC_CEF_USE_MULTITHREADED_MESSAGELOOP)
@@ -1587,6 +1620,7 @@ bool MCCefBrowserBase::LoadHTMLText(const char *p_htmltext, const char *p_base_u
15871620
if (!MCCefStringFromUtf8String(p_base_url, t_base_url))
15881621
return false;
15891622

1623+
m_client->SetLoadingPageOrigin(MCCefBrowserClient::PageOrigin::kSetSource);
15901624
m_browser->GetMainFrame()->LoadString(t_htmltext, t_base_url);
15911625

15921626
return true;
@@ -1693,6 +1727,15 @@ void MCCefBrowserBase::SetUserAgent(const char *p_user_agent)
16931727

16941728
char *MCCefBrowserBase::GetURL(void)
16951729
{
1730+
if (m_client->GetDisplayedPageOrigin() == MCCefBrowserClient::PageOrigin::kSetSource)
1731+
{
1732+
// return empty string if page was loaded from source
1733+
char *t_url_string;
1734+
t_url_string = nil;
1735+
/* UNCHECKED */ MCCStringClone("", t_url_string);
1736+
return t_url_string;
1737+
}
1738+
16961739
CefString t_url;
16971740
t_url = m_browser->GetMainFrame()->GetURL();
16981741

@@ -1789,6 +1832,8 @@ bool MCCefBrowserBase::GoToURL(const char *p_url)
17891832
if (!MCCefStringFromUtf8String(p_url, t_url))
17901833
return false;
17911834

1835+
m_client->SetLoadingPageOrigin(MCCefBrowserClient::PageOrigin::kSetUrl);
1836+
17921837
t_frame->LoadURL(t_url);
17931838
return true;
17941839
}

0 commit comments

Comments
 (0)