-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy path0001-Add-LunaServiceBridge-interface.patch
More file actions
1016 lines (1007 loc) · 29.3 KB
/
0001-Add-LunaServiceBridge-interface.patch
File metadata and controls
1016 lines (1007 loc) · 29.3 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
From 2d73c8bcae3ea988e2555a22a82bc4b549ad5595 Mon Sep 17 00:00:00 2001
From: Florian Hanel <florian@L8440CND1123VNS.(none)>
Date: Wed, 19 Dec 2012 13:27:45 +0100
Subject: [PATCH 1/2] Add LunaServiceBridge interface
LunaServiceBridge allows WebOS Apps to talk to LunaServiceBus
It is required for many enyo apps. LunaServiceBridge is a Javascript
Object that they use to communicate using liblunaservice
---
Source/WebCore/DerivedSources.pri | 6 +
Source/WebCore/Target.pri | 11 +
Source/WebCore/WebCore.pri | 3 +
Source/WebCore/page/DOMWindow.idl | 2 +
Source/WebCore/platform/webos/LunaServiceMgr.cpp | 290 ++++++++++++++
Source/WebCore/platform/webos/LunaServiceMgr.h | 56 +++
.../WebCore/platform/webos/PalmServiceBridge.cpp | 414 ++++++++++++++++++++
Source/WebCore/platform/webos/PalmServiceBridge.h | 89 +++++
.../WebCore/platform/webos/PalmServiceBridge.idl | 19 +
Tools/qmake/mkspecs/features/features.prf | 4 +
Tools/qmake/mkspecs/features/features.pri | 1 +
11 files changed, 895 insertions(+), 0 deletions(-)
create mode 100644 Source/WebCore/platform/webos/LunaServiceMgr.cpp
create mode 100644 Source/WebCore/platform/webos/LunaServiceMgr.h
create mode 100644 Source/WebCore/platform/webos/PalmServiceBridge.cpp
create mode 100644 Source/WebCore/platform/webos/PalmServiceBridge.h
create mode 100644 Source/WebCore/platform/webos/PalmServiceBridge.idl
diff --git a/Source/WebCore/DerivedSources.pri b/Source/WebCore/DerivedSources.pri
index 7e2245f..6763762 100644
--- a/Source/WebCore/DerivedSources.pri
+++ b/Source/WebCore/DerivedSources.pri
@@ -481,6 +481,12 @@ IDL_BINDINGS += \
$$PWD/xml/XPathEvaluator.idl \
$$PWD/xml/XSLTProcessor.idl
+
+enable?(PALM_SERVICE_BRIDGE) {
+ IDL_BINDINGS += platform/webos/PalmServiceBridge.idl
+}
+
+
enable?(SVG) {
IDL_BINDINGS += \
$$PWD/svg/SVGAElement.idl \
diff --git a/Source/WebCore/Target.pri b/Source/WebCore/Target.pri
index 4535a46..f1b0b7f 100644
--- a/Source/WebCore/Target.pri
+++ b/Source/WebCore/Target.pri
@@ -3475,6 +3475,17 @@ enable?(WEB_AUDIO) {
}
}
+enable?(PALM_SERVICE_BRIDGE) {
+ INCLUDEPATH += $$PWD/platform/webos
+ INCLUDEPATH += /home/florian/src-ics/root/include/luna-service2
+ SOURCES += \
+ platform/webos/PalmServiceBridge.cpp \
+ platform/webos/LunaServiceMgr.cpp
+ HEADERS += \
+ platform/webos/PalmServiceBridge.h \
+ platform/webos/LunaServiceMgr.h
+}
+
enable?(FULLSCREEN_API) {
SOURCES += \
rendering/RenderFullScreen.cpp
diff --git a/Source/WebCore/WebCore.pri b/Source/WebCore/WebCore.pri
index 67e12c0..e7fd32e 100644
--- a/Source/WebCore/WebCore.pri
+++ b/Source/WebCore/WebCore.pri
@@ -150,6 +150,9 @@ enable?(NETSCAPE_PLUGIN_API) {
-lversion
}
}
+enable?(PALM_SERVICE_BRIDGE) {
+ LIBS += -L /home/florian/src-ics/root/lib -llunaservice
+}
enable?(ORIENTATION_EVENTS)|enable?(DEVICE_ORIENTATION) {
QT += sensors
diff --git a/Source/WebCore/page/DOMWindow.idl b/Source/WebCore/page/DOMWindow.idl
index 4a3a5f4..87c9bf8 100644
--- a/Source/WebCore/page/DOMWindow.idl
+++ b/Source/WebCore/page/DOMWindow.idl
@@ -809,5 +809,7 @@
// window.toString() requires special handling in V8
[V8DoNotCheckSignature, DoNotCheckSecurity, Custom, NotEnumerable] DOMString toString();
#endif // defined(V8_BINDING)
+
+ attribute [Conditional=PALM_SERVICE_BRIDGE] PalmServiceBridgeConstructor PalmServiceBridge;
};
diff --git a/Source/WebCore/platform/webos/LunaServiceMgr.cpp b/Source/WebCore/platform/webos/LunaServiceMgr.cpp
new file mode 100644
index 0000000..e763655
--- /dev/null
+++ b/Source/WebCore/platform/webos/LunaServiceMgr.cpp
@@ -0,0 +1,290 @@
+
+#include "config.h"
+#if ENABLE(PALM_SERVICE_BRIDGE)
+#include "LunaServiceMgr.h"
+
+#include <wtf/text/WTFString.h>
+
+#include <glib.h>
+#include <lunaservice.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <wtf/text/CString.h>
+
+namespace WebCore {
+
+/**
+* @brief Internal callback for service responses.
+*
+* @param sh
+* @param reply
+* @param ctx
+*
+* @retval
+*/
+static bool
+message_filter(LSHandle *sh, LSMessage* reply, void* ctx)
+{
+ const char* payload = LSMessageGetPayload(reply);
+
+ LunaServiceManagerListener* listener = (LunaServiceManagerListener*)ctx;
+
+ if (listener) {
+ listener->serviceResponse(payload);
+ return true;
+ }
+
+ return false;
+}
+
+LunaServiceManager* s_instance = 0;
+
+/**
+* @brief Obtains the singleton LunaServiceManager.
+*
+* @retval the LunaServiceManager
+*/
+LunaServiceManager* LunaServiceManager::instance()
+{
+ bool retVal;
+ if (s_instance)
+ return s_instance;
+
+ s_instance = new LunaServiceManager();
+ retVal = s_instance->init();
+ if (!retVal)
+ goto error;
+
+ return s_instance;
+
+error:
+ fprintf(stderr, "*******************************************************************\n");
+ fprintf(stderr, "* Could got get an instance of LunaServiceManager. *\n");
+ fprintf(stderr, "* Try running with luna-dbus start; luna-dbus run <executable>. *\n");
+ fprintf(stderr, "*******************************************************************\n");
+ exit(-1);
+}
+
+/**
+* @brief Private constructor to enforce singleton.
+*/
+LunaServiceManager::LunaServiceManager() :
+ publicBus(0)
+ , privateBus(0)
+ , palmServiceHandle(0)
+ , publicBusHighPriority(0)
+ , privateBusHighPriority(0)
+ , palmServiceHandleHighPriority(0)
+{
+}
+
+LunaServiceManager::~LunaServiceManager()
+{
+ // ED : Close the single connection to DBUS.
+ if (palmServiceHandle) {
+ bool retVal;
+ LSError lserror;
+ LSErrorInit(&lserror);
+
+ retVal = LSUnregisterPalmService(palmServiceHandle, &lserror);
+ if (!retVal) {
+ g_warning("LSUnregisterPalmService ERROR %d: %s (%s @ %s:%d)",
+ lserror.error_code, lserror.message,
+ lserror.func, lserror.file, lserror.line);
+ LSErrorFree(&lserror);
+ }
+ }
+}
+
+bool LunaServiceManager::init()
+{
+ bool init;
+ LSError lserror;
+ LSErrorInit(&lserror);
+
+ String id("com.palm.luna-");
+ id.append(String::number(getpid()));
+ String active = (id + "-active");
+ String phone = (id + "-phone");
+ init = LSRegisterPalmService(id.utf8().data(), &palmServiceHandle, &lserror);
+ if (!init)
+ goto error;
+
+ init = LSGmainAttachPalmService(palmServiceHandle,
+ g_main_loop_new(g_main_context_default(), TRUE), &lserror);
+ if (!init)
+ goto error;
+
+ privateBus = LSPalmServiceGetPrivateConnection(palmServiceHandle);
+ publicBus = LSPalmServiceGetPublicConnection(palmServiceHandle);
+
+ if (privateBus) {
+ init = LSGmainSetPriority(privateBus, G_PRIORITY_DEFAULT, &lserror);
+ if (!init)
+ goto error;
+ }
+
+ if (publicBus) {
+ init = LSGmainSetPriority(publicBus, G_PRIORITY_DEFAULT, &lserror);
+ if (!init)
+ goto error;
+ }
+
+ init = LSRegisterPalmService(phone.utf8().data(), &palmServiceHandleHighPriority, &lserror);
+ if (!init)
+ goto error;
+
+ init = LSGmainAttachPalmService(palmServiceHandleHighPriority,
+ g_main_loop_new(g_main_context_default(), TRUE), &lserror);
+ if (!init)
+ goto error;
+
+ privateBusHighPriority = LSPalmServiceGetPrivateConnection(palmServiceHandleHighPriority);
+ publicBusHighPriority = LSPalmServiceGetPublicConnection(palmServiceHandleHighPriority);
+
+ if (privateBusHighPriority) {
+ init = LSGmainSetPriority(privateBusHighPriority, G_PRIORITY_HIGH, &lserror);
+ if (!init)
+ goto error;
+ }
+
+ if (publicBusHighPriority) {
+ init = LSGmainSetPriority(publicBusHighPriority, G_PRIORITY_HIGH, &lserror);
+ if (!init)
+ goto error;
+ }
+
+
+ init = LSRegisterPalmService(active.utf8().data(), &palmServiceHandleMediumPriority, &lserror);
+ if (!init)
+ goto error;
+
+ init = LSGmainAttachPalmService(palmServiceHandleMediumPriority,
+ g_main_loop_new(g_main_context_default(), TRUE), &lserror);
+ if (!init)
+ goto error;
+
+ privateBusMediumPriority = LSPalmServiceGetPrivateConnection(palmServiceHandleMediumPriority);
+ publicBusMediumPriority = LSPalmServiceGetPublicConnection(palmServiceHandleMediumPriority);
+
+ if (privateBusMediumPriority) {
+ init = LSGmainSetPriority(privateBusMediumPriority, G_PRIORITY_HIGH + 50, &lserror);
+ if (!init)
+ goto error;
+ }
+
+ if (publicBusMediumPriority) {
+ init = LSGmainSetPriority(publicBusMediumPriority, G_PRIORITY_HIGH + 50, &lserror);
+ if (!init)
+ goto error;
+ }
+
+error:
+ if (!init) {
+ g_warning("Cannot initialize LunaServiceManager ERROR %d: %s (%s @ %s:%d)",
+ lserror.error_code, lserror.message,
+ lserror.func, lserror.file, lserror.line);
+ LSErrorFree(&lserror);
+ }
+
+ return init;
+}
+
+/**
+* @brief This method will make the async call to DBUS.
+*
+* @param uri
+* @param payload
+* @param inListener
+*
+* @retval 0 if message could not be sent.
+* @retval >0 serial number for the message.
+*/
+unsigned long LunaServiceManager::call(const char* uri, const char* payload, LunaServiceManagerListener* inListener,
+ const char* callerId, bool usePrivateBus)
+{
+ bool retVal;
+ LSError lserror;
+ LSErrorInit(&lserror);
+ LSMessageToken token = 0;
+ LSHandle* serviceHandle = 0;
+
+ if (callerId && (!(*callerId)))
+ callerId = 0;
+
+ static int phoneAppIdLen = strlen("com.palm.app.phone");
+ if (callerId && !(strncmp(callerId, "com.palm.app.phone", phoneAppIdLen))) {
+
+ if (!usePrivateBus)
+ serviceHandle = publicBusHighPriority;
+ else
+ serviceHandle = privateBusHighPriority;
+
+ } else {
+/* else if (callerId && activeAppId && strncmp(callerId, activeAppId, strlen(activeAppId)) == 0) {
+
+
+ if (!usePrivateBus)
+ serviceHandle = publicBusMediumPriority;
+ else
+ serviceHandle = privateBusMediumPriority;
+ }
+*/
+ if (!usePrivateBus)
+ serviceHandle = publicBus;
+ else
+ serviceHandle = privateBus;
+ }
+
+ if (!inListener)
+ retVal = LSCallFromApplication(serviceHandle, uri, payload, callerId, 0, 0, &token, &lserror);
+ else {
+ retVal = LSCallFromApplication(serviceHandle, uri, payload, callerId, message_filter, inListener, &token, &lserror);
+ if (retVal) {
+ inListener->listenerToken = token;
+ inListener->sh = serviceHandle;
+ }
+ }
+
+ if (!retVal) {
+ g_warning("LSCallFromApplication ERROR %d: %s (%s @ %s:%d)",
+ lserror.error_code, lserror.message,
+ lserror.func, lserror.file, lserror.line);
+ LSErrorFree(&lserror);
+ token = 0;
+ goto error;
+ }
+
+error:
+ return token;
+}
+
+/**
+ * @brief Terminates a call causing any subscription for responses to end.
+ * This is also called by garbage collector's collect()
+ * when no more references to inListener exist.
+ *
+ * @param inListener
+ */
+void LunaServiceManager::cancel(LunaServiceManagerListener* inListener)
+{
+ LSError lserror;
+
+ if (!inListener || !inListener->listenerToken)
+ return;
+
+ LSErrorInit(&lserror);
+
+ if (!LSCallCancel(inListener->sh, inListener->listenerToken, &lserror)) {
+ g_warning("LSCallCancel ERROR %d: %s (%s @ %s:%d)",
+ lserror.error_code, lserror.message,
+ lserror.func, lserror.file, lserror.line);
+ LSErrorFree(&lserror);
+ }
+
+ // set the token to zero to indicate we have been canceled
+ inListener->listenerToken = 0;
+}
+};
+#endif
diff --git a/Source/WebCore/platform/webos/LunaServiceMgr.h b/Source/WebCore/platform/webos/LunaServiceMgr.h
new file mode 100644
index 0000000..cfcba24
--- /dev/null
+++ b/Source/WebCore/platform/webos/LunaServiceMgr.h
@@ -0,0 +1,56 @@
+
+#ifndef LunaServiceMgr_h
+#define LunaServiceMgr_h
+
+#if ENABLE(PALM_SERVICE_BRIDGE)
+
+#include <lunaservice.h>
+
+namespace WebCore {
+
+struct LunaServiceManagerListener {
+ LunaServiceManagerListener() : listenerToken(LSMESSAGE_TOKEN_INVALID), sh(0) { }
+ virtual ~LunaServiceManagerListener() { }
+ virtual void serviceResponse(const char* body) = 0;
+ LSMessageToken listenerToken;
+ LSHandle* sh;
+};
+
+
+//
+// LunaServiceManager
+//
+// This class is a singleton which handles all the client requests
+// for a WebKit instance.
+
+class LunaServiceManager {
+ public:
+ ~LunaServiceManager();
+
+ static LunaServiceManager* instance();
+ unsigned long call(const char* uri, const char* payload, LunaServiceManagerListener*, const char* callerId, bool usePrivateBus = false);
+ void cancel(LunaServiceManagerListener*);
+
+ private:
+ bool init();
+ LunaServiceManager();
+
+ LSHandle* publicBus;
+ LSHandle* privateBus;
+ LSPalmService* palmServiceHandle;
+
+ // The Medium Priority bus is used for the active app
+ LSHandle* publicBusMediumPriority;
+ LSHandle* privateBusMediumPriority;
+ LSPalmService* palmServiceHandleMediumPriority;
+
+ // The High Priority bus is used only for the Phone app
+ LSHandle* publicBusHighPriority;
+ LSHandle* privateBusHighPriority;
+ LSPalmService* palmServiceHandleHighPriority;
+};
+
+}
+
+#endif
+#endif
diff --git a/Source/WebCore/platform/webos/PalmServiceBridge.cpp b/Source/WebCore/platform/webos/PalmServiceBridge.cpp
new file mode 100644
index 0000000..838ce50
--- /dev/null
+++ b/Source/WebCore/platform/webos/PalmServiceBridge.cpp
@@ -0,0 +1,413 @@
+
+#include "config.h"
+#if ENABLE(PALM_SERVICE_BRIDGE)
+#include "PalmServiceBridge.h"
+
+#include "Document.h"
+#include "Event.h"
+#include "EventException.h"
+#include "EventListener.h"
+#include "EventNames.h"
+#include "ExceptionCode.h"
+#include "Frame.h"
+#include "Logging.h"
+#include "Page.h"
+#include <wtf/text/WTFString.h>
+
+#include "ScriptController.h"
+#include <wtf/RefCountedLeakCounter.h>
+
+#if USE(V8)
+#include "V8Proxy.h"
+#else
+#include "JSDOMWindow.h"
+#include "JSEventListener.h"
+#include "JSFunction.h"
+#include "ScriptSourceCode.h"
+#include "ScriptValue.h"
+#include "runtime_root.h"
+#include <runtime/JSLock.h>
+using namespace JSC;
+#endif
+
+#include <map>
+#include <set>
+
+namespace WebCore {
+
+typedef std::set<PalmServiceBridge*> ServicesSet;
+typedef std::map<Document*, ServicesSet*> ServicesSetMap;
+
+static bool sDebugServices = (getenv("DEBUG_SERVICES") ? true : false);
+
+#ifndef NDEBUG
+static WTF::RefCountedLeakCounter serviceBridgeCounter("PalmServiceBridge");
+#endif
+
+static ServicesSetMap* servicesByDocument()
+{
+ static ServicesSetMap map;
+ return ↦
+}
+
+/* static */ int PalmServiceBridge::numHandlesForUrl(const char* appId)
+{
+ for (ServicesSetMap::iterator setIt = servicesByDocument()->begin(); setIt != servicesByDocument()->end(); ++setIt) {
+ if (!strcmp(appId, setIt->first->url().string().utf8().data()))
+ return setIt->second->size();
+ }
+
+ return 0;
+}
+
+/* static */ void PalmServiceBridge::handlesForUrl(const char* appId, std::list<PalmServiceBridge*>& outHandles)
+{
+ outHandles.clear();
+ for (ServicesSetMap::iterator setIt = servicesByDocument()->begin(); setIt != servicesByDocument()->end(); ++setIt) {
+ if (!strcmp(appId, setIt->first->url().string().utf8().data())) {
+ ServicesSet* set = setIt->second;
+
+ for (ServicesSet::iterator s = set->begin(); s != set->end(); ++s)
+ outHandles.push_back(*s);
+
+ return;
+ }
+ }
+}
+
+static void addToServicesByDocument(Document* doc, PalmServiceBridge* svc)
+{
+ if (!doc || !svc)
+ return;
+
+ ServicesSet* set = 0;
+ ServicesSetMap::iterator it = servicesByDocument()->find(doc);
+ if (it == servicesByDocument()->end()) {
+ set = new ServicesSet();
+ (*servicesByDocument())[doc] = set;
+ } else
+ set = it->second;
+
+ set->insert(svc);
+}
+
+static void removeFromServicesByDocument(Document* doc, PalmServiceBridge* svc)
+{
+ if (!doc || !svc)
+ return;
+
+ ServicesSetMap::iterator it = servicesByDocument()->find(doc);
+ if (it == servicesByDocument()->end())
+ return;
+
+ ServicesSet* set = it->second;
+ if (!set)
+ return;
+
+ set->erase(svc);
+ if (!set->size()) {
+ // remove from the hash map
+ delete set;
+ servicesByDocument()->erase(it);
+ }
+}
+
+PalmServiceBridge::PalmServiceBridge(ScriptExecutionContext* context, bool subscribe)
+ : ActiveDOMObject(context, this)
+ , m_canceled(false)
+ , m_subscribed(subscribe)
+ , m_inServiceCallback(false)
+{
+ addToServicesByDocument(document(), this);
+
+#ifndef NDEBUG
+ serviceBridgeCounter.increment();
+#endif
+}
+
+bool PalmServiceBridge::init(Document* d, bool subscribe)
+{
+ m_subscribed = subscribe;
+ return true;
+}
+
+PalmServiceBridge::~PalmServiceBridge()
+{
+ ExceptionCode ec;
+ cancel(ec);
+
+#if USE(V8)
+ if (!m_callbackFunction.IsEmpty()) {
+ m_callbackFunction.Dispose();
+ m_callbackFunction.Clear();
+ }
+#endif // USE(V8)
+
+ if (scriptExecutionContext() && document())
+ removeFromServicesByDocument(document(), this);
+
+#ifndef NDEBUG
+ serviceBridgeCounter.decrement();
+#endif
+}
+
+void PalmServiceBridge::detachServices(Document* doc)
+{
+ ServicesSetMap::iterator it = servicesByDocument()->find(doc);
+ if (it == servicesByDocument()->end())
+ return;
+
+ ServicesSet* services = it->second;
+ servicesByDocument()->erase(it);
+
+ if (services) {
+ while (services->size()) {
+ ServicesSet::iterator sit = services->begin();
+ ExceptionCode ec;
+ (*sit)->cancel(ec);
+ services->erase(sit);
+ }
+ delete services;
+ }
+
+}
+
+void PalmServiceBridge::cancelServices(Document* doc)
+{
+ ServicesSetMap::iterator it = servicesByDocument()->find(doc);
+ if (it == servicesByDocument()->end())
+ return;
+
+ ServicesSet* services = it->second;
+
+ if (services) {
+ for (ServicesSet::iterator sit = services->begin(); sit != services->end(); ++sit) {
+ PalmServiceBridge* br = *sit;
+ ExceptionCode ec;
+ br->cancel(ec);
+ }
+ }
+}
+
+String PalmServiceBridge::version()
+{
+ return String("1.1");
+}
+
+#if USE(V8)
+void PalmServiceBridge::setOnservicecallback(v8::Handle<v8::Function> func)
+{
+ if (!m_callbackFunction.IsEmpty()) {
+ m_callbackFunction.Dispose();
+ m_callbackFunction.Clear();
+ }
+ m_callbackFunction = v8::Persistent<v8::Function>::New(func);
+
+ // Comment out below to use weak references on functions.
+ m_callbackFunction.MakeWeak(this, (void (*)(v8::Persistent<v8::Value>, void*)) PalmServiceBridge::WeakRefCallback);
+}
+
+// We get this callback from v8 when the only reference to our callback function
+// are also weak. We will therefore release our reference to it as well.
+void PalmServiceBridge::WeakRefCallback(v8::Persistent<v8::Value> object, void* parameter)
+{
+ PalmServiceBridge* pThis = (PalmServiceBridge*)parameter;
+
+ // In the case when we loose the function refernce during a callback, do nothing.
+ if (pThis->m_inServiceCallback)
+ return;
+
+ ExceptionCode ec;
+ pThis->cancel(ec);
+
+ if (!pThis->m_callbackFunction.IsEmpty()) {
+ pThis->m_callbackFunction.Dispose();
+ pThis->m_callbackFunction.Clear();
+ }
+
+ // Set m_canceled=true here in case this callback fn is being disposed
+ // while we're executing the callback function itself NOV-35217
+ pThis->m_canceled = true;
+}
+#else // USE(V8)
+
+#endif // USE(V8)
+
+int PalmServiceBridge::token()
+{
+ return (int)listenerToken;
+}
+
+int PalmServiceBridge::call(const String& uri, const String& payload, ExceptionCode& ec)
+{
+ bool usePrivateBus = false;
+
+ JSValue identifier;
+ ExecState* exec = 0;
+
+ if (document()->frame()) {
+
+ ScriptController* script = document()->frame()->script();
+ if (script) {
+ identifier = script->executeScript(ScriptSourceCode("PalmSystem && PalmSystem.getIdentifier()")).jsValue();
+ JSGlobalObject* globalObject = document()->frame()->script()->bindingRootObject()->globalObject();
+ exec = globalObject->globalExec();
+ }
+ usePrivateBus = true;
+ }
+
+ /*
+ * Determine here whether the caller is privileged or not (public or private bus access. This is because
+ * LunaServiceManagerListener doesn't expose a way to get to the document() fn of this object, and there's no RTTI
+ * on this platform to do a safe cast back up to PalmServiceBridge.
+ *
+ * Instead, overloading LunaServiceManager::call() with a parameter to specify bus type...
+ */
+
+
+ LunaServiceManager::instance()->call(uri.utf8().data(), payload.utf8().data(), this, identifier.toString(exec)->value(exec).utf8().data(), usePrivateBus);
+ if (LSMESSAGE_TOKEN_INVALID == listenerToken) {
+ ExceptionCode ec;
+ cancel(ec);
+ }
+
+ return (int)listenerToken;
+}
+
+void PalmServiceBridge::serviceResponse(const char* body)
+{
+ if (m_canceled || !document())
+ return;
+
+ if (!body)
+ body = "";
+
+ Frame* frame = document()->frame();
+
+#if USE(V8)
+
+
+ // Protect this object so it does not get collected when we call into V8.
+ // V8 is free to do garbage collection anytime we asked it to allocate anything,
+ // which will be first when the argument string is allocated below.
+ ref();
+ ScriptController* script = frame->script();
+
+ script->gcProtectJSWrapper(this);
+ m_inServiceCallback = true;
+
+ if (!body)
+ body = "";
+
+ if (!m_callbackFunction.IsEmpty() && m_callbackFunction->IsFunction()) {
+
+ if (script->canExecuteScripts(AboutToExecuteScript)) {
+ v8::Local<v8::Context> context = V8Proxy::context(frame);
+ if (!context.IsEmpty()) {
+ v8::Context::Scope scope(context);
+ V8Proxy* proxy = V8Proxy::retrieve(frame);
+
+ v8::Local<v8::Value> args = v8::String::New(body);
+
+ // Script timeout is handled in the proxy.
+ if (!m_callbackFunction.IsEmpty())
+ proxy->callFunction(v8::Persistent<v8::Function>::Cast(m_callbackFunction),
+ context->Global(), 1, &args);
+ }
+ }
+
+ if (!m_subscribed) {
+ ExceptionCode ec;
+ cancel(ec);
+ }
+ }
+
+ m_inServiceCallback = false;
+ script->gcUnprotectJSWrapper(this);
+ deref();
+
+ Document::updateStyleForAllDocuments();
+
+#else
+ JSGlobalObject* globalObject = frame->script()->bindingRootObject()->globalObject();
+ ExecState* exec = globalObject->globalExec();
+
+ JSC::JSLock lock;
+
+ const JSEventListener* listener = JSEventListener::cast(m_callbackFunction.get());
+ JSObject* function = 0;
+ if (listener)
+ function = listener->jsFunction(scriptExecutionContext());
+ if (!function)
+ return;
+ CallData callData;
+ CallType callType = JSFunction::getCallData(function, callData);
+
+ if (callType == CallTypeNone)
+ return;
+
+ MarkedArgumentBuffer args;
+ args.append(jsString(exec, String::fromUTF8(body)));
+
+ globalObject->globalData().timeoutChecker.start();
+ JSC::call(exec, function, callType, callData, function, args);
+ globalObject->globalData().timeoutChecker.stop();
+
+ if (exec->hadException())
+ reportCurrentException(exec);
+
+ Document::updateStyleForAllDocuments();
+#endif
+}
+
+void PalmServiceBridge::cancel(ExceptionCode& ec)
+{
+ if (m_canceled)
+ return;
+
+ m_canceled = true;
+ if (listenerToken)
+ LunaServiceManager::instance()->cancel(this);
+
+#if USE(V8)
+ if (!m_callbackFunction.IsEmpty()) {
+ m_callbackFunction.Dispose();
+ m_callbackFunction.Clear();
+ }
+#endif // USE(V8)
+}
+
+void PalmServiceBridge::stop()
+{
+ ExceptionCode ec;
+ cancel(ec);
+}
+
+bool PalmServiceBridge::canSuspend() const
+{
+ return false;
+}
+
+void PalmServiceBridge::contextDestroyed()
+{
+#if USE(V8)
+ ASSERT(m_callbackFunction.IsEmpty());
+#endif
+ ActiveDOMObject::contextDestroyed();
+}
+
+ScriptExecutionContext* PalmServiceBridge::scriptExecutionContext() const
+{
+ return ActiveDOMObject::scriptExecutionContext();
+}
+
+Document* PalmServiceBridge::document() const
+{
+ ASSERT(scriptExecutionContext()->isDocument());
+ return static_cast<Document*>(scriptExecutionContext());
+}
+
+
+}
+
+#endif
diff --git a/Source/WebCore/platform/webos/PalmServiceBridge.h b/Source/WebCore/platform/webos/PalmServiceBridge.h
new file mode 100644
index 0000000..83f723f
--- /dev/null
+++ b/Source/WebCore/platform/webos/PalmServiceBridge.h
@@ -0,0 +1,89 @@
+#ifndef PalmServiceBridge_h
+#define PalmServiceBridge_h
+
+#if ENABLE(PALM_SERVICE_BRIDGE)
+
+#include "ActiveDOMObject.h"
+#include "Event.h"
+#include "EventListener.h"
+#include "EventTarget.h"
+#include "LunaServiceMgr.h"
+#include <wtf/OwnPtr.h>
+
+#if USE(V8)
+#include "v8.h"
+#else
+#include <heap/Strong.h>
+#include <heap/StrongInlines.h>
+#endif
+
+#include <glib.h>
+#include <list>
+
+
+namespace WebCore {
+
+class Document;
+
+
+class PalmServiceBridge : public RefCounted<PalmServiceBridge>,
+ public LunaServiceManagerListener,
+ public ActiveDOMObject {
+ public:
+ static PassRefPtr<PalmServiceBridge> create(ScriptExecutionContext* context, bool subscribe = false) { return adoptRef(new PalmServiceBridge(context, subscribe)); }
+ bool init(Document*, bool subscribed = false);
+ ~PalmServiceBridge();
+
+ static int numHandlesForUrl(const char* appId);
+ static void handlesForUrl(const char* appId, std::list<PalmServiceBridge*>& outHandles);
+
+ virtual PalmServiceBridge* toPalmServiceBridge() { return this; }
+
+ static void detachServices(Document*);
+ static void cancelServices(Document*);
+
+ String version();
+
+ int token();
+
+ int call(const String& uri, const String& payload, ExceptionCode&);
+ void cancel(ExceptionCode&);
+
+ // callback from LunaServiceManagerListener
+ virtual void serviceResponse(const char* body);
+
+ Document* document() const;
+
+#if USE(V8)
+ static void WeakRefCallback(v8::Persistent<v8::Value>, void* parameter);
+ void setOnservicecallback(v8::Handle<v8::Function>);
+#else
+ void setOnservicecallback(PassRefPtr<EventListener> eventListener) { m_callbackFunction = eventListener; }
+ EventListener* onservicecallback() const { return m_callbackFunction.get(); }
+#endif
+
+ virtual ScriptExecutionContext* scriptExecutionContext() const;
+
+ // ActiveDOMObject:
+ virtual void contextDestroyed();
+ virtual bool canSuspend() const;
+ virtual void stop();
+
+ private:
+#if USE(V8)
+ v8::Persistent<v8::Function> m_callbackFunction;
+#else
+ RefPtr<EventListener> m_callbackFunction;
+#endif
+ bool m_canceled;
+ bool m_subscribed;
+ bool m_inServiceCallback;
+
+ PalmServiceBridge(ScriptExecutionContext*, bool);
+ PalmServiceBridge();
+};
+
+}
+
+#endif
+#endif
diff --git a/Source/WebCore/platform/webos/PalmServiceBridge.idl b/Source/WebCore/platform/webos/PalmServiceBridge.idl
new file mode 100644
index 0000000..f8fc656
--- /dev/null
+++ b/Source/WebCore/platform/webos/PalmServiceBridge.idl
@@ -0,0 +1,19 @@
+ interface [
+ ActiveDOMObject,
+ Constructor,
+ Conditional=PALM_SERVICE_BRIDGE,
+ CallWith=ScriptExecutionContext
+ ] PalmServiceBridge {
+
+ // event handler attributes
+ attribute EventListener onservicecallback;
+ readonly attribute unsigned long token;
+
+ // request
+ unsigned long call(in DOMString method, in DOMString url)
+ raises(DOMException);
+
+ void cancel()
+ raises(DOMException);
+ };
+
diff --git a/Tools/qmake/mkspecs/features/features.prf b/Tools/qmake/mkspecs/features/features.prf
index 8849025..19afc4a 100644
--- a/Tools/qmake/mkspecs/features/features.prf
+++ b/Tools/qmake/mkspecs/features/features.prf
@@ -139,6 +139,10 @@ defineTest(detectFeatures) {
# Minibrowser must be able to query for QtTestSupport
build?(qttestsupport): WEBKIT_CONFIG += have_qttestsupport
+ enable?(PALM_SERVICE_BRIDGE) {
+ FEATURES_DEFINES_JAVASCRIPT += ENABLE_PALM_SERVICE_BRIDGE=1
+ }