forked from mono/moon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.cpp
More file actions
2072 lines (1706 loc) · 53.4 KB
/
plugin.cpp
File metadata and controls
2072 lines (1706 loc) · 53.4 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 -*- */
/*
* moon-plugin.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 <glib.h>
#include <fcntl.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <errno.h>
#include "plugin.h"
#include "plugin-spinner.h"
#include "plugin-class.h"
#include "browser-bridge.h"
#include "downloader.h"
#include "plugin-downloader.h"
#include "npstream-request.h"
#include "xap.h"
#include "window.h"
#include "unzip.h"
#include "deployment.h"
#include "uri.h"
#include "timemanager.h"
#if PAL_GTK_WINDOWING
#include "pal/gtk/windowless-gtk.h"
#elif PAL_COCOA_WINDOWING
#include "pal/cocoa/windowless-cocoa.h"
#endif
#ifdef DEBUG
#define d(x) x
#else
#define d(x)
#endif
#define w(x) x
namespace Moonlight {
extern guint32 moonlight_flags;
char *
NPN_strdup (const char *tocopy)
{
int len = strlen(tocopy);
char *ptr = (char *)MOON_NPN_MemAlloc (len+1);
if (ptr != NULL) {
strcpy (ptr, tocopy);
// WebKit should calloc so we dont have to do this
ptr[len] = 0;
}
return ptr;
}
/*** PluginInstance:: *********************************************************/
GSList *plugin_instances = NULL;
#if PAL_GTK_WINDOWING
static MoonWindow *
create_gtk_windowless (int width, int height, PluginInstance *forPlugin)
{
return new MoonWindowlessGtk (width, height, forPlugin);
}
#elif PAL_COCOA_WINDOWING
static MoonWindow *
create_cocoa_windowless (int width, int height, PluginInstance *forPlugin)
{
return new MoonWindowlessCocoa (width, height, forPlugin);
}
#endif
PluginInstance::PluginInstance (NPP instance, guint16 mode)
{
refcount = 1;
this->instance = instance;
this->mode = mode;
window = NULL;
connected_to_container = false;
rootobject = NULL;
surface = NULL;
moon_window = NULL;
// Property fields
source_location = NULL;
source_location_original = NULL;
initParams = NULL;
source = NULL;
source_original = NULL;
source_idle = 0;
onLoad = NULL;
onError = NULL;
onResize = NULL;
onSourceDownloadProgressChanged = NULL;
onSourceDownloadComplete = NULL;
splashscreensource = NULL;
background = NULL;
id = NULL;
culture = NULL;
uiCulture = NULL;
windowless = false;
cross_domain_app = false; // false, since embedded xaml (in html) won't load anything (to change this value)
default_enable_html_access = true; // should we use the default value (wrt the HTML script supplied value)
enable_html_access = true; // an empty plugin must return TRUE before loading anything else (e.g. scripting)
default_allow_html_popup_window = true; // use the default value (popup allowed on same-domain, limited on xdomain)
allow_html_popup_window = false;
enable_redraw_regions = false;
enable_navigation = true; // true (all / default), false (none)
enable_gpu_acceleration = false;
xembed_supported = FALSE;
loading_splash = false;
is_splash = false;
is_shutting_down = false;
is_reentrant_mess = false;
has_shutdown = false;
progress_changed_token = -1;
bridge = NULL;
// MSDN says the default is 24: http://msdn2.microsoft.com/en-us/library/bb979688.aspx
// blog says the default is 60: http://blogs.msdn.com/seema/archive/2007/10/07/perf-debugging-tips-enableredrawregions-a-performance-bug-in-videobrush.aspx
// testing seems to confirm that the default is 60.
enable_framerate_counter = false;
maxFrameRate = 60;
xaml_loader = NULL;
timers = NULL;
wrapped_objects = g_hash_table_new (g_direct_hash, g_direct_equal);
cleanup_pointers = NULL;
if (plugin_instances == NULL) {
// first plugin is initialized
// FIXME add some ifdefs + runtime checks here
#if PAL_GTK_WINDOWING
Runtime::GetWindowingSystem ()->SetWindowlessCtor (create_gtk_windowless);
#elif PAL_COCOA_WINDOWING
Runtime::GetWindowingSystem ()->SetWindowlessCtor (create_cocoa_windowless);
#else
#error "no PAL windowing system"
#endif
}
plugin_instances = g_slist_append (plugin_instances, instance);
accessibility_bridge = new AccessibilityBridge ();
download_dir = NULL;
}
void
PluginInstance::Recreate (const char *source)
{
//printf ("PluginInstance::Recreate (%s) this: %p, instance->pdata: %p\n", source, this, instance->pdata);
int argc = 16;
char *maxFramerate = g_strdup_printf ("%i", maxFrameRate);
const char *argn [] =
{ "initParams", "onLoad", "onError", "onResize",
"source", "background", "windowless", "maxFramerate", "id",
"enableFrameRateCounter", "enableRedrawRegions",
"enablehtmlaccess", "allowhtmlpopupwindow", "splashscreensource",
"enablenavigation",
"onSourceDownloadProgressChanged", "onSourceDownloadComplete",
"culture", "uiculture", "enablegpuacceleration", NULL };
const char *argv [] =
{ initParams, onLoad, onError, onResize,
source, background, windowless ? "true" : "false", maxFramerate, id,
GetEnableFrameRateCounter () ? "true" : "false",
GetEnableRedrawRegions () ? "true" : "false",
enable_html_access ? "true" : "false", allow_html_popup_window ? "true" : "false", splashscreensource,
GetEnableNavigation () ? "all" : "none",
onSourceDownloadProgressChanged, onSourceDownloadComplete,
culture, uiCulture, enable_gpu_acceleration ? "true" : "false", NULL };
PluginInstance *result;
result = new PluginInstance (instance, mode);
instance->pdata = result;
// printf ("PluginInstance::Recreate (%s), created %p\n", source, result);
/* steal the root object, we need to use the same instance */
result->rootobject = rootobject;
rootobject = NULL;
if (result->rootobject)
result->rootobject->PreSwitchPlugin (this, result);
result->cross_domain_app = cross_domain_app;
result->default_enable_html_access = default_enable_html_access;
result->default_allow_html_popup_window = default_allow_html_popup_window;
result->connected_to_container = connected_to_container;
result->Initialize (argc, (char **) argn, (char **) argv);
// printf ("PluginInstance::Recreate (%s), new plugin's deployment: %p, current deployment: %p\n", source, result->deployment, Deployment::GetCurrent ());
if (surface) {
result->moon_window = surface->DetachWindow (); /* we reuse the same MoonWindow */
} else {
result->moon_window = NULL;
}
result->window = window;
result->CreateWindow ();
g_free (maxFramerate);
/* destroy the current plugin instance */
instance->pdata = this;
Deployment::SetCurrent (deployment);
Shutdown ();
instance->pdata = NULL;
unref (); /* the ref instance->pdata has */
/* put in the new plugin instance */
Deployment::SetCurrent (result->deployment);
instance->pdata = result;
if (result->rootobject) {
/* We need to reconnect all event handlers js might have to our root objects */
result->rootobject->PostSwitchPlugin (this, result);
}
// printf ("PluginInstance::Recreate (%s) [Done], new plugin: %p\n", source, result);
}
PluginInstance::~PluginInstance ()
{
deployment->unref_delayed ();
delete accessibility_bridge;
}
void
PluginInstance::ref ()
{
g_assert (refcount > 0);
g_atomic_int_inc (&refcount);
}
void
PluginInstance::unref ()
{
g_assert (refcount > 0);
int v = g_atomic_int_exchange_and_add (&refcount, -1) - 1;
if (v == 0)
delete this;
}
bool
PluginInstance::IsShuttingDown ()
{
VERIFY_MAIN_THREAD;
return is_shutting_down;
}
bool
PluginInstance::HasShutdown ()
{
VERIFY_MAIN_THREAD;
return has_shutdown;
}
void
PluginInstance::Shutdown ()
{
// Kill timers
GSList *p;
g_return_if_fail (!is_shutting_down);
g_return_if_fail (!has_shutdown);
is_shutting_down = true;
if (bridge)
bridge->Shutdown ();
Deployment::SetCurrent (deployment);
accessibility_bridge->Shutdown ();
// Destroy the XAP application
DestroyApplication ();
for (p = timers; p != NULL; p = p->next){
guint32 source_id = GPOINTER_TO_INT (p->data);
g_source_remove (source_id);
}
g_slist_free (p);
timers = NULL;
g_hash_table_destroy (wrapped_objects);
wrapped_objects = NULL;
// Remove us from the list.
plugin_instances = g_slist_remove (plugin_instances, instance);
for (GSList *l = cleanup_pointers; l; l = l->next) {
gpointer* p = (gpointer*)l->data;
*p = NULL;
}
g_slist_free (cleanup_pointers);
cleanup_pointers = NULL;
if (rootobject) {
MOON_NPN_ReleaseObject (rootobject);
rootobject = NULL;
}
g_free (background);
background = NULL;
g_free (id);
id = NULL;
g_free (onSourceDownloadProgressChanged);
onSourceDownloadProgressChanged = NULL;
g_free (onSourceDownloadComplete);
onSourceDownloadComplete = NULL;
g_free (splashscreensource);
splashscreensource = NULL;
g_free (culture);
culture = NULL;
g_free (uiCulture);
uiCulture = NULL;
g_free (initParams);
initParams = NULL;
delete xaml_loader;
xaml_loader = NULL;
g_free (source);
source = NULL;
g_free (source_original);
source_original = NULL;
g_free (source_location);
source_location = NULL;
g_free (source_location_original);
source_location_original = NULL;
if (source_idle) {
g_source_remove (source_idle);
source_idle = 0;
}
//
// The code below was an attempt at fixing this, but we are still getting spurious errors
// we might have another source of problems
//
//fprintf (stderr, "Destroying the surface: %p, plugin: %p\n", surface, this);
if (surface != NULL) {
//gdk_error_trap_push ();
surface->Zombify();
surface->unref_delayed();
//gdk_error_trap_pop ();
surface = NULL;
}
deployment->Shutdown ();
if (bridge) {
delete bridge;
bridge = NULL;
}
is_shutting_down = false;
has_shutdown = true;
g_free (onLoad);
onLoad = NULL;
g_free (onError);
onError = NULL;
g_free (onResize);
onResize = NULL;
/* Remove temporary files */
if (download_dir != NULL) {
RemoveDir (download_dir);
g_free (download_dir);
download_dir = NULL;
}
}
static bool
same_site_of_origin (const char *url1, const char *url2)
{
bool result = false;
Uri *uri1;
if (url1 == NULL || url2 == NULL)
return true;
uri1 = Uri::Create (url1);
if (uri1 != NULL) {
Uri *uri2 = Uri::Create (url2);
if (uri2 != NULL) {
// if only one of the two URI is absolute then the second one is relative to the first,
// which makes it part of the same site of origin
if ((uri1->IsAbsolute () && !uri2->IsAbsolute ()) || (!uri1->IsAbsolute () && uri2->IsAbsolute ()))
result = true;
else
result = Uri::SameSiteOfOrigin (uri1, uri2);
}
delete uri2;
}
delete uri1;
return result;
}
static bool
parse_bool_arg (const char *arg)
{
bool b;
return Xaml::BoolFromStr (arg, &b) && b;
}
void
PluginInstance::Initialize (int argc, char* argn[], char* argv[])
{
for (int i = 0; i < argc; i++) {
if (argn[i] == NULL) {
//g_warning ("PluginInstance::Initialize, arg %d == NULL", i);
continue;
}
else if (!g_ascii_strcasecmp (argn[i], "initParams")) {
initParams = g_strdup (argv[i]);
}
else if (!g_ascii_strcasecmp (argn[i], "onLoad")) {
onLoad = g_strdup (argv[i]);
}
else if (!g_ascii_strcasecmp (argn[i], "onError")) {
onError = g_strdup (argv[i]);
}
else if (!g_ascii_strcasecmp (argn[i], "onResize")) {
onResize = g_strdup (argv[i]);
}
else if (!g_ascii_strcasecmp (argn[i], "src") || !g_ascii_strcasecmp (argn[i], "source")) {
/* There is a new design pattern that creates a silverlight object with data="data:application/x-silverlight,"
* firefox is passing this to us as the src element. We need to ensure we dont set source to this value
* as this design pattern sets a xap up after the fact, but checks to ensure Source hasn't been set yet.
*
* eg: http://theamazingalbumcoveratlas.org/
*
* TODO: Find a site that has data:application/x-silverlight,SOMEDATA and figure out what we do with it
*/
if (g_ascii_strncasecmp (argv[i], "data:application/x-silverlight", 30) != 0 && argv[i][strlen(argv[i])-1] != ',') {
source = g_strdup (argv[i]);
// we must be able to retrieve the original source, e.g. after a redirect
source_original = g_strdup (source);
}
}
else if (!g_ascii_strcasecmp (argn[i], "background")) {
background = g_strdup (argv[i]);
}
else if (!g_ascii_strcasecmp (argn [i], "windowless")) {
windowless = parse_bool_arg (argv [i]);
}
else if (!g_ascii_strcasecmp (argn [i], "maxFramerate")) {
maxFrameRate = atoi (argv [i]);
}
else if (!g_ascii_strcasecmp (argn [i], "enableFrameRateCounter")) {
enable_framerate_counter = parse_bool_arg (argv [i]);
}
else if (!g_ascii_strcasecmp (argn [i], "enableRedrawRegions")) {
enable_redraw_regions = parse_bool_arg (argv [i]);
}
else if (!g_ascii_strcasecmp (argn [i], "enableGpuAcceleration")) {
enable_gpu_acceleration = parse_bool_arg (argv [i]);
}
else if (!g_ascii_strcasecmp (argn [i], "id")) {
id = g_strdup (argv [i]);
}
else if (!g_ascii_strcasecmp (argn [i], "enablehtmlaccess")) {
default_enable_html_access = false; // we're using the application value, not the default one
enable_html_access = parse_bool_arg (argv [i]);
}
else if (!g_ascii_strcasecmp (argn [i], "enablenavigation")) {
// default value is 'all', 'none' means navigation is disabled
// <quote>Strings other than none are ignored and treated as the default all case.</quote>
// http://msdn.microsoft.com/en-us/library/dd833071(v=VS.95).aspx
enable_navigation = g_ascii_strcasecmp (argv [i], "none");
}
else if (!g_ascii_strcasecmp (argn [i], "allowhtmlpopupwindow")) {
default_allow_html_popup_window = false; // we're using the application value, not the default one
allow_html_popup_window = parse_bool_arg (argv [i]);
}
else if (!g_ascii_strcasecmp (argn [i], "splashscreensource")) {
splashscreensource = g_strdup (argv [i]);
}
else if (!g_ascii_strcasecmp (argn [i], "onSourceDownloadProgressChanged")) {
onSourceDownloadProgressChanged = g_strdup (argv [i]);
}
else if (!g_ascii_strcasecmp (argn [i], "onSourceDownloadComplete")) {
onSourceDownloadComplete = g_strdup (argv [i]);
}
else if (!g_ascii_strcasecmp (argn [i], "culture")) {
culture = g_strdup (argv[i]);
}
else if (!g_ascii_strcasecmp (argn [i], "uiCulture")) {
uiCulture = g_strdup (argv[i]);
}
else {
//fprintf (stderr, "unhandled attribute %s='%s' in PluginInstance::Initialize\n", argn[i], argv[i]);
}
}
// like 'source' the original location can also be required later (for cross-domain checks after redirections)
source_location_original = GetPageLocation ();
guint32 supportsWindowless = FALSE; // NPBool + padding
int plugin_major, plugin_minor;
int netscape_major, netscape_minor;
bool try_opera_quirks = FALSE;
/* Find the version numbers. */
MOON_NPN_Version(&plugin_major, &plugin_minor,
&netscape_major, &netscape_minor);
//d(printf ("Plugin NPAPI version = %d.%d\n", plugin_major, netscape_minor));
//d(printf ("Browser NPAPI version = %d.%d\n", netscape_major, netscape_minor));
NPError error;
error = MOON_NPN_GetValue (instance, NPNVSupportsXEmbedBool, &xembed_supported);
if (error || !xembed_supported) {
// This should be an error but we'll use it to detect
// that we are running in opera
//return NPERR_INCOMPATIBLE_VERSION_ERROR;
if (!windowless)
d(printf ("*** XEmbed not supported\n"));
try_opera_quirks = true;
}
error = MOON_NPN_GetValue (instance, NPNVSupportsWindowless, &supportsWindowless);
supportsWindowless = (error == NPERR_NO_ERROR) && supportsWindowless;
#ifdef DEBUG
if ((moonlight_flags & RUNTIME_INIT_ALLOW_WINDOWLESS) == 0) {
printf ("plugin wants to be windowless, but we're not going to let it\n");
windowless = false;
}
#endif
if (windowless) {
if (supportsWindowless) {
MOON_NPN_SetValue (instance, NPPVpluginWindowBool, (void *) FALSE);
MOON_NPN_SetValue (instance, NPPVpluginTransparentBool, (void *) TRUE);
d(printf ("windowless mode\n"));
} else {
d(printf ("browser doesn't support windowless mode.\n"));
windowless = false;
}
}
// grovel around in the useragent and try to figure out which
// browser bridge we should use.
const char *useragent = MOON_NPN_UserAgent (instance);
if (strstr (useragent, "Opera"))
is_reentrant_mess = true;
if (strstr (useragent, "Gecko")) {
if (!(moonlight_flags & RUNTIME_INIT_CURL_BRIDGE)) {
// gecko based, let's look for 'rv:1.8' vs 'rv:1.9.2' vs 'rv:1.9'
if (strstr (useragent, "rv:1.8"))
TryLoadBridge ("ff2");
else if (strstr (useragent, "rv:1.9.3"))
/* No bridge needed */ ;
else if (strstr (useragent, "rv:1.9"))
TryLoadBridge ("ff3");
}
}
#if DEBUG
if (!bridge)
printf ("Moonlight: browser bridge not found for your browser (likely not needed). User agent = '%s'\n", useragent);
#endif
if (!CreatePluginDeployment ()) {
g_warning ("Couldn't initialize Mono or create the plugin Deployment");
} else {
deployment->SetUserAgent (useragent);
}
}
typedef BrowserBridge* (*create_bridge_func)();
const char*
get_plugin_dir (void)
{
static char *plugin_dir = NULL;
if (!plugin_dir) {
Dl_info dlinfo;
if (dladdr((void *) &NPN_strdup, &dlinfo) == 0) {
fprintf (stderr, "Unable to find the location of libmoonplugin.so: %s\n", dlerror ());
return NULL;
}
plugin_dir = g_path_get_dirname (dlinfo.dli_fname);
}
return plugin_dir;
}
void
PluginInstance::TryLoadBridge (const char *prefix)
{
char *bridge_name = g_strdup_printf ("libmoonplugin-%sbridge.so", prefix);
char *bridge_path;
bridge_path = g_build_filename (get_plugin_dir (), bridge_name, NULL);
void* bridge_handle = dlopen (bridge_path, RTLD_LAZY);
g_free (bridge_name);
g_free (bridge_path);
if (bridge_handle == NULL) {
g_warning ("failed to load browser bridge: %s", dlerror());
return;
}
create_bridge_func bridge_ctor = (create_bridge_func)dlsym (bridge_handle, "CreateBrowserBridge");
if (bridge_ctor == NULL) {
g_warning ("failed to locate CreateBrowserBridge symbol: %s", dlerror());
return;
}
bridge = bridge_ctor ();
bridge->SetSurface (GetSurface ());
// TODO: Remove this once things settle down around here
printf ("Using the %s bridge\n", prefix);
}
#if PAL_GTK_A11Y
AtkObject*
PluginInstance::GetRootAccessible ()
{
Deployment::SetCurrent (deployment);
return accessibility_bridge->GetRootAccessible ();
}
#endif
AccessibilityBridge*
PluginInstance::GetAccessibilityBridge ()
{
return accessibility_bridge;
}
NPError
PluginInstance::GetValue (NPPVariable variable, void *result)
{
NPError err = NPERR_NO_ERROR;
switch (variable) {
case NPPVpluginNeedsXEmbed:
*((NPBool *)result) = !windowless;
break;
case NPPVpluginScriptableNPObject:
*((NPObject**) result) = GetRootObject ();
break;
#if PAL_GTK_A11Y
case NPPVpluginNativeAccessibleAtkPlugId:
AtkObject *root;
root = GetRootAccessible ();
if (root != NULL)
*((char **)result) = accessibility_bridge->GetPlugId (root);
break;
#endif
default:
err = NPERR_INVALID_PARAM;
break;
}
return err;
}
NPError
PluginInstance::SetValue (NPNVariable variable, void *value)
{
return NPERR_NO_ERROR;
}
NPError
PluginInstance::SetWindow (NPWindow *window)
{
Deployment::SetCurrent (deployment);
if (moon_window) {
// XXX opera Window lifetime hack needs this
this->window = window;
if (!surface)
return NPERR_GENERIC_ERROR;
moon_window->Resize (window->width, window->height);
return NPERR_NO_ERROR;
}
this->window = window;
CreateWindow ();
return NPERR_NO_ERROR;
}
NPObject*
PluginInstance::GetHost()
{
NPObject *object = NULL;
if (NPERR_NO_ERROR != MOON_NPN_GetValue(instance, NPNVPluginElementNPObject, &object)) {
d(printf ("Failed to get plugin host object\n"));
}
return object;
}
#if 0
void
PluginInstance::ReportCache (Surface *surface, long bytes, void *user_data)
{
PluginInstance *plugin = (PluginInstance *) user_data;
char *msg;
if (bytes < 1048576)
msg = g_strdup_printf ("Cache size is ~%d KB", (int) (bytes / 1024));
else
msg = g_strdup_printf ("Cache size is ~%.2f MB", bytes / 1048576.0);
MOON_NPN_Status (plugin->instance, msg);
if (plugin->properties_cache_label)
gtk_label_set_text (GTK_LABEL (plugin->properties_cache_label), msg);
g_free (msg);
}
#endif
static void
register_event (NPP instance, const char *event_name, char *script_name, NPObject *npobj)
{
if (!script_name)
return;
char *retval = NPN_strdup (script_name);
NPVariant npvalue;
STRINGZ_TO_NPVARIANT (retval, npvalue);
NPIdentifier identifier = MOON_NPN_GetStringIdentifier (event_name);
MOON_NPN_SetProperty (instance, npobj, identifier, &npvalue);
MOON_NPN_MemFree (retval);
}
bool
PluginInstance::IsLoaded ()
{
if (!GetSurface () || is_splash)
return false;
return GetSurface()->IsLoaded();
}
void
PluginInstance::CreateWindow ()
{
bool created = false;
bool normal_startup = !is_reentrant_mess;
if (moon_window == NULL) {
if (windowless) {
moon_window = Runtime::GetWindowingSystem ()->CreateWindowless (window->width, window->height, this);
moon_window->SetTransparent (true);
}
else {
moon_window = Runtime::GetWindowingSystem ()->CreateWindow (MoonWindowType_Plugin, window->width, window->height);
}
created = true;
} else {
created = false;
}
surface = new Surface (moon_window);
deployment->SetSurface (surface);
moon_window->SetSurface (surface);
if (bridge)
bridge->SetSurface (surface);
MoonlightScriptControlObject *root = GetRootObject ();
register_event (instance, "onSourceDownloadProgressChanged", onSourceDownloadProgressChanged, root);
register_event (instance, "onSourceDownloadComplete", onSourceDownloadComplete, root);
register_event (instance, "onError", onError, root);
MOON_NPN_ReleaseObject (root);
// register_event (instance, "onResize", onResize, rootx->content);
// NOTE: last testing showed this call causes opera to reenter but moving it is trouble and
// the bug is on opera's side.
if (normal_startup) {
SetPageURL ();
normal_startup = LoadSplash ();
}
surface->GetTimeManager()->SetMaximumRefreshRate (maxFrameRate);
surface->SetEnableFrameRateCounter (enable_framerate_counter);
surface->SetEnableRedrawRegions (enable_redraw_regions);
if (background) {
Color *c = Color::FromStr (background);
if (c == NULL) {
d(printf ("error setting background color\n"));
c = new Color (0x00FFFFFF);
}
surface->SetBackgroundColor (c);
delete c;
}
if (normal_startup && !windowless && !connected_to_container) {
moon_window->ConnectToContainerPlatformWindow (window->window);
connected_to_container = true;
}
}
void
PluginInstance::UpdateSource ()
{
if (source_idle) {
g_source_remove (source_idle);
source_idle = 0;
}
if (deployment != NULL)
deployment->AbortAllHttpRequests ();
if (!source || strlen (source) == 0)
return;
char *pos = strchr (source, '#');
if (pos) {
this->ref ();
source_idle = Runtime::GetWindowingSystem ()->AddIdle (IdleUpdateSourceByReference, this);
// we're changing the page url as well as the xaml
// location, so we need to call SetPageUrl.
// SetPageUrl calls SetSourceLocation on the surface,
// so we don't need to include the call here.
SetPageURL ();
} else {
// we're setting the source location but not changing
// the page location, so we need to call
// SetSourceLocation here.
Uri *request_uri = NULL;
Uri *page_uri;
Uri *source_uri;
char *page_location = GetPageLocation ();
page_uri = Uri::Create (page_location);
source_uri = Uri::Create (source);
if (page_uri != NULL && source_uri != NULL) {
// apparently we only do this with a xap? ugh...
//
if (source_uri->GetPath ()
&& strlen (source_uri->GetPath ()) > 4
&& !strncmp (source_uri->GetPath () + strlen (source_uri->GetPath ()) - 4, ".xap", 4)) {
if (!source_uri->IsAbsolute ()) {
Uri *temp = Uri::Create (page_uri, source_uri);
delete source_uri;
source_uri = temp;
}
surface->SetSourceLocation (source_uri);
request_uri = source_uri;
source_uri = NULL;
}
}
g_free (page_location);
delete page_uri;
delete source_uri;
if (request_uri == NULL)
request_uri = Uri::Create (source);
HttpRequest *request;
request = deployment->CreateHttpRequest (HttpRequest::OptionsNone);
if (request != NULL) {
this->ref ();
request->AddHandler (HttpRequest::ProgressChangedEvent, SourceProgressChangedHandler, this);
request->AddHandler (HttpRequest::StoppedEvent, SourceStoppedHandler, this);
request->Open ("GET", request_uri, NoPolicy);
request->Send ();
}
delete request_uri;
}
}
bool
PluginInstance::IdleUpdateSourceByReference (gpointer data)
{
PluginInstance *instance = (PluginInstance*)data;
char *pos = NULL;
instance->source_idle = 0;
if (instance->source)
pos = strchr (instance->source, '#');
if (pos && strlen (pos+1) > 0)
instance->UpdateSourceByReference (pos+1);
instance->GetSurface ()->EmitSourceDownloadProgressChanged (1.0);
if (instance->progress_changed_token != -1) {
instance->GetSurface ()->RemoveHandler (Surface::SourceDownloadProgressChangedEvent, instance->progress_changed_token);
instance->progress_changed_token = -1;
}
instance->GetSurface ()->EmitSourceDownloadComplete ();
instance->unref ();
return false;
}
void
PluginInstance::UpdateSourceByReference (const char *value)
{
// basically do the equivalent of document.getElementById('@value').textContent
// all using NPAPI.
//
NPVariant _document;
NPVariant _element;
NPVariant _elementName;
NPVariant _textContent;
Deployment::SetCurrent (deployment);
NPIdentifier id_ownerDocument = MOON_NPN_GetStringIdentifier ("ownerDocument");
NPIdentifier id_getElementById = MOON_NPN_GetStringIdentifier ("getElementById");
NPIdentifier id_textContent = MOON_NPN_GetStringIdentifier ("textContent");
NPObject *host = GetHost();
if (!host) {
// printf ("no host\n");
return;
}
// get host.ownerDocument
bool nperr;
if (!(nperr = MOON_NPN_GetProperty (instance, host, id_ownerDocument, &_document))
|| !NPVARIANT_IS_OBJECT (_document)) {
// printf ("no document (type == %d, nperr = %d)\n", _document.type, nperr);
return;
}
// _element = document.getElementById ('@value')
string_to_npvariant (value, &_elementName);
if (!(nperr = MOON_NPN_Invoke (instance, NPVARIANT_TO_OBJECT (_document), id_getElementById,
&_elementName, 1, &_element))
|| !NPVARIANT_IS_OBJECT (_element)) {
// printf ("no valid element named #%s (type = %d, nperr = %d)\n", value, _element.type, nperr);
MOON_NPN_ReleaseVariantValue (&_document);
}
// _textContent = _element.textContent
if (!(nperr = MOON_NPN_GetProperty (instance, NPVARIANT_TO_OBJECT (_element), id_textContent, &_textContent))
|| !NPVARIANT_IS_STRING (_textContent)) {
// printf ("no text content for element named #%s (type = %d, nperr = %d)\n", value, _textContent.type, nperr);
MOON_NPN_ReleaseVariantValue (&_document);
MOON_NPN_ReleaseVariantValue (&_element);
return;
}
char *xaml = g_strndup ((char *) NPVARIANT_TO_STRING (_textContent).utf8characters, NPVARIANT_TO_STRING (_textContent).utf8length);
// printf ("yay, xaml = %s\n", xaml);
if (xaml_loader)
delete xaml_loader;
xaml_loader = PluginXamlLoader::FromStr (NULL/*FIXME*/, xaml, this, surface);
LoadXAML ();
g_free (xaml);