This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathlibbrowser_cef.cpp
More file actions
2252 lines (1736 loc) · 53.2 KB
/
libbrowser_cef.cpp
File metadata and controls
2252 lines (1736 loc) · 53.2 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include <core.h>
#include <stdlib.h>
#include "libbrowser_cef.h"
#include <include/cef_app.h>
#include <include/cef_parser.h>
#include <include/wrapper/cef_scoped_temp_dir.h>
#include <list>
#include <set>
#if defined(TARGET_PLATFORM_POSIX)
#include "signal_restore_posix.h"
#include <sys/time.h>
#endif
////////////////////////////////////////////////////////////////////////////////
#define CEF_DUMMY_URL "http://libbrowser_dummy_url/"
////////////////////////////////////////////////////////////////////////////////
// String conversion
bool MCCefStringToUtf8String(const CefString &p_cef_string, char *&r_u8_string)
{
if (p_cef_string.empty())
return MCCStringClone("", r_u8_string);
return MCCStringFromUnicode(p_cef_string.c_str(), r_u8_string);
}
bool MCCefStringFromUtf8String(const char *p_u8_string, cef_string_t *r_cef_string)
{
return 0 != cef_string_from_utf8(p_u8_string, MCCStringLength(p_u8_string), r_cef_string);
}
bool MCCefStringFromUtf8String(const char *p_u8_string, CefString &r_cef_string)
{
return MCCefStringFromUtf8String(p_u8_string, r_cef_string.GetWritableStruct());
}
bool MCCefStringToUInt(const CefString &p_string, uint32_t &r_int)
{
char_t * t_tmp_string;
t_tmp_string = nil;
uint32_t t_int;
bool t_success;
t_success = MCCefStringToUtf8String(p_string, t_tmp_string);
if (t_success)
{
char *t_end;
t_int = strtoul(t_tmp_string, &t_end, 10);
t_success = t_end == t_tmp_string + MCCStringLength(t_tmp_string);
}
if (t_tmp_string != nil)
MCCStringFree(t_tmp_string);
if (t_success)
r_int = t_int;
return t_success;
}
////////////////////////////////////////////////////////////////////////////////
bool MCCefListToBrowserList(CefRefPtr<CefListValue> p_list, MCBrowserListRef &r_list)
{
bool t_success;
t_success = true;
MCBrowserListRef t_list;
t_list = nil;
size_t t_size;
t_size = p_list->GetSize();
if (t_success)
t_success = MCBrowserListCreate(t_list, t_size);
for (uint32_t i = 0; t_success && i < t_size; i++)
{
switch (p_list->GetType(i))
{
case VTYPE_BOOL:
t_success = MCBrowserListSetBoolean(t_list, i, p_list->GetBool(i));
break;
case VTYPE_INT:
t_success = MCBrowserListSetInteger(t_list, i, p_list->GetInt(i));
break;
case VTYPE_DOUBLE:
t_success = MCBrowserListSetDouble(t_list, i, p_list->GetDouble(i));
break;
case VTYPE_STRING:
{
char *t_string = nil;
t_success = MCCefStringToUtf8String(p_list->GetString(i), t_string) && MCBrowserListSetUTF8String(t_list, i, t_string);
if (t_string != nil)
MCCStringFree(t_string);
break;
}
case VTYPE_LIST:
{
MCBrowserListRef t_list_val = nil;
t_success = MCCefListToBrowserList(p_list->GetList(i), t_list_val) && MCBrowserListSetList(t_list, i, t_list_val);
MCBrowserListRelease(t_list_val);
break;
}
case VTYPE_DICTIONARY:
{
MCBrowserDictionaryRef t_dict_val = nil;
t_success = MCCefDictionaryToBrowserDictionary(p_list->GetDictionary(i), t_dict_val) && MCBrowserListSetDictionary(t_list, i, t_dict_val);
MCBrowserDictionaryRelease(t_dict_val);
break;
}
default:
// unimplemented value type
t_success = false;
}
}
if (t_success)
r_list = t_list;
else
MCBrowserListRelease(t_list);
return t_success;
}
bool MCCefDictionaryToBrowserDictionary(CefRefPtr<CefDictionaryValue> p_dict, MCBrowserDictionaryRef &r_dict)
{
bool t_success;
t_success = true;
MCBrowserDictionaryRef t_dict;
t_dict = nil;
CefDictionaryValue::KeyList t_key_list;
if (t_success)
t_success = p_dict->GetKeys(t_key_list);
if (t_success)
t_success = MCBrowserDictionaryCreate(t_dict, t_key_list.size());
for (CefDictionaryValue::KeyList::iterator i = t_key_list.begin(); t_success && i < t_key_list.end(); i++)
{
char *t_key;
t_key = nil;
if (t_success)
t_success = MCCefStringToUtf8String(*i, t_key);
if (t_success)
{
switch (p_dict->GetType(*i))
{
case VTYPE_BOOL:
t_success = MCBrowserDictionarySetBoolean(t_dict, t_key, p_dict->GetBool(*i));
break;
case VTYPE_INT:
t_success = MCBrowserDictionarySetInteger(t_dict, t_key, p_dict->GetInt(*i));
break;
case VTYPE_DOUBLE:
t_success = MCBrowserDictionarySetDouble(t_dict, t_key, p_dict->GetDouble(*i));
break;
case VTYPE_STRING:
{
char *t_string = nil;
t_success = MCCefStringToUtf8String(p_dict->GetString(*i), t_string) && MCBrowserDictionarySetUTF8String(t_dict, t_key, t_string);
if (t_string != nil)
MCCStringFree(t_string);
break;
}
case VTYPE_LIST:
{
MCBrowserListRef t_list_val = nil;
t_success = MCCefListToBrowserList(p_dict->GetList(*i), t_list_val) && MCBrowserDictionarySetList(t_dict, t_key, t_list_val);
MCBrowserListRelease(t_list_val);
break;
}
case VTYPE_DICTIONARY:
{
MCBrowserDictionaryRef t_dict_val = nil;
t_success = MCCefDictionaryToBrowserDictionary(p_dict->GetDictionary(*i), t_dict_val) && MCBrowserDictionarySetDictionary(t_dict, t_key, t_dict_val);
MCBrowserDictionaryRelease(t_dict_val);
break;
}
default:
// unimplemented value type
t_success = false;
}
}
if (t_key != nil)
MCCStringFree(t_key);
}
if (t_success)
r_dict = t_dict;
else
MCBrowserDictionaryRelease(t_dict);
return t_success;
}
////////////////////////////////////////////////////////////////////////////////
static const char *s_auth_scheme_strings[] =
{
"basic",
"digest",
"ntlm",
"negotiate",
"spdyproxy",
nil
};
bool MCCefAuthSchemeFromCefString(const CefString &p_string, MCCefAuthScheme &r_scheme)
{
for (uint32_t i = 0; s_auth_scheme_strings[i] != nil; i++)
{
if (p_string == s_auth_scheme_strings[i])
{
r_scheme = (MCCefAuthScheme)i;
return true;
}
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
// Cef initialization / shutdown handling
static bool s_cef_initialised = false;
static bool s_cefbrowser_initialised = false;
static uint32_t s_instance_count = 0;
static CefScopedTempDir s_temp_cache;
void MCCefBrowserExternalInit(void)
{
// set up static vars
s_cef_initialised = false;
s_cefbrowser_initialised = false;
s_instance_count = 0;
}
void MCCefBrowserRunloopAction(void *p_context)
{
CefDoMessageLoopWork();
}
class MCCefBrowserApp : public CefApp, CefBrowserProcessHandler
{
public:
virtual CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler() OVERRIDE { return this; }
virtual void OnBeforeChildProcessLaunch(CefRefPtr<CefCommandLine> p_command_line) OVERRIDE
{
if (MCCefPlatformGetHiDPIEnabled())
p_command_line->AppendSwitch(MC_CEF_HIDPI_SWITCH);
}
virtual void OnBeforeCommandLineProcessing(const CefString &p_process_type, CefRefPtr<CefCommandLine> p_command_line) OVERRIDE
{
// Enable WebRTC
p_command_line->AppendSwitch("enable-media-stream");
}
IMPLEMENT_REFCOUNTING(MCCefBrowserApp);
};
extern "C" int initialise_weak_link_cef(void);
// These come from the engine we link to.
extern "C" void *MCSupportLibraryLoad(const char *p_path);
extern "C" void *MCSupportLibraryUnload(void *p_handle);
extern "C" char *MCSupportLibraryCopyNativePath(void *p_handle);
#if defined(WIN32)
static const char *kCefProcessName = "libbrowser-cefprocess.exe";
static const char *kCefPathSeparatorStr = "\\";
static const char kCefPathSeparator = '\\';
#else
static const char *kCefProcessName = "libbrowser-cefprocess";
static const char *kCefPathSeparatorStr = "/";
static const char kCefPathSeparator = '/';
#endif
#if defined(TARGET_PLATFORM_LINUX)
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
////////////////////////////////////////////////////////////////////////////////
static bool get_link_size(const char *p_path, uint32_t &r_size)
{
if (p_path == nil)
return false;
struct stat t_stat;
if (lstat(p_path, &t_stat) == -1)
return false;
r_size = t_stat.st_size;
return true;
}
static bool get_link_path(const char *p_link, char *&r_path)
{
bool t_success;
t_success = true;
char *t_buffer;
t_buffer = nil;
uint32_t t_buffer_size;
t_buffer_size = 0;
uint32_t t_link_size;
t_success = get_link_size(p_link, t_link_size);
while (t_success && t_link_size + 1 > t_buffer_size)
{
t_buffer_size = t_link_size + 1;
t_success = MCBrowserMemoryReallocate(t_buffer, t_buffer_size, t_buffer);
if (t_success)
{
int32_t t_read;
t_read = readlink(p_link, t_buffer, t_buffer_size);
t_success = t_read >= 0;
t_link_size = t_read;
}
}
if (t_success)
{
t_buffer[t_link_size] = '\0';
r_path = t_buffer;
}
else
MCBrowserMemoryDeallocate(t_buffer);
return t_success;
}
static bool get_exe_path_from_proc_fs(char *&r_path)
{
return get_link_path("/proc/self/exe", r_path);
}
//////////
const char *__MCCefPlatformGetExecutableFolder(void)
{
static char *s_exe_path = nil;
if (s_exe_path == nil)
{
bool t_success;
t_success = get_exe_path_from_proc_fs(s_exe_path);
if (t_success)
{
// remove library component from path
uint32_t t_index;
if (MCCStringLastIndexOf(s_exe_path, '/', t_index))
s_exe_path[t_index] = '\0';
}
}
return s_exe_path;
}
#endif
static bool __MCCefGetLibraryPath(char*& r_path)
{
void *t_module = nullptr;
if (t_module == nullptr)
t_module = MCSupportLibraryLoad("./CEF/libcef");
/* TODO[Bug 19381] On Linux and Windows, the in-git-checkout
* location of CEF is "./CEF" but once LiveCode is installed it's
* in "./Externals/CEF". */
#if defined(WIN32) || defined(TARGET_PLATFORM_LINUX)
if (t_module == nullptr)
t_module = MCSupportLibraryLoad("./Externals/CEF/libcef");
#endif
char *t_module_path =
t_module != nullptr ? MCSupportLibraryCopyNativePath(t_module)
: nullptr;
MCSupportLibraryUnload(t_module);
if (t_module_path == nullptr)
{
return false;
}
char *t_last_sep =
strrchr(t_module_path,
kCefPathSeparator);
if (t_last_sep == nullptr)
{
free(t_module_path);
return false;
}
*t_last_sep = '\0';
r_path = t_module_path;
return true;
}
static bool __MCCefAppendPath(const char *p_base, const char *p_path, char *&r_path)
{
if (p_base == nil)
return MCCStringClone(p_path, r_path);
else if (MCCStringEndsWith(p_base, kCefPathSeparatorStr) ||
*p_path == '\0')
return MCCStringFormat(r_path, "%s%s", p_base, p_path);
else
return MCCStringFormat(r_path, "%s%s%s", p_base, kCefPathSeparatorStr, p_path);
}
static bool __MCCefBuildPath(const char *p_library_path,
const char *p_suffix,
cef_string_t* r_string)
{
char *t_process_path = nullptr;
if (!__MCCefAppendPath(p_library_path,
p_suffix,
t_process_path) ||
!MCCefStringFromUtf8String(t_process_path,
r_string))
{
free(t_process_path);
return false;
}
return true;
}
// IM-2014-03-13: [[ revBrowserCEF ]] Initialisation of the CEF library
bool MCCefInitialise(void)
{
if (s_cef_initialised)
return true;
////////
char *t_library_path = nullptr;
if (!__MCCefGetLibraryPath(t_library_path))
return false;
////////
CefMainArgs t_args;
CefSettings t_settings;
t_settings.multi_threaded_message_loop = MC_CEF_USE_MULTITHREADED_MESSAGELOOP;
t_settings.command_line_args_disabled = true;
t_settings.windowless_rendering_enabled = true;
t_settings.no_sandbox = true;
#ifdef _DEBUG
t_settings.log_severity = LOGSEVERITY_VERBOSE;
#else
t_settings.log_severity = LOGSEVERITY_DISABLE;
#endif
bool t_success = true;
#ifdef TARGET_PLATFORM_LINUX
if (t_success)
t_success = __MCCefBuildPath(__MCCefPlatformGetExecutableFolder(), kCefProcessName, &t_settings.browser_subprocess_path);
#else
if (t_success)
t_success = __MCCefBuildPath(t_library_path, kCefProcessName, &t_settings.browser_subprocess_path);
#endif
if (t_success)
t_success = __MCCefBuildPath(t_library_path, "locales", &t_settings.locales_dir_path);
if (t_success)
t_success = __MCCefBuildPath(t_library_path, "", &t_settings.resources_dir_path);
if (t_success)
t_success = s_temp_cache.CreateUniqueTempDir();
if (t_success)
CefString(&t_settings.cache_path).FromString(s_temp_cache.GetPath());
CefRefPtr<CefApp> t_app = new (nothrow) MCCefBrowserApp();
if (t_success)
t_success = -1 == CefExecuteProcess(t_args, t_app, nil);
if (t_success)
{
#if defined(TARGET_PLATFORM_POSIX)
struct itimerval t_old_timer, t_new_timer;
memset(&t_new_timer, 0, sizeof(t_new_timer));
setitimer(ITIMER_REAL, &t_new_timer, &t_old_timer);
BackupSignalHandlers();
#endif
t_success = CefInitialize(t_args, t_settings, t_app, nil);
#if defined(TARGET_PLATFORM_POSIX)
RestoreSignalHandlers();
setitimer(ITIMER_REAL, &t_old_timer, nil);
#endif
}
s_cef_initialised = t_success;
free(t_library_path);
return s_cef_initialised;
}
// IM-2014-03-13: [[ revBrowserCEF ]] Initialise CEF & install the runloop action
bool MCCefBrowserInitialise(void)
{
if (s_cefbrowser_initialised)
return true;
bool t_success;
t_success = true;
if (t_success)
{
t_success = initialise_weak_link_cef();
}
if (t_success)
t_success = MCCefInitialise();
if (t_success && !MC_CEF_USE_MULTITHREADED_MESSAGELOOP)
t_success = MCBrowserAddRunloopAction(MCCefBrowserRunloopAction, nil);
s_cefbrowser_initialised = t_success;
return s_cefbrowser_initialised;
}
// IM-2014-03-13: [[ revBrowserCEF ]] Shutdown the CEF library
void MCCefFinalise(void)
{
if (!s_cef_initialised)
return;
if (s_temp_cache.IsValid())
{
s_temp_cache.Delete();
}
CefShutdown();
s_cef_initialised = false;
}
// IM-2014-03-13: [[ revBrowserCEF ]] Shut down the browser
void MCCefBrowserFinalise(void)
{
if (!s_cefbrowser_initialised)
return;
if (!MC_CEF_USE_MULTITHREADED_MESSAGELOOP)
MCBrowserRemoveRunloopAction(MCCefBrowserRunloopAction, nil);
s_cefbrowser_initialised = false;
}
void MCCefIncrementInstanceCount(void)
{
if (s_instance_count == 0)
/* UNCHECKED */ MCCefBrowserInitialise();
s_instance_count++;
}
void MCCefDecrementInstanceCount(void)
{
if (s_instance_count == 0)
return;
s_instance_count--;
if (s_instance_count == 0)
MCCefBrowserFinalise();
}
////////////////////////////////////////////////////////////////////////////////
// Utility class to hold a response from a request to the render process
class MCCefMessageResult
{
private:
bool m_have_result;
bool m_success;
CefValueType m_result_type;
CefString m_result_string;
int m_result_int;
public:
void SetResult(bool p_success, const CefString &p_result)
{
if (HaveResult())
Clear();
m_success = p_success;
m_result_string = p_result;
m_result_type = VTYPE_STRING;
m_have_result = true;
}
void SetResult(bool p_success, int p_result)
{
if (HaveResult())
Clear();
m_success = p_success;
m_result_int = p_result;
m_result_type = VTYPE_INT;
m_have_result = true;
}
bool HaveResult() { return m_have_result; }
bool GetResult(CefString &r_result) const
{
if (!m_success)
return false;
if (m_result_type == VTYPE_STRING)
{
r_result = m_result_string;
return true;
}
// convert to string
bool t_converted;
t_converted = false;
switch (m_result_type)
{
case VTYPE_INT:
{
char *t_tmp;
t_tmp = nil;
t_converted = MCCStringFormat(t_tmp, "%d", m_result_int) &&
MCCefStringFromUtf8String(t_tmp, r_result);
if (t_tmp != nil)
MCCStringFree(t_tmp);
}
break;
case VTYPE_NULL:
case VTYPE_BOOL:
case VTYPE_DOUBLE:
case VTYPE_STRING:
case VTYPE_BINARY:
case VTYPE_DICTIONARY:
case VTYPE_LIST:
case VTYPE_INVALID:
/* UNIMPLEMENTED */
break;
}
return t_converted;
}
bool GetResult(int &r_result) const
{
if (!m_success)
return false;
if (m_result_type == VTYPE_INT)
{
r_result = m_result_int;
return true;
}
// convert to int
return false;
}
void Clear()
{
m_have_result = false;
m_result_string.ClearAndFree();
}
};
////////////////////////////////////////////////////////////////////////////////
// Browser client - receives callback messages from the browser
struct MCCefErrorInfo
{
CefString url;
CefString error_message;
CefLoadHandler::ErrorCode error_code;
};
class MCCefBrowserClient : public CefClient, CefLifeSpanHandler, CefRequestHandler, /* CefDownloadHandler ,*/ CefLoadHandler, CefContextMenuHandler, CefDragHandler
{
public:
enum PageOrigin
{
kNone,
kSetUrl,
kSetSource,
kBrowse,
};
private:
int m_browser_id;
int m_popup_browser_id = 0;
MCCefBrowserBase *m_owner;
MCCefMessageResult m_message_result;
std::map<int64_t, MCCefErrorInfo> m_load_error_frames;
// Describes where loading url requests came from
std::map<CefString, PageOrigin> m_load_url_origins;
CefString m_last_request_url;
PageOrigin m_displayed_page_origin;
// Error handling - we need to keep track of url that failed to load in a
// frame so we can send the correct url in onLoadEnd()
void AddLoadErrorFrame(int64_t p_id, const CefString &p_url, const CefString &p_error_msg, CefLoadHandler::ErrorCode p_error_code)
{
m_load_error_frames[p_id].url = p_url;
m_load_error_frames[p_id].error_message = p_error_msg;
m_load_error_frames[p_id].error_code = p_error_code;
}
bool FindLoadErrorFrame(int64_t p_id, MCCefErrorInfo &r_info, bool p_delete)
{
std::map<int64_t, MCCefErrorInfo>::iterator t_iter;
t_iter = m_load_error_frames.find(p_id);
if (t_iter == m_load_error_frames.end())
return false;
r_info = t_iter->second;
if (p_delete)
m_load_error_frames.erase(t_iter);
return true;
}
bool RemoveLoadErrorFrame(int64_t p_id, CefString &r_error_url, CefString &r_error_msg, CefLoadHandler::ErrorCode &r_error_code)
{
MCCefErrorInfo t_info;
if (!FindLoadErrorFrame(p_id, t_info, true))
return false;
r_error_url = t_info.url;
r_error_msg = t_info.error_message;
r_error_code = t_info.error_code;
return true;
}
bool FetchLoadErrorFrame(int64_t p_id, CefString &r_error_url, CefString &r_error_msg, CefLoadHandler::ErrorCode &r_error_code)
{
MCCefErrorInfo t_info;
if (!FindLoadErrorFrame(p_id, t_info, false))
return false;
r_error_url = t_info.url;
r_error_msg = t_info.error_message;
r_error_code = t_info.error_code;
return true;
}
public:
MCCefBrowserClient(MCCefBrowserBase *p_owner)
{
m_owner = p_owner;
m_browser_id = 0;
m_displayed_page_origin = PageOrigin::kNone;
}
// IM-2014-07-21: [[ Bug 12296 ]] Method to allow owner to notify client of its deletion
void OnOwnerClosed(void)
{
m_owner = nil;
}
// Tell browser which callback interfaces we implement
virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE { return this; }
virtual CefRefPtr<CefRequestHandler> GetRequestHandler() OVERRIDE { return this; }
// virtual CefRefPtr<CefDownloadHandler> GetDownloadHandler() OVERRIDE { return this; }
virtual CefRefPtr<CefLoadHandler> GetLoadHandler() OVERRIDE { return this; }
virtual CefRefPtr<CefContextMenuHandler> GetContextMenuHandler() OVERRIDE { return this; }
virtual CefRefPtr<CefDragHandler> GetDragHandler() OVERRIDE { return this; }
void AddLoadingUrl(const CefString &p_url, PageOrigin p_origin)
{
m_load_url_origins[p_url] = p_origin;
}
void RemoveLoadingUrl(const CefString &p_url)
{
m_load_url_origins.erase(p_url);
}
bool GetLoadingUrlOrigin(const CefString &p_url, PageOrigin &r_origin)
{
auto t_iter = m_load_url_origins.find(p_url);
if (t_iter == m_load_url_origins.end())
return false;
r_origin = t_iter->second;
return true;
}
PageOrigin GetDisplayedPageOrigin()
{
return m_displayed_page_origin;
}
MCCefMessageResult &GetMessageResult()
{
return m_message_result;
}
virtual bool OnProcessMessageReceived(CefRefPtr<CefBrowser> p_browser, CefProcessId p_source_process, CefRefPtr<CefProcessMessage> p_message) OVERRIDE
{
CefString t_message_name;
t_message_name = p_message->GetName();
if (t_message_name == MC_CEFMSG_RESULT)
{
// When we get a result message, the result is stored in the message result object
CefRefPtr<CefListValue> t_args;
t_args = p_message->GetArgumentList();
switch (t_args->GetType(1))
{
case VTYPE_STRING:
m_message_result.SetResult(t_args->GetBool(0), t_args->GetString(1));
break;
case VTYPE_INT:
m_message_result.SetResult(t_args->GetBool(0), t_args->GetInt(1));
break;
default:
MCAssert(false);
}
MCBrowserRunloopBreakWait();
return true;
}
else if (t_message_name == MC_CEFMSG_JS_HANDLER)
{
// Handle JavaScript call to LiveCode handler
CefRefPtr<CefListValue> t_args;
t_args = p_message->GetArgumentList();
char *t_handler;
t_handler = nil;
bool t_success;
t_success = true;
// IM-2014-07-21: [[ Bug 12296 ]] Don't proceed with callback if browser has been closed
if (t_success)
t_success = nil != m_owner;
if (t_success)
t_success = MCCefStringToUtf8String(t_args->GetString(0), t_handler);
MCBrowserListRef t_param_list;
t_param_list = nil;
if (t_success)
t_success = MCCefListToBrowserList(t_args->GetList(1), t_param_list);
if (t_success)
m_owner->OnJavaScriptCall(t_handler, t_param_list);
if (t_handler)
MCCStringFree(t_handler);
if (t_param_list != nil)
MCBrowserListRelease(t_param_list);
return true;
}
return CefClient::OnProcessMessageReceived(p_browser, p_source_process, p_message);
}
// CefLifeSpanHandler interface
// Called on UI thread
virtual void OnAfterCreated(CefRefPtr<CefBrowser> p_browser) OVERRIDE
{
// Keep track of first created browser instance. Any others belong to pop-ups
if (m_browser_id == 0)
{
m_browser_id = p_browser->GetIdentifier();
// IM-2014-07-21: [[ Bug 12296 ]] Check owner has not been closed before invoking callback
if (m_owner != nil)
m_owner->OnCefBrowserCreated(p_browser);
}
else
{
m_popup_browser_id = p_browser->GetIdentifier();
}
MCCefIncrementInstanceCount();
}
virtual void OnBeforeClose(CefRefPtr<CefBrowser> p_browser) OVERRIDE
{
if (m_owner != nil)
m_owner->OnCefBrowserClosed(p_browser);
MCCefDecrementInstanceCount();
}
virtual bool OnBeforePopup(
CefRefPtr<CefBrowser> p_browser,
CefRefPtr<CefFrame> p_frame,
const CefString& p_target_url,
const CefString& p_target_frame_name,
CefLifeSpanHandler::WindowOpenDisposition p_target_disposition,
bool p_user_gesture,
const CefPopupFeatures& p_popup_features,
CefWindowInfo& p_window_info,
CefRefPtr<CefClient>& p_client,
CefBrowserSettings& p_settings,
bool* p_no_javascript_access ) OVERRIDE
{
// returning true here will prevent popup windows from opening
// IM-2014-07-21: [[ Bug 12296 ]] If browser has been closed then exit
if (nil == m_owner)
return true;
CefWindowInfo(t_window_info);
t_window_info.SetAsWindowless(p_browser->GetHost()->GetWindowHandle());
p_window_info = t_window_info;