-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathindex.html
3518 lines (3484 loc) · 155 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
Presentation API
</title>
<script src="https://www.w3.org/Tools/respec/respec-w3c" async="async"
class="remove"></script>
<script class="remove">
var respecConfig = {
specStatus: 'ED',
edDraftURI: 'https://w3c.github.io/presentation-api/',
shortName: 'presentation-api',
editors: [
{
w3cid: 68454,
name: 'Mark Foltz',
company: 'Google',
mailto: '[email protected]'
},
],
formerEditors: [
{
w3cid: 63802,
name: 'Dominik Röttsches',
company: 'Intel',
note: 'until April 2015'
}
],
license: "w3c-software-doc",
previousMaturity: 'CR',
previousPublishDate: '2017-06-01',
otherLinks: [
{
key: 'Test suite',
data: [
{
value: 'GitHub web-platform-tests/presentation-api',
href: 'https://github.com/web-platform-tests/wpt/tree/master/presentation-api'
},
{
value: 'wpt.live/presentation-api/',
href: 'https://wpt.live/presentation-api/'
}
]
}
],
sotdAfterWGinfo: true,
group: 'secondscreen',
github: 'https://github.com/w3c/presentation-api',
xref: ['dom', 'fileapi', 'secure-contexts', 'html', 'url', 'webidl', 'webrtc', 'websockets', 'infra'],
localBiblio: {
DIAL: {
title: 'DIscovery And Launch Protocol Specification',
href: 'http://www.dial-multiscreen.org/dial-protocol-specification',
authors: [
'Netflix',
'YouTube'
],
publisher: 'Netflix'
}
},
crEnd: '2016-09-12',
implementationReportURI: 'https://www.w3.org/wiki/Second_Screen/Implementation_Status#Tests'
};
</script>
<style>
/* Note formatting taken from HTML5 spec */
.note { border-left-style: solid; border-left-width: 0.25em; background: none repeat scroll 0 0 #E9FBE9; border-color: #52E052; }
.note em, .warning em, .note i, .warning i { font-style: normal; }
p.note, div.note { padding: 0.5em 2em; }
span.note { padding: 0 2em; }
.note p:first-child { margin-top: 0; }
.note p:last-child { margin-bottom: 0; }
p.note:before { content: 'NOTE: '; }
.non-normative { border-left-style: solid; border-left-width: 0.25em; background: none repeat scroll 0 0 #E9FBE9; border-color: #52E052; }
p.non-normative:before { content: 'Non-normative: '; font-weight: bolder;}
p.non-normative, div.non-normative { padding: 0.5em 2em; }
.algorithm li {
margin-bottom: 0.5em;
}
.interface dd, .parameters dt {
margin-bottom: 0.5em;
}
code { color: orangered; }
table { border-collapse: collapse; border-style: hidden hidden none hidden; }
table thead, table tbody { border-bottom: solid; }
table td, table th { border-left: solid; border-right: solid; border-bottom: solid thin; vertical-align: top; padding: 0.2em; }
dfn { font-weight: bolder; font-style: normal; }
var { hyphens: none; }
.copyright { font-size: small; }
.issue[id^='issue-'] > *:not([role='heading']) { display: none; }
</style>
</head>
<body>
<section id="abstract">
<p>
This specification defines an API to enable Web content to access
presentation displays and use them for presenting Web content.
</p>
</section>
<section id="sotd">
<p>
Since publication as Candidate Recommendation on <a href=
"https://www.w3.org/TR/2017/CR-presentation-api-20170601/">01 June
2017</a>, the Working Group updated the steps to construct a
<code>PresentationRequest</code> to ignore a URL with an unsupported
scheme, placed further restrictions on how receiving browsing contexts
are allowed to navigate themselves, and dropped the definition of the
<code>BinaryType</code> enum in favor of the one defined in the HTML
specification. Other interfaces defined in this document did not change
other than to adjust to WebIDL updates. Various clarifications and
editorial updates were also made. See the <a href=
"#changes-since-01-june-2017">list of changes</a> for details.
</p>
<p>
No feature has been identified as being <strong>at risk</strong>.
</p>
<p>
The Second Screen Working Group will refine the <a href=
"http://w3c-test.org/presentation-api/">test suite</a> for the
Presentation API during the Candidate Recommendation period and update
the <a href=
"https://www.w3.org/wiki/Second_Screen/Implementation_Status#Tests">preliminary
implementation report</a>. For this specification to advance to
Proposed Recommendation, two independent, interoperable implementations
of each feature must be demonstrated, as detailed in the <a href=
"#candidate-recommendation-exit-criteria">Candidate Recommendation exit
criteria</a> section.
</p>
</section>
<section class="informative">
<h2>
Introduction
</h2>
<p>
The Presentation API aims to make <a>presentation displays</a> such as
projectors, attached monitors, and network-connected TVs available to
the Web. It takes into account displays that are attached using wired
(HDMI, DVI, or similar) and wireless technologies (Miracast,
Chromecast, DLNA, AirPlay, or similar).
</p>
<p>
Devices with limited screen size lack the ability to show Web content
to a larger audience: a group of colleagues in a conference room, or
friends and family at home, for example. Web content shown on a larger
<a>presentation display</a> has greater perceived quality, legibility,
and impact.
</p>
<p>
At its core, the Presentation API enables a <a>controller</a> page to
show a <a href="#dfn-receiving-browsing-context">presentation</a> page
on a <a>presentation display</a> and exchange messages with it. How the
presentation page is transmitted to the display and how messages are
exchanged between it and the controller page are left to the
implementation; this allows the use of a wide variety of display
technologies.
</p>
<p>
For example, if the <a>presentation display</a> is connected by HDMI or
Miracast, which only allow audio and video to be transmitted, the user
agent (UA) hosting the <a>controller</a> will also render the <a href=
"#dfn-receiving-browsing-context">presentation</a>. It then uses the
operating system to send the resulting graphical and audio output to
the presentation display. We refer to this situation as the
<dfn data-lt="1-UA">1-UA mode</dfn> implementation of the Presentation
API. The only requirements are that the user agent is able to send
graphics and audio from rendering the presentation to the presentation
display, and exchange messages internally between the controller and
presentation pages.
</p>
<p>
If the <a>presentation display</a> is able to render HTML natively and
communicate with the <a>controller</a> via a network, the user agent
hosting the controller does not need to render the <a href=
"#dfn-receiving-browsing-context">presentation</a>. Instead, the user
agent acts as a proxy that requests the presentation display to load
and render the presentation page itself. Message exchange is done over
a network connection between the user agent and the presentation
display. We refer to this situation as the <dfn data-lt="2-UA">2-UA
mode</dfn> implementation of the Presentation API.
</p>
<p>
The Presentation API is intended to be used with user agents that
attach to <a>presentation displays</a> in <a>1-UA mode</a>, <a>2-UA
mode</a>, and possibly other means not listed above. To improve
interoperability between user agents and presentation displays,
standardization of network communication between browsers and displays
is being considered in the <a href=
"https://www.w3.org/community/webscreens/">Second Screen Community
Group</a>.
</p>
</section>
<section id="use-cases-and-requirements" class="informative">
<h2>
Use cases and requirements
</h2>
<p>
Use cases and requirements are captured in a separate <a href=
"https://github.com/w3c/presentation-api/blob/main/uc-req.md">Presentation
API Use Cases and Requirements</a> document.
</p>
</section>
<section id="conformance">
<p>
Requirements phrased in the imperative as part of algorithms (such as
"strip any leading space characters" or "return false and terminate
these steps") are to be interpreted with the meaning of the key word
("MUST", "SHOULD", "MAY", etc.) used in introducing the algorithm.
</p>
<p>
Conformance requirements phrased as algorithms or specific steps may be
implemented in any manner, so long as the result is equivalent. (In
particular, the algorithms defined in this specification are intended
to be easy to follow, and not intended to be performant.)
</p>
<section>
<h3>
Conformance classes
</h3>
<p>
This specification describes the conformance criteria for two classes
of <dfn data-lt="user agent">user agents</dfn>.
</p>
<dl>
<dt>
<dfn>Controlling user agent</dfn>
</dt>
<dd>
<p>
Web browsers that conform to the specifications of a
<a>controlling user agent</a> must be able to start and control
presentations by providing a <a>controlling browsing context</a>
as described in this specification. This context implements the
<a><code>Presentation</code></a>,
<a><code>PresentationAvailability</code></a>,
<a><code>PresentationConnection</code></a>,
<a><code>PresentationConnectionAvailableEvent</code></a>,
<a><code>PresentationConnectionCloseEvent</code></a>, and
<a><code>PresentationRequest</code></a> interfaces.
</p>
</dd>
<dt>
<dfn>Receiving user agent</dfn>
</dt>
<dd>
<p>
Web browsers that conform to the specifications of a <a>receiving
user agent</a> must be able to render presentations by providing
a <a>receiving browsing context</a> as described in this
specification. This context implements the
<a><code>Presentation</code></a>,
<a><code>PresentationConnection</code></a>,
<a><code>PresentationConnectionAvailableEvent</code></a>,
<a><code>PresentationConnectionCloseEvent</code></a>,
<a><code>PresentationConnectionList</code></a>, and
<a><code>PresentationReceiver</code></a> interfaces.
</p>
</dd>
</dl>
<p>
One user agent may act both as a <a>controlling user agent</a> and as
a <a>receiving user agent</a>, if it provides both browsing contexts
and implements all of their required interfaces. This can happen when
the same user agent is able to host the <a>controlling browsing
context</a> and the <a>receiving browsing context</a> for a
presentation, as in the <a>1-UA mode</a> implementation of the API.
</p>
<p>
Conformance requirements phrased against a <a>user agent</a> apply
either to a <a>controlling user agent</a>, a <a>receiving user
agent</a> or to both classes, depending on the context.
</p>
</section>
</section>
<section>
<h2>
Terminology
</h2>
<p>
The terms <dfn data-cite="ecmascript/#sec-code-realms">JavaScript
realm</dfn> and <dfn data-cite="ecmascript/#sec-code-realms">current
realm</dfn> are used as defined in [[!ECMASCRIPT]]. The terms
<dfn data-cite="ecmascript/#sec-promise-resolve-functions" data-lt=
"resolve">resolved</dfn> and <dfn data-cite=
"ecmascript/#sec-rejectpromise" data-lt="reject">rejected</dfn> in the
context of {{Promise}} objects are used as defined in [[!ECMASCRIPT]].
</p>
<p>
The terms <dfn data-cite=
"rfc9110#field.accept-language">Accept-Language</dfn> and
<dfn data-cite="rfc9110#authentication">HTTP authentication</dfn> are
used as defined in [[!RFC9110]].
</p>
<p>
The term <dfn data-cite="rfc6265#storage-model">cookie store</dfn> is
used as defined in [[!RFC6265]].
</p>
<p>
The term <dfn data-cite="rfc4122#section-1">UUID</dfn> is used as
defined in [[!RFC4122]].
</p>
<p>
The term <dfn data-cite="DIAL#">DIAL</dfn> is used as defined in
[[DIAL]].
</p>
<p>
The term <dfn>reload a document</dfn> refers to steps run when the
{{Location/reload()}} method gets called in [[!HTML]].
</p>
<p>
The term <dfn>local storage area</dfn> refers to the storage areas
exposed by the {{WindowLocalStorage/localStorage}} attribute, and the
term <dfn>session storage area</dfn> refers to the storage areas
exposed by the {{WindowSessionStorage/sessionStorage}} attribute in
[[!HTML]].
</p>
<p>
This specification references terms exported by other specifications,
see <a href="#index-defined-elsewhere"></a>. It also references the
following internal concepts from other specifications:
</p>
<ul>
<li>
<dfn>parse a url</dfn>, <a href=
"https://html.spec.whatwg.org/multipage/urls-and-fetching.html#parse-a-url"
title="Definition of 'parse a url' in HTML">defined in HTML</a>
[[!HTML]]
</li>
<li>
<dfn>creating a new browsing context</dfn>, <a href=
"https://html.spec.whatwg.org/multipage/browsers.html#creating-a-new-browsing-context"
title=
"Definition of 'creating a new browsing context' in HTML">defined in
HTML</a> [[!HTML]]
</li>
<li>
<dfn>session history</dfn>, <a href=
"https://html.spec.whatwg.org/multipage/history.html#session-history"
title="Definition of 'session history' in HTML">defined in HTML</a>
[[!HTML]]
</li>
<li>
<dfn>allowed to navigate</dfn>, <a href=
"https://html.spec.whatwg.org/multipage/browsers.html#allowed-to-navigate"
title="Definition of 'allowed to navigate' in HTML">defined in
HTML</a> [[!HTML]]
</li>
<li>
<dfn>navigating to a fragment identifier</dfn>, <a href=
"https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate-fragid"
title="Definition of 'navigate to a fragment' in HTML">defined in
HTML</a> [[!HTML]]
</li>
<li>
<dfn>unload a document</dfn>, <a href=
"https://html.spec.whatwg.org/multipage/document-lifecycle.html#unload-a-document"
title="Definition of 'unload a document' in HTML">defined in HTML</a>
[[!HTML]]
</li>
<li>
<dfn>database</dfn>, <a href=
"https://www.w3.org/TR/IndexedDB/#database" title=
"Definition of 'database' in Indexed Database API">defined in Indexed
Database API</a> [[!INDEXEDDB]]
</li>
</ul>
</section>
<section class="informative">
<h2>
Examples
</h2>
<p>
This section shows example codes that highlight the usage of main
features of the Presentation API. In these examples,
<code>controller.html</code> implements the controller and
<code>presentation.html</code> implements the presentation. Both pages
are served from the domain <code>https://example.org</code>
(<code>https://example.org/controller.html</code> and
<code>https://example.org/presentation.html</code>). These examples
assume that the controlling page is managing one presentation at a
time. Please refer to the comments in the code examples for further
details.
</p>
<section>
<h3>
Monitoring availability of presentation displays
</h3>
<p>
This code renders a button that is visible when there is at least one
compatible <a>presentation display</a> that can present
<code>https://example.com/presentation.html</code> <em>or</em>
<code>https://example.net/alternate.html</code>.
</p>
<p>
Monitoring of display availability is done by first creating a
<a>PresentationRequest</a> with the URLs you want to present, then
calling <a data-link-for="PresentationRequest">getAvailability</a> to
obtain a <a>PresentationAvailability</a> object whose <a>change</a>
event will fire when presentation availability changes state.
</p>
<pre class="example">
<!-- controller.html -->
<button id="presentBtn" style="display: none;">Present</button>
<script>
// The Present button is visible if at least one presentation display is available
var presentBtn = document.getElementById("presentBtn");
// It is also possible to use relative presentation URL e.g. "presentation.html"
var presUrls = ["https://example.com/presentation.html",
"https://example.net/alternate.html"];
// show or hide present button depending on display availability
var handleAvailabilityChange = function(available) {
presentBtn.style.display = available ? "inline" : "none";
};
// Promise is resolved as soon as the presentation display availability is
// known.
var request = new PresentationRequest(presUrls);
request.getAvailability().then(function(availability) {
// availability.value may be kept up-to-date by the controlling UA as long
// as the availability object is alive. It is advised for the Web developers
// to discard the object as soon as it's not needed.
handleAvailabilityChange(availability.value);
availability.onchange = function() { handleAvailabilityChange(this.value); };
}).catch(function() {
// Availability monitoring is not supported by the platform, so discovery of
// presentation displays will happen only after request.start() is called.
// Pretend the devices are available for simplicity; or, one could implement
// a third state for the button.
handleAvailabilityChange(true);
});
</script>
</pre>
</section>
<section>
<h3>
Starting a new presentation
</h3>
<p>
When the user clicks <code>presentBtn</code>, this code requests
presentation of one of the URLs in the <a>PresentationRequest</a>.
When <a data-link-for="PresentationRequest">start</a> is called, the
browser typically shows a dialog that allows the user to select one
of the compatible displays that are available. The first URL in the
<a>PresentationRequest</a> that is compatible with the chosen display
will be presented on that display.
</p>
<p>
The <a data-link-for="PresentationRequest">start</a> method resolves
with a <a>PresentationConnection</a> object that is used to track the
state of the presentation, and exchange messages with the
presentation page once it's loaded on the display.
</p>
<pre class="example">
<!-- controller.html -->
<script>
presentBtn.onclick = function () {
// Start new presentation.
request.start()
// The connection to the presentation will be passed to setConnection on
// success.
.then(setConnection);
// Otherwise, the user canceled the selection dialog or no screens were
// found.
};
</script>
</pre>
</section>
<section>
<h3>
Reconnecting to an existing presentation
</h3>
<p>
The presentation continues to run even after the original page that
started the presentation closes its <a>PresentationConnection</a>,
navigates, or is closed. Another page can use the <a data-link-for=
"PresentationConnection">id</a> on the <a>PresentationConnection</a>
to reconnect to an existing presentation and resume control of it.
This is only guaranteed to work from the same browser that started
the presentation.
</p>
<pre class="example">
<!-- controller.html -->
<button id="reconnectBtn" style="display: none;">Reconnect</button>
<script>
var reconnect = function () {
// read presId from localStorage if exists
var presId = localStorage["presId"];
// presId is mandatory when reconnecting to a presentation.
if (!!presId) {
request.reconnect(presId)
// The new connection to the presentation will be passed to
// setConnection on success.
.then(setConnection);
// No connection found for presUrl and presId, or an error occurred.
}
};
// On navigation of the controller, reconnect automatically.
document.addEventListener("DOMContentLoaded", reconnect);
// Or allow manual reconnection.
const reconnectBtn = document.querySelector("#reconnectBtn");
reconnectBtn.onclick = reconnect;
</script>
</pre>
</section>
<section>
<h3>
Starting a presentation by the controlling user agent
</h3>
<p>
Some browsers have a way for users to start a presentation without
interacting directly with the controlling page. Controlling pages can
opt into this behavior by setting the <a data-link-for=
"Presentation">defaultRequest</a> property on
<code>navigator.presentation</code>, and listening for a
<a>connectionavailable</a> event that is fired when a presentation is
started this way. The <a>PresentationConnection</a> passed with the
event behaves the same as if the page had called <a data-link-for=
"PresentationRequest">start</a>.
</p>
<pre class="example">
<!-- controller.html -->
<!-- Setting presentation.defaultRequest allows the page to specify the
PresentationRequest to use when the controlling UA initiates a
presentation. -->
<script>
navigator.presentation.defaultRequest = new PresentationRequest(presUrls);
navigator.presentation.defaultRequest.onconnectionavailable = function(evt) {
setConnection(evt.connection);
};
</script>
</pre>
</section>
<section>
<h3>
Monitoring the connection state and exchanging data
</h3>
<p>
Once a presentation has started, the returned
<a>PresentationConnection</a> is used to monitor its state and
exchange messages with it. Typically the user will be given the
choice to disconnect from or terminate the presentation from the
controlling page.
</p>
<p>
Since the the controlling page may connect to and disconnect from
multiple presentations during its lifetime, it's helpful to keep
track of the current <a>PresentationConnection</a> and its state.
Messages can only be sent and received on connections in a
<a data-link-for="PresentationConnectionState">connected</a> state.
</p>
<pre class="example">
<!-- controller.html -->
<button id="disconnectBtn" style="display: none;">Disconnect</button>
<button id="stopBtn" style="display: none;">Stop</button>
<script>
let connection;
// The Disconnect and Stop buttons are visible if there is a connected presentation
const stopBtn = document.querySelector("#stopBtn");
const disconnectBtn = document.querySelector("#disconnectBtn");
stopBtn.onclick = _ => {
connection && connection.terminate();
};
disconnectBtn.onclick = _ => {
connection && connection.close();
};
function setConnection(newConnection) {
// Disconnect from existing presentation, if not attempting to reconnect
if (connection && connection != newConnection && connection.state != 'closed') {
connection.onclose = undefined;
connection.close();
}
// Set the new connection and save the presentation ID
connection = newConnection;
localStorage["presId"] = connection.id;
function showConnectedUI() {
// Allow the user to disconnect from or terminate the presentation
stopBtn.style.display = "inline";
disconnectBtn.style.display = "inline";
reconnectBtn.style.display = "none";
}
function showDisconnectedUI() {
disconnectBtn.style.display = "none";
stopBtn.style.display = "none";
reconnectBtn.style.display = localStorage["presId"] ? "inline" : "none";
}
// Monitor the connection state
connection.onconnect = _ => {
showConnectedUI();
// Register message handler
connection.onmessage = message => {
console.log(`Received message: ${message.data}`);
};
// Send initial message to presentation page
connection.send("Say hello");
};
connection.onclose = _ => {
connection = null;
showDisconnectedUI();
};
connection.onterminate = _ => {
// Remove presId from localStorage if exists
delete localStorage["presId"];
connection = null;
showDisconnectedUI();
};
};
</script>
</pre>
</section>
<section>
<h3>
Listening for incoming presentation connections
</h3>
<p>
This code runs on the presented page
(<code>https://example.org/presentation.html</code>). Presentations
may be connected to from multiple controlling pages, so it's
important that the presented page listen for incoming connections on
the <a data-link-for="PresentationReceiver">connectionList</a>
object.
</p>
<pre class="example">
<!-- presentation.html -->
<script>
var addConnection = function(connection) {
connection.onmessage = function (message) {
if (message.data == "Say hello")
connection.send("hello");
};
};
navigator.presentation.receiver.connectionList.then(function (list) {
list.connections.map(function (connection) {
addConnection(connection);
});
list.onconnectionavailable = function (evt) {
addConnection(evt.connection);
};
});
</script>
</pre>
</section>
<section>
<h3>
Passing locale information with a message
</h3>
<pre class="example">
<!-- controller.html -->
<script>
connection.send('{"string": "你好,世界!", "lang": "zh-CN"}');
connection.send('{"string": "こんにちは、世界!", "lang": "ja"}');
connection.send('{"string": "안녕하세요, 세계!", "lang": "ko"}');
connection.send('{"string": "Hello, world!", "lang": "en-US"}');
</script>
<!-- presentation.html -->
<script>
connection.onmessage = function (message) {
var messageObj = JSON.parse(message.data);
var spanElt = document.createElement("SPAN");
spanElt.lang = messageObj.lang;
spanElt.textContent = messageObj.string;
document.body.appendChild(spanElt);
};
</script>
</pre>
</section>
<section>
<h3>
Creating a second presentation from the same controlling page
</h3>
<p>
It's possible for a controlling page to start and control two
independent presentations on two different presentation displays.
This code shows how a second presentation can be added to the first
one in the examples above.
</p>
<pre class="example">
<!-- controller.html -->
<!-- The same controlling page can create and manage multiple presentations,
by calling start() multiple times. -->
<button id="secondPresentBtn" style="display: none;">Present Again</button>
<script>
var secondPresentBtn = document.getElementById("secondPresentBtn");
var secondPresUrl = "https://example.com/second-presentation.html";
var secondRequest = new PresentationRequest(secondPresUrl);
// For simplicity, the logic to handle screen availability for secondRequest
// and update the status of secondPresentBtn is omitted.
secondPresentBtn.onclick = function () {
// Start new presentation, likely on a different screen than the original
// request.
secondRequest.start().then(setSecondConnection);
};
function setSecondConnection(newConnection) {
// Logic to handle messages to/from second-presentation.html.
};
</script>
</pre>
</section>
</section>
<section>
<h2>
API
</h2>
<section>
<h3>
Common idioms
</h3>
<p>
A <dfn data-lt=
"presentation display|presentation displays">presentation
display</dfn> refers to a graphical and/or audio output device
available to the user agent via an implementation specific connection
technology.
</p>
<p>
A <dfn data-lt=
"presentation connection|presentation connections">presentation
connection</dfn> is an object relating a <a>controlling browsing
context</a> to its <a>receiving browsing context</a> and enables
two-way-messaging between them. Each <a>presentation connection</a>
has a <dfn>presentation connection state</dfn>, a unique
<dfn data-lt="presentation identifier|presentation identifiers">presentation
identifier</dfn> to distinguish it from other <a>presentations</a>,
and a <dfn>presentation URL</dfn> that is a <a>URL</a> used to create
or reconnect to the <a href=
"#dfn-receiving-browsing-context">presentation</a>. A <dfn>valid
presentation identifier</dfn> consists of alphanumeric ASCII
characters only and is at least 16 characters long.
</p>
<p>
Some <a>presentation displays</a> may only be able to display a
subset of Web content because of functional, security or hardware
limitations. Examples are set-top boxes, smart TVs, or networked
speakers capable of rendering only audio. We say that such a display
is an <dfn data-lt=
"available presentation display|available presentation displays">available
presentation display</dfn> for a <a>presentation URL</a> if the
<a>controlling user agent</a> can reasonably guarantee that
presentation of the URL on that display will succeed.
</p>
<p>
A <dfn data-lt=
"controlling browsing context|controlling browsing contexts|controller">
controlling browsing context</dfn> (or <strong>controller</strong>
for short) is a <a>browsing context</a> that has connected to a
<a href="#dfn-receiving-browsing-context">presentation</a> by calling
<a data-link-for="PresentationRequest">start</a> or <a data-link-for=
"PresentationRequest">reconnect</a>, or received a <a>presentation
connection</a> via a <a data-link-for=
"PresentationRequest">connectionavailable</a> event. In algorithms
for <a>PresentationRequest</a>, the <a>controlling browsing
context</a> is the <a>browsing context</a> whose <a>JavaScript
realm</a> was used to construct the <a>PresentationRequest</a>.
</p>
<p>
The <dfn data-lt=
"receiving browsing context|receiving browsing contexts|presentations">
receiving browsing context</dfn> (or <strong>presentation</strong>
for short) is the browsing context responsible for rendering to a
<a>presentation display</a>. A <a>receiving browsing context</a> can
reside in the same user agent as the <a>controlling browsing
context</a> or a different one. A <a>receiving browsing context</a>
is created by following the steps to <a>create a receiving browsing
context</a>.
</p>
<p>
In a procedure, the <dfn>destination browsing context</dfn> is the
<a>receiving browsing context</a> when the procedure is initiated at
the <a>controlling browsing context</a>, or the <a>controlling
browsing context</a> if it is initiated at the <a>receiving browsing
context</a>.
</p>
<p>
The <dfn>set of controlled presentations</dfn>, initially empty,
contains the <a>presentation connections</a> created by the
<a>controlling browsing contexts</a> for the <a>controlling user
agent</a> (or a specific user profile within that user agent). The
<a>set of controlled presentations</a> is represented by a list of
<a>PresentationConnection</a> objects that represent the underlying
<a>presentation connections</a>. Several
<a>PresentationConnection</a> objects may share the same
<a>presentation URL</a> and <a>presentation identifier</a> in that
set, but there can be only one <a>PresentationConnection</a> with a
specific <a>presentation URL</a> and <a>presentation identifier</a>
for a given <a>controlling browsing context</a>.
</p>
<p>
The <dfn>set of presentation controllers</dfn>, initially empty,
contains the <a>presentation connections</a> created by a
<a>receiving browsing context</a> for the <a>receiving user
agent</a>. The <a>set of presentation controllers</a> is represented
by a list of <a>PresentationConnection</a> objects that represent the
underlying <a>presentation connections</a>. All <a>presentation
connections</a> in this set share the same <a>presentation URL</a>
and <a>presentation identifier</a>.
</p>
<p>
In a <a>receiving browsing context</a>, the <dfn>presentation
controllers monitor</dfn>, initially set to <code>null</code>,
exposes the current <a>set of presentation controllers</a> to the
receiving application. The <a>presentation controllers monitor</a> is
represented by a <a>PresentationConnectionList</a>.
</p>
<p>
In a <a>receiving browsing context</a>, the <dfn>presentation
controllers promise</dfn>, which is initially set to
<code>null</code>, provides the <a>presentation controllers
monitor</a> once the initial <a>presentation connection</a> is
established. The <a>presentation controllers promise</a> is
represented by a {{Promise}} that resolves with the <a>presentation
controllers monitor</a>.
</p>
<p>
In a <a>controlling browsing context</a>, the <dfn>default
presentation request</dfn>, which is initially set to
<code>null</code>, represents the request to use when the user wishes
to initiate a <a>presentation connection</a> from the browser chrome.
</p>
<p>
The <a>task source</a> for the tasks mentioned in this specification
is the <dfn>presentation task source</dfn>.
</p>
<p>
When an algorithm <dfn data-lt="queue a Presentation API task">queues
a Presentation API task</dfn> <var>T</var>, the <a>user agent</a>
MUST <a>queue a global task</a> <var>T</var> on the <a>presentation
task source</a> using the <a>global object</a> of the <a>current
realm</a>.
</p>
<p>
Unless otherwise specified, the <a>JavaScript realm</a> for script
objects constructed by algorithm steps is the <a>current realm</a>.
</p>
</section>
<section>
<h3>
Interface <dfn>Presentation</dfn>
</h3>
<pre class="idl">
partial interface Navigator {
[SecureContext, SameObject] readonly attribute Presentation presentation;
};
[SecureContext, Exposed=Window]
interface Presentation {
};
</pre>
<p>
The <dfn data-dfn-for="Navigator">presentation</dfn> attribute is
used to retrieve an instance of the <a>Presentation</a> interface. It
MUST return the <a>Presentation</a> instance.
</p>
<section>
<h4>
Controlling user agent
</h4>
<p>
<a data-lt="controlling user agent">Controlling user agents</a>
MUST implement the following partial interface:
</p>
<pre class="idl idl-controlling-ua">
partial interface Presentation {
attribute PresentationRequest? defaultRequest;
};
</pre>
<p>
The <dfn data-dfn-for=
"Presentation"><code>defaultRequest</code></dfn> attribute MUST
return the <a>default presentation request</a> if any,
<code>null</code> otherwise. On setting, the <a>default
presentation request</a> MUST be set to the new value.
</p>
<p>
The <a>controlling user agent</a> SHOULD initiate presentation
using the <a>default presentation request</a> only when the user
has expressed an intention to do so via a user gesture, for example
by clicking a button in the browser chrome.
</p>
<p>
To initiate presentation using the <a>default presentation
request</a>, the <a>controlling user agent</a> MUST follow the
steps to <a>start a presentation from a default presentation
request</a>.
</p>
<p>
Support for initiating a presentation using the <a>default
presentation request</a> is OPTIONAL.
</p>
<div class="note">
If a <a>controlling user agent</a> does not support <a>starting a
presentation from a default presentation request</a>, that user
agent should ignore any value set for <a data-link-for=
"Presentation">defaultRequest</a>.
</div>
</section>
<section>
<h4>
Receiving user agent
</h4>
<p>
<a data-lt="Receiving user agent">Receiving user agents</a> MUST
implement the following partial interface:
</p>
<pre class="idl idl-receiving-ua">
partial interface Presentation {
readonly attribute PresentationReceiver? receiver;
};
</pre>
<p>
The <dfn data-dfn-for="Presentation"><code>receiver</code></dfn>
attribute MUST return the <a>PresentationReceiver</a> instance
associated with the <a>receiving browsing context</a> and created
by the <a>receiving user agent</a> when the <a>receiving browsing
context</a> is <a data-lt=
"create a receiving browsing context">created</a>. In any other
<a>browsing context</a> (including <a data-lt=
"child navigable">child navigables</a> of the <a>receiving browsing
context</a>) it MUST return <code>null</code>.
</p>
<p class="note">
Web developers can use <a data-lt=
"Presentation.receiver">navigator.presentation.receiver</a> to
detect when a document is loaded as a presentation.
</p>
</section>
</section>
<section>
<h3>
Interface <dfn>PresentationRequest</dfn>
</h3>
<pre class="idl">
[SecureContext, Exposed=Window]
interface PresentationRequest : EventTarget {
constructor(USVString url);
constructor(sequence<USVString> urls);
Promise<PresentationConnection> start();
Promise<PresentationConnection> reconnect(USVString presentationId);
Promise<PresentationAvailability> getAvailability();
attribute EventHandler onconnectionavailable;
};
</pre>
<p>
A <a>PresentationRequest</a> object is associated with a request to
initiate or reconnect to a presentation made by a <a>controlling
browsing context</a>. The <a>PresentationRequest</a> object MUST be
implemented in a <a>controlling browsing context</a> provided by a
<a>controlling user agent</a>.
</p>
<p>
When a <a>PresentationRequest</a> is constructed, the given
<code>urls</code> MUST be used as the list of <dfn data-lt=
"presentation request URL">presentation request URLs</dfn> which are
each a possible <a>presentation URL</a> for the
<a>PresentationRequest</a> instance.
</p>
<section>
<h4>
Constructing a <a>PresentationRequest</a>
</h4>
<p>
When the <a>PresentationRequest</a> constructor is called, the
<a>controlling user agent</a> MUST run these steps:
</p>
<dl>
<dt>
Input
</dt>
<dd>
<var>url</var> or <var>urls</var>, the <a>presentation request
URLs</a>
</dd>
<dt>
Output
</dt>
<dd>
A new <a>PresentationRequest</a> object
</dd>
</dl>