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
|
/* IcedTeaScriptablePluginObject.cc
Copyright (C) 2009, 2010 Red Hat
This file is part of IcedTea.
IcedTea is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
IcedTea 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 IcedTea; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
#include <typeinfo>
#include "IcedTeaScriptablePluginObject.h"
IcedTeaScriptablePluginObject::IcedTeaScriptablePluginObject(NPP instance)
{
this->instance = instance;
IcedTeaPluginUtilities::storeInstanceID(this, instance);
}
void
IcedTeaScriptablePluginObject::deAllocate(NPObject *npobj)
{
printf ("** Unimplemented: IcedTeaScriptablePluginObject::deAllocate %p\n", npobj);
}
void
IcedTeaScriptablePluginObject::invalidate(NPObject *npobj)
{
printf ("** Unimplemented: IcedTeaScriptablePluginObject::invalidate %p\n", npobj);
}
bool
IcedTeaScriptablePluginObject::hasMethod(NPObject *npobj, NPIdentifier name)
{
printf ("** Unimplemented: IcedTeaScriptablePluginObject::hasMethod %p\n", npobj);
return false;
}
bool
IcedTeaScriptablePluginObject::invoke(NPObject *npobj, NPIdentifier name, const NPVariant *args,
uint32_t argCount,NPVariant *result)
{
printf ("** Unimplemented: IcedTeaScriptablePluginObject::invoke %p\n", npobj);
return false;
}
bool
IcedTeaScriptablePluginObject::invokeDefault(NPObject *npobj, const NPVariant *args,
uint32_t argCount, NPVariant *result)
{
printf ("** Unimplemented: IcedTeaScriptablePluginObject::invokeDefault %p\n", npobj);
return false;
}
bool
IcedTeaScriptablePluginObject::hasProperty(NPObject *npobj, NPIdentifier name)
{
printf ("** Unimplemented: IcedTeaScriptablePluginObject::hasProperty %p\n", npobj);
return false;
}
bool
IcedTeaScriptablePluginObject::getProperty(NPObject *npobj, NPIdentifier name, NPVariant *result)
{
// Package request?
if (!strcmp(browser_functions.utf8fromidentifier(name), "java"))
{
//NPObject* obj = IcedTeaScriptablePluginObject::get_scriptable_java_package_object(getInstanceFromMemberPtr(npobj), name);
//OBJECT_TO_NPVARIANT(obj, *result);
//printf ("Filling variant %p with object %p\n", result);
}
return false;
}
bool
IcedTeaScriptablePluginObject::setProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value)
{
printf ("** Unimplemented: IcedTeaScriptablePluginObject::setProperty %p\n", npobj);
return false;
}
bool
IcedTeaScriptablePluginObject::removeProperty(NPObject *npobj, NPIdentifier name)
{
printf ("** Unimplemented: IcedTeaScriptablePluginObject::removeProperty %p\n", npobj);
return false;
}
bool
IcedTeaScriptablePluginObject::enumerate(NPObject *npobj, NPIdentifier **value, uint32_t *count)
{
printf ("** Unimplemented: IcedTeaScriptablePluginObject::enumerate %p\n", npobj);
return false;
}
bool
IcedTeaScriptablePluginObject::construct(NPObject *npobj, const NPVariant *args, uint32_t argCount,
NPVariant *result)
{
printf ("** Unimplemented: IcedTeaScriptablePluginObject::construct %p\n", npobj);
return false;
}
NPObject*
allocate_scriptable_jp_object(NPP npp, NPClass *aClass)
{
PLUGIN_DEBUG("Allocating new scriptable Java Package object\n");
return new IcedTeaScriptableJavaPackageObject(npp);
}
NPObject*
IcedTeaScriptablePluginObject::get_scriptable_java_package_object(NPP instance, const NPUTF8* name)
{
NPObject* scriptable_object;
NPClass* np_class = new NPClass();
np_class->structVersion = NP_CLASS_STRUCT_VERSION;
np_class->allocate = allocate_scriptable_jp_object;
np_class->deallocate = IcedTeaScriptableJavaPackageObject::deAllocate;
np_class->invalidate = IcedTeaScriptableJavaPackageObject::invalidate;
np_class->hasMethod = IcedTeaScriptableJavaPackageObject::hasMethod;
np_class->invoke = IcedTeaScriptableJavaPackageObject::invoke;
np_class->invokeDefault = IcedTeaScriptableJavaPackageObject::invokeDefault;
np_class->hasProperty = IcedTeaScriptableJavaPackageObject::hasProperty;
np_class->getProperty = IcedTeaScriptableJavaPackageObject::getProperty;
np_class->setProperty = IcedTeaScriptableJavaPackageObject::setProperty;
np_class->removeProperty = IcedTeaScriptableJavaPackageObject::removeProperty;
np_class->enumerate = IcedTeaScriptableJavaPackageObject::enumerate;
np_class->construct = IcedTeaScriptableJavaPackageObject::construct;
scriptable_object = browser_functions.createobject(instance, np_class);
PLUGIN_DEBUG("Returning new scriptable package class: %p from instance %p with name %s\n", scriptable_object, instance, name);
((IcedTeaScriptableJavaPackageObject*) scriptable_object)->setPackageName(name);
IcedTeaPluginUtilities::storeInstanceID(scriptable_object, instance);
return scriptable_object;
}
IcedTeaScriptableJavaPackageObject::IcedTeaScriptableJavaPackageObject(NPP instance)
{
PLUGIN_DEBUG("Constructing new scriptable java package object\n");
this->instance = instance;
this->package_name = new std::string();
}
IcedTeaScriptableJavaPackageObject::~IcedTeaScriptableJavaPackageObject()
{
delete this->package_name;
}
void
IcedTeaScriptableJavaPackageObject::setPackageName(const NPUTF8* name)
{
this->package_name->append(name);
}
std::string
IcedTeaScriptableJavaPackageObject::getPackageName()
{
return this->package_name->c_str();
}
void
IcedTeaScriptableJavaPackageObject::deAllocate(NPObject *npobj)
{
delete (IcedTeaScriptableJavaPackageObject*)npobj;
}
void
IcedTeaScriptableJavaPackageObject::invalidate(NPObject *npobj)
{
// nothing to do for these
}
bool
IcedTeaScriptableJavaPackageObject::hasMethod(NPObject *npobj, NPIdentifier name)
{
// Silly caller. Methods are for objects!
return false;
}
bool
IcedTeaScriptableJavaPackageObject::invoke(NPObject *npobj, NPIdentifier name, const NPVariant *args,
uint32_t argCount,NPVariant *result)
{
printf ("** Unimplemented: IcedTeaScriptableJavaPackageObject::invoke %p\n", npobj);
return false;
}
bool
IcedTeaScriptableJavaPackageObject::invokeDefault(NPObject *npobj, const NPVariant *args,
uint32_t argCount, NPVariant *result)
{
printf ("** Unimplemented: IcedTeaScriptableJavaPackageObject::invokeDefault %p\n", npobj);
return false;
}
bool
IcedTeaScriptableJavaPackageObject::hasProperty(NPObject *npobj, NPIdentifier name)
{
PLUGIN_DEBUG("IcedTeaScriptableJavaPackageObject::hasProperty %s\n", browser_functions.utf8fromidentifier(name));
bool hasProperty = false;
JavaResultData* java_result;
JavaRequestProcessor* java_request = new JavaRequestProcessor();
NPP instance = IcedTeaPluginUtilities::getInstanceFromMemberPtr(npobj);
int plugin_instance_id = get_id_from_instance(instance);
PLUGIN_DEBUG("Object package name: \"%s\"\n", ((IcedTeaScriptableJavaPackageObject*) npobj)->getPackageName().c_str());
// "^java" is always a package
if (((IcedTeaScriptableJavaPackageObject*) npobj)->getPackageName().length() == 0 &&
( !strcmp(browser_functions.utf8fromidentifier(name), "java") ||
!strcmp(browser_functions.utf8fromidentifier(name), "javax")))
{
return true;
}
std::string property_name = ((IcedTeaScriptableJavaPackageObject*) npobj)->getPackageName();
if (property_name.length() > 0)
property_name += ".";
property_name += browser_functions.utf8fromidentifier(name);
PLUGIN_DEBUG("Looking for name \"%s\"\n", property_name.c_str());
java_result = java_request->hasPackage(plugin_instance_id, property_name);
if (!java_result->error_occurred && java_result->return_identifier != 0) hasProperty = true;
// No such package. Do we have a class with that name?
if (!hasProperty)
{
java_result = java_request->findClass(plugin_instance_id, property_name);
}
if (java_result->return_identifier != 0) hasProperty = true;
delete java_request;
return hasProperty;
}
bool
IcedTeaScriptableJavaPackageObject::getProperty(NPObject *npobj, NPIdentifier name, NPVariant *result)
{
PLUGIN_DEBUG("IcedTeaScriptableJavaPackageObject::getProperty %s\n", browser_functions.utf8fromidentifier(name));
if (!browser_functions.utf8fromidentifier(name))
return false;
bool isPropertyClass = false;
JavaResultData* java_result;
JavaRequestProcessor java_request = JavaRequestProcessor();
NPP instance = IcedTeaPluginUtilities::getInstanceFromMemberPtr(npobj);
int plugin_instance_id = get_id_from_instance(instance);
std::string property_name = ((IcedTeaScriptableJavaPackageObject*) npobj)->getPackageName();
if (property_name.length() > 0)
property_name += ".";
property_name += browser_functions.utf8fromidentifier(name);
java_result = java_request.findClass(plugin_instance_id, property_name);
isPropertyClass = (java_result->return_identifier == 0);
//NPIdentifier property = browser_functions.getstringidentifier(property_name.c_str());
NPObject* obj;
if (isPropertyClass)
{
PLUGIN_DEBUG("Returning package object\n");
obj = IcedTeaScriptablePluginObject::get_scriptable_java_package_object(
IcedTeaPluginUtilities::getInstanceFromMemberPtr(npobj),
property_name.c_str());
}
else
{
PLUGIN_DEBUG("Returning Java object\n");
obj = IcedTeaScriptableJavaPackageObject::get_scriptable_java_object(
IcedTeaPluginUtilities::getInstanceFromMemberPtr(npobj),
*(java_result->return_string), "0", false);
}
OBJECT_TO_NPVARIANT(obj, *result);
return true;
}
bool
IcedTeaScriptableJavaPackageObject::setProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value)
{
// Can't be going around setting properties on namespaces.. that's madness!
return false;
}
bool
IcedTeaScriptableJavaPackageObject::removeProperty(NPObject *npobj, NPIdentifier name)
{
printf ("** Unimplemented: IcedTeaScriptableJavaPackageObject::removeProperty %p\n", npobj);
return false;
}
bool
IcedTeaScriptableJavaPackageObject::enumerate(NPObject *npobj, NPIdentifier **value, uint32_t *count)
{
printf ("** Unimplemented: IcedTeaScriptableJavaPackageObject::enumerate %p\n", npobj);
return false;
}
bool
IcedTeaScriptableJavaPackageObject::construct(NPObject *npobj, const NPVariant *args, uint32_t argCount,
NPVariant *result)
{
printf ("** Unimplemented: IcedTeaScriptableJavaPackageObject::construct %p\n", npobj);
return false;
}
NPObject*
allocate_scriptable_java_object(NPP npp, NPClass *aClass)
{
PLUGIN_DEBUG("Allocating new scriptable Java object\n");
return new IcedTeaScriptableJavaObject(npp);
}
NPObject*
IcedTeaScriptableJavaPackageObject::get_scriptable_java_object(NPP instance,
std::string class_id,
std::string instance_id,
bool isArray)
{
NPObject* scriptable_object;
std::string obj_key = std::string();
obj_key += class_id;
obj_key += ":";
obj_key += instance_id;
PLUGIN_DEBUG("get_scriptable_java_object searching for %s...\n", obj_key.c_str());
scriptable_object = IcedTeaPluginUtilities::getNPObjectFromJavaKey(obj_key);
if (scriptable_object != NULL)
{
PLUGIN_DEBUG("Returning existing object %p\n", scriptable_object);
browser_functions.retainobject(scriptable_object);
return scriptable_object;
}
NPClass* np_class = new NPClass();
np_class->structVersion = NP_CLASS_STRUCT_VERSION;
np_class->allocate = allocate_scriptable_java_object;
np_class->deallocate = IcedTeaScriptableJavaObject::deAllocate;
np_class->invalidate = IcedTeaScriptableJavaObject::invalidate;
np_class->hasMethod = IcedTeaScriptableJavaObject::hasMethod;
np_class->invoke = IcedTeaScriptableJavaObject::invoke;
np_class->invokeDefault = IcedTeaScriptableJavaObject::invokeDefault;
np_class->hasProperty = IcedTeaScriptableJavaObject::hasProperty;
np_class->getProperty = IcedTeaScriptableJavaObject::getProperty;
np_class->setProperty = IcedTeaScriptableJavaObject::setProperty;
np_class->removeProperty = IcedTeaScriptableJavaObject::removeProperty;
np_class->enumerate = IcedTeaScriptableJavaObject::enumerate;
np_class->construct = IcedTeaScriptableJavaObject::construct;
// try to create normally
scriptable_object = browser_functions.createobject(instance, np_class);
// didn't work? try creating asynch
if (!scriptable_object)
{
AsyncCallThreadData thread_data = AsyncCallThreadData();
thread_data.result_ready = false;
thread_data.parameters = std::vector<void*>();
thread_data.result = std::string();
thread_data.parameters.push_back(instance);
thread_data.parameters.push_back(np_class);
thread_data.parameters.push_back(&scriptable_object);
IcedTeaPluginUtilities::callAndWaitForResult(instance, &_createAndRetainJavaObject, &thread_data);
} else
{
// Else retain object and continue
browser_functions.retainobject(scriptable_object);
}
PLUGIN_DEBUG("Constructed new Java Object with classid=%s, instanceid=%s, isArray=%d and scriptable_object=%p\n", class_id.c_str(), instance_id.c_str(), isArray, scriptable_object);
((IcedTeaScriptableJavaObject*) scriptable_object)->setClassIdentifier(class_id);
((IcedTeaScriptableJavaObject*) scriptable_object)->setIsArray(isArray);
if (instance_id != "0")
((IcedTeaScriptableJavaObject*) scriptable_object)->setInstanceIdentifier(instance_id);
IcedTeaPluginUtilities::storeInstanceID(scriptable_object, instance);
IcedTeaPluginUtilities::storeObjectMapping(obj_key, scriptable_object);
PLUGIN_DEBUG("Inserting into object_map key %s->%p\n", obj_key.c_str(), scriptable_object);
return scriptable_object;
}
/* Creates and retains a scriptable java object (intended to be called asynch.) */
void
_createAndRetainJavaObject(void* data)
{
PLUGIN_DEBUG("Asynchronously creating/retaining object ...\n");
std::vector<void*> parameters = ((AsyncCallThreadData*) data)->parameters;
NPP instance = (NPP) parameters.at(0);
NPClass* np_class = (NPClass*) parameters.at(1);
NPObject** scriptable_object = (NPObject**) parameters.at(2);
*scriptable_object = browser_functions.createobject(instance, np_class);
browser_functions.retainobject(*scriptable_object);
((AsyncCallThreadData*) data)->result_ready = true;
}
bool
IcedTeaScriptableJavaPackageObject::is_valid_java_object(NPObject* object_ptr) {
return IcedTeaPluginUtilities::getInstanceFromMemberPtr(object_ptr) != NULL;
}
IcedTeaScriptableJavaObject::IcedTeaScriptableJavaObject(NPP instance)
{
this->instance = instance;
this->class_id = new std::string();
this->instance_id = new std::string();
}
IcedTeaScriptableJavaObject::~IcedTeaScriptableJavaObject()
{
delete this->class_id;
delete this->instance_id;
}
void
IcedTeaScriptableJavaObject::setClassIdentifier(std::string class_id)
{
this->class_id->append(class_id);
}
void
IcedTeaScriptableJavaObject::setInstanceIdentifier(std::string instance_id)
{
this->instance_id->append(instance_id);
}
void
IcedTeaScriptableJavaObject::setIsArray(bool isArray)
{
this->isObjectArray = isArray;
}
void
IcedTeaScriptableJavaObject::deAllocate(NPObject *npobj)
{
delete (IcedTeaScriptableJavaObject*)npobj;
}
void
IcedTeaScriptableJavaObject::invalidate(NPObject *npobj)
{
IcedTeaPluginUtilities::removeInstanceID(npobj);
std::string obj_key = std::string();
obj_key += ((IcedTeaScriptableJavaObject*) npobj)->getClassID();
obj_key += ":";
obj_key += ((IcedTeaScriptableJavaObject*) npobj)->getInstanceID();
IcedTeaPluginUtilities::removeObjectMapping(obj_key);
}
bool
IcedTeaScriptableJavaObject::hasMethod(NPObject *npobj, NPIdentifier name)
{
PLUGIN_DEBUG("IcedTeaScriptableJavaObject::hasMethod %s (ival=%d)\n", browser_functions.utf8fromidentifier(name), browser_functions.intfromidentifier(name));
bool hasMethod = false;
// If object is an array and requested "method" may be a number, check for it first
if ( !((IcedTeaScriptableJavaObject*) npobj)->isArray() ||
(browser_functions.intfromidentifier(name) < 0))
{
if (!browser_functions.utf8fromidentifier(name))
return false;
JavaResultData* java_result;
JavaRequestProcessor java_request = JavaRequestProcessor();
std::string classId = std::string(((IcedTeaScriptableJavaObject*) npobj)->getClassID());
std::string methodName = browser_functions.utf8fromidentifier(name);
java_result = java_request.hasMethod(classId, methodName);
hasMethod = java_result->return_identifier != 0;
}
PLUGIN_DEBUG("IcedTeaScriptableJavaObject::hasMethod returning %d\n", hasMethod);
return hasMethod;
}
bool
IcedTeaScriptableJavaObject::invoke(NPObject *npobj, NPIdentifier name, const NPVariant *args,
uint32_t argCount, NPVariant *result)
{
NPUTF8* method_name = browser_functions.utf8fromidentifier(name);
// Extract arg type array
PLUGIN_DEBUG("IcedTeaScriptableJavaObject::invoke %s. Args follow.\n", method_name);
for (int i=0; i < argCount; i++)
{
IcedTeaPluginUtilities::printNPVariant(args[i]);
}
JavaResultData* java_result;
JavaRequestProcessor java_request = JavaRequestProcessor();
NPObject* obj;
std::string instance_id = ((IcedTeaScriptableJavaObject*) npobj)->getInstanceID();
std::string class_id = ((IcedTeaScriptableJavaObject*) npobj)->getClassID();
std::string callee;
std::string source;
NPP instance = IcedTeaPluginUtilities::getInstanceFromMemberPtr(npobj);
// First, load the arguments into the java-side table
std::string id = std::string();
std::vector<std::string> arg_ids = std::vector<std::string>();
for (int i=0; i < argCount; i++) {
id.clear();
createJavaObjectFromVariant(instance, args[i], &id);
if (id == "-1")
{
printf("Unable to create arguments on Java side\n");
return false;
}
arg_ids.push_back(id);
}
if (instance_id.length() == 0) // Static
{
PLUGIN_DEBUG("Calling static method\n");
callee = ((IcedTeaScriptableJavaObject*) npobj)->getClassID();
java_result = java_request.callStaticMethod(
IcedTeaPluginUtilities::getSourceFromInstance(instance),
callee, browser_functions.utf8fromidentifier(name), arg_ids);
} else
{
PLUGIN_DEBUG("Calling method normally\n");
callee = ((IcedTeaScriptableJavaObject*) npobj)->getInstanceID();
java_result = java_request.callMethod(
IcedTeaPluginUtilities::getSourceFromInstance(instance),
callee, browser_functions.utf8fromidentifier(name), arg_ids);
}
if (java_result->error_occurred)
{
// error message must be allocated on heap
char* error_msg = (char*) malloc(java_result->error_msg->length()*sizeof(char));
strcpy(error_msg, java_result->error_msg->c_str());
browser_functions.setexception(npobj, error_msg);
return false;
}
PLUGIN_DEBUG("IcedTeaScriptableJavaObject::invoke converting and returning.\n");
return IcedTeaPluginUtilities::javaResultToNPVariant(instance, java_result->return_string, result);
}
bool
IcedTeaScriptableJavaObject::invokeDefault(NPObject *npobj, const NPVariant *args,
uint32_t argCount, NPVariant *result)
{
printf ("** Unimplemented: IcedTeaScriptableJavaObject::invokeDefault %p\n", npobj);
return false;
}
bool
IcedTeaScriptableJavaObject::hasProperty(NPObject *npobj, NPIdentifier name)
{
PLUGIN_DEBUG("IcedTeaScriptableJavaObject::hasProperty %s (ival=%d)\n", browser_functions.utf8fromidentifier(name), browser_functions.intfromidentifier(name));
bool hasProperty = false;
// If it is an array, only length and indexes are valid
if (((IcedTeaScriptableJavaObject*) npobj)->isArray())
{
if (browser_functions.intfromidentifier(name) >= 0 ||
!strcmp(browser_functions.utf8fromidentifier(name), "length"))
hasProperty = true;
} else
{
if (!browser_functions.utf8fromidentifier(name))
return false;
if (!strcmp(browser_functions.utf8fromidentifier(name), "Packages"))
{
hasProperty = true;
} else {
JavaResultData* java_result;
JavaRequestProcessor java_request = JavaRequestProcessor();
std::string class_id = std::string(((IcedTeaScriptableJavaObject*) npobj)->getClassID());
std::string fieldName = browser_functions.utf8fromidentifier(name);
java_result = java_request.hasField(class_id, fieldName);
hasProperty = java_result->return_identifier != 0;
}
}
PLUGIN_DEBUG("IcedTeaScriptableJavaObject::hasProperty returning %d\n", hasProperty);
return hasProperty;
}
bool
IcedTeaScriptableJavaObject::getProperty(NPObject *npobj, NPIdentifier name, NPVariant *result)
{
PLUGIN_DEBUG("IcedTeaScriptableJavaObject::getProperty %s (ival=%d)\n", browser_functions.utf8fromidentifier(name), browser_functions.intfromidentifier(name));
bool isPropertyClass = false;
JavaResultData* java_result;
JavaRequestProcessor java_request = JavaRequestProcessor();
NPObject* obj;
std::string instance_id = ((IcedTeaScriptableJavaObject*) npobj)->getInstanceID();
std::string class_id = ((IcedTeaScriptableJavaObject*) npobj)->getClassID();
NPP instance = ((IcedTeaScriptableJavaObject*) npobj)->getInstance();
if (instance_id.length() > 0) // Could be an array or a simple object
{
// If array and requesting length
if ( ((IcedTeaScriptableJavaObject*) npobj)->isArray() &&
browser_functions.utf8fromidentifier(name) &&
!strcmp(browser_functions.utf8fromidentifier(name), "length"))
{
java_result = java_request.getArrayLength(instance_id);
} else if ( ((IcedTeaScriptableJavaObject*) npobj)->isArray() &&
browser_functions.intfromidentifier(name) >= 0) // else if array and requesting index
{
java_result = java_request.getArrayLength(instance_id);
if (java_result->error_occurred)
{
printf("ERROR: Couldn't fetch array length\n");
return false;
}
int length = atoi(java_result->return_string->c_str());
// Access beyond size?
if (browser_functions.intfromidentifier(name) >= length)
{
VOID_TO_NPVARIANT(*result);
return true;
}
std::string index = std::string();
IcedTeaPluginUtilities::itoa(browser_functions.intfromidentifier(name), &index);
java_result = java_request.getSlot(instance_id, index);
} else // Everything else
{
if (!browser_functions.utf8fromidentifier(name))
return false;
if (!strcmp(browser_functions.utf8fromidentifier(name), "Packages"))
{
NPObject* pkgObject = IcedTeaScriptablePluginObject::get_scriptable_java_package_object(instance, "");
OBJECT_TO_NPVARIANT(pkgObject, *result);
return true;
}
java_result = java_request.getField(
IcedTeaPluginUtilities::getSourceFromInstance(instance),
class_id, instance_id, browser_functions.utf8fromidentifier(name));
}
}
else
{
if (!browser_functions.utf8fromidentifier(name))
return true;
java_result = java_request.getStaticField(
IcedTeaPluginUtilities::getSourceFromInstance(instance),
class_id, browser_functions.utf8fromidentifier(name));
}
if (java_result->error_occurred)
{
return false;
}
PLUGIN_DEBUG("IcedTeaScriptableJavaObject::getProperty converting and returning.\n");
return IcedTeaPluginUtilities::javaResultToNPVariant(instance, java_result->return_string, result);
}
bool
IcedTeaScriptableJavaObject::setProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value)
{
PLUGIN_DEBUG("IcedTeaScriptableJavaObject::setProperty %s (ival=%d) to:\n", browser_functions.utf8fromidentifier(name), browser_functions.intfromidentifier(name));
IcedTeaPluginUtilities::printNPVariant(*value);
bool isPropertyClass = false;
JavaResultData* java_result;
JavaRequestProcessor java_request = JavaRequestProcessor();
NPObject* obj;
std::string instance_id = ((IcedTeaScriptableJavaObject*) npobj)->getInstanceID();
std::string class_id = ((IcedTeaScriptableJavaObject*) npobj)->getClassID();
NPP instance = IcedTeaPluginUtilities::getInstanceFromMemberPtr(npobj);
if (instance_id.length() > 0) // Could be an array or a simple object
{
// If array
if ( ((IcedTeaScriptableJavaObject*) npobj)->isArray() &&
browser_functions.utf8fromidentifier(name) &&
!strcmp(browser_functions.utf8fromidentifier(name), "length"))
{
printf("ERROR: Array length is not a modifiable property\n");
return false;
} else if ( ((IcedTeaScriptableJavaObject*) npobj)->isArray() &&
browser_functions.intfromidentifier(name) >= 0) // else if array and requesting index
{
java_result = java_request.getArrayLength(instance_id);
if (java_result->error_occurred)
{
printf("ERROR: Couldn't fetch array length\n");
return false;
}
int length = atoi(java_result->return_string->c_str());
// Access beyond size?
if (browser_functions.intfromidentifier(name) >= length)
{
return true;
}
std::string index = std::string();
IcedTeaPluginUtilities::itoa(browser_functions.intfromidentifier(name), &index);
std::string value_id = std::string();
createJavaObjectFromVariant(instance, *value, &value_id);
java_result = java_request.setSlot(instance_id, index, value_id);
} else // Everything else
{
std::string value_id = std::string();
createJavaObjectFromVariant(instance, *value, &value_id);
java_result = java_request.setField(
IcedTeaPluginUtilities::getSourceFromInstance(instance),
class_id, instance_id, browser_functions.utf8fromidentifier(name), value_id);
}
}
else
{
std::string value_id = std::string();
createJavaObjectFromVariant(instance, *value, &value_id);
java_result = java_request.setStaticField(
IcedTeaPluginUtilities::getSourceFromInstance(instance),
class_id, browser_functions.utf8fromidentifier(name), value_id);
}
if (java_result->error_occurred)
{
return false;
}
PLUGIN_DEBUG("IcedTeaScriptableJavaObject::setProperty returning.\n");
return true;
}
bool
IcedTeaScriptableJavaObject::removeProperty(NPObject *npobj, NPIdentifier name)
{
printf ("** Unimplemented: IcedTeaScriptableJavaObject::removeProperty %p\n", npobj);
return false;
}
bool
IcedTeaScriptableJavaObject::enumerate(NPObject *npobj, NPIdentifier **value, uint32_t *count)
{
printf ("** Unimplemented: IcedTeaScriptableJavaObject::enumerate %p\n", npobj);
return false;
}
bool
IcedTeaScriptableJavaObject::construct(NPObject *npobj, const NPVariant *args, uint32_t argCount,
NPVariant *result)
{
// Extract arg type array
PLUGIN_DEBUG("IcedTeaScriptableJavaObject::construct %s. Args follow.\n", ((IcedTeaScriptableJavaObject*) npobj)->getClassID().c_str());
for (int i=0; i < argCount; i++)
{
IcedTeaPluginUtilities::printNPVariant(args[i]);
}
JavaResultData* java_result;
JavaRequestProcessor java_request = JavaRequestProcessor();
NPObject* obj;
std::string class_id = ((IcedTeaScriptableJavaObject*) npobj)->getClassID();
NPP instance = IcedTeaPluginUtilities::getInstanceFromMemberPtr(npobj);
// First, load the arguments into the java-side table
std::string id = std::string();
std::vector<std::string> arg_ids = std::vector<std::string>();
for (int i=0; i < argCount; i++) {
id.clear();
createJavaObjectFromVariant(instance, args[i], &id);
if (id == "0")
{
// error message must be allocated on heap
char* error_msg = (char*) malloc(1024*sizeof(char));
strcpy(error_msg, "Unable to create argument on Java side");
browser_functions.setexception(npobj, error_msg);
return false;
}
arg_ids.push_back(id);
}
java_result = java_request.newObject(
IcedTeaPluginUtilities::getSourceFromInstance(instance),
class_id,
arg_ids);
if (java_result->error_occurred)
{
// error message must be allocated on heap
int length = java_result->error_msg->length();
char* error_msg = (char*) malloc((length+1)*sizeof(char));
strcpy(error_msg, java_result->error_msg->c_str());
browser_functions.setexception(npobj, error_msg);
return false;
}
std::string return_obj_instance_id = std::string();
std::string return_obj_class_id = class_id;
return_obj_instance_id.append(*(java_result->return_string));
obj = IcedTeaScriptableJavaPackageObject::get_scriptable_java_object(
IcedTeaPluginUtilities::getInstanceFromMemberPtr(npobj),
return_obj_class_id, return_obj_instance_id, false);
OBJECT_TO_NPVARIANT(obj, *result);
PLUGIN_DEBUG("IcedTeaScriptableJavaObject::construct returning.\n");
return true;
}
|