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 pathrevbrowser.cpp
More file actions
1969 lines (1654 loc) · 51 KB
/
revbrowser.cpp
File metadata and controls
1969 lines (1654 loc) · 51 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) 2003-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 <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <revolution/external.h>
#include "revbrowser.h"
///////////////////////////////////////////////////////////////////////////////
//
// UTILITY FUNCTIONS
#define TRUE true
#ifdef _WINDOWS
#define strncasecmp _strnicmp
#define strcasecmp _stricmp
#endif
inline char *istrdup(const char *p_string)
{
return strdup(p_string);
}
inline char *BoolToStr(bool b)
{
return b ? istrdup("TRUE") : istrdup("FALSE");
}
inline bool StrToBool(char *boolstr)
{
return strncasecmp(boolstr, "TRUE", 4) == 0 ? true : false;
}
inline char *IntToStr(int p_value)
{
char *t_string;
t_string = nil;
// AL-2013-11-01 [[ Bug 11289 ]] Use libcore methods to prevent potential buffer overflows in revbrowser
MCCStringFormat(t_string, "%d", p_value);
return t_string;
}
///////////////////////////////////////////////////////////////////////////////
//
// INSTANCE HANDLING
//
class BrowserInstances
{
public:
BrowserInstances(void);
~BrowserInstances(void);
void Add(CWebBrowserBase *p_browser, bool p_is_xbrowser);
void Delete(CWebBrowserBase *p_browser);
int GetActiveInstanceId(void);
CWebBrowserBase *GetFirstInstance(void);
CWebBrowserBase *GetActiveInstance(void);
CWebBrowserBase *GetInstance(int p_id);
int GetCallbackDepth(int p_id);
bool Callback(int p_instance_id, const char *p_message, const char *p_url = NULL);
bool Callback(int p_id, const char *p_message, char **p_args, uint32_t p_arg_count, bool &r_pass);
void SetActiveInstanceId(int t_id);
char *GetInstanceIds(void);
private:
struct BrowserInstance
{
BrowserInstance *next;
int instance_id;
char *stack_id;
CWebBrowserBase *browser;
int callback_depth;
bool xbrowser_callbacks;
};
BrowserInstance *m_instances;
BrowserInstance *m_active_instance;
int m_last_instance_id;
bool FindInstanceById(int p_id, BrowserInstance *&r_instance);
void SendMessage(BrowserInstance *p_instance, const char *p_message, bool &r_pass);
};
static BrowserInstances s_browsers;
BrowserInstances::BrowserInstances(void)
{
m_instances = NULL;
m_active_instance = NULL;
m_last_instance_id = 0;
}
BrowserInstances::~BrowserInstances(void)
{
while(m_instances != NULL)
{
BrowserInstance *t_next;
t_next = m_instances -> next;
if (m_instances -> stack_id != NULL)
free(m_instances -> stack_id);
delete m_instances -> browser;
delete m_instances;
m_instances = t_next;
}
}
bool BrowserInstances::FindInstanceById(int p_id, BrowserInstance *&r_instance)
{
for(BrowserInstance *t_instance = m_instances; t_instance != nil; t_instance = t_instance->next)
{
if (t_instance->instance_id == p_id)
{
r_instance = t_instance;
return true;
}
}
return false;
}
void BrowserInstances::Add(CWebBrowserBase *p_browser, bool p_is_xbrowser)
{
BrowserInstance *t_instance;
t_instance = new (nothrow) BrowserInstance;
t_instance -> next = m_instances;
t_instance -> instance_id = ++m_last_instance_id;
t_instance -> stack_id = NULL;
t_instance -> browser = p_browser;
t_instance -> xbrowser_callbacks = p_is_xbrowser;
t_instance -> callback_depth = 0;
m_instances = t_instance;
m_active_instance = t_instance;
p_browser -> SetInst(t_instance -> instance_id);
}
void BrowserInstances::Delete(CWebBrowserBase *p_browser)
{
BrowserInstance *t_instance, *t_last_instance;
for(t_instance = m_instances, t_last_instance = NULL; t_instance != NULL; t_last_instance = t_instance, t_instance = t_instance -> next)
if (t_instance -> browser == p_browser)
break;
if (t_instance != NULL)
{
if (t_last_instance != NULL)
t_last_instance -> next = t_instance -> next;
else
m_instances = t_instance -> next;
if (m_active_instance == t_instance)
m_active_instance = m_instances;
if (t_instance -> stack_id != NULL)
free(t_instance -> stack_id);
delete t_instance -> browser;
delete t_instance;
}
}
int BrowserInstances::GetActiveInstanceId(void)
{
if (m_active_instance == NULL)
return 0;
return m_active_instance -> instance_id;
}
CWebBrowserBase *BrowserInstances::GetFirstInstance(void)
{
if (m_instances == NULL)
return NULL;
return m_instances -> browser;
}
CWebBrowserBase *BrowserInstances::GetActiveInstance(void)
{
if (m_active_instance == NULL)
return NULL;
return m_active_instance -> browser;
}
CWebBrowserBase *BrowserInstances::GetInstance(int p_id)
{
BrowserInstance *t_instance;
if (FindInstanceById(p_id, t_instance))
return t_instance->browser;
else
return nil;
}
void BrowserInstances::SetActiveInstanceId(int p_id)
{
BrowserInstance *t_instance;
if (FindInstanceById(p_id, t_instance))
m_active_instance = t_instance;
}
char *BrowserInstances::GetInstanceIds(void)
{
char *t_buffer;
t_buffer = nil;
for(BrowserInstance *t_instance = m_instances; t_instance != NULL; t_instance = t_instance -> next)
{
// AL-2013-11-01 [[ Bug 11289 ]] Use libcore methods to prevent potential buffer overflows in revbrowser
if (t_instance == m_instances)
MCCStringFormat(t_buffer, "%d", t_instance -> instance_id);
else
MCCStringAppendFormat(t_buffer, ",%d", t_instance -> instance_id);
}
return t_buffer;
}
int BrowserInstances::GetCallbackDepth(int p_id)
{
BrowserInstance *t_instance;
if (FindInstanceById(p_id, t_instance))
return t_instance->callback_depth;
else
return 0;
}
bool is_escape_char(char p_char)
{
return p_char == '"';
}
// IM-2014-03-06: [[ revBrowserCEF ]] Handle double-quote in strings by generating LC expression
// that evaluates to the original string (using string concatenation with the "quote" constant)
bool MCCStringQuote(const char *p_string, char *&r_quoted)
{
if (p_string == nil || p_string[0] == '\0')
return MCCStringClone("\"\"", r_quoted);
bool t_success;
t_success = true;
char *t_quoted;
t_quoted = nil;
while (t_success && *p_string)
{
if (!is_escape_char(*p_string))
{
const char *t_run_start;
t_run_start = p_string;
uint32_t t_run_length;
t_run_length = 0;
while (*p_string != '\0' && !is_escape_char(*p_string))
p_string++;
t_run_length = p_string - t_run_start;
t_success = MCCStringAppendFormat(t_quoted, t_quoted == nil ? "\"%*.*s\"" : "&\"%*.*s\"", t_run_length, t_run_length, t_run_start);
}
else if (*p_string == '"')
{
t_success = MCCStringAppend(t_quoted, t_quoted == nil ? "quote" : ""e");
p_string++;
}
}
if (t_success)
r_quoted = t_quoted;
else if (t_quoted != nil)
MCCStringFree(t_quoted);
return t_success;
}
#define MCSCRIPT_CALLBACK "\
local tID=%d;\
local tWinID=%d\
%s;%s\
global XBrowservar;\
if XBrowservar is empty or the windowId of XBrowservar is not tWinID then;\
repeat for each line tLine in the openStacks;\
if the windowId of stack tLine is tWinID then;\
put the long id of stack tLine into XBrowservar;\
exit repeat;\
end if;\
end repeat;\
end if;\
send \"%s tID%s\" to this card of XBrowservar"
// IM-2014-03-06: [[ revBrowserCEF ]] Create script to call handler with the given parameters
bool revBrowserCreateCallbackScript(int p_id, int p_window_id, const char *p_message, char **p_args, uint32_t p_arg_count, char *&r_script)
{
bool t_success;
t_success = true;
char *t_assigns;
t_assigns = nil;
char *t_locals;
t_locals = nil;
for (uint32_t i = 0; t_success && i < p_arg_count; i++)
{
char *t_quoted_string;
t_quoted_string = nil;
t_success = MCCStringQuote(p_args[i], t_quoted_string);
if (t_success)
{
t_success = MCCStringAppendFormat(t_locals, ", tArg%d", i);
if (t_success)
t_success = MCCStringAppendFormat(t_assigns, "put %s into tArg%d;", t_quoted_string, i);
}
if (t_quoted_string != nil)
MCCStringFree(t_quoted_string);
}
char *t_script;
t_script = nil;
if (t_success)
{
// IM-2014-03-13: [[ revBrowserCEF ]] fix const ptr compile error
const char *t_locals_str = t_locals ? t_locals : "";
const char *t_assigns_str = t_assigns ? t_assigns : "";
t_success = MCCStringFormat(t_script, MCSCRIPT_CALLBACK, p_id, p_window_id, t_locals_str, t_assigns_str, p_message, t_locals_str);
}
if (t_locals)
MCCStringFree(t_locals);
if (t_assigns)
MCCStringFree(t_assigns);
if (t_success)
r_script = t_script;
else
{
if (t_script)
MCCStringFree(t_script);
}
return t_success;
}
bool BrowserInstances::Callback(int p_id, const char *p_message, char **p_args, uint32_t p_arg_count, bool &r_pass)
{
bool t_success;
t_success = true;
BrowserInstance *t_instance;
if (t_success)
t_success = FindInstanceById(p_id, t_instance);
if (t_success)
{
int t_retval;
if (t_instance -> stack_id != NULL)
SetGlobalUTF8("XBrowservar", t_instance -> stack_id, &t_retval);
else
SetGlobalUTF8("XBrowservar", "", &t_retval);
}
char *t_script;
t_script = nil;
if (t_success)
t_success = revBrowserCreateCallbackScript(p_id, t_instance->browser->GetWindowId(), p_message, p_args, p_arg_count, t_script);
bool t_pass;
if (t_success)
SendMessage(t_instance, t_script, t_pass);
if (t_script)
MCCStringFree(t_script);
if (t_success)
r_pass = t_pass;
return t_success;
}
void BrowserInstances::SendMessage(BrowserInstance *p_instance, const char *p_message, bool &r_pass)
{
int t_retval;
SetGlobal(p_instance->xbrowser_callbacks ? "XBrowserCancel" : "browserCancel", "FALSE", &t_retval);
p_instance -> callback_depth += 1;
SendCardMessageUTF8(p_message, &t_retval);
p_instance -> callback_depth -= 1;
if (p_instance -> stack_id != NULL)
free(p_instance -> stack_id);
p_instance -> stack_id = GetGlobalUTF8("XBrowservar", &t_retval);
bool t_pass;
char *t_cancel;
t_cancel = GetGlobalUTF8(p_instance -> xbrowser_callbacks ? "XBrowserCancel" : "browserCancel", &t_retval);
if (t_cancel != NULL)
{
t_pass = !StrToBool(t_cancel);
free(t_cancel);
}
else
t_pass = true;
r_pass = t_pass;
}
bool BrowserInstances::Callback(int p_id, const char *p_message, const char *p_argument)
{
static const char *s_message_template =
"global XBrowservar;\
if XBrowservar is empty or the windowId of XBrowservar is not %d then;\
repeat for each line tLine in the openStacks;\
if the windowId of stack tLine is %d then;\
put the long id of stack tLine into XBrowservar;\
exit repeat;\
end if;\
end repeat;\
end if;\
send \"%s%s\" && %d to this card of XBrowservar";
static const char *s_xbrowser_message_template_with_argument =
"global XBrowservar;\
if XBrowservar is empty or the windowId of XBrowservar is not %d then;\
repeat for each line tLine in the openStacks;\
if the windowId of stack tLine is %d then;\
put the long id of stack tLine into XBrowservar;\
exit repeat;\
end if;\
end repeat;\
end if;\
send \"XBrowser_%s\" && quote & \"%s\" & quote, %d to this card of XBrowservar";
static const char *s_message_template_with_argument =
"global XBrowservar;\
if XBrowservar is empty or the windowId of XBrowservar is not %d then;\
repeat for each line tLine in the openStacks;\
if the windowId of stack tLine is %d then;\
put the long id of stack tLine into XBrowservar;\
exit repeat;\
end if;\
end repeat;\
end if;\
send \"browser%s\" && %d, quote & \"%s\" & quote to this card of XBrowservar";
BrowserInstance *t_instance;
if (!FindInstanceById(p_id, t_instance))
return true;
int t_retval;
if (t_instance -> stack_id != NULL)
SetGlobalUTF8("XBrowservar", t_instance -> stack_id, &t_retval);
else
SetGlobalUTF8("XBrowservar", "", &t_retval);
int t_window_id;
t_window_id = t_instance -> browser -> GetWindowId();
char *t_message;
t_message = nil;
// AL-2013-11-01 [[ Bug 11289 ]] Use libcore methods to prevent potential buffer overflows in revbrowser
if (p_argument == NULL)
MCCStringFormat(t_message, s_message_template, t_window_id, t_window_id, t_instance -> xbrowser_callbacks ? "XBrowser_" : "browser", p_message, p_id);
else
{
if (t_instance -> xbrowser_callbacks)
MCCStringFormat(t_message, s_xbrowser_message_template_with_argument, t_window_id, t_window_id, p_message, p_argument, p_id);
else
MCCStringFormat(t_message, s_message_template_with_argument, t_window_id, t_window_id, p_message, p_id, p_argument);
}
SetGlobalUTF8(t_instance -> xbrowser_callbacks ? "XBrowserCancel" : "browserCancel", "FALSE", &t_retval);
t_instance -> callback_depth += 1;
SendCardMessageUTF8(t_message, &t_retval);
t_instance -> callback_depth -= 1;
MCCStringFree (t_message);
if (t_instance -> stack_id != NULL)
free(t_instance -> stack_id);
t_instance -> stack_id = GetGlobalUTF8("XBrowservar", &t_retval);
bool t_pass;
char *t_cancel;
t_cancel = GetGlobal(t_instance -> xbrowser_callbacks ? "XBrowserCancel" : "browserCancel", &t_retval);
if (t_cancel != NULL)
{
t_pass = !StrToBool(t_cancel);
free(t_cancel);
}
else
t_pass = true;
return t_pass;
}
///////////////////////////////////////////////////////////////////////////////
//
// PROPERTY HANDLING
//
enum BrowserProperty
{
BROWSERPROP_UNDEFINED,
BROWSERPROP_BUSY,
BROWSERPROP_HTMLTEXT,
BROWSERPROP_RECT,
BROWSERPROP_URL,
BROWSERPROP_OFFLINE,
BROWSERPROP_VISIBLE,
BROWSERPROP_CONTEXTMENU,
BROWSERPROP_HTMLIMAGE,
BROWSERPROP_SCROLLBARS,
BROWSERPROP_SHOWBORDER,
BROWSERPROP_NEWWINDOW,
BROWSERPROP_SCALE,
BROWSERPROP_TITLE,
BROWSERPROP_VERSION,
BROWSERPROP_BROWSER, /* cb added for plugin control */
BROWSERPROP_INSTANCE, /* cb added for multi instance support */
BROWSERPROP_INSTLIST,
BROWSERPROP_MESSAGES,
BROWSERPROP_SELECTED, /* cb added for accessing selected text */
BROWSERPROP_VSCROLL,
BROWSERPROP_HSCROLL,
BROWSERPROP_FORMATTEDHEIGHT,
BROWSERPROP_FORMATTEDWIDTH,
BROWSERPROP_FORMATTEDRECT,
BROWSERPROP_WINDOWID,
BROWSERPROP_USERAGENT,
};
struct BrowserProp
{
BrowserProperty prop;
const char *str;
};
BrowserProp browserProperties[] =
{
{BROWSERPROP_BUSY,"BUSY"},
{BROWSERPROP_HTMLTEXT,"HTMLTEXT"},
{BROWSERPROP_RECT,"RECT"},
{BROWSERPROP_URL,"URL"},
{BROWSERPROP_OFFLINE,"OFFLINE"},
{BROWSERPROP_CONTEXTMENU,"CONTEXTMENU"},
{BROWSERPROP_VISIBLE,"VISIBLE"},
{BROWSERPROP_HTMLIMAGE,"HTMLIMAGE"},
{BROWSERPROP_SCROLLBARS,"SCROLLBARS"},
{BROWSERPROP_SHOWBORDER,"SHOWBORDER"},
{BROWSERPROP_NEWWINDOW,"NEWWINDOW"},
{BROWSERPROP_BROWSER, "BROWSER"},
{BROWSERPROP_TITLE, "TITLE"},
{BROWSERPROP_VERSION, "VERSION"},
{BROWSERPROP_SCALE, "SCALE"},
{BROWSERPROP_INSTANCE, "INSTANCE"},
{BROWSERPROP_INSTLIST, "INSTANCES"},
{BROWSERPROP_SELECTED, "SELECTED"},
{BROWSERPROP_MESSAGES, "MESSAGES"},
{BROWSERPROP_VSCROLL, "VSCROLL"},
{BROWSERPROP_HSCROLL, "HSCROLL"},
{BROWSERPROP_FORMATTEDHEIGHT, "FORMATTEDHEIGHT"},
{BROWSERPROP_FORMATTEDWIDTH, "FORMATTEDWIDTH"},
{BROWSERPROP_FORMATTEDRECT, "FORMATTEDRECT"},
{BROWSERPROP_WINDOWID, "WINDOWID"},
{BROWSERPROP_USERAGENT, "USERAGENT"},
};
///////////////////////////////////////////////////////////////////////////////
//
// CALLBACKS
//
// IM-2014-03-06: [[ revBrowserCEF ]] Send a custom callback with the given params
void CB_Custom(int p_instance_id, const char *p_message, char **p_args, uint32_t p_arg_count, bool *r_cancel)
{
bool t_success, t_pass;
t_success = s_browsers.Callback(p_instance_id, p_message, p_args, p_arg_count, t_pass);
*r_cancel = t_success && !t_pass;
}
// Callback:
// XBrowser_BeforeNavigate pURL
// Description:
// The browser sends this message before it navigates to a new URL.
//
// To prevent the navigation from occuring, set the global variable
// XBrowserCancel to true.
//
void CB_NavigateRequest(int p_instance_id, const char *p_url, bool *r_cancel)
{
*r_cancel = !s_browsers . Callback(p_instance_id, "BeforeNavigate", p_url);
}
// Callback:
// XBrowser_Over pElementId, pInstanceId
// Description:
// The browser sends this message when the mouse is moving over the
// given instance.
//
void CB_ElementEnter(int p_instance_id, const char *p_element)
{
s_browsers . Callback(p_instance_id, "Over", p_element);
}
// Callback:
// XBrowser_Out pElementId, pInstanceId
// Description:
// The browser sends this message when the mouse moves out of the
// given instance.
//
void CB_ElementLeave(int p_instance_id, const char *p_element)
{
s_browsers . Callback(p_instance_id, "Out", p_element);
}
// Callback:
// XBrowser_Click pElementId, pInstanceId
// Description:
// The browser sends this message when the mouse is clicked in the
// given instance.
//
void CB_ElementClick(int p_instance_id, const char *p_element)
{
s_browsers . Callback(p_instance_id, "Click", p_element);
}
// Callback:
// XBrowser_New pInstanceId
// Description:
// The browser sends this message when a new instance is created
// with the given instance id.
//
void CB_CreateInstance(int p_instance_id)
{
s_browsers . Callback(p_instance_id, "NewInstance");
}
// Callback:
// XBrowser_Closing pInstanceId
// Description:
// The browser sends this message when an instance is being
// destroyed.
//
void CB_DestroyInstance(int p_instance_id)
{
s_browsers . Callback(p_instance_id, "Closing");
}
// Callback:
// XBrowser_RequestDownload pURL, pInstanceId
// Description:
// The browser sends this message when a download has been
// requested.
//
// To prevent the navigation from occuring, set the global variable
// XBrowserCancel to true.
//
void CB_DownloadRequest(int p_instance_id, const char *p_url, bool *r_cancel)
{
*r_cancel = !s_browsers . Callback(p_instance_id, "DownloadRequest", p_url);
}
// Callback:
// XBrowser_BeforeNavigateFrame pURL, pInstanceId
// Description:
// The browser sends this message before it navigates to a new URL
// in a frame.
//
// To prevent the navigation from occuring, set the global variable
// XBrowserCancel to true.
//
void CB_NavigateFrameRequest(int p_instance_id, const char *p_url, bool *r_cancel)
{
*r_cancel = !s_browsers . Callback(p_instance_id, "BeforeNavigateFrame", p_url);
}
// Callback:
// XBrowser_NavigateComplete pURL, pInstanceId
// Description:
// The browser sends this message when a given URL has been navigated
// to, but not necessarily finished loading.
//
void CB_NavigateComplete(int p_instance_id, const char *p_url)
{
s_browsers . Callback(p_instance_id, "NavigateComplete", p_url);
}
// Callback:
// XBrowser_NavigateCompleteFrame pURL, pInstanceId
// Description:
// The browser sends this message when a given URL has been navigated
// to in a frame, but not necessarily finished loading.
//
void CB_NavigateFrameComplete(int p_instance_id, const char *p_url)
{
s_browsers . Callback(p_instance_id, "NavigateCompleteFrame", p_url);
}
// Callback:
// XBrowser_DocumentComplete pURL, pInstanceId
// Description:
// The browser sends this message when a given URL has finished
// loading.
//
void CB_DocumentComplete(int p_instance_id, const char *p_url)
{
s_browsers . Callback(p_instance_id, "DocumentComplete", p_url);
}
// Callback:
// browserDocumentFailed pInstanceId, pURL, pErrorMessage
// Description:
// The browser sends this message when a given URL has failed to load.
//
void CB_DocumentFailed(int p_instance_id, const char *p_url, const char *p_error)
{
const char *t_params[2];
t_params[0] = p_url;
t_params[1] = p_error;
bool t_pass;
s_browsers . Callback(p_instance_id, "browserDocumentFailed", (char**)t_params, 2, t_pass);
}
// Callback:
// XBrowser_DocumentCompleteFrame pURL, pInstanceId
// Description:
// The browser sends this message when a given URL has finished
// loading into a frame.
//
void CB_DocumentFrameComplete(int p_instance_id, const char *p_url)
{
s_browsers . Callback(p_instance_id, "DocumentCompleteFrame", p_url);
}
// Callback:
// browserDocumentFailedFrame pInstanceId, pURL, pErrorMessage
// Description:
// The browser sends this message when a given URL has failed to load into a frame.
//
void CB_DocumentFrameFailed(int p_instance_id, const char *p_url, const char *p_error)
{
const char *t_params[2];
t_params[0] = p_url;
t_params[1] = p_error;
bool t_pass;
s_browsers . Callback(p_instance_id, "browserDocumentFailedFrame", (char**)t_params, 2, t_pass);
}
// Callback:
// XBrowser_NewURLWindow pURL, pInstanceId
// Description:
// The browser sends this message when a given URL has been opened
// into a new window.
//
void CB_NewWindow(int p_instance_id, const char *p_url)
{
s_browsers . Callback(p_instance_id, "NewURLWindow", p_url);
}
///////////////////////////////////////////////////////////////////////////////
//
// COMMAND HANDLING
//
// Command:
// revBrowserOpen(pWindowID, [ pURL ])
// XBrowser_Open pWindowId, [ pURL ]
// Description:
// Open a new browser in the window with the given windowId, and navigate to
// the given URL.
// Result:
// The instance ID of the new browser.
//
void commonBrowserOpen(bool p_is_xbrowser, bool p_is_cef_browser, char *args[], int nargs, char **retstring, Bool *pass, Bool *error)
{
char *result = NULL;
char inID[255];
if( nargs > 0 )
{
CWebBrowserBase *t_browser;
if (p_is_cef_browser)
t_browser = MCCefBrowserInstantiate(atoi(args[0]));
else
t_browser = InstantiateBrowser(atoi(args[0]));
if (t_browser != NULL)
{
s_browsers . Add(t_browser, p_is_xbrowser);
sprintf(inID, "%i", s_browsers . GetActiveInstanceId());
result = istrdup(inID);
CB_CreateInstance(s_browsers . GetActiveInstanceId());
if (nargs == 2)
{
t_browser -> GoURL(args[1]);
t_browser -> SetVisible(TRUE);
}
}
else
{
*retstring = strdup("creation failed");
*error = True;
*pass = False;
return;
}
}
*error = False;
*pass = False;
*retstring = result != NULL ? result : (char *)calloc(1,0);
}
void revBrowserOpen(char *args[], int nargs, char **retstring, Bool *pass, Bool *error)
{
commonBrowserOpen(false, false, args, nargs, retstring, pass, error);
}
// IM-2014-03-18: [[ revBrowserCEF ]] create new browser instance using CEF
void revBrowserOpenCef(char *args[], int nargs, char **retstring, Bool *pass, Bool *error)
{
commonBrowserOpen(false, true, args, nargs, retstring, pass, error);
}
void XBrowserOpen(char *args[], int nargs, char **retstring, Bool *pass, Bool *error)
{
commonBrowserOpen(true, false, args, nargs, retstring, pass, error);
}
// Command:
// XBrowser_Init
// Description:
// No longer needed.
//
void XBrowserInit(char *args[], int nargs, char **retstring, Bool *pass, Bool *error)
{
char *result = NULL;
*error = False;
*pass = False;
*retstring = result != NULL ? result : (char *)calloc(1,0);
}
// Command:
// revBrowserNavigate pInstanceId, pURL
// XBrowser_Navigate pURL, [ pInstanceId ]
// Description:
// Navigate to the given URL in the browser identified by pInstanceId. If no instance
// id is specified, the last created browser is used.
//
void revBrowserNavigate(CWebBrowserBase *p_instance, char *args[], int nargs, char **retstring, Bool *pass, Bool *error)
{
char *result = NULL;
CWebBrowserBase *t_browser;
t_browser = p_instance;
if (t_browser != NULL)
{
if (nargs == 2)
t_browser -> GoURL(args[0], args[1]);
else
t_browser -> GoURL(args[0]);
}
*error = False;
*pass = False;
*retstring = result != NULL ? result: (char *)calloc(1,0);
}
// Command:
// revBrowserBack pInstanceId
// XBrowser_Back [ pInstanceId ]
// Description:
// Go back in the browser specified by pInstanceId. If no instance
// id is specified, the last created browser is used.
//
void revBrowserBack(CWebBrowserBase *p_instance, char *args[], int nargs, char **retstring, Bool *pass, Bool *error)
{
char *result = NULL;
CWebBrowserBase *t_browser;
t_browser = p_instance;
if (t_browser != NULL)
t_browser -> GoBack();
*error = False;
*pass = False;
*retstring = result != NULL ? result: (char *)calloc(1,0);
}
void revBrowserFind(CWebBrowserBase *p_instance, char *args[], int nargs, char **retstring, Bool *pass, Bool *error)
{
const char *t_result;
t_result = NULL;
CWebBrowserBase *t_browser;
if (t_result == NULL)
t_browser = p_instance;
if (t_result == NULL)
if (nargs != 2)
t_result = "incorrect number of arguments";
bool t_search_up;
t_search_up = false;
if (t_result == NULL)
t_search_up = strcmp(args[1], "up") == 0;
if (t_result == NULL && t_browser != NULL)
if (!t_browser -> FindString(args[0], t_search_up))
t_result = "not found";
*error = False;
*pass = False;
if (t_result == NULL)
*retstring = strdup("");
else
*retstring = strdup(t_result);
}
void revBrowserCallScript(CWebBrowserBase *p_instance, char *p_arguments[], int p_argument_count, char **r_result, Bool *r_pass, Bool *r_error)
{
*r_error = False;
// We must be given either at least 1 argument. The first argument is the function name to call,
// any subsequent arguments are parameters to the function.
if (!*r_error)
{
if (p_argument_count < 1)
{
*r_result = strdup("incorrect number of arguments");
*r_error = True;
}
}
CWebBrowserBase *t_browser;
if (!*r_error)
t_browser = p_instance;
char *t_result;
t_result = NULL;
if (!*r_error)
{
t_result = t_browser -> CallScript(p_arguments[0], &p_arguments[1], (p_argument_count - 1));
if (t_result == NULL)
{
*r_result = strdup("error in script");
*r_error = True;
}
else