-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin-class.cpp
More file actions
5712 lines (4686 loc) · 149 KB
/
plugin-class.cpp
File metadata and controls
5712 lines (4686 loc) · 149 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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* plugin-class.cpp: MoonLight browser plugin.
*
* Contact:
* Moonlight List (moonlight-list@lists.ximian.com)
*
* Copyright 2007 Novell, Inc. (http://www.novell.com)
*
* See the LICENSE file included with the distribution for details.
*
*/
#include <config.h>
#include <ctype.h>
#include "plugin-class.h"
#include "plugin-accessibility.h"
#include "plugin.h"
#include "deployment.h"
#include "bitmapimage.h"
#include "uri.h"
#include "textbox.h"
#include "grid.h"
#include "multiscaleimage.h"
#include "tilesource.h"
#include "deepzoomimagetilesource.h"
namespace Moonlight {
#ifdef DEBUG
#define DEBUG_WARN_NOTIMPLEMENTED(x) printf ("not implemented: (%s) " G_STRLOC "\n", x)
#define d(x) x
#else
#define DEBUG_WARN_NOTIMPLEMENTED(x)
#define d(x)
#endif
// debug scriptable object
#define ds(x)
// warnings
#define w(x) x
static char*
npidentifier_to_downstr (NPIdentifier id)
{
if (!MOON_NPN_IdentifierIsString (id))
return NULL;
NPUTF8 *strname = MOON_NPN_UTF8FromIdentifier (id);
char *p = strname;
while (*p) {
*p = g_ascii_tolower (*p);
p++;
}
return strname;
}
/* for use with bsearch & qsort */
static int
compare_mapping (const void *m1, const void *m2)
{
MoonNameIdMapping *map1 = (MoonNameIdMapping*) m1;
MoonNameIdMapping *map2 = (MoonNameIdMapping*) m2;
return strcmp(map1->name, map2->name);
}
static int
map_name_to_id (NPIdentifier name, const MoonNameIdMapping mapping[], int count)
{
char *strname = npidentifier_to_downstr (name);
if (!strname)
return NoMapping;
MoonNameIdMapping key, *result;
key.name = strname;
result = (MoonNameIdMapping*)bsearch(&key, mapping, count,
sizeof(MoonNameIdMapping), compare_mapping);
MOON_NPN_MemFree (strname);
if (!result)
return NoMapping;
return result->id;
}
static const char *
map_moon_id_to_event_name (int moon_id)
{
const char *name = NULL;
switch (moon_id) {
case MoonId_BufferingProgressChanged: name = "BufferingProgressChanged"; break;
case MoonId_CurrentStateChanged: name = "CurrentStateChanged"; break;
case MoonId_DownloadProgressChanged: name = "DownloadProgressChanged"; break;
case MoonId_GotFocus: name = "GotFocus"; break;
case MoonId_KeyDown: name = "KeyDown"; break;
case MoonId_KeyUp: name = "KeyUp"; break;
case MoonId_LostFocus: name = "LostFocus"; break;
case MoonId_Loaded: name = "Loaded"; break;
case MoonId_MarkerReached: name = "MarkerReached"; break;
case MoonId_MediaEnded: name = "MediaEnded"; break;
case MoonId_MediaFailed: name = "MediaFailed"; break;
case MoonId_MediaOpened: name = "MediaOpened"; break;
case MoonId_MouseEnter: name = "MouseEnter"; break;
case MoonId_MouseLeave: name = "MouseLeave"; break;
case MoonId_MouseMove: name = "MouseMove"; break;
case MoonId_MouseLeftButtonDown: name = "MouseLeftButtonDown"; break;
case MoonId_MouseLeftButtonUp: name = "MouseLeftButtonUp"; break;
case MoonId_OnResize: name = "Resize"; break;
case MoonId_OnFullScreenChange: name = "FullScreenChange"; break;
case MoonId_OnError: name = "Error"; break;
case MoonId_OnLoad: name = "Load"; break;
case MoonId_OnSourceDownloadProgressChanged: name = "SourceDownloadProgressChanged"; break;
case MoonId_OnSourceDownloadComplete: name = "SourceDownloadComplete"; break;
}
return name;
}
void
string_to_npvariant (const char *value, NPVariant *result)
{
char *retval;
if (value)
retval = NPN_strdup ((char *)value);
else
retval = NPN_strdup ("");
STRINGZ_TO_NPVARIANT (retval, *result);
}
static void
value_to_variant (NPObject *npobj, Value *v, NPVariant *result,
PluginInstance* plugin = NULL,
DependencyObject *parent_obj = NULL,
DependencyProperty *parent_property = NULL)
{
char utf8[8];
int n;
if (!v) {
NULL_TO_NPVARIANT (*result);
return;
}
switch (v->GetKind ()) {
case Type::BOOL:
BOOLEAN_TO_NPVARIANT (v->AsBool(), *result);
break;
case Type::CHAR:
n = g_unichar_to_utf8 (v->AsChar (), utf8);
utf8[n] = '\0';
string_to_npvariant (utf8, result);
break;
case Type::INT32:
INT32_TO_NPVARIANT (v->AsInt32(), *result);
break;
case Type::UINT32:
// XXX not much else we can do here...
DOUBLE_TO_NPVARIANT (v->AsUInt32(), *result);
break;
case Type::DOUBLE:
DOUBLE_TO_NPVARIANT (v->AsDouble(), *result);
break;
case Type::STRING:
string_to_npvariant (v->AsString(), result);
break;
case Type::DATETIME: {
if (!plugin)
plugin = ((MoonlightObject *) npobj)->GetPlugin ();
NPString string;
string.utf8characters = g_strdup_printf ("new Date(%" G_GINT64_FORMAT ")", v->AsDateTime());
string.utf8length = strlen (string.utf8characters);
bool ret = MOON_NPN_Evaluate (plugin->GetInstance (), plugin->GetHost (), &string, result);
#if DEBUG
if (!ret)
printf ("value_to_variant, error creating a Date object from %s\n", string.utf8characters);
#endif
g_free ((void*)string.utf8characters);
break;
}
case Type::POINT: {
MoonlightPoint *point = (MoonlightPoint *) MOON_NPN_CreateObject (((MoonlightObject *) npobj)->GetInstance (), MoonlightPointClass);
point->point = *v->AsPoint ();
object_to_npvariant (point, *result);
break;
}
case Type::RECT: {
MoonlightRect *rect = (MoonlightRect *) MOON_NPN_CreateObject (((MoonlightObject *) npobj)->GetInstance (), MoonlightRectClass);
rect->rect = *v->AsRect ();
object_to_npvariant (rect, *result);
break;
}
case Type::DURATION: {
MoonlightDuration *duration = (MoonlightDuration *) MOON_NPN_CreateObject (((MoonlightObject *) npobj)->GetInstance (), MoonlightDurationClass);
duration->SetParentInfo (parent_obj, parent_property);
object_to_npvariant (duration, *result);
break;
}
case Type::TIMESPAN: {
MoonlightTimeSpan *timespan = (MoonlightTimeSpan *) MOON_NPN_CreateObject (((MoonlightObject *) npobj)->GetInstance (), MoonlightTimeSpanClass);
timespan->SetParentInfo (parent_obj, parent_property);
object_to_npvariant (timespan, *result);
break;
}
case Type::GLYPHTYPEFACE: {
MoonlightGlyphTypeface *typeface = (MoonlightGlyphTypeface *) MOON_NPN_CreateObject (((MoonlightObject *) npobj)->GetInstance (), MoonlightGlyphTypefaceClass);
typeface->typeface = v->AsGlyphTypeface ();
object_to_npvariant (typeface, *result);
break;
}
case Type::GRIDLENGTH: {
MoonlightGridLength *gridlength = (MoonlightGridLength *) MOON_NPN_CreateObject (((MoonlightObject *) npobj)->GetInstance (), MoonlightGridLengthClass);
gridlength->SetParentInfo (parent_obj, parent_property);
object_to_npvariant (gridlength, *result);
break;
}
case Type::URI: {
string_to_npvariant (v->AsUri () ? v->AsUri ()->ToString () : "", result);
break;
}
case Type::FONTFAMILY: {
char *family = v->AsFontFamily() ? v->AsFontFamily()->source : NULL;
string_to_npvariant (family ? family : "", result);
break;
}
case Type::FONTWEIGHT: {
const char *weight = enums_int_to_str ("FontWeight", v->AsFontWeight() ? v->AsFontWeight()->weight : FontWeightsNormal);
string_to_npvariant (weight, result);
break;
}
case Type::FONTSTYLE: {
const char *style = enums_int_to_str ("FontStyle", v->AsFontStyle() ? v->AsFontStyle()->style : FontStylesNormal);
string_to_npvariant (style, result);
break;
}
case Type::FONTSTRETCH: {
const char *stretch = enums_int_to_str ("FontStretch", v->AsFontStretch() ? v->AsFontStretch()->stretch : FontStretchesNormal);
string_to_npvariant (stretch, result);
break;
}
case Type::COLOR: {
Color *c = v->AsColor ();
gint32 color = ((((gint32)(c->a * 255.0)) << 24) | (((gint32)(c->r * 255.0)) << 16) |
(((gint32)(c->g * 255.0)) << 8) | ((gint32)(c->b * 255.0)));
INT32_TO_NPVARIANT (color, *result);
break;
}
case Type::KEYTIME: {
MoonlightKeyTime *keytime = (MoonlightKeyTime *) MOON_NPN_CreateObject (((MoonlightObject *) npobj)->GetInstance (), MoonlightKeyTimeClass);
keytime->SetParentInfo (parent_obj, parent_property);
object_to_npvariant (keytime, *result);
break;
}
case Type::THICKNESS: {
MoonlightThickness *thickness = (MoonlightThickness *) MOON_NPN_CreateObject (((MoonlightObject *) npobj)->GetInstance (), MoonlightThicknessClass);
thickness->SetParentInfo (parent_obj, parent_property);
object_to_npvariant (thickness, *result);
break;
}
case Type::CORNERRADIUS: {
MoonlightCornerRadius *corner_radius = (MoonlightCornerRadius *) MOON_NPN_CreateObject (((MoonlightObject *) npobj)->GetInstance (), MoonlightCornerRadiusClass);
corner_radius->SetParentInfo (parent_obj, parent_property);
object_to_npvariant (corner_radius, *result);
break;
}
case Type::NPOBJ: {
NPObject *npobj = (NPObject *) v->AsNPObj ();
if (npobj == NULL) {
NULL_TO_NPVARIANT (*result);
} else {
object_to_npvariant (npobj, *result);
MOON_NPN_RetainObject (npobj);
}
break;
}
default:
/* more builtins.. */
if (v->Is (Deployment::GetCurrent (), Type::DEPENDENCY_OBJECT)) {
MoonlightEventObjectObject *depobj =
EventObjectCreateWrapper (((MoonlightObject *) npobj)->GetPlugin (), v->AsDependencyObject ());
object_to_npvariant (depobj, *result);
} else {
#if DEBUG
printf ("value_to_variant, can't create a variant of a %i = %s\n", v->GetKind (), Type::Find (Deployment::GetCurrent (), v->GetKind ())->GetName ());
#endif
NULL_TO_NPVARIANT (*result);
}
break;
}
}
void
variant_to_value (const NPVariant *v, Value **result)
{
switch (v->type) {
case NPVariantType_Bool:
*result = new Value (NPVARIANT_TO_BOOLEAN (*v));
break;
case NPVariantType_Int32:
*result = new Value ((int32_t) NPVARIANT_AS_INT32 (*v));
break;
case NPVariantType_Double:
*result = new Value (NPVARIANT_AS_DOUBLE (*v));
break;
case NPVariantType_String: {
char *value = STRDUP_FROM_VARIANT (*v);
*result = new Value (value, Type::STRING);
g_free (value);
break;
}
case NPVariantType_Void:
case NPVariantType_Null:
*result = new Value (Type::NPOBJ, NULL);
break;
case NPVariantType_Object:
*result = new Value (Type::NPOBJ, NPVARIANT_TO_OBJECT (*v));
break;
default:
d (printf ("Got invalid value from javascript.\n"));
*result = NULL;
break;
}
}
enum DependencyObjectClassNames {
COLLECTION_CLASS,
CONTROL_CLASS,
DEPENDENCY_OBJECT_CLASS,
UI_ELEMENT_CLASS,
DOWNLOADER_CLASS,
IMAGE_BRUSH_CLASS,
IMAGE_CLASS,
MEDIA_ELEMENT_CLASS,
STORYBOARD_CLASS,
STYLUS_INFO_CLASS,
STYLUS_POINT_COLLECTION_CLASS,
STROKE_COLLECTION_CLASS,
STROKE_CLASS,
TEXT_BOX_CLASS,
PASSWORD_BOX_CLASS,
GLYPHS_CLASS,
GLYPH_TYPEFACE_COLLECTION_CLASS,
TEXT_BLOCK_CLASS,
EVENT_ARGS_CLASS,
ROUTED_EVENT_ARGS_CLASS,
ERROR_EVENT_ARGS_CLASS,
KEY_EVENT_ARGS_CLASS,
TIMELINE_MARKER_ROUTED_EVENT_ARGS_CLASS,
MOUSE_EVENT_ARGS_CLASS,
DOWNLOAD_PROGRESS_EVENT_ARGS_CLASS,
LOG_READY_ROUTED_EVENT_ARGS_CLASS,
MULTI_SCALE_IMAGE_CLASS,
GENERAL_TRANSFORM_CLASS,
DEPENDENCY_OBJECT_CLASS_NAMES_LAST
};
NPClass *dependency_object_classes[DEPENDENCY_OBJECT_CLASS_NAMES_LAST];
bool
npobject_is_dependency_object (NPObject *obj)
{
for (int i = 0; i < DEPENDENCY_OBJECT_CLASS_NAMES_LAST; i++) {
if (dependency_object_classes [i] == obj->_class)
return true;
}
return false;
}
static bool
npvariant_is_dependency_object (NPVariant var)
{
if (!NPVARIANT_IS_OBJECT (var))
return false;
return npobject_is_dependency_object (NPVARIANT_TO_OBJECT (var));
}
static bool
npvariant_is_typeface (NPVariant var)
{
NPObject *obj;
if (!NPVARIANT_IS_OBJECT (var))
return false;
obj = NPVARIANT_TO_OBJECT (var);
return obj->_class == MoonlightGlyphTypefaceClass;
}
static bool
npvariant_is_object_class (NPVariant var, int type)
{
NPObject *obj;
if (type < 0 || type >= DEPENDENCY_OBJECT_CLASS_NAMES_LAST)
return false;
if (!NPVARIANT_IS_OBJECT (var))
return false;
obj = NPVARIANT_TO_OBJECT (var);
return obj->_class == dependency_object_classes[type];
}
#define npvariant_is_downloader(v) npvariant_is_object_class (v, DOWNLOADER_CLASS)
static bool
npvariant_is_moonlight_object (NPVariant var)
{
NPClass *moonlight_types[] = {
MoonlightContentClass,
MoonlightDurationClass,
MoonlightObjectClass,
MoonlightPointClass,
MoonlightScriptableObjectClass,
MoonlightScriptControlClass,
MoonlightSettingsClass,
MoonlightTimeSpanClass,
MoonlightGlyphTypefaceClass,
};
NPObject *obj;
guint i;
if (!NPVARIANT_IS_OBJECT (var))
return false;
obj = NPVARIANT_TO_OBJECT (var);
if (npobject_is_dependency_object (obj))
return true;
for (i = 0; i < G_N_ELEMENTS (moonlight_types); i++) {
if (obj->_class == moonlight_types[i])
return true;
}
return false;
}
EventListenerProxy::EventListenerProxy (PluginInstance *plugin, const char *event_name, const char *cb_name)
: EventObject (Type::EVENTLISTENERPROXY)
{
this->plugin = plugin;
this->event_name = g_strdup (event_name);
this->event_id = -1;
this->target_object = NULL;
this->owner = NULL;
this->one_shot = false;
this->is_func = false;
if (!strncmp (cb_name, "javascript:", strlen ("javascript:")))
cb_name += strlen ("javascript:");
this->callback = g_strdup (cb_name);
}
EventListenerProxy::EventListenerProxy (PluginInstance *plugin, const char *event_name, const NPVariant *cb)
: EventObject (Type::EVENTLISTENERPROXY)
{
this->plugin = plugin;
this->event_name = g_strdup (event_name);
this->event_id = -1;
this->target_object = NULL;
this->owner = NULL;
this->one_shot = false;
if (NPVARIANT_IS_OBJECT (*cb)) {
this->is_func = true;
this->callback = NPVARIANT_TO_OBJECT (*cb);
MOON_NPN_RetainObject ((NPObject *) this->callback);
} else {
this->is_func = false;
this->callback = STRDUP_FROM_VARIANT (*cb);
}
}
EventListenerProxy::~EventListenerProxy ()
{
if (is_func) {
// XXX we *want* to be able to do this, we really do, but we have no
// good means to invalidate EventListenerProxy, and thereby set the
// callback to NULL.
//
// instead we do it in ::RemoveHandler, which is only invoked via JS's removeEventListener
//
// if (callback != NULL)
// MOON_NPN_ReleaseObject ((NPObject *) callback);
}
else {
g_free (callback);
}
g_free (event_name);
}
const char *
EventListenerProxy::GetCallbackAsString ()
{
if (is_func)
return "";
return (const char *)callback;
}
void
EventListenerProxy::SetOwner (MoonlightObject *owner)
{
this->owner = owner;
}
int
EventListenerProxy::AddHandler (EventObject *obj)
{
target_object = obj;
event_id = obj->GetType()->LookupEvent (event_name);
if (event_id == -1) {
d(printf ("object of type `%s' does not provide an event named `%s'\n",
obj->GetTypeName(), event_name));
return -1;
}
this->ref ();
token = obj->AddHandler (event_id, proxy_listener_to_javascript, this, on_handler_removed);
return token;
}
int
EventListenerProxy::AddXamlHandler (EventObject *obj)
{
target_object = obj;
event_id = obj->GetType()->LookupEvent (event_name);
if (event_id == -1) {
d(printf ("object of type `%s' does not provide an event named `%s'\n",
obj->GetTypeName(), event_name));
return -1;
}
this->ref ();
token = obj->AddXamlHandler (event_id, proxy_listener_to_javascript, this, on_handler_removed);
return token;
}
void
EventListenerProxy::RemoveHandler ()
{
if (target_object && event_id != -1) {
target_object->RemoveHandler (event_id, token);
if (is_func && callback) {
MOON_NPN_ReleaseObject ((NPObject *) callback);
callback = NULL;
}
}
else {
ref (); // on_handler_removed unrefs.
on_handler_removed (target_object, event_id, token, this);
}
}
void
EventListenerProxy::on_handler_removed (EventObject *eo, int event_id, int token, gpointer closure)
{
// by the time we get here, the target_object has disclaimed
// all knowledge of this proxy.
EventListenerProxy *proxy = (EventListenerProxy *) closure;
if (proxy->owner) {
proxy->owner->ClearEventProxy (proxy);
}
else {
// we don't have an owner, so there's nothing special
// for us to do here.
}
proxy->target_object = NULL;
proxy->event_id = -1;
proxy->unref_delayed();
}
void
EventListenerProxy::proxy_listener_to_javascript (EventObject *sender, EventArgs *calldata, gpointer closure)
{
EventListenerProxy *proxy = (EventListenerProxy *) closure;
EventObject *js_sender = sender;
NPVariant args[2];
NPVariant result;
int argcount = 1;
PluginInstance *plugin = proxy->GetPlugin ();
Deployment *previous_deployment;
if (calldata && calldata->Is (Type::ERROREVENTARGS)) {
js_sender = ((ErrorEventArgs *)calldata)->GetOriginalSource ();
#if DEBUG
g_warn_if_fail (js_sender != NULL);
#endif
}
if (js_sender && js_sender->GetObjectType () == Type::SURFACE) {
// This is somewhat hackish, but is required for
// the FullScreenChanged event (js expects the
// sender to be the toplevel canvas, not the surface,
// nor the content).
js_sender = ((Surface *)js_sender)->GetToplevel ();
}
if (plugin == NULL || plugin->HasShutdown ()) {
// Firefox can invalidate our NPObjects after the plugin itself
// has been destroyed. During this invalidation our NPObjects call
// into the moonlight runtime, which then emits events.
d(printf ("Moonlight: The plugin has been deleted, but we're still emitting events?\n"));
return;
}
// do not let cross-domain application re-register the events (e.g. via scripting)
if (plugin->IsCrossDomainApplication ()) {
const char *name = proxy->GetCallbackAsString ();
// g_warning ("xdomain restriction on javascript event: %s", name);
// OnLoad is a special case and is emmitted if CrossDomainAccess.ScriptableOnly is specified and if EnabledHtmlAccess == true
if (strcmp ("OnLoad", name) != 0)
return;
if (Deployment::GetCurrent ()->GetExternalCallersFromCrossDomain () != CrossDomainAccessScriptableOnly)
return;
if (!plugin->GetEnableHtmlAccess ())
return;
// OnLoad might be emitted but it does not provide a sender nor data (see DRT 361 / 366)
js_sender = NULL;
calldata = NULL;
NULL_TO_NPVARIANT (args[1]);
argcount++;
}
previous_deployment = Deployment::GetCurrent ();
Deployment::SetCurrent (plugin->GetDeployment ());
//
// if (js_sender && (js_sender->GetObjectType () == Type::SURFACE)) {
// // This is somewhat hackish, but is required for
// // the FullScreenChanged event (js expects the
// // sender to be the toplevel canvas, not the surface,
// // nor the content).
// js_sender = ((Surface*) js_sender)->GetToplevel ();
// }
MoonlightEventObjectObject *depobj = NULL;
if (js_sender) {
depobj = EventObjectCreateWrapper (plugin, js_sender);
plugin->AddCleanupPointer (&depobj);
object_to_npvariant (depobj, args[0]);
} else {
NULL_TO_NPVARIANT (args[0]);
}
//printf ("proxying event %s to javascript, sender = %p (%s)\n", proxy->event_name, sender, sender->GetTypeName ());
MoonlightEventObjectObject *depargs = NULL;
if (calldata) {
depargs = EventObjectCreateWrapper (plugin, calldata);
plugin->AddCleanupPointer (&depargs);
object_to_npvariant (depargs, args[1]);
argcount++;
}
if (proxy->is_func && proxy->callback) {
/* the event listener was added with a JS function object */
if (MOON_NPN_InvokeDefault (proxy->GetInstance (), (NPObject *) proxy->callback, args, argcount, &result))
MOON_NPN_ReleaseVariantValue (&result);
} else if (proxy->callback != NULL && ((char *) proxy->callback) [0] != 0) {
/* the event listener was added with a JS string (the function name) */
NPObject *object = NULL;
if (MOON_NPN_GetValue (proxy->GetInstance (), NPNVWindowNPObject, &object) == NPERR_NO_ERROR) {
if (MOON_NPN_Invoke (proxy->GetInstance (), object, NPID ((char *) proxy->callback), args, argcount, &result))
MOON_NPN_ReleaseVariantValue (&result);
}
}
if (depobj) {
plugin->RemoveCleanupPointer (&depobj);
MOON_NPN_ReleaseObject (depobj);
}
if (depargs) {
plugin->RemoveCleanupPointer (&depargs);
MOON_NPN_ReleaseObject (depargs);
}
if (proxy->one_shot)
proxy->RemoveHandler();
Deployment::SetCurrent (previous_deployment);
}
void
event_object_add_xaml_listener (EventObject *obj, PluginInstance *plugin, const char *event_name, const char *cb_name)
{
EventListenerProxy *proxy = new EventListenerProxy (plugin, event_name, cb_name);
proxy->AddXamlHandler (obj);
proxy->unref ();
}
class NamedProxyPredicate {
public:
NamedProxyPredicate (char *name) { this->name = g_strdup (name); }
~NamedProxyPredicate () { g_free (name); }
static bool matches (int token, EventHandler cb_handler, gpointer cb_data, gpointer data)
{
if (cb_handler != EventListenerProxy::proxy_listener_to_javascript)
return false;
if (cb_data == NULL)
return false;
EventListenerProxy *proxy = (EventListenerProxy*)cb_data;
NamedProxyPredicate *pred = (NamedProxyPredicate*)data;
return !strcasecmp (proxy->GetCallbackAsString(), pred->name);
}
private:
char *name;
};
/*** EventArgs **/
static NPObject *
event_args_allocate (NPP instance, NPClass *klass)
{
return new MoonlightEventArgs (instance);
}
MoonlightEventArgsType::MoonlightEventArgsType ()
{
allocate = event_args_allocate;
}
MoonlightEventArgsType *MoonlightEventArgsClass;
/*** RoutedEventArgs ***/
static NPObject *
routedeventargs_allocate (NPP instance, NPClass *klass)
{
return new MoonlightRoutedEventArgs (instance);
}
static const MoonNameIdMapping
routedeventargs_mapping[] = {
{ "source", MoonId_Source },
};
bool
MoonlightRoutedEventArgs::GetProperty (int id, NPIdentifier name, NPVariant *result)
{
RoutedEventArgs *args = GetRoutedEventArgs ();
switch (id) {
case MoonId_Source: {
DependencyObject *source = args->GetSource ();
if (source) {
MoonlightEventObjectObject *source_obj = EventObjectCreateWrapper (GetPlugin (), source);
object_to_npvariant (source_obj, *result);
}
else {
NULL_TO_NPVARIANT (*result);
}
return true;
}
default:
return MoonlightEventArgs::GetProperty (id, name, result);
}
}
MoonlightRoutedEventArgsType::MoonlightRoutedEventArgsType ()
{
allocate = routedeventargs_allocate;
AddMapping (routedeventargs_mapping, G_N_ELEMENTS (routedeventargs_mapping));
}
MoonlightRoutedEventArgsType *MoonlightRoutedEventArgsClass;
/*** ErrorEventArgs ***/
static NPObject *
erroreventargs_allocate (NPP instance, NPClass *klass)
{
return new MoonlightErrorEventArgs (instance);
}
static const MoonNameIdMapping
erroreventargs_mapping[] = {
{ "charposition", MoonId_CharPosition },
{ "errorcode", MoonId_ErrorCode },
{ "errormessage", MoonId_ErrorMessage },
{ "errortype", MoonId_ErrorType },
{ "linenumber", MoonId_LineNumber },
{ "methodname", MoonId_MethodName },
{ "xamlfile", MoonId_XamlFile },
};
bool
MoonlightErrorEventArgs::GetProperty (int id, NPIdentifier name, NPVariant *result)
{
ErrorEventArgs *args = GetErrorEventArgs ();
switch (id) {
case MoonId_ErrorCode:
INT32_TO_NPVARIANT (args->GetErrorCode(), *result);
return true;
case MoonId_ErrorType:
switch (args->GetErrorType()) {
case NoError: string_to_npvariant ("NoError", result); break;
case UnknownError: string_to_npvariant ("UnknownError", result); break;
case InitializeError: string_to_npvariant ("InitializeError", result); break;
case ParserError: string_to_npvariant ("ParserError", result); break;
case ObjectModelError: string_to_npvariant ("ObjectModelError", result); break;
case RuntimeError: string_to_npvariant ("RuntimeError", result); break;
case DownloadError: string_to_npvariant ("DownloadError", result); break;
case MediaError: string_to_npvariant ("MediaError", result); break;
case ImageError: string_to_npvariant ("ImageError", result); break;
}
return true;
case MoonId_ErrorMessage:
string_to_npvariant (args->GetErrorMessage(), result);
return true;
case MoonId_LineNumber:
if (args->GetErrorType() == ParserError) {
INT32_TO_NPVARIANT (((ParserErrorEventArgs*)args)->line_number, *result);
} else {
DEBUG_WARN_NOTIMPLEMENTED ("ErrorEventArgs.lineNumber");
INT32_TO_NPVARIANT (0, *result);
}
return true;
case MoonId_CharPosition:
if (args->GetErrorType() == ParserError) {
INT32_TO_NPVARIANT (((ParserErrorEventArgs*)args)->char_position, *result);
} else {
DEBUG_WARN_NOTIMPLEMENTED ("ErrorEventArgs.charPosition");
INT32_TO_NPVARIANT (0, *result);
}
return true;
case MoonId_MethodName:
DEBUG_WARN_NOTIMPLEMENTED ("ErrorEventArgs.methodName");
NULL_TO_NPVARIANT (*result);
return true;
case MoonId_XamlFile:
if (args->GetErrorType() == ParserError) {
string_to_npvariant (((ParserErrorEventArgs*)args)->xaml_file, result);
} else {
DEBUG_WARN_NOTIMPLEMENTED ("ErrorEventArgs.xamlFile");
NULL_TO_NPVARIANT (*result);
}
return true;
default:
return MoonlightEventArgs::GetProperty (id, name, result);
}
}
MoonlightErrorEventArgsType::MoonlightErrorEventArgsType ()
{
allocate = erroreventargs_allocate;
AddMapping (erroreventargs_mapping, G_N_ELEMENTS (erroreventargs_mapping));
}
MoonlightErrorEventArgsType *MoonlightErrorEventArgsClass;
/*** Typefaces ***/
static NPObject *
moonlight_glyph_typeface_allocate (NPP instance, NPClass *klass)
{
return new MoonlightGlyphTypeface (instance);
}
static const MoonNameIdMapping
glyph_typeface_mapping[] = {
{ "majorversion", MoonId_MajorVersion },
{ "minorversion", MoonId_MinorVersion },
{ "fonturi", MoonId_FontUri },
};
bool
MoonlightGlyphTypeface::GetProperty (int id, NPIdentifier name, NPVariant *result)
{
switch (id) {
case MoonId_MajorVersion:
INT32_TO_NPVARIANT (typeface->GetMajorVersion (), *result);
return true;
case MoonId_MinorVersion:
INT32_TO_NPVARIANT (typeface->GetMinorVersion (), *result);
return true;
case MoonId_FontUri:
string_to_npvariant (typeface->GetFontUri (), result);
return true;
default:
return MoonlightObject::GetProperty (id, name, result);
}
}
bool
MoonlightGlyphTypeface::SetProperty (int id, NPIdentifier name, const NPVariant *value)
{
return MoonlightObject::SetProperty (id, name, value);
}
MoonlightGlyphTypefaceType::MoonlightGlyphTypefaceType ()
{
AddMapping (glyph_typeface_mapping, G_N_ELEMENTS (glyph_typeface_mapping));
allocate = moonlight_glyph_typeface_allocate;
}
MoonlightGlyphTypefaceType *MoonlightGlyphTypefaceClass;
/*** MoonlightGlyphTypefaceCollectionClass ****************************************/
static NPObject *
moonlight_glyph_typeface_collection_allocate (NPP instance, NPClass *klass)
{
return new MoonlightGlyphTypefaceCollectionObject (instance);
}
bool
MoonlightGlyphTypefaceCollectionObject::Invoke (int id, NPIdentifier name, const NPVariant *args, guint32 argCount, NPVariant *result)
{
GlyphTypefaceCollection *typefaces = (GlyphTypefaceCollection *) GetDependencyObject ();
MoonlightGlyphTypeface *wrapper;
GlyphTypeface *typeface;
int index;
switch (id) {
case MoonId_GetItem:
if (!check_arg_list ("d", argCount, args))
THROW_JS_EXCEPTION ("getItem");
if ((index = NPVARIANT_AS_INT32 (args[0])) < 0)
THROW_JS_EXCEPTION ("getItem");
if (index >= typefaces->GetCount ()) {
NULL_TO_NPVARIANT (*result);
return true;
}
typeface = typefaces->GetValueAt (index)->AsGlyphTypeface ();
wrapper = (MoonlightGlyphTypeface *) MOON_NPN_CreateObject (GetInstance (), MoonlightGlyphTypefaceClass);
wrapper->typeface = typeface;
object_to_npvariant (wrapper, *result);
return true;
default:
return MoonlightDependencyObjectObject::Invoke (id, name, args, argCount, result);
}
}
MoonlightGlyphTypefaceCollectionType::MoonlightGlyphTypefaceCollectionType ()
{
allocate = moonlight_glyph_typeface_collection_allocate;
}
/*** Points ***/
static NPObject *
point_allocate (NPP instance, NPClass *klass)
{
return new MoonlightPoint (instance);
}
static const MoonNameIdMapping
point_mapping[] = {
{ "x", MoonId_X },
{ "y", MoonId_Y }
};
bool
MoonlightPoint::GetProperty (int id, NPIdentifier name, NPVariant *result)
{
switch (id) {
case MoonId_X:
DOUBLE_TO_NPVARIANT (point.x, *result);
return true;
case MoonId_Y:
DOUBLE_TO_NPVARIANT (point.y, *result);
return true;
default: