Skip to content

Commit 0b01610

Browse files
committed
[[ BrowserWidget ]] Convert JS objects to LCB Arrays in CEF browser
1 parent c91a771 commit 0b01610

3 files changed

Lines changed: 231 additions & 0 deletions

File tree

libbrowser/src/libbrowser_cef.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ bool MCCefListToBrowserList(CefRefPtr<CefListValue> p_list, MCBrowserListRef &r_
129129
break;
130130
}
131131

132+
case VTYPE_DICTIONARY:
133+
{
134+
MCBrowserDictionaryRef t_dict_val = nil;
135+
t_success = MCCefDictionaryToBrowserDictionary(p_list->GetDictionary(i), t_dict_val) && MCBrowserListSetDictionary(t_list, i, t_dict_val);
136+
MCBrowserDictionaryRelease(t_dict_val);
137+
break;
138+
}
132139
default:
133140
// unimplemented value type
134141
t_success = false;
@@ -143,6 +150,85 @@ bool MCCefListToBrowserList(CefRefPtr<CefListValue> p_list, MCBrowserListRef &r_
143150
return t_success;
144151
}
145152

153+
bool MCCefDictionaryToBrowserDictionary(CefRefPtr<CefDictionaryValue> p_dict, MCBrowserDictionaryRef &r_dict)
154+
{
155+
bool t_success;
156+
t_success = true;
157+
158+
MCBrowserDictionaryRef t_dict;
159+
t_dict = nil;
160+
161+
CefDictionaryValue::KeyList t_key_list;
162+
if (t_success)
163+
t_success = p_dict->GetKeys(t_key_list);
164+
165+
if (t_success)
166+
t_success = MCBrowserDictionaryCreate(t_dict, t_key_list.size());
167+
168+
for (CefDictionaryValue::KeyList::iterator i = t_key_list.begin(); t_success && i < t_key_list.end(); i++)
169+
{
170+
char *t_key;
171+
t_key = nil;
172+
173+
if (t_success)
174+
t_success = MCCefStringToUtf8String(*i, t_key);
175+
176+
if (t_success)
177+
{
178+
switch (p_dict->GetType(*i))
179+
{
180+
case VTYPE_BOOL:
181+
t_success = MCBrowserDictionarySetBoolean(t_dict, t_key, p_dict->GetBool(*i));
182+
break;
183+
184+
case VTYPE_INT:
185+
t_success = MCBrowserDictionarySetInteger(t_dict, t_key, p_dict->GetInt(*i));
186+
break;
187+
188+
case VTYPE_DOUBLE:
189+
t_success = MCBrowserDictionarySetDouble(t_dict, t_key, p_dict->GetDouble(*i));
190+
break;
191+
192+
case VTYPE_STRING:
193+
{
194+
char *t_string = nil;
195+
t_success = MCCefStringToUtf8String(p_dict->GetString(*i), t_string) && MCBrowserDictionarySetUTF8String(t_dict, t_key, t_string);
196+
if (t_string != nil)
197+
MCCStringFree(t_string);
198+
break;
199+
}
200+
201+
case VTYPE_LIST:
202+
{
203+
MCBrowserListRef t_list_val = nil;
204+
t_success = MCCefListToBrowserList(p_dict->GetList(*i), t_list_val) && MCBrowserDictionarySetList(t_dict, t_key, t_list_val);
205+
MCBrowserListRelease(t_list_val);
206+
break;
207+
}
208+
209+
case VTYPE_DICTIONARY:
210+
{
211+
MCBrowserDictionaryRef t_dict_val = nil;
212+
t_success = MCCefDictionaryToBrowserDictionary(p_dict->GetDictionary(*i), t_dict_val) && MCBrowserDictionarySetDictionary(t_dict, t_key, t_dict_val);
213+
MCBrowserDictionaryRelease(t_dict_val);
214+
break;
215+
}
216+
217+
default:
218+
// unimplemented value type
219+
t_success = false;
220+
}
221+
}
222+
}
223+
224+
if (t_success)
225+
r_dict = t_dict;
226+
else
227+
MCBrowserDictionaryRelease(t_dict);
228+
229+
return t_success;
230+
}
231+
146232
////////////////////////////////////////////////////////////////////////////////
147233

148234
static const char *s_auth_scheme_strings[] =

libbrowser/src/libbrowser_cef.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ bool MCCefStringToUInt(const CefString &p_string, uint32_t &r_int);
169169

170170
bool MCCefAuthSchemeFromCefString(const CefString &p_string, MCCefAuthScheme &r_scheme);
171171

172+
bool MCCefListToBrowserList(CefRefPtr<CefListValue> p_list, MCBrowserListRef &r_list);
173+
bool MCCefDictionaryToBrowserDictionary(CefRefPtr<CefDictionaryValue> p_dict, MCBrowserDictionaryRef &r_dict);
174+
172175
//////////
173176

174177
#define MC_CEFMSG_EXECUTE_SCRIPT "cefbrowser_request_execute_script"

libbrowser/src/libbrowser_cefprocess.cpp

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,134 @@ bool MCCefListToV8List(CefRefPtr<CefV8Context> p_context, CefRefPtr<CefListValue
142142
return t_success;
143143
}
144144

145+
bool MCCefV8ArrayToList(CefRefPtr<CefV8Context> p_context, CefRefPtr<CefV8Value> p_array, CefRefPtr<CefListValue> &r_list);
146+
bool MCCefV8ObjectToDictionary(CefRefPtr<CefV8Context> p_context, CefRefPtr<CefV8Value> p_obj, CefRefPtr<CefDictionaryValue> &r_dict);
147+
148+
bool MCCefV8ArrayToList(CefRefPtr<CefV8Context> p_context, CefRefPtr<CefV8Value> p_array, CefRefPtr<CefListValue> &r_list)
149+
{
150+
bool t_success;
151+
t_success = true;
152+
153+
p_context->Enter();
154+
155+
CefRefPtr<CefListValue> t_list;
156+
if (t_success)
157+
t_list = CefListValue::Create();
158+
159+
uint32_t t_length;
160+
t_length = p_array->GetArrayLength();
161+
162+
if (t_success)
163+
t_success = t_list->SetSize(t_length);
164+
165+
for (uint32_t i = 0; i < t_length; i++)
166+
{
167+
CefRefPtr<CefV8Value> t_val;
168+
t_val = p_array->GetValue(i);
169+
170+
if (t_val->IsBool())
171+
t_success = t_list->SetBool(i, t_val->GetBoolValue());
172+
else if (t_val->IsInt())
173+
t_success = t_list->SetInt(i, t_val->GetIntValue());
174+
else if (t_val->IsUInt())
175+
t_success = t_list->SetInt(i, t_val->GetUIntValue());
176+
else if (t_val->IsDouble())
177+
t_success = t_list->SetDouble(i, t_val->GetDoubleValue());
178+
else if (t_val->IsString())
179+
t_success = t_list->SetString(i, t_val->GetStringValue());
180+
else if (t_val->IsArray())
181+
{
182+
CefRefPtr<CefListValue> t_list_val;
183+
t_success = MCCefV8ArrayToList(p_context, t_val, t_list_val);
184+
if (t_success)
185+
t_success = t_list->SetList(i, t_list_val);
186+
}
187+
else if (t_val->IsObject())
188+
{
189+
CefRefPtr<CefDictionaryValue> t_dict_val;
190+
t_success = MCCefV8ObjectToDictionary(p_context, t_val, t_dict_val);
191+
if (t_success)
192+
t_success = t_list->SetDictionary(i, t_dict_val);
193+
}
194+
else
195+
{
196+
t_success = MCCefV8ValueConvertToString(p_context, t_val);
197+
if (t_success)
198+
t_success = t_list->SetString(i, t_val->GetStringValue());
199+
}
200+
}
201+
202+
p_context->Exit();
203+
204+
if (t_success)
205+
r_list = t_list;
206+
207+
return t_success;
208+
}
209+
210+
bool MCCefV8ObjectToDictionary(CefRefPtr<CefV8Context> p_context, CefRefPtr<CefV8Value> p_obj, CefRefPtr<CefDictionaryValue> &r_dict)
211+
{
212+
bool t_success;
213+
t_success = true;
214+
215+
p_context->Enter();
216+
217+
CefRefPtr<CefDictionaryValue> t_dict;
218+
if (t_success)
219+
t_dict = CefDictionaryValue::Create();
220+
221+
std::vector<CefString> t_keys;
222+
if (t_success)
223+
t_success = p_obj->GetKeys(t_keys);
224+
225+
for (std::vector<CefString>::const_iterator i = t_keys.begin(); t_success && i != t_keys.end(); i++)
226+
{
227+
CefRefPtr<CefV8Value> t_val;
228+
t_val = p_obj->GetValue(*i);
229+
230+
uint32_t t_index;
231+
t_index = i - t_keys.begin();
232+
233+
if (t_val->IsBool())
234+
t_success = t_dict->SetBool(*i, t_val->GetBoolValue());
235+
else if (t_val->IsInt())
236+
t_success = t_dict->SetInt(*i, t_val->GetIntValue());
237+
else if (t_val->IsUInt())
238+
t_success = t_dict->SetInt(*i, t_val->GetUIntValue());
239+
else if (t_val->IsDouble())
240+
t_success = t_dict->SetDouble(*i, t_val->GetDoubleValue());
241+
else if (t_val->IsString())
242+
t_success = t_dict->SetString(*i, t_val->GetStringValue());
243+
else if (t_val->IsArray())
244+
{
245+
CefRefPtr<CefListValue> t_list_val;
246+
t_success = MCCefV8ArrayToList(p_context, t_val, t_list_val);
247+
if (t_success)
248+
t_success = t_dict->SetList(*i, t_list_val);
249+
}
250+
else if (t_val->IsObject())
251+
{
252+
CefRefPtr<CefDictionaryValue> t_dict_val;
253+
t_success = MCCefV8ObjectToDictionary(p_context, t_val, t_dict_val);
254+
if (t_success)
255+
t_success = t_dict->SetDictionary(*i, t_dict_val);
256+
}
257+
else
258+
{
259+
t_success = MCCefV8ValueConvertToString(p_context, t_val);
260+
if (t_success)
261+
t_success = t_dict->SetString(*i, t_val->GetStringValue());
262+
}
263+
}
264+
265+
p_context->Exit();
266+
267+
if (t_success)
268+
r_dict = t_dict;
269+
270+
return t_success;
271+
}
272+
145273
bool MCCefV8ListToList(CefRefPtr<CefV8Context> p_context, const CefV8ValueList &p_list, CefRefPtr<CefListValue> &r_list)
146274
{
147275
bool t_success;
@@ -174,6 +302,20 @@ bool MCCefV8ListToList(CefRefPtr<CefV8Context> p_context, const CefV8ValueList &
174302
t_success = t_list->SetDouble(t_index, t_val->GetDoubleValue());
175303
else if (t_val->IsString())
176304
t_success = t_list->SetString(t_index, t_val->GetStringValue());
305+
else if (t_val->IsArray())
306+
{
307+
CefRefPtr<CefListValue> t_list_val;
308+
t_success = MCCefV8ArrayToList(p_context, t_val, t_list_val);
309+
if (t_success)
310+
t_success = t_list->SetList(t_index, t_list_val);
311+
}
312+
else if (t_val->IsObject())
313+
{
314+
CefRefPtr<CefDictionaryValue> t_dict_val;
315+
t_success = MCCefV8ObjectToDictionary(p_context, t_val, t_dict_val);
316+
if (t_success)
317+
t_success = t_list->SetDictionary(t_index, t_dict_val);
318+
}
177319
else
178320
{
179321
t_success = MCCefV8ValueConvertToString(p_context, t_val);

0 commit comments

Comments
 (0)