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

Commit 52a129b

Browse files
committed
[[ BrowserWidget ]] Pass js param list to callback as propertly-typed browser list (where possible)
[[ BrowserWidget ]] Fix browserlist bool values being assigned as int
1 parent 7ba23ad commit 52a129b

File tree

3 files changed

+124
-34
lines changed

3 files changed

+124
-34
lines changed

libbrowser/src/libbrowser_cef.cpp

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,69 @@ bool MCCefStringToUInt(const CefString &p_string, uint32_t &r_int)
7676

7777
////////////////////////////////////////////////////////////////////////////////
7878

79+
bool MCCefListToBrowserList(CefRefPtr<CefListValue> p_list, MCBrowserListRef &r_list)
80+
{
81+
bool t_success;
82+
t_success = true;
83+
84+
MCBrowserListRef t_list;
85+
t_list = nil;
86+
87+
size_t t_size;
88+
t_size = p_list->GetSize();
89+
90+
if (t_success)
91+
t_success = MCBrowserListCreate(t_list, t_size);
92+
93+
for (uint32_t i = 0; t_success && i < t_size; i++)
94+
{
95+
switch (p_list->GetType(i))
96+
{
97+
case VTYPE_BOOL:
98+
t_success = MCBrowserListSetBoolean(t_list, i, p_list->GetBool(i));
99+
break;
100+
101+
case VTYPE_INT:
102+
t_success = MCBrowserListSetInteger(t_list, i, p_list->GetInt(i));
103+
break;
104+
105+
case VTYPE_DOUBLE:
106+
t_success = MCBrowserListSetDouble(t_list, i, p_list->GetDouble(i));
107+
break;
108+
109+
case VTYPE_STRING:
110+
{
111+
char *t_string = nil;
112+
t_success = MCCefStringToUtf8String(p_list->GetString(i), t_string) && MCBrowserListSetUTF8String(t_list, i, t_string);
113+
if (t_string != nil)
114+
MCCStringFree(t_string);
115+
break;
116+
}
117+
118+
case VTYPE_LIST:
119+
{
120+
MCBrowserListRef t_list_val = nil;
121+
t_success = MCCefListToBrowserList(p_list->GetList(i), t_list_val) && MCBrowserListSetList(t_list, i, t_list_val);
122+
MCBrowserListRelease(t_list_val);
123+
break;
124+
}
125+
126+
default:
127+
// unimplemented value type
128+
t_success = false;
129+
}
130+
}
131+
132+
if (t_success)
133+
r_list = t_list;
134+
else
135+
MCBrowserListRelease(t_list);
136+
137+
return t_success;
138+
}
139+
140+
////////////////////////////////////////////////////////////////////////////////
141+
79142
static const char *s_auth_scheme_strings[] =
80143
{
81144
"basic",
@@ -504,38 +567,20 @@ class MCCefBrowserClient : public CefClient, CefLifeSpanHandler, CefRequestHandl
504567
uint32_t t_arg_count;
505568
t_arg_count = 0;
506569

507-
MCBrowserListRef t_list;
508-
t_list = nil;
570+
MCBrowserListRef t_param_list;
571+
t_param_list = nil;
509572

510-
// Convert message args to strings and pass to handler parameters
511573
if (t_success)
512-
{
513-
t_arg_count = t_args->GetSize() - 1;
514-
t_success = MCBrowserListCreate(t_list, t_arg_count);
515-
}
516-
517-
for (uint32_t i = 0; t_success && i < t_arg_count; i++)
518-
{
519-
char *t_utf8_string;
520-
t_utf8_string = nil;
521-
522-
t_success = MCCefStringToUtf8String(t_args->GetString(i + 1), t_utf8_string);
523-
524-
if (t_success)
525-
t_success = MCBrowserListSetUTF8String(t_list, i, t_utf8_string);
526-
527-
if (t_utf8_string != nil)
528-
MCCStringFree(t_utf8_string);
529-
}
574+
t_success = MCCefListToBrowserList(t_args->GetList(1), t_param_list);
530575

531576
if (t_success)
532-
m_owner->OnJavaScriptCall(t_handler, t_list);
577+
m_owner->OnJavaScriptCall(t_handler, t_param_list);
533578

534579
if (t_handler)
535580
MCCStringFree(t_handler);
536581

537-
if (t_list != nil)
538-
MCBrowserListRelease(t_list);
582+
if (t_param_list != nil)
583+
MCBrowserListRelease(t_param_list);
539584

540585
return true;
541586
}

libbrowser/src/libbrowser_cefprocess.cpp

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,54 @@ bool MCCefListToV8List(CefRefPtr<CefV8Context> p_context, CefRefPtr<CefListValue
140140
return t_success;
141141
}
142142

143+
bool MCCefV8ListToList(CefRefPtr<CefV8Context> p_context, const CefV8ValueList &p_list, CefRefPtr<CefListValue> &r_list)
144+
{
145+
bool t_success;
146+
t_success = true;
147+
148+
p_context->Enter();
149+
150+
CefRefPtr<CefListValue> t_list;
151+
if (t_success)
152+
t_list = CefListValue::Create();
153+
154+
if (t_success)
155+
t_success = t_list->SetSize(p_list.size());
156+
157+
for (CefV8ValueList::const_iterator i = p_list.begin(); t_success && i != p_list.end(); i++)
158+
{
159+
CefRefPtr<CefV8Value> t_val;
160+
t_val = *i;
161+
162+
uint32_t t_index;
163+
t_index = i - p_list.begin();
164+
165+
if (t_val->IsBool())
166+
t_success = t_list->SetBool(t_index, t_val->GetBoolValue());
167+
else if (t_val->IsInt())
168+
t_success = t_list->SetInt(t_index, t_val->GetIntValue());
169+
else if (t_val->IsUInt())
170+
t_success = t_list->SetInt(t_index, t_val->GetUIntValue());
171+
else if (t_val->IsDouble())
172+
t_success = t_list->SetDouble(t_index, t_val->GetDoubleValue());
173+
else if (t_val->IsString())
174+
t_success = t_list->SetString(t_index, t_val->GetStringValue());
175+
else
176+
{
177+
t_success = MCCefV8ValueConvertToString(p_context, t_val);
178+
if (t_success)
179+
t_success = t_list->SetString(t_index, t_val->GetStringValue());
180+
}
181+
}
182+
183+
p_context->Exit();
184+
185+
if (t_success)
186+
r_list = t_list;
187+
188+
return t_success;
189+
}
190+
143191
bool MCCefHandleCallScript(CefRefPtr<CefBrowser> p_browser, const CefString &p_function_name, CefRefPtr<CefListValue> p_args, CefString &r_return_value)
144192
{
145193
bool t_success;
@@ -330,15 +378,12 @@ bool MCCefSendHandlerMessage(CefRefPtr<CefBrowser> p_browser, const CefString &p
330378

331379
t_success = t_args->SetString(0, p_handler);
332380

333-
for (CefV8ValueList::const_iterator i = p_args.begin(); t_success && i != p_args.end(); i++)
334-
{
335-
CefRefPtr<CefV8Value> t_val;
336-
t_val = *i;
337-
338-
t_success = MCCefV8ValueConvertToString(CefV8Context::GetCurrentContext(), t_val);
339-
if (t_success)
340-
t_success = t_args->SetString((i - p_args.begin()) + 1, t_val->GetStringValue());
341-
}
381+
CefRefPtr<CefListValue> t_params;
382+
if (t_success)
383+
t_success = MCCefV8ListToList(CefV8Context::GetCurrentContext(), p_args, t_params);
384+
385+
if (t_success)
386+
t_success = t_args->SetList(1, t_params);
342387
}
343388

344389
if (t_success)

libbrowser/src/libbrowser_value.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void MCBrowserValueClear(MCBrowserValue &p_value)
5353
bool MCBrowserValueSetBoolean(MCBrowserValue &self, bool p_value)
5454
{
5555
MCBrowserValueClear(self);
56-
self.type = kMCBrowserValueTypeInteger;
56+
self.type = kMCBrowserValueTypeBoolean;
5757
self.boolean = p_value;
5858

5959
return true;

0 commit comments

Comments
 (0)